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   * 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          $mform->disabledIf('feedback', 'overridden');
 110  
 111          // hidden params
 112          $mform->addElement('hidden', 'oldgrade');
 113          $mform->setType('oldgrade', PARAM_RAW);
 114          $mform->addElement('hidden', 'oldfeedback');
 115          $mform->setType('oldfeedback', PARAM_RAW);
 116  
 117          $mform->addElement('hidden', 'id', 0);
 118          $mform->setType('id', PARAM_INT);
 119  
 120          $mform->addElement('hidden', 'itemid', 0);
 121          $mform->setType('itemid', PARAM_INT);
 122  
 123          $mform->addElement('hidden', 'userid', 0);
 124          $mform->setType('userid', PARAM_INT);
 125  
 126          $mform->addElement('hidden', 'courseid', $COURSE->id);
 127          $mform->setType('courseid', PARAM_INT);
 128  
 129  /// add return tracking info
 130          $gpr->add_mform_elements($mform);
 131  
 132  //-------------------------------------------------------------------------------
 133          // buttons
 134          $this->add_action_buttons();
 135      }
 136  
 137      function definition_after_data() {
 138          global $CFG, $COURSE, $DB;
 139  
 140          $context = context_course::instance($COURSE->id);
 141  
 142          $mform =& $this->_form;
 143          $grade_item = $this->_customdata['grade_item'];
 144  
 145          // fill in user name if user still exists
 146          $userid = $mform->getElementValue('userid');
 147          if ($user = $DB->get_record('user', array('id' => $userid))) {
 148              $username = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$userid.'">'.fullname($user).'</a>';
 149              $user_el =& $mform->getElement('user');
 150              $user_el->setValue($username);
 151          }
 152  
 153          // add activity name + link
 154          if ($grade_item->itemtype == 'mod') {
 155              $cm = get_coursemodule_from_instance($grade_item->itemmodule, $grade_item->iteminstance, $grade_item->courseid);
 156              $itemname = '<a href="'.$CFG->wwwroot.'/mod/'.$grade_item->itemmodule.'/view.php?id='.$cm->id.'">'.$grade_item->get_name().'</a>';
 157          } else {
 158              $itemname = $grade_item->get_name();
 159          }
 160          $itemname_el =& $mform->getElement('itemname');
 161          $itemname_el->setValue($itemname);
 162  
 163          // access control - disable not allowed elements
 164          if (!has_capability('moodle/grade:manage', $context)) {
 165              $mform->hardFreeze('excluded');
 166          }
 167  
 168          if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:hide', $context)) {
 169              $mform->hardFreeze('hidden');
 170              $mform->hardFreeze('hiddenuntil');
 171          }
 172  
 173          $old_grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$userid));
 174  
 175          $gradeitemoverridable = $grade_item->is_overridable_item();
 176          if (!$gradeitemoverridable) {
 177              $mform->removeElement('overridden');
 178          }
 179  
 180          if ($grade_item->is_hidden()) {
 181              $mform->hardFreeze('hidden');
 182          }
 183  
 184          if ($old_grade_grade->is_locked()) {
 185              if ($grade_item->is_locked()) {
 186                  $mform->hardFreeze('locked');
 187                  $mform->hardFreeze('locktime');
 188              }
 189  
 190              if ($gradeitemoverridable) {
 191                  $mform->hardFreeze('overridden');
 192              }
 193              $mform->hardFreeze('finalgrade');
 194              $mform->hardFreeze('feedback');
 195  
 196          } else {
 197              if (empty($old_grade_grade->id)) {
 198                  $old_grade_grade->locked = $grade_item->locked;
 199                  $old_grade_grade->locktime = $grade_item->locktime;
 200              }
 201  
 202              if (($old_grade_grade->locked or $old_grade_grade->locktime)
 203                and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context))) {
 204                  $mform->hardFreeze('locked');
 205                  $mform->hardFreeze('locktime');
 206  
 207              } else if ((!$old_grade_grade->locked and !$old_grade_grade->locktime)
 208                and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context))) {
 209                  $mform->hardFreeze('locked');
 210                  $mform->hardFreeze('locktime');
 211              }
 212          }
 213      }
 214  }
 215  
 216