Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * UI element representing the finalgrade column.
  19   *
  20   * @package   gradereport_singleview
  21   * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace gradereport_singleview\local\ui;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  use stdClass;
  30  /**
  31   * UI element representing the finalgrade column.
  32   *
  33   * @package   gradereport_singleview
  34   * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class finalgrade extends grade_attribute_format implements unique_value, be_disabled {
  38  
  39      /** @var string $name Name of this input */
  40      public $name = 'finalgrade';
  41  
  42      /**
  43       * Get the value for this input.
  44       *
  45       * @return string The value based on the grade_grade.
  46       */
  47      public function get_value() {
  48          $this->label = $this->grade->grade_item->itemname;
  49  
  50          $val = $this->grade->finalgrade;
  51          if ($this->grade->grade_item->scaleid) {
  52              return $val ? (int)$val : -1;
  53          } else {
  54              return $val ? format_float($val, $this->grade->grade_item->get_decimals()) : '';
  55          }
  56      }
  57  
  58      /**
  59       * Get the label for this input.
  60       *
  61       * @return string The label for this form input.
  62       */
  63      public function get_label() {
  64          if (!isset($this->grade->label)) {
  65              $this->grade->label = '';
  66          }
  67          return $this->grade->label;
  68      }
  69  
  70      /**
  71       * Is this input field disabled.
  72       *
  73       * @return bool Set disabled on this input or not.
  74       */
  75      public function is_disabled() {
  76          $locked = 0;
  77          $gradeitemlocked = 0;
  78          $overridden = 0;
  79  
  80          // Disable editing if grade item or grade score is locked
  81          // if any of these items are set,  then we will disable editing
  82          // at some point, we might want to show the reason for the lock
  83          // this code could be simplified, but its more readable for steve's little mind.
  84  
  85          if (!empty($this->grade->locked)) {
  86              $locked = 1;
  87          }
  88          if (!empty($this->grade->grade_item->locked)) {
  89              $gradeitemlocked = 1;
  90          }
  91          if ($this->grade->grade_item->is_overridable_item() and !$this->grade->is_overridden()) {
  92              $overridden = 1;
  93          }
  94          return ($locked || $gradeitemlocked || $overridden);
  95      }
  96  
  97      /**
  98       * Create the element for this column.
  99       *
 100       * @return element
 101       */
 102      public function determine_format() {
 103          if ($this->grade->grade_item->load_scale()) {
 104              $scale = $this->grade->grade_item->load_scale();
 105  
 106              $options = array(-1 => get_string('nograde'));
 107  
 108              foreach ($scale->scale_items as $i => $name) {
 109                  $options[$i + 1] = $name;
 110              }
 111  
 112              return new dropdown_attribute(
 113                  $this->get_name(),
 114                  $options,
 115                  $this->get_label(),
 116                  $this->get_value(),
 117                  $this->is_disabled()
 118              );
 119          } else {
 120              return new text_attribute(
 121                  $this->get_name(),
 122                  $this->get_value(),
 123                  $this->get_label(),
 124                  $this->is_disabled()
 125              );
 126          }
 127      }
 128  
 129      /**
 130       * Save the altered value for this field.
 131       *
 132       * @param string $value The new value.
 133       * @return string Any error string
 134       */
 135      public function set($value) {
 136          global $DB;
 137  
 138          $userid = $this->grade->userid;
 139          $gradeitem = $this->grade->grade_item;
 140  
 141          $feedback = false;
 142          $feedbackformat = false;
 143          if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
 144              if ($value == -1) {
 145                  $finalgrade = null;
 146              } else {
 147                  $finalgrade = $value;
 148              }
 149          } else {
 150              $finalgrade = unformat_float($value);
 151          }
 152  
 153          $errorstr = '';
 154          if ($finalgrade) {
 155              $bounded = $gradeitem->bounded_grade($finalgrade);
 156              if ($bounded > $finalgrade) {
 157                  $errorstr = 'lessthanmin';
 158              } else if ($bounded < $finalgrade) {
 159                  $errorstr = 'morethanmax';
 160              }
 161          }
 162  
 163          if ($errorstr) {
 164              $user = get_complete_user_data('id', $userid);
 165              $gradestr = new stdClass;
 166              if (has_capability('moodle/site:viewfullnames', \context_course::instance($gradeitem->courseid))) {
 167                  $gradestr->username = fullname($user, true);
 168              } else {
 169                  $gradestr->username = fullname($user);
 170              }
 171              $gradestr->itemname = $this->grade->grade_item->get_name();
 172              $errorstr = get_string($errorstr, 'grades', $gradestr);
 173              return $errorstr;
 174          }
 175  
 176          // Only update grades if there are no errors.
 177          $gradeitem->update_final_grade($userid, $finalgrade, 'singleview', $feedback, FORMAT_MOODLE);
 178          return '';
 179      }
 180  }