[ Index ] |
PHP Cross Reference of moodle-2.8 |
[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 class feedback_item_numeric extends feedback_item_base { 21 protected $type = "numeric"; 22 public $sep_dec, $sep_thous; 23 private $commonparams; 24 private $item_form; 25 private $item; 26 27 public function init() { 28 $this->sep_dec = get_string('separator_decimal', 'feedback'); 29 if (substr($this->sep_dec, 0, 2) == '[[') { 30 $this->sep_dec = FEEDBACK_DECIMAL; 31 } 32 33 $this->sep_thous = get_string('separator_thousand', 'feedback'); 34 if (substr($this->sep_thous, 0, 2) == '[[') { 35 $this->sep_thous = FEEDBACK_THOUSAND; 36 } 37 } 38 39 public function build_editform($item, $feedback, $cm) { 40 global $DB, $CFG; 41 require_once ('numeric_form.php'); 42 43 //get the lastposition number of the feedback_items 44 $position = $item->position; 45 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id)); 46 if ($position == -1) { 47 $i_formselect_last = $lastposition + 1; 48 $i_formselect_value = $lastposition + 1; 49 $item->position = $lastposition + 1; 50 } else { 51 $i_formselect_last = $lastposition; 52 $i_formselect_value = $item->position; 53 } 54 //the elements for position dropdownlist 55 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true); 56 57 $item->presentation = empty($item->presentation) ? '' : $item->presentation; 58 59 $range_from_to = explode('|', $item->presentation); 60 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) { 61 $range_from = str_replace(FEEDBACK_DECIMAL, 62 $this->sep_dec, 63 floatval($range_from_to[0])); 64 } else { 65 $range_from = '-'; 66 } 67 68 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) { 69 $range_to = str_replace(FEEDBACK_DECIMAL, 70 $this->sep_dec, 71 floatval($range_from_to[1])); 72 } else { 73 $range_to = '-'; 74 } 75 76 $item->rangefrom = $range_from; 77 $item->rangeto = $range_to; 78 79 //all items for dependitem 80 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item); 81 $commonparams = array('cmid'=>$cm->id, 82 'id'=>isset($item->id) ? $item->id : null, 83 'typ'=>$item->typ, 84 'items'=>$feedbackitems, 85 'feedback'=>$feedback->id); 86 87 //build the form 88 $customdata = array('item' => $item, 89 'common' => $commonparams, 90 'positionlist' => $positionlist, 91 'position' => $position); 92 93 $this->item_form = new feedback_numeric_form('edit_item.php', $customdata); 94 } 95 96 //this function only can used after the call of build_editform() 97 public function show_editform() { 98 $this->item_form->display(); 99 } 100 101 public function is_cancelled() { 102 return $this->item_form->is_cancelled(); 103 } 104 105 public function get_data() { 106 if ($this->item = $this->item_form->get_data()) { 107 return true; 108 } 109 return false; 110 } 111 112 public function save_item() { 113 global $DB; 114 115 if (!$item = $this->item_form->get_data()) { 116 return false; 117 } 118 119 if (isset($item->clone_item) AND $item->clone_item) { 120 $item->id = ''; //to clone this item 121 $item->position++; 122 } 123 124 $item->hasvalue = $this->get_hasvalue(); 125 if (!$item->id) { 126 $item->id = $DB->insert_record('feedback_item', $item); 127 } else { 128 $DB->update_record('feedback_item', $item); 129 } 130 131 return $DB->get_record('feedback_item', array('id'=>$item->id)); 132 } 133 134 135 //liefert eine Struktur ->name, ->data = array(mit Antworten) 136 public function get_analysed($item, $groupid = false, $courseid = false) { 137 global $DB; 138 139 $analysed = new stdClass(); 140 $analysed->data = array(); 141 $analysed->name = $item->name; 142 $values = feedback_get_group_values($item, $groupid, $courseid); 143 144 $avg = 0.0; 145 $counter = 0; 146 if ($values) { 147 $data = array(); 148 foreach ($values as $value) { 149 if (is_numeric($value->value)) { 150 $data[] = $value->value; 151 $avg += $value->value; 152 $counter++; 153 } 154 } 155 $avg = $counter > 0 ? $avg / $counter : 0; 156 $analysed->data = $data; 157 $analysed->avg = $avg; 158 } 159 return $analysed; 160 } 161 162 public function get_printval($item, $value) { 163 if (!isset($value->value)) { 164 return ''; 165 } 166 167 return $value->value; 168 } 169 170 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) { 171 172 $values = $this->get_analysed($item, $groupid, $courseid); 173 174 if (isset($values->data) AND is_array($values->data)) { 175 echo '<tr><th colspan="2" align="left">'; 176 echo $itemnr.' ('.$item->label.') '.$item->name; 177 echo '</th></tr>'; 178 179 foreach ($values->data as $value) { 180 echo '<tr><td colspan="2" valign="top" align="left">'; 181 echo '- '.number_format($value, 2, $this->sep_dec, $this->sep_thous); 182 echo '</td></tr>'; 183 } 184 185 if (isset($values->avg)) { 186 $avg = number_format($values->avg, 2, $this->sep_dec, $this->sep_thous); 187 } else { 188 $avg = number_format(0, 2, $this->sep_dec, $this->sep_thous); 189 } 190 echo '<tr><td align="left" colspan="2"><b>'; 191 echo get_string('average', 'feedback').': '.$avg; 192 echo '</b></td></tr>'; 193 } 194 } 195 196 public function excelprint_item(&$worksheet, $row_offset, 197 $xls_formats, $item, 198 $groupid, $courseid = false) { 199 200 $analysed_item = $this->get_analysed($item, $groupid, $courseid); 201 202 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2); 203 $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2); 204 $data = $analysed_item->data; 205 if (is_array($data)) { 206 207 //mittelwert anzeigen 208 $worksheet->write_string($row_offset, 209 2, 210 get_string('average', 'feedback'), 211 $xls_formats->value_bold); 212 213 $worksheet->write_number($row_offset + 1, 214 2, 215 $analysed_item->avg, 216 $xls_formats->value_bold); 217 $row_offset++; 218 } 219 $row_offset++; 220 return $row_offset; 221 } 222 223 /** 224 * print the item at the edit-page of feedback 225 * 226 * @global object 227 * @param object $item 228 * @return void 229 */ 230 public function print_item_preview($item) { 231 global $OUTPUT, $DB; 232 233 $align = right_to_left() ? 'right' : 'left'; 234 $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'. 235 get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />'; 236 237 //get the range 238 $range_from_to = explode('|', $item->presentation); 239 240 //get the min-value 241 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) { 242 $range_from = floatval($range_from_to[0]); 243 } else { 244 $range_from = 0; 245 } 246 247 //get the max-value 248 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) { 249 $range_to = floatval($range_from_to[1]); 250 } else { 251 $range_to = 0; 252 } 253 254 $requiredmark = ($item->required == 1) ? $strrequiredmark : ''; 255 //print the question and label 256 $inputname = $item->typ . '_' . $item->id; 257 echo '<div class="feedback_item_label_'.$align.'">'; 258 echo '<label for="'. $inputname .'">'; 259 echo '('.$item->label.') '; 260 echo format_text($item->name . $requiredmark, true, false, false); 261 if ($item->dependitem) { 262 $params = array('id'=>$item->dependitem); 263 if ($dependitem = $DB->get_record('feedback_item', $params)) { 264 echo ' <span class="feedback_depend">'; 265 echo '('.$dependitem->label.'->'.$item->dependvalue.')'; 266 echo '</span>'; 267 } 268 } 269 echo '<span class="feedback_item_numinfo">'; 270 switch(true) { 271 case ($range_from === '-' AND is_numeric($range_to)): 272 echo ' ('.get_string('maximal', 'feedback'). 273 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')'; 274 break; 275 case (is_numeric($range_from) AND $range_to === '-'): 276 echo ' ('.get_string('minimal', 'feedback'). 277 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')'; 278 break; 279 case ($range_from === '-' AND $range_to === '-'): 280 break; 281 default: 282 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from). 283 ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')'; 284 break; 285 } 286 echo '</span>'; 287 echo '</label>'; 288 echo '</div>'; 289 290 //print the presentation 291 echo '<div class="feedback_item_presentation_'.$align.'">'; 292 echo '<span class="feedback_item_textfield">'; 293 echo '<input type="text" '. 294 'id="'.$inputname.'" '. 295 'name="'.$inputname.'" '. 296 'size="10" '. 297 'maxlength="10" '. 298 'value="" />'; 299 300 echo '</span>'; 301 echo '</div>'; 302 } 303 304 /** 305 * print the item at the complete-page of feedback 306 * 307 * @global object 308 * @param object $item 309 * @param string $value 310 * @param bool $highlightrequire 311 * @return void 312 */ 313 public function print_item_complete($item, $value = '', $highlightrequire = false) { 314 global $OUTPUT; 315 $align = right_to_left() ? 'right' : 'left'; 316 $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'. 317 get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />'; 318 319 //get the range 320 $range_from_to = explode('|', $item->presentation); 321 322 //get the min-value 323 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) { 324 $range_from = floatval($range_from_to[0]); 325 } else { 326 $range_from = 0; 327 } 328 329 //get the max-value 330 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) { 331 $range_to = floatval($range_from_to[1]); 332 } else { 333 $range_to = 0; 334 } 335 336 $requiredmark = ($item->required == 1) ? $strrequiredmark : ''; 337 338 //print the question and label 339 $inputname = $item->typ . '_' . $item->id; 340 echo '<div class="feedback_item_label_'.$align.'">'; 341 echo '<label for="'. $inputname .'">'; 342 echo format_text($item->name . $requiredmark, true, false, false); 343 echo '<span class="feedback_item_numinfo">'; 344 switch(true) { 345 case ($range_from === '-' AND is_numeric($range_to)): 346 echo ' ('.get_string('maximal', 'feedback'). 347 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')'; 348 break; 349 case (is_numeric($range_from) AND $range_to === '-'): 350 echo ' ('.get_string('minimal', 'feedback'). 351 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')'; 352 break; 353 case ($range_from === '-' AND $range_to === '-'): 354 break; 355 default: 356 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from). 357 ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')'; 358 break; 359 } 360 echo '</span>'; 361 if ($highlightrequire AND (!$this->check_value($value, $item))) { 362 echo '<br class="error"><span id="id_error_'.$inputname.'" class="error"> '.get_string('err_required', 'form'). 363 '</span><br id="id_error_break_'.$inputname.'" class="error" >'; 364 } 365 echo '</label>'; 366 echo '</div>'; 367 368 //print the presentation 369 echo '<div class="feedback_item_presentation_'.$align.'">'; 370 echo '<span class="feedback_item_textfield">'; 371 echo '<input type="text" '. 372 'id="'.$inputname.'" '. 373 'name="'.$item->typ.'_'.$item->id.'" '. 374 'size="10" '. 375 'maxlength="10" '. 376 'value="'.$value.'" />'; 377 378 echo '</span>'; 379 echo '</div>'; 380 } 381 382 /** 383 * print the item at the complete-page of feedback 384 * 385 * @global object 386 * @param object $item 387 * @param string $value 388 * @return void 389 */ 390 public function print_item_show_value($item, $value = '') { 391 global $OUTPUT; 392 $align = right_to_left() ? 'right' : 'left'; 393 $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'. 394 get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />'; 395 396 //get the range 397 $range_from_to = explode('|', $item->presentation); 398 //get the min-value 399 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) { 400 $range_from = floatval($range_from_to[0]); 401 } else { 402 $range_from = 0; 403 } 404 //get the max-value 405 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) { 406 $range_to = floatval($range_from_to[1]); 407 } else { 408 $range_to = 0; 409 } 410 $requiredmark = ($item->required == 1) ? $strrequiredmark : ''; 411 412 //print the question and label 413 echo '<div class="feedback_item_label_'.$align.'">'; 414 echo '('.$item->label.') '; 415 echo format_text($item->name . $requiredmark, true, false, false); 416 switch(true) { 417 case ($range_from === '-' AND is_numeric($range_to)): 418 echo ' ('.get_string('maximal', 'feedback'). 419 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')'; 420 break; 421 case (is_numeric($range_from) AND $range_to === '-'): 422 echo ' ('.get_string('minimal', 'feedback'). 423 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')'; 424 break; 425 case ($range_from === '-' AND $range_to === '-'): 426 break; 427 default: 428 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from). 429 ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')'; 430 break; 431 } 432 echo '</div>'; 433 434 //print the presentation 435 echo '<div class="feedback_item_presentation_'.$align.'">'; 436 echo $OUTPUT->box_start('generalbox boxalign'.$align); 437 if (is_numeric($value)) { 438 $str_num_value = number_format($value, 2, $this->sep_dec, $this->sep_thous); 439 } else { 440 $str_num_value = ' '; 441 } 442 echo $str_num_value; 443 echo $OUTPUT->box_end(); 444 echo '</div>'; 445 } 446 447 public function check_value($value, $item) { 448 $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value); 449 //if the item is not required, so the check is true if no value is given 450 if ((!isset($value) OR $value == '') AND $item->required != 1) { 451 return true; 452 } 453 if (!is_numeric($value)) { 454 return false; 455 } 456 457 $range_from_to = explode('|', $item->presentation); 458 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) { 459 $range_from = floatval($range_from_to[0]); 460 } else { 461 $range_from = '-'; 462 } 463 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) { 464 $range_to = floatval($range_from_to[1]); 465 } else { 466 $range_to = '-'; 467 } 468 469 switch(true) { 470 case ($range_from === '-' AND is_numeric($range_to)): 471 if (floatval($value) <= $range_to) { 472 return true; 473 } 474 break; 475 case (is_numeric($range_from) AND $range_to === '-'): 476 if (floatval($value) >= $range_from) { 477 return true; 478 } 479 break; 480 case ($range_from === '-' AND $range_to === '-'): 481 return true; 482 break; 483 default: 484 if (floatval($value) >= $range_from AND floatval($value) <= $range_to) { 485 return true; 486 } 487 break; 488 } 489 490 return false; 491 } 492 493 public function create_value($data) { 494 $data = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data); 495 496 if (is_numeric($data)) { 497 $data = floatval($data); 498 } else { 499 $data = ''; 500 } 501 return $data; 502 } 503 504 //compares the dbvalue with the dependvalue 505 //dbvalue is the number put in by the user 506 //dependvalue is the value that is compared 507 public function compare_value($item, $dbvalue, $dependvalue) { 508 if ($dbvalue == $dependvalue) { 509 return true; 510 } 511 return false; 512 } 513 514 public function get_presentation($data) { 515 $num1 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangefrom); 516 if (is_numeric($num1)) { 517 $num1 = floatval($num1); 518 } else { 519 $num1 = '-'; 520 } 521 522 $num2 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangeto); 523 if (is_numeric($num2)) { 524 $num2 = floatval($num2); 525 } else { 526 $num2 = '-'; 527 } 528 529 if ($num1 === '-' OR $num2 === '-') { 530 return $num1 . '|'. $num2; 531 } 532 533 if ($num1 > $num2) { 534 return $num2 . '|'. $num1; 535 } else { 536 return $num1 . '|'. $num2; 537 } 538 } 539 540 public function get_hasvalue() { 541 return 1; 542 } 543 544 public function can_switch_require() { 545 return true; 546 } 547 548 public function value_type() { 549 return PARAM_FLOAT; 550 } 551 552 public function clean_input_value($value) { 553 $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value); 554 if (!is_numeric($value)) { 555 if ($value == '') { 556 return null; //an empty string should be null 557 } else { 558 return clean_param($value, PARAM_TEXT); //we have to know the value if it is wrong 559 } 560 } 561 return clean_param($value, $this->value_type()); 562 } 563 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |