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_MULTICHOICE_TYPE_SEP', '>>>>>');
21 define('FEEDBACK_MULTICHOICE_LINE_SEP', '|');
22 define('FEEDBACK_MULTICHOICE_ADJUST_SEP', '<<<<<');
23 define('FEEDBACK_MULTICHOICE_IGNOREEMPTY', 'i');
24 define('FEEDBACK_MULTICHOICE_HIDENOSELECT', 'h');
26 class feedback_item_multichoice extends feedback_item_base {
27 protected $type = "multichoice";
29 public function build_editform($item, $feedback, $cm) {
31 require_once('multichoice_form.php');
33 //get the lastposition number of the feedback_items
34 $position = $item->position;
35 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
36 if ($position == -1) {
37 $i_formselect_last = $lastposition + 1;
38 $i_formselect_value = $lastposition + 1;
39 $item->position = $lastposition + 1;
41 $i_formselect_last = $lastposition;
42 $i_formselect_value = $item->position;
44 //the elements for position dropdownlist
45 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
47 $item->presentation = empty($item->presentation) ? '' : $item->presentation;
48 $info = $this->get_info($item);
50 $item->ignoreempty = $this->ignoreempty($item);
51 $item->hidenoselect = $this->hidenoselect($item);
53 //all items for dependitem
54 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
55 $commonparams = array('cmid'=>$cm->id,
56 'id'=>isset($item->id) ? $item->id : null,
58 'items'=>$feedbackitems,
59 'feedback'=>$feedback->id);
62 $customdata = array('item' => $item,
63 'common' => $commonparams,
64 'positionlist' => $positionlist,
65 'position' => $position,
68 $this->item_form = new feedback_multichoice_form('edit_item.php', $customdata);
71 public function save_item() {
74 if (!$item = $this->item_form->get_data()) {
78 if (isset($item->clone_item) AND $item->clone_item) {
79 $item->id = ''; //to clone this item
83 $this->set_ignoreempty($item, $item->ignoreempty);
84 $this->set_hidenoselect($item, $item->hidenoselect);
86 $item->hasvalue = $this->get_hasvalue();
88 $item->id = $DB->insert_record('feedback_item', $item);
90 $DB->update_record('feedback_item', $item);
93 return $DB->get_record('feedback_item', array('id'=>$item->id));
97 //gets an array with three values(typ, name, XXX)
98 //XXX is an object with answertext, answercount and quotient
101 * Helper function for collected data, both for analysis page and export to excel
103 * @param stdClass $item the db-object from feedback_item
104 * @param int $groupid
105 * @param int $courseid
108 protected function get_analysed($item, $groupid = false, $courseid = false) {
109 $info = $this->get_info($item);
111 $analysed_item = array();
112 $analysed_item[] = $item->typ;
113 $analysed_item[] = $item->name;
115 //get the possible answers
117 $answers = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
118 if (!is_array($answers)) {
123 $values = feedback_get_group_values($item, $groupid, $courseid, $this->ignoreempty($item));
128 //get answertext, answercount and quotient for each answer
129 $analysed_answer = array();
130 if ($info->subtype == 'c') {
131 $sizeofanswers = count($answers);
132 for ($i = 1; $i <= $sizeofanswers; $i++) {
133 $ans = new stdClass();
134 $ans->answertext = $answers[$i-1];
135 $ans->answercount = 0;
136 foreach ($values as $value) {
137 //ist die Antwort gleich dem index der Antworten + 1?
138 $vallist = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value->value);
139 foreach ($vallist as $val) {
145 $ans->quotient = $ans->answercount / count($values);
146 $analysed_answer[] = $ans;
149 $sizeofanswers = count($answers);
150 for ($i = 1; $i <= $sizeofanswers; $i++) {
151 $ans = new stdClass();
152 $ans->answertext = $answers[$i-1];
153 $ans->answercount = 0;
154 foreach ($values as $value) {
155 //ist die Antwort gleich dem index der Antworten + 1?
156 if ($value->value == $i) {
160 $ans->quotient = $ans->answercount / count($values);
161 $analysed_answer[] = $ans;
164 $analysed_item[] = $analysed_answer;
165 return $analysed_item;
168 public function get_printval($item, $value) {
169 $info = $this->get_info($item);
173 if (!isset($value->value)) {
177 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
179 if ($info->subtype == 'c') {
180 $vallist = array_values(explode (FEEDBACK_MULTICHOICE_LINE_SEP, $value->value));
181 $sizeofvallist = count($vallist);
182 $sizeofpresentation = count($presentation);
183 for ($i = 0; $i < $sizeofvallist; $i++) {
184 for ($k = 0; $k < $sizeofpresentation; $k++) {
185 if ($vallist[$i] == ($k + 1)) {//Die Werte beginnen bei 1, das Array aber mit 0
186 $printval .= trim($presentation[$k]) . chr(10);
193 foreach ($presentation as $pres) {
194 if ($value->value == $index) {
204 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
207 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
208 if ($analysed_item) {
209 $itemname = $analysed_item[1];
210 echo '<tr><th colspan="2" align="left">';
212 if (strval($item->label) !== '') {
213 echo '('. format_string($item->label).') ';
218 $analysed_vals = $analysed_item[2];
220 foreach ($analysed_vals as $val) {
221 $intvalue = $pixnr % 10;
222 $pix = $OUTPUT->pix_url('multichoice/' . $intvalue, 'feedback');
223 $pixspacer = $OUTPUT->pix_url('spacer');
225 $pixwidth = max(2, intval($val->quotient * FEEDBACK_MAX_PIX_LENGTH));
226 $pixwidthspacer = FEEDBACK_MAX_PIX_LENGTH + 1 - $pixwidth;
227 $quotient = format_float($val->quotient * 100, 2);
229 if ($val->quotient > 0) {
230 $str_quotient = ' ('. $quotient . ' %)';
233 echo '<td align="left" valign="top">
234 - '.format_text(trim($val->answertext), FORMAT_HTML, array('noclean' => true, 'para' => false)).':
236 <td align="left" style="width:'.FEEDBACK_MAX_PIX_LENGTH.';">
237 <img class="feedback_bar_image" alt="'.$intvalue.'" src="'.$pix.'" width="'.$pixwidth.'" />'.
238 '<img class="feedback_bar_image" alt="" src="'.$pixspacer.'" width="'.$pixwidthspacer.'" />
239 '.$val->answercount.$str_quotient.'
246 public function excelprint_item(&$worksheet, $row_offset,
248 $groupid, $courseid = false) {
250 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
252 $data = $analysed_item[2];
255 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
256 $worksheet->write_string($row_offset, 1, $analysed_item[1], $xls_formats->head2);
257 if (is_array($data)) {
258 $sizeofdata = count($data);
259 for ($i = 0; $i < $sizeofdata; $i++) {
260 $analysed_data = $data[$i];
262 $worksheet->write_string($row_offset,
264 trim($analysed_data->answertext),
265 $xls_formats->head2);
267 $worksheet->write_number($row_offset + 1,
269 $analysed_data->answercount,
270 $xls_formats->default);
272 $worksheet->write_number($row_offset + 2,
274 $analysed_data->quotient,
275 $xls_formats->procent);
283 * Options for the multichoice element
284 * @param stdClass $item
287 protected function get_options($item) {
288 $info = $this->get_info($item);
289 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
291 foreach ($presentation as $idx => $optiontext) {
292 $options[$idx + 1] = format_text($optiontext, FORMAT_HTML, array('noclean' => true, 'para' => false));
294 if ($info->subtype === 'r' && !$this->hidenoselect($item)) {
295 $options = array(0 => get_string('not_selected', 'feedback')) + $options;
302 * Adds an input element to the complete form
304 * This element has many options - it can be displayed as group or radio elements,
305 * group of checkboxes or a dropdown list.
307 * @param stdClass $item
308 * @param mod_feedback_complete_form $form
310 public function complete_form_element($item, $form) {
311 $info = $this->get_info($item);
312 $name = $this->get_display_name($item);
313 $class = 'multichoice-' . $info->subtype;
314 $inputname = $item->typ . '_' . $item->id;
315 $options = $this->get_options($item);
316 $separator = !empty($info->horizontal) ? ' ' : '<br>';
317 $tmpvalue = $form->get_item_value($item);
319 if ($info->subtype === 'd' || ($info->subtype === 'r' && $form->is_frozen())) {
320 // Display as a dropdown in the complete form or a single value in the response view.
321 $element = $form->add_form_element($item,
322 ['select', $inputname.'[0]', $name, array(0 => '') + $options, array('class' => $class)],
324 $form->set_element_default($inputname.'[0]', $tmpvalue);
325 } else if ($info->subtype === 'c' && $form->is_frozen()) {
326 // Display list of checkbox values in the response view.
328 foreach (explode(FEEDBACK_MULTICHOICE_LINE_SEP, $form->get_item_value($item)) as $v) {
329 $objs[] = ['static', $inputname."[$v]", '', isset($options[$v]) ? $options[$v] : ''];
331 $element = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class);
333 // Display group or radio or checkbox elements.
334 $class .= ' multichoice-' . ($info->horizontal ? 'horizontal' : 'vertical');
336 if ($info->subtype === 'c') {
338 $objs[] = ['hidden', $inputname.'[0]', 0];
339 foreach ($options as $idx => $label) {
340 $objs[] = ['advcheckbox', $inputname.'['.$idx.']', '', $label, null, array(0, $idx)];
342 $element = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class);
344 foreach (explode(FEEDBACK_MULTICHOICE_LINE_SEP, $tmpvalue) as $v) {
345 $form->set_element_default($inputname.'['.$v.']', $v);
350 foreach ($options as $idx => $label) {
351 $objs[] = ['radio', $inputname.'[0]', '', $label, $idx];
353 $element = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class);
354 $form->set_element_default($inputname.'[0]', $tmpvalue);
358 // Process 'required' rule.
359 if ($item->required) {
360 $elementname = $element->getName();
361 $form->add_validation_rule(function($values, $files) use ($elementname, $item) {
362 $inputname = $item->typ . '_' . $item->id;
363 return empty($values[$inputname]) || !array_filter($values[$inputname]) ?
364 array($elementname => get_string('required')) : true;
370 * Prepares value that user put in the form for storing in DB
371 * @param array $value
374 public function create_value($value) {
375 $value = array_unique(array_filter($value));
376 return join(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
380 * Compares the dbvalue with the dependvalue
382 * @param stdClass $item
383 * @param string $dbvalue is the value input by user in the format as it is stored in the db
384 * @param string $dependvalue is the value that it needs to be compared against
386 public function compare_value($item, $dbvalue, $dependvalue) {
388 if (is_array($dbvalue)) {
389 $dbvalues = $dbvalue;
391 $dbvalues = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $dbvalue);
394 $info = $this->get_info($item);
395 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
397 foreach ($presentation as $pres) {
398 foreach ($dbvalues as $dbval) {
399 if ($dbval == $index AND trim($pres) == $dependvalue) {
408 public function get_info($item) {
409 $presentation = empty($item->presentation) ? '' : $item->presentation;
411 $info = new stdClass();
412 //check the subtype of the multichoice
413 //it can be check(c), radio(r) or dropdown(d)
415 $info->presentation = '';
416 $info->horizontal = false;
418 $parts = explode(FEEDBACK_MULTICHOICE_TYPE_SEP, $item->presentation);
419 @list($info->subtype, $info->presentation) = $parts;
420 if (!isset($info->subtype)) {
421 $info->subtype = 'r';
424 if ($info->subtype != 'd') {
425 $parts = explode(FEEDBACK_MULTICHOICE_ADJUST_SEP, $info->presentation);
426 @list($info->presentation, $info->horizontal) = $parts;
427 if (isset($info->horizontal) AND $info->horizontal == 1) {
428 $info->horizontal = true;
430 $info->horizontal = false;
436 public function set_ignoreempty($item, $ignoreempty=true) {
437 $item->options = str_replace(FEEDBACK_MULTICHOICE_IGNOREEMPTY, '', $item->options);
439 $item->options .= FEEDBACK_MULTICHOICE_IGNOREEMPTY;
443 public function ignoreempty($item) {
444 if (strstr($item->options, FEEDBACK_MULTICHOICE_IGNOREEMPTY)) {
450 public function set_hidenoselect($item, $hidenoselect=true) {
451 $item->options = str_replace(FEEDBACK_MULTICHOICE_HIDENOSELECT, '', $item->options);
453 $item->options .= FEEDBACK_MULTICHOICE_HIDENOSELECT;
457 public function hidenoselect($item) {
458 if (strstr($item->options, FEEDBACK_MULTICHOICE_HIDENOSELECT)) {