Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 402] [Versions 310 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  /**
  18   * This file contains all necessary code to define a wiki editor
  19   *
  20   * @package mod_wiki
  21   * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
  22   * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
  23   *
  24   * @author Josep Arus
  25   *
  26   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  require_once($CFG->dirroot.'/lib/formslib.php');
  30  require_once($CFG->dirroot.'/lib/form/textarea.php');
  31  require_once($CFG->dirroot.'/lib/form/templatable_form_element.php');
  32  
  33  class MoodleQuickForm_wikieditor extends MoodleQuickForm_textarea {
  34      use templatable_form_element {
  35          export_for_template as export_for_template_base;
  36      }
  37  
  38      private $files;
  39  
  40      /**
  41       * Constructor
  42       *
  43       * @param string $elementName (optional) name of the text field
  44       * @param string $elementLabel (optional) text field label
  45       * @param string $attributes (optional) Either a typical HTML attribute string or an associative array
  46       */
  47      function __construct($elementName = null, $elementLabel = null, $attributes = null) {
  48          if (isset($attributes['wiki_format'])) {
  49              $this->wikiformat = $attributes['wiki_format'];
  50              unset($attributes['wiki_format']);
  51          }
  52          if (isset($attributes['files'])) {
  53              $this->files = $attributes['files'];
  54              unset($attributes['files']);
  55          }
  56          parent::__construct($elementName, $elementLabel, $attributes);
  57          $this->_type = 'wikieditor';
  58      }
  59  
  60      /**
  61       * Old syntax of class constructor. Deprecated in PHP7.
  62       *
  63       * @deprecated since Moodle 3.1
  64       */
  65      public function MoodleQuickForm_wikieditor($elementName = null, $elementLabel = null, $attributes = null) {
  66          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  67          self::__construct($elementName, $elementLabel, $attributes);
  68      }
  69  
  70      function setWikiFormat($wikiformat) {
  71          $this->wikiformat = $wikiformat;
  72      }
  73  
  74      function toHtml() {
  75          $textarea = parent::toHtml();
  76  
  77          return $this->{
  78              $this->wikiformat."Editor"}
  79              ($textarea);
  80      }
  81  
  82      function creoleEditor($textarea) {
  83          return $this->printWikiEditor($textarea);
  84      }
  85  
  86      function nwikiEditor($textarea) {
  87          return $this->printWikiEditor($textarea);
  88      }
  89  
  90      private function printWikiEditor($textarea) {
  91          global $OUTPUT;
  92  
  93          $textarea = $OUTPUT->container_start().$textarea.$OUTPUT->container_end();
  94  
  95          $buttons = $this->getButtons();
  96  
  97          return $buttons.$textarea;
  98      }
  99  
 100      private function getButtons() {
 101          global $PAGE, $OUTPUT, $CFG;
 102  
 103          $editor = $this->wikiformat;
 104  
 105          $tag = $this->getTokens($editor, 'bold');
 106          $wiki_editor['bold'] = array('ed_bold.gif', get_string('wikiboldtext', 'wiki'), $tag[0], $tag[1], get_string('wikiboldtext', 'wiki'));
 107  
 108          $tag = $this->getTokens($editor, 'italic');
 109          $wiki_editor['italic'] = array('ed_italic.gif', get_string('wikiitalictext', 'wiki'), $tag[0], $tag[1], get_string('wikiitalictext', 'wiki'));
 110  
 111          $imagetag = $this->getTokens($editor, 'image');
 112          $wiki_editor['image'] = array('ed_img.gif', get_string('wikiimage', 'wiki'), $imagetag[0], $imagetag[1], get_string('wikiimage', 'wiki'));
 113  
 114          $tag = $this->getTokens($editor, 'link');
 115          $wiki_editor['internal'] = array('ed_internal.gif', get_string('wikiinternalurl', 'wiki'), $tag[0], $tag[1], get_string('wikiinternalurl', 'wiki'));
 116  
 117          $tag = $this->getTokens($editor, 'url');
 118          $wiki_editor['external'] = array('ed_external.gif', get_string('wikiexternalurl', 'wiki'), $tag, "", get_string('wikiexternalurl', 'wiki'));
 119  
 120          $tag = $this->getTokens($editor, 'list');
 121          $wiki_editor['u_list'] = array('ed_ul.gif', get_string('wikiunorderedlist', 'wiki'), '\\n'.$tag[0], '', '');
 122          $wiki_editor['o_list'] = array('ed_ol.gif', get_string('wikiorderedlist', 'wiki'), '\\n'.$tag[1], '', '');
 123  
 124          $tag = $this->getTokens($editor, 'header');
 125          $wiki_editor['h1'] = array('ed_h1.gif', get_string('wikiheader', 'wiki', 1), '\\n'.$tag.' ', ' '.$tag.'\\n', get_string('wikiheader', 'wiki', 1));
 126          $wiki_editor['h2'] = array('ed_h2.gif', get_string('wikiheader', 'wiki', 2), '\\n'.$tag.$tag.' ', ' '.$tag.$tag.'\\n', get_string('wikiheader', 'wiki', 2));
 127          $wiki_editor['h3'] = array('ed_h3.gif', get_string('wikiheader', 'wiki', 3), '\\n'.$tag.$tag.$tag.' ', ' '.$tag.$tag.$tag.'\\n', get_string('wikiheader', 'wiki', 3));
 128  
 129          $tag = $this->getTokens($editor, 'line_break');
 130          $wiki_editor['hr'] = array('ed_hr.gif', get_string('wikihr', 'wiki'), '\\n'.$tag.'\\n', '', '');
 131  
 132          $tag = $this->getTokens($editor, 'nowiki');
 133          $wiki_editor['nowiki'] = array('ed_nowiki.gif', get_string('wikinowikitext', 'wiki'), $tag[0], $tag[1], get_string('wikinowikitext', 'wiki'));
 134  
 135          $PAGE->requires->js('/mod/wiki/editors/wiki/buttons.js');
 136  
 137          $html = '<div class="wikieditor-toolbar">';
 138          foreach ($wiki_editor as $button) {
 139              $html .= "<a href=\"javascript:insertTags";
 140              $html .= "('".$button[2]."','".$button[3]."','".$button[4]."');\">";
 141              $html .= html_writer::empty_tag('img', array('alt' => $button[1], 'src' => $CFG->wwwroot . '/mod/wiki/editors/wiki/images/' . $button[0]));
 142              $html .= "</a>";
 143          }
 144          $html .= "<label class='accesshide' for='addtags'>" . get_string('insertimage', 'wiki')  . "</label>";
 145          $html .= "<select id='addtags' class='custom-select mx-1' " .
 146                   "onchange=\"insertTags('{$imagetag[0]}', '{$imagetag[1]}', this.value)\">";
 147          $html .= "<option value='" . s(get_string('wikiimage', 'wiki')) . "'>" . get_string('insertimage', 'wiki') . '</option>';
 148          foreach ($this->files as $filename) {
 149              $html .= "<option value='".s($filename)."'>";
 150              $html .= $filename;
 151              $html .= '</option>';
 152          }
 153          $html .= '</select>';
 154          $html .= $OUTPUT->help_icon('insertimage', 'wiki');
 155          $html .= '</div>';
 156  
 157          return $html;
 158      }
 159  
 160      private function getTokens($format, $token) {
 161          $tokens = wiki_parser_get_token($format, $token);
 162  
 163          if (is_array($tokens)) {
 164              foreach ($tokens as & $t) {
 165                  $this->escapeToken($t);
 166              }
 167          } else {
 168              $this->escapeToken($tokens);
 169          }
 170  
 171          return $tokens;
 172      }
 173  
 174      private function escapeToken(&$token) {
 175          $token = urlencode(str_replace("'", "\'", $token));
 176      }
 177  
 178      public function export_for_template(renderer_base $output) {
 179          $context = $this->export_for_template_base($output);
 180  
 181          // We do want the form-control class on the output from toHTML - but we dont' want it when calling export_for_template.
 182          // This is because in this type of form element the export_for_template calls toHTML to get the html for the context.
 183          // If we did both we would be duplicating the form-control which messes up the styles.
 184          $saved = $this->getAttribute('class');
 185          $this->updateAttributes(['class' => $saved . ' form-control']);
 186  
 187          $context['html'] = $this->toHtml();
 188          $this->updateAttributes(['class' => $saved]);
 189  
 190          return $context;
 191      }
 192  }
 193  
 194  //register wikieditor
 195  MoodleQuickForm::registerElementType('wikieditor', $CFG->dirroot."/mod/wiki/editors/wikieditor.php", 'MoodleQuickForm_wikieditor');