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.
   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 mod_data\manager;
  22  use moodle_url;
  23  use texteditor;
  24  
  25  /**
  26   * Renderable class for template editor.
  27   *
  28   * @package    mod_data
  29   * @copyright  2022 Ferran Recio <ferran@moodle.com>
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class template_editor implements templatable, renderable {
  33  
  34      /** @var manager manager instance. */
  35      private $manager;
  36  
  37      /** @var string the template name. */
  38      private $templatename;
  39  
  40      /**
  41       * The class constructor.
  42       *
  43       * @param manager $manager the activity instance manager
  44       * @param string $templatename the template to edit
  45       */
  46      public function __construct(manager $manager, string $templatename) {
  47          $this->manager = $manager;
  48          $this->templatename = $templatename;
  49      }
  50  
  51      /**
  52       * Export the data for the mustache template.
  53       *
  54       * @param \renderer_base $output renderer to be used to render the action bar elements.
  55       * @return array
  56       */
  57      public function export_for_template(\renderer_base $output): array {
  58          $instance = $this->manager->get_instance();
  59          $cm = $this->manager->get_coursemodule();
  60  
  61          $data = [
  62              'title' => get_string('header' . $this->templatename, 'data'),
  63              'sesskey' => sesskey(),
  64              'disableeditor' => true,
  65              'url' => new moodle_url('/mod/data/templates.php', ['id' => $cm->id, 'mode' => $this->templatename]),
  66          ];
  67  
  68          // Determine whether to use HTML editors.
  69          $usehtmleditor = false;
  70          $disableeditor = false;
  71          if (($this->templatename !== 'csstemplate') && ($this->templatename !== 'jstemplate')) {
  72              $usehtmleditor = data_get_config($instance, "editor_{$this->templatename}", true);
  73              $disableeditor = true;
  74          }
  75          $data['usehtmleditor'] = $usehtmleditor;
  76          // Some templates, like CSS, cannot enable the wysiwyg editor.
  77          $data['disableeditor'] = $disableeditor;
  78  
  79          $tools = new template_editor_tools($this->manager, $this->templatename);
  80          $data['toolbar'] = $tools->export_for_template($output);
  81          $data['editors'] = $this->get_editors_data($usehtmleditor);
  82  
  83          return $data;
  84      }
  85  
  86      /**
  87       * Get the editors data.
  88       *
  89       * @param bool $usehtmleditor if the user wants wysiwyg editor or not
  90       * @return array editors data
  91       */
  92      private function get_editors_data(bool $usehtmleditor): array {
  93          global $PAGE;
  94  
  95          $result = [];
  96          $manager = $this->manager;
  97          $instance = $manager->get_instance();
  98  
  99          // Setup editor.
 100          editors_head_setup();
 101          $PAGE->requires->js_call_amd(
 102              'mod_data/templateseditor',
 103              'init',
 104              ['d' => $instance->id, 'mode' => $this->templatename]
 105          );
 106  
 107          $format = FORMAT_PLAIN;
 108          if ($usehtmleditor) {
 109              $format = FORMAT_HTML;
 110          }
 111  
 112          $editor = editors_get_preferred_editor($format);
 113  
 114          // Add editors.
 115          if ($this->templatename === 'listtemplate') {
 116              $template = $manager->get_template('listtemplateheader');
 117              $result[] = $this->generate_editor_data(
 118                  $editor,
 119                  'header',
 120                  'listtemplateheader',
 121                  $template->get_template_content()
 122              );
 123              $maineditorname = 'multientry';
 124          } else {
 125              $maineditorname = $this->templatename;
 126          }
 127  
 128          $template = $manager->get_template($this->templatename);
 129          $result[] = $this->generate_editor_data(
 130              $editor,
 131              $maineditorname,
 132              $this->templatename,
 133              $template->get_template_content()
 134          );
 135  
 136          if ($this->templatename === 'listtemplate') {
 137              $template = $manager->get_template('listtemplatefooter');
 138              $result[] = $this->generate_editor_data(
 139                  $editor,
 140                  'footer',
 141                  'listtemplatefooter',
 142                  $template->get_template_content()
 143              );
 144          }
 145  
 146          if ($this->templatename === 'rsstemplate') {
 147              $template = $manager->get_template('rsstitletemplate');
 148              $result[] = $this->generate_editor_data(
 149                  $editor,
 150                  'rsstitletemplate',
 151                  'rsstitletemplate',
 152                  $template->get_template_content()
 153              );
 154          }
 155  
 156          return $result;
 157      }
 158  
 159      /**
 160       * Generate a single editor data.
 161       *
 162       * @param texteditor $editor the editor object
 163       * @param string $name the editor name
 164       * @param string $fieldname the field name
 165       * @param string|null $value the current value
 166       * @return array the editor data
 167       */
 168      private function generate_editor_data(
 169          texteditor $editor,
 170          string $name,
 171          string $fieldname,
 172          ?string $value
 173      ): array {
 174          $options = [
 175              'trusttext' => false,
 176              'forcehttps' => false,
 177              'subdirs' => false,
 178              'maxfiles' => 0,
 179              'maxbytes' => 0,
 180              'changeformat' => 0,
 181              'noclean' => false,
 182          ];
 183  
 184          $result = [
 185              'name' => get_string($name, 'data'),
 186              'fieldname' => $fieldname,
 187              'value' => $value,
 188          ];
 189          $editor->set_text($value);
 190          $editor->use_editor($fieldname, $options);
 191          return $result;
 192      }
 193  }