Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [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              $advanced = array_merge($advanced, array('aggregationposition'));
 113          }
 114  
 115  
 116          foreach ($preferences as $group => $prefs) {
 117              $mform->addElement('header', $group, get_string($group, 'grades'));
 118              $mform->setExpanded($group);
 119  
 120              foreach ($prefs as $pref => $type) {
 121                  // Detect and process dynamically numbered preferences
 122                  if (preg_match('/([^[0-9]+)([0-9]+)/', $pref, $matches)) {
 123                      $lang_string = $matches[1];
 124                      $number = ' ' . $matches[2];
 125                  } else {
 126                      $lang_string = $pref;
 127                      $number = null;
 128                  }
 129  
 130                  $full_pref  = 'grade_report_' . $pref;
 131  
 132                  $pref_value = get_user_preferences($full_pref);
 133  
 134                  $options = null;
 135                  if (is_array($type)) {
 136                      $options = $type;
 137                      $type = 'select';
 138                      // MDL-11478
 139                      // get default aggregationposition from grade_settings
 140                      $course_value = null;
 141                      if (!empty($CFG->{$full_pref})) {
 142                          $course_value = grade_get_setting($course->id, $pref, $CFG->{$full_pref});
 143                      }
 144  
 145                      if ($pref == 'aggregationposition') {
 146                          if (!empty($options[$course_value])) {
 147                              $default = $options[$course_value];
 148                          } else {
 149                              $default = $options[$CFG->grade_aggregationposition];
 150                          }
 151                      } elseif (isset($options[$CFG->{$full_pref}])) {
 152                          $default = $options[$CFG->{$full_pref}];
 153                      } else {
 154                          $default = '';
 155                      }
 156                  } else {
 157                      $default = $CFG->$full_pref;
 158                  }
 159  
 160                  // Replace the '*default*' value with the site default language string - 'default' might collide with custom language packs
 161                  if (!is_null($options) AND isset($options[GRADE_REPORT_PREFERENCE_DEFAULT]) && $options[GRADE_REPORT_PREFERENCE_DEFAULT] == '*default*') {
 162                      $options[GRADE_REPORT_PREFERENCE_DEFAULT] = get_string('reportdefault', 'grades', $default);
 163                  }
 164  
 165                  $label = get_string($lang_string, 'grades') . $number;
 166  
 167                  $mform->addElement($type, $full_pref, $label, $options);
 168                  if ($lang_string != 'showuserimage') {
 169                      $mform->addHelpButton($full_pref, $lang_string, 'grades');
 170                  }
 171                  $mform->setDefault($full_pref, $pref_value);
 172                  $mform->setType($full_pref, PARAM_ALPHANUM);
 173              }
 174          }
 175  
 176          foreach($advanced as $name) {
 177              $mform->setAdvanced('grade_report_'.$name);
 178          }
 179  
 180          $mform->addElement('hidden', 'id');
 181          $mform->setType('id', PARAM_INT);
 182          $mform->setDefault('id', $course->id);
 183  
 184          $this->add_action_buttons(false);
 185      }
 186  
 187  /// perform some extra moodle validation
 188      function validation($data, $files) {
 189          return parent::validation($data, $files);
 190      }
 191  }
 192