Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 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 mod_data\output;
  18  
  19  use templatable;
  20  use renderable;
  21  use core_tag_tag;
  22  use mod_data\manager;
  23  
  24  /**
  25   * Renderable class for template editor tools.
  26   *
  27   * @package    mod_data
  28   * @copyright  2022 Ferran Recio <ferran@moodle.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class template_editor_tools implements templatable, renderable {
  32  
  33      /** @var manager manager instance. */
  34      private $manager;
  35  
  36      /** @var string the template name. */
  37      private $templatename;
  38  
  39      /**
  40       * The class constructor.
  41       *
  42       * @param manager $manager the activity instance manager
  43       * @param string $templatename the template to edit
  44       */
  45      public function __construct(manager $manager, string $templatename) {
  46          $this->manager = $manager;
  47          $this->templatename = $templatename;
  48      }
  49  
  50      /**
  51       * Export the data for the mustache template.
  52       *
  53       * @param \renderer_base $output renderer to be used to render the action bar elements.
  54       * @return array
  55       */
  56      public function export_for_template(\renderer_base $output): array {
  57          $tools = [
  58              $this->get_field_tags($this->templatename),
  59              $this->get_field_id_tags($this->templatename),
  60              $this->get_action_tags($this->templatename),
  61              $this->get_other_tags($this->templatename),
  62          ];
  63          $tools = array_filter($tools, static function ($value) {
  64              return !empty($value['tags']);
  65          });
  66          return [
  67              'toolshelp' => $output->help_icon('availabletags', 'data'),
  68              'hastools' => !empty($tools),
  69              'tools' => array_values($tools),
  70          ];
  71      }
  72  
  73      /**
  74       * Return the field template tags.
  75       *
  76       * @param string $templatename the template name
  77       * @return array|null array of tags.
  78       */
  79      protected function get_field_tags(string $templatename): array {
  80          $name = get_string('fields', 'data');
  81          if ($templatename == 'csstemplate' || $templatename == 'jstemplate') {
  82              return $this->get_optgroup_data($name, []);
  83          }
  84          $taglist = [];
  85          $fields = $this->manager->get_fields();
  86          foreach ($fields as $field) {
  87              if ($field->type === 'unknown') {
  88                  continue;
  89              }
  90              $fieldname = $field->get_name();
  91              $taglist["[[$fieldname]]"] = $fieldname;
  92          }
  93          return $this->get_optgroup_data($name, $taglist);
  94      }
  95  
  96      /**
  97       * Return the field IDs template tags.
  98       *
  99       * @param string $templatename the template name
 100       * @return array|null array of tags.
 101       */
 102      protected function get_field_id_tags(string $templatename): array {
 103          $name = get_string('fieldids', 'data');
 104          if ($templatename != 'addtemplate') {
 105              return $this->get_optgroup_data($name, []);
 106          }
 107          $taglist = [];
 108          // Field IDs.
 109          $fields = $this->manager->get_fields();
 110          foreach ($fields as $field) {
 111              if ($field->type === 'unknown') {
 112                  continue;
 113              }
 114              $fieldname = $field->get_name();
 115              $taglist["[[$fieldname#id]]"] = "$fieldname id";
 116          }
 117          return $this->get_optgroup_data($name, $taglist);
 118      }
 119  
 120      /**
 121       * Return the field action tags.
 122       *
 123       * @param string $templatename the template name
 124       * @return array|null array of tags.
 125       */
 126      protected function get_action_tags(string $templatename): array {
 127          $name = get_string('actions');
 128          if ($templatename == 'addtemplate' || $templatename == 'asearchtemplate') {
 129              return $this->get_optgroup_data($name, []);
 130          }
 131          $taglist = [
 132              '##edit##' => get_string('edit', 'data'),
 133              '##delete##' => get_string('delete', 'data'),
 134              '##approve##' => get_string('approve', 'data'),
 135              '##disapprove##' => get_string('disapprove', 'data'),
 136              '##actionsmenu##' => get_string('actionsmenu', 'data'),
 137          ];
 138          if ($templatename != 'rsstemplate') {
 139              $taglist['##export##'] = get_string('export', 'data');
 140          }
 141          if ($templatename != 'singletemplate') {
 142              $taglist['##more##'] = get_string('more', 'data');
 143              $taglist['##moreurl##'] = get_string('moreurl', 'data');
 144              $taglist['##delcheck##'] = get_string('delcheck', 'data');
 145          }
 146          return $this->get_optgroup_data($name, $taglist);
 147      }
 148  
 149      /**
 150       * Return the available other tags
 151       *
 152       * @param string $templatename the template name
 153       * @return array associative array of tags => tag name
 154       */
 155      protected function get_other_tags(string $templatename): array {
 156          $name = get_string('other', 'data');
 157          $taglist = [];
 158          if ($templatename == 'asearchtemplate') {
 159              $taglist['##firstname##'] = get_string('firstname');
 160              $taglist['##lastname##'] = get_string('lastname');
 161              return $this->get_optgroup_data($name, $taglist);
 162          }
 163          if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
 164              $taglist['##tags##'] = get_string('tags');
 165          }
 166          if ($templatename == 'addtemplate') {
 167              return $this->get_optgroup_data($name, $taglist);
 168          }
 169          $taglist['##timeadded##'] = get_string('timeadded', 'data');
 170          $taglist['##timemodified##'] = get_string('timemodified', 'data');
 171          $taglist['##user##'] = get_string('user');
 172          $taglist['##userpicture##'] = get_string('userpic');
 173          $taglist['##approvalstatus##'] = get_string('approvalstatus', 'data');
 174          $taglist['##id##'] = get_string('id', 'data');
 175  
 176          if ($templatename == 'singletemplate') {
 177              return $this->get_optgroup_data($name, $taglist);
 178          }
 179  
 180          $taglist['##comments##'] = get_string('comments', 'data');
 181  
 182          return $this->get_optgroup_data($name, $taglist);
 183      }
 184  
 185      /**
 186       * Generate a valid optgroup data.
 187       *
 188       * @param string $name the optgroup name
 189       * @param array $taglist the indexed array of taglists ($tag => $tagname)
 190       * @return array of optgroup data
 191       */
 192      protected function get_optgroup_data (string $name, array $taglist): array {
 193          $tags = [];
 194          foreach ($taglist as $tag => $tagname) {
 195              $tags[] = [
 196                  'tag' => "$tag",
 197                  'tagname' => $tagname . ' - ' . $tag,
 198              ];
 199          }
 200          return [
 201              'name' => $name,
 202              'tags' => $tags,
 203          ];
 204      }
 205  }