Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
///////////////////////////////////////////////////////////////////////////
3
//                                                                       //
4
// NOTICE OF COPYRIGHT                                                   //
5
//                                                                       //
6
// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
7
//          http://moodle.org                                            //
8
//                                                                       //
9
// Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
10
//                                                                       //
11
// This program is free software; you can redistribute it and/or modify  //
12
// it under the terms of the GNU General Public License as published by  //
13
// the Free Software Foundation; either version 2 of the License, or     //
14
// (at your option) any later version.                                   // //                                                                       //
15
// This program is distributed in the hope that it will be useful,       //
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
18
// GNU General Public License for more details:                          //
19
//                                                                       //
20
//          http://www.gnu.org/copyleft/gpl.html                         //
21
//                                                                       //
22
///////////////////////////////////////////////////////////////////////////
23
 
24
defined('MOODLE_INTERNAL') || die();
25
 
26
global $CFG;
27
 
28
require_once($CFG->dirroot.'/lib/filelib.php');
29
require_once($CFG->dirroot.'/repository/lib.php');
30
 
31
class data_field_textarea extends data_field_base {
32
 
33
    var $type = 'textarea';
1441 ariadna 34
 
1 efrain 35
    /**
1441 ariadna 36
     * Prefix for the field name to split field id and the random part.
37
     * @var string
38
     */
39
    protected const RND_PREFIX = 'xZx';
40
 
41
    /**
1 efrain 42
     * priority for globalsearch indexing
43
     *
44
     * @var int
45
     */
46
    protected static $priority = self::LOW_PRIORITY;
47
 
1441 ariadna 48
    /**
49
     * The field name for the content field.
50
     *
51
     * @var string
52
     */
53
    protected $fieldname = null;
54
 
55
    /**
56
     * Returns a random field name for the content field.
57
     *
58
     * @return string
59
     */
60
    protected function get_content_field_name() {
61
        if ($this->fieldname === null) {
62
            $this->fieldname = sprintf(
63
                'field_%s_%s%s',
64
                $this->field->id,
65
                self::RND_PREFIX,
66
                bin2hex(random_bytes(5))
67
            );
68
        }
69
        return $this->fieldname;
70
    }
71
 
72
    /**
73
     * Check if the given field name is the content field that contains the actual data.
74
     *
75
     * @param string $name
76
     * @return bool
77
     */
78
    protected function is_content_field(string $name): bool {
79
        return (strlen($name) === 13 && substr($name, 0, 3) === self::RND_PREFIX);
80
    }
81
 
1 efrain 82
    public function supports_preview(): bool {
83
        return true;
84
    }
85
 
86
    public function get_data_content_preview(int $recordid): stdClass {
87
        return (object)[
88
            'id' => 0,
89
            'fieldid' => $this->field->id,
90
            'recordid' => $recordid,
91
            'content' => get_string('sample', 'datafield_textarea'),
92
            'content1' => 1,
93
            'content2' => null,
94
            'content3' => null,
95
            'content4' => null,
96
        ];
97
    }
98
 
99
    /**
100
     * Returns options for embedded files
101
     *
102
     * @return array
103
     */
104
    private function get_options() {
105
        if (!isset($this->field->param5)) {
106
            $this->field->param5 = 0;
107
        }
108
        $options = array();
109
        $options['trusttext'] = false;
110
        $options['forcehttps'] = false;
111
        $options['subdirs'] = false;
112
        $options['maxfiles'] = -1;
113
        $options['context'] = $this->context;
114
        $options['maxbytes'] = $this->field->param5;
115
        $options['changeformat'] = 0;
116
        $options['noclean'] = false;
117
        return $options;
118
    }
119
 
120
    function display_add_field($recordid = 0, $formdata = null) {
121
        global $CFG, $DB, $OUTPUT, $PAGE;
122
 
123
        $text   = '';
124
        $format = 0;
125
        $str = '<div title="' . s($this->field->description) . '" class="d-inline-flex">';
1441 ariadna 126
        $str .= '<label for="' . $this->get_content_field_name() . '">';
1 efrain 127
        $str .= html_writer::span($this->field->name, 'accesshide');
128
        if ($this->field->required) {
129
            $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
130
            $str .= html_writer::div($image, 'inline-req');
131
        }
132
        $str .= '</label>';
133
 
134
        editors_head_setup();
135
        $options = $this->get_options();
136
 
137
        $itemid = $this->field->id;
138
        $field = 'field_'.$itemid;
139
 
140
        if ($formdata) {
141
            $fieldname = 'field_' . $this->field->id . '_content1';
142
            if (isset($formdata->$fieldname)) {
143
                $format = $formdata->$fieldname;
144
            } else {
145
                $format = file_get_unused_draft_itemid();
146
            }
147
            $fieldname = 'field_' . $this->field->id . '_itemid';
148
            if (isset($formdata->$fieldname)) {
149
                $draftitemid = clean_param($formdata->$fieldname, PARAM_INT);
150
            } else {
151
                $draftitemid = file_get_unused_draft_itemid();
152
            }
1441 ariadna 153
            $fieldname = 'field_' . $this->field->id . '_' . self::RND_PREFIX;
154
            foreach (array_keys(get_object_vars($formdata)) as $prop) {
155
                if (strpos($prop, $fieldname) === 0) {
156
                    $text = $formdata->$prop;
157
                    break;
158
                }
1 efrain 159
            }
160
        } else if ($recordid &&
161
                   $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
162
            $format = $content->content1;
163
            $text = clean_text($content->content, $format);
164
            $text = file_prepare_draft_area($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $text);
165
        } else {
166
            $draftitemid = file_get_unused_draft_itemid();
167
            $format = editors_get_preferred_format();
168
        }
169
 
170
        // get filepicker info
171
        //
172
        $fpoptions = array();
173
        if ($options['maxfiles'] != 0 ) {
174
            $args = new stdClass();
175
            // need these three to filter repositories list
176
            $args->accepted_types = array('web_image');
177
            $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL);
178
            $args->context = $this->context;
179
            $args->env = 'filepicker';
180
            // advimage plugin
181
            $image_options = initialise_filepicker($args);
182
            $image_options->context = $this->context;
183
            $image_options->client_id = uniqid();
184
            $image_options->maxbytes = $options['maxbytes'];
185
            $image_options->env = 'editor';
186
            $image_options->itemid = $draftitemid;
187
 
188
            // moodlemedia plugin
189
            $args->accepted_types = array('video', 'audio');
190
            $media_options = initialise_filepicker($args);
191
            $media_options->context = $this->context;
192
            $media_options->client_id = uniqid();
193
            $media_options->maxbytes  = $options['maxbytes'];
194
            $media_options->env = 'editor';
195
            $media_options->itemid = $draftitemid;
196
 
197
            // advlink plugin
198
            $args->accepted_types = '*';
199
            $link_options = initialise_filepicker($args);
200
            $link_options->context = $this->context;
201
            $link_options->client_id = uniqid();
202
            $link_options->maxbytes  = $options['maxbytes'];
203
            $link_options->env = 'editor';
204
            $link_options->itemid = $draftitemid;
205
 
206
            // H5P plugin.
207
            $args->accepted_types = ['h5p'];
208
            $h5poptions = initialise_filepicker($args);
209
            $h5poptions->context = $this->context;
210
            $h5poptions->client_id = uniqid();
211
            $h5poptions->maxbytes  = $options['maxbytes'];
212
            $h5poptions->env = 'editor';
213
            $h5poptions->itemid = $draftitemid;
214
 
215
            $fpoptions['image'] = $image_options;
216
            $fpoptions['media'] = $media_options;
217
            $fpoptions['link'] = $link_options;
218
            $fpoptions['h5p'] = $h5poptions;
219
        }
220
 
221
        $editor = editors_get_preferred_editor($format);
222
        $strformats = format_text_menu();
223
        $formats =  $editor->get_supported_formats();
224
        foreach ($formats as $fid) {
225
            $formats[$fid] = $strformats[$fid];
226
        }
227
        $editor->set_text($text);
1441 ariadna 228
        $editor->use_editor($this->get_content_field_name(), $options, $fpoptions);
1 efrain 229
        $str .= '<input type="hidden" name="'.$field.'_itemid" value="'.s($draftitemid).'" />';
230
        $str .= '<div class="mod-data-input">';
1441 ariadna 231
        $str .= '<div><textarea id="' . $this->get_content_field_name() . '" name="' . $this->get_content_field_name() . '" ' .
232
            'rows="'.$this->field->param3 . '" class="form-control" data-fieldtype="editor" ' .
1 efrain 233
            'cols="'.$this->field->param2.'" spellcheck="true">'.s($text).'</textarea></div>';
234
        $str .= '<div><label class="accesshide" for="' . $field . '_content1">' . get_string('format') . '</label>';
1441 ariadna 235
        $str .= '<select id="' . $field . '_content1" name="'.$field.'_content1" class="form-select mt-2">';
1 efrain 236
        foreach ($formats as $key=>$desc) {
237
            $selected = ($format == $key) ? 'selected="selected"' : '';
238
            $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
239
        }
240
        $str .= '</select>';
241
 
242
        $str .= '</div>';
243
        $str .= '</div>';
244
        $str .= '</div>';
245
        return $str;
246
    }
