Merge branch 'MDL-27675_master' of git://github.com/grabs/moodle
[moodle.git] / mod / feedback / item / multichoice / 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');
6ee09cfe 18require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
19
20define('FEEDBACK_MULTICHOICE_TYPE_SEP', '>>>>>');
21define('FEEDBACK_MULTICHOICE_LINE_SEP', '|');
22define('FEEDBACK_MULTICHOICE_ADJUST_SEP', '<<<<<');
79473294
AG
23define('FEEDBACK_MULTICHOICE_IGNOREEMPTY', 'i');
24define('FEEDBACK_MULTICHOICE_HIDENOSELECT', 'h');
6ee09cfe 25
26class feedback_item_multichoice extends feedback_item_base {
9c5bc7a5
AG
27 protected $type = "multichoice";
28 private $commonparams;
29 private $item_form;
30 private $item;
39790bd8 31
9c5bc7a5 32 public function init() {
d4b1d58c 33
6ee09cfe 34 }
d4b1d58c 35
9c5bc7a5 36 public function build_editform($item, $feedback, $cm) {
a59ff6b0
AG
37 global $DB, $CFG;
38 require_once('multichoice_form.php');
39
40 //get the lastposition number of the feedback_items
41 $position = $item->position;
42 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
9c5bc7a5 43 if ($position == -1) {
a59ff6b0
AG
44 $i_formselect_last = $lastposition + 1;
45 $i_formselect_value = $lastposition + 1;
46 $item->position = $lastposition + 1;
9c5bc7a5 47 } else {
a59ff6b0
AG
48 $i_formselect_last = $lastposition;
49 $i_formselect_value = $item->position;
50 }
51 //the elements for position dropdownlist
9c5bc7a5 52 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
39790bd8 53
a59ff6b0
AG
54 $item->presentation = empty($item->presentation) ? '' : $item->presentation;
55 $info = $this->get_info($item);
56
79473294
AG
57 $item->ignoreempty = $this->ignoreempty($item);
58 $item->hidenoselect = $this->hidenoselect($item);
39790bd8 59
73043833
AG
60 //all items for dependitem
61 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
a59ff6b0 62 $commonparams = array('cmid'=>$cm->id,
9c5bc7a5 63 'id'=>isset($item->id) ? $item->id : null,
a59ff6b0 64 'typ'=>$item->typ,
73043833 65 'items'=>$feedbackitems,
a59ff6b0
AG
66 'feedback'=>$feedback->id);
67
68 //build the form
9c5bc7a5
AG
69 $customdata = array('item' => $item,
70 'common' => $commonparams,
71 'positionlist' => $positionlist,
72 'position' => $position,
73 'info' => $info);
74
75 $this->item_form = new feedback_multichoice_form('edit_item.php', $customdata);
a59ff6b0
AG
76 }
77
78 //this function only can used after the call of build_editform()
9c5bc7a5 79 public function show_editform() {
a59ff6b0
AG
80 $this->item_form->display();
81 }
39790bd8 82
9c5bc7a5 83 public function is_cancelled() {
a59ff6b0
AG
84 return $this->item_form->is_cancelled();
85 }
86
9c5bc7a5
AG
87 public function get_data() {
88 if ($this->item = $this->item_form->get_data()) {
a59ff6b0
AG
89 return true;
90 }
91 return false;
92 }
93
9c5bc7a5 94 public function save_item() {
a59ff6b0 95 global $DB;
39790bd8 96
9c5bc7a5 97 if (!$item = $this->item_form->get_data()) {
a59ff6b0
AG
98 return false;
99 }
39790bd8 100
9c5bc7a5 101 if (isset($item->clone_item) AND $item->clone_item) {
9e1aed53
AG
102 $item->id = ''; //to clone this item
103 $item->position++;
104 }
39790bd8
PS
105
106 $this->set_ignoreempty($item, $item->ignoreempty);
79473294
AG
107 $this->set_hidenoselect($item, $item->hidenoselect);
108
45c2f92a 109 $item->hasvalue = $this->get_hasvalue();
9c5bc7a5 110 if (!$item->id) {
a59ff6b0 111 $item->id = $DB->insert_record('feedback_item', $item);
9c5bc7a5 112 } else {
a59ff6b0
AG
113 $DB->update_record('feedback_item', $item);
114 }
39790bd8 115
a59ff6b0
AG
116 return $DB->get_record('feedback_item', array('id'=>$item->id));
117 }
118
6ee09cfe 119
9c5bc7a5
AG
120 //gets an array with three values(typ, name, XXX)
121 //XXX is an object with answertext, answercount and quotient
122 public function get_analysed($item, $groupid = false, $courseid = false) {
6ee09cfe 123 $info = $this->get_info($item);
d4b1d58c 124
9c5bc7a5
AG
125 $analysed_item = array();
126 $analysed_item[] = $item->typ;
127 $analysed_item[] = $item->name;
d4b1d58c 128
9c5bc7a5
AG
129 //get the possible answers
130 $answers = null;
294ce987 131 $answers = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
9c5bc7a5
AG
132 if (!is_array($answers)) {
133 return null;
134 }
6ee09cfe 135
9c5bc7a5 136 //get the values
79473294 137 $values = feedback_get_group_values($item, $groupid, $courseid, $this->ignoreempty($item));
9c5bc7a5
AG
138 if (!$values) {
139 return null;
140 }
d4b1d58c 141
9c5bc7a5
AG
142 //get answertext, answercount and quotient for each answer
143 $analysed_answer = array();
144 if ($info->subtype == 'c') {
145 $sizeofanswers = count($answers);
80c12897 146 for ($i = 1; $i <= $sizeofanswers; $i++) {
411fd747 147 $ans = new stdClass();
6ee09cfe 148 $ans->answertext = $answers[$i-1];
149 $ans->answercount = 0;
80c12897 150 foreach ($values as $value) {
6ee09cfe 151 //ist die Antwort gleich dem index der Antworten + 1?
152 $vallist = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value->value);
80c12897 153 foreach ($vallist as $val) {
6ee09cfe 154 if ($val == $i) {
9c5bc7a5 155 $ans->answercount++;
6ee09cfe 156 }
157 }
158 }
9c5bc7a5
AG
159 $ans->quotient = $ans->answercount / count($values);
160 $analysed_answer[] = $ans;
6ee09cfe 161 }
9c5bc7a5
AG
162 } else {
163 $sizeofanswers = count($answers);
80c12897 164 for ($i = 1; $i <= $sizeofanswers; $i++) {
411fd747 165 $ans = new stdClass();
6ee09cfe 166 $ans->answertext = $answers[$i-1];
167 $ans->answercount = 0;
80c12897 168 foreach ($values as $value) {
6ee09cfe 169 //ist die Antwort gleich dem index der Antworten + 1?
170 if ($value->value == $i) {
171 $ans->answercount++;
172 }
173 }
9c5bc7a5
AG
174 $ans->quotient = $ans->answercount / count($values);
175 $analysed_answer[] = $ans;
6ee09cfe 176 }
177 }
9c5bc7a5
AG
178 $analysed_item[] = $analysed_answer;
179 return $analysed_item;
6ee09cfe 180 }
181
9c5bc7a5 182 public function get_printval($item, $value) {
6ee09cfe 183 $info = $this->get_info($item);
d4b1d58c 184
6ee09cfe 185 $printval = '';
d4b1d58c 186
80c12897
SH
187 if (!isset($value->value)) {
188 return $printval;
189 }
d4b1d58c 190
294ce987 191 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
d4b1d58c 192
80c12897 193 if ($info->subtype == 'c') {
6ee09cfe 194 $vallist = array_values(explode (FEEDBACK_MULTICHOICE_LINE_SEP, $value->value));
9c5bc7a5
AG
195 $sizeofvallist = count($vallist);
196 $sizeofpresentation = count($presentation);
80c12897
SH
197 for ($i = 0; $i < $sizeofvallist; $i++) {
198 for ($k = 0; $k < $sizeofpresentation; $k++) {
199 if ($vallist[$i] == ($k + 1)) {//Die Werte beginnen bei 1, das Array aber mit 0
6ee09cfe 200 $printval .= trim($presentation[$k]) . chr(10);
201 break;
202 }
203 }
204 }
80c12897 205 } else {
6ee09cfe 206 $index = 1;
9c5bc7a5
AG
207 foreach ($presentation as $pres) {
208 if ($value->value == $index) {
6ee09cfe 209 $printval = $pres;
210 break;
211 }
212 $index++;
213 }
214 }
215 return $printval;
216 }
217
9c5bc7a5 218 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
536328fb 219 global $OUTPUT;
6ee09cfe 220 $sep_dec = get_string('separator_decimal', 'feedback');
9c5bc7a5 221 if (substr($sep_dec, 0, 2) == '[[') {
6ee09cfe 222 $sep_dec = FEEDBACK_DECIMAL;
223 }
d4b1d58c 224
6ee09cfe 225 $sep_thous = get_string('separator_thousand', 'feedback');
9c5bc7a5 226 if (substr($sep_thous, 0, 2) == '[[') {
6ee09cfe 227 $sep_thous = FEEDBACK_THOUSAND;
228 }
d4b1d58c 229
9c5bc7a5
AG
230 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
231 if ($analysed_item) {
232 $itemname = $analysed_item[1];
233 echo '<tr><th colspan="2" align="left">';
234 echo $itemnr.'&nbsp;('.$item->label.') '.$itemname;
235 echo '</th></tr>';
236
237 $analysed_vals = $analysed_item[2];
6ee09cfe 238 $pixnr = 0;
9c5bc7a5 239 foreach ($analysed_vals as $val) {
b7030c3d 240 $intvalue = $pixnr % 10;
536328fb 241 $pix = $OUTPUT->pix_url('multichoice/' . $intvalue, 'feedback');
6ee09cfe 242 $pixnr++;
243 $pixwidth = intval($val->quotient * FEEDBACK_MAX_PIX_LENGTH);
244 $quotient = number_format(($val->quotient * 100), 2, $sep_dec, $sep_thous);
9c5bc7a5
AG
245 $str_quotient = '';
246 if ($val->quotient > 0) {
247 $str_quotient = '&nbsp;('. $quotient . '&nbsp;%)';
248 }
249 echo '<tr>';
250 echo '<td align="left" valign="top">
251 -&nbsp;&nbsp;'.trim($val->answertext).':
252 </td>
253 <td align="left" style="width:'.FEEDBACK_MAX_PIX_LENGTH.';">
254 <img alt="'.$intvalue.'" src="'.$pix.'" height="5" width="'.$pixwidth.'" />
255 &nbsp;'.$val->answercount.$str_quotient.'
256 </td>';
257 echo '</tr>';
6ee09cfe 258 }
259 }
6ee09cfe 260 }
261
9c5bc7a5
AG
262 public function excelprint_item(&$worksheet, $row_offset,
263 $xls_formats, $item,
264 $groupid, $courseid = false) {
265
6ee09cfe 266 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
267
6ee09cfe 268 $data = $analysed_item[2];
269
6ee09cfe 270 //frage schreiben
9c5bc7a5
AG
271 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
272 $worksheet->write_string($row_offset, 1, $analysed_item[1], $xls_formats->head2);
80c12897 273 if (is_array($data)) {
9c5bc7a5 274 $sizeofdata = count($data);
80c12897 275 for ($i = 0; $i < $sizeofdata; $i++) {
9c5bc7a5
AG
276 $analysed_data = $data[$i];
277
278 $worksheet->write_string($row_offset,
279 $i + 2,
280 trim($analysed_data->answertext),
281 $xls_formats->head2);
282
283 $worksheet->write_number($row_offset + 1,
284 $i + 2,
285 $analysed_data->answercount,
286 $xls_formats->default);
287
288 $worksheet->write_number($row_offset + 2,
289 $i + 2,
290 $analysed_data->quotient,
291 $xls_formats->procent);
6ee09cfe 292 }
293 }
9c5bc7a5
AG
294 $row_offset += 3;
295 return $row_offset;
6ee09cfe 296 }
39790bd8
PS
297
298 /**
9d5fbd65
AG
299 * print the item at the edit-page of feedback
300 *
301 * @global object
302 * @param object $item
303 * @return void
304 */
9c5bc7a5 305 public function print_item_preview($item) {
73043833 306 global $OUTPUT, $DB;
9d5fbd65
AG
307 $info = $this->get_info($item);
308 $align = right_to_left() ? 'right' : 'left';
309
310 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
9c5bc7a5 311 $str_required_mark = '<span class="feedback_required_mark">*</span>';
9d5fbd65
AG
312
313 //test if required and no value is set so we have to mark this item
314 //we have to differ check and the other subtypes
9c5bc7a5 315 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
39790bd8 316
afdb1920
AG
317 //print the question and label
318 echo '<div class="feedback_item_label_'.$align.'">';
319 echo '('.$item->label.') ';
320 echo format_text($item->name.$requiredmark, true, false, false);
9c5bc7a5
AG
321 if ($item->dependitem) {
322 if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
323 echo ' <span class="feedback_depend">';
324 echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
325 echo '</span>';
73043833
AG
326 }
327 }
afdb1920 328 echo '</div>';
39790bd8 329
afdb1920
AG
330 //print the presentation
331 echo '<div class="feedback_item_presentation_'.$align.'">';
9d5fbd65
AG
332 $index = 1;
333 $checked = '';
afdb1920 334 echo '<ul>';
9c5bc7a5 335 if ($info->horizontal) {
afdb1920 336 $hv = 'h';
9c5bc7a5 337 } else {
afdb1920
AG
338 $hv = 'v';
339 }
39790bd8 340
9c5bc7a5 341 if ($info->subtype == 'r' AND !$this->hidenoselect($item)) {
afdb1920 342 //print the "not_selected" item on radiobuttons
9d5fbd65 343 ?>
9c5bc7a5
AG
344 <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
345 <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
346 <?php
347 echo '<input type="radio" '.
348 'name="'.$item->typ.'_'.$item->id.'[]" '.
349 'id="'.$item->typ.'_'.$item->id.'_xxx" '.
350 'value="" checked="checked" />';
351 ?>
352 </span>
353 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
354 <label for="<?php echo $item->typ . '_' . $item->id.'_xxx';?>">
355 <?php print_string('not_selected', 'feedback');?>&nbsp;
356 </label>
357 </span>
358 </li>
9d5fbd65
AG
359 <?php
360 }
9d5fbd65
AG
361
362 switch($info->subtype) {
363 case 'r':
364 $this->print_item_radio($presentation, $item, false, $info, $align);
365 break;
366 case 'c':
367 $this->print_item_check($presentation, $item, false, $info, $align);
368 break;
369 case 'd':
370 $this->print_item_dropdown($presentation, $item, false, $info, $align);
371 break;
372 }
afdb1920
AG
373 echo '</ul>';
374 echo '</div>';
9d5fbd65 375 }
39790bd8
PS
376
377 /**
9d5fbd65
AG
378 * print the item at the complete-page of feedback
379 *
380 * @global object
381 * @param object $item
382 * @param string $value
383 * @param bool $highlightrequire
384 * @return void
385 */
9c5bc7a5 386 public function print_item_complete($item, $value = null, $highlightrequire = false) {
d4b1d58c 387 global $OUTPUT;
6ee09cfe 388 $info = $this->get_info($item);
e372f4c7 389 $align = right_to_left() ? 'right' : 'left';
d4b1d58c 390
9c5bc7a5
AG
391 if ($value == null) {
392 $value = array();
393 }
294ce987 394 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
9c5bc7a5 395 $str_required_mark = '<span class="feedback_required_mark">*</span>';
d4b1d58c 396
6ee09cfe 397 //test if required and no value is set so we have to mark this item
398 //we have to differ check and the other subtypes
9c5bc7a5 399 // if ($info->subtype == 'c') {
6ee09cfe 400 if (is_array($value)) {
401 $values = $value;
9c5bc7a5 402 } else {
6ee09cfe 403 $values = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
404 }
9c5bc7a5
AG
405 $highlight = '';
406 if ($highlightrequire AND $item->required) {
407 if (count($values) == 0 OR $values[0] == '' OR $values[0] == 0) {
408 $highlight = ' missingrequire';
409 }
6ee09cfe 410 }
9c5bc7a5
AG
411 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
412 // } else {
413 // if ($highlightrequire AND $item->required AND intval($value) <= 0) {
414 // $highlight = ' missingrequire';
415 // } else {
416 // $highlight = '';
417 // }
418 // $requiredmark = ($item->required == 1) ? $str_required_mark : '';
419 // }
afdb1920
AG
420
421 //print the question and label
422 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
423 echo format_text($item->name.$requiredmark, true, false, false);
424 echo '</div>';
425
426 //print the presentation
427 echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
39790bd8 428
afdb1920 429 echo '<ul>';
9c5bc7a5 430 if ($info->horizontal) {
afdb1920 431 $hv = 'h';
9c5bc7a5 432 } else {
afdb1920
AG
433 $hv = 'v';
434 }
9d5fbd65 435 //print the "not_selected" item on radiobuttons
9c5bc7a5 436 if ($info->subtype == 'r' AND !$this->hidenoselect($item)) {
9d5fbd65 437 ?>
afdb1920
AG
438 <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
439 <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
9c5bc7a5
AG
440 <?php
441 $checked = '';
442 // if (!$value) {
443 // $checked = 'checked="checked"';
444 // }
445 if (count($values) == 0 OR $values[0] == '' OR $values[0] == 0) {
446 $checked = 'checked="checked"';
447 }
448 echo '<input type="radio" '.
449 'name="'.$item->typ.'_'.$item->id.'[]" '.
450 'id="'.$item->typ.'_'.$item->id.'_xxx" '.
451 'value="" '.$checked.' />';
452 ?>
afdb1920
AG
453 </span>
454 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
9c5bc7a5
AG
455 <label for="<?php echo $item->typ.'_'.$item->id.'_xxx';?>">
456 <?php print_string('not_selected', 'feedback');?>&nbsp;
457 </label>
afdb1920
AG
458 </span>
459 </li>
9d5fbd65
AG
460 <?php
461 }
9d5fbd65
AG
462
463 switch($info->subtype) {
464 case 'r':
465 $this->print_item_radio($presentation, $item, $value, $info, $align);
466 break;
467 case 'c':
468 $this->print_item_check($presentation, $item, $value, $info, $align);
469 break;
470 case 'd':
471 $this->print_item_dropdown($presentation, $item, $value, $info, $align);
472 break;
473 }
afdb1920
AG
474 echo '</ul>';
475 echo '</div>';
9d5fbd65 476 }
d4b1d58c 477
39790bd8 478 /**
9d5fbd65
AG
479 * print the item at the complete-page of feedback
480 *
481 * @global object
482 * @param object $item
483 * @param string $value
484 * @return void
485 */
9c5bc7a5 486 public function print_item_show_value($item, $value = null) {
9d5fbd65
AG
487 global $OUTPUT;
488 $info = $this->get_info($item);
489 $align = right_to_left() ? 'right' : 'left';
490
9c5bc7a5
AG
491 if ($value == null) {
492 $value = array();
493 }
9d5fbd65 494
9c5bc7a5 495 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
9d5fbd65
AG
496
497 //test if required and no value is set so we have to mark this item
498 //we have to differ check and the other subtypes
9c5bc7a5 499 if ($info->subtype == 'c') {
9d5fbd65
AG
500 if (is_array($value)) {
501 $values = $value;
9c5bc7a5 502 } else {
9d5fbd65 503 $values = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
6ee09cfe 504 }
9c5bc7a5
AG
505 }
506 $requiredmark = '';
507 if ($item->required == 1) {
508 $requiredmark = '<span class="feedback_required_mark">*</span>';
9d5fbd65 509 }
39790bd8 510
afdb1920
AG
511 //print the question and label
512 echo '<div class="feedback_item_label_'.$align.'">';
513 echo '('.$item->label.') ';
514 echo format_text($item->name . $requiredmark, true, false, false);
515 echo '</div>';
39790bd8 516
afdb1920
AG
517 //print the presentation
518 echo '<div class="feedback_item_presentation_'.$align.'">';
9d5fbd65 519 $index = 1;
9c5bc7a5 520 if ($info->subtype == 'c') {
9d5fbd65 521 echo $OUTPUT->box_start('generalbox boxalign'.$align);
9c5bc7a5
AG
522 foreach ($presentation as $pres) {
523 foreach ($values as $val) {
524 if ($val == $index) {
afdb1920
AG
525 echo '<div class="feedback_item_multianswer">';
526 echo text_to_html($pres, true, false, false);
527 echo '</div>';
9d5fbd65
AG
528 break;
529 }
6ee09cfe 530 }
9d5fbd65 531 $index++;
6ee09cfe 532 }
9d5fbd65 533 echo $OUTPUT->box_end();
9c5bc7a5
AG
534 } else {
535 foreach ($presentation as $pres) {
536 if ($value == $index) {
9d5fbd65
AG
537 echo $OUTPUT->box_start('generalbox boxalign'.$align);
538 echo text_to_html($pres, true, false, false);
539 echo $OUTPUT->box_end();
540 break;
541 }
542 $index++;
6ee09cfe 543 }
6ee09cfe 544 }
afdb1920 545 echo '</div>';
6ee09cfe 546 }
547
9c5bc7a5 548 public function check_value($value, $item) {
6ee09cfe 549 $info = $this->get_info($item);
d4b1d58c 550
9c5bc7a5 551 if ($item->required != 1) {
6ee09cfe 552 return true;
6ee09cfe 553 }
9c5bc7a5
AG
554
555 if (!isset($value) OR !is_array($value) OR $value[0] == '' OR $value[0] == 0) {
556 return false;
557 }
558
559 return true;
6ee09cfe 560 }
561
9c5bc7a5 562 public function create_value($data) {
6ee09cfe 563 $vallist = $data;
9c5bc7a5 564 return trim($this->item_array_to_string($vallist));
6ee09cfe 565 }
39790bd8 566
73043833
AG
567 //compares the dbvalue with the dependvalue
568 //dbvalue is the number of one selection
569 //dependvalue is the presentation of one selection
9c5bc7a5 570 public function compare_value($item, $dbvalue, $dependvalue) {
6ee09cfe 571
73043833
AG
572 if (is_array($dbvalue)) {
573 $dbvalues = $dbvalue;
9c5bc7a5 574 } else {
73043833
AG
575 $dbvalues = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $dbvalue);
576 }
39790bd8 577
73043833
AG
578 $info = $this->get_info($item);
579 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
580 $index = 1;
9c5bc7a5
AG
581 foreach ($presentation as $pres) {
582 foreach ($dbvalues as $dbval) {
583 if ($dbval == $index AND trim($pres) == $dependvalue) {
73043833
AG
584 return true;
585 }
586 }
587 $index++;
588 }
589 return false;
590 }
39790bd8 591
9c5bc7a5 592 public function get_presentation($data) {
6ee09cfe 593 $present = str_replace("\n", FEEDBACK_MULTICHOICE_LINE_SEP, trim($data->itemvalues));
9c5bc7a5 594 if (!isset($data->subtype)) {
6ee09cfe 595 $subtype = 'r';
9c5bc7a5 596 } else {
6ee09cfe 597 $subtype = substr($data->subtype, 0, 1);
598 }
9c5bc7a5 599 if (isset($data->horizontal) AND $data->horizontal == 1 AND $subtype != 'd') {
6ee09cfe 600 $present .= FEEDBACK_MULTICHOICE_ADJUST_SEP.'1';
601 }
602 return $subtype.FEEDBACK_MULTICHOICE_TYPE_SEP.$present;
603 }
604
9c5bc7a5 605 public function get_hasvalue() {
6ee09cfe 606 return 1;
607 }
d4b1d58c 608
9c5bc7a5 609 private function get_info($item) {
6ee09cfe 610 $presentation = empty($item->presentation) ? '' : $item->presentation;
d4b1d58c 611
39790bd8 612 $info = new stdClass();
6ee09cfe 613 //check the subtype of the multichoice
614 //it can be check(c), radio(r) or dropdown(d)
615 $info->subtype = '';
616 $info->presentation = '';
617 $info->horizontal = false;
d4b1d58c 618
9c5bc7a5
AG
619 $parts = explode(FEEDBACK_MULTICHOICE_TYPE_SEP, $item->presentation);
620 @list($info->subtype, $info->presentation) = $parts;
621 if (!isset($info->subtype)) {
6ee09cfe 622 $info->subtype = 'r';
623 }
624
9c5bc7a5
AG
625 if ($info->subtype != 'd') {
626 $parts = explode(FEEDBACK_MULTICHOICE_ADJUST_SEP, $info->presentation);
627 @list($info->presentation, $info->horizontal) = $parts;
628 if (isset($info->horizontal) AND $info->horizontal == 1) {
6ee09cfe 629 $info->horizontal = true;
9c5bc7a5 630 } else {
6ee09cfe 631 $info->horizontal = false;
632 }
633 }
634 return $info;
635 }
d4b1d58c 636
9c5bc7a5 637 private function item_array_to_string($value) {
80c12897 638 if (!is_array($value)) {
6ee09cfe 639 return $value;
640 }
641 $retval = '';
642 $arrvals = array_values($value);
9c5bc7a5 643 $arrvals = clean_param_array($arrvals, PARAM_INT); //prevent sql-injection
6ee09cfe 644 $retval = $arrvals[0];
9c5bc7a5 645 $sizeofarrvals = count($arrvals);
80c12897 646 for ($i = 1; $i < $sizeofarrvals; $i++) {
6ee09cfe 647 $retval .= FEEDBACK_MULTICHOICE_LINE_SEP.$arrvals[$i];
648 }
649 return $retval;
650 }
d4b1d58c 651
9c5bc7a5 652 private function print_item_radio($presentation, $item, $value, $info, $align) {
6ee09cfe 653 $index = 1;
654 $checked = '';
39790bd8 655
9c5bc7a5
AG
656 if (is_array($value)) {
657 $values = $value;
658 } else {
659 $values = array($value);
660 }
661
662 if ($info->horizontal) {
afdb1920 663 $hv = 'h';
9c5bc7a5 664 } else {
afdb1920
AG
665 $hv = 'v';
666 }
667
9c5bc7a5
AG
668 foreach ($presentation as $radio) {
669 foreach ($values as $val) {
670 if ($val == $index) {
671 $checked = 'checked="checked"';
672 break;
673 } else {
674 $checked = '';
675 }
6ee09cfe 676 }
677 $inputname = $item->typ . '_' . $item->id;
678 $inputid = $inputname.'_'.$index;
6ee09cfe 679 ?>
afdb1920
AG
680 <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
681 <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
9c5bc7a5
AG
682 <?php
683 echo '<input type="radio" '.
684 'name="'.$inputname.'[]" '.
685 'id="'.$inputid.'" '.
686 'value="'.$index.'" '.$checked.' />';
687 ?>
afdb1920
AG
688 </span>
689 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
9c5bc7a5
AG
690 <label for="<?php echo $inputid;?>">
691 <?php echo text_to_html($radio, true, false, false);?>&nbsp;
692 </label>
afdb1920
AG
693 </span>
694 </li>
6ee09cfe 695 <?php
6ee09cfe 696 $index++;
697 }
698 }
699
9c5bc7a5 700 private function print_item_check($presentation, $item, $value, $info, $align) {
d4b1d58c 701
6ee09cfe 702 if (is_array($value)) {
703 $values = $value;
9c5bc7a5 704 } else {
6ee09cfe 705 $values = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
706 }
39790bd8 707
9c5bc7a5 708 if ($info->horizontal) {
afdb1920 709 $hv = 'h';
9c5bc7a5 710 } else {
afdb1920
AG
711 $hv = 'v';
712 }
d4b1d58c 713
6ee09cfe 714 $index = 1;
715 $checked = '';
9c5bc7a5
AG
716 foreach ($presentation as $check) {
717 foreach ($values as $val) {
718 if ($val == $index) {
6ee09cfe 719 $checked = 'checked="checked"';
720 break;
9c5bc7a5 721 } else {
6ee09cfe 722 $checked = '';
723 }
724 }
725 $inputname = $item->typ. '_' . $item->id;
726 $inputid = $item->typ. '_' . $item->id.'_'.$index;
6ee09cfe 727 ?>
afdb1920
AG
728 <li class="feedback_item_check_<?php echo $hv.'_'.$align;?>">
729 <span class="feedback_item_check_<?php echo $hv.'_'.$align;?>">
9c5bc7a5
AG
730 <?php
731 echo '<input type="checkbox" '.
732 'name="'.$inputname.'[]" '.
733 'id="'.$inputid.'" '.
734 'value="'.$index.'" '.$checked.' />';
735 ?>
afdb1920
AG
736 </span>
737 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
9c5bc7a5
AG
738 <label for="<?php echo $inputid;?>">
739 <?php echo text_to_html($check, true, false, false);?>&nbsp;
740 </label>
afdb1920
AG
741 </span>
742 </li>
6ee09cfe 743 <?php
6ee09cfe 744 $index++;
745 }
746 }
d4b1d58c 747
9c5bc7a5
AG
748 private function print_item_dropdown($presentation, $item, $value, $info, $align) {
749 if (is_array($value)) {
750 $values = $value;
751 } else {
752 $values = array($value);
753 }
754
755 if ($info->horizontal) {
afdb1920 756 $hv = 'h';
9c5bc7a5 757 } else {
afdb1920 758 $hv = 'v';
6ee09cfe 759 }
39790bd8 760
6ee09cfe 761 ?>
afdb1920 762 <li class="feedback_item_select_<?php echo $hv.'_'.$align;?>">
3211569a 763 <select name="<?php echo $item->typ .'_' . $item->id;?>[]" size="1">
afdb1920
AG
764 <option value="0">&nbsp;</option>
765 <?php
766 $index = 1;
9c5bc7a5
AG
767 $selected = '';
768 foreach ($presentation as $dropdown) {
769 foreach ($values as $val) {
770 if ($val == $index) {
771 $selected = 'selected="selected"';
772 break;
773 } else {
774 $selected = '';
775 }
afdb1920
AG
776 }
777 ?>
9c5bc7a5
AG
778 <option value="<?php echo $index;?>" <?php echo $selected;?>>
779 <?php echo text_to_html($dropdown, true, false, false);?>
780 </option>
afdb1920
AG
781 <?php
782 $index++;
783 }
784 ?>
785 </select>
786 </li>
6ee09cfe 787 <?php
788 }
39790bd8 789
9c5bc7a5 790 public function set_ignoreempty($item, $ignoreempty=true) {
79473294 791 $item->options = str_replace(FEEDBACK_MULTICHOICE_IGNOREEMPTY, '', $item->options);
9c5bc7a5 792 if ($ignoreempty) {
79473294
AG
793 $item->options .= FEEDBACK_MULTICHOICE_IGNOREEMPTY;
794 }
795 }
39790bd8 796
9c5bc7a5
AG
797 public function ignoreempty($item) {
798 if (strstr($item->options, FEEDBACK_MULTICHOICE_IGNOREEMPTY)) {
79473294
AG
799 return true;
800 }
801 return false;
802 }
39790bd8 803
9c5bc7a5 804 public function set_hidenoselect($item, $hidenoselect=true) {
79473294 805 $item->options = str_replace(FEEDBACK_MULTICHOICE_HIDENOSELECT, '', $item->options);
9c5bc7a5 806 if ($hidenoselect) {
79473294
AG
807 $item->options .= FEEDBACK_MULTICHOICE_HIDENOSELECT;
808 }
809 }
39790bd8 810
9c5bc7a5
AG
811 public function hidenoselect($item) {
812 if (strstr($item->options, FEEDBACK_MULTICHOICE_HIDENOSELECT)) {
79473294
AG
813 return true;
814 }
815 return false;
816 }
39790bd8 817
9c5bc7a5
AG
818 public function can_switch_require() {
819 return true;
820 }
821
822 public function value_type() {
823 return PARAM_INT;
824 }
39790bd8 825
9c5bc7a5 826 public function value_is_array() {
6cc1599e
AG
827 return true;
828 }
e7fc2ff4
AG
829
830 public function clean_input_value($value) {
831 return clean_param_array($value, $this->value_type());
832 }
6ee09cfe 833}