MDL-70912 mod_data: Add filepicker to the H5P Atto button
[moodle.git] / mod / data / field / textarea / field.class.php
CommitLineData
1adbd2c3 1<?php
35bcb325 2///////////////////////////////////////////////////////////////////////////
3// //
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
7// http://moodle.org //
8// //
0997e51a 9// Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
35bcb325 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 //
601104f2 14// (at your option) any later version. // // //
35bcb325 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
4e033542
SH
24require_once($CFG->dirroot.'/lib/filelib.php');
25require_once($CFG->dirroot.'/repository/lib.php');
26
35bcb325 27class data_field_textarea extends data_field_base {
28
29 var $type = 'textarea';
2af9ad7d
DG
30 /**
31 * priority for globalsearch indexing
32 *
33 * @var int
34 */
35 protected static $priority = self::LOW_PRIORITY;
aab98aaf 36
29207706
MG
37 /**
38 * Returns options for embedded files
39 *
40 * @return array
41 */
42 private function get_options() {
43 if (!isset($this->field->param5)) {
44 $this->field->param5 = 0;
45 }
46 $options = array();
47 $options['trusttext'] = false;
48 $options['forcehttps'] = false;
49 $options['subdirs'] = false;
50 $options['maxfiles'] = -1;
51 $options['context'] = $this->context;
52 $options['maxbytes'] = $this->field->param5;
53 $options['changeformat'] = 0;
54 $options['noclean'] = false;
55 return $options;
56 }
57
b89cca19 58 function display_add_field($recordid = 0, $formdata = null) {
4e033542 59 global $CFG, $DB, $OUTPUT, $PAGE;
0997e51a 60
423bd918 61 $text = '';
62 $format = 0;
d0329e84
PH
63 $str = '<div title="' . s($this->field->description) . '" class="d-inline-flex">';
64 $str .= '<label for="field_' . $this->field->id . '">';
65 $str .= html_writer::span($this->field->name, 'accesshide');
b89cca19 66 if ($this->field->required) {
663640f5 67 $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
c07c5e7b 68 $str .= html_writer::div($image, 'inline-req');
b89cca19 69 }
1c3b2058 70 $str .= '</label>';
aab98aaf 71
4e033542 72 editors_head_setup();
29207706 73 $options = $this->get_options();
4e033542
SH
74
75 $itemid = $this->field->id;
76 $field = 'field_'.$itemid;
77
b89cca19
DW
78 if ($formdata) {
79 $fieldname = 'field_' . $this->field->id . '_content1';
1c3b2058
JO
80 if (isset($formdata->$fieldname)) {
81 $format = $formdata->$fieldname;
82 } else {
83 $format = file_get_unused_draft_itemid();
84 }
b89cca19 85 $fieldname = 'field_' . $this->field->id . '_itemid';
1c3b2058 86 if (isset($formdata->$fieldname)) {
a5fae3b0 87 $draftitemid = clean_param($formdata->$fieldname, PARAM_INT);
1c3b2058
JO
88 } else {
89 $draftitemid = file_get_unused_draft_itemid();
90 }
b89cca19 91 $fieldname = 'field_' . $this->field->id;
1c3b2058
JO
92 if (isset($formdata->$fieldname)) {
93 $text = $formdata->$fieldname;
94 }
95 } else if ($recordid &&
96 $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
4e033542 97 $format = $content->content1;
29207706
MG
98 $text = clean_text($content->content, $format);
99 $text = file_prepare_draft_area($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $text);
0997e51a 100 } else {
29207706 101 $draftitemid = file_get_unused_draft_itemid();
3d27180e 102 $format = FORMAT_HTML;
29207706
MG
103 }
104
105 // get filepicker info
106 //
107 $fpoptions = array();
108 if ($options['maxfiles'] != 0 ) {
109 $args = new stdClass();
110 // need these three to filter repositories list
ae7f35b9 111 $args->accepted_types = array('web_image');
29207706
MG
112 $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL);
113 $args->context = $this->context;
114 $args->env = 'filepicker';
115 // advimage plugin
116 $image_options = initialise_filepicker($args);
117 $image_options->context = $this->context;
118 $image_options->client_id = uniqid();
119 $image_options->maxbytes = $options['maxbytes'];
120 $image_options->env = 'editor';
121 $image_options->itemid = $draftitemid;
122
123 // moodlemedia plugin
124 $args->accepted_types = array('video', 'audio');
125 $media_options = initialise_filepicker($args);
126 $media_options->context = $this->context;
127 $media_options->client_id = uniqid();
128 $media_options->maxbytes = $options['maxbytes'];
129 $media_options->env = 'editor';
130 $media_options->itemid = $draftitemid;
131
132 // advlink plugin
133 $args->accepted_types = '*';
134 $link_options = initialise_filepicker($args);
135 $link_options->context = $this->context;
136 $link_options->client_id = uniqid();
137 $link_options->maxbytes = $options['maxbytes'];
138 $link_options->env = 'editor';
139 $link_options->itemid = $draftitemid;
140
1be2350a
SA
141 // H5P plugin.
142 $args->accepted_types = ['h5p'];
143 $h5poptions = initialise_filepicker($args);
144 $h5poptions->context = $this->context;
145 $h5poptions->client_id = uniqid();
146 $h5poptions->maxbytes = $options['maxbytes'];
147 $h5poptions->env = 'editor';
148 $h5poptions->itemid = $draftitemid;
149
29207706
MG
150 $fpoptions['image'] = $image_options;
151 $fpoptions['media'] = $media_options;
152 $fpoptions['link'] = $link_options;
1be2350a 153 $fpoptions['h5p'] = $h5poptions;
4e033542 154 }
0997e51a 155
20e5da7d 156 $editor = editors_get_preferred_editor($format);
4e033542
SH
157 $strformats = format_text_menu();
158 $formats = $editor->get_supported_formats();
159 foreach ($formats as $fid) {
160 $formats[$fid] = $strformats[$fid];
11378212 161 }
988592c5 162 $editor->set_text($text);
29207706 163 $editor->use_editor($field, $options, $fpoptions);
a5fae3b0 164 $str .= '<input type="hidden" name="'.$field.'_itemid" value="'.s($draftitemid).'" />';
c07c5e7b 165 $str .= '<div class="mod-data-input">';
4f9f4b2a
BB
166 $str .= '<div><textarea id="'.$field.'" name="'.$field.'" rows="'.$this->field->param3.'" class="form-control" ' .
167 'cols="'.$this->field->param2.'" spellcheck="true">'.s($text).'</textarea></div>';
01f8e80d
RW
168 $str .= '<div><label class="accesshide" for="' . $field . '_content1">' . get_string('format') . '</label>';
169 $str .= '<select id="' . $field . '_content1" name="'.$field.'_content1">';
4e033542
SH
170 foreach ($formats as $key=>$desc) {
171 $selected = ($format == $key) ? 'selected="selected"' : '';
172 $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
173 }
174 $str .= '</select>';
4e033542 175
c07c5e7b 176 $str .= '</div>';
b89cca19 177 $str .= '</div>';
bbe39b6c 178 $str .= '</div>';
35bcb325 179 return $str;
180 }
1adbd2c3
PS
181
182
7900ecb0 183 function display_search_field($value = '') {
01f8e80d 184 return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' .
398be7c8
DW
185 '<input type="text" size="16" id="f_' . $this->field->id . '" name="f_' . $this->field->id . '" ' .
186 'value="' . s($value) . '" class="form-control"/>';
7900ecb0 187 }
1adbd2c3 188
c8a804ff
JL
189 public function parse_search_field($defaults = null) {
190 $param = 'f_'.$this->field->id;
191 if (empty($defaults[$param])) {
192 $defaults = array($param => '');
193 }
194 return optional_param($param, $defaults[$param], PARAM_NOTAGS);
7900ecb0 195 }
1adbd2c3 196
7900ecb0 197 function generate_sql($tablealias, $value) {
e3487936 198 global $DB;
199
e3487936 200 static $i=0;
201 $i++;
be4d5a92 202 $name = "df_textarea_$i";
800bb0f7 203 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
7900ecb0 204 }
1adbd2c3 205
f1ad19bc 206 function print_after_form() {
f1ad19bc 207 }
aab98aaf 208
209
0997e51a 210 function update_content($recordid, $value, $name='') {
a656d951 211 global $DB;
212
39790bd8 213 $content = new stdClass();
0997e51a 214 $content->fieldid = $this->field->id;
215 $content->recordid = $recordid;
0997e51a 216
217 $names = explode('_', $name);
218 if (!empty($names[2])) {
29207706
MG
219 if ($names[2] == 'itemid') {
220 // the value will be retrieved by file_get_submitted_draft_itemid, do not need to save in DB
221 return true;
222 } else {
67efc377 223 $content->{$names[2]} = clean_param($value, PARAM_NOTAGS); // content[1-4]
29207706 224 }
0997e51a 225 } else {
505d3123 226 $content->content = clean_param($value, PARAM_CLEAN);
11378212 227 }
0997e51a 228
a656d951 229 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
11378212 230 $content->id = $oldcontent->id;
0997e51a 231 } else {
29207706
MG
232 $content->id = $DB->insert_record('data_content', $content);
233 if (!$content->id) {
234 return false;
235 }
236 }
237 if (!empty($content->content)) {
238 $draftitemid = file_get_submitted_draft_itemid('field_'. $this->field->id. '_itemid');
239 $options = $this->get_options();
240 $content->content = file_save_draft_area_files($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $content->content);
11378212 241 }
29207706
MG
242 $rv = $DB->update_record('data_content', $content);
243 return $rv;
244 }
245
246 /**
247 * Display the content of the field in browse mode
248 *
249 * @param int $recordid
250 * @param object $template
251 * @return bool|string
252 */
253 function display_browse_field($recordid, $template) {
254 global $DB;
255
256 if ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
257 if (isset($content->content)) {
258 $options = new stdClass();
259 if ($this->field->param1 == '1') { // We are autolinking this field, so disable linking within us
260 $options->filter = false;
261 }
262 $options->para = false;
263 $str = file_rewrite_pluginfile_urls($content->content, 'pluginfile.php', $this->context->id, 'mod_data', 'content', $content->id, $this->get_options());
264 $str = format_text($str, $content->content1, $options);
265 } else {
266 $str = '';
267 }
268 return $str;
269 }
270 return false;
271 }
272
273 /**
274 * Whether this module support files
275 *
276 * @param string $relativepath
277 * @return bool
278 */
279 function file_ok($relativepath) {
280 return true;
11378212 281 }
1adbd2c3 282
b89cca19
DW
283 /**
284 * Only look at the first item (second is format)
285 *
286 * @param string $value
287 * @param string $name
288 * @return bool
289 */
290 function notemptyfield($value, $name) {
1c3b2058
JO
291 $names = explode('_', $name);
292 // Clean first.
b89cca19 293 if (count($names) == 2) {
8611d1df
AG
294 // Don't assume that this is coming from a text editor with tags.
295 return strval($value) !== '';
b89cca19
DW
296 }
297 return false;
298 }
2af9ad7d
DG
299
300 /**
301 * Returns the presentable string value for a field content.
fa90f529
DM
302 *
303 * The returned string should be plain text.
304 *
305 * @param stdClass $content
2af9ad7d
DG
306 * @return string
307 */
fa90f529
DM
308 public static function get_content_value($content) {
309 return content_to_text($content->content, $content->content1);
2af9ad7d
DG
310 }
311
aefe9c5f
JL
312 /**
313 * Return the plugin configs for external functions.
314 *
315 * @return array the list of config parameters
316 * @since Moodle 3.3
317 */
318 public function get_config_for_external() {
319 // Return all the config parameters.
320 $configs = [];
321 for ($i = 1; $i <= 10; $i++) {
322 $configs["param$i"] = $this->field->{"param$i"};
323 }
324 return $configs;
325 }
b89cca19 326}