latest tags
[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'];
30 }else{
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;
40
85e287de 41 $mform =& $this->_form;
42
3f8bcf7c 43 // this is an array of headers
44 $header = $this->_customdata['header'];
45 // temporary filename
46 $filename = $this->_customdata['filename'];
47 // course id
85e287de 48
f115f8c8 49 $mform->addElement('header', 'general', get_string('identifier', 'grades'));
3f8bcf7c 50 $mapfromoptions = array();
51
52 if ($header) {
b4355273 53 foreach ($header as $i=>$h) {
54 $mapfromoptions[$i] = $h;
3f8bcf7c 55 }
56 }
f115f8c8 57 $mform->addElement('select', 'mapfrom', get_string('mapfrom', 'grades'), $mapfromoptions);
3f8bcf7c 58 //choose_from_menu($mapfromoptions, 'mapfrom');
59
60 $maptooptions = array('userid'=>'userid', 'username'=>'username', 'useridnumber'=>'useridnumber', 'useremail'=>'useremail', '0'=>'ignore');
61 //choose_from_menu($maptooptions, 'mapto');
f115f8c8 62 $mform->addElement('select', 'mapto', get_string('mapto', 'grades'), $maptooptions);
3f8bcf7c 63
f115f8c8 64 $mform->addElement('header', 'general', get_string('mappings', 'grades'));
3f8bcf7c 65
b4355273 66 $gradeitems = $this->_customdata['gradeitems'];
67
3f8bcf7c 68 include_once($CFG->libdir.'/gradelib.php');
3f8bcf7c 69
b4355273 70 if ($header) {
71 $i = 0; // index
3f8bcf7c 72 foreach ($header as $h) {
b4355273 73
3f8bcf7c 74 $h = trim($h);
75 // this is the order of the headers
b4355273 76 // $mform->addElement('hidden', 'maps[]', $h);
77
78 // this is what they map to
79 $mapfromoptions = array('0'=>'ignore', 'new'=>'new gradeitem') + $gradeitems;
3f8bcf7c 80
b4355273 81 $mform->addElement('select', 'mapping_'.$i, s($h), $mapfromoptions);
82 $i++;
3f8bcf7c 83 }
85e287de 84 }
b4355273 85
86 // find a non-conflicting file name based on time stamp
3f8bcf7c 87 $newfilename = 'cvstemp_'.time();
b4355273 88 while (file_exists($CFG->dataroot.'/temp/'.$newfilename)) {
89 $newfilename = 'cvstemp_'.time();
90 }
91
92 // move the uploaded file
3f8bcf7c 93 move_uploaded_file($filename, $CFG->dataroot.'/temp/'.$newfilename);
94
95 // course id needs to be passed for auth purposes
96 $mform->addElement('hidden', 'map', 1);
eff9c473 97 $mform->setType('map', PARAM_INT);
3f8bcf7c 98 $mform->addElement('hidden', 'id', optional_param('id'));
eff9c473 99 $mform->setType('id', PARAM_INT);
3f8bcf7c 100 //echo '<input name="filename" value='.$newfilename.' type="hidden" />';
101 $mform->addElement('hidden', 'filename', $newfilename);
eff9c473 102 $mform->setType('filename', PARAM_FILE);
f115f8c8 103 $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
85e287de 104
85e287de 105 }
106}
7f999ccb 107?>