Fixed php notice
[moodle.git] / mod / data / field / textarea / field.class.php
CommitLineData
35bcb325 1<?php ///Class file for textarea field, extends base_field
2///////////////////////////////////////////////////////////////////////////
3// //
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
7// http://moodle.org //
8// //
9// Copyright (C) 2005 Martin Dougiamas http://dougiamas.com //
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 //
14// (at your option) any later version. //
15// //
16// This program is distributed in the hope that it will be useful, //
17// but WITHOUT ANY WARRANTY; without even the implied warranty of //
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19// GNU General Public License for more details: //
20// //
21// http://www.gnu.org/copyleft/gpl.html //
22// //
23///////////////////////////////////////////////////////////////////////////
24
25/// Please refer to lib.php for method comments
26
27class data_field_textarea extends data_field_base {
28
29 var $type = 'textarea';
30 var $id;
31
32
33 function data_field_textarea($fid=0){
34 parent::data_field_base($fid);
35 }
36
37
38 /***********************************************
39 * Saves the field into the database *
40 ***********************************************/
0bbc6f57 41 function insert_field($dataid, $type='textarea', $name, $desc='', $width='', $height='') {
35bcb325 42 $newfield = new object;
43 $newfield->dataid = $dataid;
44 $newfield->type = $type;
45 $newfield->name = $name;
46 $newfield->description = $desc;
0bbc6f57 47 $newfield->param1 = $width;
48 $newfield->param2 = $height;
35bcb325 49
50 if (!insert_record('data_fields', $newfield)) {
51 notify('Insertion of new field failed!');
52 }
53 }
54
55
56 /***********************************************
57 * Prints the form element in the add template *
58 ***********************************************/
59 function display_add_field($id, $rid=0) {
60 global $CFG;
61 if (!$field = get_record('data_fields', 'id', $id)){
62 notify('That is not a valid field id!');
63 exit;
64 }
65 if ($rid) {
11378212 66 $dataContent = get_record('data_content', 'fieldid', $id, 'recordid', $rid);
67 $content = $dataContent->content;
35bcb325 68 }
69 else {
70 $content = '';
71 }
72 $str = '';
bbe39b6c 73 /*
35bcb325 74 if ($field->description) {
75 $str .= '<img src="'.$CFG->pixpath.'/help.gif" alt="'.$field->description.'" title="'.$field->description.'" />&nbsp;';
bbe39b6c 76 }*/
77 $str .= '<div title="'.$field->description.'">';
11378212 78
f1ad19bc 79 if (can_use_richtext_editor()) {
80 // Show a rich text html editor.
81 $str .= helpbutton("richtext", get_string("helprichtext"), "moodle", true, true, '', true);
82 $str .= data_field_textarea::gen_textarea(true, 'field_' . $field->id, $field->param2, $field->param3, $content);
83 $str .= '<input type="hidden" name="field_' . $field->id . '_content1' . '" value="' . FORMAT_HTML . '" />';
84 }
85 else {
86 // Show a normal textarea. Also let the user specify the format to be used.
87 $str .= data_field_textarea::gen_textarea(false, 'field_' . $field->id, $field->param2, $field->param3, $content);
f1ad19bc 88 // Get the available text formats for this field.
89 $formatsForField = format_text_menu();
11378212 90 $str .= '<br />';
91
92 if (empty($dataContent->content1)) {
93 $str .= choose_from_menu($formatsForField, 'field_' . $field->id . '_content1', '', 'choose', '', '', true);
94 }
95 else {
96 $str .= choose_from_menu($formatsForField, 'field_' . $field->id . '_content1', $dataContent->content1, 'choose', '', '', true);
97 }
f1ad19bc 98 $str .= helpbutton("textformat", get_string("helpformatting"), 'moodle', true, false, '', true);
11378212 99 }
bbe39b6c 100 $str .= '</div>';
35bcb325 101 return $str;
102 }
f1ad19bc 103
104
105 function gen_textarea($usehtmleditor, $name, $cols=65, $rows=10, $value='') {
106 global $CFG, $course;
f1ad19bc 107
663b45a8 108 return print_textarea($usehtmleditor, $rows, $cols, '', '', $name, $value, '', true);
f1ad19bc 109 }
110
111
112 function print_after_form() {
113 if (can_use_richtext_editor()) {
6b9dbb37 114 use_html_editor('field_' . $this->id);
f1ad19bc 115 }
116 }
117
118
35bcb325 119 function display_edit_field($id, $mode=0) {
120 parent::display_edit_field($id, $mode);
121 }
122
6b9dbb37 123
35bcb325 124 function update($fieldobject) {
125 $fieldobject->param1 = trim($fieldobject->param1);
126 $fieldobject->param2 = trim($fieldobject->param2);
11378212 127
35bcb325 128 if (!update_record('data_fields',$fieldobject)){
129 notify ('upate failed');
130 }
131 }
11378212 132
133
134 /************************************
135 * store content of this field type *
136 ************************************/
137 function store_data_content($fieldid, $recordid, $value, $name=''){
138 if ($value) {
139 $content = new object;
140 $content->fieldid = $fieldid;
141 $content->recordid = $recordid;
142
143 if ($oldcontent = get_record('data_content','fieldid', $fieldid, 'recordid', $recordid)) {
144 // This belongs to an existing data_content.
145 $content->id = $oldcontent->id;
146 $nameParts = explode('_', $name);
147 $column = $nameParts[count($nameParts) - 1]; // Format is field_<fieldid>_content[1 to 4]
148
f1ad19bc 149 $content->$column = clean_param($value, PARAM_INT);
11378212 150 update_record('data_content', $content);
151 }
152 else {
153 // First (and maybe only) data content for this field for this record.
f1ad19bc 154 $content->content = clean_param($value, PARAM_CLEANHTML);
11378212 155 insert_record('data_content', $content);
156 }
157 }
158 }
159
160
161 /*************************************
162 * update content of this field type *
163 *************************************/
164 function update_data_content($fieldid, $recordid, $value, $name=''){
165 // If data_content already exists, we update.
c87fbb27 166 if ($oldcontent = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)){
11378212 167 $content = new object;
168 $content->fieldid = $fieldid;
169 $content->recordid = $recordid;
170
171 $nameParts = explode('_', $name);
172 if (!empty($nameParts[2])) {
173 $content->$nameParts[2] = clean_param($value, PARAM_NOTAGS);
174 }
175 else {
176 $content->content = clean_param($value, PARAM_NOTAGS);
177 }
178 $content->id = $oldcontent->id;
179 update_record('data_content', $content);
180 }
181 else { //make 1 if there isn't one already
182 $this->store_data_content($fieldid, $recordid, $value, $name='');
183 }
184 }
35bcb325 185}
186?>