1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
18if (!defined('MOODLE_INTERNAL')) {
19    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
20}
21
22require_once $CFG->libdir.'/formslib.php';
23require_once($CFG->libdir.'/gradelib.php');
24
25class grade_import_form extends moodleform {
26    function definition (){
27        global $COURSE;
28
29        $mform =& $this->_form;
30
31        if (isset($this->_customdata)) {  // hardcoding plugin names here is hacky
32            $features = $this->_customdata;
33        } else {
34            $features = array();
35        }
36
37        // course id needs to be passed for auth purposes
38        $mform->addElement('hidden', 'id', optional_param('id', 0, PARAM_INT));
39        $mform->setType('id', PARAM_INT);
40        $mform->addElement('header', 'general', get_string('importfile', 'grades'));
41
42        // Restrict the possible upload file types.
43        if (!empty($features['acceptedtypes'])) {
44            $acceptedtypes = $features['acceptedtypes'];
45        } else {
46            $acceptedtypes = '*';
47        }
48
49        // File upload.
50        $mform->addElement('filepicker', 'userfile', get_string('file'), null, array('accepted_types' => $acceptedtypes));
51        $mform->addRule('userfile', null, 'required');
52        $encodings = core_text::get_encodings();
53        $mform->addElement('select', 'encoding', get_string('encoding', 'grades'), $encodings);
54        $mform->addHelpButton('encoding', 'encoding', 'grades');
55
56        if (!empty($features['includeseparator'])) {
57            $radio = array();
58            $radio[] = $mform->createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab');
59            $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma');
60            $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcolon', 'grades'), 'colon');
61            $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepsemicolon', 'grades'), 'semicolon');
62            $mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false);
63            $mform->addHelpButton('separator', 'separator', 'grades');
64            $mform->setDefault('separator', 'comma');
65        }
66
67        if (!empty($features['verbosescales'])) {
68            $options = array(1=>get_string('yes'), 0=>get_string('no'));
69            $mform->addElement('select', 'verbosescales', get_string('verbosescales', 'grades'), $options);
70            $mform->addHelpButton('verbosescales', 'verbosescales', 'grades');
71        }
72
73        $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
74        $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'grades'), $options); // TODO: localize
75        $mform->addHelpButton('previewrows', 'rowpreviewnum', 'grades');
76        $mform->setType('previewrows', PARAM_INT);
77        $mform->addElement('checkbox', 'forceimport', get_string('forceimport', 'grades'));
78        $mform->addHelpButton('forceimport', 'forceimport', 'grades');
79        $mform->setDefault('forceimport', false);
80        $mform->setType('forceimport', PARAM_BOOL);
81        $mform->addElement('hidden', 'groupid', groups_get_course_group($COURSE));
82        $mform->setType('groupid', PARAM_INT);
83        $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
84    }
85}
86
87class grade_import_mapping_form extends moodleform {
88
89    function definition () {
90        global $CFG, $COURSE;
91        $mform =& $this->_form;
92
93        // this is an array of headers
94        $header = $this->_customdata['header'];
95        // course id
96
97        $mform->addElement('header', 'general', get_string('identifier', 'grades'));
98        $mapfromoptions = array();
99
100        if ($header) {
101            foreach ($header as $i=>$h) {
102                $mapfromoptions[$i] = s($h);
103            }
104        }
105        $mform->addElement('select', 'mapfrom', get_string('mapfrom', 'grades'), $mapfromoptions);
106        $mform->addHelpButton('mapfrom', 'mapfrom', 'grades');
107
108        $maptooptions = array(
109            'userid'       => get_string('userid', 'grades'),
110            'username'     => get_string('username'),
111            'useridnumber' => get_string('idnumber'),
112            'useremail'    => get_string('email'),
113            '0'            => get_string('ignore', 'grades')
114        );
115        $mform->addElement('select', 'mapto', get_string('mapto', 'grades'), $maptooptions);
116
117        $mform->addHelpButton('mapto', 'mapto', 'grades');
118        $mform->addElement('header', 'general_map', get_string('mappings', 'grades'));
119        $mform->addHelpButton('general_map', 'mappings', 'grades');
120
121        // Add a feedback option.
122        $feedbacks = array();
123        if ($gradeitems = $this->_customdata['gradeitems']) {
124            foreach ($gradeitems as $itemid => $itemname) {
125                $feedbacks['feedback_'.$itemid] = get_string('feedbackforgradeitems', 'grades', $itemname);
126            }
127        }
128
129        if ($header) {
130            $i = 0; // index
131            foreach ($header as $h) {
132                $h = trim($h);
133                // This is what each header maps to.
134                $headermapsto = array(
135                    get_string('others', 'grades')     => array(
136                        '0'   => get_string('ignore', 'grades'),
137                        'new' => get_string('newitem', 'grades')
138                    ),
139                    get_string('gradeitems', 'grades') => $gradeitems,
140                    get_string('feedbacks', 'grades')  => $feedbacks
141                );
142                $mform->addElement('selectgroups', 'mapping_'.$i, s($h), $headermapsto);
143                $i++;
144            }
145        }
146
147        // course id needs to be passed for auth purposes
148        $mform->addElement('hidden', 'map', 1);
149        $mform->setType('map', PARAM_INT);
150        $mform->setConstant('map', 1);
151        $mform->addElement('hidden', 'id', $this->_customdata['id']);
152        $mform->setType('id', PARAM_INT);
153        $mform->setConstant('id', $this->_customdata['id']);
154        $mform->addElement('hidden', 'iid', $this->_customdata['iid']);
155        $mform->setType('iid', PARAM_INT);
156        $mform->setConstant('iid', $this->_customdata['iid']);
157        $mform->addElement('hidden', 'importcode', $this->_customdata['importcode']);
158        $mform->setType('importcode', PARAM_FILE);
159        $mform->setConstant('importcode', $this->_customdata['importcode']);
160        $mform->addElement('hidden', 'verbosescales', 1);
161        $mform->setType('verbosescales', PARAM_INT);
162        $mform->setConstant('verbosescales', $this->_customdata['verbosescales']);
163        $mform->addElement('hidden', 'groupid', groups_get_course_group($COURSE));
164        $mform->setType('groupid', PARAM_INT);
165        $mform->setConstant('groupid', groups_get_course_group($COURSE));
166        $mform->addElement('hidden', 'forceimport', $this->_customdata['forceimport']);
167        $mform->setType('forceimport', PARAM_BOOL);
168        $mform->setConstant('forceimport', $this->_customdata['forceimport']);
169        $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
170
171    }
172}
173