Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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   * Drop down list (select list) 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   * Drop down list (select list) 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 dropdown_attribute extends element {
  37  
  38      /**
  39       * Who is selected?
  40       * @var string $selected
  41       */
  42      private $selected;
  43  
  44      /**
  45       * List of options
  46       * @var array $options
  47       */
  48      private $options;
  49  
  50      /**
  51       * Is this input disabled.
  52       * @var bool $isdisabled
  53       */
  54      private $isdisabled;
  55  
  56      /** @var bool If this is a read-only input. */
  57      private bool $isreadonly;
  58  
  59      /**
  60       * Constructor
  61       *
  62       * @param string $name The first bit of the name of this input.
  63       * @param array $options The options list for this select.
  64       * @param string $label The form label for this input.
  65       * @param string $selected The name of the selected item in this input.
  66       * @param bool $isdisabled Are we disabled?
  67       * @param bool $isreadonly If this is a read-only input.
  68       */
  69      public function __construct(
  70          string $name,
  71          array $options,
  72          string $label,
  73          string $selected = '',
  74          bool $isdisabled = false,
  75          bool $isreadonly = false
  76      ) {
  77          $this->selected = $selected;
  78          $this->options = $options;
  79          $this->isdisabled = $isdisabled;
  80          $this->isreadonly = $isreadonly;
  81          parent::__construct($name, $selected, $label);
  82      }
  83  
  84      /**
  85       * Nasty function spreading dropdown logic around.
  86       *
  87       * @return bool
  88       */
  89      public function is_dropdown(): bool {
  90          return true;
  91      }
  92  
  93      /**
  94       * Render this element as html.
  95       *
  96       * @return string
  97       */
  98      public function html(): string {
  99          global $OUTPUT;
 100  
 101          $options = $this->options;
 102          $selected = $this->selected;
 103  
 104          $context = [
 105              'name' => $this->name,
 106              'value' => $this->selected,
 107              'text' => $options[$selected],
 108              'disabled' => !empty($this->isdisabled),
 109              'readonly' => $this->isreadonly,
 110              'options' => array_map(function($option) use ($options, $selected) {
 111                  return [
 112                      'name' => $options[$option],
 113                      'value' => $option,
 114                      'selected' => $selected == $option
 115                  ];
 116              }, array_keys($options)),
 117              'label' => get_string('gradefor', 'gradereport_singleview', $this->label),
 118          ];
 119  
 120          return $OUTPUT->render_from_template('gradereport_singleview/dropdown_attribute', $context);
 121      }
 122  }