2 require_once $CFG->libdir
.'/formslib.php';
4 class grade_import_form
extends moodleform
{
5 function definition (){
6 $mform =& $this->_form
;
8 // course id needs to be passed for auth purposes
9 $mform->addElement('hidden', 'id', optional_param('id'));
10 $mform->setType('id', PARAM_INT
);
11 $mform->addElement('header', 'general', get_string('importfile', 'grades'));
13 $mform->addElement('file', 'userfile', get_string('file'));
14 $mform->setType('userfile', PARAM_FILE
);
15 $mform->addRule('userfile', null, 'required');
16 $textlib = textlib_get_instance();
17 $encodings = $textlib->get_encodings();
18 $mform->addElement('select', 'encoding', get_string('encoding', 'grades'), $encodings);
20 $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
21 $mform->addElement('select', 'previewrows', 'Preview rows', $options); // TODO: localize
22 $mform->setType('previewrows', PARAM_INT
);
23 $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
26 function get_userfile_name(){
27 if ($this->is_submitted() and $this->is_validated()) {
28 // return the temporary filename to process
29 return $this->_upload_manager
->files
['userfile']['tmp_name'];
36 class grade_import_mapping_form
extends moodleform
{
38 function definition () {
40 $mform =& $this->_form
;
42 // this is an array of headers
43 $header = $this->_customdata
['header'];
45 $filename = $this->_customdata
['filename'];
48 $mform->addElement('header', 'general', get_string('identifier', 'grades'));
49 $mapfromoptions = array();
52 foreach ($header as $i=>$h) {
53 $mapfromoptions[$i] = s($h);
56 $mform->addElement('select', 'mapfrom', get_string('mapfrom', 'grades'), $mapfromoptions);
57 //choose_from_menu($mapfromoptions, 'mapfrom');
59 $maptooptions = array('userid'=>'userid', 'username'=>'username', 'useridnumber'=>'useridnumber', 'useremail'=>'useremail', '0'=>'ignore');
60 //choose_from_menu($maptooptions, 'mapto');
61 $mform->addElement('select', 'mapto', get_string('mapto', 'grades'), $maptooptions);
63 $mform->addElement('header', 'general', get_string('mappings', 'grades'));
65 // add a comment option
67 if ($gradeitems = $this->_customdata
['gradeitems']) {
69 foreach ($gradeitems as $itemid => $itemname) {
70 $comments['feedback_'.$itemid] = 'comments for '.$itemname;
74 include_once($CFG->libdir
.'/gradelib.php');
78 foreach ($header as $h) {
81 // this is what each header maps to
82 $mform->addElement('selectgroups',
84 array('others'=>array('0'=>'ignore', 'new'=>'new gradeitem'),
85 'gradeitems'=>$gradeitems,
86 'comments'=>$comments));
91 // find a non-conflicting file name based on time stamp
92 $newfilename = 'cvstemp_'.time();
93 while (file_exists($CFG->dataroot
.'/temp/'.$newfilename)) {
94 $newfilename = 'cvstemp_'.time();
97 // move the uploaded file
98 move_uploaded_file($filename, $CFG->dataroot
.'/temp/'.$newfilename);
100 // course id needs to be passed for auth purposes
101 $mform->addElement('hidden', 'map', 1);
102 $mform->setType('map', PARAM_INT
);
103 $mform->addElement('hidden', 'id', optional_param('id'));
104 $mform->setType('id', PARAM_INT
);
105 //echo '<input name="filename" value='.$newfilename.' type="hidden" />';
106 $mform->addElement('hidden', 'filename', $newfilename);
107 $mform->setType('filename', PARAM_FILE
);
108 $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));