Merge branch 'MDL-70476_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[moodle.git] / grade / export / grade_export_form.php
CommitLineData
e060e33d 1<?php
8ad36f4c 2
e060e33d 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/>.
8ad36f4c 17
bfebaf64
MD
18if (!defined('MOODLE_INTERNAL')) {
19 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
20}
21
3f8bcf7c 22require_once $CFG->libdir.'/formslib.php';
23
24class grade_export_form extends moodleform {
d93d67b7 25 function definition() {
5c75a0a3 26 global $CFG, $COURSE, $USER, $DB;
61c6071f 27
cca51baa
DW
28 $isdeprecatedui = false;
29
3f8bcf7c 30 $mform =& $this->_form;
ca82569b 31 if (isset($this->_customdata)) { // hardcoding plugin names here is hacky
32 $features = $this->_customdata;
0da010f7 33 } else {
ca82569b 34 $features = array();
0da010f7 35 }
97599c0a 36
cca51baa
DW
37 if (empty($features['simpleui'])) {
38 debugging('Grade export plugin needs updating to support one step exports.', DEBUG_DEVELOPER);
39 }
9871a438
DW
40
41 $mform->addElement('header', 'gradeitems', get_string('gradeitemsinc', 'grades'));
42 $mform->setExpanded('gradeitems', true);
43
6c6d05a6 44 if (!empty($features['idnumberrequired'])) {
6c6d05a6
DW
45 $mform->addElement('static', 'idnumberwarning', get_string('useridnumberwarning', 'grades'));
46 }
cca51baa 47
9871a438
DW
48 $switch = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);
49
50 // Grab the grade_seq for this course
51 $gseq = new grade_seq($COURSE->id, $switch);
52
53 if ($grade_items = $gseq->items) {
54 $needs_multiselect = false;
55 $canviewhidden = has_capability('moodle/grade:viewhidden', context_course::instance($COURSE->id));
56
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;
61 }
62
63 if (!empty($features['idnumberrequired']) and empty($grade_item->idnumber)) {
b52b1ee9 64 $mform->addElement('checkbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), get_string('noidnumber', 'grades'));
9871a438
DW
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;
70 }
71 }
72
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
75 }
76 }
77
78
cca51baa
DW
79 $mform->addElement('header', 'options', get_string('exportformatoptions', 'grades'));
80 if (!empty($features['simpleui'])) {
81 $mform->setExpanded('options', false);
82 }
97599c0a 83
caffc55a 84 $mform->addElement('advcheckbox', 'export_feedback', get_string('exportfeedback', 'grades'));
8284ffed
KW
85 $exportfeedback = isset($CFG->grade_export_exportfeedback) ? $CFG->grade_export_exportfeedback : 0;
86 $mform->setDefault('export_feedback', $exportfeedback);
38c1dd19
RT
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);
97 }
78ab98bc 98
cca51baa
DW
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);
102 }
103
104
5c75a0a3 105
94c68814 106 if (!empty($features['updategradesonly'])) {
107 $mform->addElement('advcheckbox', 'updatedgradesonly', get_string('updatedgradesonly', 'grades'));
108 }
864d1f8c 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'));
5c75a0a3 114
864d1f8c 115 /*
116 foreach ($options as $key=>$option) {
117 if ($key == $default_gradedisplaytype) {
118 $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
119 break;
120 }
121 }
122 */
be289f57
SL
123 if ($features['multipledisplaytypes']) {
124 /*
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.
127 */
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);
140 }
5c75a0a3 141
864d1f8c 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);
147 /*
148 if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
149 $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
150 }
151 */
152
ca82569b 153 if (!empty($features['includeseparator'])) {
154 $radio = array();
f20edd52
PS
155 $radio[] = $mform->createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab');
156 $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma');
bb2306eb
AG
157 $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcolon', 'grades'), 'colon');
158 $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepsemicolon', 'grades'), 'semicolon');
ca82569b 159 $mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false);
160 $mform->setDefault('separator', 'comma');
161 }
0eeada79 162
caffc55a 163 if (!empty($CFG->gradepublishing) and !empty($features['publishing'])) {
cca51baa
DW
164 $mform->addElement('header', 'publishing', get_string('publishingoptions', 'grades'));
165 if (!empty($features['simpleui'])) {
166 $mform->setExpanded('publishing', false);
167 }
ca82569b 168 $options = array(get_string('nopublish', 'grades'), get_string('createnewkey', 'userkey'));
5c75a0a3 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) {
ca82569b 172 foreach ($keys as $key) {
173 $options[$key->value] = $key->value; // TODO: add more details - ip restriction, valid until ??
174 }
175 }
176 $mform->addElement('select', 'key', get_string('userkey', 'userkey'), $options);
2e98f5ac 177 $mform->addHelpButton('key', 'userkey', 'userkey');
ca82569b 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>');
caffc55a 180
ca82569b 181 $mform->addElement('text', 'iprestriction', get_string('keyiprestriction', 'userkey'), array('size'=>80));
2e98f5ac 182 $mform->addHelpButton('iprestriction', 'keyiprestriction', 'userkey');
662a057c 183 $mform->setDefault('iprestriction', getremoteaddr()); // own IP - just in case somebody does not know what user key is
c597ff32 184 $mform->setType('iprestriction', PARAM_RAW_TRIMMED);
caffc55a 185
ca82569b 186 $mform->addElement('date_time_selector', 'validuntil', get_string('keyvaliduntil', 'userkey'), array('optional'=>true));
2e98f5ac 187 $mform->addHelpButton('validuntil', 'keyvaliduntil', 'userkey');
662a057c 188 $mform->setDefault('validuntil', time()+3600*24*7); // only 1 week default duration - just in case somebody does not know what user key is
c597ff32 189 $mform->setType('validuntil', PARAM_INT);
662a057c 190
8925adab 191 $mform->disabledIf('iprestriction', 'key', 'noteq', 1);
192 $mform->disabledIf('validuntil', 'key', 'noteq', 1);
ca82569b 193 }
86655fb6 194
ca82569b 195 $mform->addElement('hidden', 'id', $COURSE->id);
d18e0fe6 196 $mform->setType('id', PARAM_INT);
cca51baa
DW
197 $submitstring = get_string('download');
198 if (empty($features['simpleui'])) {
199 $submitstring = get_string('submit');
e82aa043
SL
200 } else if (!empty($CFG->gradepublishing)) {
201 $submitstring = get_string('export', 'grades');
cca51baa 202 }
6073a598 203
cca51baa 204 $this->add_action_buttons(false, $submitstring);
3f8bcf7c 205 }
be289f57
SL
206
207 /**
208 * Overrides the mform get_data method.
209 *
210 * Created to force a value since the validation method does not work with multiple checkbox.
211 *
212 * @return stdClass form data object.
213 */
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;
226 }
227 }
228 }
229 return $data;
230 }
3f8bcf7c 231}
6c3ef410 232