247
 
248
    function display_search_field($value = '') {
249
        return '<label class="accesshide" for="f_' . $this->field->id . '">' . s($this->field->name) . '</label>' .
250
               '<input type="text" size="16" id="f_' . $this->field->id . '" name="f_' . $this->field->id . '" ' .
251
               'value="' . s($value) . '" class="form-control"/>';
252
    }
253
 
254
    public function parse_search_field($defaults = null) {
255
        $param = 'f_'.$this->field->id;
256
        if (empty($defaults[$param])) {
257
            $defaults = array($param => '');
258
        }
259
        return optional_param($param, $defaults[$param], PARAM_NOTAGS);
260
    }
261
 
262
    function generate_sql($tablealias, $value) {
263
        global $DB;
264
 
265
        static $i=0;
266
        $i++;
267
        $name = "df_textarea_$i";
268
        return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
269
    }
270
 
271
    function print_after_form() {
272
    }
273
 
274
 
275
    function update_content($recordid, $value, $name='') {
276
        global $DB;
277
 
278
        $content = new stdClass();
279
        $content->fieldid = $this->field->id;
280
        $content->recordid = $recordid;
281
 
282
        $names = explode('_', $name);
283
        if (!empty($names[2])) {
284
            if ($names[2] == 'itemid') {
285
                // the value will be retrieved by file_get_submitted_draft_itemid, do not need to save in DB
286
                return true;
1441 ariadna 287
            } else if ($this->is_content_field($names[2])) {
288
                $content->content = clean_param($value, PARAM_RAW_TRIMMED);
1 efrain 289
            } else {
290
                $content->{$names[2]} = clean_param($value, PARAM_NOTAGS);  // content[1-4]
291
            }
292
        } else {
1441 ariadna 293
            $content->content = clean_param($value, PARAM_RAW_TRIMMED);
1 efrain 294
        }
295
 
296
        if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
297
            $content->id = $oldcontent->id;
298
        } else {
299
            $content->id = $DB->insert_record('data_content', $content);
300
            if (!$content->id) {
301
                return false;
302
            }
303
        }
