Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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/>.
  16  
  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   */
  24  
  25  if (!defined('MOODLE_INTERNAL')) {
  26      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
  27  }
  28  
  29  require_once($CFG->libdir.'/formslib.php');
  30  
  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 {
  36  
  37      function definition() {
  38          global $USER, $CFG;
  39  
  40          $mform    =& $this->_form;
  41          $course   = $this->_customdata['course'];
  42  
  43          $context = context_course::instance($course->id);
  44  
  45          $canviewhidden = has_capability('moodle/grade:viewhidden', $context);
  46  
  47          $checkbox_default = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*', 0 => get_string('no'), 1 => get_string('yes'));
  48  
  49          $advanced = array();
  50  /// form definition with preferences defaults
  51  //--------------------------------------------------------------------------------
  52          $preferences = array();
  53  
  54          // Initialise the preferences arrays with grade:manage capabilities
  55          if (has_capability('moodle/grade:manage', $context)) {
  56  
  57              $preferences['prefshow'] = array();
  58  
  59              $preferences['prefshow']['showcalculations'] = $checkbox_default;
  60  
  61              $preferences['prefshow']['showeyecons']       = $checkbox_default;
  62              if ($canviewhidden) {
  63                  $preferences['prefshow']['showaverages']  = $checkbox_default;
  64              }
  65              $preferences['prefshow']['showlocks']         = $checkbox_default;
  66  
  67              $preferences['prefrows'] = array(
  68                          'rangesdisplaytype'      => array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
  69                                                            GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
  70                                                            GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
  71                                                            GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
  72                                                            GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades')),
  73                          'rangesdecimalpoints'    => array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
  74                                                            GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
  75                                                            0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5));
  76              $advanced = array_merge($advanced, array('rangesdisplaytype', 'rangesdecimalpoints'));
  77  
  78              if ($canviewhidden) {
  79                  $preferences['prefrows']['averagesdisplaytype'] = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
  80                                                                          GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
  81                                                                          GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
  82                                                                          GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
  83                                                                          GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'));
  84                  $preferences['prefrows']['averagesdecimalpoints'] = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
  85                                                                            GRADE_REPORT_PREFERENCE_INHERIT => get_string('inherit', 'grades'),
  86                                                                            0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
  87                  $preferences['prefrows']['meanselection']  = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
  88                                                                     GRADE_REPORT_MEAN_ALL => get_string('meanall', 'grades'),
  89                                                                     GRADE_REPORT_MEAN_GRADED => get_string('meangraded', 'grades'));
  90  
  91                  $advanced = array_merge($advanced, array('averagesdisplaytype', 'averagesdecimalpoints'));
  92              }
  93          }
  94  
  95          // quickgrading and showquickfeedback are conditional on grade:edit capability
  96          if (has_capability('moodle/grade:edit', $context)) {
  97              $preferences['prefgeneral']['quickgrading'] = $checkbox_default;
  98              $preferences['prefgeneral']['showquickfeedback'] = $checkbox_default;
  99          }
 100  
 101          // View capability is the lowest permission. Users with grade:manage or grade:edit must also have grader:view
 102          if (has_capability('gradereport/grader:view', $context)) {
 103              $preferences['prefgeneral']['studentsperpage'] = 'text';
 104              if (has_capability('moodle/course:viewsuspendedusers', $context)) {
 105                  $preferences['prefgeneral']['showonlyactiveenrol'] = $checkbox_default;
 106              }
 107              $preferences['prefgeneral']['aggregationposition'] = array(GRADE_REPORT_PREFERENCE_DEFAULT => '*default*',
 108                                                                         GRADE_REPORT_AGGREGATION_POSITION_FIRST => get_string('positionfirst', 'grades'),
 109                                                                         GRADE_REPORT_AGGREGATION_POSITION_LAST => get_string('positionlast', 'grades'));
 110              $preferences['prefgeneral']['enableajax'] = $checkbox_default;
 111  
 112              $preferences['prefshow']['showuserimage'] = $checkbox_default;
 113              $preferences['prefshow']['showactivityicons'] = $checkbox_default;
 114              $preferences['prefshow']['showranges'] = $checkbox_default;
 115              $preferences['prefshow']['showanalysisicon'] = $checkbox_default;
 116  
 117              if ($canviewhidden) {
 118                  $preferences['prefrows']['shownumberofgrades'] = $checkbox_default;
 119              }
 120  
 121              $advanced = array_merge($advanced, array('aggregationposition'));
 122          }
 123  
 124  
 125          foreach ($preferences as $group => $prefs) {
 126              $mform->addElement('header', $group, get_string($group, 'grades'));
 127              $mform->setExpanded($group);
 128  
 129              foreach ($prefs as $pref => $type) {
 130                  // Detect and process dynamically numbered preferences
 131                  if (preg_match('/([^[0-9]+)([0-9]+)/', $pref, $matches)) {
 132                      $lang_string = $matches[1];
 133                      $number = ' ' . $matches[2];
 134                  } else {
 135                      $lang_string = $pref;
 136                      $number = null;
 137                  }
 138  
 139                  $full_pref  = 'grade_report_' . $pref;
 140  
 141                  $pref_value = get_user_preferences($full_pref);
 142  
 143                  $options = null;
 144                  if (is_array($type)) {
 145                      $options = $type;
 146                      $type = 'select';
 147                      // MDL-11478
 148                      // get default aggregationposition from grade_settings
 149                      $course_value = null;
 150                      if (!empty($CFG->{$full_pref})) {
 151                          $course_value = grade_get_setting($course->id, $pref, $CFG->{$full_pref});
 152                      }
 153  
 154                      if ($pref == 'aggregationposition') {
 155                          if (!empty($options[$course_value])) {
 156                              $default = $options[$course_value];
 157                          } else {
 158                              $default = $options[$CFG->grade_aggregationposition];
 159                          }
 160                      } elseif (isset($options[$CFG->{$full_pref}])) {
 161                          $default = $options[$CFG->{$full_pref}];
 162                      } else {
 163                          $default = '';
 164                      }
 165                  } else {
 166                      $default = $CFG->$full_pref;
 167                  }
 168  
 169                  // Replace the '*default*' value with the site default language string - 'default' might collide with custom language packs
 170                  if (!is_null($options) AND isset($options[GRADE_REPORT_PREFERENCE_DEFAULT]) && $options[GRADE_REPORT_PREFERENCE_DEFAULT] == '*default*') {
 171                      $options[GRADE_REPORT_PREFERENCE_DEFAULT] = get_string('reportdefault', 'grades', $default);
 172                  }
 173  
 174                  $label = get_string($lang_string, 'grades') . $number;
 175  
 176                  $mform->addElement($type, $full_pref, $label, $options);
 177                  if ($lang_string != 'showuserimage') {
 178                      $mform->addHelpButton($full_pref, $lang_string, 'grades');
 179                  }
 180                  $mform->setDefault($full_pref, $pref_value);
 181                  $mform->setType($full_pref, PARAM_ALPHANUM);
 182              }
 183          }
 184  
 185          foreach($advanced as $name) {
 186              $mform->setAdvanced('grade_report_'.$name);
 187          }
 188  
 189          $mform->addElement('hidden', 'id');
 190          $mform->setType('id', PARAM_INT);
 191          $mform->setDefault('id', $course->id);
 192  
 193          $this->add_action_buttons();
 194      }
 195  
 196  /// perform some extra moodle validation
 197      function validation($data, $files) {
 198          return parent::validation($data, $files);
 199      }
 200  }
 201