Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
114
        if (!$context) {
115
            $context = $this->get_handler()->get_configuration_context();
116
        }
117
 
118
        return [
119
            'context' => $context,
120
            'trusttext' => true,
121
            'maxfiles' => EDITOR_UNLIMITED_FILES,
122
            'maxbytes' => $CFG->maxbytes,
123
        ];
124
    }
125
 
126
    /**
127
     * Saves the field configuration
128
     */
129
    public function save() {
130
        $configdata = $this->get('configdata');
131
        if (!array_key_exists('defaultvalue_editor', $configdata)) {
132
            $this->field->save();
133
            return;
134
        }
135
 
136
        if (!$this->get('id')) {
137
            $this->field->save();
138
        }
139
 
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'));
145
 
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();
151
    }
152
}