[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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_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'); 25 26 class feedback_item_multichoice extends feedback_item_base { 27 protected $type = "multichoice"; 28 29 public function build_editform($item, $feedback, $cm) { 30 global $DB, $CFG; 31 require_once ('multichoice_form.php'); 32 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; 40 } else { 41 $i_formselect_last = $lastposition; 42 $i_formselect_value = $item->position; 43 } 44 //the elements for position dropdownlist 45 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true); 46 47 $item->presentation = empty($item->presentation) ? '' : $item->presentation; 48 $info = $this->get_info($item); 49 50 $item->ignoreempty = $this->ignoreempty($item); 51 $item->hidenoselect = $this->hidenoselect($item); 52 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, 57 'typ'=>$item->typ, 58 'items'=>$feedbackitems, 59 'feedback'=>$feedback->id); 60 61 //build the form 62 $customdata = array('item' => $item, 63 'common' => $commonparams, 64 'positionlist' => $positionlist, 65 'position' => $position, 66 'info' => $info); 67 68 $this->item_form = new feedback_multichoice_form('edit_item.php', $customdata); 69 } 70 71 public function save_item() { 72 global $DB; 73 74 if (!$item = $this->item_form->get_data()) { 75 return false; 76 } 77 78 if (isset($item->clone_item) AND $item->clone_item) { 79 $item->id = ''; //to clone this item 80 $item->position++; 81 } 82 83 $this->set_ignoreempty($item, $item->ignoreempty); 84 $this->set_hidenoselect($item, $item->hidenoselect); 85 86 $item->hasvalue = $this->get_hasvalue(); 87 if (!$item->id) { 88 $item->id = $DB->insert_record('feedback_item', $item); 89 } else { 90 $DB->update_record('feedback_item', $item); 91 } 92 93 return $DB->get_record('feedback_item', array('id'=>$item->id)); 94 } 95 96 97 //gets an array with three values(typ, name, XXX) 98 //XXX is an object with answertext, answercount and quotient 99 100 /** 101 * Helper function for collected data, both for analysis page and export to excel 102 * 103 * @param stdClass $item the db-object from feedback_item 104 * @param int $groupid 105 * @param int $courseid 106 * @return array 107 */ 108 protected function get_analysed($item, $groupid = false, $courseid = false) { 109 $info = $this->get_info($item); 110 111 $analysed_item = array(); 112 $analysed_item[] = $item->typ; 113 $analysed_item[] = $item->name; 114 115 //get the possible answers 116 $answers = null; 117 $answers = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation); 118 if (!is_array($answers)) { 119 return null; 120 } 121 122 //get the values 123 $values = feedback_get_group_values($item, $groupid, $courseid, $this->ignoreempty($item)); 124 if (!$values) { 125 return null; 126 } 127 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) { 140 if ($val == $i) { 141 $ans->answercount++; 142 } 143 } 144 } 145 $ans->quotient = $ans->answercount / count($values); 146 $analysed_answer[] = $ans; 147 } 148 } else { 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) { 157 $ans->answercount++; 158 } 159 } 160 $ans->quotient = $ans->answercount / count($values); 161 $analysed_answer[] = $ans; 162 } 163 } 164 $analysed_item[] = $analysed_answer; 165 return $analysed_item; 166 } 167 168 public function get_printval($item, $value) { 169 $info = $this->get_info($item); 170 171 $printval = ''; 172 173 if (!isset($value->value)) { 174 return $printval; 175 } 176 177 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation); 178 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); 187 break; 188 } 189 } 190 } 191 } else { 192 $index = 1; 193 foreach ($presentation as $pres) { 194 if ($value->value == $index) { 195 $printval = $pres; 196 break; 197 } 198 $index++; 199 } 200 } 201 return $printval; 202 } 203 204 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) { 205 global $OUTPUT; 206 207 $analysed_item = $this->get_analysed($item, $groupid, $courseid); 208 if ($analysed_item) { 209 $itemname = $analysed_item[1]; 210 echo "<table class=\"analysis itemtype_{$item->typ}\">"; 211 echo '<tr><th colspan="2" align="left">'; 212 echo $itemnr . ' '; 213 if (strval($item->label) !== '') { 214 echo '('. format_string($item->label).') '; 215 } 216 echo $itemname; 217 echo '</th></tr>'; 218 echo "</table>"; 219 $analysed_vals = $analysed_item[2]; 220 $count = 0; 221 $data = []; 222 foreach ($analysed_vals as $val) { 223 $quotient = format_float($val->quotient * 100, 2); 224 $strquotient = ''; 225 if ($val->quotient > 0) { 226 $strquotient = ' ('. $quotient . ' %)'; 227 } 228 $answertext = format_text(trim($val->answertext), FORMAT_HTML, 229 array('noclean' => true, 'para' => false)); 230 231 $data['labels'][$count] = $answertext; 232 $data['series'][$count] = $val->answercount; 233 $data['series_labels'][$count] = $val->answercount . $strquotient; 234 $count++; 235 } 236 $chart = new \core\chart_bar(); 237 $chart->set_horizontal(true); 238 $series = new \core\chart_series(format_string(get_string("responses", "feedback")), $data['series']); 239 $series->set_labels($data['series_labels']); 240 $chart->add_series($series); 241 $chart->set_labels($data['labels']); 242 243 echo $OUTPUT->render($chart); 244 } 245 } 246 247 public function excelprint_item(&$worksheet, $row_offset, 248 $xls_formats, $item, 249 $groupid, $courseid = false) { 250 251 $analysed_item = $this->get_analysed($item, $groupid, $courseid); 252 253 $data = $analysed_item[2]; 254 255 //frage schreiben 256 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2); 257 $worksheet->write_string($row_offset, 1, $analysed_item[1], $xls_formats->head2); 258 if (is_array($data)) { 259 $sizeofdata = count($data); 260 for ($i = 0; $i < $sizeofdata; $i++) { 261 $analysed_data = $data[$i]; 262 263 $worksheet->write_string($row_offset, 264 $i + 2, 265 trim($analysed_data->answertext), 266 $xls_formats->head2); 267 268 $worksheet->write_number($row_offset + 1, 269 $i + 2, 270 $analysed_data->answercount, 271 $xls_formats->default); 272 273 $worksheet->write_number($row_offset + 2, 274 $i + 2, 275 $analysed_data->quotient, 276 $xls_formats->procent); 277 } 278 } 279 $row_offset += 3; 280 return $row_offset; 281 } 282 283 /** 284 * Options for the multichoice element 285 * @param stdClass $item 286 * @return array 287 */ 288 protected function get_options($item) { 289 $info = $this->get_info($item); 290 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation); 291 $options = array(); 292 foreach ($presentation as $idx => $optiontext) { 293 $options[$idx + 1] = format_text($optiontext, FORMAT_HTML, array('noclean' => true, 'para' => false)); 294 } 295 if ($info->subtype === 'r' && !$this->hidenoselect($item)) { 296 $options = array(0 => get_string('not_selected', 'feedback')) + $options; 297 } 298 299 return $options; 300 } 301 302 /** 303 * Adds an input element to the complete form 304 * 305 * This element has many options - it can be displayed as group or radio elements, 306 * group of checkboxes or a dropdown list. 307 * 308 * @param stdClass $item 309 * @param mod_feedback_complete_form $form 310 */ 311 public function complete_form_element($item, $form) { 312 $info = $this->get_info($item); 313 $name = $this->get_display_name($item); 314 $class = 'multichoice-' . $info->subtype; 315 $inputname = $item->typ . '_' . $item->id; 316 $options = $this->get_options($item); 317 $separator = !empty($info->horizontal) ? ' ' : '<br>'; 318 $tmpvalue = $form->get_item_value($item); 319 320 if ($info->subtype === 'd' || ($info->subtype === 'r' && $form->is_frozen())) { 321 // Display as a dropdown in the complete form or a single value in the response view. 322 $element = $form->add_form_element($item, 323 ['select', $inputname.'[0]', $name, array(0 => '') + $options, array('class' => $class)], 324 false, false); 325 $form->set_element_default($inputname.'[0]', $tmpvalue); 326 } else if ($info->subtype === 'c' && $form->is_frozen()) { 327 // Display list of checkbox values in the response view. 328 $objs = []; 329 foreach (explode(FEEDBACK_MULTICHOICE_LINE_SEP, $form->get_item_value($item)) as $v) { 330 $objs[] = ['static', $inputname."[$v]", '', isset($options[$v]) ? $options[$v] : '']; 331 } 332 $element = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class); 333 } else { 334 // Display group or radio or checkbox elements. 335 $class .= ' multichoice-' . ($info->horizontal ? 'horizontal' : 'vertical'); 336 $objs = []; 337 if ($info->subtype === 'c') { 338 // Checkboxes. 339 $objs[] = ['hidden', $inputname.'[0]', 0]; 340 $form->set_element_type($inputname.'[0]', PARAM_INT); 341 foreach ($options as $idx => $label) { 342 $objs[] = ['advcheckbox', $inputname.'['.$idx.']', '', $label, null, array(0, $idx)]; 343 $form->set_element_type($inputname.'['.$idx.']', PARAM_INT); 344 } 345 $element = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class); 346 if ($tmpvalue) { 347 foreach (explode(FEEDBACK_MULTICHOICE_LINE_SEP, $tmpvalue) as $v) { 348 $form->set_element_default($inputname.'['.$v.']', $v); 349 } 350 } 351 } else { 352 // Radio. 353 foreach ($options as $idx => $label) { 354 $objs[] = ['radio', $inputname.'[0]', '', $label, $idx]; 355 } 356 $element = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class); 357 $form->set_element_default($inputname.'[0]', $tmpvalue); 358 } 359 } 360 361 // Process 'required' rule. 362 if ($item->required) { 363 $elementname = $element->getName(); 364 $form->add_validation_rule(function($values, $files) use ($elementname, $item) { 365 $inputname = $item->typ . '_' . $item->id; 366 return empty($values[$inputname]) || !array_filter($values[$inputname]) ? 367 array($elementname => get_string('required')) : true; 368 }); 369 } 370 } 371 372 /** 373 * Prepares value that user put in the form for storing in DB 374 * @param array $value 375 * @return string 376 */ 377 public function create_value($value) { 378 $value = array_unique(array_filter($value)); 379 return join(FEEDBACK_MULTICHOICE_LINE_SEP, $value); 380 } 381 382 /** 383 * Compares the dbvalue with the dependvalue 384 * 385 * @param stdClass $item 386 * @param string $dbvalue is the value input by user in the format as it is stored in the db 387 * @param string $dependvalue is the value that it needs to be compared against 388 */ 389 public function compare_value($item, $dbvalue, $dependvalue) { 390 391 if (is_array($dbvalue)) { 392 $dbvalues = $dbvalue; 393 } else { 394 $dbvalues = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $dbvalue); 395 } 396 397 $info = $this->get_info($item); 398 $presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation); 399 $index = 1; 400 foreach ($presentation as $pres) { 401 foreach ($dbvalues as $dbval) { 402 if ($dbval == $index AND trim($pres) == $dependvalue) { 403 return true; 404 } 405 } 406 $index++; 407 } 408 return false; 409 } 410 411 public function get_info($item) { 412 $presentation = empty($item->presentation) ? '' : $item->presentation; 413 414 $info = new stdClass(); 415 //check the subtype of the multichoice 416 //it can be check(c), radio(r) or dropdown(d) 417 $info->subtype = ''; 418 $info->presentation = ''; 419 $info->horizontal = false; 420 421 $parts = explode(FEEDBACK_MULTICHOICE_TYPE_SEP, $item->presentation); 422 @list($info->subtype, $info->presentation) = $parts; 423 if (!isset($info->subtype)) { 424 $info->subtype = 'r'; 425 } 426 427 if ($info->subtype != 'd') { 428 $parts = explode(FEEDBACK_MULTICHOICE_ADJUST_SEP, $info->presentation); 429 @list($info->presentation, $info->horizontal) = $parts; 430 if (isset($info->horizontal) AND $info->horizontal == 1) { 431 $info->horizontal = true; 432 } else { 433 $info->horizontal = false; 434 } 435 } 436 return $info; 437 } 438 439 public function set_ignoreempty($item, $ignoreempty=true) { 440 $item->options = str_replace(FEEDBACK_MULTICHOICE_IGNOREEMPTY, '', $item->options); 441 if ($ignoreempty) { 442 $item->options .= FEEDBACK_MULTICHOICE_IGNOREEMPTY; 443 } 444 } 445 446 public function ignoreempty($item) { 447 if (strstr($item->options, FEEDBACK_MULTICHOICE_IGNOREEMPTY)) { 448 return true; 449 } 450 return false; 451 } 452 453 public function set_hidenoselect($item, $hidenoselect=true) { 454 $item->options = str_replace(FEEDBACK_MULTICHOICE_HIDENOSELECT, '', $item->options); 455 if ($hidenoselect) { 456 $item->options .= FEEDBACK_MULTICHOICE_HIDENOSELECT; 457 } 458 } 459 460 public function hidenoselect($item) { 461 if (strstr($item->options, FEEDBACK_MULTICHOICE_HIDENOSELECT)) { 462 return true; 463 } 464 return false; 465 } 466 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |