| Total Complexity | 252 |
| Total Lines | 2364 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Question 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 Question, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class Question |
||
| 20 | { |
||
| 21 | public $id; |
||
| 22 | public $iid; |
||
| 23 | public $question; |
||
| 24 | public $description; |
||
| 25 | public $weighting; |
||
| 26 | public $position; |
||
| 27 | public $type; |
||
| 28 | public $level; |
||
| 29 | public $picture; |
||
| 30 | public $exerciseList; // array with the list of exercises which this question is in |
||
| 31 | public $category_list; |
||
| 32 | public $parent_id; |
||
| 33 | public $category; |
||
| 34 | public $isContent; |
||
| 35 | public $course; |
||
| 36 | public $feedback; |
||
| 37 | public static $typePicture = 'new_question.png'; |
||
| 38 | public static $explanationLangVar = ''; |
||
| 39 | public $question_table_class = 'table table-striped'; |
||
| 40 | public $questionTypeWithFeedback; |
||
| 41 | public $extra; |
||
| 42 | public $export = false; |
||
| 43 | public $code; |
||
| 44 | public static $questionTypes = [ |
||
| 45 | UNIQUE_ANSWER => ['unique_answer.class.php', 'UniqueAnswer'], |
||
| 46 | MULTIPLE_ANSWER => ['multiple_answer.class.php', 'MultipleAnswer'], |
||
| 47 | FILL_IN_BLANKS => ['fill_blanks.class.php', 'FillBlanks'], |
||
| 48 | MATCHING => ['matching.class.php', 'Matching'], |
||
| 49 | FREE_ANSWER => ['freeanswer.class.php', 'FreeAnswer'], |
||
| 50 | ORAL_EXPRESSION => ['oral_expression.class.php', 'OralExpression'], |
||
| 51 | HOT_SPOT => ['hotspot.class.php', 'HotSpot'], |
||
| 52 | HOT_SPOT_DELINEATION => ['hotspot.class.php', 'HotspotDelineation'], |
||
| 53 | MULTIPLE_ANSWER_COMBINATION => ['multiple_answer_combination.class.php', 'MultipleAnswerCombination'], |
||
| 54 | UNIQUE_ANSWER_NO_OPTION => ['unique_answer_no_option.class.php', 'UniqueAnswerNoOption'], |
||
| 55 | MULTIPLE_ANSWER_TRUE_FALSE => ['multiple_answer_true_false.class.php', 'MultipleAnswerTrueFalse'], |
||
| 56 | MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY => [ |
||
| 57 | 'MultipleAnswerTrueFalseDegreeCertainty.php', |
||
| 58 | 'MultipleAnswerTrueFalseDegreeCertainty', |
||
| 59 | ], |
||
| 60 | MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE => [ |
||
| 61 | 'multiple_answer_combination_true_false.class.php', |
||
| 62 | 'MultipleAnswerCombinationTrueFalse', |
||
| 63 | ], |
||
| 64 | GLOBAL_MULTIPLE_ANSWER => ['global_multiple_answer.class.php', 'GlobalMultipleAnswer'], |
||
| 65 | CALCULATED_ANSWER => ['calculated_answer.class.php', 'CalculatedAnswer'], |
||
| 66 | UNIQUE_ANSWER_IMAGE => ['UniqueAnswerImage.php', 'UniqueAnswerImage'], |
||
| 67 | DRAGGABLE => ['Draggable.php', 'Draggable'], |
||
| 68 | MATCHING_DRAGGABLE => ['MatchingDraggable.php', 'MatchingDraggable'], |
||
| 69 | //MEDIA_QUESTION => array('media_question.class.php' , 'MediaQuestion') |
||
| 70 | ANNOTATION => ['Annotation.php', 'Annotation'], |
||
| 71 | READING_COMPREHENSION => ['ReadingComprehension.php', 'ReadingComprehension'], |
||
| 72 | ]; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * constructor of the class. |
||
| 76 | * |
||
| 77 | * @author Olivier Brouckaert |
||
| 78 | */ |
||
| 79 | public function __construct() |
||
| 80 | { |
||
| 81 | $this->id = 0; |
||
| 82 | $this->iid = 0; |
||
| 83 | $this->question = ''; |
||
| 84 | $this->description = ''; |
||
| 85 | $this->weighting = 0; |
||
| 86 | $this->position = 1; |
||
| 87 | $this->picture = ''; |
||
| 88 | $this->level = 1; |
||
| 89 | $this->category = 0; |
||
| 90 | // This variable is used when loading an exercise like an scenario with |
||
| 91 | // an special hotspot: final_overlap, final_missing, final_excess |
||
| 92 | $this->extra = ''; |
||
| 93 | $this->exerciseList = []; |
||
| 94 | $this->course = api_get_course_info(); |
||
| 95 | $this->category_list = []; |
||
| 96 | $this->parent_id = 0; |
||
| 97 | // See BT#12611 |
||
| 98 | $this->questionTypeWithFeedback = [ |
||
| 99 | MATCHING, |
||
| 100 | MATCHING_DRAGGABLE, |
||
| 101 | DRAGGABLE, |
||
| 102 | FILL_IN_BLANKS, |
||
| 103 | FREE_ANSWER, |
||
| 104 | ORAL_EXPRESSION, |
||
| 105 | CALCULATED_ANSWER, |
||
| 106 | ANNOTATION, |
||
| 107 | ]; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return int|null |
||
| 112 | */ |
||
| 113 | public function getIsContent() |
||
| 114 | { |
||
| 115 | $isContent = null; |
||
| 116 | if (isset($_REQUEST['isContent'])) { |
||
| 117 | $isContent = intval($_REQUEST['isContent']); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $this->isContent = $isContent; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Reads question information from the data base. |
||
| 125 | * |
||
| 126 | * @param int $id - question ID |
||
| 127 | * @param int $course_id |
||
| 128 | * |
||
| 129 | * @return Question |
||
| 130 | * |
||
| 131 | * @author Olivier Brouckaert |
||
| 132 | */ |
||
| 133 | public static function read($id, $course_id = null) |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * returns the question ID. |
||
| 204 | * |
||
| 205 | * @author Olivier Brouckaert |
||
| 206 | * |
||
| 207 | * @return int - question ID |
||
| 208 | */ |
||
| 209 | public function selectId() |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * returns the question title. |
||
| 216 | * |
||
| 217 | * @author Olivier Brouckaert |
||
| 218 | * |
||
| 219 | * @return string - question title |
||
| 220 | */ |
||
| 221 | public function selectTitle() |
||
| 222 | { |
||
| 223 | if (!api_get_configuration_value('save_titles_as_html')) { |
||
| 224 | return $this->question; |
||
| 225 | } |
||
| 226 | |||
| 227 | return Display::div($this->question, ['style' => 'display: inline-block;']); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param int $itemNumber |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getTitleToDisplay($itemNumber) |
||
| 236 | { |
||
| 237 | $showQuestionTitleHtml = api_get_configuration_value('save_titles_as_html'); |
||
| 238 | $title = ''; |
||
| 239 | if (api_get_configuration_value('show_question_id')) { |
||
| 240 | $title .= '<h4>#'.$this->course['code'].'-'.$this->iid.'</h4>'; |
||
| 241 | } |
||
| 242 | |||
| 243 | $title .= $showQuestionTitleHtml ? '' : '<strong>'; |
||
| 244 | $title .= $itemNumber.'. '.$this->selectTitle(); |
||
| 245 | $title .= $showQuestionTitleHtml ? '' : '</strong>'; |
||
| 246 | |||
| 247 | return Display::div( |
||
| 248 | $title, |
||
| 249 | ['class' => 'question_title'] |
||
| 250 | ); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * returns the question description. |
||
| 255 | * |
||
| 256 | * @author Olivier Brouckaert |
||
| 257 | * |
||
| 258 | * @return string - question description |
||
| 259 | */ |
||
| 260 | public function selectDescription() |
||
| 261 | { |
||
| 262 | return $this->description; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * returns the question weighting. |
||
| 267 | * |
||
| 268 | * @author Olivier Brouckaert |
||
| 269 | * |
||
| 270 | * @return int - question weighting |
||
| 271 | */ |
||
| 272 | public function selectWeighting() |
||
| 273 | { |
||
| 274 | return $this->weighting; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * returns the question position. |
||
| 279 | * |
||
| 280 | * @author Olivier Brouckaert |
||
| 281 | * |
||
| 282 | * @return int - question position |
||
| 283 | */ |
||
| 284 | public function selectPosition() |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * returns the answer type. |
||
| 291 | * |
||
| 292 | * @author Olivier Brouckaert |
||
| 293 | * |
||
| 294 | * @return int - answer type |
||
| 295 | */ |
||
| 296 | public function selectType() |
||
| 297 | { |
||
| 298 | return $this->type; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * returns the level of the question. |
||
| 303 | * |
||
| 304 | * @author Nicolas Raynaud |
||
| 305 | * |
||
| 306 | * @return int - level of the question, 0 by default |
||
| 307 | */ |
||
| 308 | public function getLevel() |
||
| 309 | { |
||
| 310 | return $this->level; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * returns the picture name. |
||
| 315 | * |
||
| 316 | * @author Olivier Brouckaert |
||
| 317 | * |
||
| 318 | * @return string - picture name |
||
| 319 | */ |
||
| 320 | public function selectPicture() |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | public function selectPicturePath() |
||
| 329 | { |
||
| 330 | if (!empty($this->picture)) { |
||
| 331 | $router = \Chamilo\CoreBundle\Framework\Container::getRouter(); |
||
| 332 | // this "filter" param is used to resize the image to width 800px see config/packages/liip_imagine.yaml |
||
| 333 | $params = [ |
||
| 334 | 'file' => 'images/'.$this->getPictureFilename(), |
||
| 335 | 'type' => 'show', |
||
| 336 | 'filter' => 'hotspot_question', |
||
| 337 | 'course' => api_get_course_id(), |
||
| 338 | ]; |
||
| 339 | $url = $router->generate('core_tool_document', $params); |
||
| 340 | |||
| 341 | return $url; |
||
| 342 | |||
| 343 | /*return api_get_path(WEB_COURSE_PATH). |
||
| 344 | $this->course['directory'].'/document/images/'.$this->getPictureFilename().'?'.api_get_cidreq().'&type=show&filter=hotspot_question';*/ |
||
| 345 | } |
||
| 346 | |||
| 347 | return ''; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return int|string |
||
| 352 | */ |
||
| 353 | public function getPictureId() |
||
| 354 | { |
||
| 355 | // for backward compatibility |
||
| 356 | // when in field picture we had the filename not the document id |
||
| 357 | if (preg_match("/quiz-.*/", $this->picture)) { |
||
| 358 | return DocumentManager::get_document_id( |
||
| 359 | $this->course, |
||
| 360 | $this->selectPicturePath(), |
||
| 361 | api_get_session_id() |
||
| 362 | ); |
||
| 363 | } |
||
| 364 | |||
| 365 | return $this->picture; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param int $courseId |
||
| 370 | * @param int $sessionId |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | public function getPictureFilename($courseId = 0, $sessionId = 0) |
||
| 375 | { |
||
| 376 | $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId; |
||
| 377 | $sessionId = empty($sessionId) ? api_get_session_id() : (int) $sessionId; |
||
| 378 | |||
| 379 | if (empty($courseId)) { |
||
| 380 | return ''; |
||
| 381 | } |
||
| 382 | // for backward compatibility |
||
| 383 | // when in field picture we had the filename not the document id |
||
| 384 | if (preg_match("/quiz-.*/", $this->picture)) { |
||
| 385 | return $this->picture; |
||
| 386 | } |
||
| 387 | |||
| 388 | $pictureId = $this->getPictureId(); |
||
| 389 | $courseInfo = $this->course; |
||
| 390 | $documentInfo = DocumentManager::get_document_data_by_id( |
||
| 391 | $pictureId, |
||
| 392 | $courseInfo['code'], |
||
| 393 | false, |
||
| 394 | $sessionId |
||
| 395 | ); |
||
| 396 | $documentFilename = ''; |
||
| 397 | if ($documentInfo) { |
||
| 398 | // document in document/images folder |
||
| 399 | $documentFilename = pathinfo( |
||
| 400 | $documentInfo['path'], |
||
| 401 | PATHINFO_BASENAME |
||
| 402 | ); |
||
| 403 | } |
||
| 404 | |||
| 405 | return $documentFilename; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param int $courseId |
||
| 410 | * @param int $sessionId |
||
| 411 | * |
||
| 412 | * @return false|CDocument |
||
| 413 | */ |
||
| 414 | public function getPicture($courseId = 0, $sessionId = 0) |
||
| 415 | { |
||
| 416 | $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId; |
||
| 417 | $sessionId = empty($sessionId) ? api_get_session_id() : (int) $sessionId; |
||
| 418 | |||
| 419 | if (empty($courseId)) { |
||
| 420 | return false; |
||
| 421 | } |
||
| 422 | |||
| 423 | $pictureId = $this->getPictureId(); |
||
| 424 | $courseInfo = $this->course; |
||
| 425 | $documentInfo = DocumentManager::get_document_data_by_id( |
||
| 426 | $pictureId, |
||
| 427 | $courseInfo['code'], |
||
| 428 | false, |
||
| 429 | $sessionId |
||
| 430 | ); |
||
| 431 | |||
| 432 | if ($documentInfo) { |
||
| 433 | $em = Database::getManager(); |
||
| 434 | |||
| 435 | /** @var CDocument $document */ |
||
| 436 | $document = $em->getRepository('ChamiloCourseBundle:CDocument')->find($documentInfo['iid']); |
||
| 437 | |||
| 438 | return $document; |
||
| 439 | } |
||
| 440 | |||
| 441 | return false; |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * returns the array with the exercise ID list. |
||
| 446 | * |
||
| 447 | * @author Olivier Brouckaert |
||
| 448 | * |
||
| 449 | * @return array - list of exercise ID which the question is in |
||
| 450 | */ |
||
| 451 | public function selectExerciseList() |
||
| 452 | { |
||
| 453 | return $this->exerciseList; |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * returns the number of exercises which this question is in. |
||
| 458 | * |
||
| 459 | * @author Olivier Brouckaert |
||
| 460 | * |
||
| 461 | * @return int - number of exercises |
||
| 462 | */ |
||
| 463 | public function selectNbrExercises() |
||
| 464 | { |
||
| 465 | return count($this->exerciseList); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * changes the question title. |
||
| 470 | * |
||
| 471 | * @param string $title - question title |
||
| 472 | * |
||
| 473 | * @author Olivier Brouckaert |
||
| 474 | */ |
||
| 475 | public function updateTitle($title) |
||
| 476 | { |
||
| 477 | $this->question = $title; |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param int $id |
||
| 482 | */ |
||
| 483 | public function updateParentId($id) |
||
| 484 | { |
||
| 485 | $this->parent_id = (int) $id; |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * changes the question description. |
||
| 490 | * |
||
| 491 | * @param string $description - question description |
||
| 492 | * |
||
| 493 | * @author Olivier Brouckaert |
||
| 494 | */ |
||
| 495 | public function updateDescription($description) |
||
| 496 | { |
||
| 497 | $this->description = $description; |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * changes the question weighting. |
||
| 502 | * |
||
| 503 | * @param int $weighting - question weighting |
||
| 504 | * |
||
| 505 | * @author Olivier Brouckaert |
||
| 506 | */ |
||
| 507 | public function updateWeighting($weighting) |
||
| 508 | { |
||
| 509 | $this->weighting = $weighting; |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param array $category |
||
| 514 | * |
||
| 515 | * @author Hubert Borderiou 12-10-2011 |
||
| 516 | */ |
||
| 517 | public function updateCategory($category) |
||
| 518 | { |
||
| 519 | $this->category = $category; |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param int $value |
||
| 524 | * |
||
| 525 | * @author Hubert Borderiou 12-10-2011 |
||
| 526 | */ |
||
| 527 | public function updateScoreAlwaysPositive($value) |
||
| 528 | { |
||
| 529 | $this->scoreAlwaysPositive = $value; |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param int $value |
||
| 534 | * |
||
| 535 | * @author Hubert Borderiou 12-10-2011 |
||
| 536 | */ |
||
| 537 | public function updateUncheckedMayScore($value) |
||
| 538 | { |
||
| 539 | $this->uncheckedMayScore = $value; |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Save category of a question. |
||
| 544 | * |
||
| 545 | * A question can have n categories if category is empty, |
||
| 546 | * then question has no category then delete the category entry |
||
| 547 | * |
||
| 548 | * @param array $category_list |
||
| 549 | * |
||
| 550 | * @author Julio Montoya - Adding multiple cat support |
||
| 551 | */ |
||
| 552 | public function saveCategories($category_list) |
||
| 553 | { |
||
| 554 | if (!empty($category_list)) { |
||
| 555 | $this->deleteCategory(); |
||
| 556 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); |
||
| 557 | |||
| 558 | // update or add category for a question |
||
| 559 | foreach ($category_list as $category_id) { |
||
| 560 | $category_id = (int) $category_id; |
||
| 561 | $question_id = (int) $this->id; |
||
| 562 | $sql = "SELECT count(*) AS nb |
||
| 563 | FROM $table |
||
| 564 | WHERE |
||
| 565 | category_id = $category_id |
||
| 566 | AND question_id = $question_id |
||
| 567 | AND c_id=".api_get_course_int_id(); |
||
| 568 | $res = Database::query($sql); |
||
| 569 | $row = Database::fetch_array($res); |
||
| 570 | if ($row['nb'] > 0) { |
||
| 571 | // DO nothing |
||
| 572 | } else { |
||
| 573 | $sql = "INSERT INTO $table (c_id, question_id, category_id) |
||
| 574 | VALUES (".api_get_course_int_id().", $question_id, $category_id)"; |
||
| 575 | Database::query($sql); |
||
| 576 | } |
||
| 577 | } |
||
| 578 | } |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * in this version, a question can only have 1 category |
||
| 583 | * if category is 0, then question has no category then delete the category entry. |
||
| 584 | * |
||
| 585 | * @param int $categoryId |
||
| 586 | * @param int $courseId |
||
| 587 | * |
||
| 588 | * @return bool |
||
| 589 | * |
||
| 590 | * @author Hubert Borderiou 12-10-2011 |
||
| 591 | */ |
||
| 592 | public function saveCategory($categoryId, $courseId = 0) |
||
| 593 | { |
||
| 594 | $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId; |
||
| 595 | |||
| 596 | if (empty($courseId)) { |
||
| 597 | return false; |
||
| 598 | } |
||
| 599 | |||
| 600 | if ($categoryId <= 0) { |
||
| 601 | $this->deleteCategory($courseId); |
||
| 602 | } else { |
||
| 603 | // update or add category for a question |
||
| 604 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); |
||
| 605 | $categoryId = (int) $categoryId; |
||
| 606 | $question_id = (int) $this->id; |
||
| 607 | $sql = "SELECT count(*) AS nb FROM $table |
||
| 608 | WHERE |
||
| 609 | question_id = $question_id AND |
||
| 610 | c_id = ".$courseId; |
||
| 611 | $res = Database::query($sql); |
||
| 612 | $row = Database::fetch_array($res); |
||
| 613 | if ($row['nb'] > 0) { |
||
| 614 | $sql = "UPDATE $table |
||
| 615 | SET category_id = $categoryId |
||
| 616 | WHERE |
||
| 617 | question_id = $question_id AND |
||
| 618 | c_id = ".$courseId; |
||
| 619 | Database::query($sql); |
||
| 620 | } else { |
||
| 621 | $sql = "INSERT INTO $table (c_id, question_id, category_id) |
||
| 622 | VALUES (".$courseId.", $question_id, $categoryId)"; |
||
| 623 | Database::query($sql); |
||
| 624 | } |
||
| 625 | |||
| 626 | return true; |
||
| 627 | } |
||
| 628 | } |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @author hubert borderiou 12-10-2011 |
||
| 632 | * |
||
| 633 | * @param int $courseId |
||
| 634 | * delete any category entry for question id |
||
| 635 | * delete the category for question |
||
| 636 | * |
||
| 637 | * @return bool |
||
| 638 | */ |
||
| 639 | public function deleteCategory($courseId = 0) |
||
| 640 | { |
||
| 641 | $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId; |
||
| 642 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); |
||
| 643 | $questionId = (int) $this->id; |
||
| 644 | if (empty($courseId) || empty($questionId)) { |
||
| 645 | return false; |
||
| 646 | } |
||
| 647 | $sql = "DELETE FROM $table |
||
| 648 | WHERE |
||
| 649 | question_id = $questionId AND |
||
| 650 | c_id = ".$courseId; |
||
| 651 | Database::query($sql); |
||
| 652 | |||
| 653 | return true; |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * changes the question position. |
||
| 658 | * |
||
| 659 | * @param int $position - question position |
||
| 660 | * |
||
| 661 | * @author Olivier Brouckaert |
||
| 662 | */ |
||
| 663 | public function updatePosition($position) |
||
| 664 | { |
||
| 665 | $this->position = $position; |
||
| 666 | } |
||
| 667 | |||
| 668 | /** |
||
| 669 | * changes the question level. |
||
| 670 | * |
||
| 671 | * @param int $level - question level |
||
| 672 | * |
||
| 673 | * @author Nicolas Raynaud |
||
| 674 | */ |
||
| 675 | public function updateLevel($level) |
||
| 676 | { |
||
| 677 | $this->level = $level; |
||
| 678 | } |
||
| 679 | |||
| 680 | /** |
||
| 681 | * changes the answer type. If the user changes the type from "unique answer" to "multiple answers" |
||
| 682 | * (or conversely) answers are not deleted, otherwise yes. |
||
| 683 | * |
||
| 684 | * @param int $type - answer type |
||
| 685 | * |
||
| 686 | * @author Olivier Brouckaert |
||
| 687 | */ |
||
| 688 | public function updateType($type) |
||
| 689 | { |
||
| 690 | $table = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 691 | $course_id = $this->course['real_id']; |
||
| 692 | |||
| 693 | if (empty($course_id)) { |
||
| 694 | $course_id = api_get_course_int_id(); |
||
| 695 | } |
||
| 696 | // if we really change the type |
||
| 697 | if ($type != $this->type) { |
||
| 698 | // if we don't change from "unique answer" to "multiple answers" (or conversely) |
||
| 699 | if (!in_array($this->type, [UNIQUE_ANSWER, MULTIPLE_ANSWER]) || |
||
| 700 | !in_array($type, [UNIQUE_ANSWER, MULTIPLE_ANSWER]) |
||
| 701 | ) { |
||
| 702 | // removes old answers |
||
| 703 | $sql = "DELETE FROM $table |
||
| 704 | WHERE c_id = $course_id AND question_id = ".intval($this->id); |
||
| 705 | Database::query($sql); |
||
| 706 | } |
||
| 707 | |||
| 708 | $this->type = $type; |
||
| 709 | } |
||
| 710 | } |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Get default hot spot folder in documents. |
||
| 714 | * |
||
| 715 | * @param array $courseInfo |
||
| 716 | * |
||
| 717 | * @return CDocument |
||
| 718 | */ |
||
| 719 | public function getHotSpotFolderInCourse($courseInfo = []) |
||
| 720 | { |
||
| 721 | $courseInfo = empty($courseInfo) ? $this->course : $courseInfo; |
||
| 722 | |||
| 723 | if (empty($courseInfo) || empty($courseInfo['directory'])) { |
||
| 724 | // Stop everything if course is not set. |
||
| 725 | api_not_allowed(); |
||
| 726 | } |
||
| 727 | |||
| 728 | $pictureAbsolutePath = api_get_path(SYS_COURSE_PATH).$courseInfo['directory'].'/document/images/'; |
||
| 729 | $picturePath = basename($pictureAbsolutePath); |
||
| 730 | |||
| 731 | $folder = create_unexisting_directory( |
||
| 732 | $courseInfo, |
||
| 733 | api_get_user_id(), |
||
| 734 | 0, |
||
| 735 | 0, |
||
| 736 | 0, |
||
| 737 | dirname($pictureAbsolutePath), |
||
| 738 | '/'.$picturePath, |
||
| 739 | $picturePath, |
||
| 740 | '', |
||
| 741 | false, |
||
| 742 | false |
||
| 743 | ); |
||
| 744 | |||
| 745 | return $folder; |
||
| 746 | } |
||
| 747 | |||
| 748 | /** |
||
| 749 | * adds a picture to the question. |
||
| 750 | * |
||
| 751 | * @param array $picture - picture to upload |
||
| 752 | * |
||
| 753 | * @return bool - true if uploaded, otherwise false |
||
| 754 | * |
||
| 755 | */ |
||
| 756 | public function uploadPicture($picture) |
||
| 784 | } |
||
| 785 | |||
| 786 | /** |
||
| 787 | * return the name for image use in hotspot question |
||
| 788 | * to be unique, name is quiz-[utc unix timestamp].jpg. |
||
| 789 | * |
||
| 790 | * @param string $prefix |
||
| 791 | * @param string $extension |
||
| 792 | * |
||
| 793 | * @return string |
||
| 794 | */ |
||
| 795 | public function generatePictureName($prefix = 'quiz-', $extension = 'jpg') |
||
| 796 | { |
||
| 797 | // image name is quiz-xxx.jpg in folder images/ |
||
| 798 | $utcTime = time(); |
||
| 799 | |||
| 800 | return $prefix.$utcTime.'.'.$extension; |
||
| 801 | } |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Deletes a hot spot picture. |
||
| 805 | * |
||
| 806 | * @return bool - true |
||
| 807 | */ |
||
| 808 | public function removePicture() |
||
| 809 | { |
||
| 810 | $picture = $this->getPicture(); |
||
| 811 | |||
| 812 | if ($picture) { |
||
| 813 | $manager = Database::getManager(); |
||
| 814 | $manager->remove($picture); |
||
| 815 | $manager->flush(); |
||
| 816 | |||
| 817 | return true; |
||
| 818 | } |
||
| 819 | |||
| 820 | return false; |
||
| 821 | } |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Exports a picture to another question. |
||
| 825 | * |
||
| 826 | * @author Olivier Brouckaert |
||
| 827 | * |
||
| 828 | * @param int $questionId - ID of the target question |
||
| 829 | * @param array $courseInfo destination course info |
||
| 830 | * |
||
| 831 | * @return bool - true if copied, otherwise false |
||
| 832 | */ |
||
| 833 | public function exportPicture($questionId, $courseInfo) |
||
| 834 | { |
||
| 835 | if (empty($questionId) || empty($courseInfo)) { |
||
| 836 | return false; |
||
| 837 | } |
||
| 838 | |||
| 839 | $course_id = $courseInfo['real_id']; |
||
| 840 | $destination_path = $this->getHotSpotFolderInCourse($courseInfo); |
||
| 841 | |||
| 842 | if (empty($destination_path)) { |
||
| 843 | return false; |
||
| 844 | } |
||
| 845 | |||
| 846 | $source_path = $this->getHotSpotFolderInCourse(); |
||
| 847 | |||
| 848 | // if the question has got an ID and if the picture exists |
||
| 849 | if (!$this->id || empty($this->picture)) { |
||
| 850 | return false; |
||
| 851 | } |
||
| 852 | |||
| 853 | $sourcePictureName = $this->getPictureFilename($course_id); |
||
| 854 | $picture = $this->generatePictureName(); |
||
| 855 | $result = false; |
||
| 856 | if (file_exists($source_path.'/'.$sourcePictureName)) { |
||
| 857 | // for backward compatibility |
||
| 858 | $result = copy( |
||
| 859 | $source_path.'/'.$sourcePictureName, |
||
| 860 | $destination_path.'/'.$picture |
||
| 861 | ); |
||
| 862 | } else { |
||
| 863 | $imageInfo = DocumentManager::get_document_data_by_id( |
||
| 864 | $this->picture, |
||
| 865 | $courseInfo['code'] |
||
| 866 | ); |
||
| 867 | if (file_exists($imageInfo['absolute_path'])) { |
||
| 868 | $result = @copy( |
||
| 869 | $imageInfo['absolute_path'], |
||
| 870 | $destination_path.'/'.$picture |
||
| 871 | ); |
||
| 872 | } |
||
| 873 | } |
||
| 874 | |||
| 875 | // If copy was correct then add to the database |
||
| 876 | if (!$result) { |
||
| 877 | return false; |
||
| 878 | } |
||
| 879 | |||
| 880 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 881 | $sql = "UPDATE $table SET |
||
| 882 | picture = '".Database::escape_string($picture)."' |
||
| 883 | WHERE c_id = $course_id AND id='".intval($questionId)."'"; |
||
| 884 | Database::query($sql); |
||
| 885 | |||
| 886 | $documentId = DocumentManager::addDocument( |
||
| 887 | $courseInfo, |
||
| 888 | '/images/'.$picture, |
||
| 889 | 'file', |
||
| 890 | filesize($destination_path.'/'.$picture), |
||
| 891 | $picture |
||
| 892 | ); |
||
| 893 | |||
| 894 | if (!$documentId) { |
||
| 895 | return false; |
||
| 896 | } |
||
| 897 | |||
| 898 | return true; |
||
| 899 | } |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Set title. |
||
| 903 | * |
||
| 904 | * @param string $title |
||
| 905 | */ |
||
| 906 | public function setTitle($title) |
||
| 907 | { |
||
| 908 | $this->question = $title; |
||
| 909 | } |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Sets extra info. |
||
| 913 | * |
||
| 914 | * @param string $extra |
||
| 915 | */ |
||
| 916 | public function setExtra($extra) |
||
| 917 | { |
||
| 918 | $this->extra = $extra; |
||
| 919 | } |
||
| 920 | |||
| 921 | /** |
||
| 922 | * updates the question in the data base |
||
| 923 | * if an exercise ID is provided, we add that exercise ID into the exercise list. |
||
| 924 | * |
||
| 925 | * @author Olivier Brouckaert |
||
| 926 | * |
||
| 927 | * @param Exercise $exercise |
||
| 928 | */ |
||
| 929 | public function save($exercise) |
||
| 930 | { |
||
| 931 | $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 932 | $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 933 | $em = Database::getManager(); |
||
| 934 | $exerciseId = $exercise->id; |
||
| 935 | |||
| 936 | $id = $this->id; |
||
| 937 | $question = $this->question; |
||
| 938 | $description = $this->description; |
||
| 939 | $weighting = $this->weighting; |
||
| 940 | $position = $this->position; |
||
| 941 | $type = $this->type; |
||
| 942 | $picture = $this->picture; |
||
| 943 | $level = $this->level; |
||
| 944 | $extra = $this->extra; |
||
| 945 | $c_id = $this->course['real_id']; |
||
| 946 | $categoryId = $this->category; |
||
| 947 | |||
| 948 | // question already exists |
||
| 949 | if (!empty($id)) { |
||
| 950 | $params = [ |
||
| 951 | 'question' => $question, |
||
| 952 | 'description' => $description, |
||
| 953 | 'ponderation' => $weighting, |
||
| 954 | 'position' => $position, |
||
| 955 | 'type' => $type, |
||
| 956 | 'picture' => $picture, |
||
| 957 | 'extra' => $extra, |
||
| 958 | 'level' => $level, |
||
| 959 | ]; |
||
| 960 | if ($exercise->questionFeedbackEnabled) { |
||
| 961 | $params['feedback'] = $this->feedback; |
||
| 962 | } |
||
| 963 | Database::update( |
||
| 964 | $TBL_QUESTIONS, |
||
| 965 | $params, |
||
| 966 | ['c_id = ? AND id = ?' => [$c_id, $id]] |
||
| 967 | ); |
||
| 968 | |||
| 969 | Event::addEvent( |
||
| 970 | LOG_QUESTION_UPDATED, |
||
| 971 | LOG_QUESTION_ID, |
||
| 972 | $this->iid |
||
| 973 | ); |
||
| 974 | $this->saveCategory($categoryId); |
||
| 975 | |||
| 976 | if (!empty($exerciseId)) { |
||
| 977 | api_item_property_update( |
||
| 978 | $this->course, |
||
| 979 | TOOL_QUIZ, |
||
| 980 | $id, |
||
| 981 | 'QuizQuestionUpdated', |
||
| 982 | api_get_user_id() |
||
| 983 | ); |
||
| 984 | } |
||
| 985 | if (api_get_setting('search_enabled') == 'true') { |
||
| 986 | if ($exerciseId != 0) { |
||
| 987 | $this->search_engine_edit($exerciseId); |
||
| 988 | } else { |
||
| 989 | /** |
||
| 990 | * actually there is *not* an user interface for |
||
| 991 | * creating questions without a relation with an exercise. |
||
| 992 | */ |
||
| 993 | } |
||
| 994 | } |
||
| 995 | } else { |
||
| 996 | // creates a new question |
||
| 997 | $sql = "SELECT max(position) |
||
| 998 | FROM $TBL_QUESTIONS as question, |
||
| 999 | $TBL_EXERCISE_QUESTION as test_question |
||
| 1000 | WHERE |
||
| 1001 | question.id = test_question.question_id AND |
||
| 1002 | test_question.exercice_id = ".intval($exerciseId)." AND |
||
| 1003 | question.c_id = $c_id AND |
||
| 1004 | test_question.c_id = $c_id "; |
||
| 1005 | $result = Database::query($sql); |
||
| 1006 | $current_position = Database::result($result, 0, 0); |
||
| 1007 | $this->updatePosition($current_position + 1); |
||
| 1008 | $position = $this->position; |
||
| 1009 | |||
| 1010 | $params = [ |
||
| 1011 | 'c_id' => $c_id, |
||
| 1012 | 'question' => $question, |
||
| 1013 | 'description' => $description, |
||
| 1014 | 'ponderation' => $weighting, |
||
| 1015 | 'position' => $position, |
||
| 1016 | 'type' => $type, |
||
| 1017 | 'picture' => $picture, |
||
| 1018 | 'extra' => $extra, |
||
| 1019 | 'level' => $level, |
||
| 1020 | ]; |
||
| 1021 | |||
| 1022 | if ($exercise->questionFeedbackEnabled) { |
||
| 1023 | $params['feedback'] = $this->feedback; |
||
| 1024 | } |
||
| 1025 | $this->id = Database::insert($TBL_QUESTIONS, $params); |
||
| 1026 | |||
| 1027 | if ($this->id) { |
||
| 1028 | $sql = "UPDATE $TBL_QUESTIONS SET id = iid WHERE iid = {$this->id}"; |
||
| 1029 | Database::query($sql); |
||
| 1030 | |||
| 1031 | Event::addEvent( |
||
| 1032 | LOG_QUESTION_CREATED, |
||
| 1033 | LOG_QUESTION_ID, |
||
| 1034 | $this->id |
||
| 1035 | ); |
||
| 1036 | |||
| 1037 | api_item_property_update( |
||
| 1038 | $this->course, |
||
| 1039 | TOOL_QUIZ, |
||
| 1040 | $this->id, |
||
| 1041 | 'QuizQuestionAdded', |
||
| 1042 | api_get_user_id() |
||
| 1043 | ); |
||
| 1044 | |||
| 1045 | // If hotspot, create first answer |
||
| 1046 | if ($type == HOT_SPOT || $type == HOT_SPOT_ORDER) { |
||
| 1047 | $quizAnswer = new CQuizAnswer(); |
||
| 1048 | $quizAnswer |
||
| 1049 | ->setCId($c_id) |
||
| 1050 | ->setQuestionId($this->id) |
||
| 1051 | ->setAnswer('') |
||
| 1052 | ->setPonderation(10) |
||
| 1053 | ->setPosition(1) |
||
| 1054 | ->setHotspotCoordinates('0;0|0|0') |
||
| 1055 | ->setHotspotType('square'); |
||
| 1056 | |||
| 1057 | $em->persist($quizAnswer); |
||
| 1058 | $em->flush(); |
||
| 1059 | |||
| 1060 | $id = $quizAnswer->getIid(); |
||
| 1061 | |||
| 1062 | if ($id) { |
||
| 1063 | $quizAnswer |
||
| 1064 | ->setId($id) |
||
| 1065 | ->setIdAuto($id); |
||
| 1066 | |||
| 1067 | $em->merge($quizAnswer); |
||
| 1068 | $em->flush(); |
||
| 1069 | } |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | if ($type == HOT_SPOT_DELINEATION) { |
||
| 1073 | $quizAnswer = new CQuizAnswer(); |
||
| 1074 | $quizAnswer |
||
| 1075 | ->setCId($c_id) |
||
| 1076 | ->setQuestionId($this->id) |
||
| 1077 | ->setAnswer('') |
||
| 1078 | ->setPonderation(10) |
||
| 1079 | ->setPosition(1) |
||
| 1080 | ->setHotspotCoordinates('0;0|0|0') |
||
| 1081 | ->setHotspotType('delineation'); |
||
| 1082 | |||
| 1083 | $em->persist($quizAnswer); |
||
| 1084 | $em->flush(); |
||
| 1085 | |||
| 1086 | $id = $quizAnswer->getIid(); |
||
| 1087 | |||
| 1088 | if ($id) { |
||
| 1089 | $quizAnswer |
||
| 1090 | ->setId($id) |
||
| 1091 | ->setIdAuto($id); |
||
| 1092 | |||
| 1093 | $em->merge($quizAnswer); |
||
| 1094 | $em->flush(); |
||
| 1095 | } |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | if (api_get_setting('search_enabled') == 'true') { |
||
| 1099 | if ($exerciseId != 0) { |
||
| 1100 | $this->search_engine_edit($exerciseId, true); |
||
| 1101 | } else { |
||
| 1102 | /** |
||
| 1103 | * actually there is *not* an user interface for |
||
| 1104 | * creating questions without a relation with an exercise. |
||
| 1105 | */ |
||
| 1106 | } |
||
| 1107 | } |
||
| 1108 | } |
||
| 1109 | } |
||
| 1110 | |||
| 1111 | // if the question is created in an exercise |
||
| 1112 | if ($exerciseId) { |
||
| 1113 | // adds the exercise into the exercise list of this question |
||
| 1114 | $this->addToList($exerciseId, true); |
||
| 1115 | } |
||
| 1116 | } |
||
| 1117 | |||
| 1118 | public function search_engine_edit( |
||
| 1251 | } |
||
| 1252 | } |
||
| 1253 | } |
||
| 1254 | } |
||
| 1255 | } |
||
| 1256 | |||
| 1257 | /** |
||
| 1258 | * adds an exercise into the exercise list. |
||
| 1259 | * |
||
| 1260 | * @author Olivier Brouckaert |
||
| 1261 | * |
||
| 1262 | * @param int $exerciseId - exercise ID |
||
| 1263 | * @param bool $fromSave - from $this->save() or not |
||
| 1264 | */ |
||
| 1265 | public function addToList($exerciseId, $fromSave = false) |
||
| 1266 | { |
||
| 1267 | $exerciseRelQuestionTable = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 1268 | $id = $this->id; |
||
| 1269 | // checks if the exercise ID is not in the list |
||
| 1270 | if (!in_array($exerciseId, $this->exerciseList)) { |
||
| 1271 | $this->exerciseList[] = $exerciseId; |
||
| 1272 | $new_exercise = new Exercise(); |
||
| 1273 | $new_exercise->read($exerciseId); |
||
| 1274 | $count = $new_exercise->selectNbrQuestions(); |
||
| 1275 | $count++; |
||
| 1276 | $sql = "INSERT INTO $exerciseRelQuestionTable (c_id, question_id, exercice_id, question_order) |
||
| 1277 | VALUES ({$this->course['real_id']}, ".intval($id).", ".intval($exerciseId).", '$count')"; |
||
| 1278 | Database::query($sql); |
||
| 1279 | |||
| 1280 | // we do not want to reindex if we had just saved adnd indexed the question |
||
| 1281 | if (!$fromSave) { |
||
| 1282 | $this->search_engine_edit($exerciseId, true); |
||
| 1283 | } |
||
| 1284 | } |
||
| 1285 | } |
||
| 1286 | |||
| 1287 | /** |
||
| 1288 | * removes an exercise from the exercise list. |
||
| 1289 | * |
||
| 1290 | * @author Olivier Brouckaert |
||
| 1291 | * |
||
| 1292 | * @param int $exerciseId - exercise ID |
||
| 1293 | * |
||
| 1294 | * @return bool - true if removed, otherwise false |
||
| 1295 | */ |
||
| 1296 | public function removeFromList($exerciseId) |
||
| 1297 | { |
||
| 1298 | $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 1299 | $id = $this->id; |
||
| 1300 | |||
| 1301 | // searches the position of the exercise ID in the list |
||
| 1302 | $pos = array_search($exerciseId, $this->exerciseList); |
||
| 1303 | $course_id = api_get_course_int_id(); |
||
| 1304 | |||
| 1305 | // exercise not found |
||
| 1306 | if ($pos === false) { |
||
| 1307 | return false; |
||
| 1308 | } else { |
||
| 1309 | // deletes the position in the array containing the wanted exercise ID |
||
| 1310 | unset($this->exerciseList[$pos]); |
||
| 1311 | //update order of other elements |
||
| 1312 | $sql = "SELECT question_order |
||
| 1313 | FROM $TBL_EXERCISE_QUESTION |
||
| 1314 | WHERE |
||
| 1315 | c_id = $course_id |
||
| 1316 | AND question_id = ".intval($id)." |
||
| 1317 | AND exercice_id = ".intval($exerciseId); |
||
| 1318 | $res = Database::query($sql); |
||
| 1319 | if (Database::num_rows($res) > 0) { |
||
| 1320 | $row = Database::fetch_array($res); |
||
| 1321 | if (!empty($row['question_order'])) { |
||
| 1322 | $sql = "UPDATE $TBL_EXERCISE_QUESTION |
||
| 1323 | SET question_order = question_order-1 |
||
| 1324 | WHERE |
||
| 1325 | c_id = $course_id |
||
| 1326 | AND exercice_id = ".intval($exerciseId)." |
||
| 1327 | AND question_order > ".$row['question_order']; |
||
| 1328 | Database::query($sql); |
||
| 1329 | } |
||
| 1330 | } |
||
| 1331 | |||
| 1332 | $sql = "DELETE FROM $TBL_EXERCISE_QUESTION |
||
| 1333 | WHERE |
||
| 1334 | c_id = $course_id |
||
| 1335 | AND question_id = ".intval($id)." |
||
| 1336 | AND exercice_id = ".intval($exerciseId); |
||
| 1337 | Database::query($sql); |
||
| 1338 | |||
| 1339 | return true; |
||
| 1340 | } |
||
| 1341 | } |
||
| 1342 | |||
| 1343 | /** |
||
| 1344 | * Deletes a question from the database |
||
| 1345 | * the parameter tells if the question is removed from all exercises (value = 0), |
||
| 1346 | * or just from one exercise (value = exercise ID). |
||
| 1347 | * |
||
| 1348 | * @author Olivier Brouckaert |
||
| 1349 | * |
||
| 1350 | * @param int $deleteFromEx - exercise ID if the question is only removed from one exercise |
||
| 1351 | */ |
||
| 1352 | public function delete($deleteFromEx = 0) |
||
| 1353 | { |
||
| 1354 | $course_id = api_get_course_int_id(); |
||
| 1355 | |||
| 1356 | $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 1357 | $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 1358 | $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 1359 | $TBL_QUIZ_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); |
||
| 1360 | |||
| 1361 | $id = (int) $this->id; |
||
| 1362 | |||
| 1363 | // if the question must be removed from all exercises |
||
| 1364 | if (!$deleteFromEx) { |
||
| 1365 | //update the question_order of each question to avoid inconsistencies |
||
| 1366 | $sql = "SELECT exercice_id, question_order |
||
| 1367 | FROM $TBL_EXERCISE_QUESTION |
||
| 1368 | WHERE c_id = $course_id AND question_id = ".intval($id); |
||
| 1369 | |||
| 1370 | $res = Database::query($sql); |
||
| 1371 | if (Database::num_rows($res) > 0) { |
||
| 1372 | while ($row = Database::fetch_array($res)) { |
||
| 1373 | if (!empty($row['question_order'])) { |
||
| 1374 | $sql = "UPDATE $TBL_EXERCISE_QUESTION |
||
| 1375 | SET question_order = question_order-1 |
||
| 1376 | WHERE |
||
| 1377 | c_id = $course_id AND |
||
| 1378 | exercice_id = ".intval($row['exercice_id'])." AND |
||
| 1379 | question_order > ".$row['question_order']; |
||
| 1380 | Database::query($sql); |
||
| 1381 | } |
||
| 1382 | } |
||
| 1383 | } |
||
| 1384 | |||
| 1385 | $sql = "DELETE FROM $TBL_EXERCISE_QUESTION |
||
| 1386 | WHERE c_id = $course_id AND question_id = ".$id; |
||
| 1387 | Database::query($sql); |
||
| 1388 | |||
| 1389 | $sql = "DELETE FROM $TBL_QUESTIONS |
||
| 1390 | WHERE c_id = $course_id AND id = ".$id; |
||
| 1391 | Database::query($sql); |
||
| 1392 | |||
| 1393 | $sql = "DELETE FROM $TBL_REPONSES |
||
| 1394 | WHERE c_id = $course_id AND question_id = ".$id; |
||
| 1395 | Database::query($sql); |
||
| 1396 | |||
| 1397 | // remove the category of this question in the question_rel_category table |
||
| 1398 | $sql = "DELETE FROM $TBL_QUIZ_QUESTION_REL_CATEGORY |
||
| 1399 | WHERE |
||
| 1400 | c_id = $course_id AND |
||
| 1401 | question_id = ".$id; |
||
| 1402 | Database::query($sql); |
||
| 1403 | |||
| 1404 | api_item_property_update( |
||
| 1405 | $this->course, |
||
| 1406 | TOOL_QUIZ, |
||
| 1407 | $id, |
||
| 1408 | 'QuizQuestionDeleted', |
||
| 1409 | api_get_user_id() |
||
| 1410 | ); |
||
| 1411 | $this->removePicture(); |
||
| 1412 | } else { |
||
| 1413 | // just removes the exercise from the list |
||
| 1414 | $this->removeFromList($deleteFromEx); |
||
| 1415 | if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) { |
||
| 1416 | // disassociate question with this exercise |
||
| 1417 | $this->search_engine_edit($deleteFromEx, false, true); |
||
| 1418 | } |
||
| 1419 | |||
| 1420 | api_item_property_update( |
||
| 1421 | $this->course, |
||
| 1422 | TOOL_QUIZ, |
||
| 1423 | $id, |
||
| 1424 | 'QuizQuestionDeleted', |
||
| 1425 | api_get_user_id() |
||
| 1426 | ); |
||
| 1427 | } |
||
| 1428 | } |
||
| 1429 | |||
| 1430 | /** |
||
| 1431 | * Duplicates the question. |
||
| 1432 | * |
||
| 1433 | * @author Olivier Brouckaert |
||
| 1434 | * |
||
| 1435 | * @param array $courseInfo Course info of the destination course |
||
| 1436 | * |
||
| 1437 | * @return false|string ID of the new question |
||
| 1438 | */ |
||
| 1439 | public function duplicate($courseInfo = []) |
||
| 1440 | { |
||
| 1441 | $courseInfo = empty($courseInfo) ? $this->course : $courseInfo; |
||
| 1442 | |||
| 1443 | if (empty($courseInfo)) { |
||
| 1444 | return false; |
||
| 1445 | } |
||
| 1446 | $questionTable = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 1447 | $TBL_QUESTION_OPTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); |
||
| 1448 | |||
| 1449 | $question = $this->question; |
||
| 1450 | $description = $this->description; |
||
| 1451 | $weighting = $this->weighting; |
||
| 1452 | $position = $this->position; |
||
| 1453 | $type = $this->type; |
||
| 1454 | $level = (int) $this->level; |
||
| 1455 | $extra = $this->extra; |
||
| 1456 | |||
| 1457 | // Using the same method used in the course copy to transform URLs |
||
| 1458 | if ($this->course['id'] != $courseInfo['id']) { |
||
| 1459 | $description = DocumentManager::replaceUrlWithNewCourseCode( |
||
| 1460 | $description, |
||
| 1461 | $this->course['code'], |
||
| 1462 | $courseInfo['id'] |
||
| 1463 | ); |
||
| 1464 | $question = DocumentManager::replaceUrlWithNewCourseCode( |
||
| 1465 | $question, |
||
| 1466 | $this->course['code'], |
||
| 1467 | $courseInfo['id'] |
||
| 1468 | ); |
||
| 1469 | } |
||
| 1470 | |||
| 1471 | $course_id = $courseInfo['real_id']; |
||
| 1472 | |||
| 1473 | // Read the source options |
||
| 1474 | $options = self::readQuestionOption($this->id, $this->course['real_id']); |
||
| 1475 | |||
| 1476 | // Inserting in the new course db / or the same course db |
||
| 1477 | $params = [ |
||
| 1478 | 'c_id' => $course_id, |
||
| 1479 | 'question' => $question, |
||
| 1480 | 'description' => $description, |
||
| 1481 | 'ponderation' => $weighting, |
||
| 1482 | 'position' => $position, |
||
| 1483 | 'type' => $type, |
||
| 1484 | 'level' => $level, |
||
| 1485 | 'extra' => $extra, |
||
| 1486 | ]; |
||
| 1487 | $newQuestionId = Database::insert($questionTable, $params); |
||
| 1488 | |||
| 1489 | if ($newQuestionId) { |
||
| 1490 | $sql = "UPDATE $questionTable |
||
| 1491 | SET id = iid |
||
| 1492 | WHERE iid = $newQuestionId"; |
||
| 1493 | Database::query($sql); |
||
| 1494 | |||
| 1495 | if (!empty($options)) { |
||
| 1496 | // Saving the quiz_options |
||
| 1497 | foreach ($options as $item) { |
||
| 1498 | $item['question_id'] = $newQuestionId; |
||
| 1499 | $item['c_id'] = $course_id; |
||
| 1500 | unset($item['id']); |
||
| 1501 | unset($item['iid']); |
||
| 1502 | $id = Database::insert($TBL_QUESTION_OPTIONS, $item); |
||
| 1503 | if ($id) { |
||
| 1504 | $sql = "UPDATE $TBL_QUESTION_OPTIONS |
||
| 1505 | SET id = iid |
||
| 1506 | WHERE iid = $id"; |
||
| 1507 | Database::query($sql); |
||
| 1508 | } |
||
| 1509 | } |
||
| 1510 | } |
||
| 1511 | |||
| 1512 | // Duplicates the picture of the hotspot |
||
| 1513 | $this->exportPicture($newQuestionId, $courseInfo); |
||
| 1514 | } |
||
| 1515 | |||
| 1516 | return $newQuestionId; |
||
| 1517 | } |
||
| 1518 | |||
| 1519 | /** |
||
| 1520 | * @return string |
||
| 1521 | */ |
||
| 1522 | public function get_question_type_name() |
||
| 1523 | { |
||
| 1524 | $key = self::$questionTypes[$this->type]; |
||
| 1525 | |||
| 1526 | return get_lang($key[1]); |
||
| 1527 | } |
||
| 1528 | |||
| 1529 | /** |
||
| 1530 | * @param string $type |
||
| 1531 | */ |
||
| 1532 | public static function get_question_type($type) |
||
| 1533 | { |
||
| 1534 | if ($type == ORAL_EXPRESSION && api_get_setting('enable_record_audio') !== 'true') { |
||
| 1535 | return null; |
||
| 1536 | } |
||
| 1537 | |||
| 1538 | return self::$questionTypes[$type]; |
||
| 1539 | } |
||
| 1540 | |||
| 1541 | /** |
||
| 1542 | * @return array |
||
| 1543 | */ |
||
| 1544 | public static function get_question_type_list() |
||
| 1545 | { |
||
| 1546 | if (api_get_setting('enable_record_audio') !== 'true') { |
||
| 1547 | self::$questionTypes[ORAL_EXPRESSION] = null; |
||
| 1548 | unset(self::$questionTypes[ORAL_EXPRESSION]); |
||
| 1549 | } |
||
| 1550 | if (api_get_setting('enable_quiz_scenario') !== 'true') { |
||
| 1551 | self::$questionTypes[HOT_SPOT_DELINEATION] = null; |
||
| 1552 | unset(self::$questionTypes[HOT_SPOT_DELINEATION]); |
||
| 1553 | } |
||
| 1554 | |||
| 1555 | return self::$questionTypes; |
||
| 1556 | } |
||
| 1557 | |||
| 1558 | /** |
||
| 1559 | * Returns an instance of the class corresponding to the type. |
||
| 1560 | * |
||
| 1561 | * @param int $type the type of the question |
||
| 1562 | * |
||
| 1563 | * @return $this instance of a Question subclass (or of Questionc class by default) |
||
| 1564 | */ |
||
| 1565 | public static function getInstance($type) |
||
| 1579 | } |
||
| 1580 | |||
| 1581 | /** |
||
| 1582 | * Creates the form to create / edit a question |
||
| 1583 | * A subclass can redefine this function to add fields... |
||
| 1584 | * |
||
| 1585 | * @param FormValidator $form |
||
| 1586 | * @param Exercise $exercise |
||
| 1587 | */ |
||
| 1588 | public function createForm(&$form, $exercise) |
||
| 1589 | { |
||
| 1590 | echo '<style> |
||
| 1591 | .media { display:none;} |
||
| 1592 | </style>'; |
||
| 1593 | |||
| 1594 | // question name |
||
| 1595 | if (api_get_configuration_value('save_titles_as_html')) { |
||
| 1596 | $editorConfig = ['ToolbarSet' => 'Minimal']; |
||
| 1597 | $form->addHtmlEditor( |
||
| 1598 | 'questionName', |
||
| 1599 | get_lang('Question'), |
||
| 1600 | false, |
||
| 1601 | false, |
||
| 1602 | $editorConfig, |
||
| 1603 | true |
||
| 1604 | ); |
||
| 1605 | } else { |
||
| 1606 | $form->addElement('text', 'questionName', get_lang('Question')); |
||
| 1607 | } |
||
| 1608 | |||
| 1609 | $form->addRule('questionName', get_lang('GiveQuestion'), 'required'); |
||
| 1610 | |||
| 1611 | // default content |
||
| 1612 | $isContent = isset($_REQUEST['isContent']) ? intval($_REQUEST['isContent']) : null; |
||
| 1613 | |||
| 1614 | // Question type |
||
| 1615 | $answerType = isset($_REQUEST['answerType']) ? intval($_REQUEST['answerType']) : null; |
||
| 1616 | $form->addElement('hidden', 'answerType', $answerType); |
||
| 1617 | |||
| 1618 | // html editor |
||
| 1619 | $editorConfig = [ |
||
| 1620 | 'ToolbarSet' => 'TestQuestionDescription', |
||
| 1621 | 'Height' => '150', |
||
| 1622 | ]; |
||
| 1623 | |||
| 1624 | if (!api_is_allowed_to_edit(null, true)) { |
||
| 1625 | $editorConfig['UserStatus'] = 'student'; |
||
| 1626 | } |
||
| 1627 | |||
| 1628 | $form->addButtonAdvancedSettings('advanced_params'); |
||
| 1629 | $form->addElement('html', '<div id="advanced_params_options" style="display:none">'); |
||
| 1630 | $form->addHtmlEditor( |
||
| 1631 | 'questionDescription', |
||
| 1632 | get_lang('QuestionDescription'), |
||
| 1633 | false, |
||
| 1634 | false, |
||
| 1635 | $editorConfig |
||
| 1636 | ); |
||
| 1637 | |||
| 1638 | // hidden values |
||
| 1639 | $my_id = isset($_REQUEST['myid']) ? intval($_REQUEST['myid']) : null; |
||
| 1640 | $form->addElement('hidden', 'myid', $my_id); |
||
| 1641 | |||
| 1642 | if ($this->type != MEDIA_QUESTION) { |
||
| 1643 | // Advanced parameters |
||
| 1644 | $select_level = self::get_default_levels(); |
||
| 1645 | $form->addElement( |
||
| 1646 | 'select', |
||
| 1647 | 'questionLevel', |
||
| 1648 | get_lang('Difficulty'), |
||
| 1649 | $select_level |
||
| 1650 | ); |
||
| 1651 | |||
| 1652 | // Categories |
||
| 1653 | $tabCat = TestCategory::getCategoriesIdAndName(); |
||
| 1654 | $form->addElement( |
||
| 1655 | 'select', |
||
| 1656 | 'questionCategory', |
||
| 1657 | get_lang('Category'), |
||
| 1658 | $tabCat |
||
| 1659 | ); |
||
| 1660 | |||
| 1661 | global $text; |
||
| 1662 | |||
| 1663 | switch ($this->type) { |
||
| 1664 | case UNIQUE_ANSWER: |
||
| 1665 | $buttonGroup = []; |
||
| 1666 | $buttonGroup[] = $form->addButtonSave( |
||
| 1667 | $text, |
||
| 1668 | 'submitQuestion', |
||
| 1669 | true |
||
| 1670 | ); |
||
| 1671 | $buttonGroup[] = $form->addButton( |
||
| 1672 | 'convertAnswer', |
||
| 1673 | get_lang('ConvertToMultipleAnswer'), |
||
| 1674 | 'dot-circle-o', |
||
| 1675 | 'default', |
||
| 1676 | null, |
||
| 1677 | null, |
||
| 1678 | null, |
||
| 1679 | true |
||
| 1680 | ); |
||
| 1681 | $form->addGroup($buttonGroup); |
||
| 1682 | break; |
||
| 1683 | case MULTIPLE_ANSWER: |
||
| 1684 | $buttonGroup = []; |
||
| 1685 | $buttonGroup[] = $form->addButtonSave( |
||
| 1686 | $text, |
||
| 1687 | 'submitQuestion', |
||
| 1688 | true |
||
| 1689 | ); |
||
| 1690 | $buttonGroup[] = $form->addButton( |
||
| 1691 | 'convertAnswer', |
||
| 1692 | get_lang('ConvertToUniqueAnswer'), |
||
| 1693 | 'check-square-o', |
||
| 1694 | 'default', |
||
| 1695 | null, |
||
| 1696 | null, |
||
| 1697 | null, |
||
| 1698 | true |
||
| 1699 | ); |
||
| 1700 | $form->addGroup($buttonGroup); |
||
| 1701 | break; |
||
| 1702 | } |
||
| 1703 | //Medias |
||
| 1704 | //$course_medias = self::prepare_course_media_select(api_get_course_int_id()); |
||
| 1705 | //$form->addElement('select', 'parent_id', get_lang('AttachToMedia'), $course_medias); |
||
| 1706 | } |
||
| 1707 | |||
| 1708 | $form->addElement('html', '</div>'); |
||
| 1709 | |||
| 1710 | if (!isset($_GET['fromExercise'])) { |
||
| 1711 | switch ($answerType) { |
||
| 1712 | case 1: |
||
| 1713 | $this->question = get_lang('DefaultUniqueQuestion'); |
||
| 1714 | break; |
||
| 1715 | case 2: |
||
| 1716 | $this->question = get_lang('DefaultMultipleQuestion'); |
||
| 1717 | break; |
||
| 1718 | case 3: |
||
| 1719 | $this->question = get_lang('DefaultFillBlankQuestion'); |
||
| 1720 | break; |
||
| 1721 | case 4: |
||
| 1722 | $this->question = get_lang('DefaultMathingQuestion'); |
||
| 1723 | break; |
||
| 1724 | case 5: |
||
| 1725 | $this->question = get_lang('DefaultOpenQuestion'); |
||
| 1726 | break; |
||
| 1727 | case 9: |
||
| 1728 | $this->question = get_lang('DefaultMultipleQuestion'); |
||
| 1729 | break; |
||
| 1730 | } |
||
| 1731 | } |
||
| 1732 | |||
| 1733 | if (!is_null($exercise)) { |
||
| 1734 | if ($exercise->questionFeedbackEnabled && $this->showFeedback($exercise)) { |
||
| 1735 | $form->addTextarea('feedback', get_lang('FeedbackIfNotCorrect')); |
||
| 1736 | } |
||
| 1737 | } |
||
| 1738 | |||
| 1739 | // default values |
||
| 1740 | $defaults = []; |
||
| 1741 | $defaults['questionName'] = $this->question; |
||
| 1742 | $defaults['questionDescription'] = $this->description; |
||
| 1743 | $defaults['questionLevel'] = $this->level; |
||
| 1744 | $defaults['questionCategory'] = $this->category; |
||
| 1745 | $defaults['feedback'] = $this->feedback; |
||
| 1746 | |||
| 1747 | // Came from he question pool |
||
| 1748 | if (isset($_GET['fromExercise'])) { |
||
| 1749 | $form->setDefaults($defaults); |
||
| 1750 | } |
||
| 1751 | |||
| 1752 | if (!empty($_REQUEST['myid'])) { |
||
| 1753 | $form->setDefaults($defaults); |
||
| 1754 | } else { |
||
| 1755 | if ($isContent == 1) { |
||
| 1756 | $form->setDefaults($defaults); |
||
| 1757 | } |
||
| 1758 | } |
||
| 1759 | } |
||
| 1760 | |||
| 1761 | /** |
||
| 1762 | * function which process the creation of questions. |
||
| 1763 | * |
||
| 1764 | * @param FormValidator $form |
||
| 1765 | * @param Exercise $exercise |
||
| 1766 | */ |
||
| 1767 | public function processCreation($form, $exercise) |
||
| 1768 | { |
||
| 1769 | $this->updateTitle($form->getSubmitValue('questionName')); |
||
| 1770 | $this->updateDescription($form->getSubmitValue('questionDescription')); |
||
| 1771 | $this->updateLevel($form->getSubmitValue('questionLevel')); |
||
| 1772 | $this->updateCategory($form->getSubmitValue('questionCategory')); |
||
| 1773 | $this->setFeedback($form->getSubmitValue('feedback')); |
||
| 1774 | |||
| 1775 | //Save normal question if NOT media |
||
| 1776 | if ($this->type != MEDIA_QUESTION) { |
||
| 1777 | $this->save($exercise); |
||
| 1778 | |||
| 1779 | $exercise->addToList($this->id); |
||
| 1780 | $exercise->update_question_positions(); |
||
| 1781 | } |
||
| 1782 | } |
||
| 1783 | |||
| 1784 | /** |
||
| 1785 | * abstract function which creates the form to create / edit the answers of the question. |
||
| 1786 | * |
||
| 1787 | * @param FormValidator $form |
||
| 1788 | */ |
||
| 1789 | abstract public function createAnswersForm($form); |
||
| 1790 | |||
| 1791 | /** |
||
| 1792 | * abstract function which process the creation of answers. |
||
| 1793 | * |
||
| 1794 | * @param FormValidator $form |
||
| 1795 | * @param Exercise $exercise |
||
| 1796 | */ |
||
| 1797 | abstract public function processAnswersCreation($form, $exercise); |
||
| 1798 | |||
| 1799 | /** |
||
| 1800 | * Displays the menu of question types. |
||
| 1801 | * |
||
| 1802 | * @param Exercise $objExercise |
||
| 1803 | */ |
||
| 1804 | public static function displayTypeMenu($objExercise) |
||
| 1805 | { |
||
| 1806 | $feedback_type = $objExercise->feedback_type; |
||
| 1807 | $exerciseId = $objExercise->id; |
||
| 1808 | |||
| 1809 | // 1. by default we show all the question types |
||
| 1810 | $question_type_custom_list = self::get_question_type_list(); |
||
| 1811 | |||
| 1812 | if (!isset($feedback_type)) { |
||
| 1813 | $feedback_type = 0; |
||
| 1814 | } |
||
| 1815 | |||
| 1816 | if ($feedback_type == 1) { |
||
| 1817 | //2. but if it is a feedback DIRECT we only show the UNIQUE_ANSWER type that is currently available |
||
| 1818 | $question_type_custom_list = [ |
||
| 1819 | UNIQUE_ANSWER => self::$questionTypes[UNIQUE_ANSWER], |
||
| 1820 | HOT_SPOT_DELINEATION => self::$questionTypes[HOT_SPOT_DELINEATION], |
||
| 1821 | ]; |
||
| 1822 | } else { |
||
| 1823 | unset($question_type_custom_list[HOT_SPOT_DELINEATION]); |
||
| 1824 | } |
||
| 1825 | |||
| 1826 | echo '<div class="card card-exercise">'; |
||
| 1827 | echo '<div class="card-body">'; |
||
| 1828 | echo '<ul class="list-exercise">'; |
||
| 1829 | foreach ($question_type_custom_list as $i => $a_type) { |
||
| 1830 | // @todo remove require_once classes are already loaded using composer |
||
| 1831 | // include the class of the type |
||
| 1832 | require_once $a_type[0]; |
||
| 1833 | // get the picture of the type and the langvar which describes it |
||
| 1834 | $img = $explanation = ''; |
||
| 1835 | eval('$img = '.$a_type[1].'::$typePicture;'); |
||
| 1836 | eval('$explanation = get_lang('.$a_type[1].'::$explanationLangVar);'); |
||
| 1837 | echo '<li>'; |
||
| 1838 | echo '<div class="icon">'; |
||
| 1839 | $icon = '<a href="admin.php?'.api_get_cidreq().'&newQuestion=yes&answerType='.$i.'">'. |
||
| 1840 | Display::return_icon($img, $explanation, null, ICON_SIZE_BIG).'</a>'; |
||
| 1841 | |||
| 1842 | if ($objExercise->force_edit_exercise_in_lp === false) { |
||
| 1843 | if ($objExercise->exercise_was_added_in_lp == true) { |
||
| 1844 | $img = pathinfo($img); |
||
| 1845 | $img = $img['filename'].'_na.'.$img['extension']; |
||
| 1846 | $icon = Display::return_icon($img, $explanation, null, ICON_SIZE_BIG); |
||
| 1847 | } |
||
| 1848 | } |
||
| 1849 | |||
| 1850 | echo $icon; |
||
| 1851 | echo '</div>'; |
||
| 1852 | echo '</li>'; |
||
| 1853 | } |
||
| 1854 | |||
| 1855 | echo '<li>'; |
||
| 1856 | echo '<div class="content">'; |
||
| 1857 | if ($objExercise->exercise_was_added_in_lp == true) { |
||
| 1858 | echo Display::return_icon( |
||
| 1859 | 'database_na.png', |
||
| 1860 | get_lang('GetExistingQuestion'), |
||
| 1861 | null, |
||
| 1862 | ICON_SIZE_BIG |
||
| 1863 | ); |
||
| 1864 | } else { |
||
| 1865 | if ($feedback_type == 1) { |
||
| 1866 | echo $url = "<a href=\"question_pool.php?".api_get_cidreq()."&type=1&fromExercise=$exerciseId\">"; |
||
| 1867 | } else { |
||
| 1868 | echo $url = '<a href="question_pool.php?'.api_get_cidreq().'&fromExercise='.$exerciseId.'">'; |
||
| 1869 | } |
||
| 1870 | echo Display::return_icon( |
||
| 1871 | 'database.png', |
||
| 1872 | get_lang('GetExistingQuestion'), |
||
| 1873 | null, |
||
| 1874 | ICON_SIZE_BIG |
||
| 1875 | ); |
||
| 1876 | } |
||
| 1877 | echo '</a>'; |
||
| 1878 | echo '</div></li>'; |
||
| 1879 | echo '</ul>'; |
||
| 1880 | echo '</div>'; |
||
| 1881 | echo '</div>'; |
||
| 1882 | } |
||
| 1883 | |||
| 1884 | /** |
||
| 1885 | * @param int $question_id |
||
| 1886 | * @param string $name |
||
| 1887 | * @param int $course_id |
||
| 1888 | * @param int $position |
||
| 1889 | * |
||
| 1890 | * @return false|string |
||
| 1891 | */ |
||
| 1892 | public static function saveQuestionOption($question_id, $name, $course_id, $position = 0) |
||
| 1893 | { |
||
| 1894 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); |
||
| 1895 | $params['question_id'] = (int) $question_id; |
||
| 1896 | $params['name'] = $name; |
||
| 1897 | $params['position'] = $position; |
||
| 1898 | $params['c_id'] = $course_id; |
||
| 1899 | $result = self::readQuestionOption($question_id, $course_id); |
||
| 1900 | $last_id = Database::insert($table, $params); |
||
| 1901 | if ($last_id) { |
||
| 1902 | $sql = "UPDATE $table SET id = iid WHERE iid = $last_id"; |
||
| 1903 | Database::query($sql); |
||
| 1904 | } |
||
| 1905 | |||
| 1906 | return $last_id; |
||
| 1907 | } |
||
| 1908 | |||
| 1909 | /** |
||
| 1910 | * @param int $question_id |
||
| 1911 | * @param int $course_id |
||
| 1912 | */ |
||
| 1913 | public static function deleteAllQuestionOptions($question_id, $course_id) |
||
| 1922 | ], |
||
| 1923 | ] |
||
| 1924 | ); |
||
| 1925 | } |
||
| 1926 | |||
| 1927 | /** |
||
| 1928 | * @param int $id |
||
| 1929 | * @param array $params |
||
| 1930 | * @param int $course_id |
||
| 1931 | * |
||
| 1932 | * @return bool|int |
||
| 1933 | */ |
||
| 1934 | public static function updateQuestionOption($id, $params, $course_id) |
||
| 1935 | { |
||
| 1936 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); |
||
| 1937 | $result = Database::update( |
||
| 1938 | $table, |
||
| 1939 | $params, |
||
| 1940 | ['c_id = ? AND id = ?' => [$course_id, $id]] |
||
| 1941 | ); |
||
| 1942 | |||
| 1943 | return $result; |
||
| 1944 | } |
||
| 1945 | |||
| 1946 | /** |
||
| 1947 | * @param int $question_id |
||
| 1948 | * @param int $course_id |
||
| 1949 | * |
||
| 1950 | * @return array |
||
| 1951 | */ |
||
| 1952 | public static function readQuestionOption($question_id, $course_id) |
||
| 1953 | { |
||
| 1954 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); |
||
| 1955 | $result = Database::select( |
||
| 1956 | '*', |
||
| 1957 | $table, |
||
| 1958 | [ |
||
| 1959 | 'where' => [ |
||
| 1960 | 'c_id = ? AND question_id = ?' => [ |
||
| 1961 | $course_id, |
||
| 1962 | $question_id, |
||
| 1963 | ], |
||
| 1964 | ], |
||
| 1965 | 'order' => 'id ASC', |
||
| 1966 | ] |
||
| 1967 | ); |
||
| 1968 | |||
| 1969 | return $result; |
||
| 1970 | } |
||
| 1971 | |||
| 1972 | /** |
||
| 1973 | * Shows question title an description. |
||
| 1974 | * |
||
| 1975 | * @param Exercise $exercise |
||
| 1976 | * @param int $counter |
||
| 1977 | * @param array $score |
||
| 1978 | * |
||
| 1979 | * @return string HTML string with the header of the question (before the answers table) |
||
| 1980 | */ |
||
| 1981 | public function return_header($exercise, $counter = null, $score = []) |
||
| 2074 | } |
||
| 2075 | |||
| 2076 | /** |
||
| 2077 | * Create a question from a set of parameters. |
||
| 2078 | * |
||
| 2079 | * @param int Quiz ID |
||
| 2080 | * @param string Question name |
||
| 2081 | * @param int Maximum result for the question |
||
| 2082 | * @param int Type of question (see constants at beginning of question.class.php) |
||
| 2083 | * @param int Question level/category |
||
| 2084 | * @param string $quiz_id |
||
| 2085 | */ |
||
| 2086 | public function create_question( |
||
| 2087 | $quiz_id, |
||
| 2088 | $question_name, |
||
| 2089 | $question_description = '', |
||
| 2090 | $max_score = 0, |
||
| 2091 | $type = 1, |
||
| 2092 | $level = 1 |
||
| 2093 | ) { |
||
| 2094 | $course_id = api_get_course_int_id(); |
||
| 2095 | $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 2096 | $tbl_quiz_rel_question = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 2097 | |||
| 2098 | $quiz_id = intval($quiz_id); |
||
| 2099 | $max_score = (float) $max_score; |
||
| 2100 | $type = intval($type); |
||
| 2101 | $level = intval($level); |
||
| 2102 | |||
| 2103 | // Get the max position |
||
| 2104 | $sql = "SELECT max(position) as max_position |
||
| 2105 | FROM $tbl_quiz_question q |
||
| 2106 | INNER JOIN $tbl_quiz_rel_question r |
||
| 2107 | ON |
||
| 2108 | q.id = r.question_id AND |
||
| 2109 | exercice_id = $quiz_id AND |
||
| 2110 | q.c_id = $course_id AND |
||
| 2111 | r.c_id = $course_id"; |
||
| 2112 | $rs_max = Database::query($sql); |
||
| 2113 | $row_max = Database::fetch_object($rs_max); |
||
| 2114 | $max_position = $row_max->max_position + 1; |
||
| 2115 | |||
| 2116 | $params = [ |
||
| 2117 | 'c_id' => $course_id, |
||
| 2118 | 'question' => $question_name, |
||
| 2119 | 'description' => $question_description, |
||
| 2120 | 'ponderation' => $max_score, |
||
| 2121 | 'position' => $max_position, |
||
| 2122 | 'type' => $type, |
||
| 2123 | 'level' => $level, |
||
| 2124 | ]; |
||
| 2125 | $question_id = Database::insert($tbl_quiz_question, $params); |
||
| 2126 | |||
| 2127 | if ($question_id) { |
||
| 2128 | $sql = "UPDATE $tbl_quiz_question |
||
| 2129 | SET id = iid WHERE iid = $question_id"; |
||
| 2130 | Database::query($sql); |
||
| 2131 | |||
| 2132 | // Get the max question_order |
||
| 2133 | $sql = "SELECT max(question_order) as max_order |
||
| 2134 | FROM $tbl_quiz_rel_question |
||
| 2135 | WHERE c_id = $course_id AND exercice_id = $quiz_id "; |
||
| 2136 | $rs_max_order = Database::query($sql); |
||
| 2137 | $row_max_order = Database::fetch_object($rs_max_order); |
||
| 2138 | $max_order = $row_max_order->max_order + 1; |
||
| 2139 | // Attach questions to quiz |
||
| 2140 | $sql = "INSERT INTO $tbl_quiz_rel_question (c_id, question_id, exercice_id, question_order) |
||
| 2141 | VALUES($course_id, $question_id, $quiz_id, $max_order)"; |
||
| 2142 | Database::query($sql); |
||
| 2143 | } |
||
| 2144 | |||
| 2145 | return $question_id; |
||
| 2146 | } |
||
| 2147 | |||
| 2148 | /** |
||
| 2149 | * @return array the image filename of the question type |
||
| 2150 | */ |
||
| 2151 | public function get_type_icon_html() |
||
| 2152 | { |
||
| 2153 | $type = $this->selectType(); |
||
| 2154 | $tabQuestionList = self::get_question_type_list(); // [0]=file to include [1]=type name |
||
| 2155 | |||
| 2156 | require_once $tabQuestionList[$type][0]; |
||
| 2157 | |||
| 2158 | $img = $explanation = null; |
||
| 2159 | eval('$img = '.$tabQuestionList[$type][1].'::$typePicture;'); |
||
| 2160 | eval('$explanation = get_lang('.$tabQuestionList[$type][1].'::$explanationLangVar);'); |
||
| 2161 | |||
| 2162 | return [$img, $explanation]; |
||
| 2163 | } |
||
| 2164 | |||
| 2165 | /** |
||
| 2166 | * Get course medias. |
||
| 2167 | * |
||
| 2168 | * @param int course id |
||
| 2169 | * @param int $course_id |
||
| 2170 | * |
||
| 2171 | * @return array |
||
| 2172 | */ |
||
| 2173 | public static function get_course_medias( |
||
| 2174 | $course_id, |
||
| 2175 | $start = 0, |
||
| 2176 | $limit = 100, |
||
| 2177 | $sidx = "question", |
||
| 2178 | $sord = "ASC", |
||
| 2179 | $where_condition = [] |
||
| 2180 | ) { |
||
| 2181 | $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 2182 | $default_where = [ |
||
| 2183 | 'c_id = ? AND parent_id = 0 AND type = ?' => [ |
||
| 2184 | $course_id, |
||
| 2185 | MEDIA_QUESTION, |
||
| 2186 | ], |
||
| 2187 | ]; |
||
| 2188 | $result = Database::select( |
||
| 2189 | '*', |
||
| 2190 | $table_question, |
||
| 2191 | [ |
||
| 2192 | 'limit' => " $start, $limit", |
||
| 2193 | 'where' => $default_where, |
||
| 2194 | 'order' => "$sidx $sord", |
||
| 2195 | ] |
||
| 2196 | ); |
||
| 2197 | |||
| 2198 | return $result; |
||
| 2199 | } |
||
| 2200 | |||
| 2201 | /** |
||
| 2202 | * Get count course medias. |
||
| 2203 | * |
||
| 2204 | * @param int course id |
||
| 2205 | * |
||
| 2206 | * @return int |
||
| 2207 | */ |
||
| 2208 | public static function get_count_course_medias($course_id) |
||
| 2209 | { |
||
| 2210 | $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 2211 | $result = Database::select( |
||
| 2212 | 'count(*) as count', |
||
| 2213 | $table_question, |
||
| 2214 | [ |
||
| 2215 | 'where' => [ |
||
| 2216 | 'c_id = ? AND parent_id = 0 AND type = ?' => [ |
||
| 2217 | $course_id, |
||
| 2218 | MEDIA_QUESTION, |
||
| 2219 | ], |
||
| 2220 | ], |
||
| 2221 | ], |
||
| 2222 | 'first' |
||
| 2223 | ); |
||
| 2224 | |||
| 2225 | if ($result && isset($result['count'])) { |
||
| 2226 | return $result['count']; |
||
| 2227 | } |
||
| 2228 | |||
| 2229 | return 0; |
||
| 2230 | } |
||
| 2231 | |||
| 2232 | /** |
||
| 2233 | * @param int $course_id |
||
| 2234 | * |
||
| 2235 | * @return array |
||
| 2236 | */ |
||
| 2237 | public static function prepare_course_media_select($course_id) |
||
| 2238 | { |
||
| 2239 | $medias = self::get_course_medias($course_id); |
||
| 2240 | $media_list = []; |
||
| 2241 | $media_list[0] = get_lang('NoMedia'); |
||
| 2242 | |||
| 2243 | if (!empty($medias)) { |
||
| 2244 | foreach ($medias as $media) { |
||
| 2245 | $media_list[$media['id']] = empty($media['question']) ? get_lang('Untitled') : $media['question']; |
||
| 2246 | } |
||
| 2247 | } |
||
| 2248 | |||
| 2249 | return $media_list; |
||
| 2250 | } |
||
| 2251 | |||
| 2252 | /** |
||
| 2253 | * @return array |
||
| 2254 | */ |
||
| 2255 | public static function get_default_levels() |
||
| 2256 | { |
||
| 2257 | $levels = [ |
||
| 2258 | 1 => 1, |
||
| 2259 | 2 => 2, |
||
| 2260 | 3 => 3, |
||
| 2261 | 4 => 4, |
||
| 2262 | 5 => 5, |
||
| 2263 | ]; |
||
| 2264 | |||
| 2265 | return $levels; |
||
| 2266 | } |
||
| 2267 | |||
| 2268 | /** |
||
| 2269 | * @return string |
||
| 2270 | */ |
||
| 2271 | public function show_media_content() |
||
| 2283 | } |
||
| 2284 | |||
| 2285 | /** |
||
| 2286 | * Swap between unique and multiple type answers. |
||
| 2287 | * |
||
| 2288 | * @return UniqueAnswer|MultipleAnswer |
||
| 2289 | */ |
||
| 2290 | public function swapSimpleAnswerTypes() |
||
| 2291 | { |
||
| 2292 | $oppositeAnswers = [ |
||
| 2293 | UNIQUE_ANSWER => MULTIPLE_ANSWER, |
||
| 2294 | MULTIPLE_ANSWER => UNIQUE_ANSWER, |
||
| 2295 | ]; |
||
| 2296 | $this->type = $oppositeAnswers[$this->type]; |
||
| 2297 | Database::update( |
||
| 2298 | Database::get_course_table(TABLE_QUIZ_QUESTION), |
||
| 2299 | ['type' => $this->type], |
||
| 2300 | ['c_id = ? AND id = ?' => [$this->course['real_id'], $this->id]] |
||
| 2301 | ); |
||
| 2302 | $answerClasses = [ |
||
| 2303 | UNIQUE_ANSWER => 'UniqueAnswer', |
||
| 2304 | MULTIPLE_ANSWER => 'MultipleAnswer', |
||
| 2305 | ]; |
||
| 2306 | $swappedAnswer = new $answerClasses[$this->type](); |
||
| 2307 | foreach ($this as $key => $value) { |
||
| 2308 | $swappedAnswer->$key = $value; |
||
| 2309 | } |
||
| 2310 | |||
| 2311 | return $swappedAnswer; |
||
| 2312 | } |
||
| 2313 | |||
| 2314 | /** |
||
| 2315 | * @param array $score |
||
| 2316 | * |
||
| 2317 | * @return bool |
||
| 2318 | */ |
||
| 2319 | public function isQuestionWaitingReview($score) |
||
| 2320 | { |
||
| 2321 | $isReview = false; |
||
| 2322 | if (!empty($score)) { |
||
| 2323 | if (!empty($score['comments']) || $score['score'] > 0) { |
||
| 2324 | $isReview = true; |
||
| 2325 | } |
||
| 2326 | } |
||
| 2327 | |||
| 2328 | return $isReview; |
||
| 2329 | } |
||
| 2330 | |||
| 2331 | /** |
||
| 2332 | * @param string $value |
||
| 2333 | */ |
||
| 2334 | public function setFeedback($value) |
||
| 2335 | { |
||
| 2336 | $this->feedback = $value; |
||
| 2337 | } |
||
| 2338 | |||
| 2339 | /** |
||
| 2340 | * @param Exercise $exercise |
||
| 2341 | * |
||
| 2342 | * @return bool |
||
| 2343 | */ |
||
| 2344 | public function showFeedback($exercise) |
||
| 2345 | { |
||
| 2346 | return |
||
| 2347 | in_array($this->type, $this->questionTypeWithFeedback) && |
||
| 2348 | $exercise->feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM; |
||
| 2349 | } |
||
| 2350 | |||
| 2351 | /** |
||
| 2352 | * @return string |
||
| 2353 | */ |
||
| 2354 | public function returnFormatFeedback() |
||
| 2355 | { |
||
| 2356 | return '<br />'.Display::return_message($this->feedback, 'normal', false); |
||
| 2357 | } |
||
| 2358 | |||
| 2359 | /** |
||
| 2360 | * Check if this question exists in another exercise. |
||
| 2361 | * |
||
| 2362 | * @throws \Doctrine\ORM\Query\QueryException |
||
| 2363 | * |
||
| 2364 | * @return mixed |
||
| 2365 | */ |
||
| 2366 | public function existsInAnotherExercises() |
||
| 2379 | } |
||
| 2380 | |||
| 2381 | public function getHotSpotData() |
||
| 2382 | { |
||
| 2383 | |||
| 2384 | } |
||
| 2385 | } |
||
| 2386 |