MDL-65292 style: Fix all function declarations white space
[moodle.git] / customfield / field / textarea / classes / field_controller.php
bloba3955bd2719ec71826a75fecd46ee9275cd6f53a
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.
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/>.
17 /**
18 * Customfield textarea plugin
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
25 namespace customfield_textarea;
27 defined('MOODLE_INTERNAL') || die;
29 /**
30 * Class field
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
36 class field_controller extends \core_customfield\field_controller {
37 /**
38 * Const type
40 const TYPE = 'textarea';
42 /**
43 * Before delete bulk actions
45 public function delete(): bool {
46 global $DB;
47 $fs = get_file_storage();
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'));
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();
61 $filerecords->close();
63 // Delete data and field.
64 return parent::delete();
67 /**
68 * Prepare the field data to set in the configuration form
70 * Necessary if some preprocessing required for editor or filemanager fields
72 * @param \stdClass $formdata
74 public function prepare_for_config_form(\stdClass $formdata) {
76 if (!empty($formdata->configdata['defaultvalue'])) {
77 $textoptions = $this->value_editor_options();
78 $context = $textoptions['context'];
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;
89 /**
90 * Add fields for editing a textarea field.
92 * @param \MoodleQuickForm $mform
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);
98 $desceditoroptions = $this->value_editor_options();
100 $mform->addElement('editor', 'configdata[defaultvalue_editor]', get_string('defaultvalue', 'core_customfield'),
101 null, $desceditoroptions);
105 * Options for editor
107 * @param \context|null $context context if known, otherwise configuration context will be used
108 * @return array
110 public function value_editor_options(\context $context = null) {
111 global $CFG;
112 require_once($CFG->libdir.'/formslib.php');
114 if (!$context) {
115 $context = $this->get_handler()->get_configuration_context();
118 return [
119 'context' => $context,
120 'trusttext' => true,
121 'maxfiles' => EDITOR_UNLIMITED_FILES,
122 'maxbytes' => $CFG->maxbytes,
127 * Saves the field configuration
129 public function save() {
130 $configdata = $this->get('configdata');
131 if (!array_key_exists('defaultvalue_editor', $configdata)) {
132 $this->field->save();
133 return;
136 if (!$this->get('id')) {
137 $this->field->save();
140 // Store files.
141 $textoptions = $this->value_editor_options();
142 $tempvalue = (object) ['defaultvalue_editor' => $configdata['defaultvalue_editor']];
143 $tempvalue = file_postupdate_standard_editor($tempvalue, 'defaultvalue', $textoptions, $textoptions['context'],
144 'customfield_textarea', 'defaultvalue', $this->get('id'));
146 $configdata['defaultvalue'] = $tempvalue->defaultvalue;
147 $configdata['defaultvalueformat'] = $tempvalue->defaultvalueformat;
148 unset($configdata['defaultvalue_editor']);
149 $this->field->set('configdata', json_encode($configdata));
150 $this->field->save();