| Total Complexity | 276 |
| Total Lines | 2456 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 20 | abstract class Question |
||
| 21 | { |
||
| 22 | public $id; |
||
| 23 | public $iid; |
||
| 24 | public $question; |
||
| 25 | public $description; |
||
| 26 | public $weighting; |
||
| 27 | public $position; |
||
| 28 | public $type; |
||
| 29 | public $level; |
||
| 30 | public $picture; |
||
| 31 | public $exerciseList; // array with the list of exercises which this question is in |
||
| 32 | public $category_list; |
||
| 33 | public $parent_id; |
||
| 34 | public $category; |
||
| 35 | public $mandatory; |
||
| 36 | public $isContent; |
||
| 37 | public $course; |
||
| 38 | public $feedback; |
||
| 39 | public $typePicture = 'new_question.png'; |
||
| 40 | public $explanationLangVar = ''; |
||
| 41 | public $questionTableClass = 'table table-striped question-answer-result__detail'; |
||
| 42 | public $questionTypeWithFeedback; |
||
| 43 | public $extra; |
||
| 44 | public $export = false; |
||
| 45 | public $code; |
||
| 46 | public static $questionTypes = [ |
||
| 47 | // — Single-choice |
||
| 48 | UNIQUE_ANSWER => ['unique_answer.class.php', 'UniqueAnswer', 'Unique answer'], |
||
| 49 | UNIQUE_ANSWER_IMAGE => ['UniqueAnswerImage.php', 'UniqueAnswerImage', 'Unique answer (image)'], |
||
| 50 | UNIQUE_ANSWER_NO_OPTION => ['unique_answer_no_option.class.php', 'UniqueAnswerNoOption', 'Unique answer (no options)'], |
||
| 51 | |||
| 52 | // — Multiple-choice (all variants together) |
||
| 53 | MULTIPLE_ANSWER => ['multiple_answer.class.php', 'MultipleAnswer', 'Multiple answer'], |
||
| 54 | GLOBAL_MULTIPLE_ANSWER => ['global_multiple_answer.class.php', 'GlobalMultipleAnswer', 'Global multiple answer'], |
||
| 55 | MULTIPLE_ANSWER_DROPDOWN => ['MultipleAnswerDropdown.php', 'MultipleAnswerDropdown', 'Multiple answer (dropdown)'], |
||
| 56 | MULTIPLE_ANSWER_DROPDOWN_COMBINATION => ['MultipleAnswerDropdownCombination.php', 'MultipleAnswerDropdownCombination', 'Multiple answer (dropdown combination)'], |
||
| 57 | MULTIPLE_ANSWER_COMBINATION => ['multiple_answer_combination.class.php', 'MultipleAnswerCombination', 'Multiple answer (combination)'], |
||
| 58 | MULTIPLE_ANSWER_TRUE_FALSE => ['multiple_answer_true_false.class.php', 'MultipleAnswerTrueFalse', 'True/False multiple answer'], |
||
| 59 | MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE => [ |
||
| 60 | 'multiple_answer_combination_true_false.class.php', 'MultipleAnswerCombinationTrueFalse', 'True/False combination multiple answer' |
||
| 61 | ], |
||
| 62 | MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY => [ |
||
| 63 | 'MultipleAnswerTrueFalseDegreeCertainty.php', 'MultipleAnswerTrueFalseDegreeCertainty', 'True/False with degree of certainty' |
||
| 64 | ], |
||
| 65 | |||
| 66 | // — Matching / draggable |
||
| 67 | MATCHING => ['matching.class.php', 'Matching', 'Matching'], |
||
| 68 | MATCHING_COMBINATION => ['MatchingCombination.php', 'MatchingCombination', 'Matching (combination)'], |
||
| 69 | DRAGGABLE => ['Draggable.php', 'Draggable', 'Draggable'], |
||
| 70 | MATCHING_DRAGGABLE => ['MatchingDraggable.php', 'MatchingDraggable', 'Matching (draggable)'], |
||
| 71 | MATCHING_DRAGGABLE_COMBINATION => ['MatchingDraggableCombination.php', 'MatchingDraggableCombination', 'Matching (draggable combination)'], |
||
| 72 | |||
| 73 | // — Fill-in-the-blanks / calculated |
||
| 74 | FILL_IN_BLANKS => ['fill_blanks.class.php', 'FillBlanks', 'Fill in the blanks'], |
||
| 75 | FILL_IN_BLANKS_COMBINATION => ['FillBlanksCombination.php', 'FillBlanksCombination', 'Fill in the blanks (combination)'], |
||
| 76 | CALCULATED_ANSWER => ['calculated_answer.class.php', 'CalculatedAnswer', 'Calculated answer'], |
||
| 77 | |||
| 78 | // — Open answers / expression |
||
| 79 | FREE_ANSWER => ['freeanswer.class.php', 'FreeAnswer', 'Free answer'], |
||
| 80 | ORAL_EXPRESSION => ['oral_expression.class.php', 'OralExpression', 'Oral expression'], |
||
| 81 | |||
| 82 | // — Hotspot |
||
| 83 | HOT_SPOT => ['hotspot.class.php', 'HotSpot', 'Hotspot'], |
||
| 84 | HOT_SPOT_COMBINATION => ['HotSpotCombination.php', 'HotSpotCombination', 'Hotspot (combination)'], |
||
| 85 | HOT_SPOT_DELINEATION => ['HotSpotDelineation.php', 'HotSpotDelineation', 'Hotspot delineation'], |
||
| 86 | |||
| 87 | // — Media / annotation |
||
| 88 | MEDIA_QUESTION => ['MediaQuestion.php', 'MediaQuestion', 'Media question'], |
||
| 89 | ANNOTATION => ['Annotation.php', 'Annotation', 'Annotation'], |
||
| 90 | |||
| 91 | // — Special |
||
| 92 | READING_COMPREHENSION => ['ReadingComprehension.php', 'ReadingComprehension', 'Reading comprehension'], |
||
| 93 | PAGE_BREAK => ['PageBreakQuestion.php', 'PageBreakQuestion', 'Page break'], |
||
| 94 | UPLOAD_ANSWER => ['UploadAnswer.php', 'UploadAnswer', 'Upload answer'], |
||
| 95 | ]; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Question types that support adaptive scenario. |
||
| 99 | */ |
||
| 100 | protected static $adaptiveScenarioTypes = [ |
||
| 101 | UNIQUE_ANSWER, |
||
| 102 | MULTIPLE_ANSWER, |
||
| 103 | MULTIPLE_ANSWER_COMBINATION, |
||
| 104 | MULTIPLE_ANSWER_TRUE_FALSE, |
||
| 105 | MATCHING, |
||
| 106 | MATCHING_COMBINATION, |
||
| 107 | DRAGGABLE, |
||
| 108 | MATCHING_DRAGGABLE, |
||
| 109 | MATCHING_DRAGGABLE_COMBINATION, |
||
| 110 | HOT_SPOT_DELINEATION, |
||
| 111 | FILL_IN_BLANKS, |
||
| 112 | FILL_IN_BLANKS_COMBINATION, |
||
| 113 | CALCULATED_ANSWER, |
||
| 114 | ANNOTATION, |
||
| 115 | // Do NOT include FREE_ANSWER, ORAL_EXPRESSION, UPLOAD_ANSWER, MEDIA_QUESTION, PAGE_BREAK, etc. |
||
| 116 | ]; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * constructor of the class. |
||
| 120 | * |
||
| 121 | * @author Olivier Brouckaert |
||
| 122 | */ |
||
| 123 | public function __construct() |
||
| 124 | { |
||
| 125 | $this->id = 0; |
||
| 126 | $this->iid = 0; |
||
| 127 | $this->question = ''; |
||
| 128 | $this->description = ''; |
||
| 129 | $this->weighting = 0; |
||
| 130 | $this->position = 1; |
||
| 131 | $this->picture = ''; |
||
| 132 | $this->level = 1; |
||
| 133 | $this->category = 0; |
||
| 134 | // This variable is used when loading an exercise like an scenario with |
||
| 135 | // an special hotspot: final_overlap, final_missing, final_excess |
||
| 136 | $this->extra = ''; |
||
| 137 | $this->exerciseList = []; |
||
| 138 | $this->course = api_get_course_info(); |
||
| 139 | $this->category_list = []; |
||
| 140 | $this->parent_id = 0; |
||
| 141 | $this->mandatory = 0; |
||
| 142 | // See BT#12611 |
||
| 143 | $this->questionTypeWithFeedback = [ |
||
| 144 | MATCHING, |
||
| 145 | MATCHING_COMBINATION, |
||
| 146 | MATCHING_DRAGGABLE, |
||
| 147 | MATCHING_DRAGGABLE_COMBINATION, |
||
| 148 | DRAGGABLE, |
||
| 149 | FILL_IN_BLANKS, |
||
| 150 | FILL_IN_BLANKS_COMBINATION, |
||
| 151 | FREE_ANSWER, |
||
| 152 | UPLOAD_ANSWER, |
||
| 153 | ORAL_EXPRESSION, |
||
| 154 | CALCULATED_ANSWER, |
||
| 155 | ANNOTATION, |
||
| 156 | ]; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function getId() |
||
| 160 | { |
||
| 161 | return $this->iid; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return int|null |
||
| 166 | */ |
||
| 167 | public function getIsContent() |
||
| 168 | { |
||
| 169 | $isContent = null; |
||
| 170 | if (isset($_REQUEST['isContent'])) { |
||
| 171 | $isContent = (int) $_REQUEST['isContent']; |
||
| 172 | } |
||
| 173 | |||
| 174 | return $this->isContent = $isContent; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Reads question information from the data base. |
||
| 179 | * |
||
| 180 | * @param int $id - question ID |
||
| 181 | * @param array $course_info |
||
| 182 | * @param bool $getExerciseList |
||
| 183 | * |
||
| 184 | * @return Question |
||
| 185 | * |
||
| 186 | * @author Olivier Brouckaert |
||
| 187 | */ |
||
| 188 | public static function read($id, $course_info = [], $getExerciseList = true) |
||
| 189 | { |
||
| 190 | $id = (int) $id; |
||
| 191 | if (empty($course_info)) { |
||
| 192 | $course_info = api_get_course_info(); |
||
| 193 | } |
||
| 194 | $course_id = $course_info['real_id']; |
||
| 195 | |||
| 196 | if (empty($course_id) || -1 == $course_id) { |
||
| 197 | return false; |
||
| 198 | } |
||
| 199 | |||
| 200 | $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 201 | $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 202 | |||
| 203 | $sql = "SELECT * |
||
| 204 | FROM $TBL_QUESTIONS |
||
| 205 | WHERE iid = $id "; |
||
| 206 | $result = Database::query($sql); |
||
| 207 | |||
| 208 | // if the question has been found |
||
| 209 | if ($object = Database::fetch_object($result)) { |
||
| 210 | $objQuestion = self::getInstance($object->type); |
||
| 211 | if (!empty($objQuestion)) { |
||
| 212 | $objQuestion->id = $id; |
||
| 213 | $objQuestion->iid = (int) $object->iid; |
||
| 214 | $objQuestion->question = $object->question; |
||
| 215 | $objQuestion->description = $object->description; |
||
| 216 | $objQuestion->weighting = $object->ponderation; |
||
| 217 | $objQuestion->position = $object->position; |
||
| 218 | $objQuestion->type = (int) $object->type; |
||
| 219 | $objQuestion->picture = $object->picture; |
||
| 220 | $objQuestion->level = (int) $object->level; |
||
| 221 | $objQuestion->extra = $object->extra; |
||
| 222 | $objQuestion->course = $course_info; |
||
| 223 | $objQuestion->feedback = isset($object->feedback) ? $object->feedback : ''; |
||
| 224 | $objQuestion->category = TestCategory::getCategoryForQuestion($id, $course_id); |
||
| 225 | $objQuestion->code = isset($object->code) ? $object->code : ''; |
||
| 226 | $categoryInfo = TestCategory::getCategoryInfoForQuestion($id, $course_id); |
||
| 227 | |||
| 228 | if (!empty($categoryInfo)) { |
||
| 229 | if (isset($categoryInfo['category_id'])) { |
||
| 230 | $objQuestion->category = (int) $categoryInfo['category_id']; |
||
| 231 | } |
||
| 232 | |||
| 233 | if (('true' === api_get_setting('exercise.allow_mandatory_question_in_category')) && |
||
| 234 | isset($categoryInfo['mandatory']) |
||
| 235 | ) { |
||
| 236 | $objQuestion->mandatory = (int) $categoryInfo['mandatory']; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | if ($getExerciseList) { |
||
| 241 | $tblQuiz = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 242 | $sessionId = (int) api_get_session_id(); |
||
| 243 | $sessionJoin = $sessionId > 0 ? "rl.session_id = $sessionId" : "rl.session_id IS NULL"; |
||
| 244 | |||
| 245 | $sql = "SELECT DISTINCT q.quiz_id |
||
| 246 | FROM $TBL_EXERCISE_QUESTION q |
||
| 247 | INNER JOIN $tblQuiz e ON e.iid = q.quiz_id |
||
| 248 | INNER JOIN resource_node rn ON rn.id = e.resource_node_id |
||
| 249 | INNER JOIN resource_link rl ON rl.resource_node_id = rn.id AND $sessionJoin |
||
| 250 | WHERE q.question_id = $id |
||
| 251 | AND rl.deleted_at IS NULL"; |
||
| 252 | $result = Database::query($sql); |
||
| 253 | while ($obj = Database::fetch_object($result)) { |
||
| 254 | $objQuestion->exerciseList[] = (int) $obj->quiz_id; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | $objQuestion->parent_id = isset($object->parent_media_id) |
||
| 259 | ? (int) $object->parent_media_id |
||
| 260 | : 0; |
||
| 261 | |||
| 262 | return $objQuestion; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | // question not found |
||
| 267 | return false; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * returns the question title. |
||
| 272 | * |
||
| 273 | * @author Olivier Brouckaert |
||
| 274 | * |
||
| 275 | * @return string - question title |
||
| 276 | */ |
||
| 277 | public function selectTitle() |
||
| 278 | { |
||
| 279 | if ('true' !== api_get_setting('editor.save_titles_as_html')) { |
||
| 280 | return $this->question; |
||
| 281 | } |
||
| 282 | |||
| 283 | return Display::div($this->question, ['style' => 'display: inline-block;']); |
||
| 284 | } |
||
| 285 | |||
| 286 | public function getTitleToDisplay(Exercise $exercise, int $itemNumber): string |
||
| 287 | { |
||
| 288 | $showQuestionTitleHtml = ('true' === api_get_setting('editor.save_titles_as_html')); |
||
| 289 | $title = ''; |
||
| 290 | if ('true' === api_get_setting('exercise.show_question_id')) { |
||
| 291 | $title .= '<h4>#'.$this->course['code'].'-'.$this->iid.'</h4>'; |
||
| 292 | } |
||
| 293 | |||
| 294 | $title .= $showQuestionTitleHtml ? '' : '<strong>'; |
||
| 295 | if (1 !== $exercise->getHideQuestionNumber()) { |
||
| 296 | $title .= $itemNumber.'. '; |
||
| 297 | } |
||
| 298 | $title .= $this->selectTitle(); |
||
| 299 | $title .= $showQuestionTitleHtml ? '' : '</strong>'; |
||
| 300 | |||
| 301 | return Display::div( |
||
| 302 | $title, |
||
| 303 | ['class' => 'question_title'] |
||
| 304 | ); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * returns the question description. |
||
| 309 | * |
||
| 310 | * @author Olivier Brouckaert |
||
| 311 | * |
||
| 312 | * @return string - question description |
||
| 313 | */ |
||
| 314 | public function selectDescription() |
||
| 315 | { |
||
| 316 | return $this->description; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * returns the question weighting. |
||
| 321 | * |
||
| 322 | * @author Olivier Brouckaert |
||
| 323 | * |
||
| 324 | * @return int - question weighting |
||
| 325 | */ |
||
| 326 | public function selectWeighting() |
||
| 327 | { |
||
| 328 | return $this->weighting; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * returns the answer type. |
||
| 333 | * |
||
| 334 | * @author Olivier Brouckaert |
||
| 335 | * |
||
| 336 | * @return int - answer type |
||
| 337 | */ |
||
| 338 | public function selectType() |
||
| 339 | { |
||
| 340 | return $this->type; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * returns the level of the question. |
||
| 345 | * |
||
| 346 | * @author Nicolas Raynaud |
||
| 347 | * |
||
| 348 | * @return int - level of the question, 0 by default |
||
| 349 | */ |
||
| 350 | public function getLevel() |
||
| 351 | { |
||
| 352 | return $this->level; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * changes the question title. |
||
| 357 | * |
||
| 358 | * @param string $title - question title |
||
| 359 | * |
||
| 360 | * @author Olivier Brouckaert |
||
| 361 | */ |
||
| 362 | public function updateTitle($title) |
||
| 363 | { |
||
| 364 | $this->question = $title; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * changes the question description. |
||
| 369 | * |
||
| 370 | * @param string $description - question description |
||
| 371 | * |
||
| 372 | * @author Olivier Brouckaert |
||
| 373 | */ |
||
| 374 | public function updateDescription($description) |
||
| 375 | { |
||
| 376 | $this->description = $description; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * changes the question weighting. |
||
| 381 | * |
||
| 382 | * @param int $weighting - question weighting |
||
| 383 | * |
||
| 384 | * @author Olivier Brouckaert |
||
| 385 | */ |
||
| 386 | public function updateWeighting($weighting) |
||
| 387 | { |
||
| 388 | $this->weighting = $weighting; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param array $category |
||
| 393 | * |
||
| 394 | * @author Hubert Borderiou 12-10-2011 |
||
| 395 | */ |
||
| 396 | public function updateCategory($category) |
||
| 397 | { |
||
| 398 | $this->category = $category; |
||
| 399 | } |
||
| 400 | |||
| 401 | public function setMandatory($value) |
||
| 402 | { |
||
| 403 | $this->mandatory = (int) $value; |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * in this version, a question can only have 1 category |
||
| 408 | * if category is 0, then question has no category then delete the category entry. |
||
| 409 | * |
||
| 410 | * @author Hubert Borderiou 12-10-2011 |
||
| 411 | */ |
||
| 412 | public function saveCategory(int $categoryId): bool |
||
| 413 | { |
||
| 414 | if ($categoryId <= 0) { |
||
| 415 | $this->deleteCategory(); |
||
| 416 | } else { |
||
| 417 | // update or add category for a question |
||
| 418 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); |
||
| 419 | $categoryId = (int) $categoryId; |
||
| 420 | $questionId = (int) $this->id; |
||
| 421 | $sql = "SELECT count(*) AS nb FROM $table |
||
| 422 | WHERE |
||
| 423 | question_id = $questionId |
||
| 424 | "; |
||
| 425 | $res = Database::query($sql); |
||
| 426 | $row = Database::fetch_array($res); |
||
| 427 | $allowMandatory = ('true' === api_get_setting('exercise.allow_mandatory_question_in_category')); |
||
| 428 | if ($row['nb'] > 0) { |
||
| 429 | $extraMandatoryCondition = ''; |
||
| 430 | if ($allowMandatory) { |
||
| 431 | $extraMandatoryCondition = ", mandatory = {$this->mandatory}"; |
||
| 432 | } |
||
| 433 | $sql = "UPDATE $table |
||
| 434 | SET category_id = $categoryId |
||
| 435 | $extraMandatoryCondition |
||
| 436 | WHERE |
||
| 437 | question_id = $questionId |
||
| 438 | "; |
||
| 439 | Database::query($sql); |
||
| 440 | } else { |
||
| 441 | $sql = "INSERT INTO $table (question_id, category_id) |
||
| 442 | VALUES ($questionId, $categoryId) |
||
| 443 | "; |
||
| 444 | Database::query($sql); |
||
| 445 | if ($allowMandatory) { |
||
| 446 | $id = Database::insert_id(); |
||
| 447 | if ($id) { |
||
| 448 | $sql = "UPDATE $table SET mandatory = {$this->mandatory} |
||
| 449 | WHERE iid = $id"; |
||
| 450 | Database::query($sql); |
||
| 451 | } |
||
| 452 | } |
||
| 453 | } |
||
| 454 | } |
||
| 455 | |||
| 456 | return true; |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @author hubert borderiou 12-10-2011 |
||
| 461 | * |
||
| 462 | * delete any category entry for question id |
||
| 463 | * delete the category for question |
||
| 464 | */ |
||
| 465 | public function deleteCategory(): bool |
||
| 466 | { |
||
| 467 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); |
||
| 468 | $questionId = (int) $this->id; |
||
| 469 | if (empty($questionId)) { |
||
| 470 | return false; |
||
| 471 | } |
||
| 472 | $sql = "DELETE FROM $table |
||
| 473 | WHERE |
||
| 474 | question_id = $questionId |
||
| 475 | "; |
||
| 476 | Database::query($sql); |
||
| 477 | |||
| 478 | return true; |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * changes the question position. |
||
| 483 | * |
||
| 484 | * @param int $position - question position |
||
| 485 | * |
||
| 486 | * @author Olivier Brouckaert |
||
| 487 | */ |
||
| 488 | public function updatePosition($position) |
||
| 489 | { |
||
| 490 | $this->position = $position; |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * changes the question level. |
||
| 495 | * |
||
| 496 | * @param int $level - question level |
||
| 497 | * |
||
| 498 | * @author Nicolas Raynaud |
||
| 499 | */ |
||
| 500 | public function updateLevel($level) |
||
| 501 | { |
||
| 502 | $this->level = $level; |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * changes the answer type. If the user changes the type from "unique answer" to "multiple answers" |
||
| 507 | * (or conversely) answers are not deleted, otherwise yes. |
||
| 508 | * |
||
| 509 | * @param int $type - answer type |
||
| 510 | * |
||
| 511 | * @author Olivier Brouckaert |
||
| 512 | */ |
||
| 513 | public function updateType($type) |
||
| 514 | { |
||
| 515 | $table = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 516 | $course_id = $this->course['real_id']; |
||
| 517 | |||
| 518 | if (empty($course_id)) { |
||
| 519 | $course_id = api_get_course_int_id(); |
||
| 520 | } |
||
| 521 | // if we really change the type |
||
| 522 | if ($type != $this->type) { |
||
| 523 | // if we don't change from "unique answer" to "multiple answers" (or conversely) |
||
| 524 | if (!in_array($this->type, [UNIQUE_ANSWER, MULTIPLE_ANSWER]) || |
||
| 525 | !in_array($type, [UNIQUE_ANSWER, MULTIPLE_ANSWER]) |
||
| 526 | ) { |
||
| 527 | // removes old answers |
||
| 528 | $sql = "DELETE FROM $table |
||
| 529 | WHERE c_id = $course_id AND question_id = ".(int) ($this->id); |
||
| 530 | Database::query($sql); |
||
| 531 | } |
||
| 532 | |||
| 533 | $this->type = $type; |
||
| 534 | } |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Set title. |
||
| 539 | * |
||
| 540 | * @param string $title |
||
| 541 | */ |
||
| 542 | public function setTitle($title) |
||
| 543 | { |
||
| 544 | $this->question = $title; |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Sets extra info. |
||
| 549 | * |
||
| 550 | * @param string $extra |
||
| 551 | */ |
||
| 552 | public function setExtra($extra) |
||
| 553 | { |
||
| 554 | $this->extra = $extra; |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * updates the question in the data base |
||
| 559 | * if an exercise ID is provided, we add that exercise ID into the exercise list. |
||
| 560 | * |
||
| 561 | * @author Olivier Brouckaert |
||
| 562 | * |
||
| 563 | * @param Exercise $exercise |
||
| 564 | */ |
||
| 565 | public function save($exercise) |
||
| 566 | { |
||
| 567 | $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 568 | $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 569 | $em = Database::getManager(); |
||
| 570 | $exerciseId = $exercise->iId; |
||
| 571 | |||
| 572 | $id = $this->id; |
||
| 573 | $type = $this->type; |
||
| 574 | $c_id = $this->course['real_id']; |
||
| 575 | |||
| 576 | $courseEntity = api_get_course_entity($c_id); |
||
| 577 | $categoryId = $this->category; |
||
| 578 | |||
| 579 | $questionCategoryRepo = Container::getQuestionCategoryRepository(); |
||
| 580 | $questionRepo = Container::getQuestionRepository(); |
||
| 581 | |||
| 582 | // question already exists |
||
| 583 | if (!empty($id)) { |
||
| 584 | /** @var CQuizQuestion $question */ |
||
| 585 | $question = $questionRepo->find($id); |
||
| 586 | if ($question) { |
||
|
|
|||
| 587 | $question |
||
| 588 | ->setQuestion($this->question) |
||
| 589 | ->setDescription($this->description) |
||
| 590 | ->setPonderation($this->weighting) |
||
| 591 | ->setPosition($this->position) |
||
| 592 | ->setType($this->type) |
||
| 593 | ->setExtra($this->extra) |
||
| 594 | ->setLevel((int) $this->level) |
||
| 595 | ->setFeedback($this->feedback) |
||
| 596 | ->setParentMediaId($this->parent_id); |
||
| 597 | |||
| 598 | if (!empty($categoryId)) { |
||
| 599 | $category = $questionCategoryRepo->find($categoryId); |
||
| 600 | $question->updateCategory($category); |
||
| 601 | } |
||
| 602 | |||
| 603 | $em->persist($question); |
||
| 604 | $em->flush(); |
||
| 605 | |||
| 606 | Event::addEvent( |
||
| 607 | LOG_QUESTION_UPDATED, |
||
| 608 | LOG_QUESTION_ID, |
||
| 609 | $this->iid |
||
| 610 | ); |
||
| 611 | if ('true' === api_get_setting('search_enabled')) { |
||
| 612 | $this->search_engine_edit($exerciseId); |
||
| 613 | } |
||
| 614 | } |
||
| 615 | } else { |
||
| 616 | // Creates a new question |
||
| 617 | $sql = "SELECT max(position) |
||
| 618 | FROM $TBL_QUESTIONS as question, |
||
| 619 | $TBL_EXERCISE_QUESTION as test_question |
||
| 620 | WHERE |
||
| 621 | question.iid = test_question.question_id AND |
||
| 622 | test_question.quiz_id = ".$exerciseId; |
||
| 623 | $result = Database::query($sql); |
||
| 624 | $current_position = Database::result($result, 0, 0); |
||
| 625 | $this->updatePosition($current_position + 1); |
||
| 626 | $position = $this->position; |
||
| 627 | //$exerciseEntity = $exerciseRepo->find($exerciseId); |
||
| 628 | |||
| 629 | $question = (new CQuizQuestion()) |
||
| 630 | ->setQuestion($this->question) |
||
| 631 | ->setDescription($this->description) |
||
| 632 | ->setPonderation($this->weighting) |
||
| 633 | ->setPosition($position) |
||
| 634 | ->setType($this->type) |
||
| 635 | ->setExtra($this->extra) |
||
| 636 | ->setLevel((int) $this->level) |
||
| 637 | ->setFeedback($this->feedback) |
||
| 638 | ->setParentMediaId($this->parent_id) |
||
| 639 | ->setParent($courseEntity) |
||
| 640 | ->addCourseLink($courseEntity, api_get_session_entity(), api_get_group_entity()); |
||
| 641 | |||
| 642 | $em->persist($question); |
||
| 643 | $em->flush(); |
||
| 644 | |||
| 645 | $this->id = $question->getIid(); |
||
| 646 | |||
| 647 | if ($this->id) { |
||
| 648 | Event::addEvent( |
||
| 649 | LOG_QUESTION_CREATED, |
||
| 650 | LOG_QUESTION_ID, |
||
| 651 | $this->id |
||
| 652 | ); |
||
| 653 | |||
| 654 | $questionRepo->addFileFromFileRequest($question, 'imageUpload'); |
||
| 655 | |||
| 656 | // If hotspot, create first answer |
||
| 657 | if (in_array($type, [HOT_SPOT, HOT_SPOT_COMBINATION, HOT_SPOT_ORDER])) { |
||
| 658 | $quizAnswer = new CQuizAnswer(); |
||
| 659 | $quizAnswer |
||
| 660 | ->setQuestion($question) |
||
| 661 | ->setPonderation(10) |
||
| 662 | ->setPosition(1) |
||
| 663 | ->setHotspotCoordinates('0;0|0|0') |
||
| 664 | ->setHotspotType('square'); |
||
| 665 | |||
| 666 | $em->persist($quizAnswer); |
||
| 667 | $em->flush(); |
||
| 668 | } |
||
| 669 | |||
| 670 | if (HOT_SPOT_DELINEATION == $type) { |
||
| 671 | $quizAnswer = new CQuizAnswer(); |
||
| 672 | $quizAnswer |
||
| 673 | ->setQuestion($question) |
||
| 674 | ->setPonderation(10) |
||
| 675 | ->setPosition(1) |
||
| 676 | ->setHotspotCoordinates('0;0|0|0') |
||
| 677 | ->setHotspotType('delineation'); |
||
| 678 | |||
| 679 | $em->persist($quizAnswer); |
||
| 680 | $em->flush(); |
||
| 681 | } |
||
| 682 | |||
| 683 | if ('true' === api_get_setting('search_enabled')) { |
||
| 684 | $this->search_engine_edit($exerciseId, true); |
||
| 685 | } |
||
| 686 | } |
||
| 687 | } |
||
| 688 | |||
| 689 | // if the question is created in an exercise |
||
| 690 | if (!empty($exerciseId)) { |
||
| 691 | // adds the exercise into the exercise list of this question |
||
| 692 | $this->addToList($exerciseId, true); |
||
| 693 | } |
||
| 694 | } |
||
| 695 | |||
| 696 | /** |
||
| 697 | * @param int $exerciseId |
||
| 698 | * @param bool $addQs |
||
| 699 | * @param bool $rmQs |
||
| 700 | */ |
||
| 701 | public function search_engine_edit( |
||
| 839 | } |
||
| 840 | } |
||
| 841 | } |
||
| 842 | } |
||
| 843 | } |
||
| 844 | |||
| 845 | /** |
||
| 846 | * adds an exercise into the exercise list. |
||
| 847 | * |
||
| 848 | * @author Olivier Brouckaert |
||
| 849 | * |
||
| 850 | * @param int $exerciseId - exercise ID |
||
| 851 | * @param bool $fromSave - from $this->save() or not |
||
| 852 | */ |
||
| 853 | public function addToList($exerciseId, $fromSave = false) |
||
| 874 | } |
||
| 875 | } |
||
| 876 | } |
||
| 877 | |||
| 878 | /** |
||
| 879 | * removes an exercise from the exercise list. |
||
| 880 | * |
||
| 881 | * @author Olivier Brouckaert |
||
| 882 | * |
||
| 883 | * @param int $exerciseId - exercise ID |
||
| 884 | * @param int $courseId |
||
| 885 | * |
||
| 886 | * @return bool - true if removed, otherwise false |
||
| 887 | */ |
||
| 888 | public function removeFromList($exerciseId, $courseId = 0) |
||
| 889 | { |
||
| 890 | $table = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 891 | $tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 892 | $id = (int) $this->id; |
||
| 893 | $exerciseId = (int) $exerciseId; |
||
| 894 | |||
| 895 | // searches the position of the exercise ID in the list |
||
| 896 | $pos = array_search($exerciseId, $this->exerciseList); |
||
| 897 | $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId; |
||
| 898 | |||
| 899 | // exercise not found |
||
| 900 | if (false === $pos) { |
||
| 901 | return false; |
||
| 902 | } else { |
||
| 903 | // deletes the position in the array containing the wanted exercise ID |
||
| 904 | unset($this->exerciseList[$pos]); |
||
| 905 | //update order of other elements |
||
| 906 | $sql = "SELECT question_order |
||
| 907 | FROM $table |
||
| 908 | WHERE |
||
| 909 | question_id = $id AND |
||
| 910 | quiz_id = $exerciseId"; |
||
| 911 | $res = Database::query($sql); |
||
| 912 | if (Database::num_rows($res) > 0) { |
||
| 913 | $row = Database::fetch_array($res); |
||
| 914 | if (!empty($row['question_order'])) { |
||
| 915 | $sql = "UPDATE $table |
||
| 916 | SET question_order = question_order-1 |
||
| 917 | WHERE |
||
| 918 | quiz_id = $exerciseId AND |
||
| 919 | question_order > ".$row['question_order']; |
||
| 920 | Database::query($sql); |
||
| 921 | } |
||
| 922 | } |
||
| 923 | |||
| 924 | $sql = "DELETE FROM $table |
||
| 925 | WHERE |
||
| 926 | question_id = $id AND |
||
| 927 | quiz_id = $exerciseId"; |
||
| 928 | Database::query($sql); |
||
| 929 | |||
| 930 | $reset = "UPDATE $tableQuestion |
||
| 931 | SET parent_media_id = NULL |
||
| 932 | WHERE parent_media_id = $id"; |
||
| 933 | Database::query($reset); |
||
| 934 | |||
| 935 | return true; |
||
| 936 | } |
||
| 937 | } |
||
| 938 | |||
| 939 | /** |
||
| 940 | * Deletes a question from the database |
||
| 941 | * the parameter tells if the question is removed from all exercises (value = 0), |
||
| 942 | * or just from one exercise (value = exercise ID). |
||
| 943 | * |
||
| 944 | * @author Olivier Brouckaert |
||
| 945 | * |
||
| 946 | * @param int $deleteFromEx - exercise ID if the question is only removed from one exercise |
||
| 947 | * |
||
| 948 | * @return bool |
||
| 949 | */ |
||
| 950 | public function delete($deleteFromEx = 0) |
||
| 951 | { |
||
| 952 | if (empty($this->course)) { |
||
| 953 | return false; |
||
| 954 | } |
||
| 955 | |||
| 956 | $courseId = $this->course['real_id']; |
||
| 957 | |||
| 958 | if (empty($courseId)) { |
||
| 959 | return false; |
||
| 960 | } |
||
| 961 | |||
| 962 | $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 963 | $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 964 | $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 965 | $TBL_QUIZ_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); |
||
| 966 | |||
| 967 | $id = (int) $this->id; |
||
| 968 | |||
| 969 | // if the question must be removed from all exercises |
||
| 970 | if (!$deleteFromEx) { |
||
| 971 | //update the question_order of each question to avoid inconsistencies |
||
| 972 | $sql = "SELECT quiz_id, question_order |
||
| 973 | FROM $TBL_EXERCISE_QUESTION |
||
| 974 | WHERE question_id = ".$id; |
||
| 975 | |||
| 976 | $res = Database::query($sql); |
||
| 977 | if (Database::num_rows($res) > 0) { |
||
| 978 | while ($row = Database::fetch_array($res)) { |
||
| 979 | if (!empty($row['question_order'])) { |
||
| 980 | $sql = "UPDATE $TBL_EXERCISE_QUESTION |
||
| 981 | SET question_order = question_order-1 |
||
| 982 | WHERE |
||
| 983 | quiz_id = ".(int) ($row['quiz_id']).' AND |
||
| 984 | question_order > '.$row['question_order']; |
||
| 985 | Database::query($sql); |
||
| 986 | } |
||
| 987 | } |
||
| 988 | } |
||
| 989 | |||
| 990 | $reset = "UPDATE $TBL_QUESTIONS |
||
| 991 | SET parent_media_id = NULL |
||
| 992 | WHERE parent_media_id = $id"; |
||
| 993 | Database::query($reset); |
||
| 994 | |||
| 995 | $sql = "DELETE FROM $TBL_EXERCISE_QUESTION |
||
| 996 | WHERE question_id = ".$id; |
||
| 997 | Database::query($sql); |
||
| 998 | |||
| 999 | $sql = "DELETE FROM $TBL_QUESTIONS |
||
| 1000 | WHERE iid = ".$id; |
||
| 1001 | Database::query($sql); |
||
| 1002 | |||
| 1003 | $sql = "DELETE FROM $TBL_REPONSES |
||
| 1004 | WHERE question_id = ".$id; |
||
| 1005 | Database::query($sql); |
||
| 1006 | |||
| 1007 | // remove the category of this question in the question_rel_category table |
||
| 1008 | $sql = "DELETE FROM $TBL_QUIZ_QUESTION_REL_CATEGORY |
||
| 1009 | WHERE |
||
| 1010 | question_id = ".$id; |
||
| 1011 | Database::query($sql); |
||
| 1012 | |||
| 1013 | // Add extra fields. |
||
| 1014 | $extraField = new ExtraFieldValue('question'); |
||
| 1015 | $extraField->deleteValuesByItem($this->iid); |
||
| 1016 | |||
| 1017 | /*api_item_property_update( |
||
| 1018 | $this->course, |
||
| 1019 | TOOL_QUIZ, |
||
| 1020 | $id, |
||
| 1021 | 'QuizQuestionDeleted', |
||
| 1022 | api_get_user_id() |
||
| 1023 | );*/ |
||
| 1024 | Event::addEvent( |
||
| 1025 | LOG_QUESTION_DELETED, |
||
| 1026 | LOG_QUESTION_ID, |
||
| 1027 | $this->iid |
||
| 1028 | ); |
||
| 1029 | //$this->removePicture(); |
||
| 1030 | } else { |
||
| 1031 | // just removes the exercise from the list |
||
| 1032 | $this->removeFromList($deleteFromEx, $courseId); |
||
| 1033 | if ('true' == api_get_setting('search_enabled') && extension_loaded('xapian')) { |
||
| 1034 | // disassociate question with this exercise |
||
| 1035 | $this->search_engine_edit($deleteFromEx, false, true); |
||
| 1036 | } |
||
| 1037 | /* |
||
| 1038 | api_item_property_update( |
||
| 1039 | $this->course, |
||
| 1040 | TOOL_QUIZ, |
||
| 1041 | $id, |
||
| 1042 | 'QuizQuestionDeleted', |
||
| 1043 | api_get_user_id() |
||
| 1044 | );*/ |
||
| 1045 | Event::addEvent( |
||
| 1046 | LOG_QUESTION_REMOVED_FROM_QUIZ, |
||
| 1047 | LOG_QUESTION_ID, |
||
| 1048 | $this->iid |
||
| 1049 | ); |
||
| 1050 | } |
||
| 1051 | |||
| 1052 | return true; |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Duplicates the question. |
||
| 1057 | * |
||
| 1058 | * @author Olivier Brouckaert |
||
| 1059 | * |
||
| 1060 | * @param array $courseInfo Course info of the destination course |
||
| 1061 | * |
||
| 1062 | * @return false|string ID of the new question |
||
| 1063 | */ |
||
| 1064 | public function duplicate($courseInfo = []) |
||
| 1139 | } |
||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * @return string |
||
| 1143 | */ |
||
| 1144 | public function get_question_type_name(): string |
||
| 1145 | { |
||
| 1146 | $labelKey = trim((string) $this->explanationLangVar); |
||
| 1147 | if ($labelKey !== '') { |
||
| 1148 | $translated = get_lang($labelKey); |
||
| 1149 | if ($translated !== $labelKey) { |
||
| 1150 | return $translated; |
||
| 1151 | } |
||
| 1152 | } |
||
| 1153 | |||
| 1154 | $def = self::$questionTypes[$this->type] ?? null; |
||
| 1155 | $className = is_array($def) ? ($def[1] ?? '') : ''; |
||
| 1156 | if ($className !== '') { |
||
| 1157 | $human = preg_replace('/(?<!^)(?=[A-Z])/', ' ', $className) ?: $className; |
||
| 1158 | $translated = get_lang($human); |
||
| 1159 | |||
| 1160 | return $translated !== $human ? $translated : $human; |
||
| 1161 | } |
||
| 1162 | |||
| 1163 | return ''; |
||
| 1164 | } |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * @param string $type |
||
| 1168 | */ |
||
| 1169 | public static function get_question_type($type) |
||
| 1170 | { |
||
| 1171 | return self::$questionTypes[$type]; |
||
| 1172 | } |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * @return array |
||
| 1176 | */ |
||
| 1177 | public static function getQuestionTypeList(): array |
||
| 1178 | { |
||
| 1179 | $list = self::$questionTypes; |
||
| 1180 | |||
| 1181 | if ('true' !== api_get_setting('enable_quiz_scenario')) { |
||
| 1182 | unset($list[HOT_SPOT_DELINEATION]); |
||
| 1183 | } |
||
| 1184 | |||
| 1185 | ksort($list, SORT_NUMERIC); |
||
| 1186 | |||
| 1187 | return $list; |
||
| 1188 | } |
||
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Returns an instance of the class corresponding to the type. |
||
| 1192 | * |
||
| 1193 | * @param int $type the type of the question |
||
| 1194 | * |
||
| 1195 | * @return $this instance of a Question subclass (or of Questionc class by default) |
||
| 1196 | */ |
||
| 1197 | public static function getInstance($type) |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Creates the form to create / edit a question |
||
| 1215 | * A subclass can redefine this function to add fields... |
||
| 1216 | * |
||
| 1217 | * @param FormValidator $form |
||
| 1218 | * @param Exercise $exercise |
||
| 1219 | */ |
||
| 1220 | public function createForm(&$form, $exercise) |
||
| 1221 | { |
||
| 1222 | $zoomOptions = api_get_setting('exercise.quiz_image_zoom', true); |
||
| 1223 | if (isset($zoomOptions['options'])) { |
||
| 1224 | $finderFolder = api_get_path(WEB_PATH).'vendor/studio-42/elfinder/'; |
||
| 1225 | echo '<!-- elFinder CSS (REQUIRED) -->'; |
||
| 1226 | echo '<link rel="stylesheet" type="text/css" media="screen" href="'.$finderFolder.'css/elfinder.full.css">'; |
||
| 1227 | echo '<link rel="stylesheet" type="text/css" media="screen" href="'.$finderFolder.'css/theme.css">'; |
||
| 1228 | |||
| 1229 | echo '<!-- elFinder JS (REQUIRED) -->'; |
||
| 1230 | echo '<script src="'.$finderFolder.'js/elfinder.full.js"></script>'; |
||
| 1231 | |||
| 1232 | echo '<!-- elFinder translation (OPTIONAL) -->'; |
||
| 1233 | $language = 'en'; |
||
| 1234 | $platformLanguage = api_get_language_isocode(); |
||
| 1235 | $iso = api_get_language_isocode($platformLanguage); |
||
| 1236 | $filePart = "vendor/studio-42/elfinder/js/i18n/elfinder.$iso.js"; |
||
| 1237 | $file = api_get_path(SYS_PATH).$filePart; |
||
| 1238 | $includeFile = ''; |
||
| 1239 | if (file_exists($file)) { |
||
| 1240 | $includeFile = '<script src="'.api_get_path(WEB_PATH).$filePart.'"></script>'; |
||
| 1241 | $language = $iso; |
||
| 1242 | } |
||
| 1243 | echo $includeFile; |
||
| 1244 | echo '<script> |
||
| 1245 | $(function() { |
||
| 1246 | $(".create_img_link").click(function(e){ |
||
| 1247 | e.preventDefault(); |
||
| 1248 | e.stopPropagation(); |
||
| 1249 | var imageZoom = $("input[name=\'imageZoom\']").val(); |
||
| 1250 | var imageWidth = $("input[name=\'imageWidth\']").val(); |
||
| 1251 | CKEDITOR.instances.questionDescription.insertHtml(\'<img id="zoom_picture" class="zoom_picture" src="\'+imageZoom+\'" data-zoom-image="\'+imageZoom+\'" width="\'+imageWidth+\'px" />\'); |
||
| 1252 | }); |
||
| 1253 | |||
| 1254 | $("input[name=\'imageZoom\']").on("click", function(){ |
||
| 1255 | var elf = $("#elfinder").elfinder({ |
||
| 1256 | url : "'.api_get_path(WEB_LIBRARY_PATH).'elfinder/connectorAction.php?'.api_get_cidreq().'", |
||
| 1257 | getFileCallback: function(file) { |
||
| 1258 | var filePath = file; //file contains the relative url. |
||
| 1259 | var imgPath = "<img src = \'"+filePath+"\'/>"; |
||
| 1260 | $("input[name=\'imageZoom\']").val(filePath.url); |
||
| 1261 | $("#elfinder").remove(); //close the window after image is selected |
||
| 1262 | }, |
||
| 1263 | startPathHash: "l2_Lw", // Sets the course driver as default |
||
| 1264 | resizable: false, |
||
| 1265 | lang: "'.$language.'" |
||
| 1266 | }).elfinder("instance"); |
||
| 1267 | }); |
||
| 1268 | }); |
||
| 1269 | </script>'; |
||
| 1270 | echo '<div id="elfinder"></div>'; |
||
| 1271 | } |
||
| 1272 | |||
| 1273 | // Question name |
||
| 1274 | if ('true' === api_get_setting('editor.save_titles_as_html')) { |
||
| 1275 | $editorConfig = ['ToolbarSet' => 'TitleAsHtml']; |
||
| 1276 | $form->addHtmlEditor( |
||
| 1277 | 'questionName', |
||
| 1278 | get_lang('Question'), |
||
| 1279 | false, |
||
| 1280 | false, |
||
| 1281 | $editorConfig |
||
| 1282 | ); |
||
| 1283 | } else { |
||
| 1284 | $form->addText('questionName', get_lang('Question')); |
||
| 1285 | } |
||
| 1286 | |||
| 1287 | $form->addRule('questionName', get_lang('Please type the question'), 'required'); |
||
| 1288 | |||
| 1289 | // Default content |
||
| 1290 | $isContent = isset($_REQUEST['isContent']) ? (int) $_REQUEST['isContent'] : null; |
||
| 1291 | |||
| 1292 | // Question type (answer type) |
||
| 1293 | $answerType = isset($_REQUEST['answerType']) ? (int) $_REQUEST['answerType'] : null; |
||
| 1294 | $form->addHidden('answerType', $answerType); |
||
| 1295 | |||
| 1296 | // HTML editor for description |
||
| 1297 | $editorConfig = [ |
||
| 1298 | 'ToolbarSet' => 'TestQuestionDescription', |
||
| 1299 | 'Height' => '150', |
||
| 1300 | ]; |
||
| 1301 | |||
| 1302 | if (!api_is_allowed_to_edit(null, true)) { |
||
| 1303 | $editorConfig['UserStatus'] = 'student'; |
||
| 1304 | } |
||
| 1305 | |||
| 1306 | $form->addButtonAdvancedSettings('advanced_params'); |
||
| 1307 | $form->addHtml('<div id="advanced_params_options" style="display:none">'); |
||
| 1308 | |||
| 1309 | if (isset($zoomOptions['options'])) { |
||
| 1310 | $form->addElement('text', 'imageZoom', get_lang('Image URL')); |
||
| 1311 | $form->addElement('text', 'imageWidth', get_lang('px width')); |
||
| 1312 | $form->addButton('btn_create_img', get_lang('Add to editor'), 'plus', 'info', 'small', 'create_img_link'); |
||
| 1313 | } |
||
| 1314 | |||
| 1315 | $form->addHtmlEditor( |
||
| 1316 | 'questionDescription', |
||
| 1317 | get_lang('Enrich question'), |
||
| 1318 | false, |
||
| 1319 | false, |
||
| 1320 | $editorConfig |
||
| 1321 | ); |
||
| 1322 | |||
| 1323 | if (MEDIA_QUESTION != $this->type) { |
||
| 1324 | // Advanced parameters. |
||
| 1325 | $form->addSelect( |
||
| 1326 | 'questionLevel', |
||
| 1327 | get_lang('Difficulty'), |
||
| 1328 | self::get_default_levels() |
||
| 1329 | ); |
||
| 1330 | |||
| 1331 | // Categories. |
||
| 1332 | $form->addSelect( |
||
| 1333 | 'questionCategory', |
||
| 1334 | get_lang('Category'), |
||
| 1335 | TestCategory::getCategoriesIdAndName() |
||
| 1336 | ); |
||
| 1337 | |||
| 1338 | $courseMedias = self::prepare_course_media_select($exercise->iId); |
||
| 1339 | $form->addSelect( |
||
| 1340 | 'parent_id', |
||
| 1341 | get_lang('Attach to media'), |
||
| 1342 | $courseMedias |
||
| 1343 | ); |
||
| 1344 | |||
| 1345 | if (EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_RANDOM == $exercise->getQuestionSelectionType() && |
||
| 1346 | ('true' === api_get_setting('exercise.allow_mandatory_question_in_category')) |
||
| 1347 | ) { |
||
| 1348 | $form->addCheckBox('mandatory', get_lang('Mandatory?')); |
||
| 1349 | } |
||
| 1350 | |||
| 1351 | $text = get_lang('Save the question'); |
||
| 1352 | switch ($this->type) { |
||
| 1353 | case UNIQUE_ANSWER: |
||
| 1354 | $buttonGroup = []; |
||
| 1355 | $buttonGroup[] = $form->addButtonSave( |
||
| 1356 | $text, |
||
| 1357 | 'submitQuestion', |
||
| 1358 | true |
||
| 1359 | ); |
||
| 1360 | $buttonGroup[] = $form->addButton( |
||
| 1361 | 'convertAnswer', |
||
| 1362 | get_lang('Convert to multiple answer'), |
||
| 1363 | 'dot-circle-o', |
||
| 1364 | 'default', |
||
| 1365 | null, |
||
| 1366 | null, |
||
| 1367 | null, |
||
| 1368 | true |
||
| 1369 | ); |
||
| 1370 | $form->addGroup($buttonGroup); |
||
| 1371 | |||
| 1372 | break; |
||
| 1373 | case MULTIPLE_ANSWER: |
||
| 1374 | $buttonGroup = []; |
||
| 1375 | $buttonGroup[] = $form->addButtonSave( |
||
| 1376 | $text, |
||
| 1377 | 'submitQuestion', |
||
| 1378 | true |
||
| 1379 | ); |
||
| 1380 | $buttonGroup[] = $form->addButton( |
||
| 1381 | 'convertAnswer', |
||
| 1382 | get_lang('Convert to unique answer'), |
||
| 1383 | 'check-square-o', |
||
| 1384 | 'default', |
||
| 1385 | null, |
||
| 1386 | null, |
||
| 1387 | null, |
||
| 1388 | true |
||
| 1389 | ); |
||
| 1390 | $form->addGroup($buttonGroup); |
||
| 1391 | |||
| 1392 | break; |
||
| 1393 | } |
||
| 1394 | } |
||
| 1395 | |||
| 1396 | $form->addElement('html', '</div>'); |
||
| 1397 | |||
| 1398 | // Sample default questions when creating from templates |
||
| 1399 | if (!isset($_GET['fromExercise'])) { |
||
| 1400 | switch ($answerType) { |
||
| 1401 | case 1: |
||
| 1402 | $this->question = get_lang('Select the good reasoning'); |
||
| 1403 | |||
| 1404 | break; |
||
| 1405 | case 2: |
||
| 1406 | $this->question = get_lang('The marasmus is a consequence of'); |
||
| 1407 | |||
| 1408 | break; |
||
| 1409 | case 3: |
||
| 1410 | $this->question = get_lang('Calculate the Body Mass Index'); |
||
| 1411 | |||
| 1412 | break; |
||
| 1413 | case 4: |
||
| 1414 | $this->question = get_lang('Order the operations'); |
||
| 1415 | |||
| 1416 | break; |
||
| 1417 | case 5: |
||
| 1418 | $this->question = get_lang('List what you consider the 10 top qualities of a good project manager?'); |
||
| 1419 | |||
| 1420 | break; |
||
| 1421 | case 9: |
||
| 1422 | $this->question = get_lang('The marasmus is a consequence of'); |
||
| 1423 | |||
| 1424 | break; |
||
| 1425 | } |
||
| 1426 | } |
||
| 1427 | |||
| 1428 | // ------------------------------------------------------------------------- |
||
| 1429 | // Adaptive scenario (success/failure) — centralised for supported types |
||
| 1430 | // ------------------------------------------------------------------------- |
||
| 1431 | $scenarioEnabled = ('true' === api_get_setting('enable_quiz_scenario')); |
||
| 1432 | $hasExercise = ($exercise instanceof Exercise); |
||
| 1433 | $isAdaptiveFeedback = $hasExercise && |
||
| 1434 | EXERCISE_FEEDBACK_TYPE_DIRECT === $exercise->getFeedbackType(); |
||
| 1435 | $supportsScenario = in_array( |
||
| 1436 | (int) $this->type, |
||
| 1437 | static::$adaptiveScenarioTypes, |
||
| 1438 | true |
||
| 1439 | ); |
||
| 1440 | |||
| 1441 | if ($scenarioEnabled && $isAdaptiveFeedback && $supportsScenario && $hasExercise) { |
||
| 1442 | // Build the question list once per exercise to feed the scenario selector |
||
| 1443 | $exercise->setQuestionList(true); |
||
| 1444 | $questionList = $exercise->getQuestionList(); |
||
| 1445 | |||
| 1446 | if (is_array($questionList) && !empty($questionList)) { |
||
| 1447 | $this->addAdaptiveScenarioFields($form, $questionList); |
||
| 1448 | // Pre-fill selector defaults when editing an existing question in this exercise. |
||
| 1449 | if (!empty($this->id)) { |
||
| 1450 | $this->loadAdaptiveScenarioDefaults($form, $exercise); |
||
| 1451 | } |
||
| 1452 | } |
||
| 1453 | } |
||
| 1454 | |||
| 1455 | if (null !== $exercise) { |
||
| 1456 | if ($exercise->questionFeedbackEnabled && $this->showFeedback($exercise)) { |
||
| 1457 | $form->addTextarea('feedback', get_lang('Feedback if not correct')); |
||
| 1458 | } |
||
| 1459 | } |
||
| 1460 | |||
| 1461 | $extraField = new ExtraField('question'); |
||
| 1462 | $extraField->addElements($form, $this->iid); |
||
| 1463 | |||
| 1464 | // Default values |
||
| 1465 | $defaults = [ |
||
| 1466 | 'questionName' => $this->question, |
||
| 1467 | 'questionDescription' => $this->description, |
||
| 1468 | 'questionLevel' => $this->level, |
||
| 1469 | 'questionCategory' => $this->category, |
||
| 1470 | 'feedback' => $this->feedback, |
||
| 1471 | 'mandatory' => $this->mandatory, |
||
| 1472 | 'parent_id' => $this->parent_id, |
||
| 1473 | ]; |
||
| 1474 | |||
| 1475 | // Came from the question pool |
||
| 1476 | if (isset($_GET['fromExercise'])) { |
||
| 1477 | $form->setDefaults($defaults); |
||
| 1478 | } |
||
| 1479 | |||
| 1480 | if (!isset($_GET['newQuestion']) || $isContent) { |
||
| 1481 | $form->setDefaults($defaults); |
||
| 1482 | } |
||
| 1483 | |||
| 1484 | /*if (!empty($_REQUEST['myid'])) { |
||
| 1485 | $form->setDefaults($defaults); |
||
| 1486 | } else { |
||
| 1487 | if ($isContent == 1) { |
||
| 1488 | $form->setDefaults($defaults); |
||
| 1489 | } |
||
| 1490 | }*/ |
||
| 1491 | } |
||
| 1492 | |||
| 1493 | /** |
||
| 1494 | * Function which process the creation of questions. |
||
| 1495 | */ |
||
| 1496 | public function processCreation(FormValidator $form, Exercise $exercise) |
||
| 1497 | { |
||
| 1498 | $this->parent_id = (int) $form->getSubmitValue('parent_id'); |
||
| 1499 | $this->updateTitle($form->getSubmitValue('questionName')); |
||
| 1500 | $this->updateDescription($form->getSubmitValue('questionDescription')); |
||
| 1501 | $this->updateLevel($form->getSubmitValue('questionLevel')); |
||
| 1502 | $this->updateCategory($form->getSubmitValue('questionCategory')); |
||
| 1503 | $this->setMandatory($form->getSubmitValue('mandatory')); |
||
| 1504 | $this->setFeedback($form->getSubmitValue('feedback')); |
||
| 1505 | |||
| 1506 | //Save normal question if NOT media |
||
| 1507 | if (MEDIA_QUESTION != $this->type) { |
||
| 1508 | $this->save($exercise); |
||
| 1509 | // modify the exercise |
||
| 1510 | $exercise->addToList($this->id); |
||
| 1511 | $exercise->update_question_positions(); |
||
| 1512 | |||
| 1513 | $params = $form->exportValues(); |
||
| 1514 | $params['item_id'] = $this->id; |
||
| 1515 | |||
| 1516 | $extraFieldValues = new ExtraFieldValue('question'); |
||
| 1517 | $extraFieldValues->saveFieldValues($params); |
||
| 1518 | } |
||
| 1519 | } |
||
| 1520 | |||
| 1521 | /** |
||
| 1522 | * Creates the form to create / edit the answers of the question. |
||
| 1523 | */ |
||
| 1524 | abstract public function createAnswersForm(FormValidator $form); |
||
| 1525 | |||
| 1526 | /** |
||
| 1527 | * Process the creation of answers. |
||
| 1528 | * |
||
| 1529 | * @param FormValidator $form |
||
| 1530 | * @param Exercise $exercise |
||
| 1531 | */ |
||
| 1532 | abstract public function processAnswersCreation($form, $exercise); |
||
| 1533 | |||
| 1534 | /** |
||
| 1535 | * Displays the menu of question types. |
||
| 1536 | * |
||
| 1537 | * @param Exercise $objExercise |
||
| 1538 | */ |
||
| 1539 | public static function displayTypeMenu(Exercise $objExercise) |
||
| 1540 | { |
||
| 1541 | if (empty($objExercise)) { |
||
| 1542 | return ''; |
||
| 1543 | } |
||
| 1544 | |||
| 1545 | $feedbackType = $objExercise->getFeedbackType(); |
||
| 1546 | $exerciseId = $objExercise->id; |
||
| 1547 | |||
| 1548 | $questionTypeList = self::getQuestionTypeList(); |
||
| 1549 | |||
| 1550 | if (!isset($feedbackType)) { |
||
| 1551 | $feedbackType = 0; |
||
| 1552 | } |
||
| 1553 | |||
| 1554 | switch ($feedbackType) { |
||
| 1555 | case EXERCISE_FEEDBACK_TYPE_DIRECT: |
||
| 1556 | // Keep original behavior: base types for adaptative tests. |
||
| 1557 | $questionTypeList = [ |
||
| 1558 | UNIQUE_ANSWER => self::$questionTypes[UNIQUE_ANSWER], |
||
| 1559 | HOT_SPOT_DELINEATION => self::$questionTypes[HOT_SPOT_DELINEATION], |
||
| 1560 | ]; |
||
| 1561 | |||
| 1562 | // Add all other non-open question types. |
||
| 1563 | $allTypes = self::getQuestionTypeList(); |
||
| 1564 | |||
| 1565 | // The task explicitly mentions NOT including free-answer questions. |
||
| 1566 | // So we only exclude FREE_ANSWER here. |
||
| 1567 | if (isset($allTypes[FREE_ANSWER])) { |
||
| 1568 | unset($allTypes[FREE_ANSWER]); |
||
| 1569 | } |
||
| 1570 | |||
| 1571 | // Append remaining types, without overriding the original ones. |
||
| 1572 | foreach ($allTypes as $typeId => $def) { |
||
| 1573 | if (!isset($questionTypeList[$typeId])) { |
||
| 1574 | $questionTypeList[$typeId] = $def; |
||
| 1575 | } |
||
| 1576 | } |
||
| 1577 | |||
| 1578 | break; |
||
| 1579 | case EXERCISE_FEEDBACK_TYPE_POPUP: |
||
| 1580 | $questionTypeList = [ |
||
| 1581 | UNIQUE_ANSWER => self::$questionTypes[UNIQUE_ANSWER], |
||
| 1582 | MULTIPLE_ANSWER => self::$questionTypes[MULTIPLE_ANSWER], |
||
| 1583 | DRAGGABLE => self::$questionTypes[DRAGGABLE], |
||
| 1584 | HOT_SPOT_DELINEATION => self::$questionTypes[HOT_SPOT_DELINEATION], |
||
| 1585 | CALCULATED_ANSWER => self::$questionTypes[CALCULATED_ANSWER], |
||
| 1586 | ]; |
||
| 1587 | |||
| 1588 | break; |
||
| 1589 | default: |
||
| 1590 | unset($questionTypeList[HOT_SPOT_DELINEATION]); |
||
| 1591 | |||
| 1592 | break; |
||
| 1593 | } |
||
| 1594 | |||
| 1595 | echo '<div class="card">'; |
||
| 1596 | echo ' <div class="card-body">'; |
||
| 1597 | echo ' <ul class="qtype-menu flex flex-wrap gap-x-2 gap-y-2 items-center justify-start w-full">'; |
||
| 1598 | foreach ($questionTypeList as $i => $type) { |
||
| 1599 | /** @var Question $type */ |
||
| 1600 | $type = new $type[1](); |
||
| 1601 | $img = $type->getTypePicture(); |
||
| 1602 | $expl = $type->getExplanation(); |
||
| 1603 | |||
| 1604 | echo ' <li class="flex items-center justify-center">'; |
||
| 1605 | |||
| 1606 | $icon = Display::url( |
||
| 1607 | Display::return_icon($img, $expl, null, ICON_SIZE_BIG), |
||
| 1608 | 'admin.php?' . api_get_cidreq() . '&' . http_build_query([ |
||
| 1609 | 'newQuestion' => 'yes', |
||
| 1610 | 'answerType' => $i, |
||
| 1611 | 'exerciseId' => $exerciseId, |
||
| 1612 | ]), |
||
| 1613 | ['title' => $expl, 'class' => 'block'] |
||
| 1614 | ); |
||
| 1615 | |||
| 1616 | if (false === $objExercise->force_edit_exercise_in_lp && $objExercise->exercise_was_added_in_lp) { |
||
| 1617 | $img = pathinfo($img); |
||
| 1618 | $img = $img['filename'].'_na.'.$img['extension']; |
||
| 1619 | $icon = Display::return_icon($img, $expl, null, ICON_SIZE_BIG); |
||
| 1620 | } |
||
| 1621 | echo $icon; |
||
| 1622 | echo ' </li>'; |
||
| 1623 | } |
||
| 1624 | |||
| 1625 | echo ' <li class="flex items-center justify-center">'; |
||
| 1626 | if ($objExercise->exercise_was_added_in_lp) { |
||
| 1627 | echo Display::getMdiIcon('database', 'ch-tool-icon-disabled', null, ICON_SIZE_BIG, get_lang('Recycle existing questions')); |
||
| 1628 | } else { |
||
| 1629 | $href = in_array($feedbackType, [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP]) |
||
| 1630 | ? 'question_pool.php?' . api_get_cidreq() . "&type=1&fromExercise={$exerciseId}" |
||
| 1631 | : 'question_pool.php?' . api_get_cidreq() . "&fromExercise={$exerciseId}"; |
||
| 1632 | |||
| 1633 | echo Display::url( |
||
| 1634 | Display::getMdiIcon('database', 'ch-tool-icon', null, ICON_SIZE_BIG, get_lang('Recycle existing questions')), |
||
| 1635 | $href, |
||
| 1636 | ['class' => 'block', 'title' => get_lang('Recycle existing questions')] |
||
| 1637 | ); |
||
| 1638 | } |
||
| 1639 | echo ' </li>'; |
||
| 1640 | |||
| 1641 | echo ' </ul>'; |
||
| 1642 | echo ' </div>'; |
||
| 1643 | echo '</div>'; |
||
| 1644 | } |
||
| 1645 | |||
| 1646 | /** |
||
| 1647 | * @param string $name |
||
| 1648 | * @param int $position |
||
| 1649 | * |
||
| 1650 | * @return CQuizQuestion|null |
||
| 1651 | */ |
||
| 1652 | public static function saveQuestionOption(CQuizQuestion $question, $name, $position = 0) |
||
| 1653 | { |
||
| 1654 | $option = new CQuizQuestionOption(); |
||
| 1655 | $option |
||
| 1656 | ->setQuestion($question) |
||
| 1657 | ->setTitle($name) |
||
| 1658 | ->setPosition($position) |
||
| 1659 | ; |
||
| 1660 | $em = Database::getManager(); |
||
| 1661 | $em->persist($option); |
||
| 1662 | $em->flush(); |
||
| 1663 | } |
||
| 1664 | |||
| 1665 | /** |
||
| 1666 | * @param int $question_id |
||
| 1667 | * @param int $course_id |
||
| 1668 | */ |
||
| 1669 | public static function deleteAllQuestionOptions($question_id, $course_id) |
||
| 1670 | { |
||
| 1671 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); |
||
| 1672 | Database::delete( |
||
| 1673 | $table, |
||
| 1674 | [ |
||
| 1675 | 'c_id = ? AND question_id = ?' => [ |
||
| 1676 | $course_id, |
||
| 1677 | $question_id, |
||
| 1678 | ], |
||
| 1679 | ] |
||
| 1680 | ); |
||
| 1681 | } |
||
| 1682 | |||
| 1683 | /** |
||
| 1684 | * @param int $question_id |
||
| 1685 | * |
||
| 1686 | * @return array |
||
| 1687 | */ |
||
| 1688 | public static function readQuestionOption($question_id) |
||
| 1689 | { |
||
| 1690 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); |
||
| 1691 | |||
| 1692 | return Database::select( |
||
| 1693 | '*', |
||
| 1694 | $table, |
||
| 1695 | [ |
||
| 1696 | 'where' => [ |
||
| 1697 | 'question_id = ?' => [ |
||
| 1698 | $question_id, |
||
| 1699 | ], |
||
| 1700 | ], |
||
| 1701 | 'order' => 'iid ASC', |
||
| 1702 | ] |
||
| 1703 | ); |
||
| 1704 | } |
||
| 1705 | |||
| 1706 | /** |
||
| 1707 | * Shows question title an description. |
||
| 1708 | * |
||
| 1709 | * @param int $counter |
||
| 1710 | * @param array $score |
||
| 1711 | * |
||
| 1712 | * @return string HTML string with the header of the question (before the answers table) |
||
| 1713 | */ |
||
| 1714 | public function return_header(Exercise $exercise, $counter = null, $score = []) |
||
| 1715 | { |
||
| 1716 | $counterLabel = ''; |
||
| 1717 | if (!empty($counter)) { |
||
| 1718 | $counterLabel = (int) $counter; |
||
| 1719 | } |
||
| 1720 | |||
| 1721 | $scoreLabel = get_lang('Wrong'); |
||
| 1722 | if (in_array($exercise->results_disabled, [ |
||
| 1723 | RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER, |
||
| 1724 | RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING, |
||
| 1725 | ]) |
||
| 1726 | ) { |
||
| 1727 | $scoreLabel = get_lang('Wrong answer. The correct one was:'); |
||
| 1728 | } |
||
| 1729 | |||
| 1730 | $class = 'error'; |
||
| 1731 | if (isset($score['pass']) && true == $score['pass']) { |
||
| 1732 | $scoreLabel = get_lang('Correct'); |
||
| 1733 | |||
| 1734 | if (in_array($exercise->results_disabled, [ |
||
| 1735 | RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER, |
||
| 1736 | RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING, |
||
| 1737 | ]) |
||
| 1738 | ) { |
||
| 1739 | $scoreLabel = get_lang('Correct answer'); |
||
| 1740 | } |
||
| 1741 | $class = 'success'; |
||
| 1742 | } |
||
| 1743 | |||
| 1744 | switch ($this->type) { |
||
| 1745 | case FREE_ANSWER: |
||
| 1746 | case UPLOAD_ANSWER: |
||
| 1747 | case ORAL_EXPRESSION: |
||
| 1748 | case ANNOTATION: |
||
| 1749 | $score['revised'] = isset($score['revised']) ? $score['revised'] : false; |
||
| 1750 | if (true == $score['revised']) { |
||
| 1751 | $scoreLabel = get_lang('Reviewed'); |
||
| 1752 | $class = ''; |
||
| 1753 | } else { |
||
| 1754 | $scoreLabel = get_lang('Not reviewed'); |
||
| 1755 | $class = 'warning'; |
||
| 1756 | if (isset($score['weight'])) { |
||
| 1757 | $weight = float_format($score['weight'], 1); |
||
| 1758 | $score['result'] = ' ? / '.$weight; |
||
| 1759 | } |
||
| 1760 | $model = ExerciseLib::getCourseScoreModel(); |
||
| 1761 | if (!empty($model)) { |
||
| 1762 | $score['result'] = ' ? '; |
||
| 1763 | } |
||
| 1764 | |||
| 1765 | $hide = ('true' === api_get_setting('exercise.hide_free_question_score')); |
||
| 1766 | if (true === $hide) { |
||
| 1767 | $score['result'] = '-'; |
||
| 1768 | } |
||
| 1769 | } |
||
| 1770 | |||
| 1771 | break; |
||
| 1772 | case UNIQUE_ANSWER: |
||
| 1773 | if (in_array($exercise->results_disabled, [ |
||
| 1774 | RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER, |
||
| 1775 | RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING, |
||
| 1776 | ]) |
||
| 1777 | ) { |
||
| 1778 | if (isset($score['user_answered'])) { |
||
| 1779 | if (false === $score['user_answered']) { |
||
| 1780 | $scoreLabel = get_lang('Unanswered'); |
||
| 1781 | $class = 'info'; |
||
| 1782 | } |
||
| 1783 | } |
||
| 1784 | } |
||
| 1785 | |||
| 1786 | break; |
||
| 1787 | } |
||
| 1788 | |||
| 1789 | // display question category, if any |
||
| 1790 | $header = ''; |
||
| 1791 | if ($exercise->display_category_name) { |
||
| 1792 | $header = TestCategory::returnCategoryAndTitle($this->id); |
||
| 1793 | } |
||
| 1794 | $show_media = ''; |
||
| 1795 | if ($show_media) { |
||
| 1796 | $header .= $this->show_media_content(); |
||
| 1797 | } |
||
| 1798 | |||
| 1799 | $scoreCurrent = [ |
||
| 1800 | 'used' => isset($score['score']) ? $score['score'] : '', |
||
| 1801 | 'missing' => isset($score['weight']) ? $score['weight'] : '', |
||
| 1802 | ]; |
||
| 1803 | $header .= Display::page_subheader2($counterLabel.'. '.$this->question); |
||
| 1804 | |||
| 1805 | $showRibbon = true; |
||
| 1806 | // dont display score for certainty degree questions |
||
| 1807 | if (MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY == $this->type) { |
||
| 1808 | $showRibbon = false; |
||
| 1809 | $ribbonResult = ('true' === api_get_setting('exercise.show_exercise_question_certainty_ribbon_result')); |
||
| 1810 | if (true === $ribbonResult) { |
||
| 1811 | $showRibbon = true; |
||
| 1812 | } |
||
| 1813 | } |
||
| 1814 | |||
| 1815 | if ($showRibbon && isset($score['result'])) { |
||
| 1816 | if (in_array($exercise->results_disabled, [ |
||
| 1817 | RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER, |
||
| 1818 | RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING, |
||
| 1819 | ]) |
||
| 1820 | ) { |
||
| 1821 | $score['result'] = null; |
||
| 1822 | } |
||
| 1823 | $header .= $exercise->getQuestionRibbon($class, $scoreLabel, $score['result'], $scoreCurrent); |
||
| 1824 | } |
||
| 1825 | |||
| 1826 | if (READING_COMPREHENSION != $this->type) { |
||
| 1827 | // Do not show the description (the text to read) if the question is of type READING_COMPREHENSION |
||
| 1828 | $header .= Display::div( |
||
| 1829 | $this->description, |
||
| 1830 | ['class' => 'question-answer-result__header-description'] |
||
| 1831 | ); |
||
| 1832 | } else { |
||
| 1833 | /** @var ReadingComprehension $this */ |
||
| 1834 | if (true === $score['pass']) { |
||
| 1835 | $message = Display::div( |
||
| 1836 | sprintf( |
||
| 1837 | get_lang( |
||
| 1838 | 'Congratulations, you have reached and correctly understood, at a speed of %s words per minute, a text of a total %s words.' |
||
| 1839 | ), |
||
| 1840 | ReadingComprehension::$speeds[$this->level], |
||
| 1841 | $this->getWordsCount() |
||
| 1842 | ) |
||
| 1843 | ); |
||
| 1844 | } else { |
||
| 1845 | $message = Display::div( |
||
| 1846 | sprintf( |
||
| 1847 | get_lang( |
||
| 1848 | 'Sorry, it seems like a speed of %s words/minute was too fast for this text of %s words.' |
||
| 1849 | ), |
||
| 1850 | ReadingComprehension::$speeds[$this->level], |
||
| 1851 | $this->getWordsCount() |
||
| 1852 | ) |
||
| 1853 | ); |
||
| 1854 | } |
||
| 1855 | $header .= $message.'<br />'; |
||
| 1856 | } |
||
| 1857 | |||
| 1858 | if ($exercise->hideComment && in_array($this->type, [HOT_SPOT, HOT_SPOT_COMBINATION])) { |
||
| 1859 | $header .= Display::return_message(get_lang('Results only available online')); |
||
| 1860 | |||
| 1861 | return $header; |
||
| 1862 | } |
||
| 1863 | |||
| 1864 | if (isset($score['pass']) && false === $score['pass']) { |
||
| 1865 | if ($this->showFeedback($exercise)) { |
||
| 1866 | $header .= $this->returnFormatFeedback(); |
||
| 1867 | } |
||
| 1868 | } |
||
| 1869 | |||
| 1870 | return Display::div( |
||
| 1871 | $header, |
||
| 1872 | ['class' => 'question-answer-result__header'] |
||
| 1873 | ); |
||
| 1874 | } |
||
| 1875 | |||
| 1876 | /** |
||
| 1877 | * @deprecated |
||
| 1878 | * Create a question from a set of parameters |
||
| 1879 | * |
||
| 1880 | * @param int $question_name Quiz ID |
||
| 1881 | * @param string $question_description Question name |
||
| 1882 | * @param int $max_score Maximum result for the question |
||
| 1883 | * @param int $type Type of question (see constants at beginning of question.class.php) |
||
| 1884 | * @param int $level Question level/category |
||
| 1885 | * @param string $quiz_id |
||
| 1886 | */ |
||
| 1887 | public function create_question( |
||
| 1888 | $quiz_id, |
||
| 1889 | $question_name, |
||
| 1890 | $question_description = '', |
||
| 1891 | $max_score = 0, |
||
| 1892 | $type = 1, |
||
| 1893 | $level = 1 |
||
| 1894 | ) { |
||
| 1895 | $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 1896 | $tbl_quiz_rel_question = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 1897 | |||
| 1898 | $quiz_id = (int) $quiz_id; |
||
| 1899 | $max_score = (float) $max_score; |
||
| 1900 | $type = (int) $type; |
||
| 1901 | $level = (int) $level; |
||
| 1902 | |||
| 1903 | // Get the max position |
||
| 1904 | $sql = "SELECT max(position) as max_position |
||
| 1905 | FROM $tbl_quiz_question q |
||
| 1906 | INNER JOIN $tbl_quiz_rel_question r |
||
| 1907 | ON |
||
| 1908 | q.iid = r.question_id AND |
||
| 1909 | quiz_id = $quiz_id"; |
||
| 1910 | $rs_max = Database::query($sql); |
||
| 1911 | $row_max = Database::fetch_object($rs_max); |
||
| 1912 | $max_position = $row_max->max_position + 1; |
||
| 1913 | |||
| 1914 | $params = [ |
||
| 1915 | 'question' => $question_name, |
||
| 1916 | 'description' => $question_description, |
||
| 1917 | 'ponderation' => $max_score, |
||
| 1918 | 'position' => $max_position, |
||
| 1919 | 'type' => $type, |
||
| 1920 | 'level' => $level, |
||
| 1921 | 'mandatory' => 0, |
||
| 1922 | ]; |
||
| 1923 | $question_id = Database::insert($tbl_quiz_question, $params); |
||
| 1924 | |||
| 1925 | if ($question_id) { |
||
| 1926 | // Get the max question_order |
||
| 1927 | $sql = "SELECT max(question_order) as max_order |
||
| 1928 | FROM $tbl_quiz_rel_question |
||
| 1929 | WHERE quiz_id = $quiz_id "; |
||
| 1930 | $rs_max_order = Database::query($sql); |
||
| 1931 | $row_max_order = Database::fetch_object($rs_max_order); |
||
| 1932 | $max_order = $row_max_order->max_order + 1; |
||
| 1933 | // Attach questions to quiz |
||
| 1934 | $sql = "INSERT INTO $tbl_quiz_rel_question (question_id, quiz_id, question_order) |
||
| 1935 | VALUES($question_id, $quiz_id, $max_order)"; |
||
| 1936 | Database::query($sql); |
||
| 1937 | } |
||
| 1938 | |||
| 1939 | return $question_id; |
||
| 1940 | } |
||
| 1941 | |||
| 1942 | /** |
||
| 1943 | * @return string |
||
| 1944 | */ |
||
| 1945 | public function getTypePicture() |
||
| 1948 | } |
||
| 1949 | |||
| 1950 | /** |
||
| 1951 | * @return string |
||
| 1952 | */ |
||
| 1953 | public function getExplanation() |
||
| 1954 | { |
||
| 1955 | return get_lang($this->explanationLangVar); |
||
| 1956 | } |
||
| 1957 | |||
| 1958 | /** |
||
| 1959 | * Get course medias. |
||
| 1960 | * |
||
| 1961 | * @param int $course_id |
||
| 1962 | * |
||
| 1963 | * @return array |
||
| 1964 | */ |
||
| 1965 | public static function get_course_medias( |
||
| 1966 | $course_id, |
||
| 1967 | $start = 0, |
||
| 1968 | $limit = 100, |
||
| 1969 | $sidx = 'question', |
||
| 1970 | $sord = 'ASC', |
||
| 1971 | $where_condition = [] |
||
| 1972 | ) { |
||
| 1973 | $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 1974 | $default_where = [ |
||
| 1975 | 'c_id = ? AND parent_id = 0 AND type = ?' => [ |
||
| 1976 | $course_id, |
||
| 1977 | MEDIA_QUESTION, |
||
| 1978 | ], |
||
| 1979 | ]; |
||
| 1980 | |||
| 1981 | return Database::select( |
||
| 1982 | '*', |
||
| 1983 | $table_question, |
||
| 1984 | [ |
||
| 1985 | 'limit' => " $start, $limit", |
||
| 1986 | 'where' => $default_where, |
||
| 1987 | 'order' => "$sidx $sord", |
||
| 1988 | ] |
||
| 1989 | ); |
||
| 1990 | } |
||
| 1991 | |||
| 1992 | /** |
||
| 1993 | * Get count course medias. |
||
| 1994 | * |
||
| 1995 | * @param int $course_id course id |
||
| 1996 | * |
||
| 1997 | * @return int |
||
| 1998 | */ |
||
| 1999 | public static function get_count_course_medias($course_id) |
||
| 2000 | { |
||
| 2001 | $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 2002 | $result = Database::select( |
||
| 2003 | 'count(*) as count', |
||
| 2004 | $table_question, |
||
| 2005 | [ |
||
| 2006 | 'where' => [ |
||
| 2007 | 'c_id = ? AND parent_id = 0 AND type = ?' => [ |
||
| 2008 | $course_id, |
||
| 2009 | MEDIA_QUESTION, |
||
| 2010 | ], |
||
| 2011 | ], |
||
| 2012 | ], |
||
| 2013 | 'first' |
||
| 2014 | ); |
||
| 2015 | |||
| 2016 | if ($result && isset($result['count'])) { |
||
| 2017 | return $result['count']; |
||
| 2018 | } |
||
| 2019 | |||
| 2020 | return 0; |
||
| 2021 | } |
||
| 2022 | |||
| 2023 | /** |
||
| 2024 | * @param int $course_id |
||
| 2025 | * |
||
| 2026 | * @return array |
||
| 2027 | */ |
||
| 2028 | public static function prepare_course_media_select(int $quizId): array |
||
| 2029 | { |
||
| 2030 | $tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 2031 | $tableRelQuestion = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 2032 | |||
| 2033 | $medias = Database::select( |
||
| 2034 | '*', |
||
| 2035 | "$tableQuestion q |
||
| 2036 | JOIN $tableRelQuestion rq ON rq.question_id = q.iid", |
||
| 2037 | [ |
||
| 2038 | 'where' => [ |
||
| 2039 | 'rq.quiz_id = ? AND (q.parent_media_id IS NULL OR q.parent_media_id = 0) AND q.type = ?' |
||
| 2040 | => [$quizId, MEDIA_QUESTION], |
||
| 2041 | ], |
||
| 2042 | 'order' => 'question ASC', |
||
| 2043 | ] |
||
| 2044 | ); |
||
| 2045 | |||
| 2046 | $mediaList = [ |
||
| 2047 | 0 => get_lang('Not linked to media'), |
||
| 2048 | ]; |
||
| 2049 | |||
| 2050 | foreach ($medias as $media) { |
||
| 2051 | $mediaList[$media['question_id']] = empty($media['question']) |
||
| 2052 | ? get_lang('Untitled') |
||
| 2053 | : $media['question']; |
||
| 2054 | } |
||
| 2055 | |||
| 2056 | return $mediaList; |
||
| 2057 | } |
||
| 2058 | |||
| 2059 | /** |
||
| 2060 | * @return array |
||
| 2061 | */ |
||
| 2062 | public static function get_default_levels() |
||
| 2063 | { |
||
| 2064 | return [ |
||
| 2065 | 1 => 1, |
||
| 2066 | 2 => 2, |
||
| 2067 | 3 => 3, |
||
| 2068 | 4 => 4, |
||
| 2069 | 5 => 5, |
||
| 2070 | ]; |
||
| 2071 | } |
||
| 2072 | |||
| 2073 | /** |
||
| 2074 | * @return string |
||
| 2075 | */ |
||
| 2076 | public function show_media_content() |
||
| 2077 | { |
||
| 2078 | $html = ''; |
||
| 2079 | if (0 != $this->parent_id) { |
||
| 2080 | $parent_question = self::read($this->parent_id); |
||
| 2081 | $html = $parent_question->show_media_content(); |
||
| 2082 | } else { |
||
| 2083 | $html .= Display::page_subheader($this->selectTitle()); |
||
| 2084 | $html .= $this->selectDescription(); |
||
| 2085 | } |
||
| 2086 | |||
| 2087 | return $html; |
||
| 2088 | } |
||
| 2089 | |||
| 2090 | /** |
||
| 2091 | * Swap between unique and multiple type answers. |
||
| 2092 | * |
||
| 2093 | * @return UniqueAnswer|MultipleAnswer |
||
| 2094 | */ |
||
| 2095 | public function swapSimpleAnswerTypes() |
||
| 2096 | { |
||
| 2097 | $oppositeAnswers = [ |
||
| 2098 | UNIQUE_ANSWER => MULTIPLE_ANSWER, |
||
| 2099 | MULTIPLE_ANSWER => UNIQUE_ANSWER, |
||
| 2100 | ]; |
||
| 2101 | $this->type = $oppositeAnswers[$this->type]; |
||
| 2102 | Database::update( |
||
| 2103 | Database::get_course_table(TABLE_QUIZ_QUESTION), |
||
| 2104 | ['type' => $this->type], |
||
| 2105 | ['c_id = ? AND id = ?' => [$this->course['real_id'], $this->id]] |
||
| 2106 | ); |
||
| 2107 | $answerClasses = [ |
||
| 2108 | UNIQUE_ANSWER => 'UniqueAnswer', |
||
| 2109 | MULTIPLE_ANSWER => 'MultipleAnswer', |
||
| 2110 | MULTIPLE_ANSWER_DROPDOWN => 'MultipleAnswerDropdown', |
||
| 2111 | MULTIPLE_ANSWER_DROPDOWN_COMBINATION => 'MultipleAnswerDropdownCombination', |
||
| 2112 | ]; |
||
| 2113 | $swappedAnswer = new $answerClasses[$this->type](); |
||
| 2114 | foreach ($this as $key => $value) { |
||
| 2115 | $swappedAnswer->$key = $value; |
||
| 2116 | } |
||
| 2117 | |||
| 2118 | return $swappedAnswer; |
||
| 2119 | } |
||
| 2120 | |||
| 2121 | /** |
||
| 2122 | * @param array $score |
||
| 2123 | * |
||
| 2124 | * @return bool |
||
| 2125 | */ |
||
| 2126 | public function isQuestionWaitingReview($score) |
||
| 2127 | { |
||
| 2128 | $isReview = false; |
||
| 2129 | if (!empty($score)) { |
||
| 2130 | if (!empty($score['comments']) || $score['score'] > 0) { |
||
| 2131 | $isReview = true; |
||
| 2132 | } |
||
| 2133 | } |
||
| 2134 | |||
| 2135 | return $isReview; |
||
| 2136 | } |
||
| 2137 | |||
| 2138 | /** |
||
| 2139 | * @param string $value |
||
| 2140 | */ |
||
| 2141 | public function setFeedback($value) |
||
| 2142 | { |
||
| 2143 | $this->feedback = $value; |
||
| 2144 | } |
||
| 2145 | |||
| 2146 | /** |
||
| 2147 | * @param Exercise $exercise |
||
| 2148 | * |
||
| 2149 | * @return bool |
||
| 2150 | */ |
||
| 2151 | public function showFeedback($exercise) |
||
| 2152 | { |
||
| 2153 | if (false === $exercise->hideComment) { |
||
| 2154 | return false; |
||
| 2155 | } |
||
| 2156 | |||
| 2157 | return |
||
| 2158 | in_array($this->type, $this->questionTypeWithFeedback) && |
||
| 2159 | EXERCISE_FEEDBACK_TYPE_EXAM != $exercise->getFeedbackType(); |
||
| 2160 | } |
||
| 2161 | |||
| 2162 | /** |
||
| 2163 | * @return string |
||
| 2164 | */ |
||
| 2165 | public function returnFormatFeedback() |
||
| 2166 | { |
||
| 2167 | return '<br />'.Display::return_message($this->feedback, 'normal', false); |
||
| 2168 | } |
||
| 2169 | |||
| 2170 | /** |
||
| 2171 | * Check if this question exists in another exercise. |
||
| 2172 | * |
||
| 2173 | * @throws \Doctrine\ORM\Query\QueryException |
||
| 2174 | * |
||
| 2175 | * @return bool |
||
| 2176 | */ |
||
| 2177 | public function existsInAnotherExercise() |
||
| 2178 | { |
||
| 2179 | $count = $this->getCountExercise(); |
||
| 2180 | |||
| 2181 | return $count > 1; |
||
| 2182 | } |
||
| 2183 | |||
| 2184 | /** |
||
| 2185 | * @throws \Doctrine\ORM\Query\QueryException |
||
| 2186 | * |
||
| 2187 | * @return int |
||
| 2188 | */ |
||
| 2189 | public function getCountExercise() |
||
| 2190 | { |
||
| 2191 | $em = Database::getManager(); |
||
| 2192 | |||
| 2193 | $count = $em |
||
| 2194 | ->createQuery(' |
||
| 2195 | SELECT COUNT(qq.iid) FROM ChamiloCourseBundle:CQuizRelQuestion qq |
||
| 2196 | WHERE qq.question = :id |
||
| 2197 | ') |
||
| 2198 | ->setParameters(['id' => (int) $this->id]) |
||
| 2199 | ->getSingleScalarResult(); |
||
| 2200 | |||
| 2201 | return (int) $count; |
||
| 2202 | } |
||
| 2203 | |||
| 2204 | /** |
||
| 2205 | * Check if this question exists in another exercise. |
||
| 2206 | * |
||
| 2207 | * @throws \Doctrine\ORM\Query\QueryException |
||
| 2208 | */ |
||
| 2209 | public function getExerciseListWhereQuestionExists() |
||
| 2210 | { |
||
| 2211 | $em = Database::getManager(); |
||
| 2212 | |||
| 2213 | return $em |
||
| 2214 | ->createQuery(' |
||
| 2215 | SELECT e |
||
| 2216 | FROM ChamiloCourseBundle:CQuizRelQuestion qq |
||
| 2217 | JOIN ChamiloCourseBundle:CQuiz e |
||
| 2218 | WHERE e.iid = qq.exerciceId AND qq.questionId = :id |
||
| 2219 | ') |
||
| 2220 | ->setParameters(['id' => (int) $this->id]) |
||
| 2221 | ->getResult(); |
||
| 2222 | } |
||
| 2223 | |||
| 2224 | /** |
||
| 2225 | * @return int |
||
| 2226 | */ |
||
| 2227 | public function countAnswers() |
||
| 2228 | { |
||
| 2229 | $result = Database::select( |
||
| 2230 | 'COUNT(1) AS c', |
||
| 2231 | Database::get_course_table(TABLE_QUIZ_ANSWER), |
||
| 2232 | ['where' => ['question_id = ?' => [$this->id]]], |
||
| 2233 | 'first' |
||
| 2234 | ); |
||
| 2235 | |||
| 2236 | return (int) $result['c']; |
||
| 2237 | } |
||
| 2238 | |||
| 2239 | /** |
||
| 2240 | * Add adaptive scenario selector fields (success/failure) to the question form. |
||
| 2241 | * |
||
| 2242 | * @param FormValidator $form |
||
| 2243 | * @param array $questionList List of question IDs from the exercise. |
||
| 2244 | */ |
||
| 2245 | protected function addAdaptiveScenarioFields(FormValidator $form, array $questionList): void |
||
| 2246 | { |
||
| 2247 | // Section header |
||
| 2248 | $form->addHtml('<h4 class="m-4">'.get_lang('Adaptive behavior (success/failure)').'</h4>'); |
||
| 2249 | |||
| 2250 | // Options for redirection behavior |
||
| 2251 | $questionListOptions = [ |
||
| 2252 | '' => get_lang('Select destination'), |
||
| 2253 | 'repeat'=> get_lang('Repeat question'), |
||
| 2254 | '-1' => get_lang('End of test'), |
||
| 2255 | 'url' => get_lang('Other (custom URL)'), |
||
| 2256 | ]; |
||
| 2257 | |||
| 2258 | // Append available questions to the dropdown |
||
| 2259 | foreach ($questionList as $index => $qid) { |
||
| 2260 | if (!is_numeric($qid)) { |
||
| 2261 | continue; |
||
| 2262 | } |
||
| 2263 | |||
| 2264 | $q = self::read((int) $qid); |
||
| 2265 | if (!$q) { |
||
| 2266 | continue; |
||
| 2267 | } |
||
| 2268 | |||
| 2269 | $questionListOptions[(string) $qid] = 'Q'.$index.': '.strip_tags($q->selectTitle()); |
||
| 2270 | } |
||
| 2271 | |||
| 2272 | // Success selector and optional URL field |
||
| 2273 | $form->addSelect( |
||
| 2274 | 'scenario_success_selector', |
||
| 2275 | get_lang('On success'), |
||
| 2276 | $questionListOptions, |
||
| 2277 | ['id' => 'scenario_success_selector'] |
||
| 2278 | ); |
||
| 2279 | $form->addText( |
||
| 2280 | 'scenario_success_url', |
||
| 2281 | get_lang('Custom URL'), |
||
| 2282 | false, |
||
| 2283 | [ |
||
| 2284 | 'class' => 'form-control mb-5', |
||
| 2285 | 'id' => 'scenario_success_url', |
||
| 2286 | 'placeholder' => '/main/lp/134', |
||
| 2287 | ] |
||
| 2288 | ); |
||
| 2289 | |||
| 2290 | // Failure selector and optional URL field |
||
| 2291 | $form->addSelect( |
||
| 2292 | 'scenario_failure_selector', |
||
| 2293 | get_lang('On failure'), |
||
| 2294 | $questionListOptions, |
||
| 2295 | ['id' => 'scenario_failure_selector'] |
||
| 2296 | ); |
||
| 2297 | $form->addText( |
||
| 2298 | 'scenario_failure_url', |
||
| 2299 | get_lang('Custom URL'), |
||
| 2300 | false, |
||
| 2301 | [ |
||
| 2302 | 'class' => 'form-control mb-5', |
||
| 2303 | 'id' => 'scenario_failure_url', |
||
| 2304 | 'placeholder' => '/main/lp/134', |
||
| 2305 | ] |
||
| 2306 | ); |
||
| 2307 | |||
| 2308 | // JavaScript to toggle custom URL fields when 'url' is selected |
||
| 2309 | $form->addHtml(' |
||
| 2310 | <script> |
||
| 2311 | function toggleScenarioUrlFields() { |
||
| 2312 | var successSelector = document.getElementById("scenario_success_selector"); |
||
| 2313 | var successUrlRow = document.getElementById("scenario_success_url").parentNode.parentNode; |
||
| 2314 | |||
| 2315 | var failureSelector = document.getElementById("scenario_failure_selector"); |
||
| 2316 | var failureUrlRow = document.getElementById("scenario_failure_url").parentNode.parentNode; |
||
| 2317 | |||
| 2318 | if (successSelector && successSelector.value === "url") { |
||
| 2319 | successUrlRow.style.display = "table-row"; |
||
| 2320 | } else { |
||
| 2321 | successUrlRow.style.display = "none"; |
||
| 2322 | } |
||
| 2323 | |||
| 2324 | if (failureSelector && failureSelector.value === "url") { |
||
| 2325 | failureUrlRow.style.display = "table-row"; |
||
| 2326 | } else { |
||
| 2327 | failureUrlRow.style.display = "none"; |
||
| 2328 | } |
||
| 2329 | } |
||
| 2330 | |||
| 2331 | document.addEventListener("DOMContentLoaded", toggleScenarioUrlFields); |
||
| 2332 | document.getElementById("scenario_success_selector") |
||
| 2333 | .addEventListener("change", toggleScenarioUrlFields); |
||
| 2334 | document.getElementById("scenario_failure_selector") |
||
| 2335 | .addEventListener("change", toggleScenarioUrlFields); |
||
| 2336 | </script> |
||
| 2337 | '); |
||
| 2338 | } |
||
| 2339 | |||
| 2340 | /** |
||
| 2341 | * Persist adaptive scenario (success/failure) configuration for this question. |
||
| 2342 | * |
||
| 2343 | * This stores the "destination" JSON in TABLE_QUIZ_TEST_QUESTION for the |
||
| 2344 | * current (question, exercise) pair. It is intended to be called from |
||
| 2345 | * processAnswersCreation() implementations. |
||
| 2346 | * |
||
| 2347 | * @param FormValidator $form |
||
| 2348 | * @param Exercise $exercise |
||
| 2349 | */ |
||
| 2350 | public function saveAdaptiveScenario(FormValidator $form, Exercise $exercise): void |
||
| 2351 | { |
||
| 2352 | // Global feature flag disabled → nothing to do. |
||
| 2353 | if ('true' !== api_get_setting('enable_quiz_scenario')) { |
||
| 2354 | return; |
||
| 2355 | } |
||
| 2356 | |||
| 2357 | // We only support adaptive scenarios when feedback is "direct". |
||
| 2358 | if (EXERCISE_FEEDBACK_TYPE_DIRECT !== $exercise->getFeedbackType()) { |
||
| 2359 | return; |
||
| 2360 | } |
||
| 2361 | |||
| 2362 | // This question type is not listed as "scenario capable". |
||
| 2363 | if (!$this->supportsAdaptiveScenario()) { |
||
| 2364 | return; |
||
| 2365 | } |
||
| 2366 | |||
| 2367 | $successSelector = trim((string) $form->getSubmitValue('scenario_success_selector')); |
||
| 2368 | $successUrl = trim((string) $form->getSubmitValue('scenario_success_url')); |
||
| 2369 | $failureSelector = trim((string) $form->getSubmitValue('scenario_failure_selector')); |
||
| 2370 | $failureUrl = trim((string) $form->getSubmitValue('scenario_failure_url')); |
||
| 2371 | |||
| 2372 | // Map "url" selector to the actual custom URL, keep other values as-is. |
||
| 2373 | $success = ('url' === $successSelector) ? $successUrl : $successSelector; |
||
| 2374 | $failure = ('url' === $failureSelector) ? $failureUrl : $failureSelector; |
||
| 2375 | |||
| 2376 | // If nothing is configured at all, avoid touching the DB. |
||
| 2377 | if ('' === $success && '' === $failure) { |
||
| 2378 | return; |
||
| 2379 | } |
||
| 2380 | |||
| 2381 | $destination = json_encode( |
||
| 2382 | [ |
||
| 2383 | 'success' => $success ?: '', |
||
| 2384 | 'failure' => $failure ?: '', |
||
| 2385 | ], |
||
| 2386 | JSON_UNESCAPED_UNICODE |
||
| 2387 | ); |
||
| 2388 | |||
| 2389 | $table = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 2390 | $questionId = (int) $this->id; |
||
| 2391 | $exerciseId = (int) $exercise->id; // Consistent with existing code in UniqueAnswer |
||
| 2392 | |||
| 2393 | if ($questionId <= 0 || $exerciseId <= 0) { |
||
| 2394 | // The (question, exercise) relation does not exist yet. |
||
| 2395 | return; |
||
| 2396 | } |
||
| 2397 | |||
| 2398 | Database::update( |
||
| 2399 | $table, |
||
| 2400 | ['destination' => $destination], |
||
| 2401 | ['question_id = ? AND quiz_id = ?' => [$questionId, $exerciseId]] |
||
| 2402 | ); |
||
| 2403 | } |
||
| 2404 | |||
| 2405 | /** |
||
| 2406 | * Pre-fill adaptive scenario fields from the stored (question, exercise) relation. |
||
| 2407 | */ |
||
| 2408 | protected function loadAdaptiveScenarioDefaults(FormValidator $form, Exercise $exercise): void |
||
| 2409 | { |
||
| 2410 | if ('true' !== api_get_setting('enable_quiz_scenario')) { |
||
| 2411 | return; |
||
| 2412 | } |
||
| 2413 | |||
| 2414 | if (!$this->supportsAdaptiveScenario()) { |
||
| 2415 | return; |
||
| 2416 | } |
||
| 2417 | |||
| 2418 | if (empty($this->id) || empty($exercise->id)) { |
||
| 2419 | return; |
||
| 2420 | } |
||
| 2421 | |||
| 2422 | $table = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 2423 | |||
| 2424 | $row = Database::select( |
||
| 2425 | 'destination', |
||
| 2426 | $table, |
||
| 2427 | [ |
||
| 2428 | 'where' => [ |
||
| 2429 | 'question_id = ? AND quiz_id = ?' => [ |
||
| 2430 | (int) $this->id, |
||
| 2431 | (int) $exercise->id, |
||
| 2432 | ], |
||
| 2433 | ], |
||
| 2434 | 'limit' => 1, |
||
| 2435 | ], |
||
| 2436 | 'first' |
||
| 2437 | ); |
||
| 2438 | |||
| 2439 | if (empty($row['destination'])) { |
||
| 2440 | return; |
||
| 2441 | } |
||
| 2442 | |||
| 2443 | $json = json_decode((string) $row['destination'], true) ?: []; |
||
| 2444 | |||
| 2445 | $defaults = []; |
||
| 2446 | |||
| 2447 | if (!empty($json['success'])) { |
||
| 2448 | if (str_starts_with($json['success'], '/')) { |
||
| 2449 | $defaults['scenario_success_selector'] = 'url'; |
||
| 2450 | $defaults['scenario_success_url'] = $json['success']; |
||
| 2451 | } else { |
||
| 2452 | $defaults['scenario_success_selector'] = $json['success']; |
||
| 2453 | } |
||
| 2454 | } |
||
| 2455 | |||
| 2456 | if (!empty($json['failure'])) { |
||
| 2457 | if (str_starts_with($json['failure'], '/')) { |
||
| 2458 | $defaults['scenario_failure_selector'] = 'url'; |
||
| 2459 | $defaults['scenario_failure_url'] = $json['failure']; |
||
| 2460 | } else { |
||
| 2461 | $defaults['scenario_failure_selector'] = $json['failure']; |
||
| 2462 | } |
||
| 2463 | } |
||
| 2464 | |||
| 2465 | if (!empty($defaults)) { |
||
| 2466 | $form->setDefaults($defaults); |
||
| 2467 | } |
||
| 2468 | } |
||
| 2469 | |||
| 2470 | /** |
||
| 2471 | * Check if this question type supports adaptive scenarios. |
||
| 2472 | */ |
||
| 2473 | protected function supportsAdaptiveScenario(): bool |
||
| 2476 | } |
||
| 2477 | } |
||
| 2478 |