3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
20 * Form for grader report preferences.
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 if (!defined('MOODLE_INTERNAL')) {
29 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
32 require_once($CFG->libdir
.'/formslib.php');
35 * First implementation of the preferences in the form of a moodleform.
36 * TODO add "reset to site defaults" button
38 class grader_report_preferences_form
extends moodleform
{
40 function definition() {
43 $mform =& $this->_form
;
44 $course = $this->_customdata
['course'];
46 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
47 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
49 $canviewhidden = has_capability('moodle/grade:viewhidden', $context);
51 $checkbox_default = array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*', 0 => get_string('no'), 1 => get_string('yes'));
54 /// form definition with preferences defaults
55 //--------------------------------------------------------------------------------
56 $preferences = array();
58 // Initialise the preferences arrays with grade:manage capabilities
59 if (has_capability('moodle/grade:manage', $context)) {
61 $preferences['prefshow'] = array();
62 $preferences['prefshow']['showcalculations'] = $checkbox_default;
63 $preferences['prefshow']['showeyecons'] = $checkbox_default;
65 $preferences['prefshow']['showaverages'] = $checkbox_default;
67 $preferences['prefshow']['showlocks'] = $checkbox_default;
69 $preferences['prefrows'] = array(
70 'rangesdisplaytype' => array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*',
71 GRADE_REPORT_PREFERENCE_INHERIT
=> get_string('inherit', 'grades'),
72 GRADE_DISPLAY_TYPE_REAL
=> get_string('real', 'grades'),
73 GRADE_DISPLAY_TYPE_PERCENTAGE
=> get_string('percentage', 'grades'),
74 GRADE_DISPLAY_TYPE_LETTER
=> get_string('letter', 'grades')),
75 'rangesdecimalpoints' => array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*',
76 GRADE_REPORT_PREFERENCE_INHERIT
=> get_string('inherit', 'grades'),
77 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5));
78 $advanced = array_merge($advanced, array('rangesdisplaytype', 'rangesdecimalpoints'));
81 $preferences['prefrows']['averagesdisplaytype'] = array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*',
82 GRADE_REPORT_PREFERENCE_INHERIT
=> get_string('inherit', 'grades'),
83 GRADE_DISPLAY_TYPE_REAL
=> get_string('real', 'grades'),
84 GRADE_DISPLAY_TYPE_PERCENTAGE
=> get_string('percentage', 'grades'),
85 GRADE_DISPLAY_TYPE_LETTER
=> get_string('letter', 'grades'));
86 $preferences['prefrows']['averagesdecimalpoints'] = array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*',
87 GRADE_REPORT_PREFERENCE_INHERIT
=> get_string('inherit', 'grades'),
88 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
89 $preferences['prefrows']['meanselection'] = array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*',
90 GRADE_REPORT_MEAN_ALL
=> get_string('meanall', 'grades'),
91 GRADE_REPORT_MEAN_GRADED
=> get_string('meangraded', 'grades'));
93 $advanced = array_merge($advanced, array('averagesdisplaytype', 'averagesdecimalpoints'));
97 // quickgrading and showquickfeedback are conditional on grade:edit capability
98 if (has_capability('moodle/grade:edit', $context)) {
99 $preferences['prefgeneral']['quickgrading'] = $checkbox_default;
100 $preferences['prefgeneral']['showquickfeedback'] = $checkbox_default;
103 // View capability is the lowest permission. Users with grade:manage or grade:edit must also have grader:view
104 if (has_capability('gradereport/grader:view', $context)) {
105 $preferences['prefgeneral']['studentsperpage'] = 'text';
106 $preferences['prefgeneral']['aggregationposition'] = array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*',
107 GRADE_REPORT_AGGREGATION_POSITION_FIRST
=> get_string('positionfirst', 'grades'),
108 GRADE_REPORT_AGGREGATION_POSITION_LAST
=> get_string('positionlast', 'grades'));
109 $preferences['prefgeneral']['enableajax'] = $checkbox_default;
111 $preferences['prefshow']['showuserimage'] = $checkbox_default;
112 $preferences['prefshow']['showuseridnumber'] = $checkbox_default;
113 $preferences['prefshow']['showactivityicons'] = $checkbox_default;
114 $preferences['prefshow']['showranges'] = $checkbox_default;
116 if ($canviewhidden) {
117 $preferences['prefrows']['shownumberofgrades'] = $checkbox_default;
120 $advanced = array_merge($advanced, array('aggregationposition'));
124 foreach ($preferences as $group => $prefs) {
125 $mform->addElement('header', $group, get_string($group, 'grades'));
127 foreach ($prefs as $pref => $type) {
128 // Detect and process dynamically numbered preferences
129 if (preg_match('/([^[0-9]+)([0-9]+)/', $pref, $matches)) {
130 $lang_string = $matches[1];
131 $number = ' ' . $matches[2];
133 $lang_string = $pref;
137 $full_pref = 'grade_report_' . $pref;
139 $pref_value = get_user_preferences($full_pref);
142 if (is_array($type)) {
146 // get default aggregationposition from grade_settings
147 $course_value = null;
148 if (!empty($CFG->{$full_pref})) {
149 $course_value = grade_get_setting($course->id
, $pref, $CFG->{$full_pref});
152 if ($pref == 'aggregationposition') {
153 if (!empty($options[$course_value])) {
154 $default = $options[$course_value];
156 $default = $options[$CFG->grade_aggregationposition
];
158 } elseif (isset($options[$CFG->{$full_pref}])) {
159 $default = $options[$CFG->{$full_pref}];
164 $default = $CFG->$full_pref;
167 // Replace the '*default*' value with the site default language string - 'default' might collide with custom language packs
168 if (!is_null($options) AND isset($options[GRADE_REPORT_PREFERENCE_DEFAULT
]) && $options[GRADE_REPORT_PREFERENCE_DEFAULT
] == '*default*') {
169 $options[GRADE_REPORT_PREFERENCE_DEFAULT
] = get_string('reportdefault', 'grades', $default);
172 $label = get_string($lang_string, 'grades') . $number;
174 $mform->addElement($type, $full_pref, $label, $options);
175 if ($lang_string != 'showuserimage') {
176 $mform->addHelpButton($full_pref, $lang_string, 'grades');
178 $mform->setDefault($full_pref, $pref_value);
179 $mform->setType($full_pref, PARAM_ALPHANUM
);
183 foreach($advanced as $name) {
184 $mform->setAdvanced('grade_report_'.$name);
187 $mform->addElement('hidden', 'id');
188 $mform->setType('id', PARAM_INT
);
189 $mform->setDefault('id', $course->id
);
191 $this->add_action_buttons();
194 /// perform some extra moodle validation
195 function validation($data, $files) {
196 return parent
::validation($data, $files);