MDL-5583 behat: Add multiline version of 'And I set the field to'
[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
0997e51a 52 function display_add_field($recordid=0) {
4e033542 53 global $CFG, $DB, $OUTPUT, $PAGE;
0997e51a 54
423bd918 55 $text = '';
56 $format = 0;
57
423bd918 58 $str = '<div title="'.$this->field->description.'">';
aab98aaf 59
4e033542 60 editors_head_setup();
29207706 61 $options = $this->get_options();
4e033542
SH
62
63 $itemid = $this->field->id;
64 $field = 'field_'.$itemid;
65
66 if ($recordid && $content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))){
4e033542 67 $format = $content->content1;
29207706
MG
68 $text = clean_text($content->content, $format);
69 $text = file_prepare_draft_area($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $text);
0997e51a 70 } else {
29207706 71 $draftitemid = file_get_unused_draft_itemid();
3d27180e 72 $format = FORMAT_HTML;
29207706
MG
73 }
74
75 // get filepicker info
76 //
77 $fpoptions = array();
78 if ($options['maxfiles'] != 0 ) {
79 $args = new stdClass();
80 // need these three to filter repositories list
ae7f35b9 81 $args->accepted_types = array('web_image');
29207706
MG
82 $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL);
83 $args->context = $this->context;
84 $args->env = 'filepicker';
85 // advimage plugin
86 $image_options = initialise_filepicker($args);
87 $image_options->context = $this->context;
88 $image_options->client_id = uniqid();
89 $image_options->maxbytes = $options['maxbytes'];
90 $image_options->env = 'editor';
91 $image_options->itemid = $draftitemid;
92
93 // moodlemedia plugin
94 $args->accepted_types = array('video', 'audio');
95 $media_options = initialise_filepicker($args);
96 $media_options->context = $this->context;
97 $media_options->client_id = uniqid();
98 $media_options->maxbytes = $options['maxbytes'];
99 $media_options->env = 'editor';
100 $media_options->itemid = $draftitemid;
101
102 // advlink plugin
103 $args->accepted_types = '*';
104 $link_options = initialise_filepicker($args);
105 $link_options->context = $this->context;
106 $link_options->client_id = uniqid();
107 $link_options->maxbytes = $options['maxbytes'];
108 $link_options->env = 'editor';
109 $link_options->itemid = $draftitemid;
110
111 $fpoptions['image'] = $image_options;
112 $fpoptions['media'] = $media_options;
113 $fpoptions['link'] = $link_options;
4e033542 114 }
0997e51a 115
20e5da7d 116 $editor = editors_get_preferred_editor($format);
4e033542
SH
117 $strformats = format_text_menu();
118 $formats = $editor->get_supported_formats();
119 foreach ($formats as $fid) {
120 $formats[$fid] = $strformats[$fid];
11378212 121 }
29207706
MG
122 $editor->use_editor($field, $options, $fpoptions);
123 $str .= '<input type="hidden" name="'.$field.'_itemid" value="'.$draftitemid.'" />';
0ac97084 124 $str .= '<div><textarea id="'.$field.'" name="'.$field.'" rows="'.$this->field->param3.'" cols="'.$this->field->param2.'" spellcheck="true">'.s($text).'</textarea></div>';
01f8e80d
RW
125 $str .= '<div><label class="accesshide" for="' . $field . '_content1">' . get_string('format') . '</label>';
126 $str .= '<select id="' . $field . '_content1" name="'.$field.'_content1">';
4e033542
SH
127 foreach ($formats as $key=>$desc) {
128 $selected = ($format == $key) ? 'selected="selected"' : '';
129 $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
130 }
131 $str .= '</select>';
4e033542
SH
132 $str .= '</div>';
133
bbe39b6c 134 $str .= '</div>';
35bcb325 135 return $str;
136 }
1adbd2c3
PS
137
138
7900ecb0 139 function display_search_field($value = '') {
01f8e80d
RW
140 return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' .
141 '<input type="text" size="16" id="f_'.$this->field->id.'" name="f_'.$this->field->id.'" value="'.$value.'" />';
7900ecb0 142 }
1adbd2c3 143
7900ecb0 144 function parse_search_field() {
145 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
146 }
1adbd2c3 147
7900ecb0 148 function generate_sql($tablealias, $value) {
e3487936 149 global $DB;
150
e3487936 151 static $i=0;
152 $i++;
be4d5a92 153 $name = "df_textarea_$i";
800bb0f7 154 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
7900ecb0 155 }
1adbd2c3 156
f1ad19bc 157 function print_after_form() {
f1ad19bc 158 }
aab98aaf 159
160
0997e51a 161 function update_content($recordid, $value, $name='') {
a656d951 162 global $DB;
163
39790bd8 164 $content = new stdClass();
0997e51a 165 $content->fieldid = $this->field->id;
166 $content->recordid = $recordid;
0997e51a 167
168 $names = explode('_', $name);
169 if (!empty($names[2])) {
29207706
MG
170 if ($names[2] == 'itemid') {
171 // the value will be retrieved by file_get_submitted_draft_itemid, do not need to save in DB
172 return true;
173 } else {
174 $content->$names[2] = clean_param($value, PARAM_NOTAGS); // content[1-4]
175 }
0997e51a 176 } else {
505d3123 177 $content->content = clean_param($value, PARAM_CLEAN);
11378212 178 }
0997e51a 179
a656d951 180 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
11378212 181 $content->id = $oldcontent->id;
0997e51a 182 } else {
29207706
MG
183 $content->id = $DB->insert_record('data_content', $content);
184 if (!$content->id) {
185 return false;
186 }
187 }
188 if (!empty($content->content)) {
189 $draftitemid = file_get_submitted_draft_itemid('field_'. $this->field->id. '_itemid');
190 $options = $this->get_options();
191 $content->content = file_save_draft_area_files($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $content->content);
11378212 192 }
29207706
MG
193 $rv = $DB->update_record('data_content', $content);
194 return $rv;
195 }
196
197 /**
198 * Display the content of the field in browse mode
199 *
200 * @param int $recordid
201 * @param object $template
202 * @return bool|string
203 */
204 function display_browse_field($recordid, $template) {
205 global $DB;
206
207 if ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
208 if (isset($content->content)) {
209 $options = new stdClass();
210 if ($this->field->param1 == '1') { // We are autolinking this field, so disable linking within us
211 $options->filter = false;
212 }
213 $options->para = false;
214 $str = file_rewrite_pluginfile_urls($content->content, 'pluginfile.php', $this->context->id, 'mod_data', 'content', $content->id, $this->get_options());
215 $str = format_text($str, $content->content1, $options);
216 } else {
217 $str = '';
218 }
219 return $str;
220 }
221 return false;
222 }
223
224 /**
225 * Whether this module support files
226 *
227 * @param string $relativepath
228 * @return bool
229 */
230 function file_ok($relativepath) {
231 return true;
11378212 232 }
35bcb325 233}
1adbd2c3 234