Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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  namespace core\output\local\dropdown;
  18  
  19  use core\output\choicelist;
  20  
  21  /**
  22   * Class to render a dropdown dialog element.
  23   *
  24   * A dropdown dialog allows to render any arbitrary HTML into a dropdown elements triggered
  25   * by a button.
  26   *
  27   * @package    core
  28   * @category   output
  29   * @copyright  2023 Ferran Recio <ferran@moodle.com>
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class status extends dialog {
  33  
  34      /**
  35       * @var choicelist content of dialog.
  36       */
  37      protected $choices = null;
  38  
  39      /**
  40       * Constructor.
  41       *
  42       * The definition object could contain the following keys:
  43       * - classes: component CSS classes.
  44       * - buttonclasses: the button CSS classes.
  45       * - dialogwidth: the dropdown width.
  46       * - extras: extra HTML attributes (attribute => value).
  47       * - buttonsync: if the button should be synced with the selected value.
  48       * - updatestatus: if component must update the status and trigger a change event when clicked.
  49       *
  50       * @param string $buttoncontent the button content
  51       * @param choicelist $choices the choice object
  52       * @param array $definition an optional array of the element definition
  53       */
  54      public function __construct(string $buttoncontent, choicelist $choices, array $definition = []) {
  55          parent::__construct($buttoncontent, '', $definition);
  56          $this->set_choice($choices);
  57          if ($definition['buttonsync'] ?? false) {
  58              $this->extras['data-button-sync'] = 'true';
  59          }
  60          if ($definition['updatestatus'] ?? false) {
  61              $this->extras['data-update-status'] = 'true';
  62          }
  63      }
  64  
  65      /**
  66       * Set the dialog contents.
  67       *
  68       * @param choicelist $choices
  69       */
  70      public function set_choice(choicelist $choices) {
  71          $this->choices = $choices;
  72          $description = $choices->get_description();
  73          if (!empty($description)) {
  74              $this->set_content($description);
  75          }
  76      }
  77  
  78      /**
  79       * Export this data so it can be used as the context for a mustache template (core/inplace_editable).
  80       *
  81       * @param \renderer_base $output typically, the renderer that's calling this function
  82       * @return array data context for a mustache template
  83       */
  84      public function export_for_template(\renderer_base $output): array {
  85          $data = parent::export_for_template($output);
  86          if ($this->choices !== null) {
  87              $data['choices'] = $this->choices->export_for_template($output);
  88          }
  89          $selectedvalue = $this->choices->get_selected_value();
  90          if ($selectedvalue !== null) {
  91              $data['extras'][] = (object)[
  92                  'attribute' => 'data-value',
  93                  'value' => $selectedvalue,
  94              ];
  95          }
  96          return $data;
  97      }
  98  
  99      /**
 100       * Get the name of the template to use for this templatable.
 101       *
 102       * @param \renderer_base $renderer The renderer requesting the template name
 103       * @return string the template name
 104       */
 105      public function get_template_name(\renderer_base $renderer): string {
 106          return 'core/local/dropdown/status';
 107      }
 108  }