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  // 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   * Customfield textarea plugin
  19   *
  20   * @package   customfield_textarea
  21   * @copyright 2018 David Matamoros <davidmc@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 field
  31   *
  32   * @package customfield_textarea
  33   * @copyright 2018 David Matamoros <davidmc@moodle.com>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class field_controller extends \core_customfield\field_controller {
  37      /**
  38       * Const type
  39       */
  40      const TYPE = 'textarea';
  41  
  42      /**
  43       * Before delete bulk actions
  44       */
  45      public function delete() : bool {
  46          global $DB;
  47          $fs = get_file_storage();
  48  
  49          // Delete files in the defaultvalue.
  50          $fs->delete_area_files($this->get_handler()->get_configuration_context()->id, 'customfield_textarea',
  51              'defaultvalue', $this->get('id'));
  52  
  53          // Delete files in the data. We can not use $fs->delete_area_files_select() because context may be different.
  54          $params = ['component' => 'customfield_textarea', 'filearea' => 'value', 'fieldid' => $this->get('id')];
  55          $where = "component = :component AND filearea = :filearea
  56                  AND itemid IN (SELECT cfd.id FROM {customfield_data} cfd WHERE cfd.fieldid = :fieldid)";
  57          $filerecords = $DB->get_recordset_select('files', $where, $params);
  58          foreach ($filerecords as $filerecord) {
  59              $fs->get_file_instance($filerecord)->delete();
  60          }
  61          $filerecords->close();
  62  
  63          // Delete data and field.
  64          return parent::delete();
  65      }
  66  
  67      /**
  68       * Prepare the field data to set in the configuration form
  69       *
  70       * Necessary if some preprocessing required for editor or filemanager fields
  71       *
  72       * @param \stdClass $formdata
  73       */
  74      public function prepare_for_config_form(\stdClass $formdata) {
  75  
  76          if (!empty($formdata->configdata['defaultvalue'])) {
  77              $textoptions = $this->value_editor_options();
  78              $context = $textoptions['context'];
  79  
  80              $record = new \stdClass();
  81              $record->defaultvalue = $formdata->configdata['defaultvalue'];
  82              $record->defaultvalueformat = $formdata->configdata['defaultvalueformat'];
  83              file_prepare_standard_editor($record, 'defaultvalue', $textoptions, $context,
  84                  'customfield_textarea', 'defaultvalue', $formdata->id);
  85              $formdata->configdata['defaultvalue_editor'] = $record->defaultvalue_editor;
  86          }
  87      }
  88  
  89      /**
  90       * Add fields for editing a textarea field.
  91       *
  92       * @param \MoodleQuickForm $mform
  93       */
  94      public function config_form_definition(\MoodleQuickForm $mform) {
  95          $mform->addElement('header', 'header_specificsettings', get_string('specificsettings', 'customfield_textarea'));
  96          $mform->setExpanded('header_specificsettings', true);
  97  
  98          $desceditoroptions = $this->value_editor_options();
  99  
 100          $mform->addElement('editor', 'configdata[defaultvalue_editor]', get_string('defaultvalue', 'core_customfield'),
 101              null, $desceditoroptions);
 102      }
 103  
 104      /**
 105       * Options for editor
 106       *
 107       * @param \context|null $context context if known, otherwise configuration context will be used
 108       * @return array
 109       */
 110      public function value_editor_options(\context $context = null) {
 111          global $CFG;
 112          require_once($CFG->libdir.'/formslib.php');
 113          if (!$context) {
 114              $context = $this->get_handler()->get_configuration_context();
 115          }
 116          return ['maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $CFG->maxbytes, 'context' => $context];
 117      }
 118  
 119      /**
 120       * Saves the field configuration
 121       */
 122      public function save() {
 123          $configdata = $this->get('configdata');
 124          if (!array_key_exists('defaultvalue_editor', $configdata)) {
 125              $this->field->save();
 126              return;
 127          }
 128  
 129          if (!$this->get('id')) {
 130              $this->field->save();
 131          }
 132  
 133          // Store files.
 134          $textoptions = $this->value_editor_options();
 135          $tempvalue = (object) ['defaultvalue_editor' => $configdata['defaultvalue_editor']];
 136          $tempvalue = file_postupdate_standard_editor($tempvalue, 'defaultvalue', $textoptions, $textoptions['context'],
 137              'customfield_textarea', 'defaultvalue', $this->get('id'));
 138  
 139          $configdata['defaultvalue'] = $tempvalue->defaultvalue;
 140          $configdata['defaultvalueformat'] = $tempvalue->defaultvalueformat;
 141          unset($configdata['defaultvalue_editor']);
 142          $this->field->set('configdata', json_encode($configdata));
 143          $this->field->save();
 144      }
 145  }