weekly release 2.3dev
[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
0997e51a 31 function display_add_field($recordid=0) {
4e033542 32 global $CFG, $DB, $OUTPUT, $PAGE;
0997e51a 33
423bd918 34 $text = '';
35 $format = 0;
36
423bd918 37 $str = '<div title="'.$this->field->description.'">';
aab98aaf 38
4e033542
SH
39 editors_head_setup();
40
41 $options = array();
42 $options['trusttext'] = false;
43 $options['forcehttps'] = false;
44 $options['subdirs'] = false;
45 $options['maxfiles'] = 0;
46 $options['maxbytes'] = 0;
47 $options['changeformat'] = 0;
48 $options['noclean'] = false;
49
50 $itemid = $this->field->id;
51 $field = 'field_'.$itemid;
52
53 if ($recordid && $content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))){
54 $text = $content->content;
55 $format = $content->content1;
b67bb422 56 $text = clean_text($text, $format);
4e033542
SH
57 } else if (can_use_html_editor()) {
58 $format = FORMAT_HTML;
0997e51a 59 } else {
4e033542
SH
60 $format = FORMAT_PLAIN;
61 }
0997e51a 62
20e5da7d 63 $editor = editors_get_preferred_editor($format);
4e033542
SH
64 $strformats = format_text_menu();
65 $formats = $editor->get_supported_formats();
66 foreach ($formats as $fid) {
67 $formats[$fid] = $strformats[$fid];
11378212 68 }
4e033542 69 $editor->use_editor($field, $options);
767cb7f0 70 $str .= '<div><textarea id="'.$field.'" name="'.$field.'" rows="'.$this->field->param3.'" cols="'.$this->field->param2.'">'.s($text).'</textarea></div>';
4e033542
SH
71 $str .= '<div><select name="'.$field.'_content1">';
72 foreach ($formats as $key=>$desc) {
73 $selected = ($format == $key) ? 'selected="selected"' : '';
74 $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
75 }
76 $str .= '</select>';
4e033542
SH
77 $str .= '</div>';
78
bbe39b6c 79 $str .= '</div>';
35bcb325 80 return $str;
81 }
1adbd2c3
PS
82
83
7900ecb0 84 function display_search_field($value = '') {
1adbd2c3 85 return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />';
7900ecb0 86 }
1adbd2c3 87
7900ecb0 88 function parse_search_field() {
89 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
90 }
1adbd2c3 91
7900ecb0 92 function generate_sql($tablealias, $value) {
e3487936 93 global $DB;
94
e3487936 95 static $i=0;
96 $i++;
be4d5a92 97 $name = "df_textarea_$i";
800bb0f7 98 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
7900ecb0 99 }
1adbd2c3 100
f1ad19bc 101 function print_after_form() {
f1ad19bc 102 }
aab98aaf 103
104
0997e51a 105 function update_content($recordid, $value, $name='') {
a656d951 106 global $DB;
107
39790bd8 108 $content = new stdClass();
0997e51a 109 $content->fieldid = $this->field->id;
110 $content->recordid = $recordid;
0997e51a 111
112 $names = explode('_', $name);
113 if (!empty($names[2])) {
114 $content->$names[2] = clean_param($value, PARAM_NOTAGS); // content[1-4]
115 } else {
505d3123 116 $content->content = clean_param($value, PARAM_CLEAN);
11378212 117 }
0997e51a 118
a656d951 119 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
11378212 120 $content->id = $oldcontent->id;
a656d951 121 return $DB->update_record('data_content', $content);
0997e51a 122 } else {
a656d951 123 return $DB->insert_record('data_content', $content);
11378212 124 }
125 }
35bcb325 126}
1adbd2c3 127