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  /**
   3   * This file defines a simple editor
   4   *
   5   * @author Jordi Piguillem
   6   * @author Kenneth Riba
   7   *
   8   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
   9   * @package mod_wiki
  10   *
  11   */
  12  
  13  /**
  14   * Printing wiki editor.
  15   * Depending on where it is called , action will go to different destinations.
  16   * If it is called from comments section, the return will be in comments section
  17   *  in any other case it will be in edit view section.
  18   * @param $pageid. Current pageid
  19   * @param $content. Content to be edited.
  20   * @param $section. Current section, default null
  21   * @param $comesfrom. Information about where the function call is made
  22   * @param commentid. id comment of comment that will be edited.
  23   */
  24  
  25  function wiki_print_editor_wiki($pageid, $content, $editor, $version = -1, $section = null, $upload = false, $deleteuploads = array(), $comesfrom = 'editorview', $commentid = 0) {
  26      global $CFG, $OUTPUT, $PAGE;
  27  
  28      if ($comesfrom == 'editcomments') {
  29          $action = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $pageid . '&id=' . $commentid . '&action=edit';
  30      } else if ($comesfrom == 'addcomments') {
  31          $action = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $pageid . '&id=' . $commentid . '&action=add';
  32      } else {
  33          $action = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $pageid;
  34      }
  35  
  36      if (!empty($section)) {
  37          $action .= "&amp;section=" . urlencode($section);
  38      }
  39  
  40      ///Get tags for every element we are displaying
  41      $tag = getTokens($editor, 'bold');
  42      $wiki_editor['bold'] = array('ed_bold.gif', get_string('wikiboldtext', 'wiki'), $tag[0], $tag[1], get_string('wikiboldtext', 'wiki'));
  43      $tag = getTokens($editor, 'italic');
  44      $wiki_editor['italic'] = array('ed_italic.gif', get_string('wikiitalictext', 'wiki'), $tag[0], $tag[1], get_string('wikiitalictext', 'wiki'));
  45      $tag = getTokens($editor, 'link');
  46      $wiki_editor['internal'] = array('ed_internal.gif', get_string('wikiinternalurl', 'wiki'), $tag[0], $tag[1], get_string('wikiinternalurl', 'wiki'));
  47      $tag = getTokens($editor, 'url');
  48      $wiki_editor['external'] = array('ed_external.gif', get_string('wikiexternalurl', 'wiki'), $tag[0], $tag[1], get_string('wikiexternalurl', 'wiki'));
  49      $tag = getTokens($editor, 'list');
  50      $wiki_editor['u_list'] = array('ed_ul.gif', get_string('wikiunorderedlist', 'wiki'), '\\n' . $tag[0], '', '');
  51      $wiki_editor['o_list'] = array('ed_ol.gif', get_string('wikiorderedlist', 'wiki'), '\\n' . $tag[1], '', '');
  52      $tag = getTokens($editor, 'image');
  53      $wiki_editor['image'] = array('ed_img.gif', get_string('wikiimage', 'wiki'), $tag[0], $tag[1], get_string('wikiimage', 'wiki'));
  54      $tag = getTokens($editor, 'header');
  55      $wiki_editor['h1'] = array('ed_h1.gif', get_string('wikiheader', 'wiki', 1), '\\n' . $tag . ' ', ' ' . $tag . '\\n', get_string('wikiheader', 'wiki', 1));
  56      $wiki_editor['h2'] = array('ed_h2.gif', get_string('wikiheader', 'wiki', 2), '\\n' . $tag . $tag . ' ', ' ' . $tag . $tag . '\\n', get_string('wikiheader', 'wiki', 2));
  57      $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));
  58      $tag = getTokens($editor, 'line_break');
  59      $wiki_editor['hr'] = array('ed_hr.gif', get_string('wikihr', 'wiki'), '\\n' . $tag . '\\n', '', '');
  60      $tag = getTokens($editor, 'nowiki');
  61      $wiki_editor['nowiki'] = array('ed_nowiki.gif', get_string('wikinowikitext', 'wiki'), $tag[0], $tag[1], get_string('wikinowikitext', 'wiki'));
  62  
  63      $OUTPUT->heading(strtoupper(get_string('format' . $editor, 'wiki')), 3);
  64  
  65      $PAGE->requires->js('/mod/wiki/editors/wiki/buttons.js');
  66  
  67      echo $OUTPUT->container_start();
  68      foreach ($wiki_editor as $button) {
  69          echo "<a href=\"javascript:insertTags";
  70          echo "('" . $button[2] . "','" . $button[3] . "','" . $button[4] . "');\">";
  71          echo "<img width=\"23\" height=\"22\" src=\"$CFG->wwwroot/mod/wiki/editors/wiki/images/$button[0]\" alt=\"" . $button[1] . "\" title=\"" . $button[1] . "\" />";
  72          echo "</a>";
  73      }
  74      echo $OUTPUT->container_end();
  75  
  76      echo $OUTPUT->container_start();
  77      echo '<form method="post" id="mform1" action="' . $action . '">';
  78      $textarea = $OUTPUT->print_textarea('newcontent', 'edit-newcontent', $content, 20, 60);
  79      echo $OUTPUT->container($textarea, false, 'wiki_editor');
  80      echo $OUTPUT->container_start();
  81      wiki_print_edit_form_default_fields($editor, $pageid, $version, $upload, $deleteuploads);
  82      echo $OUTPUT->container_end();
  83      echo '</form>';
  84      echo $OUTPUT->container_end();
  85  }
  86  
  87  /**
  88   * Returns escaped token used by a wiki language to represent a given tag or "object" (bold -> **)
  89   *
  90   * @param string $format format of page
  91   * @param array|string $token format tokens which needs to be escaped
  92   * @return array|string
  93   */
  94  function getTokens($format, $token) {
  95      $tokens = wiki_parser_get_token($format, $token);
  96  
  97      if (is_array($tokens)) {
  98          foreach ($tokens as $key => $value) {
  99              $tokens[$key] = urlencode(str_replace("'", "\'", $value));
 100          }
 101      } else {
 102          urlencode(str_replace("'", "\'", $token));
 103      }
 104  
 105      return $tokens;
 106  }