weekly release 3.2dev
[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';
aab98aaf 30
29207706
MG
31 /**
32 * Returns options for embedded files
33 *
34 * @return array
35 */
36 private function get_options() {
37 if (!isset($this->field->param5)) {
38 $this->field->param5 = 0;
39 }
40 $options = array();
41 $options['trusttext'] = false;
42 $options['forcehttps'] = false;
43 $options['subdirs'] = false;
44 $options['maxfiles'] = -1;
45 $options['context'] = $this->context;
46 $options['maxbytes'] = $this->field->param5;
47 $options['changeformat'] = 0;
48 $options['noclean'] = false;
49 return $options;
50 }
51
b89cca19 52 function display_add_field($recordid = 0, $formdata = null) {
4e033542 53 global $CFG, $DB, $OUTPUT, $PAGE;
0997e51a 54
423bd918 55 $text = '';
56 $format = 0;
1c3b2058
JO
57 $str = '<div title="' . s($this->field->description) . '">';
58 $str .= '<label for="field_' . $this->field->id . '">';
59 $str .= html_writer::span($this->field->name, "accesshide");
b89cca19 60 if ($this->field->required) {
c07c5e7b 61 $image = html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'),
1c3b2058 62 array('class' => 'req', 'title' => get_string('requiredelement', 'form')));
c07c5e7b 63 $str .= html_writer::div($image, 'inline-req');
b89cca19 64 }
1c3b2058 65 $str .= '</label>';
aab98aaf 66
4e033542 67 editors_head_setup();
29207706 68 $options = $this->get_options();
4e033542
SH
69
70 $itemid = $this->field->id;
71 $field = 'field_'.$itemid;
72
b89cca19
DW
73 if ($formdata) {
74 $fieldname = 'field_' . $this->field->id . '_content1';
1c3b2058
JO
75 if (isset($formdata->$fieldname)) {
76 $format = $formdata->$fieldname;
77 } else {
78 $format = file_get_unused_draft_itemid();
79 }
b89cca19 80 $fieldname = 'field_' . $this->field->id . '_itemid';
1c3b2058 81 if (isset($formdata->$fieldname)) {
a5fae3b0 82 $draftitemid = clean_param($formdata->$fieldname, PARAM_INT);
1c3b2058
JO
83 } else {
84 $draftitemid = file_get_unused_draft_itemid();
85 }
b89cca19 86 $fieldname = 'field_' . $this->field->id;
1c3b2058
JO
87 if (isset($formdata->$fieldname)) {
88 $text = $formdata->$fieldname;
89 }
90 } else if ($recordid &&
91 $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
4e033542 92 $format = $content->content1;
29207706
MG
93 $text = clean_text($content->content, $format);
94 $text = file_prepare_draft_area($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $text);
0997e51a 95 } else {
29207706 96 $draftitemid = file_get_unused_draft_itemid();
3d27180e 97 $format = FORMAT_HTML;
29207706
MG
98 }
99
100 // get filepicker info
101 //
102 $fpoptions = array();
103 if ($options['maxfiles'] != 0 ) {
104 $args = new stdClass();
105 // need these three to filter repositories list
ae7f35b9 106 $args->accepted_types = array('web_image');
29207706
MG
107 $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL);
108 $args->context = $this->context;
109 $args->env = 'filepicker';
110 // advimage plugin
111 $image_options = initialise_filepicker($args);
112 $image_options->context = $this->context;
113 $image_options->client_id = uniqid();
114 $image_options->maxbytes = $options['maxbytes'];
115 $image_options->env = 'editor';
116 $image_options->itemid = $draftitemid;
117
118 // moodlemedia plugin
119 $args->accepted_types = array('video', 'audio');
120 $media_options = initialise_filepicker($args);
121 $media_options->context = $this->context;
122 $media_options->client_id = uniqid();
123 $media_options->maxbytes = $options['maxbytes'];
124 $media_options->env = 'editor';
125 $media_options->itemid = $draftitemid;
126
127 // advlink plugin
128 $args->accepted_types = '*';
129 $link_options = initialise_filepicker($args);
130 $link_options->context = $this->context;
131 $link_options->client_id = uniqid();
132 $link_options->maxbytes = $options['maxbytes'];
133 $link_options->env = 'editor';
134 $link_options->itemid = $draftitemid;
135
136 $fpoptions['image'] = $image_options;
137 $fpoptions['media'] = $media_options;
138 $fpoptions['link'] = $link_options;
4e033542 139 }
0997e51a 140
20e5da7d 141 $editor = editors_get_preferred_editor($format);
4e033542
SH
142 $strformats = format_text_menu();
143 $formats = $editor->get_supported_formats();
144 foreach ($formats as $fid) {
145 $formats[$fid] = $strformats[$fid];
11378212 146 }
988592c5 147 $editor->set_text($text);
29207706 148 $editor->use_editor($field, $options, $fpoptions);
a5fae3b0 149 $str .= '<input type="hidden" name="'.$field.'_itemid" value="'.s($draftitemid).'" />';
c07c5e7b 150 $str .= '<div class="mod-data-input">';
0ac97084 151 $str .= '<div><textarea id="'.$field.'" name="'.$field.'" rows="'.$this->field->param3.'" cols="'.$this->field->param2.'" spellcheck="true">'.s($text).'</textarea></div>';
01f8e80d
RW
152 $str .= '<div><label class="accesshide" for="' . $field . '_content1">' . get_string('format') . '</label>';
153 $str .= '<select id="' . $field . '_content1" name="'.$field.'_content1">';
4e033542
SH
154 foreach ($formats as $key=>$desc) {
155 $selected = ($format == $key) ? 'selected="selected"' : '';
156 $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
157 }
158 $str .= '</select>';
4e033542 159
c07c5e7b 160 $str .= '</div>';
b89cca19 161 $str .= '</div>';
bbe39b6c 162 $str .= '</div>';
35bcb325 163 return $str;
164 }
1adbd2c3
PS
165
166
7900ecb0 167 function display_search_field($value = '') {
01f8e80d 168 return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' .
a5fae3b0 169 '<input type="text" size="16" id="f_'.$this->field->id.'" name="f_'.$this->field->id.'" value="'.s($value).'" />';
7900ecb0 170 }
1adbd2c3 171
7900ecb0 172 function parse_search_field() {
173 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
174 }
1adbd2c3 175
7900ecb0 176 function generate_sql($tablealias, $value) {
e3487936 177 global $DB;
178
e3487936 179 static $i=0;
180 $i++;
be4d5a92 181 $name = "df_textarea_$i";
800bb0f7 182 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
7900ecb0 183 }
1adbd2c3 184
f1ad19bc 185 function print_after_form() {
f1ad19bc 186 }
aab98aaf 187
188
0997e51a 189 function update_content($recordid, $value, $name='') {
a656d951 190 global $DB;
191
39790bd8 192 $content = new stdClass();
0997e51a 193 $content->fieldid = $this->field->id;
194 $content->recordid = $recordid;
0997e51a 195
196 $names = explode('_', $name);
197 if (!empty($names[2])) {
29207706
MG
198 if ($names[2] == 'itemid') {
199 // the value will be retrieved by file_get_submitted_draft_itemid, do not need to save in DB
200 return true;
201 } else {
67efc377 202 $content->{$names[2]} = clean_param($value, PARAM_NOTAGS); // content[1-4]
29207706 203 }
0997e51a 204 } else {
505d3123 205 $content->content = clean_param($value, PARAM_CLEAN);
11378212 206 }
0997e51a 207
a656d951 208 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
11378212 209 $content->id = $oldcontent->id;
0997e51a 210 } else {
29207706
MG
211 $content->id = $DB->insert_record('data_content', $content);
212 if (!$content->id) {
213 return false;
214 }
215 }
216 if (!empty($content->content)) {
217 $draftitemid = file_get_submitted_draft_itemid('field_'. $this->field->id. '_itemid');
218 $options = $this->get_options();
219 $content->content = file_save_draft_area_files($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $content->content);
11378212 220 }
29207706
MG
221 $rv = $DB->update_record('data_content', $content);
222 return $rv;
223 }
224
225 /**
226 * Display the content of the field in browse mode
227 *
228 * @param int $recordid
229 * @param object $template
230 * @return bool|string
231 */
232 function display_browse_field($recordid, $template) {
233 global $DB;
234
235 if ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
236 if (isset($content->content)) {
237 $options = new stdClass();
238 if ($this->field->param1 == '1') { // We are autolinking this field, so disable linking within us
239 $options->filter = false;
240 }
241 $options->para = false;
242 $str = file_rewrite_pluginfile_urls($content->content, 'pluginfile.php', $this->context->id, 'mod_data', 'content', $content->id, $this->get_options());
243 $str = format_text($str, $content->content1, $options);
244 } else {
245 $str = '';
246 }
247 return $str;
248 }
249 return false;
250 }
251
252 /**
253 * Whether this module support files
254 *
255 * @param string $relativepath
256 * @return bool
257 */
258 function file_ok($relativepath) {
259 return true;
11378212 260 }
1adbd2c3 261
b89cca19
DW
262 /**
263 * Only look at the first item (second is format)
264 *
265 * @param string $value
266 * @param string $name
267 * @return bool
268 */
269 function notemptyfield($value, $name) {
1c3b2058
JO
270 $names = explode('_', $name);
271 // Clean first.
b89cca19 272 if (count($names) == 2) {
8611d1df
AG
273 // Don't assume that this is coming from a text editor with tags.
274 return strval($value) !== '';
b89cca19
DW
275 }
276 return false;
277 }
278}