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