2 defined('MOODLE_INTERNAL') OR die('not allowed');
3 require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
5 define('FEEDBACK_MULTICHOICE_TYPE_SEP', '>>>>>');
6 define('FEEDBACK_MULTICHOICE_LINE_SEP', '|');
7 define('FEEDBACK_MULTICHOICE_ADJUST_SEP', '<<<<<');
8 define('FEEDBACK_MULTICHOICE_IGNOREEMPTY', 'i');
9 define('FEEDBACK_MULTICHOICE_HIDENOSELECT', 'h');
11 class feedback_item_multichoice extends feedback_item_base {
12 var $type = "multichoice";
21 function build_editform($item, $feedback, $cm) {
23 require_once('multichoice_form.php');
25 //get the lastposition number of the feedback_items
26 $position = $item->position;
27 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
29 $i_formselect_last = $lastposition + 1;
30 $i_formselect_value = $lastposition + 1;
31 $item->position = $lastposition + 1;
33 $i_formselect_last = $lastposition;
34 $i_formselect_value = $item->position;
36 //the elements for position dropdownlist
37 $positionlist = array_slice(range(0,$i_formselect_last),1,$i_formselect_last,true);
39 $item->presentation = empty($item->presentation) ? '' : $item->presentation;
40 $info = $this->get_info($item);
42 $item->ignoreempty = $this->ignoreempty($item);
43 $item->hidenoselect = $this->hidenoselect($item);
45 //all items for dependitem
46 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
47 $commonparams = array('cmid'=>$cm->id,
48 'id'=>isset($item->id) ? $item->id : NULL,
50 'items'=>$feedbackitems,
51 'feedback'=>$feedback->id);
54 $this->item_form = new feedback_multichoice_form('edit_item.php', array('item'=>$item, 'common'=>$commonparams, 'positionlist'=>$positionlist, 'position'=>$position, 'info'=>$info));
57 //this function only can used after the call of build_editform()
58 function show_editform() {
59 $this->item_form->display();
62 function is_cancelled() {
63 return $this->item_form->is_cancelled();
67 if($this->item = $this->item_form->get_data()) {
73 function save_item() {
76 if(!$item = $this->item_form->get_data()) {
80 if(isset($item->clone_item) AND $item->clone_item) {
81 $item->id = ''; //to clone this item
85 $this->set_ignoreempty($item, $item->ignoreempty);
86 $this->set_hidenoselect($item, $item->hidenoselect);
88 $item->hasvalue = $this->get_hasvalue();
90 $item->id = $DB->insert_record('feedback_item', $item);
92 $DB->update_record('feedback_item', $item);
95 return $DB->get_record('feedback_item', array('id'=>$item->id));
99 //liefert ein eindimensionales Array mit drei Werten(typ, name, XXX)
100 //XXX ist ein eindimensionales Array (anzahl der Antworten bei Typ Radio) Jedes Element ist eine Struktur (answertext, answercount)
101 function get_analysed($item, $groupid = false, $courseid = false) {
102 $info = $this->get_info($item);
104 $analysedItem = array();
105 $analysedItem[] = $item->typ;
106 $analysedItem[] = $item->name;
107 //die moeglichen Antworten extrahieren
109 // $presentation = '';
110 // @list($presentation) = explode(FEEDBACK_RADIO_ADJUST_SEP, $item->presentation); //remove the adjustment-info
112 $answers = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
113 if(!is_array($answers)) return null;
116 $values = feedback_get_group_values($item, $groupid, $courseid, $this->ignoreempty($item));
117 if(!$values) return null;
118 //schleife ueber den Werten und ueber die Antwortmoeglichkeiten
120 $analysedAnswer = array();
121 if($info->subtype == 'c') {
122 $sizeofanswers = sizeof($answers);
123 for ($i = 1; $i <= $sizeofanswers; $i++) {
125 $ans->answertext = $answers[$i-1];
126 $ans->answercount = 0;
127 foreach ($values as $value) {
128 //ist die Antwort gleich dem index der Antworten + 1?
129 $vallist = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value->value);
130 foreach ($vallist as $val) {
136 $ans->quotient = $ans->answercount / sizeof($values);
137 $analysedAnswer[] = $ans;
140 $sizeofanswers = sizeof($answers);
141 for ($i = 1; $i <= $sizeofanswers; $i++) {
143 $ans->answertext = $answers[$i-1];
144 $ans->answercount = 0;
145 foreach ($values as $value) {
146 //ist die Antwort gleich dem index der Antworten + 1?
147 if ($value->value == $i) {
151 $ans->quotient = $ans->answercount / sizeof($values);
152 $analysedAnswer[] = $ans;
155 $analysedItem[] = $analysedAnswer;
156 return $analysedItem;
159 function get_printval($item, $value) {
160 $info = $this->get_info($item);
164 if (!isset($value->value)) {
168 // @list($presentation) = explode(FEEDBACK_RADIO_ADJUST_SEP, $item->presentation); //remove the adjustment-info
170 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
172 if ($info->subtype == 'c') {
173 $vallist = array_values(explode (FEEDBACK_MULTICHOICE_LINE_SEP, $value->value));
174 $sizeofvallist = sizeof($vallist);
175 $sizeofpresentation = sizeof($presentation);
176 for ($i = 0; $i < $sizeofvallist; $i++) {
177 for ($k = 0; $k < $sizeofpresentation; $k++) {
178 if ($vallist[$i] == ($k + 1)) {//Die Werte beginnen bei 1, das Array aber mit 0
179 $printval .= trim($presentation[$k]) . chr(10);
186 foreach($presentation as $pres){
187 if ($value->value == $index){
197 function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
198 $sep_dec = get_string('separator_decimal', 'feedback');
199 if(substr($sep_dec, 0, 2) == '[['){
200 $sep_dec = FEEDBACK_DECIMAL;
203 $sep_thous = get_string('separator_thousand', 'feedback');
204 if(substr($sep_thous, 0, 2) == '[['){
205 $sep_thous = FEEDBACK_THOUSAND;
208 $analysedItem = $this->get_analysed($item, $groupid, $courseid);
211 $itemname = $analysedItem[1];
212 echo '<tr><th colspan="2" align="left">'. $itemnr . ' ('. $item->label .') ' . $itemname .'</th></tr>';
213 $analysedVals = $analysedItem[2];
215 foreach($analysedVals as $val) {
216 if( function_exists("bcmod")) {
217 $intvalue = bcmod($pixnr, 10);
221 $pix = "pics/$intvalue.gif";
223 $pixwidth = intval($val->quotient * FEEDBACK_MAX_PIX_LENGTH);
224 $quotient = number_format(($val->quotient * 100), 2, $sep_dec, $sep_thous);
225 echo '<tr><td align="left" valign="top">- ' . trim($val->answertext) . ':</td><td align="left" style="width: '.FEEDBACK_MAX_PIX_LENGTH.'"><img alt="'.$intvalue.'" src="'.$pix.'" height="5" width="'.$pixwidth.'" /> ' . $val->answercount . (($val->quotient > 0)?' ('. $quotient . ' %)':'').'</td></tr>';
231 function excelprint_item(&$worksheet, $rowOffset, $xlsFormats, $item, $groupid, $courseid = false) {
232 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
234 $data = $analysed_item[2];
236 // $worksheet->setFormat("<l><f><ro2><vo><c:green>");
238 $worksheet->write_string($rowOffset, 0, $item->label, $xlsFormats->head2_green);
239 $worksheet->write_string($rowOffset, 1, $analysed_item[1], $xlsFormats->head2_green);
240 if (is_array($data)) {
241 $sizeofdata = sizeof($data);
242 for ($i = 0; $i < $sizeofdata; $i++) {
245 // $worksheet->setFormat("<l><f><ro2><vo><c:blue>");
246 $worksheet->write_string($rowOffset, $i + 2, trim($aData->answertext), $xlsFormats->value_blue);
248 // $worksheet->setFormat("<l><vo>");
249 $worksheet->write_number($rowOffset + 1, $i + 2, $aData->answercount, $xlsFormats->default);
250 // $worksheet->setFormat("<l><f><vo><pr>");
251 $worksheet->write_number($rowOffset + 2, $i + 2, $aData->quotient, $xlsFormats->procent);
259 * print the item at the edit-page of feedback
262 * @param object $item
265 function print_item_preview($item) {
267 $info = $this->get_info($item);
268 $align = right_to_left() ? 'right' : 'left';
270 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
273 //test if required and no value is set so we have to mark this item
274 //we have to differ check and the other subtypes
275 $requiredmark = ($item->required == 1) ? '<span class="feedback_required_mark">*</span>' : '';
277 //print the question and label
278 echo '<div class="feedback_item_label_'.$align.'">';
279 echo '('.$item->label.') ';
280 echo format_text($item->name.$requiredmark, true, false, false);
281 if($item->dependitem) {
282 if($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
283 echo ' <span class="feedback_depend">('.$dependitem->label.'->'.$item->dependvalue.')</span>';
288 //print the presentation
289 echo '<div class="feedback_item_presentation_'.$align.'">';
293 if($info->horizontal) {
299 if($info->subtype == 'r' AND !$this->hidenoselect($item)) {
300 //print the "not_selected" item on radiobuttons
302 <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
303 <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
304 <input type="radio" name="<?php echo $item->typ . '_' . $item->id ;?>" id="<?php echo $item->typ . '_' . $item->id.'_xxx';?>" value="" checked="checked" />
306 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
307 <label for="<?php echo $item->typ . '_' . $item->id.'_xxx';?>"><?php print_string('not_selected', 'feedback');?> </label>
313 switch($info->subtype) {
315 $this->print_item_radio($presentation, $item, false, $info, $align);
318 $this->print_item_check($presentation, $item, false, $info, $align);
321 $this->print_item_dropdown($presentation, $item, false, $info, $align);
329 * print the item at the complete-page of feedback
332 * @param object $item
333 * @param string $value
334 * @param bool $highlightrequire
337 function print_item_complete($item, $value = '', $highlightrequire = false) {
339 $info = $this->get_info($item);
340 $align = right_to_left() ? 'right' : 'left';
342 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
345 //test if required and no value is set so we have to mark this item
346 //we have to differ check and the other subtypes
347 if($info->subtype == 'c') {
348 if (is_array($value)) {
351 $values = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
353 if($highlightrequire AND $item->required AND $values[0] == '') {
354 $highlight = ' missingrequire';
358 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
360 if($highlightrequire AND $item->required AND intval($value) <= 0) {
361 $highlight = ' missingrequire';
365 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
368 //print the question and label
369 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
370 echo format_text($item->name.$requiredmark, true, false, false);
373 //print the presentation
374 echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
377 if($info->horizontal) {
382 //print the "not_selected" item on radiobuttons
383 if($info->subtype == 'r' AND !$this->hidenoselect($item)) {
385 <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
386 <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
387 <input type="radio" name="<?php echo $item->typ.'_'.$item->id ;?>" id="<?php echo $item->typ . '_' . $item->id.'_xxx';?>" value="" <?php echo $value ? '' : 'checked="checked"';?> />
389 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
390 <label for="<?php echo $item->typ.'_'.$item->id.'_xxx';?>"><?php print_string('not_selected', 'feedback');?> </label>
396 switch($info->subtype) {
398 $this->print_item_radio($presentation, $item, $value, $info, $align);
401 $this->print_item_check($presentation, $item, $value, $info, $align);
404 $this->print_item_dropdown($presentation, $item, $value, $info, $align);
412 * print the item at the complete-page of feedback
415 * @param object $item
416 * @param string $value
419 function print_item_show_value($item, $value = '') {
421 $info = $this->get_info($item);
422 $align = right_to_left() ? 'right' : 'left';
424 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
427 //test if required and no value is set so we have to mark this item
428 //we have to differ check and the other subtypes
429 if($info->subtype == 'c') {
430 if (is_array($value)) {
433 $values = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
435 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
437 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
440 //print the question and label
441 echo '<div class="feedback_item_label_'.$align.'">';
442 echo '('.$item->label.') ';
443 echo format_text($item->name . $requiredmark, true, false, false);
446 //print the presentation
447 echo '<div class="feedback_item_presentation_'.$align.'">';
449 if($info->subtype == 'c') {
450 echo $OUTPUT->box_start('generalbox boxalign'.$align);
451 foreach($presentation as $pres){
452 foreach($values as $val) {
454 echo '<div class="feedback_item_multianswer">';
455 echo text_to_html($pres, true, false, false);
462 echo $OUTPUT->box_end();
464 foreach($presentation as $pres){
465 if($value == $index){
466 echo $OUTPUT->box_start('generalbox boxalign'.$align);
467 echo text_to_html($pres, true, false, false);
468 echo $OUTPUT->box_end();
477 function check_value($value, $item) {
478 $info = $this->get_info($item);
480 if($info->subtype == 'c') {
481 if((!isset($value) OR !is_array($value) OR $value[0] == '' OR $value[0] == 0) AND $item->required != 1){
489 //if the item is not required, so the check is true if no value is given
490 if((!isset($value) OR $value == '' OR $value == 0) AND $item->required != 1) return true;
491 if(intval($value) > 0)return true;
496 function create_value($data) {
498 return trim($this->item_arrayToString($vallist));
501 //compares the dbvalue with the dependvalue
502 //dbvalue is the number of one selection
503 //dependvalue is the presentation of one selection
504 function compare_value($item, $dbvalue, $dependvalue) {
506 if (is_array($dbvalue)) {
507 $dbvalues = $dbvalue;
509 $dbvalues = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $dbvalue);
512 $info = $this->get_info($item);
513 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
515 foreach($presentation as $pres) {
516 foreach($dbvalues as $dbval) {
517 if($dbval == $index AND trim($pres) == $dependvalue) {
526 function get_presentation($data) {
527 $present = str_replace("\n", FEEDBACK_MULTICHOICE_LINE_SEP, trim($data->itemvalues));
528 if(!isset($data->subtype)) {
531 $subtype = substr($data->subtype, 0, 1);
533 if(isset($data->horizontal) AND $data->horizontal == 1 AND $subtype != 'd') {
534 $present .= FEEDBACK_MULTICHOICE_ADJUST_SEP.'1';
536 return $subtype.FEEDBACK_MULTICHOICE_TYPE_SEP.$present;
539 function get_hasvalue() {
543 function get_info($item) {
544 $presentation = empty($item->presentation) ? '' : $item->presentation;
546 $info = new stdClass();
547 //check the subtype of the multichoice
548 //it can be check(c), radio(r) or dropdown(d)
550 $info->presentation = '';
551 $info->horizontal = false;
553 @list($info->subtype, $info->presentation) = explode(FEEDBACK_MULTICHOICE_TYPE_SEP, $item->presentation);
554 if(!isset($info->subtype)) {
555 $info->subtype = 'r';
558 if($info->subtype != 'd') {
559 @list($info->presentation, $info->horizontal) = explode(FEEDBACK_MULTICHOICE_ADJUST_SEP, $info->presentation);
560 if(isset($info->horizontal) AND $info->horizontal == 1) {
561 $info->horizontal = true;
563 $info->horizontal = false;
569 function item_arrayToString($value) {
570 if (!is_array($value)) {
574 $arrvals = array_values($value);
575 $arrvals = clean_param($arrvals, PARAM_INT); //prevent sql-injection
576 $retval = $arrvals[0];
577 $sizeofarrvals = sizeof($arrvals);
578 for ($i = 1; $i < $sizeofarrvals; $i++) {
579 $retval .= FEEDBACK_MULTICHOICE_LINE_SEP.$arrvals[$i];
584 function print_item_radio($presentation, $item, $value, $info, $align) {
588 if($info->horizontal) {
594 foreach($presentation as $radio){
595 if($value == $index){
596 $checked = 'checked="checked"';
600 $inputname = $item->typ . '_' . $item->id;
601 $inputid = $inputname.'_'.$index;
603 <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
604 <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
605 <input type="radio" name="<?php echo $inputname;?>" id="<?php echo $inputid;?>" value="<?php echo $index;?>" <?php echo $checked;?> />
607 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
608 <label for="<?php echo $inputid;?>"><?php echo text_to_html($radio, true, false, false);?> </label>
616 function print_item_check($presentation, $item, $value, $info, $align) {
618 if (is_array($value)) {
621 $values = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
624 if($info->horizontal) {
632 foreach($presentation as $check){
633 foreach($values as $val) {
635 $checked = 'checked="checked"';
641 $inputname = $item->typ. '_' . $item->id;
642 $inputid = $item->typ. '_' . $item->id.'_'.$index;
644 <li class="feedback_item_check_<?php echo $hv.'_'.$align;?>">
645 <span class="feedback_item_check_<?php echo $hv.'_'.$align;?>">
646 <input type="checkbox" name="<?php echo $inputname;?>[]" id="<?php echo $inputid;?>" value="<?php echo $index;?>" <?php echo $checked;?> />
648 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
649 <label for="<?php echo $inputid;?>"><?php echo text_to_html($check, true, false, false);?> </label>
657 function print_item_dropdown($presentation, $item, $value, $info, $align) {
658 if($info->horizontal) {
665 <li class="feedback_item_select_<?php echo $hv.'_'.$align;?>">
666 <select name="<?php echo $item->typ .'_' . $item->id;?>" size="1">
667 <option value="0"> </option>
671 foreach($presentation as $dropdown){
672 if($value == $index){
673 $selected = 'selected="selected"';
678 <option value="<?php echo $index;?>" <?php echo $selected;?>><?php echo text_to_html($dropdown, true, false, false);?></option>
688 function set_ignoreempty($item, $ignoreempty=true) {
689 $item->options = str_replace(FEEDBACK_MULTICHOICE_IGNOREEMPTY, '', $item->options);
691 $item->options .= FEEDBACK_MULTICHOICE_IGNOREEMPTY;
695 function ignoreempty($item) {
696 if(strstr($item->options, FEEDBACK_MULTICHOICE_IGNOREEMPTY)) {
702 function set_hidenoselect($item, $hidenoselect=true) {
703 $item->options = str_replace(FEEDBACK_MULTICHOICE_HIDENOSELECT, '', $item->options);
705 $item->options .= FEEDBACK_MULTICHOICE_HIDENOSELECT;
709 function hidenoselect($item) {
710 if(strstr($item->options, FEEDBACK_MULTICHOICE_HIDENOSELECT)) {
717 function can_switch_require() {