304
        if (!empty($content->content)) {
305
            $draftitemid = file_get_submitted_draft_itemid('field_'. $this->field->id. '_itemid');
306
            $options = $this->get_options();
307
            $content->content = file_save_draft_area_files($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $content->content);
308
        }
309
        $rv = $DB->update_record('data_content', $content);
310
        return $rv;
311
    }
312
 
313
    /**
314
     * Display the content of the field in browse mode
315
     *
316
     * @param int $recordid
317
     * @param object $template
318
     * @return bool|string
319
     */
320
    function display_browse_field($recordid, $template) {
321
        $content = $this->get_data_content($recordid);
322
        if (!$content || !isset($content->content)) {
323
            return '';
324
        }
325
        $options = new stdClass();
326
        if ($this->field->param1 == '1') {  // We are autolinking this field, so disable linking within us.
327
            $options->filter = false;
328
        }
329
        $options->para = false;
330
        $str = file_rewrite_pluginfile_urls(
331
            $content->content,
332
            'pluginfile.php',
333
            $this->context->id,
334
            'mod_data',
335
            'content',
336
            $content->id,
337
            $this->get_options()
338
        );
339
        $str = format_text($str, $content->content1, $options);
340
        return '<div class="data-field-html">' . $str . '</div>';
341
    }
342
 
343
    /**
344
     * Whether this module support files
345
     *
346
     * @param string $relativepath
347
     * @return bool
348
     */
349
    function file_ok($relativepath) {
350
        return true;
351
    }
352
 
353
    /**
354
     * Only look at the first item (second is format)
355
     *
356
     * @param string $value
357
     * @param string $name
358
     * @return bool
359
     */
360
    function notemptyfield($value, $name) {
361
        $names = explode('_', $name);
362
        // Clean first.
363
        if (count($names) == 2) {
364
            // Don't assume that this is coming from a text editor with tags.
365
            return strval($value) !== '';
366
        }
1441 ariadna 367
        if ($this->is_content_field($names[2])) {
368
            return strval($value) !== '';
369
        }
1 efrain 370
        return false;
371
    }
372
 
373
    /**
374
     * Returns the presentable string value for a field content.
375
     *
376
     * The returned string should be plain text.
377
     *
378
     * @param stdClass $content
379
     * @return string
380
     */
381
    public static function get_content_value($content) {
382
        return content_to_text($content->content, $content->content1);
383
    }
384
 
385
    /**
386
     * Return the plugin configs for external functions.
387
     *
388
     * @return array the list of config parameters
389
     * @since Moodle 3.3
390
     */
391
    public function get_config_for_external() {
392
        // Return all the config parameters.
393
        $configs = [];
394
        for ($i = 1; $i <= 10; $i++) {
395
            $configs["param$i"] = $this->field->{"param$i"};
396
        }
397
        return $configs;
398
    }
399
}