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.
   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_grades\output;
  18  
  19  use moodle_exception;
  20  use renderable;
  21  use renderer_base;
  22  use templatable;
  23  
  24  /**
  25   * Renderable class for the dropdown in the gradebook pages.
  26   *
  27   * We have opted to have this as a class as opposed to a renderable for prosperity
  28   * in the case that custom handling is required by the calling code.
  29   *
  30   * This could become a abstract class if other components require similar functionality and wish to extend the base here.
  31   *
  32   * @package    core_grades
  33   * @copyright  2022 Mathew May <Mathew.solutions>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class gradebook_dropdown implements renderable, templatable {
  37  
  38      /** @var bool $renderlater Should the dropdown render straightaway? */
  39      protected $renderlater;
  40  
  41      /** @var string $buttoncontent What is the content of the "Button" that users will always see. */
  42      protected $buttoncontent;
  43  
  44      /** @var null|string $dropdowncontent The content that can be passed in to render immediately. */
  45      protected $dropdowncontent;
  46  
  47      /** @var null|string $parentclasses Any special classes to put on the HTMLElement that contains the BS events. */
  48      protected $parentclasses;
  49  
  50      /** @var null|string $buttonclasses Any special classes to put on the HTMLElement that triggers the dropdown. */
  51      protected $buttonclasses;
  52  
  53      /** @var null|string $dropdownclasses Any special classes to put on the HTMLElement that contains the actual dropdown. */
  54      protected $dropdownclasses;
  55  
  56      /** @var null|string $buttonheader If the button item in the tertiary nav needs an extra top header for context. */
  57      protected $buttonheader;
  58  
  59      /** @var boolean $usesbutton Whether to provide a A11y button. */
  60      protected $usesbutton;
  61  
  62      /**
  63       * The class constructor.
  64       *
  65       * @param bool $renderlater How we figure out if we should render the template instantly.
  66       * @param string $buttoncontent What gets placed in the button.
  67       * @param ?string $dropdowncontent What can be placed in the dropdown if we are rendering now.
  68       * @param ?string $parentclasses The classes that can be added that the bootstrap events are attached to.
  69       * @param ?string $buttonclasses Any special classes that may be needed.
  70       * @param ?string $dropdownclasses Any special classes that may be needed.
  71       * @param ?string $buttonheader If the button item in the tertiary nav needs an extra top header for context.
  72       * @param bool $usebutton If we want the mustache to add the button roles for us or do we have another aria role node?
  73       * @throws moodle_exception If the implementor incorrectly call this module.
  74       */
  75      public function __construct(
  76          bool $renderlater,
  77          string $buttoncontent,
  78          ?string $dropdowncontent = null,
  79          ?string $parentclasses = null,
  80          ?string $buttonclasses = null,
  81          ?string $dropdownclasses = null,
  82          ?string $buttonheader = null,
  83          ?bool $usebutton = true
  84      ) {
  85          // Ensure implementors cant request to render the content now and not provide us any to show.
  86          if (!$renderlater && empty($dropdowncontent)) {
  87              throw new moodle_exception(
  88                  'incorrectdropdownvars',
  89                  'core_grades',
  90                  '', null,
  91                  'Dropdown content must be set to render later.'
  92              );
  93          }
  94  
  95          $this->renderlater = $renderlater;
  96          $this->buttoncontent = $buttoncontent;
  97          $this->dropdowncontent = $dropdowncontent;
  98          $this->parentclasses = $parentclasses;
  99          $this->buttonclasses = $buttonclasses;
 100          $this->dropdownclasses = $dropdownclasses;
 101          $this->buttonheader = $buttonheader;
 102          $this->usesbutton = $usebutton;
 103      }
 104  
 105      /**
 106       * Export the data for the mustache template.
 107       *
 108       * @param renderer_base $output renderer to be used to render the action bar elements.
 109       * @return array
 110       */
 111      public function export_for_template(renderer_base $output): array {
 112          return [
 113              'rtl' => right_to_left(),
 114              'renderlater' => $this->renderlater,
 115              'buttoncontent' => $this->buttoncontent ,
 116              'dropdowncontent' => $this->dropdowncontent,
 117              'parentclasses' => $this->parentclasses,
 118              'buttonclasses' => $this->buttonclasses,
 119              'dropdownclasses' => $this->dropdownclasses,
 120              'buttonheader' => $this->buttonheader,
 121              'usebutton' => $this->usesbutton,
 122          ];
 123      }
 124  
 125      /**
 126       * Returns the standard template for the dropdown.
 127       *
 128       * @return string
 129       */
 130      public function get_template(): string {
 131          return 'core_grades/tertiary_navigation_dropdown';
 132      }
 133  }