Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

   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   * Class containing data for list_templates page
  19   *
  20   * @package    tool_templatelibrary
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_templatelibrary\output;
  25  
  26  use renderable;
  27  use templatable;
  28  use renderer_base;
  29  use stdClass;
  30  use core_collator;
  31  use core_component;
  32  use core_plugin_manager;
  33  use tool_templatelibrary\api;
  34  
  35  /**
  36   * Class containing data for list_templates page
  37   *
  38   * @copyright  2015 Damyon Wiese
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class list_templates_page implements renderable, templatable {
  42  
  43      /** @var string $component The currently selected component */
  44      protected $component;
  45      /** @var string $search The current search */
  46      protected $search;
  47  
  48      /**
  49       * Template page constructor
  50       *
  51       * @param string $component
  52       * @param string $search
  53       */
  54      public function __construct(string $component = '', string $search = '') {
  55          $this->component = $component;
  56          $this->search = $search;
  57      }
  58  
  59      /**
  60       * Export this data so it can be used as the context for a mustache template.
  61       *
  62       * @return stdClass
  63       */
  64      public function export_for_template(renderer_base $output) {
  65          $fulltemplatenames = api::list_templates();
  66          $pluginmanager = core_plugin_manager::instance();
  67          $components = [];
  68  
  69          foreach ($fulltemplatenames as $templatename) {
  70              [$component, ] = explode('/', $templatename, 2);
  71              [$type, ] = core_component::normalize_component($component);
  72  
  73              // Core sub-systems are grouped together and are denoted by a distinct lang string.
  74              $coresubsystem = (strpos($component, 'core') === 0);
  75  
  76              if (!array_key_exists($type, $components)) {
  77                  $typename = $coresubsystem
  78                      ? get_string('core', 'tool_templatelibrary')
  79                      : $pluginmanager->plugintype_name_plural($type);
  80  
  81                  $components[$type] = (object) [
  82                      'type' => $typename,
  83                      'plugins' => [],
  84                  ];
  85              }
  86  
  87              $pluginname = $coresubsystem
  88                  ? get_string('coresubsystem', 'tool_templatelibrary', $component)
  89                  : $pluginmanager->plugin_name($component);
  90  
  91              $components[$type]->plugins[$component] = (object) [
  92                  'name' => $pluginname,
  93                  'component' => $component,
  94                  'selected' => ($component === $this->component),
  95              ];
  96          }
  97  
  98          // Sort returned components according to their type, followed by name.
  99          core_collator::asort_objects_by_property($components, 'type');
 100          array_walk($components, function(stdClass $component) {
 101              core_collator::asort_objects_by_property($component->plugins, 'name');
 102              $component->plugins = array_values($component->plugins);
 103          });
 104  
 105          return (object) [
 106              'allcomponents' => array_values($components),
 107              'search' => $this->search,
 108          ];
 109      }
 110  }