Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 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   * A moodleform to allow the editing of a user's grade for a particular activity
  19   *
  20   * @package   core_grades
  21   * @copyright 2007 Petr Skoda
  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  class edit_grade_form extends moodleform {
  32  
  33      function definition() {
  34          global $CFG, $COURSE, $DB;
  35  
  36          $mform =& $this->_form;
  37  
  38          $grade_item = $this->_customdata['grade_item'];
  39          $gpr        = $this->_customdata['gpr'];
  40  
  41          if ($grade_item->is_course_item()) {
  42              $grade_category = null;
  43          } else if ($grade_item->is_category_item()) {
  44              $grade_category = $grade_item->get_item_category();
  45              $grade_category = $grade_category->get_parent_category();
  46          } else {
  47              $grade_category = $grade_item->get_parent_category();
  48          }
  49  
  50          /// information fields
  51          $mform->addElement('static', 'user', get_string('user'));
  52          $mform->addElement('static', 'itemname', get_string('itemname', 'grades'));
  53  
  54          $mform->addElement('checkbox', 'overridden', get_string('overridden', 'grades'));
  55          $mform->addHelpButton('overridden', 'overridden', 'grades');
  56  
  57          /// actual grade - numeric or scale
  58          if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
  59              // numeric grade
  60              $mform->addElement('text', 'finalgrade', get_string('finalgrade', 'grades'));
  61              $mform->setType('finalgrade', PARAM_RAW);
  62              $mform->addHelpButton('finalgrade', 'finalgrade', 'grades');
  63              $mform->disabledIf('finalgrade', 'overridden', 'notchecked');
  64  
  65          } else if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
  66              // scale grade
  67              $scaleopt = array();
  68  
  69              if (empty($grade_item->outcomeid)) {
  70                  $scaleopt[-1] = get_string('nograde');
  71              } else {
  72                  $scaleopt[-1] = get_string('nooutcome', 'grades');
  73              }
  74  
  75              $i = 1;
  76              if ($scale = $DB->get_record('scale', array('id' => $grade_item->scaleid))) {
  77                  foreach (explode(",", $scale->scale) as $option) {
  78                      $scaleopt[$i] = $option;
  79                      $i++;
  80                  }
  81              }
  82  
  83              $mform->addElement('select', 'finalgrade', get_string('finalgrade', 'grades'), $scaleopt);
  84              $mform->addHelpButton('finalgrade', 'finalgrade', 'grades');
  85              $mform->disabledIf('finalgrade', 'overridden', 'notchecked');
  86          }
  87  
  88          $mform->addElement('advcheckbox', 'excluded', get_string('excluded', 'grades'));
  89          $mform->addHelpButton('excluded', 'excluded', 'grades');
  90  
  91          /// hiding
  92          /// advcheckbox is not compatible with disabledIf !!
  93          $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
  94          $mform->addHelpButton('hidden', 'hidden', 'grades');
  95          $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
  96          $mform->disabledIf('hidden', 'hiddenuntil[enabled]', 'checked');
  97  
  98          /// locking
  99          $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
 100          $mform->addHelpButton('locked', 'locked', 'grades');
 101          $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
 102          $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE);
 103  
 104          // Feedback format is automatically converted to html if user has enabled editor
 105          $feedbackoptions = array('maxfiles'=>0, 'maxbytes'=>0); //TODO: no files here for now, if ever gets implemented use component 'grade' and filearea 'feedback'
 106          $mform->addElement('editor', 'feedback', get_string('feedback', 'grades'), null, $feedbackoptions);
 107          $mform->addHelpButton('feedback', 'feedback', 'grades');
 108          $mform->setType('text', PARAM_RAW); // to be cleaned before display, no XSS risk
 109  
 110          // hidden params
 111          $mform->addElement('hidden', 'oldgrade');
 112          $mform->setType('oldgrade', PARAM_RAW);
 113          $mform->addElement('hidden', 'oldfeedback');
 114          $mform->setType('oldfeedback', PARAM_RAW);
 115  
 116          $mform->addElement('hidden', 'id', 0);
 117          $mform->setType('id', PARAM_INT);
 118  
 119          $mform->addElement('hidden', 'itemid', 0);
 120          $mform->setType('itemid', PARAM_INT);
 121  
 122          $mform->addElement('hidden', 'userid', 0);
 123          $mform->setType('userid', PARAM_INT);
 124  
 125          $mform->addElement('hidden', 'courseid', $COURSE->id);
 126          $mform->setType('courseid', PARAM_INT);
 127  
 128  /// add return tracking info
 129          $gpr->add_mform_elements($mform);
 130  
 131  //-------------------------------------------------------------------------------
 132          // buttons
 133          $this->add_action_buttons();
 134      }
 135  
 136      function definition_after_data() {
 137          global $CFG, $COURSE, $DB;
 138  
 139          $context = context_course::instance($COURSE->id);
 140  
 141          $mform =& $this->_form;
 142          $grade_item = $this->_customdata['grade_item'];
 143  
 144          // fill in user name if user still exists
 145          $userid = $mform->getElementValue('userid');
 146          if ($user = $DB->get_record('user', array('id' => $userid))) {
 147              $username = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$userid.'">'.fullname($user).'</a>';
 148              $user_el =& $mform->getElement('user');
 149              $user_el->setValue($username);
 150          }
 151  
 152          // add activity name + link
 153          if ($grade_item->itemtype == 'mod') {
 154              $cm = get_coursemodule_from_instance($grade_item->itemmodule, $grade_item->iteminstance, $grade_item->courseid);
 155              $itemname = '<a href="'.$CFG->wwwroot.'/mod/'.$grade_item->itemmodule.'/view.php?id='.$cm->id.'">'.$grade_item->get_name().'</a>';
 156          } else {
 157              $itemname = $grade_item->get_name();
 158          }
 159          $itemname_el =& $mform->getElement('itemname');
 160          $itemname_el->setValue($itemname);
 161  
 162          // access control - disable not allowed elements
 163          if (!has_capability('moodle/grade:manage', $context)) {
 164              $mform->hardFreeze('excluded');
 165          }
 166  
 167          if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:hide', $context)) {
 168              $mform->hardFreeze('hidden');
 169              $mform->hardFreeze('hiddenuntil');
 170          }
 171  
 172          $old_grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$userid));
 173  
 174          $gradeitemoverridable = $grade_item->is_overridable_item();
 175          if (!$gradeitemoverridable) {
 176              $mform->removeElement('overridden');
 177          }
 178  
 179          if ($grade_item->is_hidden()) {
 180              $mform->hardFreeze('hidden');
 181          }
 182  
 183          if ($old_grade_grade->is_locked()) {
 184              if ($grade_item->is_locked()) {
 185                  $mform->hardFreeze('locked');
 186                  $mform->hardFreeze('locktime');
 187              }
 188  
 189              if ($gradeitemoverridable) {
 190                  $mform->hardFreeze('overridden');
 191              }
 192              $mform->hardFreeze('finalgrade');
 193              $mform->hardFreeze('feedback');
 194  
 195          } else {
 196              if (empty($old_grade_grade->id)) {
 197                  $old_grade_grade->locked = $grade_item->locked;
 198                  $old_grade_grade->locktime = $grade_item->locktime;
 199              }
 200  
 201              if (($old_grade_grade->locked or $old_grade_grade->locktime)
 202                and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context))) {
 203                  $mform->hardFreeze('locked');
 204                  $mform->hardFreeze('locktime');
 205  
 206              } else if ((!$old_grade_grade->locked and !$old_grade_grade->locktime)
 207                and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context))) {
 208                  $mform->hardFreeze('locked');
 209                  $mform->hardFreeze('locktime');
 210              }
 211          }
 212      }
 213  }
 214  
 215