Merge branch 'MDL-79664' of https://github.com/paulholden/moodle
[moodle.git] / grade / export / grade_export_form.php
blobc704cefa5fc8a4c4d181da87152957b22e78fefd
1 <?php
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.
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/>.
18 if (!defined('MOODLE_INTERNAL')) {
19 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
22 require_once $CFG->libdir.'/formslib.php';
24 class grade_export_form extends moodleform {
25 function definition() {
26 global $CFG, $COURSE, $USER, $DB;
28 $isdeprecatedui = false;
30 $mform =& $this->_form;
31 if (isset($this->_customdata)) { // hardcoding plugin names here is hacky
32 $features = $this->_customdata;
33 } else {
34 $features = array();
37 if (empty($features['simpleui'])) {
38 debugging('Grade export plugin needs updating to support one step exports.', DEBUG_DEVELOPER);
41 $mform->addElement('header', 'gradeitems', get_string('gradeitemsinc', 'grades'));
42 $mform->setExpanded('gradeitems', true);
44 if (!empty($features['idnumberrequired'])) {
45 $mform->addElement('static', 'idnumberwarning', get_string('useridnumberwarning', 'grades'));
48 $switch = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);
50 // Grab the grade_seq for this course
51 $gseq = new grade_seq($COURSE->id, $switch);
53 if ($grade_items = $gseq->items) {
54 $needs_multiselect = false;
55 $canviewhidden = has_capability('moodle/grade:viewhidden', context_course::instance($COURSE->id));
57 foreach ($grade_items as $grade_item) {
58 // Is the grade_item hidden? If so, can the user see hidden grade_items?
59 if ($grade_item->is_hidden() && !$canviewhidden) {
60 continue;
63 if (!empty($features['idnumberrequired']) and empty($grade_item->idnumber)) {
64 $mform->addElement('checkbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), get_string('noidnumber', 'grades'));
65 $mform->hardFreeze('itemids['.$grade_item->id.']');
66 } else {
67 $mform->addElement('advcheckbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), null, array('group' => 1));
68 $mform->setDefault('itemids['.$grade_item->id.']', 1);
69 $needs_multiselect = true;
73 if ($needs_multiselect) {
74 $this->add_checkbox_controller(1, null, null, 1); // 1st argument is group name, 2nd is link text, 3rd is attributes and 4th is original value
79 $mform->addElement('header', 'options', get_string('exportformatoptions', 'grades'));
80 if (!empty($features['simpleui'])) {
81 $mform->setExpanded('options', false);
84 $mform->addElement('advcheckbox', 'export_feedback', get_string('exportfeedback', 'grades'));
85 $exportfeedback = isset($CFG->grade_export_exportfeedback) ? $CFG->grade_export_exportfeedback : 0;
86 $mform->setDefault('export_feedback', $exportfeedback);
87 $coursecontext = context_course::instance($COURSE->id);
88 if (has_capability('moodle/course:viewsuspendedusers', $coursecontext)) {
89 $mform->addElement('advcheckbox', 'export_onlyactive', get_string('exportonlyactive', 'grades'));
90 $mform->setType('export_onlyactive', PARAM_BOOL);
91 $mform->setDefault('export_onlyactive', 1);
92 $mform->addHelpButton('export_onlyactive', 'exportonlyactive', 'grades');
93 } else {
94 $mform->addElement('hidden', 'export_onlyactive', 1);
95 $mform->setType('export_onlyactive', PARAM_BOOL);
96 $mform->setConstant('export_onlyactive', 1);
99 if (empty($features['simpleui'])) {
100 $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
101 $mform->addElement('select', 'previewrows', get_string('previewrows', 'grades'), $options);
106 if (!empty($features['updategradesonly'])) {
107 $mform->addElement('advcheckbox', 'updatedgradesonly', get_string('updatedgradesonly', 'grades'));
109 /// selections for decimal points and format, MDL-11667, defaults to site settings, if set
110 //$default_gradedisplaytype = $CFG->grade_export_displaytype;
111 $options = array(GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
112 GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
113 GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'));
116 foreach ($options as $key=>$option) {
117 if ($key == $default_gradedisplaytype) {
118 $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
119 break;
123 if ($features['multipledisplaytypes']) {
125 * Using advcheckbox because we need the grade display type (name) as key and grade display type (constant) as value.
126 * The method format_column_name requires the lang file string and the format_grade method requires the constant.
128 $checkboxes = array();
129 $checkboxes[] = $mform->createElement('advcheckbox', 'display[real]', null, get_string('real', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_REAL));
130 $checkboxes[] = $mform->createElement('advcheckbox', 'display[percentage]', null, get_string('percentage', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_PERCENTAGE));
131 $checkboxes[] = $mform->createElement('advcheckbox', 'display[letter]', null, get_string('letter', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_LETTER));
132 $mform->addGroup($checkboxes, 'displaytypes', get_string('gradeexportdisplaytypes', 'grades'), ' ', false);
133 $mform->setDefault('display[real]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_REAL);
134 $mform->setDefault('display[percentage]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_PERCENTAGE);
135 $mform->setDefault('display[letter]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_LETTER);
136 } else {
137 // Only used by XML grade export format.
138 $mform->addElement('select', 'display', get_string('gradeexportdisplaytype', 'grades'), $options);
139 $mform->setDefault('display', $CFG->grade_export_displaytype);
142 //$default_gradedecimals = $CFG->grade_export_decimalpoints;
143 $options = array(0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
144 $mform->addElement('select', 'decimals', get_string('gradeexportdecimalpoints', 'grades'), $options);
145 $mform->setDefault('decimals', $CFG->grade_export_decimalpoints);
146 $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
148 if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
149 $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
153 if (!empty($features['includeseparator'])) {
154 $radio = array();
155 $radio[] = $mform->createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab');
156 $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma');
157 $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcolon', 'grades'), 'colon');
158 $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepsemicolon', 'grades'), 'semicolon');
159 $mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false);
160 $mform->setDefault('separator', 'comma');
163 if (!empty($CFG->gradepublishing) and !empty($features['publishing'])) {
164 $mform->addElement('header', 'publishing', get_string('publishingoptions', 'grades'));
165 if (!empty($features['simpleui'])) {
166 $mform->setExpanded('publishing', false);
168 $options = array(get_string('nopublish', 'grades'), get_string('createnewkey', 'userkey'));
169 $keys = $DB->get_records_select('user_private_key', "script='grade/export' AND instance=? AND userid=?",
170 array($COURSE->id, $USER->id));
171 if ($keys) {
172 foreach ($keys as $key) {
173 $options[$key->value] = $key->value; // TODO: add more details - ip restriction, valid until ??
176 $mform->addElement('select', 'key', get_string('userkey', 'userkey'), $options);
177 $mform->addHelpButton('key', 'userkey', 'userkey');
178 $mform->addElement('static', 'keymanagerlink', get_string('keymanager', 'userkey'),
179 '<a href="'.$CFG->wwwroot.'/grade/export/keymanager.php?id='.$COURSE->id.'">'.get_string('keymanager', 'userkey').'</a>');
181 $mform->addElement('text', 'iprestriction', get_string('keyiprestriction', 'userkey'), array('size'=>80));
182 $mform->addHelpButton('iprestriction', 'keyiprestriction', 'userkey');
183 $mform->setDefault('iprestriction', getremoteaddr()); // own IP - just in case somebody does not know what user key is
184 $mform->setType('iprestriction', PARAM_RAW_TRIMMED);
186 $mform->addElement('date_time_selector', 'validuntil', get_string('keyvaliduntil', 'userkey'), array('optional'=>true));
187 $mform->addHelpButton('validuntil', 'keyvaliduntil', 'userkey');
188 $mform->setDefault('validuntil', time()+3600*24*7); // only 1 week default duration - just in case somebody does not know what user key is
189 $mform->setType('validuntil', PARAM_INT);
191 $mform->disabledIf('iprestriction', 'key', 'noteq', 1);
192 $mform->disabledIf('validuntil', 'key', 'noteq', 1);
195 $mform->addElement('hidden', 'id', $COURSE->id);
196 $mform->setType('id', PARAM_INT);
197 $submitstring = get_string('download');
198 if (empty($features['simpleui'])) {
199 $submitstring = get_string('submit');
200 } else if (!empty($CFG->gradepublishing)) {
201 $submitstring = get_string('export', 'grades');
204 $this->add_sticky_action_buttons(false, $submitstring);
208 * Overrides the mform get_data method.
210 * Created to force a value since the validation method does not work with multiple checkbox.
212 * @return stdClass form data object.
214 public function get_data() {
215 global $CFG;
216 $data = parent::get_data();
217 if ($data && $this->_customdata['multipledisplaytypes']) {
218 if (count(array_filter($data->display)) == 0) {
219 // Ensure that a value was selected as the export plugins expect at least one value.
220 if ($CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_LETTER) {
221 $data->display['letter'] = GRADE_DISPLAY_TYPE_LETTER;
222 } else if ($CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_PERCENTAGE) {
223 $data->display['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
224 } else {
225 $data->display['real'] = GRADE_DISPLAY_TYPE_REAL;
229 return $data;