2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Form for grader report preferences
20 * @package gradereport_grader
21 * @copyright 2009 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
29 require_once($CFG->libdir
.'/formslib.php');
32 * First implementation of the preferences in the form of a moodleform.
33 * TODO add "reset to site defaults" button
35 class grader_report_preferences_form
extends moodleform
{
37 function definition() {
40 $mform =& $this->_form
;
41 $course = $this->_customdata
['course'];
43 $context = context_course
::instance($course->id
);
45 $canviewhidden = has_capability('moodle/grade:viewhidden', $context);
47 $checkbox_default = array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*', 0 => get_string('no'), 1 => get_string('yes'));
50 /// form definition with preferences defaults
51 //--------------------------------------------------------------------------------
52 $preferences = array();
54 // Initialise the preferences arrays with grade:manage capabilities
55 if (has_capability('moodle/grade:manage', $context)) {
57 $preferences['prefshow'] = array();
60 $preferences['prefshow']['showaverages'] = $checkbox_default;
63 $preferences['prefrows'] = array(
64 'rangesdisplaytype' => array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*',
65 GRADE_REPORT_PREFERENCE_INHERIT
=> get_string('inherit', 'grades'),
66 GRADE_DISPLAY_TYPE_REAL
=> get_string('real', 'grades'),
67 GRADE_DISPLAY_TYPE_PERCENTAGE
=> get_string('percentage', 'grades'),
68 GRADE_DISPLAY_TYPE_LETTER
=> get_string('letter', 'grades')),
69 'rangesdecimalpoints' => array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*',
70 GRADE_REPORT_PREFERENCE_INHERIT
=> get_string('inherit', 'grades'),
71 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5));
72 $advanced = array_merge($advanced, array('rangesdisplaytype', 'rangesdecimalpoints'));
75 $preferences['prefrows']['averagesdisplaytype'] = array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*',
76 GRADE_REPORT_PREFERENCE_INHERIT
=> get_string('inherit', 'grades'),
77 GRADE_DISPLAY_TYPE_REAL
=> get_string('real', 'grades'),
78 GRADE_DISPLAY_TYPE_PERCENTAGE
=> get_string('percentage', 'grades'),
79 GRADE_DISPLAY_TYPE_LETTER
=> get_string('letter', 'grades'));
80 $preferences['prefrows']['averagesdecimalpoints'] = array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*',
81 GRADE_REPORT_PREFERENCE_INHERIT
=> get_string('inherit', 'grades'),
82 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
83 $preferences['prefrows']['meanselection'] = array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*',
84 GRADE_REPORT_MEAN_ALL
=> get_string('meanall', 'grades'),
85 GRADE_REPORT_MEAN_GRADED
=> get_string('meangraded', 'grades'));
87 $advanced = array_merge($advanced, array('averagesdisplaytype', 'averagesdecimalpoints'));
91 // Quickgrading use conditional on grade:edit capability.
92 if (has_capability('moodle/grade:edit', $context)) {
93 $preferences['prefgeneral']['quickgrading'] = $checkbox_default;
96 // View capability is the lowest permission. Users with grade:manage or grade:edit must also have grader:view
97 if (has_capability('gradereport/grader:view', $context)) {
98 if (has_capability('moodle/course:viewsuspendedusers', $context)) {
99 $preferences['prefgeneral']['showonlyactiveenrol'] = $checkbox_default;
101 $preferences['prefgeneral']['aggregationposition'] = array(GRADE_REPORT_PREFERENCE_DEFAULT
=> '*default*',
102 GRADE_REPORT_AGGREGATION_POSITION_FIRST
=> get_string('positionfirst', 'grades'),
103 GRADE_REPORT_AGGREGATION_POSITION_LAST
=> get_string('positionlast', 'grades'));
105 $preferences['prefshow']['showuserimage'] = $checkbox_default;
106 $preferences['prefshow']['showranges'] = $checkbox_default;
108 if ($canviewhidden) {
109 $preferences['prefrows']['shownumberofgrades'] = $checkbox_default;
114 foreach ($preferences as $group => $prefs) {
115 $mform->addElement('header', $group, get_string($group, 'grades'));
116 $mform->setExpanded($group);
118 foreach ($prefs as $pref => $type) {
119 // Detect and process dynamically numbered preferences
120 if (preg_match('/([^[0-9]+)([0-9]+)/', $pref, $matches)) {
121 $lang_string = $matches[1];
122 $number = ' ' . $matches[2];
124 $lang_string = $pref;
128 $full_pref = 'grade_report_' . $pref;
130 $pref_value = get_user_preferences($full_pref);
133 if (is_array($type)) {
137 // get default aggregationposition from grade_settings
138 $course_value = null;
139 if (!empty($CFG->{$full_pref})) {
140 $course_value = grade_get_setting($course->id
, $pref, $CFG->{$full_pref});
143 if ($pref == 'aggregationposition') {
144 if (!empty($options[$course_value])) {
145 $default = $options[$course_value];
147 $default = $options[$CFG->grade_aggregationposition
];
149 } elseif (isset($options[$CFG->{$full_pref}])) {
150 $default = $options[$CFG->{$full_pref}];
155 $default = $CFG->$full_pref;
158 // Replace the '*default*' value with the site default language string - 'default' might collide with custom language packs
159 if (!is_null($options) AND isset($options[GRADE_REPORT_PREFERENCE_DEFAULT
]) && $options[GRADE_REPORT_PREFERENCE_DEFAULT
] == '*default*') {
160 $options[GRADE_REPORT_PREFERENCE_DEFAULT
] = get_string('reportdefault', 'grades', $default);
163 $label = get_string($lang_string, 'grades') . $number;
165 $mform->addElement($type, $full_pref, $label, $options);
166 if ($lang_string != 'showuserimage') {
167 $mform->addHelpButton($full_pref, $lang_string, 'grades');
169 $mform->setDefault($full_pref, $pref_value);
170 $mform->setType($full_pref, PARAM_ALPHANUM
);
174 foreach($advanced as $name) {
175 $mform->setAdvanced('grade_report_'.$name);
178 $mform->addElement('hidden', 'id');
179 $mform->setType('id', PARAM_INT
);
180 $mform->setDefault('id', $course->id
);
182 $this->add_action_buttons(false);
185 /// perform some extra moodle validation
186 function validation($data, $files) {
187 return parent
::validation($data, $files);