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   * Class that represents the exclude checkbox on a grade_grade.
  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 grade_grade;
  30  
  31  /**
  32   * Class that represents the exclude checkbox on a grade_grade.
  33   *
  34   * @package   gradereport_singleview
  35   * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class exclude extends grade_attribute_format implements be_checked, be_disabled, be_readonly {
  39  
  40      /**
  41       * The name of the input
  42       * @var string $name
  43       */
  44      public $name = 'exclude';
  45  
  46      /**
  47       * Is the checkbox disabled?
  48       * @var bool $disabled
  49       */
  50      public $disabled = false;
  51  
  52      /**
  53       * Is it checked?
  54       *
  55       * @return bool
  56       */
  57      public function is_checked(): bool {
  58          return $this->grade->is_excluded();
  59      }
  60  
  61      /**
  62       * Is it disabled?
  63       *
  64       * @return bool
  65       */
  66      public function is_disabled(): bool {
  67          return $this->disabled;
  68      }
  69  
  70      /**
  71       * Return true if this is read-only.
  72       *
  73       * @return bool
  74       */
  75      public function is_readonly(): bool {
  76          global $USER;
  77          return empty($USER->editing);
  78      }
  79  
  80      /**
  81       * Generate the element used to render the UI
  82       *
  83       * @return element
  84       */
  85      public function determine_format(): element {
  86          return new checkbox_attribute(
  87              $this->get_name(),
  88              $this->get_label(),
  89              $this->is_checked(),
  90              $this->is_disabled(),
  91              $this->is_readonly()
  92          );
  93      }
  94  
  95      /**
  96       * Get the label for the form input
  97       *
  98       * @return string
  99       */
 100      public function get_label(): string {
 101          if (!isset($this->grade->label)) {
 102              $this->grade->label = '';
 103          }
 104          return $this->grade->label;
 105      }
 106  
 107      /**
 108       * Set the value that was changed in the form.
 109       *
 110       * @param string $value The value to set.
 111       * @return mixed string|bool An error message or false.
 112       */
 113      public function set($value) {
 114          if (empty($this->grade->id)) {
 115              if (empty($value)) {
 116                  return false;
 117              }
 118  
 119              $gradeitem = $this->grade->grade_item;
 120  
 121              // Fill in arbitrary grade to be excluded.
 122              $gradeitem->update_final_grade(
 123                  $this->grade->userid, null, 'singleview', null, FORMAT_MOODLE
 124              );
 125  
 126              $gradeparams = [
 127                  'userid' => $this->grade->userid,
 128                  'itemid' => $this->grade->itemid
 129              ];
 130  
 131              $this->grade = grade_grade::fetch($gradeparams);
 132              $this->grade->grade_item = $gradeitem;
 133          }
 134  
 135          $state = !($value == 0);
 136  
 137          $this->grade->set_excluded($state);
 138  
 139          $this->grade->grade_item->get_parent_category()->force_regrading();
 140          return false;
 141      }
 142  }