Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   * Customfields textarea plugin
  19   *
  20   * @package   customfield_textarea
  21   * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace customfield_textarea;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  /**
  30   * Class data
  31   *
  32   * @package customfield_textarea
  33   * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class data_controller extends \core_customfield\data_controller {
  37  
  38      /**
  39       * Return the name of the field where the information is stored
  40       * @return string
  41       */
  42      public function datafield() : string {
  43          return 'value';
  44      }
  45  
  46      /**
  47       * Options for the editor
  48       *
  49       * @return array
  50       */
  51      protected function value_editor_options() {
  52          /** @var field_controller $field */
  53          $field = $this->get_field();
  54          return $field->value_editor_options($this->get('id') ? $this->get_context() : null);
  55      }
  56  
  57      /**
  58       * Returns the name of the field to be used on HTML forms.
  59       *
  60       * @return string
  61       */
  62      public function get_form_element_name() : string {
  63          return parent::get_form_element_name() . '_editor';
  64      }
  65  
  66      /**
  67       * Add fields for editing a textarea field.
  68       *
  69       * @param \MoodleQuickForm $mform
  70       */
  71      public function instance_form_definition(\MoodleQuickForm $mform) {
  72          $field = $this->get_field();
  73          $desceditoroptions = $this->value_editor_options();
  74          $elementname = $this->get_form_element_name();
  75          $mform->addElement('editor', $elementname, $this->get_field()->get_formatted_name(), null, $desceditoroptions);
  76          if ($field->get_configdata_property('required')) {
  77              $mform->addRule($elementname, null, 'required', null, 'client');
  78          }
  79      }
  80  
  81      /**
  82       * Saves the data coming from form
  83       *
  84       * @param \stdClass $datanew data coming from the form
  85       */
  86      public function instance_form_save(\stdClass $datanew) {
  87          $fieldname = $this->get_form_element_name();
  88          if (!property_exists($datanew, $fieldname)) {
  89              return;
  90          }
  91          $fromform = $datanew->$fieldname;
  92  
  93          if (!$this->get('id')) {
  94              $this->data->set('value', '');
  95              $this->data->set('valueformat', FORMAT_MOODLE);
  96              $this->save();
  97          }
  98  
  99          if (array_key_exists('text', $fromform)) {
 100              $textoptions = $this->value_editor_options();
 101              $data = (object) ['field_editor' => $fromform];
 102              $data = file_postupdate_standard_editor($data, 'field', $textoptions, $textoptions['context'],
 103                  'customfield_textarea', 'value', $this->get('id'));
 104              $this->data->set('value', $data->field);
 105              $this->data->set('valueformat', $data->fieldformat);
 106  
 107              $this->save();
 108          }
 109      }
 110  
 111      /**
 112       * Prepares the custom field data related to the object to pass to mform->set_data() and adds them to it
 113       *
 114       * This function must be called before calling $form->set_data($object);
 115       *
 116       * @param \stdClass $instance the entity that has custom fields, if 'id' attribute is present the custom
 117       *    fields for this entity will be added, otherwise the default values will be added.
 118       */
 119      public function instance_form_before_set_data(\stdClass $instance) {
 120          $textoptions = $this->value_editor_options();
 121          if ($this->get('id')) {
 122              $text = $this->get('value');
 123              $format = $this->get('valueformat');
 124              $temp = (object)['field' => $text, 'fieldformat' => $format];
 125              file_prepare_standard_editor($temp, 'field', $textoptions, $textoptions['context'], 'customfield_textarea',
 126                  'value', $this->get('id'));
 127              $value = $temp->field_editor;
 128          } else {
 129              $text = $this->get_field()->get_configdata_property('defaultvalue');
 130              $format = $this->get_field()->get_configdata_property('defaultvalueformat');
 131              $temp = (object)['field' => $text, 'fieldformat' => $format];
 132              file_prepare_standard_editor($temp, 'field', $textoptions, $textoptions['context'], 'customfield_textarea',
 133                  'defaultvalue', $this->get_field()->get('id'));
 134              $value = $temp->field_editor;
 135          }
 136          $instance->{$this->get_form_element_name()} = $value;
 137      }
 138  
 139      /**
 140       * Delete data
 141       *
 142       * @return bool
 143       */
 144      public function delete() {
 145          get_file_storage()->delete_area_files($this->get('contextid'), 'customfield_textarea',
 146              'value', $this->get('id'));
 147          return parent::delete();
 148      }
 149  
 150      /**
 151       * Returns the default value as it would be stored in the database (not in human-readable format).
 152       *
 153       * @return mixed
 154       */
 155      public function get_default_value() {
 156          return $this->get_field()->get_configdata_property('defaultvalue');
 157      }
 158  
 159      /**
 160       * Returns value in a human-readable format
 161       *
 162       * @return mixed|null value or null if empty
 163       */
 164      public function export_value() {
 165          global $CFG;
 166          require_once($CFG->libdir . '/filelib.php');
 167  
 168          $value = $this->get_value();
 169          if ($this->is_empty($value)) {
 170              return null;
 171          }
 172  
 173          if ($dataid = $this->get('id')) {
 174              $context = $this->get_context();
 175              $processed = file_rewrite_pluginfile_urls($value, 'pluginfile.php',
 176                  $context->id, 'customfield_textarea', 'value', $dataid);
 177              $value = format_text($processed, $this->get('valueformat'), ['context' => $context]);
 178          } else {
 179              $fieldid = $this->get_field()->get('id');
 180              $configcontext = $this->get_field()->get_handler()->get_configuration_context();
 181              $processed = file_rewrite_pluginfile_urls($value, 'pluginfile.php',
 182                  $configcontext->id, 'customfield_textarea', 'defaultvalue', $fieldid);
 183              $valueformat = $this->get_field()->get_configdata_property('defaultvalueformat');
 184              $value = format_text($processed, $valueformat, ['context' => $configcontext]);
 185          }
 186  
 187          return $value;
 188      }
 189  }