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
 * unilabel module.
19
 *
20
 * @package     mod_unilabel
21
 * @author      Andreas Grabs <info@grabs-edv.de>
22
 * @copyright   2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace mod_unilabel;
27
 
28
defined('MOODLE_INTERNAL') || die;
29
 
30
require_once($CFG->libdir . '/formslib.php');
31
 
32
/**
33
 * Build a moodle form and uses the form elements given by the used content type.
34
 * @package     mod_unilabel
35
 * @author      Andreas Grabs <info@grabs-edv.de>
36
 * @copyright   2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
37
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class edit_content_form extends \moodleform {
40
    /** @var \stdClass */
41
    private $_course;
42
    /** @var content_type */
43
    protected $unilabeltype;
44
    /** @var \stdClass */
45
    protected $cm;
46
    /** @var \context */
47
    public $context;
48
    /** @var \stdClass */
49
    public $unilabel;
50
 
51
    /**
52
     * Get an options array to use files in the editor.
53
     *
54
     * @param  \context $context
55
     * @return array
56
     */
57
    public static function editor_options($context) {
58
        return [
59
            'maxfiles' => EDITOR_UNLIMITED_FILES,
60
            'noclean'  => true,
61
            'context'  => $context,
62
            'subdirs'  => true,
63
        ];
64
    }
65
 
66
    /**
67
     * Moodle form definition method to define all needed elements.
68
     * It uses the elements needed by the current content type.
69
     *
70
     * @return void
71
     */
72
    public function definition() {
73
        $mform              = $this->_form;
74
        $this->unilabel     = $this->_customdata['unilabel'];
75
        $this->unilabeltype = $this->_customdata['unilabeltype'];
76
        $this->cm           = $this->_customdata['cm'];
77
        $this->context      = \context_module::instance($this->cm->id);
78
        $this->_course      = get_course($this->cm->course);
79
 
80
        $mform->addElement('hidden', 'cmid');
81
        $mform->setType('cmid', PARAM_INT);
82
 
83
        $mform->addElement('hidden', 'id');
84
        $mform->setType('id', PARAM_INT);
85
 
86
        $mform->addElement('header', 'unilabelcontenthdr', get_string('editcontent', 'mod_unilabel'));
87
 
88
        $this->add_intro_editor();
89
        $this->add_plugin_form_elements();
90
 
91
        $this->add_action_buttons();
92
 
93
        $this->set_data((array) $this->unilabel);
94
    }
95
 
96
    /**
97
     * Does the validation of the submitted values.
98
     *
99
     * @param  array $data
100
     * @param  array $files
101
     * @return array
102
     */
103
    public function validation($data, $files) {
104
        $errors = parent::validation($data, $files);
105
        $errors = $this->unilabeltype->form_validation($errors, $data, $files);
106
 
107
        return $errors;
108
    }
109
 
110
    /**
111
     * Add the intro editor as form element.
112
     *
113
     * @return void
114
     */
115
    private function add_intro_editor() {
116
        $mform = $this->_form;
117
        $mform->addElement('editor',
118
            'introeditor',
119
            get_string('unilabeltext', 'mod_unilabel'),
120
            ['rows' => 10],
121
            self::editor_options($this->context)
122
        );
123
        $mform->setType('introeditor', PARAM_RAW); // No XSS prevention here, users must be trusted.
124
    }
125
 
126
    /**
127
     * Set all default data while loading the form.
128
     *
129
     * @param  array $defaultvalues
130
     * @return void
131
     */
132
    public function set_data($defaultvalues) {
133
        $defaultvalues['cmid'] = $this->cm->id;
134
 
135
        $plugindefaultvalues = $this->get_plugin_defaultvalues();
136
 
137
        $defaultvalues += $plugindefaultvalues;
138
 
139
        $draftitemid = file_get_submitted_draft_itemid('introeditor');
140
 
141
        $defaultvalues['introeditor']['text'] = file_prepare_draft_area($draftitemid,
142
                                    $this->context->id,
143
                                    'mod_unilabel',
144
                                    'intro',
145
                                    false,
146
                                    ['subdirs' => true],
147
                                    $defaultvalues['intro']);
148
        $defaultvalues['introeditor']['format'] = $defaultvalues['introformat'];
149
        $defaultvalues['introeditor']['itemid'] = $draftitemid;
150
 
151
        parent::set_data($defaultvalues);
152
    }
153
 
154
    /**
155
     * Add all form elements needed by the current content type.
156
     *
157
     * @return void
158
     */
159
    private function add_plugin_form_elements() {
160
        $this->unilabeltype->add_form_fragment($this, $this->context);
161
    }
162
 
163
    /**
164
     * Get all default values from the current content type.
165
     *
166
     * @return array
167
     */
168
    private function get_plugin_defaultvalues() {
169
        $data = [];
170
 
171
        $data = $this->unilabeltype->get_form_default($data, $this->unilabel);
172
 
173
        return $data;
174
    }
175
 
176
    /**
177
     * Return the \MoodleQuickForm form so the current content type can use it.
178
     *
179
     * @return \MoodleQuickForm
180
     */
181
    public function get_mform() {
182
        return $this->_form;
183
    }
184
 
185
    /**
186
     * Return the current course.
187
     *
188
     * @return \stdClass
189
     */
190
    public function get_course() {
191
        return $this->_course;
192
    }
193
}