Merge branch 'MDL-27675_master' of git://github.com/grabs/moodle
[moodle.git] / mod / feedback / item / textarea / lib.php
CommitLineData
1adbd2c3 1<?php
9c5bc7a5
AG
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
01910dff 17defined('MOODLE_INTERNAL') OR die('not allowed');
c70ad9f7 18require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
19
20class feedback_item_textarea extends feedback_item_base {
9c5bc7a5
AG
21 protected $type = "textarea";
22 private $commonparams;
23 private $item_form;
24 private $item;
271fdd62 25
9c5bc7a5 26 public function init() {
d4b1d58c 27
c70ad9f7 28 }
d4b1d58c 29
9c5bc7a5 30 public function build_editform($item, $feedback, $cm) {
271fdd62 31 global $DB, $CFG;
6ee09cfe 32 require_once('textarea_form.php');
d4b1d58c 33
271fdd62
AG
34 //get the lastposition number of the feedback_items
35 $position = $item->position;
36 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
9c5bc7a5 37 if ($position == -1) {
271fdd62
AG
38 $i_formselect_last = $lastposition + 1;
39 $i_formselect_value = $lastposition + 1;
40 $item->position = $lastposition + 1;
9c5bc7a5 41 } else {
271fdd62
AG
42 $i_formselect_last = $lastposition;
43 $i_formselect_value = $item->position;
6ee09cfe 44 }
271fdd62 45 //the elements for position dropdownlist
9c5bc7a5
AG
46 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
47
271fdd62 48 $item->presentation = empty($item->presentation) ? '' : $item->presentation;
9c5bc7a5
AG
49
50 $width_and_height = explode('|', $item->presentation);
51
52 if (isset($width_and_height[0]) AND $width_and_height[0] >= 5) {
53 $itemwidth = $width_and_height[0];
54 } else {
55 $itemwidth = 30;
56 }
57
58 if (isset($width_and_height[1])) {
59 $itemheight = $width_and_height[1];
60 } else {
61 $itemheight = 5;
62 }
271fdd62
AG
63 $item->itemwidth = $itemwidth;
64 $item->itemheight = $itemheight;
65
73043833
AG
66 //all items for dependitem
67 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
271fdd62 68 $commonparams = array('cmid'=>$cm->id,
9c5bc7a5 69 'id'=>isset($item->id) ? $item->id : null,
271fdd62 70 'typ'=>$item->typ,
73043833 71 'items'=>$feedbackitems,
271fdd62
AG
72 'feedback'=>$feedback->id);
73
74 //build the form
9c5bc7a5
AG
75 $customdata = array('item' => $item,
76 'common' => $commonparams,
77 'positionlist' => $positionlist,
78 'position' => $position);
79
80 $this->item_form = new feedback_textarea_form('edit_item.php', $customdata);
271fdd62
AG
81 }
82
83 //this function only can used after the call of build_editform()
9c5bc7a5 84 public function show_editform() {
271fdd62
AG
85 $this->item_form->display();
86 }
9c5bc7a5
AG
87
88 public function is_cancelled() {
271fdd62
AG
89 return $this->item_form->is_cancelled();
90 }
91
9c5bc7a5
AG
92 public function get_data() {
93 if ($this->item = $this->item_form->get_data()) {
271fdd62
AG
94 return true;
95 }
96 return false;
97 }
d4b1d58c 98
9c5bc7a5 99 public function save_item() {
271fdd62 100 global $DB;
9c5bc7a5
AG
101
102 if (!$item = $this->item_form->get_data()) {
271fdd62
AG
103 return false;
104 }
9c5bc7a5
AG
105
106 if (isset($item->clone_item) AND $item->clone_item) {
9e1aed53
AG
107 $item->id = ''; //to clone this item
108 $item->position++;
109 }
9c5bc7a5 110
45c2f92a 111 $item->hasvalue = $this->get_hasvalue();
9c5bc7a5 112 if (!$item->id) {
271fdd62 113 $item->id = $DB->insert_record('feedback_item', $item);
9c5bc7a5 114 } else {
271fdd62
AG
115 $DB->update_record('feedback_item', $item);
116 }
9c5bc7a5 117
271fdd62 118 return $DB->get_record('feedback_item', array('id'=>$item->id));
c70ad9f7 119 }
120
271fdd62 121
c70ad9f7 122 //liefert eine Struktur ->name, ->data = array(mit Antworten)
9c5bc7a5 123 public function get_analysed($item, $groupid = false, $courseid = false) {
0085fff8 124 global $DB;
125
411fd747 126 $analysed_val = new stdClass();
9c5bc7a5
AG
127 $analysed_val->data = array();
128 $analysed_val->name = $item->name;
129
c70ad9f7 130 $values = feedback_get_group_values($item, $groupid, $courseid);
9c5bc7a5 131 if ($values) {
c70ad9f7 132 $data = array();
9c5bc7a5 133 foreach ($values as $value) {
c70ad9f7 134 $data[] = str_replace("\n", '<br />', $value->value);
135 }
9c5bc7a5 136 $analysed_val->data = $data;
c70ad9f7 137 }
9c5bc7a5 138 return $analysed_val;
c70ad9f7 139 }
140
9c5bc7a5 141 public function get_printval($item, $value) {
d4b1d58c 142
9c5bc7a5
AG
143 if (!isset($value->value)) {
144 return '';
145 }
c70ad9f7 146
147 return $value->value;
148 }
149
9c5bc7a5 150 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
c70ad9f7 151 $values = feedback_get_group_values($item, $groupid, $courseid);
9c5bc7a5
AG
152 if ($values) {
153 echo '<tr><th colspan="2" align="left">';
154 echo $itemnr.'&nbsp;('.$item->label.') '.$item->name;
155 echo '</th></tr>';
156 foreach ($values as $value) {
157 echo '<tr>';
158 echo '<td valign="top" align="left">';
159 echo '-&nbsp;&nbsp;';
160 echo '</td>';
161 echo '<td align="left" valign="top">';
162 echo str_replace("\n", '<br />', $value->value);
163 echo '</td>';
164 echo '</tr>';
c70ad9f7 165 }
c70ad9f7 166 }
c70ad9f7 167 }
168
9c5bc7a5
AG
169 public function excelprint_item(&$worksheet, $row_offset,
170 $xls_formats, $item,
171 $groupid, $courseid = false) {
172
c70ad9f7 173 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
174
9c5bc7a5
AG
175 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
176 $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
c70ad9f7 177 $data = $analysed_item->data;
9c5bc7a5
AG
178 if (is_array($data)) {
179 if (isset($data[0])) {
180 $worksheet->write_string($row_offset, 2, $data[0], $xls_formats->value_bold);
d5580808 181 }
9c5bc7a5
AG
182 $row_offset++;
183 $sizeofdata = count($data);
184 for ($i = 1; $i < $sizeofdata; $i++) {
185 $worksheet->write_string($row_offset, 2, $data[$i], $xls_formats->default);
186 $row_offset++;
c70ad9f7 187 }
188 }
9c5bc7a5
AG
189 $row_offset++;
190 return $row_offset;
c70ad9f7 191 }
9c5bc7a5 192
9d5fbd65
AG
193 /**
194 * print the item at the edit-page of feedback
195 *
196 * @global object
197 * @param object $item
198 * @return void
199 */
9c5bc7a5 200 public function print_item_preview($item) {
73043833 201 global $OUTPUT, $DB;
9c5bc7a5 202
9d5fbd65 203 $align = right_to_left() ? 'right' : 'left';
9c5bc7a5 204 $str_required_mark = '<span class="feedback_required_mark">*</span>';
c70ad9f7 205
9d5fbd65 206 $presentation = explode ("|", $item->presentation);
9c5bc7a5 207 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
afdb1920
AG
208 //print the question and label
209 echo '<div class="feedback_item_label_'.$align.'">';
210 echo '('.$item->label.') ';
211 echo format_text($item->name.$requiredmark, true, false, false);
9c5bc7a5
AG
212 if ($item->dependitem) {
213 if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
214 echo ' <span class="feedback_depend">';
215 echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
216 echo '</span>';
73043833
AG
217 }
218 }
afdb1920
AG
219 echo '</div>';
220
221 //print the presentation
222 echo '<div class="feedback_item_presentation_'.$align.'">';
223 echo '<span class="feedback_item_textarea">';
9c5bc7a5
AG
224 echo '<textarea name="'.$item->typ.'_'.$item->id.'" '.
225 'cols="'.$presentation[0].'" '.
226 'rows="'.$presentation[1].'">';
227 echo '</textarea>';
afdb1920
AG
228 echo '</span>';
229 echo '</div>';
9d5fbd65 230 }
9c5bc7a5 231
9d5fbd65
AG
232 /**
233 * print the item at the complete-page of feedback
234 *
235 * @global object
236 * @param object $item
237 * @param string $value
238 * @param bool $highlightrequire
239 * @return void
240 */
9c5bc7a5 241 public function print_item_complete($item, $value = '', $highlightrequire = false) {
d4b1d58c 242 global $OUTPUT;
e372f4c7 243 $align = right_to_left() ? 'right' : 'left';
9c5bc7a5 244 $str_required_mark = '<span class="feedback_required_mark">*</span>';
d4b1d58c 245
c70ad9f7 246 $presentation = explode ("|", $item->presentation);
9c5bc7a5 247 if ($highlightrequire AND $item->required AND strval($value) == '') {
afdb1920 248 $highlight = ' missingrequire';
9c5bc7a5 249 } else {
c70ad9f7 250 $highlight = '';
251 }
9c5bc7a5 252 $requiredmark = ($item->required == 1) ? $str_required_mark :'';
afdb1920
AG
253
254 //print the question and label
255 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
b7a47958 256 echo format_text($item->name . $requiredmark, true, false, false);
afdb1920 257 echo '</div>';
9c5bc7a5 258
afdb1920
AG
259 //print the presentation
260 echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
261 echo '<span class="feedback_item_textarea">';
9c5bc7a5
AG
262 echo '<textarea name="'.$item->typ.'_'.$item->id.'" '.
263 'cols="'.$presentation[0].'" '.
264 'rows="'.$presentation[1].'">';
e7fc2ff4 265 echo $value;
9c5bc7a5 266 echo '</textarea>';
afdb1920
AG
267 echo '</span>';
268 echo '</div>';
9d5fbd65
AG
269 }
270
271 /**
272 * print the item at the complete-page of feedback
273 *
274 * @global object
275 * @param object $item
276 * @param string $value
277 * @return void
278 */
9c5bc7a5 279 public function print_item_show_value($item, $value = '') {
9d5fbd65
AG
280 global $OUTPUT;
281 $align = right_to_left() ? 'right' : 'left';
9c5bc7a5 282 $str_required_mark = '<span class="feedback_required_mark">*</span>';
9d5fbd65
AG
283
284 $presentation = explode ("|", $item->presentation);
9c5bc7a5 285 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
afdb1920
AG
286
287 //print the question and label
288 echo '<div class="feedback_item_label_'.$align.'">';
9d5fbd65
AG
289 echo '('.$item->label.') ';
290 echo format_text($item->name . $requiredmark, true, false, false);
afdb1920 291 echo '</div>';
9c5bc7a5 292
afdb1920 293 //print the presentation
9d5fbd65 294 echo $OUTPUT->box_start('generalbox boxalign'.$align);
9c5bc7a5 295 echo $value ? str_replace("\n", '<br />', $value) : '&nbsp;';
9d5fbd65 296 echo $OUTPUT->box_end();
c70ad9f7 297 }
298
9c5bc7a5 299 public function check_value($value, $item) {
c70ad9f7 300 //if the item is not required, so the check is true if no value is given
9c5bc7a5
AG
301 if ((!isset($value) OR $value == '') AND $item->required != 1) {
302 return true;
303 }
304 if ($value == "") {
305 return false;
306 }
c70ad9f7 307 return true;
308 }
309
9c5bc7a5 310 public function create_value($data) {
e7fc2ff4 311 $data = s($data);
c70ad9f7 312 return $data;
313 }
314
73043833
AG
315 //compares the dbvalue with the dependvalue
316 //dbvalue is the value put in by the user
317 //dependvalue is the value that is compared
9c5bc7a5
AG
318 public function compare_value($item, $dbvalue, $dependvalue) {
319 if ($dbvalue == $dependvalue) {
73043833
AG
320 return true;
321 }
322 return false;
323 }
9c5bc7a5
AG
324
325 public function get_presentation($data) {
326 return $data->itemwidth.'|'.$data->itemheight;
c70ad9f7 327 }
328
9c5bc7a5 329 public function get_hasvalue() {
c70ad9f7 330 return 1;
331 }
9c5bc7a5
AG
332
333 public function can_switch_require() {
6cc1599e
AG
334 return true;
335 }
e7fc2ff4
AG
336
337 public function value_type() {
338 return PARAM_RAW;
339 }
340
d2448b8b 341 public function clean_input_value($value) {
e7fc2ff4
AG
342 return s($value);
343 }
c70ad9f7 344}