MDL-22388 Added some checks to kill these scripts dead with an unequivocal notice...
[moodle.git] / grade / report / grader / preferences_form.php
CommitLineData
e060e33d 1<?php
606af46f 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
cae83708 18
19/**
20 * Form for grader report preferences.
21 *
22 * @package moodlecore
23 * @subpackage grade
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
bfebaf64
MD
28if (!defined('MOODLE_INTERNAL')) {
29 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
30}
31
606af46f 32require_once($CFG->libdir.'/formslib.php');
33
34/**
35 * First implementation of the preferences in the form of a moodleform.
606af46f 36 * TODO add "reset to site defaults" button
606af46f 37 */
38class grader_report_preferences_form extends moodleform {
39
40 function definition() {
41 global $USER, $CFG;
42
43 $mform =& $this->_form;
44 $course = $this->_customdata['course'];
45
a5b8be62 46 $context = get_context_instance(CONTEXT_COURSE, $course->id);
606af46f 47 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
48
32b97bb2 49 $strgradeboundary = get_string('gradeboundary', 'grades');
50 $strconfiggradeboundary = get_string('configgradeboundary', 'grades');
51 $strgradeletter = get_string('gradeletter', 'grades');
52 $strconfiggradeletter = get_string('configgradeletter', 'grades');
32b97bb2 53 $stryes = get_string('yes');
54 $strno = get_string('no');
55
57068674 56 $canviewhidden = has_capability('moodle/grade:viewhidden', $context);
57
32b97bb2 58
d622930b 59 $checkbox_default = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*', 0 => $strno, 1 => $stryes);
32b97bb2 60
e0724506 61 $advanced = array();
606af46f 62/// form definition with preferences defaults
63//--------------------------------------------------------------------------------
28c95515 64 $preferences = array();
a5b8be62 65
66 // Initialise the preferences arrays with grade:manage capabilities
67 if (has_capability('moodle/grade:manage', $context)) {
a5b8be62 68
57068674 69 $preferences['prefshow'] = array();
70 $preferences['prefshow']['showcalculations'] = $checkbox_default;
71 $preferences['prefshow']['showeyecons'] = $checkbox_default;
72 if ($canviewhidden) {
73 $preferences['prefshow']['showaverages'] = $checkbox_default;
74 }
57068674 75 $preferences['prefshow']['showlocks'] = $checkbox_default;
a5b8be62 76
77 $preferences['prefrows'] = array(
e0724506 78 'rangesdisplaytype' => array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
79 GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
1796708d 80 GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
81 GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
82 GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades')),
e0724506 83 'rangesdecimalpoints' => array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
84 GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
57068674 85 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5));
86 $advanced = array_merge($advanced, array('rangesdisplaytype', 'rangesdecimalpoints'));
87
88 if ($canviewhidden) {
89 $preferences['prefrows']['averagesdisplaytype'] = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
90 GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
91 GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
92 GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
93 GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'));
94 $preferences['prefrows']['averagesdecimalpoints'] = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
95 GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
96 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
97 $preferences['prefrows']['meanselection'] = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
98 GRADE_REPORT_MEAN_ALL => get_string('meanall', 'grades'),
99 GRADE_REPORT_MEAN_GRADED => get_string('meangraded', 'grades'));
100
101 $advanced = array_merge($advanced, array('averagesdisplaytype', 'averagesdecimalpoints'));
102 }
32b97bb2 103 }
104
2ca093fa 105 // quickgrading and showquickfeedback are conditional on grade:edit capability
a5b8be62 106 if (has_capability('moodle/grade:edit', $context)) {
107 $preferences['prefgeneral']['quickgrading'] = $checkbox_default;
2ca093fa 108 $preferences['prefgeneral']['showquickfeedback'] = $checkbox_default;
a5b8be62 109 }
110
111 // View capability is the lowest permission. Users with grade:manage or grade:edit must also have grader:view
112 if (has_capability('gradereport/grader:view', $context)) {
113 $preferences['prefgeneral']['studentsperpage'] = 'text';
d622930b 114 $preferences['prefgeneral']['aggregationposition'] = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
e0724506 115 GRADE_REPORT_AGGREGATION_POSITION_FIRST => get_string('positionfirst', 'grades'),
116 GRADE_REPORT_AGGREGATION_POSITION_LAST => get_string('positionlast', 'grades'));
b244b9b7 117 $preferences['prefgeneral']['enableajax'] = $checkbox_default;
a5b8be62 118
119 $preferences['prefshow']['showuserimage'] = $checkbox_default;
9d35e66e 120 $preferences['prefshow']['showuseridnumber'] = $checkbox_default;
a5b8be62 121 $preferences['prefshow']['showactivityicons'] = $checkbox_default;
122 $preferences['prefshow']['showranges'] = $checkbox_default;
123
57068674 124 if ($canviewhidden) {
125 $preferences['prefrows']['shownumberofgrades'] = $checkbox_default;
126 }
e0724506 127
128 $advanced = array_merge($advanced, array('aggregationposition'));
a5b8be62 129 }
130
131
28c95515 132 foreach ($preferences as $group => $prefs) {
133 $mform->addElement('header', $group, get_string($group, 'grades'));
134
135 foreach ($prefs as $pref => $type) {
136 // Detect and process dynamically numbered preferences
137 if (preg_match('/([^[0-9]+)([0-9]+)/', $pref, $matches)) {
138 $lang_string = $matches[1];
139 $number = ' ' . $matches[2];
140 } else {
141 $lang_string = $pref;
142 $number = null;
143 }
144
145 $full_pref = 'grade_report_' . $pref;
146
147 $pref_value = get_user_preferences($full_pref);
148
149 $options = null;
150 if (is_array($type)) {
151 $options = $type;
152 $type = 'select';
e5ea84fa 153 // MDL-11478
a2342ad3 154 // get default aggregationposition from grade_settings
b882f393 155 $course_value = null;
5ebce7bb 156 if (!empty($CFG->{$full_pref})) {
157 $course_value = grade_get_setting($course->id, $pref, $CFG->{$full_pref});
158 }
7f304262 159
a2342ad3 160 if ($pref == 'aggregationposition') {
05766b50 161 if (!empty($options[$course_value])) {
162 $default = $options[$course_value];
163 } else {
164 $default = $options[$CFG->grade_aggregationposition];
165 }
a2342ad3 166 } elseif (isset($options[$CFG->{$full_pref}])) {
e0724506 167 $default = $options[$CFG->{$full_pref}];
e5ea84fa 168 } else {
e0724506 169 $default = '';
e5ea84fa 170 }
28c95515 171 } else {
172 $default = $CFG->$full_pref;
173 }
174
8933d48c 175 $help_string = get_string("config$lang_string", 'grades');
176
d622930b 177 // Replace the '*default*' value with the site default language string - 'default' might collide with custom language packs
178 if (!is_null($options) AND isset($options[GRADE_REPORT_PREFERENCE_DEFAULT]) && $options[GRADE_REPORT_PREFERENCE_DEFAULT] == '*default*') {
e0724506 179 $options[GRADE_REPORT_PREFERENCE_DEFAULT] = get_string('reportdefault', 'grades', $default);
8933d48c 180 } elseif ($type == 'text') {
56cef27d 181 $help_string = get_string("config{$lang_string}default", 'grades', $default);
28c95515 182 }
183
184 $label = get_string($lang_string, 'grades') . $number;
185
186 $mform->addElement($type, $full_pref, $label, $options);
c2acd831 187 if ($lang_string != 'showuserimage') {
4de3322d 188 $mform->setHelpButton($full_pref, array($lang_string, get_string($lang_string, 'grades'), 'grade'), true);
c2acd831 189 }
28c95515 190 $mform->setDefault($full_pref, $pref_value);
191 $mform->setType($full_pref, PARAM_ALPHANUM);
a69e7335 192 }
a69e7335 193 }
ce0c946a 194
e0724506 195 foreach($advanced as $name) {
196 $mform->setAdvanced('grade_report_'.$name);
197 }
198
606af46f 199 $mform->addElement('hidden', 'id');
1dd1bc8e 200 $mform->setType('id', PARAM_INT);
606af46f 201 $mform->setDefault('id', $course->id);
202
eda77deb 203 $this->add_action_buttons();
606af46f 204 }
205
606af46f 206/// perform some extra moodle validation
a78890d5 207 function validation($data, $files) {
208 return parent::validation($data, $files);
606af46f 209 }
210}
6c3ef410 211