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.

Differences Between: [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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_question\output;
  18  
  19  use core_question\local\bank\navigation_node_base;
  20  use core_question\local\bank\plugin_features_base;
  21  use moodle_url;
  22  use renderer_base;
  23  use templatable;
  24  use renderable;
  25  use url_select;
  26  
  27  /**
  28   * Rendered HTML elements for tertiary nav for Question bank.
  29   *
  30   * Provides a menu of links for question bank tertiary navigation, based on get_navigation_node() implemented by each plugin.
  31   * Optionally includes and additional action button to display alongside the menu.
  32   *
  33   * @package   core_question
  34   * @copyright 2021 Sujith Haridasan <sujith@moodle.com>
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class qbank_action_menu implements templatable, renderable {
  38      /** @var moodle_url */
  39      private $currenturl;
  40  
  41      /** @var ?moodle_url $actionurl URL for additional action button */
  42      protected ?moodle_url $actionurl = null;
  43  
  44      /** @var ?string $actionlabel Label for additional action button  */
  45      protected ?string $actionlabel = null;
  46  
  47      /**
  48       * qbank_actionbar constructor.
  49       *
  50       * @param moodle_url $currenturl The current URL.
  51       */
  52      public function __construct(moodle_url $currenturl) {
  53          $this->currenturl = $currenturl;
  54      }
  55  
  56      /**
  57       * Set the properties of an additional action button specific to the current page.
  58       *
  59       * @param moodle_url $url
  60       * @param string $label
  61       * @return void
  62       */
  63      public function set_action_button(moodle_url $url, string $label): void {
  64          $this->actionurl = $url;
  65          $this->actionlabel = $label;
  66      }
  67  
  68      /**
  69       * Provides the data for the template.
  70       *
  71       * @param renderer_base $output renderer_base object.
  72       * @return array data for the template
  73       */
  74      public function export_for_template(renderer_base $output): array {
  75          $questionslink = new moodle_url('/question/edit.php', $this->currenturl->params());
  76          $menu = [
  77              $questionslink->out(false) => get_string('questions', 'question'),
  78          ];
  79          $plugins = \core_component::get_plugin_list_with_class('qbank', 'plugin_feature', 'plugin_feature.php');
  80          foreach ($plugins as $componentname => $pluginfeaturesclass) {
  81              if (!\core\plugininfo\qbank::is_plugin_enabled($componentname)) {
  82                  continue;
  83              }
  84              /** @var plugin_features_base $pluginfeatures */
  85              $pluginfeatures = new $pluginfeaturesclass();
  86              $navigationnode = $pluginfeatures->get_navigation_node();
  87              if (is_null($navigationnode)) {
  88                  continue;
  89              }
  90              /** @var moodle_url $url */
  91              $url = $navigationnode->get_navigation_url();
  92              $url->params($this->currenturl->params());
  93              $menu[$url->out(false)] = $navigationnode->get_navigation_title();
  94          }
  95  
  96          $actionbutton = null;
  97          if ($this->actionurl) {
  98              $actionbutton = [
  99                  'url' => $this->actionurl->out(false),
 100                  'label' => $this->actionlabel,
 101              ];
 102          }
 103  
 104          $urlselect = new url_select($menu, $this->currenturl->out(false), null, 'questionbankaction');
 105          $urlselect->set_label(get_string('questionbanknavigation', 'question'), ['class' => 'accesshide']);
 106  
 107          return [
 108              'questionbankselect' => $urlselect->export_for_template($output),
 109              'actionbutton' => $actionbutton
 110          ];
 111      }
 112  }