Merge branch 'MDL-38416-master' of git://github.com/FMCorz/moodle
[moodle.git] / grade / report / grader / preferences_form.php
CommitLineData
e060e33d 1<?php
e060e33d 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
8ad36f4c 16
cae83708 17/**
a153c9f2 18 * Form for grader report preferences
cae83708 19 *
a153c9f2 20 * @package gradereport_grader
cae83708 21 * @copyright 2009 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
bfebaf64
MD
25if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
27}
28
606af46f 29require_once($CFG->libdir.'/formslib.php');
30
31/**
32 * First implementation of the preferences in the form of a moodleform.
606af46f 33 * TODO add "reset to site defaults" button
606af46f 34 */
35class grader_report_preferences_form extends moodleform {
36
37 function definition() {
38 global $USER, $CFG;
39
40 $mform =& $this->_form;
41 $course = $this->_customdata['course'];
42
d4060472 43 $context = context_course::instance($course->id);
606af46f 44
57068674 45 $canviewhidden = has_capability('moodle/grade:viewhidden', $context);
46
4d73fde1 47 $checkbox_default = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*', 0 => get_string('no'), 1 => get_string('yes'));
32b97bb2 48
e0724506 49 $advanced = array();
606af46f 50/// form definition with preferences defaults
51//--------------------------------------------------------------------------------
28c95515 52 $preferences = array();
a5b8be62 53
54 // Initialise the preferences arrays with grade:manage capabilities
55 if (has_capability('moodle/grade:manage', $context)) {
a5b8be62 56
57068674 57 $preferences['prefshow'] = array();
58 $preferences['prefshow']['showcalculations'] = $checkbox_default;
59 $preferences['prefshow']['showeyecons'] = $checkbox_default;
60 if ($canviewhidden) {
61 $preferences['prefshow']['showaverages'] = $checkbox_default;
62 }
57068674 63 $preferences['prefshow']['showlocks'] = $checkbox_default;
a5b8be62 64
65 $preferences['prefrows'] = array(
e0724506 66 'rangesdisplaytype' => array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
67 GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
1796708d 68 GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
69 GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
70 GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades')),
e0724506 71 'rangesdecimalpoints' => array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
72 GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
57068674 73 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5));
74 $advanced = array_merge($advanced, array('rangesdisplaytype', 'rangesdecimalpoints'));
75
76 if ($canviewhidden) {
77 $preferences['prefrows']['averagesdisplaytype'] = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
78 GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
79 GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
80 GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
81 GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'));
82 $preferences['prefrows']['averagesdecimalpoints'] = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
83 GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
84 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
85 $preferences['prefrows']['meanselection'] = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
86 GRADE_REPORT_MEAN_ALL => get_string('meanall', 'grades'),
87 GRADE_REPORT_MEAN_GRADED => get_string('meangraded', 'grades'));
88
89 $advanced = array_merge($advanced, array('averagesdisplaytype', 'averagesdecimalpoints'));
90 }
32b97bb2 91 }
92
2ca093fa 93 // quickgrading and showquickfeedback are conditional on grade:edit capability
a5b8be62 94 if (has_capability('moodle/grade:edit', $context)) {
95 $preferences['prefgeneral']['quickgrading'] = $checkbox_default;
2ca093fa 96 $preferences['prefgeneral']['showquickfeedback'] = $checkbox_default;
a5b8be62 97 }
98
99 // View capability is the lowest permission. Users with grade:manage or grade:edit must also have grader:view
100 if (has_capability('gradereport/grader:view', $context)) {
101 $preferences['prefgeneral']['studentsperpage'] = 'text';
d622930b 102 $preferences['prefgeneral']['aggregationposition'] = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
e0724506 103 GRADE_REPORT_AGGREGATION_POSITION_FIRST => get_string('positionfirst', 'grades'),
104 GRADE_REPORT_AGGREGATION_POSITION_LAST => get_string('positionlast', 'grades'));
b244b9b7 105 $preferences['prefgeneral']['enableajax'] = $checkbox_default;
a5b8be62 106
107 $preferences['prefshow']['showuserimage'] = $checkbox_default;
108 $preferences['prefshow']['showactivityicons'] = $checkbox_default;
109 $preferences['prefshow']['showranges'] = $checkbox_default;
bb17ac1e 110 $preferences['prefshow']['showanalysisicon'] = $checkbox_default;
a5b8be62 111
57068674 112 if ($canviewhidden) {
113 $preferences['prefrows']['shownumberofgrades'] = $checkbox_default;
114 }
e0724506 115
116 $advanced = array_merge($advanced, array('aggregationposition'));
a5b8be62 117 }
118
119
28c95515 120 foreach ($preferences as $group => $prefs) {
121 $mform->addElement('header', $group, get_string($group, 'grades'));
a4faf4f4 122 $mform->setExpanded($group);
28c95515 123
124 foreach ($prefs as $pref => $type) {
125 // Detect and process dynamically numbered preferences
126 if (preg_match('/([^[0-9]+)([0-9]+)/', $pref, $matches)) {
127 $lang_string = $matches[1];
128 $number = ' ' . $matches[2];
129 } else {
130 $lang_string = $pref;
131 $number = null;
132 }
133
134 $full_pref = 'grade_report_' . $pref;
135
136 $pref_value = get_user_preferences($full_pref);
137
138 $options = null;
139 if (is_array($type)) {
140 $options = $type;
141 $type = 'select';
e5ea84fa 142 // MDL-11478
a2342ad3 143 // get default aggregationposition from grade_settings
b882f393 144 $course_value = null;
5ebce7bb 145 if (!empty($CFG->{$full_pref})) {
146 $course_value = grade_get_setting($course->id, $pref, $CFG->{$full_pref});
147 }
7f304262 148
a2342ad3 149 if ($pref == 'aggregationposition') {
05766b50 150 if (!empty($options[$course_value])) {
151 $default = $options[$course_value];
152 } else {
153 $default = $options[$CFG->grade_aggregationposition];
154 }
a2342ad3 155 } elseif (isset($options[$CFG->{$full_pref}])) {
e0724506 156 $default = $options[$CFG->{$full_pref}];
e5ea84fa 157 } else {
e0724506 158 $default = '';
e5ea84fa 159 }
28c95515 160 } else {
161 $default = $CFG->$full_pref;
162 }
163
d622930b 164 // Replace the '*default*' value with the site default language string - 'default' might collide with custom language packs
165 if (!is_null($options) AND isset($options[GRADE_REPORT_PREFERENCE_DEFAULT]) && $options[GRADE_REPORT_PREFERENCE_DEFAULT] == '*default*') {
e0724506 166 $options[GRADE_REPORT_PREFERENCE_DEFAULT] = get_string('reportdefault', 'grades', $default);
28c95515 167 }
168
169 $label = get_string($lang_string, 'grades') . $number;
170
171 $mform->addElement($type, $full_pref, $label, $options);
c2acd831 172 if ($lang_string != 'showuserimage') {
caf698c8 173 $mform->addHelpButton($full_pref, $lang_string, 'grades');
c2acd831 174 }
28c95515 175 $mform->setDefault($full_pref, $pref_value);
176 $mform->setType($full_pref, PARAM_ALPHANUM);
a69e7335 177 }
a69e7335 178 }
ce0c946a 179
e0724506 180 foreach($advanced as $name) {
181 $mform->setAdvanced('grade_report_'.$name);
182 }
183
606af46f 184 $mform->addElement('hidden', 'id');
1dd1bc8e 185 $mform->setType('id', PARAM_INT);
606af46f 186 $mform->setDefault('id', $course->id);
187
eda77deb 188 $this->add_action_buttons();
606af46f 189 }
190
606af46f 191/// perform some extra moodle validation
a78890d5 192 function validation($data, $files) {
193 return parent::validation($data, $files);
606af46f 194 }
195}
6c3ef410 196