MDL-70114 mod_data: add border for to textarea
[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
141 $fpoptions['image'] = $image_options;
142 $fpoptions['media'] = $media_options;
143 $fpoptions['link'] = $link_options;
4e033542 144 }
0997e51a 145
20e5da7d 146 $editor = editors_get_preferred_editor($format);
4e033542
SH
147 $strformats = format_text_menu();
148 $formats = $editor->get_supported_formats();
149 foreach ($formats as $fid) {
150 $formats[$fid] = $strformats[$fid];
11378212 151 }
988592c5 152 $editor->set_text($text);
29207706 153 $editor->use_editor($field, $options, $fpoptions);
a5fae3b0 154 $str .= '<input type="hidden" name="'.$field.'_itemid" value="'.s($draftitemid).'" />';
c07c5e7b 155 $str .= '<div class="mod-data-input">';
4f9f4b2a
BB
156 $str .= '<div><textarea id="'.$field.'" name="'.$field.'" rows="'.$this->field->param3.'" class="form-control" ' .
157 'cols="'.$this->field->param2.'" spellcheck="true">'.s($text).'</textarea></div>';
01f8e80d
RW
158 $str .= '<div><label class="accesshide" for="' . $field . '_content1">' . get_string('format') . '</label>';
159 $str .= '<select id="' . $field . '_content1" name="'.$field.'_content1">';
4e033542
SH
160 foreach ($formats as $key=>$desc) {
161 $selected = ($format == $key) ? 'selected="selected"' : '';
162 $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
163 }
164 $str .= '</select>';
4e033542 165
c07c5e7b 166 $str .= '</div>';
b89cca19 167 $str .= '</div>';
bbe39b6c 168 $str .= '</div>';
35bcb325 169 return $str;
170 }
1adbd2c3
PS
171
172
7900ecb0 173 function display_search_field($value = '') {
01f8e80d 174 return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' .
398be7c8
DW
175 '<input type="text" size="16" id="f_' . $this->field->id . '" name="f_' . $this->field->id . '" ' .
176 'value="' . s($value) . '" class="form-control"/>';
7900ecb0 177 }
1adbd2c3 178
c8a804ff
JL
179 public function parse_search_field($defaults = null) {
180 $param = 'f_'.$this->field->id;
181 if (empty($defaults[$param])) {
182 $defaults = array($param => '');
183 }
184 return optional_param($param, $defaults[$param], PARAM_NOTAGS);
7900ecb0 185 }
1adbd2c3 186
7900ecb0 187 function generate_sql($tablealias, $value) {
e3487936 188 global $DB;
189
e3487936 190 static $i=0;
191 $i++;
be4d5a92 192 $name = "df_textarea_$i";
800bb0f7 193 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
7900ecb0 194 }
1adbd2c3 195
f1ad19bc 196 function print_after_form() {
f1ad19bc 197 }
aab98aaf 198
199
0997e51a 200 function update_content($recordid, $value, $name='') {
a656d951 201 global $DB;
202
39790bd8 203 $content = new stdClass();
0997e51a 204 $content->fieldid = $this->field->id;
205 $content->recordid = $recordid;
0997e51a 206
207 $names = explode('_', $name);
208 if (!empty($names[2])) {
29207706
MG
209 if ($names[2] == 'itemid') {
210 // the value will be retrieved by file_get_submitted_draft_itemid, do not need to save in DB
211 return true;
212 } else {
67efc377 213 $content->{$names[2]} = clean_param($value, PARAM_NOTAGS); // content[1-4]
29207706 214 }
0997e51a 215 } else {
505d3123 216 $content->content = clean_param($value, PARAM_CLEAN);
11378212 217 }
0997e51a 218
a656d951 219 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
11378212 220 $content->id = $oldcontent->id;
0997e51a 221 } else {
29207706
MG
222 $content->id = $DB->insert_record('data_content', $content);
223 if (!$content->id) {
224 return false;
225 }
226 }
227 if (!empty($content->content)) {
228 $draftitemid = file_get_submitted_draft_itemid('field_'. $this->field->id. '_itemid');
229 $options = $this->get_options();
230 $content->content = file_save_draft_area_files($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $content->content);
11378212 231 }
29207706
MG
232 $rv = $DB->update_record('data_content', $content);
233 return $rv;
234 }
235
236 /**
237 * Display the content of the field in browse mode
238 *
239 * @param int $recordid
240 * @param object $template
241 * @return bool|string
242 */
243 function display_browse_field($recordid, $template) {
244 global $DB;
245
246 if ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
247 if (isset($content->content)) {
248 $options = new stdClass();
249 if ($this->field->param1 == '1') { // We are autolinking this field, so disable linking within us
250 $options->filter = false;
251 }
252 $options->para = false;
253 $str = file_rewrite_pluginfile_urls($content->content, 'pluginfile.php', $this->context->id, 'mod_data', 'content', $content->id, $this->get_options());
254 $str = format_text($str, $content->content1, $options);
255 } else {
256 $str = '';
257 }
258 return $str;
259 }
260 return false;
261 }
262
263 /**
264 * Whether this module support files
265 *
266 * @param string $relativepath
267 * @return bool
268 */
269 function file_ok($relativepath) {
270 return true;
11378212 271 }
1adbd2c3 272
b89cca19
DW
273 /**
274 * Only look at the first item (second is format)
275 *
276 * @param string $value
277 * @param string $name
278 * @return bool
279 */
280 function notemptyfield($value, $name) {
1c3b2058
JO
281 $names = explode('_', $name);
282 // Clean first.
b89cca19 283 if (count($names) == 2) {
8611d1df
AG
284 // Don't assume that this is coming from a text editor with tags.
285 return strval($value) !== '';
b89cca19
DW
286 }
287 return false;
288 }
2af9ad7d
DG
289
290 /**
291 * Returns the presentable string value for a field content.
fa90f529
DM
292 *
293 * The returned string should be plain text.
294 *
295 * @param stdClass $content
2af9ad7d
DG
296 * @return string
297 */
fa90f529
DM
298 public static function get_content_value($content) {
299 return content_to_text($content->content, $content->content1);
2af9ad7d
DG
300 }
301
aefe9c5f
JL
302 /**
303 * Return the plugin configs for external functions.
304 *
305 * @return array the list of config parameters
306 * @since Moodle 3.3
307 */
308 public function get_config_for_external() {
309 // Return all the config parameters.
310 $configs = [];
311 for ($i = 1; $i <= 10; $i++) {
312 $configs["param$i"] = $this->field->{"param$i"};
313 }
314 return $configs;
315 }
b89cca19 316}