2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
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 //
14 // (at your option) any later version. // // //
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: //
20 // http://www.gnu.org/copyleft/gpl.html //
22 ///////////////////////////////////////////////////////////////////////////
24 require_once($CFG->dirroot.'/lib/filelib.php');
25 require_once($CFG->dirroot.'/repository/lib.php');
27 class data_field_textarea extends data_field_base {
29 var $type = 'textarea';
31 * priority for globalsearch indexing
35 protected static $priority = self::LOW_PRIORITY;
38 * Returns options for embedded files
42 private function get_options() {
43 if (!isset($this->field->param5)) {
44 $this->field->param5 = 0;
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;
58 function display_add_field($recordid = 0, $formdata = null) {
59 global $CFG, $DB, $OUTPUT, $PAGE;
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');
66 if ($this->field->required) {
67 $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
68 $str .= html_writer::div($image, 'inline-req');
73 $options = $this->get_options();
75 $itemid = $this->field->id;
76 $field = 'field_'.$itemid;
79 $fieldname = 'field_' . $this->field->id . '_content1';
80 if (isset($formdata->$fieldname)) {
81 $format = $formdata->$fieldname;
83 $format = file_get_unused_draft_itemid();
85 $fieldname = 'field_' . $this->field->id . '_itemid';
86 if (isset($formdata->$fieldname)) {
87 $draftitemid = clean_param($formdata->$fieldname, PARAM_INT);
89 $draftitemid = file_get_unused_draft_itemid();
91 $fieldname = 'field_' . $this->field->id;
92 if (isset($formdata->$fieldname)) {
93 $text = $formdata->$fieldname;
95 } else if ($recordid &&
96 $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
97 $format = $content->content1;
98 $text = clean_text($content->content, $format);
99 $text = file_prepare_draft_area($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $text);
101 $draftitemid = file_get_unused_draft_itemid();
102 $format = FORMAT_HTML;
105 // get filepicker info
107 $fpoptions = array();
108 if ($options['maxfiles'] != 0 ) {
109 $args = new stdClass();
110 // need these three to filter repositories list
111 $args->accepted_types = array('web_image');
112 $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL);
113 $args->context = $this->context;
114 $args->env = 'filepicker';
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;
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;
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;
141 $fpoptions['image'] = $image_options;
142 $fpoptions['media'] = $media_options;
143 $fpoptions['link'] = $link_options;
146 $editor = editors_get_preferred_editor($format);
147 $strformats = format_text_menu();
148 $formats = $editor->get_supported_formats();
149 foreach ($formats as $fid) {
150 $formats[$fid] = $strformats[$fid];
152 $editor->set_text($text);
153 $editor->use_editor($field, $options, $fpoptions);
154 $str .= '<input type="hidden" name="'.$field.'_itemid" value="'.s($draftitemid).'" />';
155 $str .= '<div class="mod-data-input">';
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>';
158 $str .= '<div><label class="accesshide" for="' . $field . '_content1">' . get_string('format') . '</label>';
159 $str .= '<select id="' . $field . '_content1" name="'.$field.'_content1">';
160 foreach ($formats as $key=>$desc) {
161 $selected = ($format == $key) ? 'selected="selected"' : '';
162 $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
173 function display_search_field($value = '') {
174 return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' .
175 '<input type="text" size="16" id="f_' . $this->field->id . '" name="f_' . $this->field->id . '" ' .
176 'value="' . s($value) . '" class="form-control"/>';
179 public function parse_search_field($defaults = null) {
180 $param = 'f_'.$this->field->id;
181 if (empty($defaults[$param])) {
182 $defaults = array($param => '');
184 return optional_param($param, $defaults[$param], PARAM_NOTAGS);
187 function generate_sql($tablealias, $value) {
192 $name = "df_textarea_$i";
193 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
196 function print_after_form() {
200 function update_content($recordid, $value, $name='') {
203 $content = new stdClass();
204 $content->fieldid = $this->field->id;
205 $content->recordid = $recordid;
207 $names = explode('_', $name);
208 if (!empty($names[2])) {
209 if ($names[2] == 'itemid') {
210 // the value will be retrieved by file_get_submitted_draft_itemid, do not need to save in DB
213 $content->{$names[2]} = clean_param($value, PARAM_NOTAGS); // content[1-4]
216 $content->content = clean_param($value, PARAM_CLEAN);
219 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
220 $content->id = $oldcontent->id;
222 $content->id = $DB->insert_record('data_content', $content);
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);
232 $rv = $DB->update_record('data_content', $content);
237 * Display the content of the field in browse mode
239 * @param int $recordid
240 * @param object $template
241 * @return bool|string
243 function display_browse_field($recordid, $template) {
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;
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);
264 * Whether this module support files
266 * @param string $relativepath
269 function file_ok($relativepath) {
274 * Only look at the first item (second is format)
276 * @param string $value
277 * @param string $name
280 function notemptyfield($value, $name) {
281 $names = explode('_', $name);
283 if (count($names) == 2) {
284 // Don't assume that this is coming from a text editor with tags.
285 return strval($value) !== '';
291 * Returns the presentable string value for a field content.
293 * The returned string should be plain text.
295 * @param stdClass $content
298 public static function get_content_value($content) {
299 return content_to_text($content->content, $content->content1);
303 * Return the plugin configs for external functions.
305 * @return array the list of config parameters
308 public function get_config_for_external() {
309 // Return all the config parameters.
311 for ($i = 1; $i <= 10; $i++) {
312 $configs["param$i"] = $this->field->{"param$i"};