2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 defined('MOODLE_INTERNAL') OR die('not allowed');
18 require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
20 define('FEEDBACK_RADIORATED_ADJUST_SEP', '<<<<<');
22 define('FEEDBACK_MULTICHOICERATED_MAXCOUNT', 10); //count of possible items
23 define('FEEDBACK_MULTICHOICERATED_VALUE_SEP', '####');
24 define('FEEDBACK_MULTICHOICERATED_VALUE_SEP2', '/');
25 define('FEEDBACK_MULTICHOICERATED_TYPE_SEP', '>>>>>');
26 define('FEEDBACK_MULTICHOICERATED_LINE_SEP', '|');
27 define('FEEDBACK_MULTICHOICERATED_ADJUST_SEP', '<<<<<');
28 define('FEEDBACK_MULTICHOICERATED_IGNOREEMPTY', 'i');
29 define('FEEDBACK_MULTICHOICERATED_HIDENOSELECT', 'h');
31 class feedback_item_multichoicerated extends feedback_item_base {
32 protected $type = "multichoicerated";
33 private $commonparams;
37 public function init() {
41 public function build_editform($item, $feedback, $cm) {
43 require_once('multichoicerated_form.php');
45 //get the lastposition number of the feedback_items
46 $position = $item->position;
47 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
48 if ($position == -1) {
49 $i_formselect_last = $lastposition + 1;
50 $i_formselect_value = $lastposition + 1;
51 $item->position = $lastposition + 1;
53 $i_formselect_last = $lastposition;
54 $i_formselect_value = $item->position;
56 //the elements for position dropdownlist
57 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
59 $item->presentation = empty($item->presentation) ? '' : $item->presentation;
60 $info = $this->get_info($item);
62 $item->ignoreempty = $this->ignoreempty($item);
63 $item->hidenoselect = $this->hidenoselect($item);
65 //all items for dependitem
66 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
67 $commonparams = array('cmid'=>$cm->id,
68 'id'=>isset($item->id) ? $item->id : null,
70 'items'=>$feedbackitems,
71 'feedback'=>$feedback->id);
74 $customdata = array('item' => $item,
75 'common' => $commonparams,
76 'positionlist' => $positionlist,
77 'position' => $position,
80 $this->item_form = new feedback_multichoicerated_form('edit_item.php', $customdata);
83 //this function only can used after the call of build_editform()
84 public function show_editform() {
85 $this->item_form->display();
88 public function is_cancelled() {
89 return $this->item_form->is_cancelled();
92 public function get_data() {
93 if ($this->item = $this->item_form->get_data()) {
99 public function save_item() {
102 if (!$item = $this->item_form->get_data()) {
106 if (isset($item->clone_item) AND $item->clone_item) {
107 $item->id = ''; //to clone this item
111 $this->set_ignoreempty($item, $item->ignoreempty);
112 $this->set_hidenoselect($item, $item->hidenoselect);
114 $item->hasvalue = $this->get_hasvalue();
116 $item->id = $DB->insert_record('feedback_item', $item);
118 $DB->update_record('feedback_item', $item);
121 return $DB->get_record('feedback_item', array('id'=>$item->id));
125 //gets an array with three values(typ, name, XXX)
126 //XXX is an object with answertext, answercount and quotient
127 public function get_analysed($item, $groupid = false, $courseid = false) {
128 $analysed_item = array();
129 $analysed_item[] = $item->typ;
130 $analysed_item[] = $item->name;
132 //die moeglichen Antworten extrahieren
133 $info = $this->get_info($item);
135 $lines = explode (FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation);
136 if (!is_array($lines)) {
141 $values = feedback_get_group_values($item, $groupid, $courseid, $this->ignoreempty($item));
145 //schleife ueber den Werten und ueber die Antwortmoeglichkeiten
147 $analysed_answer = array();
148 $sizeoflines = count($lines);
149 for ($i = 1; $i <= $sizeoflines; $i++) {
150 $item_values = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $lines[$i-1]);
151 $ans = new stdClass();
152 $ans->answertext = $item_values[1];
155 foreach ($values as $value) {
156 //ist die Antwort gleich dem index der Antworten + 1?
157 if ($value->value == $i) {
158 $avg += $item_values[0]; //erst alle Werte aufsummieren
162 $ans->answercount = $anscount;
163 $ans->avg = doubleval($avg) / doubleval(count($values));
164 $ans->value = $item_values[0];
165 $ans->quotient = $ans->answercount / count($values);
166 $analysed_answer[] = $ans;
168 $analysed_item[] = $analysed_answer;
169 return $analysed_item;
172 public function get_printval($item, $value) {
175 if (!isset($value->value)) {
179 $info = $this->get_info($item);
181 $presentation = explode (FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation);
183 foreach ($presentation as $pres) {
184 if ($value->value == $index) {
185 $item_label = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $pres);
186 $printval = $item_label[1];
194 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
196 $sep_dec = get_string('separator_decimal', 'feedback');
197 if (substr($sep_dec, 0, 2) == '[[') {
198 $sep_dec = FEEDBACK_DECIMAL;
201 $sep_thous = get_string('separator_thousand', 'feedback');
202 if (substr($sep_thous, 0, 2) == '[[') {
203 $sep_thous = FEEDBACK_THOUSAND;
206 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
207 if ($analysed_item) {
208 echo '<tr><th colspan="2" align="left">';
209 echo $itemnr.' ('.$item->label.') '.$analysed_item[1];
211 $analysed_vals = $analysed_item[2];
214 foreach ($analysed_vals as $val) {
215 $intvalue = $pixnr % 10;
216 $pix = $OUTPUT->pix_url('multichoice/' . $intvalue, 'feedback');
218 $pixwidth = intval($val->quotient * FEEDBACK_MAX_PIX_LENGTH);
221 $quotient = number_format(($val->quotient * 100), 2, $sep_dec, $sep_thous);
223 echo '<td align="left" valign="top">';
224 echo '- '.trim($val->answertext).' ('.$val->value.'):</td>';
225 echo '<td align="left" style="width: '.FEEDBACK_MAX_PIX_LENGTH.'">';
226 echo '<img alt="'.$intvalue.'" src="'.$pix.'" height="5" width="'.$pixwidth.'" />';
227 echo $val->answercount;
228 if ($val->quotient > 0) {
229 echo ' ('.$quotient.' %)';
235 $avg = number_format(($avg), 2, $sep_dec, $sep_thous);
236 echo '<tr><td align="left" colspan="2"><b>';
237 echo get_string('average', 'feedback').': '.$avg.'</b>';
242 public function excelprint_item(&$worksheet, $row_offset,
244 $groupid, $courseid = false) {
246 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
248 $data = $analysed_item[2];
251 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
252 $worksheet->write_string($row_offset, 1, $analysed_item[1], $xls_formats->head2);
253 if (is_array($data)) {
255 $sizeofdata = count($data);
256 for ($i = 0; $i < $sizeofdata; $i++) {
257 $analysed_data = $data[$i];
259 $worksheet->write_string($row_offset,
261 trim($analysed_data->answertext).' ('.$analysed_data->value.')',
262 $xls_formats->value_bold);
264 $worksheet->write_number($row_offset + 1,
266 $analysed_data->answercount,
267 $xls_formats->default);
269 $avg += $analysed_data->avg;
271 //mittelwert anzeigen
272 $worksheet->write_string($row_offset,
274 get_string('average', 'feedback'),
275 $xls_formats->value_bold);
277 $worksheet->write_number($row_offset + 1,
280 $xls_formats->value_bold);
287 * print the item at the edit-page of feedback
290 * @param object $item
293 public function print_item_preview($item) {
296 $align = right_to_left() ? 'right' : 'left';
297 $info = $this->get_info($item);
298 $str_required_mark = '<span class="feedback_required_mark">*</span>';
300 $lines = explode (FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation);
301 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
302 //print the question and label
303 echo '<div class="feedback_item_label_'.$align.'">';
304 echo '('.$item->label.') ';
305 echo format_text($item->name.$requiredmark, true, false, false);
306 if ($item->dependitem) {
307 if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
308 echo ' <span class="feedback_depend">';
309 echo '('.$dependitem->label.'->'.$item->dependvalue.')';
315 //print the presentation
316 echo '<div class="feedback_item_presentation_'.$align.'">';
317 switch($info->subtype) {
319 $this->print_item_radio($item, false, $info, $align, true, $lines);
322 $this->print_item_dropdown($item, false, $info, $align, true, $lines);
329 * print the item at the complete-page of feedback
332 * @param object $item
333 * @param string $value
334 * @param bool $highlightrequire
337 public function print_item_complete($item, $value = '', $highlightrequire = false) {
339 $align = right_to_left() ? 'right' : 'left';
340 $info = $this->get_info($item);
341 $str_required_mark = '<span class="feedback_required_mark">*</span>';
343 $lines = explode (FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation);
344 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
345 if ($highlightrequire AND $item->required AND intval($value) <= 0) {
346 $highlight = ' missingrequire';
351 //print the question and label
352 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
353 echo format_text($item->name.$requiredmark, true, false, false);
356 //print the presentation
357 echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
358 switch($info->subtype) {
360 $this->print_item_radio($item, $value, $info, $align, false, $lines);
363 $this->print_item_dropdown($item, $value, $info, $align, false, $lines);
370 * print the item at the complete-page of feedback
373 * @param object $item
374 * @param string $value
377 public function print_item_show_value($item, $value = '') {
379 $align = right_to_left() ? 'right' : 'left';
380 $info = $this->get_info($item);
382 $lines = explode (FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation);
383 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
385 //print the question and label
386 echo '<div class="feedback_item_label_'.$align.'">';
387 echo '('.$item->label.') ';
388 echo format_text($item->name . $requiredmark, true, false, false);
391 //print the presentation
392 echo '<div class="feedback_item_presentation_'.$align.'">';
394 foreach ($lines as $line) {
395 if ($value == $index) {
396 $item_value = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $line);
397 echo $OUTPUT->box_start('generalbox boxalign'.$align);
398 echo text_to_html($item_value[1], true, false, false);
399 echo $OUTPUT->box_end();
407 public function check_value($value, $item) {
408 if ((!isset($value) OR $value == '' OR $value == 0) AND $item->required != 1) {
411 if (intval($value) > 0) {
417 public function create_value($data) {
422 //compares the dbvalue with the dependvalue
423 //dbvalue is the number of one selection
424 //dependvalue is the presentation of one selection
425 public function compare_value($item, $dbvalue, $dependvalue) {
427 if (is_array($dbvalue)) {
428 $dbvalues = $dbvalue;
430 $dbvalues = explode(FEEDBACK_MULTICHOICERATED_LINE_SEP, $dbvalue);
433 $info = $this->get_info($item);
434 $presentation = explode (FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation);
436 foreach ($presentation as $pres) {
437 $presvalues = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $pres);
439 foreach ($dbvalues as $dbval) {
440 if ($dbval == $index AND trim($presvalues[1]) == $dependvalue) {
449 public function get_presentation($data) {
450 $present = $this->prepare_presentation_values_save(trim($data->itemvalues),
451 FEEDBACK_MULTICHOICERATED_VALUE_SEP2,
452 FEEDBACK_MULTICHOICERATED_VALUE_SEP);
453 if (!isset($data->subtype)) {
456 $subtype = substr($data->subtype, 0, 1);
458 if (isset($data->horizontal) AND $data->horizontal == 1 AND $subtype != 'd') {
459 $present .= FEEDBACK_MULTICHOICERATED_ADJUST_SEP.'1';
461 return $subtype.FEEDBACK_MULTICHOICERATED_TYPE_SEP.$present;
464 public function get_hasvalue() {
468 private function get_info($item) {
469 $presentation = empty($item->presentation) ? '' : $item->presentation;
471 $info = new stdClass();
472 //check the subtype of the multichoice
473 //it can be check(c), radio(r) or dropdown(d)
475 $info->presentation = '';
476 $info->horizontal = false;
478 $parts = explode(FEEDBACK_MULTICHOICERATED_TYPE_SEP, $item->presentation);
479 @list($info->subtype, $info->presentation) = $parts;
481 if (!isset($info->subtype)) {
482 $info->subtype = 'r';
485 if ($info->subtype != 'd') {
486 $parts = explode(FEEDBACK_MULTICHOICERATED_ADJUST_SEP, $info->presentation);
487 @list($info->presentation, $info->horizontal) = $parts;
489 if (isset($info->horizontal) AND $info->horizontal == 1) {
490 $info->horizontal = true;
492 $info->horizontal = false;
496 $info->values = $this->prepare_presentation_values_print($info->presentation,
497 FEEDBACK_MULTICHOICERATED_VALUE_SEP,
498 FEEDBACK_MULTICHOICERATED_VALUE_SEP2);
502 private function print_item_radio($item, $value, $info, $align, $showrating, $lines) {
506 if ($info->horizontal) {
512 if (!$this->hidenoselect($item)) {
514 <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
515 <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
517 echo '<input type="radio" '.
518 'name="'.$item->typ.'_'.$item->id.'" '.
519 'id="'.$item->typ.'_'.$item->id.'_xxx" '.
520 'value="" checked="checked" />';
523 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
524 <label for="<?php echo $item->typ . '_' . $item->id.'_xxx';?>">
525 <?php print_string('not_selected', 'feedback');?>
531 foreach ($lines as $line) {
532 if ($value == $index) {
533 $checked = 'checked="checked"';
537 $radio_value = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $line);
538 $inputname = $item->typ . '_' . $item->id;
539 $inputid = $inputname.'_'.$index;
541 <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
542 <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
544 echo '<input type="radio" '.
545 'name="'.$inputname.'" '.
546 'id="'.$inputid.'" '.
547 'value="'.$index.'" '.$checked.' />';
550 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
551 <label for="<?php echo $inputid;?>">
554 $str_rating_value = '('.$radio_value[0].') '.$radio_value[1];
555 echo text_to_html($str_rating_value, true, false, false);
557 echo text_to_html($radio_value[1], true, false, false);
569 private function print_item_dropdown($item, $value, $info, $align, $showrating, $lines) {
570 if ($info->horizontal) {
577 <li class="feedback_item_select_<?php echo $hv.'_'.$align;?>">
578 <select name="<?php echo $item->typ.'_'.$item->id;?>">
579 <option value="0"> </option>
583 foreach ($lines as $line) {
584 if ($value == $index) {
585 $selected = 'selected="selected"';
589 $dropdown_value = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $line);
591 echo '<option value="'.$index.'" '.$selected.'>';
592 echo clean_text('('.$dropdown_value[0].') '.$dropdown_value[1]);
595 echo '<option value="'.$index.'" '.$selected.'>';
596 echo clean_text($dropdown_value[1]);
608 public function prepare_presentation_values($linesep1,
614 $lines = explode($linesep1, $valuestring);
616 foreach ($lines as $line) {
619 if (strpos($line, $valuesep1) === false) {
623 @list($value, $text) = explode($valuesep1, $line, 2);
626 $value = intval($value);
627 $newlines[] = $value.$valuesep2.$text;
629 $newlines = implode($linesep2, $newlines);
633 public function prepare_presentation_values_print($valuestring, $valuesep1, $valuesep2) {
634 return $this->prepare_presentation_values(FEEDBACK_MULTICHOICERATED_LINE_SEP,
641 public function prepare_presentation_values_save($valuestring, $valuesep1, $valuesep2) {
642 return $this->prepare_presentation_values("\n",
643 FEEDBACK_MULTICHOICERATED_LINE_SEP,
649 public function set_ignoreempty($item, $ignoreempty=true) {
650 $item->options = str_replace(FEEDBACK_MULTICHOICERATED_IGNOREEMPTY, '', $item->options);
652 $item->options .= FEEDBACK_MULTICHOICERATED_IGNOREEMPTY;
656 public function ignoreempty($item) {
657 if (strstr($item->options, FEEDBACK_MULTICHOICERATED_IGNOREEMPTY)) {
663 public function set_hidenoselect($item, $hidenoselect=true) {
664 $item->options = str_replace(FEEDBACK_MULTICHOICERATED_HIDENOSELECT, '', $item->options);
666 $item->options .= FEEDBACK_MULTICHOICERATED_HIDENOSELECT;
670 public function hidenoselect($item) {
671 if (strstr($item->options, FEEDBACK_MULTICHOICERATED_HIDENOSELECT)) {
677 public function can_switch_require() {
681 public function value_type() {
685 public function clean_input_value($value) {
686 return clean_param($value, $this->value_type());