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