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 used to render a feedback input box.
  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   * Class used to render a feedback input box.
  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 feedback extends grade_attribute_format implements unique_value, be_disabled, be_readonly {
  37  
  38      /**
  39       * Name of this input
  40       * @var string $name
  41       */
  42      public $name = 'feedback';
  43  
  44      /**
  45       * Get the value for this input.
  46       *
  47       * @return string The value
  48       */
  49      public function get_value(): ?string {
  50          return $this->grade->feedback ? $this->grade->feedback : '';
  51      }
  52  
  53      /**
  54       * Get the string to use in the label for this input.
  55       *
  56       * @return string The label text
  57       */
  58      public function get_label(): string {
  59          if (!isset($this->grade->label)) {
  60              $this->grade->label = '';
  61          }
  62          return $this->grade->label;
  63      }
  64  
  65      /**
  66       * Determine if this input should be disabled based on the other settings.
  67       *
  68       * @return boolean Should this input be disabled when the page loads.
  69       */
  70      public function is_disabled(): bool {
  71          $locked = 0;
  72          $gradeitemlocked = 0;
  73          $overridden = 0;
  74          /* Disable editing if grade item or grade score is locked
  75          * if any of these items are set,  then we will disable editing
  76          * at some point, we might want to show the reason for the lock
  77          * this code could be simplified, but its more readable for steve's little mind
  78          */
  79          if (!empty($this->grade->locked)) {
  80              $locked = 1;
  81          }
  82          if (!empty($this->grade->grade_item->locked)) {
  83              $gradeitemlocked = 1;
  84          }
  85          if ($this->grade->grade_item->is_overridable_item() and !$this->grade->is_overridden()) {
  86              $overridden = 1;
  87          }
  88          return ($locked || $gradeitemlocked || $overridden);
  89      }
  90  
  91      /**
  92       * Return true if this is read-only.
  93       *
  94       * @return bool
  95       */
  96      public function is_readonly(): bool {
  97          global $USER;
  98          return empty($USER->editing);
  99      }
 100  
 101      /**
 102       * Create a text_attribute for this ui element.
 103       *
 104       * @return element
 105       */
 106      public function determine_format(): element {
 107          return new text_attribute(
 108              $this->get_name(),
 109              $this->get_value(),
 110              $this->get_label(),
 111              $this->is_disabled(),
 112              $this->is_readonly()
 113          );
 114      }
 115  
 116      /**
 117       * Update the value for this input.
 118       *
 119       * @param string $value The new feedback value.
 120       * @return null|string Any error message
 121       */
 122      public function set($value) {
 123          $finalgrade = false;
 124          $trimmed = trim($value);
 125          if (empty($trimmed)) {
 126              $feedback = null;
 127          } else {
 128              $feedback = $value;
 129          }
 130  
 131          $this->grade->grade_item->update_final_grade(
 132              $this->grade->userid, $finalgrade, 'singleview',
 133              $feedback, FORMAT_MOODLE
 134          );
 135      }
 136  }