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  declare(strict_types=1);
  18  
  19  namespace core\output\dynamic_tabs;
  20  
  21  use moodle_exception;
  22  use templatable;
  23  
  24  /**
  25   * Class tab_base
  26   *
  27   * @package     core
  28   * @copyright   2018 Marina Glancy
  29   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  abstract class base implements templatable {
  32  
  33      /** @var array */
  34      protected $data;
  35  
  36      /**
  37       * tab constructor.
  38       *
  39       * @param array $data
  40       */
  41      final public function __construct(array $data) {
  42          $this->data = $data;
  43      }
  44  
  45      /**
  46       * HTML "id" attribute that should be used for this tab, by default the last part of class name
  47       *
  48       * @return string
  49       */
  50      public function get_tab_id(): string {
  51          $parts = preg_split('/\\\\/', static::class);
  52          return array_pop($parts);
  53      }
  54  
  55      /**
  56       * The label to be displayed on the tab
  57       *
  58       * @return string
  59       */
  60      abstract public function get_tab_label(): string;
  61  
  62      /**
  63       * Check permission of the current user to access this tab
  64       *
  65       * @return bool
  66       */
  67      abstract public function is_available(): bool;
  68  
  69      /**
  70       * Check that tab is accessible, throw exception otherwise - used from WS requesting tab contents
  71       *
  72       * @throws moodle_exception
  73       */
  74      final public function require_access() {
  75          if (!$this->is_available()) {
  76              throw new moodle_exception('nopermissiontoaccesspage', 'error');
  77          }
  78      }
  79  
  80      /**
  81       * Template to use to display tab contents
  82       *
  83       * @return string
  84       */
  85      abstract public function get_template(): string;
  86  
  87      /**
  88       * Return tab data attributes
  89       *
  90       * @return array
  91       */
  92      public function get_data(): array {
  93          return $this->data;
  94      }
  95  
  96      /**
  97       * Add custom data to the tab data attributes
  98       *
  99       * @param array $data
 100       */
 101      public function add_data(array $data): void {
 102          $this->data = array_merge($this->data, $data);
 103      }
 104  }