Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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              if ($canviewhidden) {
  60                  $preferences['prefshow']['showaverages']  = $checkbox_default;
  61              }
  62  
  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'));
  73  
  74              if ($canviewhidden) {
  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'));
  86  
  87                  $advanced = array_merge($advanced, array('averagesdisplaytype', 'averagesdecimalpoints'));
  88              }
  89          }
  90  
  91          // Quickgrading use conditional on grade:edit capability.
  92          if (has_capability('moodle/grade:edit', $context)) {
  93              $preferences['prefgeneral']['quickgrading'] = $checkbox_default;
  94          }
  95  
  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;
 100              }
 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'));
 104  
 105              $preferences['prefshow']['showuserimage'] = $checkbox_default;
 106              $preferences['prefshow']['showranges'] = $checkbox_default;
 107  
 108              if ($canviewhidden) {
 109                  $preferences['prefrows']['shownumberofgrades'] = $checkbox_default;
 110              }
 111          }
 112  
 113  
 114          foreach ($preferences as $group => $prefs) {
 115              $mform->addElement('header', $group, get_string($group, 'grades'));
 116              $mform->setExpanded($group);
 117  
 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];
 123                  } else {
 124                      $lang_string = $pref;
 125                      $number = null;
 126                  }
 127  
 128                  $full_pref  = 'grade_report_' . $pref;
 129  
 130                  $pref_value = get_user_preferences($full_pref);
 131  
 132                  $options = null;
 133                  if (is_array($type)) {
 134                      $options = $type;
 135                      $type = 'select';
 136                      // MDL-11478
 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});
 141                      }
 142  
 143                      if ($pref == 'aggregationposition') {
 144                          if (!empty($options[$course_value])) {
 145                              $default = $options[$course_value];
 146                          } else {
 147                              $default = $options[$CFG->grade_aggregationposition];
 148                          }
 149                      } elseif (isset($options[$CFG->{$full_pref}])) {
 150                          $default = $options[$CFG->{$full_pref}];
 151                      } else {
 152                          $default = '';
 153                      }
 154                  } else {
 155                      $default = $CFG->$full_pref;
 156                  }
 157  
 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);
 161                  }
 162  
 163                  $label = get_string($lang_string, 'grades') . $number;
 164  
 165                  $mform->addElement($type, $full_pref, $label, $options);
 166                  if ($lang_string != 'showuserimage') {
 167                      $mform->addHelpButton($full_pref, $lang_string, 'grades');
 168                  }
 169                  $mform->setDefault($full_pref, $pref_value);
 170                  $mform->setType($full_pref, PARAM_ALPHANUM);
 171              }
 172          }
 173  
 174          foreach($advanced as $name) {
 175              $mform->setAdvanced('grade_report_'.$name);
 176          }
 177  
 178          $mform->addElement('hidden', 'id');
 179          $mform->setType('id', PARAM_INT);
 180          $mform->setDefault('id', $course->id);
 181  
 182          $this->add_action_buttons(false);
 183      }
 184  
 185  /// perform some extra moodle validation
 186      function validation($data, $files) {
 187          return parent::validation($data, $files);
 188      }
 189  }
 190