Merge branch 'MDL-38416-master' of git://github.com/FMCorz/moodle
[moodle.git] / grade / report / grader / preferences_form.php
1 <?php
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/>.
17 /**
18  * Form for grader report preferences
19  *
20  * @package    gradereport_grader
21  * @copyright  2009 Nicolas Connault
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 if (!defined('MOODLE_INTERNAL')) {
26     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
27 }
29 require_once($CFG->libdir.'/formslib.php');
31 /**
32  * First implementation of the preferences in the form of a moodleform.
33  * TODO add "reset to site defaults" button
34  */
35 class grader_report_preferences_form extends moodleform {
37     function definition() {
38         global $USER, $CFG;
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'));
49         $advanced = array();
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();
58             $preferences['prefshow']['showcalculations']  = $checkbox_default;
59             $preferences['prefshow']['showeyecons']       = $checkbox_default;
60             if ($canviewhidden) {
61                 $preferences['prefshow']['showaverages']  = $checkbox_default;
62             }
63             $preferences['prefshow']['showlocks']         = $checkbox_default;
65             $preferences['prefrows'] = array(
66                         'rangesdisplaytype'      => array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
67                                                           GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
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')),
71                         'rangesdecimalpoints'    => array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
72                                                           GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
73                                                           0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5));
74             $advanced = array_merge($advanced, array('rangesdisplaytype', 'rangesdecimalpoints'));
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'));
89                 $advanced = array_merge($advanced, array('averagesdisplaytype', 'averagesdecimalpoints'));
90             }
91         }
93         // quickgrading and showquickfeedback are conditional on grade:edit capability
94         if (has_capability('moodle/grade:edit', $context)) {
95             $preferences['prefgeneral']['quickgrading'] = $checkbox_default;
96             $preferences['prefgeneral']['showquickfeedback'] = $checkbox_default;
97         }
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';
102             $preferences['prefgeneral']['aggregationposition'] = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
103                                                                        GRADE_REPORT_AGGREGATION_POSITION_FIRST => get_string('positionfirst', 'grades'),
104                                                                        GRADE_REPORT_AGGREGATION_POSITION_LAST => get_string('positionlast', 'grades'));
105             $preferences['prefgeneral']['enableajax'] = $checkbox_default;
107             $preferences['prefshow']['showuserimage'] = $checkbox_default;
108             $preferences['prefshow']['showactivityicons'] = $checkbox_default;
109             $preferences['prefshow']['showranges'] = $checkbox_default;
110             $preferences['prefshow']['showanalysisicon'] = $checkbox_default;
112             if ($canviewhidden) {
113                 $preferences['prefrows']['shownumberofgrades'] = $checkbox_default;
114             }
116             $advanced = array_merge($advanced, array('aggregationposition'));
117         }
120         foreach ($preferences as $group => $prefs) {
121             $mform->addElement('header', $group, get_string($group, 'grades'));
122             $mform->setExpanded($group);
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                 }
134                 $full_pref  = 'grade_report_' . $pref;
136                 $pref_value = get_user_preferences($full_pref);
138                 $options = null;
139                 if (is_array($type)) {
140                     $options = $type;
141                     $type = 'select';
142                     // MDL-11478
143                     // get default aggregationposition from grade_settings
144                     $course_value = null;
145                     if (!empty($CFG->{$full_pref})) {
146                         $course_value = grade_get_setting($course->id, $pref, $CFG->{$full_pref});
147                     }
149                     if ($pref == 'aggregationposition') {
150                         if (!empty($options[$course_value])) {
151                             $default = $options[$course_value];
152                         } else {
153                             $default = $options[$CFG->grade_aggregationposition];
154                         }
155                     } elseif (isset($options[$CFG->{$full_pref}])) {
156                         $default = $options[$CFG->{$full_pref}];
157                     } else {
158                         $default = '';
159                     }
160                 } else {
161                     $default = $CFG->$full_pref;
162                 }
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*') {
166                     $options[GRADE_REPORT_PREFERENCE_DEFAULT] = get_string('reportdefault', 'grades', $default);
167                 }
169                 $label = get_string($lang_string, 'grades') . $number;
171                 $mform->addElement($type, $full_pref, $label, $options);
172                 if ($lang_string != 'showuserimage') {
173                     $mform->addHelpButton($full_pref, $lang_string, 'grades');
174                 }
175                 $mform->setDefault($full_pref, $pref_value);
176                 $mform->setType($full_pref, PARAM_ALPHANUM);
177             }
178         }
180         foreach($advanced as $name) {
181             $mform->setAdvanced('grade_report_'.$name);
182         }
184         $mform->addElement('hidden', 'id');
185         $mform->setType('id', PARAM_INT);
186         $mform->setDefault('id', $course->id);
188         $this->add_action_buttons();
189     }
191 /// perform some extra moodle validation
192     function validation($data, $files) {
193         return parent::validation($data, $files);
194     }