Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 and 403]
1 <?php 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 17 defined('MOODLE_INTERNAL') OR die('not allowed'); 18 require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php'); 19 20 define('FEEDBACK_RADIORATED_ADJUST_SEP', '<<<<<'); 21 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'); 30 31 class feedback_item_multichoicerated extends feedback_item_base { 32 protected $type = "multichoicerated"; 33 34 public function build_editform($item, $feedback, $cm) { 35 global $DB, $CFG; 36 require_once ('multichoicerated_form.php'); 37 38 //get the lastposition number of the feedback_items 39 $position = $item->position; 40 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id)); 41 if ($position == -1) { 42 $i_formselect_last = $lastposition + 1; 43 $i_formselect_value = $lastposition + 1; 44 $item->position = $lastposition + 1; 45 } else { 46 $i_formselect_last = $lastposition; 47 $i_formselect_value = $item->position; 48 } 49 //the elements for position dropdownlist 50 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true); 51 52 $item->presentation = empty($item->presentation) ? '' : $item->presentation; 53 $info = $this->get_info($item); 54 55 $item->ignoreempty = $this->ignoreempty($item); 56 $item->hidenoselect = $this->hidenoselect($item); 57 58 //all items for dependitem 59 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item); 60 $commonparams = array('cmid'=>$cm->id, 61 'id'=>isset($item->id) ? $item->id : null, 62 'typ'=>$item->typ, 63 'items'=>$feedbackitems, 64 'feedback'=>$feedback->id); 65 66 //build the form 67 $customdata = array('item' => $item, 68 'common' => $commonparams, 69 'positionlist' => $positionlist, 70 'position' => $position, 71 'info' => $info); 72 73 $this->item_form = new feedback_multichoicerated_form('edit_item.php', $customdata); 74 } 75 76 public function save_item() { 77 global $DB; 78 79 if (!$this->get_data()) { 80 return false; 81 } 82 $item = $this->item; 83 84 if (isset($item->clone_item) AND $item->clone_item) { 85 $item->id = ''; //to clone this item 86 $item->position++; 87 } 88 89 $this->set_ignoreempty($item, $item->ignoreempty); 90 $this->set_hidenoselect($item, $item->hidenoselect); 91 92 $item->hasvalue = $this->get_hasvalue(); 93 if (!$item->id) { 94 $item->id = $DB->insert_record('feedback_item', $item); 95 } else { 96 $DB->update_record('feedback_item', $item); 97 } 98 99 return $DB->get_record('feedback_item', array('id'=>$item->id)); 100 } 101 102 103 /** 104 * Helper function for collected data, both for analysis page and export to excel 105 * 106 * @param stdClass $item the db-object from feedback_item 107 * @param int $groupid 108 * @param int $courseid 109 * @return array|null 110 */ 111 protected function get_analysed($item, $groupid = false, $courseid = false) { 112 $analysed_item = array(); 113 $analysed_item[] = $item->typ; 114 $analysed_item[] = $item->name; 115 116 //die moeglichen Antworten extrahieren 117 $info = $this->get_info($item); 118 $lines = null; 119 $lines = explode (FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation); 120 if (!is_array($lines)) { 121 return null; 122 } 123 124 //die Werte holen 125 $values = feedback_get_group_values($item, $groupid, $courseid, $this->ignoreempty($item)); 126 if (!$values) { 127 return null; 128 } 129 //schleife ueber den Werten und ueber die Antwortmoeglichkeiten 130 131 $analysed_answer = array(); 132 $sizeoflines = count($lines); 133 for ($i = 1; $i <= $sizeoflines; $i++) { 134 $item_values = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $lines[$i-1]); 135 $ans = new stdClass(); 136 $ans->answertext = $item_values[1]; 137 $avg = 0.0; 138 $anscount = 0; 139 foreach ($values as $value) { 140 //ist die Antwort gleich dem index der Antworten + 1? 141 if ($value->value == $i) { 142 $avg += $item_values[0]; //erst alle Werte aufsummieren 143 $anscount++; 144 } 145 } 146 $ans->answercount = $anscount; 147 $ans->avg = doubleval($avg) / doubleval(count($values)); 148 $ans->value = $item_values[0]; 149 $ans->quotient = $ans->answercount / count($values); 150 $analysed_answer[] = $ans; 151 } 152 $analysed_item[] = $analysed_answer; 153 return $analysed_item; 154 } 155 156 public function get_printval($item, $value) { 157 $printval = ''; 158 159 if (!isset($value->value)) { 160 return $printval; 161 } 162 163 $info = $this->get_info($item); 164 165 $presentation = explode (FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation); 166 $index = 1; 167 foreach ($presentation as $pres) { 168 if ($value->value == $index) { 169 $item_label = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $pres); 170 $printval = format_string($item_label[1]); 171 break; 172 } 173 $index++; 174 } 175 return $printval; 176 } 177 178 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) { 179 global $OUTPUT; 180 $analysed_item = $this->get_analysed($item, $groupid, $courseid); 181 if ($analysed_item) { 182 echo "<table class=\"analysis itemtype_{$item->typ}\">"; 183 echo '<tr><th class="text-left">'; 184 echo $itemnr . ' '; 185 if (strval($item->label) !== '') { 186 echo '('. format_string($item->label).') '; 187 } 188 echo format_string($analysed_item[1]); 189 echo '</th></tr>'; 190 $analysed_vals = $analysed_item[2]; 191 $avg = 0.0; 192 $count = 0; 193 $data = []; 194 foreach ($analysed_vals as $val) { 195 $avg += $val->avg; 196 $quotient = format_float($val->quotient * 100, 2); 197 $answertext = '('.$val->value.') ' . format_text(trim($val->answertext), FORMAT_HTML, 198 array('noclean' => true, 'para' => false)); 199 200 if ($val->quotient > 0) { 201 $strquotient = ' ('.$quotient.' %)'; 202 } else { 203 $strquotient = ''; 204 } 205 206 $data['labels'][$count] = $answertext; 207 $data['series'][$count] = $val->answercount; 208 $data['series_labels'][$count] = $val->answercount . $strquotient; 209 $count++; 210 } 211 $chart = new \core\chart_bar(); 212 $chart->set_horizontal(true); 213 $series = new \core\chart_series(format_string(get_string("responses", "feedback")), $data['series']); 214 $series->set_labels($data['series_labels']); 215 $chart->add_series($series); 216 $chart->set_labels($data['labels']); 217 echo '<tr><td>'. $OUTPUT->render($chart) . '</td></tr>'; 218 $avg = format_float($avg, 2); 219 echo '<tr><td class="text-left"><b>'; 220 echo get_string('average', 'feedback').': '.$avg.'</b>'; 221 echo '</td></tr>'; 222 echo '</table>'; 223 } 224 } 225 226 public function excelprint_item(&$worksheet, $row_offset, 227 $xls_formats, $item, 228 $groupid, $courseid = false) { 229 230 $analysed_item = $this->get_analysed($item, $groupid, $courseid); 231 if (!$analysed_item) { 232 return $row_offset; 233 } 234 235 $data = $analysed_item[2]; 236 237 //write the item 238 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2); 239 $worksheet->write_string($row_offset, 1, $analysed_item[1], $xls_formats->head2); 240 if (is_array($data)) { 241 $avg = 0.0; 242 $sizeofdata = count($data); 243 for ($i = 0; $i < $sizeofdata; $i++) { 244 $analysed_data = $data[$i]; 245 246 $worksheet->write_string($row_offset, 247 $i + 2, 248 trim($analysed_data->answertext).' ('.$analysed_data->value.')', 249 $xls_formats->value_bold); 250 251 $worksheet->write_number($row_offset + 1, 252 $i + 2, 253 $analysed_data->answercount, 254 $xls_formats->default); 255 256 $avg += $analysed_data->avg; 257 } 258 //mittelwert anzeigen 259 $worksheet->write_string($row_offset, 260 count($data) + 2, 261 get_string('average', 'feedback'), 262 $xls_formats->value_bold); 263 264 $worksheet->write_number($row_offset + 1, 265 count($data) + 2, 266 $avg, 267 $xls_formats->value_bold); 268 } 269 $row_offset +=2; 270 return $row_offset; 271 } 272 273 /** 274 * Options for the multichoice element 275 * @param stdClass $item 276 * @return array 277 */ 278 protected function get_options($item) { 279 $info = $this->get_info($item); 280 $lines = explode(FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation); 281 $options = array(); 282 foreach ($lines as $idx => $line) { 283 list($weight, $optiontext) = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $line); 284 $a = new stdclass(); 285 $a->weight = $weight; 286 $a->name = format_text($optiontext, FORMAT_HTML, array('noclean' => true, 'para' => false)); 287 $options[$idx + 1] = get_string('multichoiceoption', 'feedback', $a); 288 } 289 if ($info->subtype === 'r' && !$this->hidenoselect($item)) { 290 $options = array(0 => get_string('not_selected', 'feedback')) + $options; 291 } 292 293 return $options; 294 } 295 296 /** 297 * Adds an input element to the complete form 298 * 299 * @param stdClass $item 300 * @param mod_feedback_complete_form $form 301 */ 302 public function complete_form_element($item, $form) { 303 $info = $this->get_info($item); 304 $name = $this->get_display_name($item); 305 $class = 'multichoicerated-' . $info->subtype; 306 $inputname = $item->typ . '_' . $item->id; 307 $options = $this->get_options($item); 308 if ($info->subtype === 'd' || $form->is_frozen()) { 309 $el = $form->add_form_element($item, 310 ['select', $inputname, $name, array('' => '') + $options, array('class' => $class)]); 311 } else { 312 $objs = array(); 313 if (!array_key_exists(0, $options)) { 314 // Always add '0' as hidden element, otherwise form submit data may not have this element. 315 $objs[] = ['hidden', $inputname]; 316 } 317 foreach ($options as $idx => $label) { 318 $objs[] = ['radio', $inputname, '', $label, $idx]; 319 } 320 // Span to hold the element id. The id is used for drag and drop reordering. 321 $objs[] = ['static', '', '', html_writer::span('', '', ['id' => 'feedback_item_' . $item->id])]; 322 $separator = $info->horizontal ? ' ' : '<br>'; 323 $class .= ' multichoicerated-' . ($info->horizontal ? 'horizontal' : 'vertical'); 324 $el = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class); 325 $form->set_element_type($inputname, PARAM_INT); 326 327 // Set previously input values. 328 $form->set_element_default($inputname, $form->get_item_value($item)); 329 330 // Process "required" rule. 331 if ($item->required) { 332 $form->add_validation_rule(function($values, $files) use ($item) { 333 $inputname = $item->typ . '_' . $item->id; 334 return empty($values[$inputname]) ? array('group_' . $inputname => get_string('required')) : true; 335 }); 336 } 337 } 338 } 339 340 /** 341 * Compares the dbvalue with the dependvalue 342 * 343 * @param stdClass $item 344 * @param string $dbvalue is the value input by user in the format as it is stored in the db 345 * @param string $dependvalue is the value that it needs to be compared against 346 */ 347 public function compare_value($item, $dbvalue, $dependvalue) { 348 349 if (is_array($dbvalue)) { 350 $dbvalues = $dbvalue; 351 } else { 352 $dbvalues = explode(FEEDBACK_MULTICHOICERATED_LINE_SEP, $dbvalue); 353 } 354 355 $info = $this->get_info($item); 356 $presentation = explode (FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation); 357 $index = 1; 358 foreach ($presentation as $pres) { 359 $presvalues = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $pres); 360 361 foreach ($dbvalues as $dbval) { 362 if ($dbval == $index AND trim($presvalues[1]) == $dependvalue) { 363 return true; 364 } 365 } 366 $index++; 367 } 368 return false; 369 } 370 371 public function get_info($item) { 372 $presentation = empty($item->presentation) ? '' : $item->presentation; 373 374 $info = new stdClass(); 375 //check the subtype of the multichoice 376 //it can be check(c), radio(r) or dropdown(d) 377 $info->subtype = ''; 378 $info->presentation = ''; 379 $info->horizontal = false; 380 381 $parts = explode(FEEDBACK_MULTICHOICERATED_TYPE_SEP, $item->presentation); 382 $info->subtype = $parts[0]; 383 if (count($parts) > 1) { 384 $info->presentation = $parts[1]; 385 } 386 387 if (!isset($info->subtype)) { 388 $info->subtype = 'r'; 389 } 390 391 if ($info->subtype != 'd') { 392 $parts = explode(FEEDBACK_MULTICHOICERATED_ADJUST_SEP, $info->presentation); 393 $info->presentation = $parts[0]; 394 if (count($parts) > 1) { 395 $info->horizontal = $parts[1]; 396 } 397 398 if (isset($info->horizontal) AND $info->horizontal == 1) { 399 $info->horizontal = true; 400 } else { 401 $info->horizontal = false; 402 } 403 } 404 405 $info->values = $this->prepare_presentation_values_print($info->presentation, 406 FEEDBACK_MULTICHOICERATED_VALUE_SEP, 407 FEEDBACK_MULTICHOICERATED_VALUE_SEP2); 408 return $info; 409 } 410 411 public function prepare_presentation_values($linesep1, 412 $linesep2, 413 $valuestring, 414 $valuesep1, 415 $valuesep2) { 416 417 $lines = explode($linesep1, $valuestring); 418 $newlines = array(); 419 foreach ($lines as $line) { 420 $value = ''; 421 $text = ''; 422 if (strpos($line, $valuesep1) === false) { 423 $value = 0; 424 $text = $line; 425 } else { 426 @list($value, $text) = explode($valuesep1, $line, 2); 427 } 428 429 $value = intval($value); 430 $newlines[] = $value.$valuesep2.$text; 431 } 432 $newlines = implode($linesep2, $newlines); 433 return $newlines; 434 } 435 436 public function prepare_presentation_values_print($valuestring, $valuesep1, $valuesep2) { 437 $valuestring = str_replace(array("\n","\r"), "", $valuestring); 438 return $this->prepare_presentation_values(FEEDBACK_MULTICHOICERATED_LINE_SEP, 439 "\n", 440 $valuestring, 441 $valuesep1, 442 $valuesep2); 443 } 444 445 public function prepare_presentation_values_save($valuestring, $valuesep1, $valuesep2) { 446 $valuestring = str_replace("\r", "\n", $valuestring); 447 $valuestring = str_replace("\n\n", "\n", $valuestring); 448 return $this->prepare_presentation_values("\n", 449 FEEDBACK_MULTICHOICERATED_LINE_SEP, 450 $valuestring, 451 $valuesep1, 452 $valuesep2); 453 } 454 455 public function set_ignoreempty($item, $ignoreempty=true) { 456 $item->options = str_replace(FEEDBACK_MULTICHOICERATED_IGNOREEMPTY, '', $item->options); 457 if ($ignoreempty) { 458 $item->options .= FEEDBACK_MULTICHOICERATED_IGNOREEMPTY; 459 } 460 } 461 462 public function ignoreempty($item) { 463 if (strstr($item->options, FEEDBACK_MULTICHOICERATED_IGNOREEMPTY)) { 464 return true; 465 } 466 return false; 467 } 468 469 public function set_hidenoselect($item, $hidenoselect=true) { 470 $item->options = str_replace(FEEDBACK_MULTICHOICERATED_HIDENOSELECT, '', $item->options); 471 if ($hidenoselect) { 472 $item->options .= FEEDBACK_MULTICHOICERATED_HIDENOSELECT; 473 } 474 } 475 476 public function hidenoselect($item) { 477 if (strstr($item->options, FEEDBACK_MULTICHOICERATED_HIDENOSELECT)) { 478 return true; 479 } 480 return false; 481 } 482 483 /** 484 * Return the analysis data ready for external functions. 485 * 486 * @param stdClass $item the item (question) information 487 * @param int $groupid the group id to filter data (optional) 488 * @param int $courseid the course id (optional) 489 * @return array an array of data with non scalar types json encoded 490 * @since Moodle 3.3 491 */ 492 public function get_analysed_for_external($item, $groupid = false, $courseid = false) { 493 494 $externaldata = array(); 495 $data = $this->get_analysed($item, $groupid, $courseid); 496 497 if ($data && !empty($data[2]) && is_array($data[2])) { 498 foreach ($data[2] as $d) { 499 $externaldata[] = json_encode($d); 500 } 501 } 502 return $externaldata; 503 } 504 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body