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]

   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   * An override grade checkbox element
  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  /**
  30   * An override grade checkbox element
  31   *
  32   * @package   gradereport_singleview
  33   * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class override extends grade_attribute_format implements be_checked, be_disabled, be_readonly {
  37  
  38      /**
  39       * The name for this input
  40       * @var string $name
  41       */
  42      public $name = 'override';
  43  
  44      /**
  45       * Is this input checked
  46       *
  47       * @return bool
  48       */
  49      public function is_checked(): bool {
  50          return $this->grade->is_overridden();
  51      }
  52  
  53      /**
  54       * Is this input disabled
  55       *
  56       * @return bool
  57       */
  58      public function is_disabled(): bool {
  59          $lockedgrade = $lockedgradeitem = 0;
  60          if (!empty($this->grade->locked)) {
  61              $lockedgrade = 1;
  62          }
  63          if (!empty($this->grade->grade_item->locked)) {
  64              $lockedgradeitem = 1;
  65          }
  66          return ($lockedgrade || $lockedgradeitem);
  67      }
  68  
  69      /**
  70       * Return true if this is read-only.
  71       *
  72       * @return bool
  73       */
  74      public function is_readonly(): bool {
  75          global $USER;
  76          return empty($USER->editing);
  77      }
  78  
  79      /**
  80       * Get the label for this form element.
  81       *
  82       * @return string
  83       */
  84      public function get_label(): string {
  85          if (!isset($this->grade->label)) {
  86              $this->grade->label = '';
  87          }
  88          return $this->grade->label;
  89      }
  90  
  91      /**
  92       * Generate the element for this form input.
  93       *
  94       * @return element
  95       */
  96      public function determine_format(): element {
  97          if (!$this->grade->grade_item->is_overridable_item()) {
  98              return new empty_element();
  99          }
 100          return new checkbox_attribute(
 101              $this->get_name(),
 102              $this->get_label(),
 103              $this->is_checked(),
 104              $this->is_disabled(),
 105              $this->is_readonly()
 106          );
 107      }
 108  
 109      /**
 110       * Save the modified value of this form element.
 111       *
 112       * @param string $value The new value to set
 113       * @return mixed string|false Any error message
 114       */
 115      public function set($value) {
 116          if (empty($this->grade->id)) {
 117              return false;
 118          }
 119  
 120          $state = !($value == 0);
 121  
 122          $this->grade->set_overridden($state);
 123          $this->grade->grade_item->force_regrading();
 124          return false;
 125      }
 126  }