| Total Complexity | 121 |
| Total Lines | 1026 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Answer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Answer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Answer |
||
| 17 | { |
||
| 18 | public $questionId; |
||
| 19 | |||
| 20 | // these are arrays |
||
| 21 | public $answer; |
||
| 22 | public $correct; |
||
| 23 | public $comment; |
||
| 24 | public $weighting; |
||
| 25 | public $position; |
||
| 26 | public $hotspot_coordinates; |
||
| 27 | public $hotspot_type; |
||
| 28 | public $destination; |
||
| 29 | // these arrays are used to save temporarily new answers |
||
| 30 | // then they are moved into the arrays above or deleted in the event of cancellation |
||
| 31 | public $new_answer; |
||
| 32 | public $new_correct; |
||
| 33 | public $new_comment; |
||
| 34 | public $new_weighting; |
||
| 35 | public $new_position; |
||
| 36 | public $new_hotspot_coordinates; |
||
| 37 | public $new_hotspot_type; |
||
| 38 | public $autoId; |
||
| 39 | /** @var int Number of answers in the question */ |
||
| 40 | public $nbrAnswers; |
||
| 41 | public $new_nbrAnswers; |
||
| 42 | public $new_destination; // id of the next question if feedback option is set to Directfeedback |
||
| 43 | public $course; //Course information |
||
| 44 | public $iid; |
||
| 45 | public $questionJSId; |
||
| 46 | public $standalone; |
||
| 47 | /** @var Exercise|null */ |
||
| 48 | private $exercise; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @author Olivier Brouckaert |
||
| 52 | * |
||
| 53 | * @param int $questionId that answers belong to |
||
| 54 | * @param int $course_id |
||
| 55 | * @param Exercise $exercise |
||
| 56 | * @param bool $readAnswer |
||
| 57 | */ |
||
| 58 | public function __construct($questionId, $course_id = 0, $exercise = null, $readAnswer = true) |
||
| 59 | { |
||
| 60 | $this->questionId = (int) $questionId; |
||
| 61 | $this->answer = []; |
||
| 62 | $this->correct = []; |
||
| 63 | $this->comment = []; |
||
| 64 | $this->weighting = []; |
||
| 65 | $this->position = []; |
||
| 66 | $this->hotspot_coordinates = []; |
||
| 67 | $this->hotspot_type = []; |
||
| 68 | $this->destination = []; |
||
| 69 | // clears $new_* arrays |
||
| 70 | $this->cancel(); |
||
| 71 | |||
| 72 | if (empty($course_id)) { |
||
| 73 | $course_id = api_get_course_int_id(); |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->course = api_get_course_info_by_id($course_id); |
||
| 77 | if (empty($exercise)) { |
||
| 78 | // fills arrays |
||
| 79 | $objExercise = new Exercise($course_id); |
||
| 80 | $exerciseId = $_REQUEST['exerciseId'] ?? null; |
||
| 81 | $objExercise->read($exerciseId, false); |
||
| 82 | } else { |
||
| 83 | $objExercise = $exercise; |
||
| 84 | } |
||
| 85 | $this->exercise = $objExercise; |
||
| 86 | |||
| 87 | if ($readAnswer) { |
||
| 88 | if ('1' == $objExercise->random_answers && CALCULATED_ANSWER != $this->getQuestionType()) { |
||
| 89 | $this->readOrderedBy('rand()', ''); // randomize answers |
||
| 90 | } else { |
||
| 91 | $this->read(); // natural order |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Clears $new_* arrays. |
||
| 98 | * |
||
| 99 | * @author Olivier Brouckaert |
||
| 100 | */ |
||
| 101 | public function cancel() |
||
| 102 | { |
||
| 103 | $this->new_answer = []; |
||
| 104 | $this->new_correct = []; |
||
| 105 | $this->new_comment = []; |
||
| 106 | $this->new_weighting = []; |
||
| 107 | $this->new_position = []; |
||
| 108 | $this->new_hotspot_coordinates = []; |
||
| 109 | $this->new_hotspot_type = []; |
||
| 110 | $this->new_nbrAnswers = 0; |
||
| 111 | $this->new_destination = []; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Reads answer information from the database. |
||
| 116 | * |
||
| 117 | * @author Olivier Brouckaert |
||
| 118 | */ |
||
| 119 | public function read() |
||
| 120 | { |
||
| 121 | $table = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 122 | $questionId = $this->questionId; |
||
| 123 | |||
| 124 | $sql = "SELECT * FROM $table |
||
| 125 | WHERE |
||
| 126 | question_id ='".$questionId."' |
||
| 127 | ORDER BY position"; |
||
| 128 | |||
| 129 | $result = Database::query($sql); |
||
| 130 | $i = 1; |
||
| 131 | |||
| 132 | // while a record is found |
||
| 133 | while ($object = Database::fetch_object($result)) { |
||
| 134 | $this->id[$i] = $object->iid; |
||
| 135 | $this->autoId[$i] = $object->iid; |
||
| 136 | $this->iid[$i] = $object->iid; |
||
| 137 | $this->answer[$i] = $object->answer; |
||
| 138 | $this->correct[$i] = $object->correct; |
||
| 139 | $this->comment[$i] = $object->comment; |
||
| 140 | $this->weighting[$i] = $object->ponderation; |
||
| 141 | $this->position[$i] = $object->position; |
||
| 142 | $this->hotspot_coordinates[$i] = $object->hotspot_coordinates; |
||
| 143 | $this->hotspot_type[$i] = $object->hotspot_type; |
||
| 144 | $i++; |
||
| 145 | } |
||
| 146 | $this->nbrAnswers = $i - 1; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param int $id |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | public function getAnswerByAutoId($id) |
||
| 155 | { |
||
| 156 | foreach ($this->autoId as $key => $autoId) { |
||
| 157 | if ($autoId == $id) { |
||
| 158 | return [ |
||
| 159 | 'answer' => $this->answer[$key], |
||
| 160 | 'correct' => $this->correct[$key], |
||
| 161 | 'comment' => $this->comment[$key], |
||
| 162 | ]; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | return []; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * returns all answer ids from this question Id. |
||
| 171 | * |
||
| 172 | * @author Yoselyn Castillo |
||
| 173 | * |
||
| 174 | * @return array - $id (answer ids) |
||
| 175 | */ |
||
| 176 | public function selectAnswerId() |
||
| 177 | { |
||
| 178 | $table = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 179 | $questionId = $this->questionId; |
||
| 180 | |||
| 181 | $sql = "SELECT id FROM |
||
| 182 | $table |
||
| 183 | WHERE c_id = {$this->course_id} AND question_id ='".$questionId."'"; |
||
|
|
|||
| 184 | |||
| 185 | $result = Database::query($sql); |
||
| 186 | $id = []; |
||
| 187 | // while a record is found |
||
| 188 | if (Database::num_rows($result) > 0) { |
||
| 189 | while ($object = Database::fetch_array($result)) { |
||
| 190 | $id[] = $object['id']; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return $id; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Reads answer information from the data base ordered by parameter. |
||
| 199 | * |
||
| 200 | * @param string $field Field we want to order by |
||
| 201 | * @param string $order DESC or ASC |
||
| 202 | * |
||
| 203 | * @return bool |
||
| 204 | * |
||
| 205 | * @author Frederic Vauthier |
||
| 206 | */ |
||
| 207 | public function readOrderedBy($field, $order = 'ASC') |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * returns the autoincrement id. |
||
| 290 | * |
||
| 291 | * @author Juan Carlos Ra�a |
||
| 292 | * |
||
| 293 | * @return int - answer num |
||
| 294 | */ |
||
| 295 | public function selectAutoId($id) |
||
| 296 | { |
||
| 297 | return isset($this->iid[$id]) ? (int) $this->iid[$id] : 0; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * returns the number of answers in this question. |
||
| 302 | * |
||
| 303 | * @author Olivier Brouckaert |
||
| 304 | * |
||
| 305 | * @return int - number of answers |
||
| 306 | */ |
||
| 307 | public function selectNbrAnswers() |
||
| 308 | { |
||
| 309 | return $this->nbrAnswers; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * returns the question ID which the answers belong to. |
||
| 314 | * |
||
| 315 | * @author Olivier Brouckaert |
||
| 316 | * |
||
| 317 | * @return int - the question ID |
||
| 318 | */ |
||
| 319 | public function selectQuestionId() |
||
| 320 | { |
||
| 321 | return $this->questionId; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * returns the question ID of the destination question. |
||
| 326 | * |
||
| 327 | * @author Julio Montoya |
||
| 328 | * |
||
| 329 | * @param int $id |
||
| 330 | * |
||
| 331 | * @return int - the question ID |
||
| 332 | */ |
||
| 333 | public function selectDestination($id) |
||
| 334 | { |
||
| 335 | return isset($this->destination[$id]) ? $this->destination[$id] : null; |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * returns the answer title. |
||
| 340 | * |
||
| 341 | * @author Olivier Brouckaert |
||
| 342 | * |
||
| 343 | * @param - integer $id - answer ID |
||
| 344 | * |
||
| 345 | * @return string - answer title |
||
| 346 | */ |
||
| 347 | public function selectAnswer($id) |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * return array answer by id else return a bool. |
||
| 354 | * |
||
| 355 | * @param int $auto_id |
||
| 356 | * |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | public function selectAnswerByAutoId($auto_id) |
||
| 360 | { |
||
| 361 | $table = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 362 | $auto_id = (int) $auto_id; |
||
| 363 | $sql = "SELECT iid, answer FROM $table |
||
| 364 | WHERE iid='$auto_id'"; |
||
| 365 | $rs = Database::query($sql); |
||
| 366 | |||
| 367 | if (Database::num_rows($rs) > 0) { |
||
| 368 | return Database::fetch_assoc($rs); |
||
| 369 | } |
||
| 370 | |||
| 371 | return false; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * returns the answer title from an answer's position. |
||
| 376 | * |
||
| 377 | * @author Yannick Warnier |
||
| 378 | * |
||
| 379 | * @param - integer $pos - answer ID |
||
| 380 | * |
||
| 381 | * @return bool - answer title |
||
| 382 | */ |
||
| 383 | public function selectAnswerIdByPosition($pos) |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Returns a list of answers. |
||
| 398 | * |
||
| 399 | * @author Yannick Warnier <[email protected]> |
||
| 400 | * |
||
| 401 | * @param bool $decode |
||
| 402 | * |
||
| 403 | * @return array List of answers where each answer is an array |
||
| 404 | * of (id, answer, comment, grade) and grade=weighting |
||
| 405 | */ |
||
| 406 | public function getAnswersList($decode = false) |
||
| 407 | { |
||
| 408 | $list = []; |
||
| 409 | for ($i = 1; $i <= $this->nbrAnswers; $i++) { |
||
| 410 | if (!empty($this->answer[$i])) { |
||
| 411 | //Avoid problems when parsing elements with accents |
||
| 412 | if ($decode) { |
||
| 413 | $this->answer[$i] = api_html_entity_decode( |
||
| 414 | $this->answer[$i], |
||
| 415 | ENT_QUOTES, |
||
| 416 | api_get_system_encoding() |
||
| 417 | ); |
||
| 418 | $this->comment[$i] = api_html_entity_decode( |
||
| 419 | $this->comment[$i], |
||
| 420 | ENT_QUOTES, |
||
| 421 | api_get_system_encoding() |
||
| 422 | ); |
||
| 423 | } |
||
| 424 | |||
| 425 | $list[] = [ |
||
| 426 | 'id' => $i, |
||
| 427 | 'answer' => $this->answer[$i], |
||
| 428 | 'comment' => $this->comment[$i], |
||
| 429 | 'grade' => $this->weighting[$i], |
||
| 430 | 'hotspot_coord' => $this->hotspot_coordinates[$i], |
||
| 431 | 'hotspot_type' => $this->hotspot_type[$i], |
||
| 432 | 'correct' => $this->correct[$i], |
||
| 433 | ]; |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | return $list; |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Returns a list of grades. |
||
| 442 | * |
||
| 443 | * @author Yannick Warnier <[email protected]> |
||
| 444 | * |
||
| 445 | * @return array List of grades where grade=weighting (?) |
||
| 446 | */ |
||
| 447 | public function getGradesList() |
||
| 448 | { |
||
| 449 | $list = []; |
||
| 450 | for ($i = 0; $i < $this->nbrAnswers; $i++) { |
||
| 451 | if (!empty($this->answer[$i])) { |
||
| 452 | $list[$i] = $this->weighting[$i]; |
||
| 453 | } |
||
| 454 | } |
||
| 455 | |||
| 456 | return $list; |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Returns the question type. |
||
| 461 | * |
||
| 462 | * @author Yannick Warnier <[email protected]> |
||
| 463 | * |
||
| 464 | * @todo remove this function use CQuizQuestion |
||
| 465 | * |
||
| 466 | * @return int The type of the question this answer is bound to |
||
| 467 | */ |
||
| 468 | public function getQuestionType() |
||
| 469 | { |
||
| 470 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 471 | $sql = "SELECT type FROM $table |
||
| 472 | WHERE iid = '".$this->questionId."'"; |
||
| 473 | $res = Database::query($sql); |
||
| 474 | if (Database::num_rows($res) <= 0) { |
||
| 475 | return null; |
||
| 476 | } |
||
| 477 | $row = Database::fetch_array($res); |
||
| 478 | |||
| 479 | return (int) $row['type']; |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * tells if answer is correct or not. |
||
| 484 | * |
||
| 485 | * @author Olivier Brouckaert |
||
| 486 | * |
||
| 487 | * @param - integer $id - answer ID |
||
| 488 | * |
||
| 489 | * @return int - 0 if bad answer, not 0 if good answer |
||
| 490 | */ |
||
| 491 | public function isCorrect($id) |
||
| 492 | { |
||
| 493 | return isset($this->correct[$id]) ? $this->correct[$id] : null; |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * returns answer comment. |
||
| 498 | * |
||
| 499 | * @author Olivier Brouckaert |
||
| 500 | * |
||
| 501 | * @param - integer $id - answer ID |
||
| 502 | * |
||
| 503 | * @return string - answer comment |
||
| 504 | */ |
||
| 505 | public function selectComment($id) |
||
| 506 | { |
||
| 507 | return isset($this->comment[$id]) ? $this->comment[$id] : null; |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * returns answer weighting. |
||
| 512 | * |
||
| 513 | * @author Olivier Brouckaert |
||
| 514 | * |
||
| 515 | * @param - integer $id - answer ID |
||
| 516 | * |
||
| 517 | * @return int - answer weighting |
||
| 518 | */ |
||
| 519 | public function selectWeighting($id) |
||
| 520 | { |
||
| 521 | return isset($this->weighting[$id]) ? $this->weighting[$id] : null; |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * returns answer position. |
||
| 526 | * |
||
| 527 | * @author Olivier Brouckaert |
||
| 528 | * |
||
| 529 | * @param - integer $id - answer ID |
||
| 530 | * |
||
| 531 | * @return int - answer position |
||
| 532 | */ |
||
| 533 | public function selectPosition($id) |
||
| 534 | { |
||
| 535 | return isset($this->position[$id]) ? $this->position[$id] : null; |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * returns answer hotspot coordinates. |
||
| 540 | * |
||
| 541 | * @author Olivier Brouckaert |
||
| 542 | * |
||
| 543 | * @param int $id Answer ID |
||
| 544 | * |
||
| 545 | * @return int Answer position |
||
| 546 | */ |
||
| 547 | public function selectHotspotCoordinates($id) |
||
| 548 | { |
||
| 549 | return isset($this->hotspot_coordinates[$id]) ? $this->hotspot_coordinates[$id] : null; |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * returns answer hotspot type. |
||
| 554 | * |
||
| 555 | * @author Toon Keppens |
||
| 556 | * |
||
| 557 | * @param int $id Answer ID |
||
| 558 | * |
||
| 559 | * @return int Answer position |
||
| 560 | */ |
||
| 561 | public function selectHotspotType($id) |
||
| 562 | { |
||
| 563 | return isset($this->hotspot_type[$id]) ? $this->hotspot_type[$id] : null; |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Creates a new answer. |
||
| 568 | * |
||
| 569 | * @author Olivier Brouckaert |
||
| 570 | * |
||
| 571 | * @param string $answer answer title |
||
| 572 | * @param int $correct 0 if bad answer, not 0 if good answer |
||
| 573 | * @param string $comment answer comment |
||
| 574 | * @param int $weighting answer weighting |
||
| 575 | * @param int $position answer position |
||
| 576 | * @param array $new_hotspot_coordinates Coordinates for hotspot exercises (optional) |
||
| 577 | * @param int $new_hotspot_type Type for hotspot exercises (optional) |
||
| 578 | * @param string $destination |
||
| 579 | */ |
||
| 580 | public function createAnswer( |
||
| 581 | $answer, |
||
| 582 | $correct, |
||
| 583 | $comment, |
||
| 584 | $weighting, |
||
| 585 | $position, |
||
| 586 | $new_hotspot_coordinates = null, |
||
| 587 | $new_hotspot_type = null, |
||
| 588 | $destination = '' |
||
| 589 | ) { |
||
| 590 | $this->new_nbrAnswers++; |
||
| 591 | $id = $this->new_nbrAnswers; |
||
| 592 | $this->new_answer[$id] = $answer; |
||
| 593 | $this->new_correct[$id] = $correct; |
||
| 594 | $this->new_comment[$id] = $comment; |
||
| 595 | $this->new_weighting[$id] = $weighting; |
||
| 596 | $this->new_position[$id] = $position; |
||
| 597 | $this->new_hotspot_coordinates[$id] = $new_hotspot_coordinates; |
||
| 598 | $this->new_hotspot_type[$id] = $new_hotspot_type; |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Updates an answer. |
||
| 603 | * |
||
| 604 | * @author Toon Keppens |
||
| 605 | * |
||
| 606 | * @param int $iid |
||
| 607 | * @param string $answer |
||
| 608 | * @param string $comment |
||
| 609 | * @param string $correct |
||
| 610 | * @param string $weighting |
||
| 611 | * @param string $position |
||
| 612 | * @param string $destination |
||
| 613 | * @param string $hotSpotCoordinates |
||
| 614 | * @param string $hotSpotType |
||
| 615 | * |
||
| 616 | * @return CQuizAnswer |
||
| 617 | */ |
||
| 618 | public function updateAnswers( |
||
| 651 | } |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Records answers into the data base. |
||
| 655 | * |
||
| 656 | * @author Olivier Brouckaert |
||
| 657 | */ |
||
| 658 | public function save() |
||
| 659 | { |
||
| 660 | $answerTable = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 661 | $em = Database::getManager(); |
||
| 662 | $questionId = (int) $this->questionId; |
||
| 663 | |||
| 664 | $courseId = $this->course['real_id']; |
||
| 665 | $answerList = []; |
||
| 666 | $questionRepo = Container::getQuestionRepository(); |
||
| 667 | /** @var CQuizQuestion $question */ |
||
| 668 | $question = $questionRepo->find($questionId); |
||
| 669 | $questionType = $this->getQuestionType(); |
||
| 670 | |||
| 671 | for ($i = 1; $i <= $this->new_nbrAnswers; $i++) { |
||
| 672 | $answer = (string) ($this->new_answer[$i] ?? ''); |
||
| 673 | $correct = isset($this->new_correct[$i]) ? (int) $this->new_correct[$i] : null; |
||
| 674 | $comment = $this->new_comment[$i] ?? null; |
||
| 675 | $weighting = isset($this->new_weighting[$i]) ? (float) $this->new_weighting[$i] : null; |
||
| 676 | $position = isset($this->new_position[$i]) ? (int) $this->new_position[$i] : (int) $i; |
||
| 677 | $hotspot_coordinates = $this->new_hotspot_coordinates[$i] ?? null; |
||
| 678 | $hotspot_type = $this->new_hotspot_type[$i] ?? null; |
||
| 679 | $iid = isset($this->iid[$i]) ? (int) $this->iid[$i] : 0; |
||
| 680 | |||
| 681 | if (!isset($this->position[$i])) { |
||
| 682 | $quizAnswer = new CQuizAnswer(); |
||
| 683 | $quizAnswer |
||
| 684 | ->setQuestion($question) |
||
| 685 | ->setAnswer($answer) |
||
| 686 | ->setCorrect($correct) |
||
| 687 | ->setComment($comment) |
||
| 688 | ->setPonderation(!is_null($weighting) ? $weighting : 0.0) |
||
| 689 | ->setPosition($position) |
||
| 690 | ->setHotspotCoordinates($hotspot_coordinates) |
||
| 691 | ->setHotspotType($hotspot_type); |
||
| 692 | |||
| 693 | $em->persist($quizAnswer); |
||
| 694 | $em->flush(); |
||
| 695 | |||
| 696 | $iid = (int) $quizAnswer->getIid(); |
||
| 697 | |||
| 698 | if ($iid) { |
||
| 699 | if (in_array( |
||
| 700 | $questionType, |
||
| 701 | [MATCHING, MATCHING_COMBINATION, MATCHING_DRAGGABLE, MATCHING_DRAGGABLE_COMBINATION], |
||
| 702 | true |
||
| 703 | )) { |
||
| 704 | $answerObj = new self($this->questionId, $courseId, $this->exercise, false); |
||
| 705 | $answerObj->read(); |
||
| 706 | $correctAnswerId = $answerObj->selectAnswerIdByPosition($correct); |
||
| 707 | |||
| 708 | if (in_array($questionType, [MATCHING, MATCHING_COMBINATION], true) && !$correctAnswerId) { |
||
| 709 | continue; |
||
| 710 | } |
||
| 711 | $correctAnswerAutoId = $answerObj->selectAutoId($correct); |
||
| 712 | $quizAnswer->setCorrect($correctAnswerAutoId ?: 0); |
||
| 713 | } |
||
| 714 | |||
| 715 | $em->persist($quizAnswer); |
||
| 716 | $em->flush(); |
||
| 717 | } |
||
| 718 | } else { |
||
| 719 | // Update existing answer (note: updateAnswers will cast position to int now) |
||
| 720 | $this->updateAnswers( |
||
| 721 | $iid, |
||
| 722 | $this->new_answer[$i], |
||
| 723 | $this->new_comment[$i], |
||
| 724 | $this->new_correct[$i], |
||
| 725 | $this->new_weighting[$i], |
||
| 726 | $this->new_position[$i], |
||
| 727 | null, |
||
| 728 | $this->new_hotspot_coordinates[$i], |
||
| 729 | $this->new_hotspot_type[$i] |
||
| 730 | ); |
||
| 731 | } |
||
| 732 | |||
| 733 | $answerList[$i] = $iid; |
||
| 734 | } |
||
| 735 | |||
| 736 | // Post-processing for draggable/matching types |
||
| 737 | switch ($questionType) { |
||
| 738 | case MATCHING_DRAGGABLE: |
||
| 739 | foreach ($this->new_correct as $value => $status) { |
||
| 740 | if (!empty($status)) { |
||
| 741 | $correct = $answerList[$status] ?? $status; |
||
| 742 | $myAutoId = $answerList[$value]; |
||
| 743 | $sql = "UPDATE $answerTable SET correct = '$correct' WHERE iid = $myAutoId"; |
||
| 744 | Database::query($sql); |
||
| 745 | } |
||
| 746 | } |
||
| 747 | break; |
||
| 748 | |||
| 749 | case MATCHING_DRAGGABLE_COMBINATION: |
||
| 750 | foreach ($this->new_correct as $value => $status) { |
||
| 751 | if (!empty($status)) { |
||
| 752 | $correct = $answerList[$status] ?? $status; |
||
| 753 | $myAutoId = $answerList[$value]; |
||
| 754 | $sql = "UPDATE $answerTable SET correct = '$correct' WHERE iid = $myAutoId"; |
||
| 755 | Database::query($sql); |
||
| 756 | } |
||
| 757 | } |
||
| 758 | break; |
||
| 759 | |||
| 760 | case DRAGGABLE: |
||
| 761 | foreach ($this->new_correct as $value => $status) { |
||
| 762 | if (!empty($status)) { |
||
| 763 | $correct = $answerList[$status]; |
||
| 764 | $myAutoId = $answerList[$value]; |
||
| 765 | $sql = "UPDATE $answerTable SET correct = '$correct' WHERE iid = $myAutoId"; |
||
| 766 | Database::query($sql); |
||
| 767 | } |
||
| 768 | } |
||
| 769 | break; |
||
| 770 | } |
||
| 771 | |||
| 772 | // Delete trailing answers when we saved fewer than previously existed |
||
| 773 | if (count($this->position) > $this->new_nbrAnswers) { |
||
| 774 | $i = $this->new_nbrAnswers + 1; |
||
| 775 | while (isset($this->position[$i])) { |
||
| 776 | $toDeletePos = (int) $this->position[$i]; |
||
| 777 | $sql = "DELETE FROM $answerTable |
||
| 778 | WHERE question_id = '".$questionId."' AND position = '$toDeletePos'"; |
||
| 779 | Database::query($sql); |
||
| 780 | $i++; |
||
| 781 | } |
||
| 782 | } |
||
| 783 | |||
| 784 | // Move new_* arrays into main arrays |
||
| 785 | $this->answer = $this->new_answer; |
||
| 786 | $this->correct = $this->new_correct; |
||
| 787 | $this->comment = $this->new_comment; |
||
| 788 | $this->weighting = $this->new_weighting; |
||
| 789 | $this->position = $this->new_position; |
||
| 790 | $this->hotspot_coordinates = $this->new_hotspot_coordinates; |
||
| 791 | $this->hotspot_type = $this->new_hotspot_type; |
||
| 792 | $this->nbrAnswers = $this->new_nbrAnswers; |
||
| 793 | |||
| 794 | $this->cancel(); |
||
| 795 | } |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Duplicates answers by copying them into another question. |
||
| 799 | * |
||
| 800 | * @author Olivier Brouckaert |
||
| 801 | * |
||
| 802 | * @param null $courseInfo destination course info (result of the function api_get_course_info() ) |
||
| 803 | * |
||
| 804 | * @throws \Doctrine\ORM\ORMException |
||
| 805 | * @throws \Doctrine\ORM\OptimisticLockException |
||
| 806 | */ |
||
| 807 | public function duplicate(Question $newQuestion, $courseInfo = null) |
||
| 968 | } |
||
| 969 | } |
||
| 970 | } |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Get the necessary JavaScript for some answers. |
||
| 974 | * |
||
| 975 | * @return string |
||
| 976 | */ |
||
| 977 | public function getJs() |
||
| 978 | { |
||
| 979 | return "<script> |
||
| 980 | $(window).on('load', function() { |
||
| 981 | jsPlumb.ready(function() { |
||
| 982 | if ($('#drag{$this->questionId}_question').length > 0) { |
||
| 983 | setTimeout(function() { |
||
| 984 | // Initialize MatchingDraggable |
||
| 985 | MatchingDraggable.init('{$this->questionId}'); |
||
| 986 | }, 1000); |
||
| 987 | } |
||
| 988 | }); |
||
| 989 | }); |
||
| 990 | </script>"; |
||
| 991 | } |
||
| 992 | |||
| 993 | /** |
||
| 994 | * Check if a answer is correct by an answer auto id. |
||
| 995 | * |
||
| 996 | * @param int $needle The answer auto id |
||
| 997 | * |
||
| 998 | * @return bool |
||
| 999 | */ |
||
| 1000 | public function isCorrectByAutoId($needle) |
||
| 1001 | { |
||
| 1002 | $key = 0; |
||
| 1003 | if (is_array($this->autoId)) { |
||
| 1004 | foreach ($this->autoId as $autoIdKey => $autoId) { |
||
| 1005 | if ($autoId == $needle) { |
||
| 1006 | $key = $autoIdKey; |
||
| 1007 | } |
||
| 1008 | } |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | if (!$key) { |
||
| 1012 | return false; |
||
| 1013 | } |
||
| 1014 | |||
| 1015 | return $this->isCorrect($key) ? true : false; |
||
| 1016 | } |
||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Get answers already added to question. |
||
| 1020 | * |
||
| 1021 | * @return array |
||
| 1022 | */ |
||
| 1023 | public function getAnswers() |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | } |
||
| 1045 |