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] [Versions 401 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   * A checkbox ui 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  namespace gradereport_singleview\local\ui;
  25  
  26  use html_writer;
  27  
  28  defined('MOODLE_INTERNAL') || die;
  29  
  30  /**
  31   * A checkbox ui element.
  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 checkbox_attribute extends element {
  38  
  39      /** @var bool $ischecked Is it checked? */
  40      private $ischecked;
  41  
  42      /** @var bool If this is a read-only input. */
  43      private bool $isreadonly;
  44  
  45      /** @var int Is this element locked either 0 or a time. */
  46      protected int $locked;
  47  
  48      /**
  49       * Constructor
  50       *
  51       * @param string $name The element name
  52       * @param string $label The label for the form element
  53       * @param bool $ischecked Is this thing on?
  54       * @param int $locked Is this element locked either 0 or a time.
  55       * @param bool $isreadonly If this is a read-only input.
  56       */
  57      public function __construct(string $name, string $label, bool $ischecked = false, int $locked=0, bool $isreadonly = false) {
  58          $this->ischecked = $ischecked;
  59          $this->locked = $locked;
  60          $this->isreadonly = $isreadonly;
  61          parent::__construct($name, 1, $label);
  62      }
  63  
  64      /**
  65       * Nasty function allowing checkbox logic to escape the class.
  66       * @return bool
  67       */
  68      public function is_checkbox(): bool {
  69          return true;
  70      }
  71  
  72      /**
  73       * Generate the HTML
  74       *
  75       * @return string
  76       */
  77      public function html(): string {
  78          global $OUTPUT;
  79  
  80          $attributes = [
  81              'type' => 'checkbox',
  82              'name' => $this->name,
  83              'value' => 1,
  84              'id' => $this->name
  85          ];
  86  
  87          // UCSB fixed user should not be able to override locked grade.
  88          if ( $this->locked) {
  89              $attributes['disabled'] = 'DISABLED';
  90          }
  91  
  92          $hidden = [
  93              'type' => 'hidden',
  94              'name' => 'old' . $this->name
  95          ];
  96  
  97          if ($this->ischecked) {
  98              $attributes['checked'] = 'CHECKED';
  99              $hidden['value'] = 1;
 100          }
 101  
 102          $type = "override";
 103          if (preg_match("/^exclude/", $this->name)) {
 104              $type = "exclude";
 105          }
 106  
 107          if (!$this->isreadonly) {
 108              return (
 109                  html_writer::tag('label',
 110                                   get_string($type . 'for', 'gradereport_singleview', $this->label),
 111                                   ['for' => $this->name, 'class' => 'accesshide']) .
 112                  html_writer::empty_tag('input', $attributes) .
 113                  html_writer::empty_tag('input', $hidden)
 114              );
 115          } else if ($this->ischecked) {
 116              return $OUTPUT->pix_icon('i/checked', get_string('selected', 'core_form'),
 117                  'moodle', ['class' => 'overrideexcludecheck']);
 118          } else {
 119              return '';
 120          }
 121      }
 122  }