Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * This file contains all necessary code to define and process an edit form
  20   *
  21   * @package mod_wiki
  22   * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
  23   * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
  24   *
  25   * @author Josep Arus
  26   *
  27   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  
  30  if (!defined('MOODLE_INTERNAL')) {
  31      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
  32  }
  33  
  34  require_once($CFG->dirroot . '/mod/wiki/editors/wikieditor.php');
  35  
  36  class mod_wiki_edit_form extends moodleform {
  37  
  38      protected function definition() {
  39          global $CFG;
  40  
  41          $mform = $this->_form;
  42          // BEWARE HACK: In order for things to work we need to override the form id and set it to mform1.
  43          // The first form to be instantiated never gets displayed so this should be safe.
  44          $mform->updateAttributes(array('id' => 'mform1'));
  45  
  46          $version = $this->_customdata['version'];
  47          $format = $this->_customdata['format'];
  48  
  49          if (empty($this->_customdata['contextid'])) {
  50              // Hack alert
  51              // This is being done ONLY to aid those who may have created there own wiki pages. It should be removed sometime
  52              // after the release of 2.3 (not creating an issue because this whole thing should be reviewed)
  53              debugging('You must always provide mod_wiki_edit_form with a contextid in its custom data', DEBUG_DEVELOPER);
  54              global $PAGE;
  55              $contextid = $PAGE->context->id;
  56          } else {
  57              $contextid = $this->_customdata['contextid'];
  58          }
  59  
  60          if (isset($this->_customdata['pagetitle'])) {
  61              // Page title must be formatted properly here as this is output and not an element.
  62              $pagetitle = get_string('editingpage', 'wiki', format_string($this->_customdata['pagetitle'], true, array('context' => context::instance_by_id($contextid, MUST_EXIST))));
  63          } else {
  64              $pagetitle = get_string('editing', 'wiki');
  65          }
  66  
  67          //editor
  68          $mform->addElement('header', 'general', $pagetitle);
  69  
  70          $fieldname = get_string('format' . $format, 'wiki');
  71          if ($format != 'html') {
  72              // Use wiki editor
  73              $extensions = file_get_typegroup('extension', 'web_image');
  74              $fs = get_file_storage();
  75              $tree = $fs->get_area_tree($contextid, 'mod_wiki', $this->_customdata['filearea'], $this->_customdata['fileitemid']);
  76              $files = array();
  77              foreach ($tree['files'] as $file) {
  78                  $filename = $file->get_filename();
  79                  foreach ($extensions as $ext) {
  80                      if (preg_match('#'.$ext.'$#i', $filename)) {
  81                          $files[] = $filename;
  82                      }
  83                  }
  84              }
  85              $mform->addElement('wikieditor', 'newcontent', $fieldname, array('cols' => 100, 'rows' => 20, 'wiki_format' => $format, 'files'=>$files));
  86              $mform->addHelpButton('newcontent', 'format'.$format, 'wiki');
  87              $mform->setType('newcontent', PARAM_RAW); // processed by trust text or cleaned before the display
  88          } else {
  89              $mform->addElement('editor', 'newcontent_editor', $fieldname, null, page_wiki_edit::$attachmentoptions);
  90              $mform->addHelpButton('newcontent_editor', 'formathtml', 'wiki');
  91              $mform->setType('newcontent_editor', PARAM_RAW); // processed by trust text or cleaned before the display
  92          }
  93  
  94          //hiddens
  95          if ($version >= 0) {
  96              $mform->addElement('hidden', 'version', $version);
  97              $mform->setType('version', PARAM_FLOAT);
  98          }
  99  
 100          $mform->addElement('hidden', 'contentformat', $format);
 101          $mform->setType('contentformat', PARAM_ALPHANUMEXT);
 102  
 103          if (core_tag_tag::is_enabled('mod_wiki', 'wiki_pages')) {
 104              $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
 105          }
 106          $mform->addElement('tags', 'tags', get_string('tags'),
 107                  array('itemtype' => 'wiki_pages', 'component' => 'mod_wiki'));
 108  
 109          $buttongroup = array();
 110          $buttongroup[] = $mform->createElement('submit', 'editoption', get_string('save', 'wiki'), array('id' => 'save'));
 111          $buttongroup[] = $mform->createElement('submit', 'editoption', get_string('preview'), array('id' => 'preview'), false);
 112          $buttongroup[] = $mform->createElement('submit', 'editoption', get_string('cancel'), array('id' => 'cancel'), false);
 113  
 114          $mform->addGroup($buttongroup, 'buttonar', '', array(' '), false);
 115          $mform->closeHeaderBefore('buttonar');
 116      }
 117  
 118  }