fixed problem with quickgrading overriding nonexistent grades with no grade
[moodle.git] / grade / import / grade_import_form.php
CommitLineData
7f999ccb 1<?php // $Id$
2require_once $CFG->libdir.'/formslib.php';
3
4class grade_import_form extends moodleform {
5 function definition (){
6 $mform =& $this->_form;
7
8 // course id needs to be passed for auth purposes
9 $mform->addElement('hidden', 'id', optional_param('id'));
eff9c473 10 $mform->setType('id', PARAM_INT);
f115f8c8 11 $mform->addElement('header', 'general', get_string('importfile', 'grades'));
7f999ccb 12 // file upload
13 $mform->addElement('file', 'userfile', get_string('file'));
eff9c473 14 $mform->setType('userfile', PARAM_FILE);
7f999ccb 15 $mform->addRule('userfile', null, 'required');
4d40aa14 16 $textlib = new textlib();
17 $encodings = $textlib->get_encodings();
f115f8c8 18 $mform->addElement('select', 'encoding', get_string('encoding', 'grades'), $encodings);
7f999ccb 19
4d40aa14 20 $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
21 $mform->addElement('select', 'previewrows', 'Preview rows', $options); // TODO: localize
eff9c473 22 $mform->setType('previewrows', PARAM_INT);
f115f8c8 23 $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
7f999ccb 24 }
25
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'];
e4add64d 30 } else{
7f999ccb 31 return NULL;
32 }
33 }
34}
85e287de 35
85e287de 36class grade_import_mapping_form extends moodleform {
3f8bcf7c 37
85e287de 38 function definition () {
3f8bcf7c 39 global $CFG;
85e287de 40 $mform =& $this->_form;
41
3f8bcf7c 42 // this is an array of headers
43 $header = $this->_customdata['header'];
44 // temporary filename
45 $filename = $this->_customdata['filename'];
46 // course id
85e287de 47
f115f8c8 48 $mform->addElement('header', 'general', get_string('identifier', 'grades'));
3f8bcf7c 49 $mapfromoptions = array();
ffe8ee55 50
3f8bcf7c 51 if ($header) {
b4355273 52 foreach ($header as $i=>$h) {
5c2aa198 53 $mapfromoptions[$i] = s($h);
3f8bcf7c 54 }
55 }
f115f8c8 56 $mform->addElement('select', 'mapfrom', get_string('mapfrom', 'grades'), $mapfromoptions);
3f8bcf7c 57 //choose_from_menu($mapfromoptions, 'mapfrom');
58
59 $maptooptions = array('userid'=>'userid', 'username'=>'username', 'useridnumber'=>'useridnumber', 'useremail'=>'useremail', '0'=>'ignore');
60 //choose_from_menu($maptooptions, 'mapto');
f115f8c8 61 $mform->addElement('select', 'mapto', get_string('mapto', 'grades'), $maptooptions);
3f8bcf7c 62
f115f8c8 63 $mform->addElement('header', 'general', get_string('mappings', 'grades'));
3f8bcf7c 64
ffe8ee55 65 // add a comment option
66
67 if ($gradeitems = $this->_customdata['gradeitems']) {
68 $comments = array();
69 foreach ($gradeitems as $itemid => $itemname) {
70 $comments['feedback_'.$itemid] = 'comments for '.$itemname;
71 }
72 }
b4355273 73
3f8bcf7c 74 include_once($CFG->libdir.'/gradelib.php');
3f8bcf7c 75
b4355273 76 if ($header) {
77 $i = 0; // index
3f8bcf7c 78 foreach ($header as $h) {
3f8bcf7c 79
ffe8ee55 80 $h = trim($h);
81 // this is what each header maps to
82 $mform->addElement('selectgroups',
83 'mapping_'.$i, s($h),
84 array('others'=>array('0'=>'ignore', 'new'=>'new gradeitem'),
85 'gradeitems'=>$gradeitems,
86 'comments'=>$comments));
b4355273 87 $i++;
3f8bcf7c 88 }
85e287de 89 }
ffe8ee55 90
b4355273 91 // find a non-conflicting file name based on time stamp
3f8bcf7c 92 $newfilename = 'cvstemp_'.time();
b4355273 93 while (file_exists($CFG->dataroot.'/temp/'.$newfilename)) {
94 $newfilename = 'cvstemp_'.time();
95 }
ffe8ee55 96
b4355273 97 // move the uploaded file
3f8bcf7c 98 move_uploaded_file($filename, $CFG->dataroot.'/temp/'.$newfilename);
ffe8ee55 99
3f8bcf7c 100 // course id needs to be passed for auth purposes
101 $mform->addElement('hidden', 'map', 1);
eff9c473 102 $mform->setType('map', PARAM_INT);
3f8bcf7c 103 $mform->addElement('hidden', 'id', optional_param('id'));
eff9c473 104 $mform->setType('id', PARAM_INT);
3f8bcf7c 105 //echo '<input name="filename" value='.$newfilename.' type="hidden" />';
106 $mform->addElement('hidden', 'filename', $newfilename);
eff9c473 107 $mform->setType('filename', PARAM_FILE);
f115f8c8 108 $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
ffe8ee55 109
85e287de 110 }
111}
7f999ccb 112?>