MDL-57822 feedback: Correct page param for nonrespondents WS
[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;
1c3b2058 63 $str = '<div title="' . s($this->field->description) . '">';
930ce605
DW
64 $str .= '<label for="field_' . $this->field->id . '" class="accesshide">';
65 $str .= html_writer::span($this->field->name);
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">';
0ac97084 156 $str .= '<div><textarea id="'.$field.'" name="'.$field.'" rows="'.$this->field->param3.'" cols="'.$this->field->param2.'" spellcheck="true">'.s($text).'</textarea></div>';
01f8e80d
RW
157 $str .= '<div><label class="accesshide" for="' . $field . '_content1">' . get_string('format') . '</label>';
158 $str .= '<select id="' . $field . '_content1" name="'.$field.'_content1">';
4e033542
SH
159 foreach ($formats as $key=>$desc) {
160 $selected = ($format == $key) ? 'selected="selected"' : '';
161 $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
162 }
163 $str .= '</select>';
4e033542 164
c07c5e7b 165 $str .= '</div>';
b89cca19 166 $str .= '</div>';
bbe39b6c 167 $str .= '</div>';
35bcb325 168 return $str;
169 }
1adbd2c3
PS
170
171
7900ecb0 172 function display_search_field($value = '') {
01f8e80d 173 return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' .
398be7c8
DW
174 '<input type="text" size="16" id="f_' . $this->field->id . '" name="f_' . $this->field->id . '" ' .
175 'value="' . s($value) . '" class="form-control"/>';
7900ecb0 176 }
1adbd2c3 177
7900ecb0 178 function parse_search_field() {
179 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
180 }
1adbd2c3 181
7900ecb0 182 function generate_sql($tablealias, $value) {
e3487936 183 global $DB;
184
e3487936 185 static $i=0;
186 $i++;
be4d5a92 187 $name = "df_textarea_$i";
800bb0f7 188 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
7900ecb0 189 }
1adbd2c3 190
f1ad19bc 191 function print_after_form() {
f1ad19bc 192 }
aab98aaf 193
194
0997e51a 195 function update_content($recordid, $value, $name='') {
a656d951 196 global $DB;
197
39790bd8 198 $content = new stdClass();
0997e51a 199 $content->fieldid = $this->field->id;
200 $content->recordid = $recordid;
0997e51a 201
202 $names = explode('_', $name);
203 if (!empty($names[2])) {
29207706
MG
204 if ($names[2] == 'itemid') {
205 // the value will be retrieved by file_get_submitted_draft_itemid, do not need to save in DB
206 return true;
207 } else {
67efc377 208 $content->{$names[2]} = clean_param($value, PARAM_NOTAGS); // content[1-4]
29207706 209 }
0997e51a 210 } else {
505d3123 211 $content->content = clean_param($value, PARAM_CLEAN);
11378212 212 }
0997e51a 213
a656d951 214 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
11378212 215 $content->id = $oldcontent->id;
0997e51a 216 } else {
29207706
MG
217 $content->id = $DB->insert_record('data_content', $content);
218 if (!$content->id) {
219 return false;
220 }
221 }
222 if (!empty($content->content)) {
223 $draftitemid = file_get_submitted_draft_itemid('field_'. $this->field->id. '_itemid');
224 $options = $this->get_options();
225 $content->content = file_save_draft_area_files($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $content->content);
11378212 226 }
29207706
MG
227 $rv = $DB->update_record('data_content', $content);
228 return $rv;
229 }
230
231 /**
232 * Display the content of the field in browse mode
233 *
234 * @param int $recordid
235 * @param object $template
236 * @return bool|string
237 */
238 function display_browse_field($recordid, $template) {
239 global $DB;
240
241 if ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
242 if (isset($content->content)) {
243 $options = new stdClass();
244 if ($this->field->param1 == '1') { // We are autolinking this field, so disable linking within us
245 $options->filter = false;
246 }
247 $options->para = false;
248 $str = file_rewrite_pluginfile_urls($content->content, 'pluginfile.php', $this->context->id, 'mod_data', 'content', $content->id, $this->get_options());
249 $str = format_text($str, $content->content1, $options);
250 } else {
251 $str = '';
252 }
253 return $str;
254 }
255 return false;
256 }
257
258 /**
259 * Whether this module support files
260 *
261 * @param string $relativepath
262 * @return bool
263 */
264 function file_ok($relativepath) {
265 return true;
11378212 266 }
1adbd2c3 267
b89cca19
DW
268 /**
269 * Only look at the first item (second is format)
270 *
271 * @param string $value
272 * @param string $name
273 * @return bool
274 */
275 function notemptyfield($value, $name) {
1c3b2058
JO
276 $names = explode('_', $name);
277 // Clean first.
b89cca19 278 if (count($names) == 2) {
8611d1df
AG
279 // Don't assume that this is coming from a text editor with tags.
280 return strval($value) !== '';
b89cca19
DW
281 }
282 return false;
283 }
2af9ad7d
DG
284
285 /**
286 * Returns the presentable string value for a field content.
fa90f529
DM
287 *
288 * The returned string should be plain text.
289 *
290 * @param stdClass $content
2af9ad7d
DG
291 * @return string
292 */
fa90f529
DM
293 public static function get_content_value($content) {
294 return content_to_text($content->content, $content->content1);
2af9ad7d
DG
295 }
296
b89cca19 297}