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  /**
  18   * Form for editing HTML block instances.
  19   *
  20   * @package   block_html
  21   * @copyright 2009 Tim Hunt
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Form for editing HTML block instances.
  27   *
  28   * @copyright 2009 Tim Hunt
  29   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class block_html_edit_form extends block_edit_form {
  32      protected function specific_definition($mform) {
  33          global $CFG;
  34  
  35          // Fields for editing HTML block title and contents.
  36          $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
  37  
  38          $mform->addElement('text', 'config_title', get_string('configtitle', 'block_html'));
  39          $mform->setType('config_title', PARAM_TEXT);
  40  
  41          $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->block->context);
  42          $mform->addElement('editor', 'config_text', get_string('configcontent', 'block_html'), null, $editoroptions);
  43          $mform->addRule('config_text', null, 'required', null, 'client');
  44          $mform->setType('config_text', PARAM_RAW); // XSS is prevented when printing the block contents and serving files
  45  
  46          if (!empty($CFG->block_html_allowcssclasses)) {
  47              $mform->addElement('text', 'config_classes', get_string('configclasses', 'block_html'));
  48              $mform->setType('config_classes', PARAM_TEXT);
  49              $mform->addHelpButton('config_classes', 'configclasses', 'block_html');
  50          }
  51      }
  52  
  53      function set_data($defaults) {
  54          if (!empty($this->block->config) && !empty($this->block->config->text)) {
  55              $text = $this->block->config->text;
  56              $draftid_editor = file_get_submitted_draft_itemid('config_text');
  57              if (empty($text)) {
  58                  $currenttext = '';
  59              } else {
  60                  $currenttext = $text;
  61              }
  62              $defaults->config_text['text'] = file_prepare_draft_area($draftid_editor, $this->block->context->id, 'block_html', 'content', 0, array('subdirs'=>true), $currenttext);
  63              $defaults->config_text['itemid'] = $draftid_editor;
  64              $defaults->config_text['format'] = $this->block->config->format ?? FORMAT_MOODLE;
  65          } else {
  66              $text = '';
  67          }
  68  
  69          if (!$this->block->user_can_edit() && !empty($this->block->config->title)) {
  70              // If a title has been set but the user cannot edit it format it nicely
  71              $title = $this->block->config->title;
  72              $defaults->config_title = format_string($title, true, $this->page->context);
  73              // Remove the title from the config so that parent::set_data doesn't set it.
  74              unset($this->block->config->title);
  75          }
  76  
  77          // have to delete text here, otherwise parent::set_data will empty content
  78          // of editor
  79          unset($this->block->config->text);
  80          parent::set_data($defaults);
  81          // restore $text
  82          if (!isset($this->block->config)) {
  83              $this->block->config = new stdClass();
  84          }
  85          $this->block->config->text = $text;
  86          if (isset($title)) {
  87              // Reset the preserved title
  88              $this->block->config->title = $title;
  89          }
  90      }
  91  }