Merged fix for MDL-9041 (undo)
[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'));
4d40aa14 10 $mform->addElement('header', 'general', get_string('importfile'));
7f999ccb 11 // file upload
12 $mform->addElement('file', 'userfile', get_string('file'));
13 $mform->addRule('userfile', null, 'required');
4d40aa14 14 $textlib = new textlib();
15 $encodings = $textlib->get_encodings();
16 $mform->addElement('select', 'encoding', get_string('encoding'), $encodings);
7f999ccb 17
4d40aa14 18 $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
19 $mform->addElement('select', 'previewrows', 'Preview rows', $options); // TODO: localize
7f999ccb 20 $this->add_action_buttons(false, get_string('uploadgrades'));
21 }
22
23 function get_userfile_name(){
24 if ($this->is_submitted() and $this->is_validated()) {
25 // return the temporary filename to process
26 return $this->_upload_manager->files['userfile']['tmp_name'];
27 }else{
28 return NULL;
29 }
30 }
31}
85e287de 32
85e287de 33class grade_import_mapping_form extends moodleform {
3f8bcf7c 34
85e287de 35 function definition () {
3f8bcf7c 36 global $CFG;
37
85e287de 38 $mform =& $this->_form;
39
3f8bcf7c 40 // this is an array of headers
41 $header = $this->_customdata['header'];
42 // temporary filename
43 $filename = $this->_customdata['filename'];
44 // course id
45 $id = $this->_customdata['id'];
85e287de 46
3f8bcf7c 47 $mform->addElement('header', 'general', get_string('identifier'));
48 $mapfromoptions = array();
49
50 if ($header) {
51 foreach ($header as $h) {
52 $mapfromoptions[$h] = $h;
53 }
54 }
55 $mform->addElement('select', 'mapfrom', get_string('mapfrom'), $mapfromoptions);
56 //choose_from_menu($mapfromoptions, 'mapfrom');
57
58 $maptooptions = array('userid'=>'userid', 'username'=>'username', 'useridnumber'=>'useridnumber', 'useremail'=>'useremail', '0'=>'ignore');
59 //choose_from_menu($maptooptions, 'mapto');
60 $mform->addElement('select', 'mapto', get_string('mapto'), $maptooptions);
61
62 $mform->addElement('header', 'general', get_string('mappings'));
63
64 $gradeitems = array();
85e287de 65
3f8bcf7c 66 include_once($CFG->libdir.'/gradelib.php');
67
68 if ($id) {
69 if ($grade_items = grade_get_items($id)) {
70 foreach ($grade_items as $grade_item) {
71 $gradeitems[$grade_item->idnumber] = $grade_item->itemname;
72 }
85e287de 73 }
74 }
3f8bcf7c 75
76 if ($header) {
77 foreach ($header as $h) {
78
79 $h = trim($h);
80 // this is the order of the headers
81 $mform->addElement('hidden', 'maps[]', $h);
82 //echo '<input type="hidden" name="maps[]" value="'.$h.'"/>';
83 // this is what they map to
84
b89ef1db 85 $mapfromoptions = array_merge(array('0'=>'ignore', 'new'=>'new gradeitem'), $gradeitems);
3f8bcf7c 86 $mform->addElement('select', 'mapping[]', $h, $mapfromoptions);
87 //choose_from_menu($mapfromoptions, 'mapping[]', $h);
88
89 }
85e287de 90 }
3f8bcf7c 91 $newfilename = 'cvstemp_'.time();
92 move_uploaded_file($filename, $CFG->dataroot.'/temp/'.$newfilename);
93
94 // course id needs to be passed for auth purposes
95 $mform->addElement('hidden', 'map', 1);
96 $mform->addElement('hidden', 'id', optional_param('id'));
97 //echo '<input name="filename" value='.$newfilename.' type="hidden" />';
98 $mform->addElement('hidden', 'filename', $newfilename);
99
100 $this->add_action_buttons(false, get_string('uploadgrades'));
85e287de 101
85e287de 102 }
103}
7f999ccb 104?>