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)) return $printval;
166 // @list($presentation) = explode(FEEDBACK_RADIO_ADJUST_SEP, $item->presentation); //remove the adjustment-info
168 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
170 if($info->subtype == 'c') {
171 $vallist = array_values(explode (FEEDBACK_MULTICHOICE_LINE_SEP, $value->value));
172 $sizeofvallist = sizeof($vallist);
173 for($i = 0; $i < $sizeofvallist; $i++) {
174 $sizeofpresentation = sizeof($presentation);
175 for($k = 0; $k < $sizeofpresentation; $k++) {
176 if($vallist[$i] == ($k + 1)) {//Die Werte beginnen bei 1, das Array aber mit 0
177 $printval .= trim($presentation[$k]) . chr(10);
184 foreach($presentation as $pres){
185 if($value->value == $index){
195 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 $analysedItem = $this->get_analysed($item, $groupid, $courseid);
209 $itemname = $analysedItem[1];
210 echo '<tr><th colspan="2" align="left">'. $itemnr . ' ('. $item->label .') ' . $itemname .'</th></tr>';
211 $analysedVals = $analysedItem[2];
213 foreach($analysedVals as $val) {
214 if( function_exists("bcmod")) {
215 $intvalue = bcmod($pixnr, 10);
219 $pix = "pics/$intvalue.gif";
221 $pixwidth = intval($val->quotient * FEEDBACK_MAX_PIX_LENGTH);
222 $quotient = number_format(($val->quotient * 100), 2, $sep_dec, $sep_thous);
223 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>';
229 function excelprint_item(&$worksheet, $rowOffset, $xlsFormats, $item, $groupid, $courseid = false) {
230 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
232 $data = $analysed_item[2];
234 // $worksheet->setFormat("<l><f><ro2><vo><c:green>");
236 $worksheet->write_string($rowOffset, 0, $item->label, $xlsFormats->head2_green);
237 $worksheet->write_string($rowOffset, 1, $analysed_item[1], $xlsFormats->head2_green);
238 if(is_array($data)) {
239 $sizeofdata = sizeof($data);
240 for($i = 0; $i < $sizeofdata; $i++) {
243 // $worksheet->setFormat("<l><f><ro2><vo><c:blue>");
244 $worksheet->write_string($rowOffset, $i + 2, trim($aData->answertext), $xlsFormats->value_blue);
246 // $worksheet->setFormat("<l><vo>");
247 $worksheet->write_number($rowOffset + 1, $i + 2, $aData->answercount, $xlsFormats->default);
248 // $worksheet->setFormat("<l><f><vo><pr>");
249 $worksheet->write_number($rowOffset + 2, $i + 2, $aData->quotient, $xlsFormats->procent);
257 * print the item at the edit-page of feedback
260 * @param object $item
263 function print_item_preview($item) {
265 $info = $this->get_info($item);
266 $align = right_to_left() ? 'right' : 'left';
268 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
271 //test if required and no value is set so we have to mark this item
272 //we have to differ check and the other subtypes
273 $requiredmark = ($item->required == 1) ? '<span class="feedback_required_mark">*</span>' : '';
275 //print the question and label
276 echo '<div class="feedback_item_label_'.$align.'">';
277 echo '('.$item->label.') ';
278 echo format_text($item->name.$requiredmark, true, false, false);
279 if($item->dependitem) {
280 if($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
281 echo ' <span class="feedback_depend">('.$dependitem->label.'->'.$item->dependvalue.')</span>';
286 //print the presentation
287 echo '<div class="feedback_item_presentation_'.$align.'">';
291 if($info->horizontal) {
297 if($info->subtype == 'r' AND !$this->hidenoselect($item)) {
298 //print the "not_selected" item on radiobuttons
300 <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
301 <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
302 <input type="radio" name="<?php echo $item->typ . '_' . $item->id ;?>" id="<?php echo $item->typ . '_' . $item->id.'_xxx';?>" value="" checked="checked" />
304 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
305 <label for="<?php echo $item->typ . '_' . $item->id.'_xxx';?>"><?php print_string('not_selected', 'feedback');?> </label>
311 switch($info->subtype) {
313 $this->print_item_radio($presentation, $item, false, $info, $align);
316 $this->print_item_check($presentation, $item, false, $info, $align);
319 $this->print_item_dropdown($presentation, $item, false, $info, $align);
327 * print the item at the complete-page of feedback
330 * @param object $item
331 * @param string $value
332 * @param bool $highlightrequire
335 function print_item_complete($item, $value = '', $highlightrequire = false) {
337 $info = $this->get_info($item);
338 $align = right_to_left() ? 'right' : 'left';
340 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
343 //test if required and no value is set so we have to mark this item
344 //we have to differ check and the other subtypes
345 if($info->subtype == 'c') {
346 if (is_array($value)) {
349 $values = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
351 if($highlightrequire AND $item->required AND $values[0] == '') {
352 $highlight = ' missingrequire';
356 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
358 if($highlightrequire AND $item->required AND intval($value) <= 0) {
359 $highlight = ' missingrequire';
363 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
366 //print the question and label
367 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
368 echo format_text($item->name.$requiredmark, true, false, false);
371 //print the presentation
372 echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
375 if($info->horizontal) {
380 //print the "not_selected" item on radiobuttons
381 if($info->subtype == 'r' AND !$this->hidenoselect($item)) {
383 <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
384 <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
385 <input type="radio" name="<?php echo $item->typ.'_'.$item->id ;?>" id="<?php echo $item->typ . '_' . $item->id.'_xxx';?>" value="" <?php echo $value ? '' : 'checked="checked"';?> />
387 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
388 <label for="<?php echo $item->typ.'_'.$item->id.'_xxx';?>"><?php print_string('not_selected', 'feedback');?> </label>
394 switch($info->subtype) {
396 $this->print_item_radio($presentation, $item, $value, $info, $align);
399 $this->print_item_check($presentation, $item, $value, $info, $align);
402 $this->print_item_dropdown($presentation, $item, $value, $info, $align);
410 * print the item at the complete-page of feedback
413 * @param object $item
414 * @param string $value
417 function print_item_show_value($item, $value = '') {
419 $info = $this->get_info($item);
420 $align = right_to_left() ? 'right' : 'left';
422 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
425 //test if required and no value is set so we have to mark this item
426 //we have to differ check and the other subtypes
427 if($info->subtype == 'c') {
428 if (is_array($value)) {
431 $values = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
433 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
435 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
438 //print the question and label
439 echo '<div class="feedback_item_label_'.$align.'">';
440 echo '('.$item->label.') ';
441 echo format_text($item->name . $requiredmark, true, false, false);
444 //print the presentation
445 echo '<div class="feedback_item_presentation_'.$align.'">';
447 if($info->subtype == 'c') {
448 echo $OUTPUT->box_start('generalbox boxalign'.$align);
449 foreach($presentation as $pres){
450 foreach($values as $val) {
452 echo '<div class="feedback_item_multianswer">';
453 echo text_to_html($pres, true, false, false);
460 echo $OUTPUT->box_end();
462 foreach($presentation as $pres){
463 if($value == $index){
464 echo $OUTPUT->box_start('generalbox boxalign'.$align);
465 echo text_to_html($pres, true, false, false);
466 echo $OUTPUT->box_end();
475 function check_value($value, $item) {
476 $info = $this->get_info($item);
478 if($info->subtype == 'c') {
479 if((!isset($value) OR !is_array($value) OR $value[0] == '' OR $value[0] == 0) AND $item->required != 1){
487 //if the item is not required, so the check is true if no value is given
488 if((!isset($value) OR $value == '' OR $value == 0) AND $item->required != 1) return true;
489 if(intval($value) > 0)return true;
494 function create_value($data) {
496 return trim($this->item_arrayToString($vallist));
499 //compares the dbvalue with the dependvalue
500 //dbvalue is the number of one selection
501 //dependvalue is the presentation of one selection
502 function compare_value($item, $dbvalue, $dependvalue) {
504 if (is_array($dbvalue)) {
505 $dbvalues = $dbvalue;
507 $dbvalues = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $dbvalue);
510 $info = $this->get_info($item);
511 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
513 foreach($presentation as $pres) {
514 foreach($dbvalues as $dbval) {
515 if($dbval == $index AND trim($pres) == $dependvalue) {
524 function get_presentation($data) {
525 $present = str_replace("\n", FEEDBACK_MULTICHOICE_LINE_SEP, trim($data->itemvalues));
526 if(!isset($data->subtype)) {
529 $subtype = substr($data->subtype, 0, 1);
531 if(isset($data->horizontal) AND $data->horizontal == 1 AND $subtype != 'd') {
532 $present .= FEEDBACK_MULTICHOICE_ADJUST_SEP.'1';
534 return $subtype.FEEDBACK_MULTICHOICE_TYPE_SEP.$present;
537 function get_hasvalue() {
541 function get_info($item) {
542 $presentation = empty($item->presentation) ? '' : $item->presentation;
544 $info = new stdClass();
545 //check the subtype of the multichoice
546 //it can be check(c), radio(r) or dropdown(d)
548 $info->presentation = '';
549 $info->horizontal = false;
551 @list($info->subtype, $info->presentation) = explode(FEEDBACK_MULTICHOICE_TYPE_SEP, $item->presentation);
552 if(!isset($info->subtype)) {
553 $info->subtype = 'r';
556 if($info->subtype != 'd') {
557 @list($info->presentation, $info->horizontal) = explode(FEEDBACK_MULTICHOICE_ADJUST_SEP, $info->presentation);
558 if(isset($info->horizontal) AND $info->horizontal == 1) {
559 $info->horizontal = true;
561 $info->horizontal = false;
567 function item_arrayToString($value) {
568 if(!is_array($value)) {
572 $arrvals = array_values($value);
573 $arrvals = clean_param($arrvals, PARAM_INT); //prevent sql-injection
574 $retval = $arrvals[0];
575 $sizeofarrvals = sizeof($arrvals);
576 for($i = 1; $i < $sizeofarrvals; $i++) {
577 $retval .= FEEDBACK_MULTICHOICE_LINE_SEP.$arrvals[$i];
582 function print_item_radio($presentation, $item, $value, $info, $align) {
586 if($info->horizontal) {
592 foreach($presentation as $radio){
593 if($value == $index){
594 $checked = 'checked="checked"';
598 $inputname = $item->typ . '_' . $item->id;
599 $inputid = $inputname.'_'.$index;
601 <li class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
602 <span class="feedback_item_radio_<?php echo $hv.'_'.$align;?>">
603 <input type="radio" name="<?php echo $inputname;?>" id="<?php echo $inputid;?>" value="<?php echo $index;?>" <?php echo $checked;?> />
605 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
606 <label for="<?php echo $inputid;?>"><?php echo text_to_html($radio, true, false, false);?> </label>
614 function print_item_check($presentation, $item, $value, $info, $align) {
616 if (is_array($value)) {
619 $values = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
622 if($info->horizontal) {
630 foreach($presentation as $check){
631 foreach($values as $val) {
633 $checked = 'checked="checked"';
639 $inputname = $item->typ. '_' . $item->id;
640 $inputid = $item->typ. '_' . $item->id.'_'.$index;
642 <li class="feedback_item_check_<?php echo $hv.'_'.$align;?>">
643 <span class="feedback_item_check_<?php echo $hv.'_'.$align;?>">
644 <input type="checkbox" name="<?php echo $inputname;?>[]" id="<?php echo $inputid;?>" value="<?php echo $index;?>" <?php echo $checked;?> />
646 <span class="feedback_item_radiolabel_<?php echo $hv.'_'.$align;?>">
647 <label for="<?php echo $inputid;?>"><?php echo text_to_html($check, true, false, false);?> </label>
655 function print_item_dropdown($presentation, $item, $value, $info, $align) {
656 if($info->horizontal) {
663 <li class="feedback_item_select_<?php echo $hv.'_'.$align;?>">
664 <select name="<?php echo $item->typ .'_' . $item->id;?>" size="1">
665 <option value="0"> </option>
669 foreach($presentation as $dropdown){
670 if($value == $index){
671 $selected = 'selected="selected"';
676 <option value="<?php echo $index;?>" <?php echo $selected;?>><?php echo text_to_html($dropdown, true, false, false);?></option>
686 function set_ignoreempty($item, $ignoreempty=true) {
687 $item->options = str_replace(FEEDBACK_MULTICHOICE_IGNOREEMPTY, '', $item->options);
689 $item->options .= FEEDBACK_MULTICHOICE_IGNOREEMPTY;
693 function ignoreempty($item) {
694 if(strstr($item->options, FEEDBACK_MULTICHOICE_IGNOREEMPTY)) {
700 function set_hidenoselect($item, $hidenoselect=true) {
701 $item->options = str_replace(FEEDBACK_MULTICHOICE_HIDENOSELECT, '', $item->options);
703 $item->options .= FEEDBACK_MULTICHOICE_HIDENOSELECT;
707 function hidenoselect($item) {
708 if(strstr($item->options, FEEDBACK_MULTICHOICE_HIDENOSELECT)) {
715 function can_switch_require() {