| Total Complexity | 269 |
| Total Lines | 2463 |
| 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 => ['HotSpotDelineation.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 array $course_info |
||
| 128 | * @param bool $getExerciseList |
||
| 129 | * |
||
| 130 | * @return Question |
||
| 131 | * |
||
| 132 | * @author Olivier Brouckaert |
||
| 133 | */ |
||
| 134 | public static function read($id, $course_info = [], $getExerciseList = true) |
||
| 135 | { |
||
| 136 | $id = (int) $id; |
||
| 137 | if (empty($course_info)) { |
||
| 138 | $course_info = api_get_course_info(); |
||
| 139 | } |
||
| 140 | $course_id = $course_info['real_id']; |
||
| 141 | |||
| 142 | if (empty($course_id) || $course_id == -1) { |
||
| 143 | return false; |
||
| 144 | } |
||
| 145 | |||
| 146 | $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 147 | $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 148 | |||
| 149 | $sql = "SELECT * |
||
| 150 | FROM $TBL_QUESTIONS |
||
| 151 | WHERE c_id = $course_id AND id = $id "; |
||
| 152 | $result = Database::query($sql); |
||
| 153 | |||
| 154 | // if the question has been found |
||
| 155 | if ($object = Database::fetch_object($result)) { |
||
| 156 | $objQuestion = self::getInstance($object->type); |
||
| 157 | if (!empty($objQuestion)) { |
||
| 158 | $objQuestion->id = (int) $id; |
||
| 159 | $objQuestion->iid = (int) $object->iid; |
||
| 160 | $objQuestion->question = $object->question; |
||
| 161 | $objQuestion->description = $object->description; |
||
| 162 | $objQuestion->weighting = $object->ponderation; |
||
| 163 | $objQuestion->position = $object->position; |
||
| 164 | $objQuestion->type = (int) $object->type; |
||
| 165 | $objQuestion->picture = $object->picture; |
||
| 166 | $objQuestion->level = (int) $object->level; |
||
| 167 | $objQuestion->extra = $object->extra; |
||
| 168 | $objQuestion->course = $course_info; |
||
| 169 | $objQuestion->feedback = isset($object->feedback) ? $object->feedback : ''; |
||
| 170 | $objQuestion->category = TestCategory::getCategoryForQuestion($id, $course_id); |
||
| 171 | $objQuestion->code = isset($object->code) ? $object->code : ''; |
||
| 172 | |||
| 173 | if ($getExerciseList) { |
||
| 174 | $tblQuiz = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 175 | $sql = "SELECT DISTINCT q.exercice_id |
||
| 176 | FROM $TBL_EXERCISE_QUESTION q |
||
| 177 | INNER JOIN $tblQuiz e |
||
| 178 | ON e.c_id = q.c_id AND e.id = q.exercice_id |
||
| 179 | WHERE |
||
| 180 | q.c_id = $course_id AND |
||
| 181 | q.question_id = $id AND |
||
| 182 | e.active >= 0"; |
||
| 183 | |||
| 184 | $result = Database::query($sql); |
||
| 185 | |||
| 186 | // fills the array with the exercises which this question is in |
||
| 187 | if ($result) { |
||
|
|
|||
| 188 | while ($obj = Database::fetch_object($result)) { |
||
| 189 | $objQuestion->exerciseList[] = $obj->exercice_id; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return $objQuestion; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | // question not found |
||
| 199 | return false; |
||
| 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) |
||
| 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) |
||
| 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 = []) |
||
| 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 | public function uploadPicture($picture) |
||
| 783 | } |
||
| 784 | |||
| 785 | /** |
||
| 786 | * return the name for image use in hotspot question |
||
| 787 | * to be unique, name is quiz-[utc unix timestamp].jpg. |
||
| 788 | * |
||
| 789 | * @param string $prefix |
||
| 790 | * @param string $extension |
||
| 791 | * |
||
| 792 | * @return string |
||
| 793 | */ |
||
| 794 | public function generatePictureName($prefix = 'quiz-', $extension = 'jpg') |
||
| 795 | { |
||
| 796 | // image name is quiz-xxx.jpg in folder images/ |
||
| 797 | $utcTime = time(); |
||
| 798 | |||
| 799 | return $prefix.$utcTime.'.'.$extension; |
||
| 800 | } |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Deletes a hot spot picture. |
||
| 804 | * |
||
| 805 | * @return bool - true |
||
| 806 | */ |
||
| 807 | public function removePicture() |
||
| 808 | { |
||
| 809 | $picture = $this->getPicture(); |
||
| 810 | |||
| 811 | if ($picture) { |
||
| 812 | $manager = Database::getManager(); |
||
| 813 | $manager->remove($picture); |
||
| 814 | $manager->flush(); |
||
| 815 | |||
| 816 | return true; |
||
| 817 | } |
||
| 818 | |||
| 819 | return false; |
||
| 820 | } |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Exports a picture to another question. |
||
| 824 | * |
||
| 825 | * @author Olivier Brouckaert |
||
| 826 | * |
||
| 827 | * @param int $questionId - ID of the target question |
||
| 828 | * @param array $courseInfo destination course info |
||
| 829 | * |
||
| 830 | * @return bool - true if copied, otherwise false |
||
| 831 | */ |
||
| 832 | public function exportPicture($questionId, $courseInfo) |
||
| 833 | { |
||
| 834 | if (empty($questionId) || empty($courseInfo)) { |
||
| 835 | return false; |
||
| 836 | } |
||
| 837 | |||
| 838 | $course_id = $courseInfo['real_id']; |
||
| 839 | $destination_path = $this->getHotSpotFolderInCourse($courseInfo); |
||
| 840 | |||
| 841 | if (empty($destination_path)) { |
||
| 842 | return false; |
||
| 843 | } |
||
| 844 | |||
| 845 | $source_path = $this->getHotSpotFolderInCourse(); |
||
| 846 | |||
| 847 | // if the question has got an ID and if the picture exists |
||
| 848 | if (!$this->id || empty($this->picture)) { |
||
| 849 | return false; |
||
| 850 | } |
||
| 851 | |||
| 852 | $sourcePictureName = $this->getPictureFilename($course_id); |
||
| 853 | $picture = $this->generatePictureName(); |
||
| 854 | $result = false; |
||
| 855 | if (file_exists($source_path.'/'.$sourcePictureName)) { |
||
| 856 | // for backward compatibility |
||
| 857 | $result = copy( |
||
| 858 | $source_path.'/'.$sourcePictureName, |
||
| 859 | $destination_path.'/'.$picture |
||
| 860 | ); |
||
| 861 | } else { |
||
| 862 | $imageInfo = DocumentManager::get_document_data_by_id( |
||
| 863 | $this->picture, |
||
| 864 | $courseInfo['code'] |
||
| 865 | ); |
||
| 866 | if (file_exists($imageInfo['absolute_path'])) { |
||
| 867 | $result = @copy( |
||
| 868 | $imageInfo['absolute_path'], |
||
| 869 | $destination_path.'/'.$picture |
||
| 870 | ); |
||
| 871 | } |
||
| 872 | } |
||
| 873 | |||
| 874 | // If copy was correct then add to the database |
||
| 875 | if (!$result) { |
||
| 876 | return false; |
||
| 877 | } |
||
| 878 | |||
| 879 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 880 | $sql = "UPDATE $table SET |
||
| 881 | picture = '".Database::escape_string($picture)."' |
||
| 882 | WHERE c_id = $course_id AND id='".intval($questionId)."'"; |
||
| 883 | Database::query($sql); |
||
| 884 | |||
| 885 | $documentId = DocumentManager::addDocument( |
||
| 886 | $courseInfo, |
||
| 887 | '/images/'.$picture, |
||
| 888 | 'file', |
||
| 889 | filesize($destination_path.'/'.$picture), |
||
| 890 | $picture |
||
| 891 | ); |
||
| 892 | |||
| 893 | if (!$documentId) { |
||
| 894 | return false; |
||
| 895 | } |
||
| 896 | |||
| 897 | return true; |
||
| 898 | } |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Set title. |
||
| 902 | * |
||
| 903 | * @param string $title |
||
| 904 | */ |
||
| 905 | public function setTitle($title) |
||
| 908 | } |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Sets extra info. |
||
| 912 | * |
||
| 913 | * @param string $extra |
||
| 914 | */ |
||
| 915 | public function setExtra($extra) |
||
| 918 | } |
||
| 919 | |||
| 920 | /** |
||
| 921 | * updates the question in the data base |
||
| 922 | * if an exercise ID is provided, we add that exercise ID into the exercise list. |
||
| 923 | * |
||
| 924 | * @author Olivier Brouckaert |
||
| 925 | * |
||
| 926 | * @param Exercise $exercise |
||
| 927 | */ |
||
| 928 | public function save($exercise) |
||
| 929 | { |
||
| 930 | $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 931 | $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 932 | $em = Database::getManager(); |
||
| 933 | $exerciseId = $exercise->id; |
||
| 934 | |||
| 935 | $id = $this->id; |
||
| 936 | $question = $this->question; |
||
| 937 | $description = $this->description; |
||
| 938 | $weighting = $this->weighting; |
||
| 939 | $position = $this->position; |
||
| 940 | $type = $this->type; |
||
| 941 | $picture = $this->picture; |
||
| 942 | $level = $this->level; |
||
| 943 | $extra = $this->extra; |
||
| 944 | $c_id = $this->course['real_id']; |
||
| 945 | $categoryId = $this->category; |
||
| 946 | |||
| 947 | // question already exists |
||
| 948 | if (!empty($id)) { |
||
| 949 | $params = [ |
||
| 950 | 'question' => $question, |
||
| 951 | 'description' => $description, |
||
| 952 | 'ponderation' => $weighting, |
||
| 953 | 'position' => $position, |
||
| 954 | 'type' => $type, |
||
| 955 | 'picture' => $picture, |
||
| 956 | 'extra' => $extra, |
||
| 957 | 'level' => $level, |
||
| 958 | ]; |
||
| 959 | if ($exercise->questionFeedbackEnabled) { |
||
| 960 | $params['feedback'] = $this->feedback; |
||
| 961 | } |
||
| 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 | $this->search_engine_edit($exerciseId); |
||
| 987 | } |
||
| 988 | } else { |
||
| 989 | // Creates a new question |
||
| 990 | $sql = "SELECT max(position) |
||
| 991 | FROM $TBL_QUESTIONS as question, |
||
| 992 | $TBL_EXERCISE_QUESTION as test_question |
||
| 993 | WHERE |
||
| 994 | question.id = test_question.question_id AND |
||
| 995 | test_question.exercice_id = ".$exerciseId." AND |
||
| 996 | question.c_id = $c_id AND |
||
| 997 | test_question.c_id = $c_id "; |
||
| 998 | $result = Database::query($sql); |
||
| 999 | $current_position = Database::result($result, 0, 0); |
||
| 1000 | $this->updatePosition($current_position + 1); |
||
| 1001 | $position = $this->position; |
||
| 1002 | |||
| 1003 | $params = [ |
||
| 1004 | 'c_id' => $c_id, |
||
| 1005 | 'question' => $question, |
||
| 1006 | 'description' => $description, |
||
| 1007 | 'ponderation' => $weighting, |
||
| 1008 | 'position' => $position, |
||
| 1009 | 'type' => $type, |
||
| 1010 | 'picture' => $picture, |
||
| 1011 | 'extra' => $extra, |
||
| 1012 | 'level' => $level, |
||
| 1013 | ]; |
||
| 1014 | |||
| 1015 | if ($exercise->questionFeedbackEnabled) { |
||
| 1016 | $params['feedback'] = $this->feedback; |
||
| 1017 | } |
||
| 1018 | $this->id = Database::insert($TBL_QUESTIONS, $params); |
||
| 1019 | |||
| 1020 | if ($this->id) { |
||
| 1021 | $sql = "UPDATE $TBL_QUESTIONS SET id = iid WHERE iid = {$this->id}"; |
||
| 1022 | Database::query($sql); |
||
| 1023 | |||
| 1024 | Event::addEvent( |
||
| 1025 | LOG_QUESTION_CREATED, |
||
| 1026 | LOG_QUESTION_ID, |
||
| 1027 | $this->id |
||
| 1028 | ); |
||
| 1029 | |||
| 1030 | api_item_property_update( |
||
| 1031 | $this->course, |
||
| 1032 | TOOL_QUIZ, |
||
| 1033 | $this->id, |
||
| 1034 | 'QuizQuestionAdded', |
||
| 1035 | api_get_user_id() |
||
| 1036 | ); |
||
| 1037 | |||
| 1038 | // If hotspot, create first answer |
||
| 1039 | if ($type == HOT_SPOT || $type == HOT_SPOT_ORDER) { |
||
| 1040 | $quizAnswer = new CQuizAnswer(); |
||
| 1041 | $quizAnswer |
||
| 1042 | ->setCId($c_id) |
||
| 1043 | ->setQuestionId($this->id) |
||
| 1044 | ->setAnswer('') |
||
| 1045 | ->setPonderation(10) |
||
| 1046 | ->setPosition(1) |
||
| 1047 | ->setHotspotCoordinates('0;0|0|0') |
||
| 1048 | ->setHotspotType('square'); |
||
| 1049 | |||
| 1050 | $em->persist($quizAnswer); |
||
| 1051 | $em->flush(); |
||
| 1052 | |||
| 1053 | $id = $quizAnswer->getIid(); |
||
| 1054 | |||
| 1055 | if ($id) { |
||
| 1056 | $quizAnswer |
||
| 1057 | ->setId($id) |
||
| 1058 | ->setIdAuto($id); |
||
| 1059 | |||
| 1060 | $em->merge($quizAnswer); |
||
| 1061 | $em->flush(); |
||
| 1062 | } |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | if ($type == HOT_SPOT_DELINEATION) { |
||
| 1066 | $quizAnswer = new CQuizAnswer(); |
||
| 1067 | $quizAnswer |
||
| 1068 | ->setCId($c_id) |
||
| 1069 | ->setQuestionId($this->id) |
||
| 1070 | ->setAnswer('') |
||
| 1071 | ->setPonderation(10) |
||
| 1072 | ->setPosition(1) |
||
| 1073 | ->setHotspotCoordinates('0;0|0|0') |
||
| 1074 | ->setHotspotType('delineation'); |
||
| 1075 | |||
| 1076 | $em->persist($quizAnswer); |
||
| 1077 | $em->flush(); |
||
| 1078 | |||
| 1079 | $id = $quizAnswer->getIid(); |
||
| 1080 | |||
| 1081 | if ($id) { |
||
| 1082 | $quizAnswer |
||
| 1083 | ->setId($id) |
||
| 1084 | ->setIdAuto($id); |
||
| 1085 | |||
| 1086 | $em->merge($quizAnswer); |
||
| 1087 | $em->flush(); |
||
| 1088 | } |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | if (api_get_setting('search_enabled') === 'true') { |
||
| 1092 | $this->search_engine_edit($exerciseId, true); |
||
| 1093 | } |
||
| 1094 | } |
||
| 1095 | } |
||
| 1096 | |||
| 1097 | // if the question is created in an exercise |
||
| 1098 | if (!empty($exerciseId)) { |
||
| 1099 | // adds the exercise into the exercise list of this question |
||
| 1100 | $this->addToList($exerciseId, true); |
||
| 1101 | } |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * @param int $exerciseId |
||
| 1106 | * @param bool $addQs |
||
| 1107 | * @param bool $rmQs |
||
| 1108 | */ |
||
| 1109 | public function search_engine_edit( |
||
| 1247 | } |
||
| 1248 | } |
||
| 1249 | } |
||
| 1250 | } |
||
| 1251 | } |
||
| 1252 | |||
| 1253 | /** |
||
| 1254 | * adds an exercise into the exercise list. |
||
| 1255 | * |
||
| 1256 | * @author Olivier Brouckaert |
||
| 1257 | * |
||
| 1258 | * @param int $exerciseId - exercise ID |
||
| 1259 | * @param bool $fromSave - from $this->save() or not |
||
| 1260 | */ |
||
| 1261 | public function addToList($exerciseId, $fromSave = false) |
||
| 1282 | } |
||
| 1283 | } |
||
| 1284 | } |
||
| 1285 | |||
| 1286 | /** |
||
| 1287 | * removes an exercise from the exercise list. |
||
| 1288 | * |
||
| 1289 | * @author Olivier Brouckaert |
||
| 1290 | * |
||
| 1291 | * @param int $exerciseId - exercise ID |
||
| 1292 | * @param int $courseId |
||
| 1293 | * |
||
| 1294 | * @return bool - true if removed, otherwise false |
||
| 1295 | */ |
||
| 1296 | public function removeFromList($exerciseId, $courseId = 0) |
||
| 1297 | { |
||
| 1298 | $table = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 1299 | $id = (int) $this->id; |
||
| 1300 | $exerciseId = (int) $exerciseId; |
||
| 1301 | |||
| 1302 | // searches the position of the exercise ID in the list |
||
| 1303 | $pos = array_search($exerciseId, $this->exerciseList); |
||
| 1304 | $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId; |
||
| 1305 | |||
| 1306 | // exercise not found |
||
| 1307 | if ($pos === false) { |
||
| 1308 | return false; |
||
| 1309 | } else { |
||
| 1310 | // deletes the position in the array containing the wanted exercise ID |
||
| 1311 | unset($this->exerciseList[$pos]); |
||
| 1312 | //update order of other elements |
||
| 1313 | $sql = "SELECT question_order |
||
| 1314 | FROM $table |
||
| 1315 | WHERE |
||
| 1316 | c_id = $courseId AND |
||
| 1317 | question_id = $id AND |
||
| 1318 | exercice_id = $exerciseId"; |
||
| 1319 | $res = Database::query($sql); |
||
| 1320 | if (Database::num_rows($res) > 0) { |
||
| 1321 | $row = Database::fetch_array($res); |
||
| 1322 | if (!empty($row['question_order'])) { |
||
| 1323 | $sql = "UPDATE $table |
||
| 1324 | SET question_order = question_order-1 |
||
| 1325 | WHERE |
||
| 1326 | c_id = $courseId AND |
||
| 1327 | exercice_id = $exerciseId AND |
||
| 1328 | question_order > ".$row['question_order']; |
||
| 1329 | Database::query($sql); |
||
| 1330 | } |
||
| 1331 | } |
||
| 1332 | |||
| 1333 | $sql = "DELETE FROM $table |
||
| 1334 | WHERE |
||
| 1335 | c_id = $courseId AND |
||
| 1336 | question_id = $id AND |
||
| 1337 | exercice_id = $exerciseId"; |
||
| 1338 | Database::query($sql); |
||
| 1339 | |||
| 1340 | return true; |
||
| 1341 | } |
||
| 1342 | } |
||
| 1343 | |||
| 1344 | /** |
||
| 1345 | * Deletes a question from the database |
||
| 1346 | * the parameter tells if the question is removed from all exercises (value = 0), |
||
| 1347 | * or just from one exercise (value = exercise ID). |
||
| 1348 | * |
||
| 1349 | * @author Olivier Brouckaert |
||
| 1350 | * |
||
| 1351 | * @param int $deleteFromEx - exercise ID if the question is only removed from one exercise |
||
| 1352 | * |
||
| 1353 | * @return bool |
||
| 1354 | */ |
||
| 1355 | public function delete($deleteFromEx = 0) |
||
| 1356 | { |
||
| 1357 | if (empty($this->course)) { |
||
| 1358 | return false; |
||
| 1359 | } |
||
| 1360 | |||
| 1361 | $courseId = $this->course['real_id']; |
||
| 1362 | |||
| 1363 | if (empty($courseId)) { |
||
| 1364 | return false; |
||
| 1365 | } |
||
| 1366 | |||
| 1367 | $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 1368 | $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 1369 | $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 1370 | $TBL_QUIZ_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); |
||
| 1371 | |||
| 1372 | $id = (int) $this->id; |
||
| 1373 | |||
| 1374 | // if the question must be removed from all exercises |
||
| 1375 | if (!$deleteFromEx) { |
||
| 1376 | //update the question_order of each question to avoid inconsistencies |
||
| 1377 | $sql = "SELECT exercice_id, question_order |
||
| 1378 | FROM $TBL_EXERCISE_QUESTION |
||
| 1379 | WHERE c_id = $courseId AND question_id = ".$id; |
||
| 1380 | |||
| 1381 | $res = Database::query($sql); |
||
| 1382 | if (Database::num_rows($res) > 0) { |
||
| 1383 | while ($row = Database::fetch_array($res)) { |
||
| 1384 | if (!empty($row['question_order'])) { |
||
| 1385 | $sql = "UPDATE $TBL_EXERCISE_QUESTION |
||
| 1386 | SET question_order = question_order-1 |
||
| 1387 | WHERE |
||
| 1388 | c_id = $courseId AND |
||
| 1389 | exercice_id = ".intval($row['exercice_id'])." AND |
||
| 1390 | question_order > ".$row['question_order']; |
||
| 1391 | Database::query($sql); |
||
| 1392 | } |
||
| 1393 | } |
||
| 1394 | } |
||
| 1395 | |||
| 1396 | $sql = "DELETE FROM $TBL_EXERCISE_QUESTION |
||
| 1397 | WHERE c_id = $courseId AND question_id = ".$id; |
||
| 1398 | Database::query($sql); |
||
| 1399 | |||
| 1400 | $sql = "DELETE FROM $TBL_QUESTIONS |
||
| 1401 | WHERE c_id = $courseId AND id = ".$id; |
||
| 1402 | Database::query($sql); |
||
| 1403 | |||
| 1404 | $sql = "DELETE FROM $TBL_REPONSES |
||
| 1405 | WHERE c_id = $courseId AND question_id = ".$id; |
||
| 1406 | Database::query($sql); |
||
| 1407 | |||
| 1408 | // remove the category of this question in the question_rel_category table |
||
| 1409 | $sql = "DELETE FROM $TBL_QUIZ_QUESTION_REL_CATEGORY |
||
| 1410 | WHERE |
||
| 1411 | c_id = $courseId AND |
||
| 1412 | question_id = ".$id; |
||
| 1413 | Database::query($sql); |
||
| 1414 | |||
| 1415 | api_item_property_update( |
||
| 1416 | $this->course, |
||
| 1417 | TOOL_QUIZ, |
||
| 1418 | $id, |
||
| 1419 | 'QuizQuestionDeleted', |
||
| 1420 | api_get_user_id() |
||
| 1421 | ); |
||
| 1422 | $this->removePicture(); |
||
| 1423 | } else { |
||
| 1424 | // just removes the exercise from the list |
||
| 1425 | $this->removeFromList($deleteFromEx, $courseId); |
||
| 1426 | if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) { |
||
| 1427 | // disassociate question with this exercise |
||
| 1428 | $this->search_engine_edit($deleteFromEx, false, true); |
||
| 1429 | } |
||
| 1430 | |||
| 1431 | api_item_property_update( |
||
| 1432 | $this->course, |
||
| 1433 | TOOL_QUIZ, |
||
| 1434 | $id, |
||
| 1435 | 'QuizQuestionDeleted', |
||
| 1436 | api_get_user_id() |
||
| 1437 | ); |
||
| 1438 | } |
||
| 1439 | |||
| 1440 | return true; |
||
| 1441 | } |
||
| 1442 | |||
| 1443 | /** |
||
| 1444 | * Duplicates the question. |
||
| 1445 | * |
||
| 1446 | * @author Olivier Brouckaert |
||
| 1447 | * |
||
| 1448 | * @param array $courseInfo Course info of the destination course |
||
| 1449 | * |
||
| 1450 | * @return false|string ID of the new question |
||
| 1451 | */ |
||
| 1452 | public function duplicate($courseInfo = []) |
||
| 1453 | { |
||
| 1454 | $courseInfo = empty($courseInfo) ? $this->course : $courseInfo; |
||
| 1455 | |||
| 1456 | if (empty($courseInfo)) { |
||
| 1457 | return false; |
||
| 1458 | } |
||
| 1459 | $questionTable = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 1460 | $TBL_QUESTION_OPTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); |
||
| 1461 | |||
| 1462 | $question = $this->question; |
||
| 1463 | $description = $this->description; |
||
| 1464 | $weighting = $this->weighting; |
||
| 1465 | $position = $this->position; |
||
| 1466 | $type = $this->type; |
||
| 1467 | $level = (int) $this->level; |
||
| 1468 | $extra = $this->extra; |
||
| 1469 | |||
| 1470 | // Using the same method used in the course copy to transform URLs |
||
| 1471 | if ($this->course['id'] != $courseInfo['id']) { |
||
| 1472 | $description = DocumentManager::replaceUrlWithNewCourseCode( |
||
| 1473 | $description, |
||
| 1474 | $this->course['code'], |
||
| 1475 | $courseInfo['id'] |
||
| 1476 | ); |
||
| 1477 | $question = DocumentManager::replaceUrlWithNewCourseCode( |
||
| 1478 | $question, |
||
| 1479 | $this->course['code'], |
||
| 1480 | $courseInfo['id'] |
||
| 1481 | ); |
||
| 1482 | } |
||
| 1483 | |||
| 1484 | $course_id = $courseInfo['real_id']; |
||
| 1485 | |||
| 1486 | // Read the source options |
||
| 1487 | $options = self::readQuestionOption($this->id, $this->course['real_id']); |
||
| 1488 | |||
| 1489 | // Inserting in the new course db / or the same course db |
||
| 1490 | $params = [ |
||
| 1491 | 'c_id' => $course_id, |
||
| 1492 | 'question' => $question, |
||
| 1493 | 'description' => $description, |
||
| 1494 | 'ponderation' => $weighting, |
||
| 1495 | 'position' => $position, |
||
| 1496 | 'type' => $type, |
||
| 1497 | 'level' => $level, |
||
| 1498 | 'extra' => $extra, |
||
| 1499 | ]; |
||
| 1500 | $newQuestionId = Database::insert($questionTable, $params); |
||
| 1501 | |||
| 1502 | if ($newQuestionId) { |
||
| 1503 | $sql = "UPDATE $questionTable |
||
| 1504 | SET id = iid |
||
| 1505 | WHERE iid = $newQuestionId"; |
||
| 1506 | Database::query($sql); |
||
| 1507 | |||
| 1508 | if (!empty($options)) { |
||
| 1509 | // Saving the quiz_options |
||
| 1510 | foreach ($options as $item) { |
||
| 1511 | $item['question_id'] = $newQuestionId; |
||
| 1512 | $item['c_id'] = $course_id; |
||
| 1513 | unset($item['id']); |
||
| 1514 | unset($item['iid']); |
||
| 1515 | $id = Database::insert($TBL_QUESTION_OPTIONS, $item); |
||
| 1516 | if ($id) { |
||
| 1517 | $sql = "UPDATE $TBL_QUESTION_OPTIONS |
||
| 1518 | SET id = iid |
||
| 1519 | WHERE iid = $id"; |
||
| 1520 | Database::query($sql); |
||
| 1521 | } |
||
| 1522 | } |
||
| 1523 | } |
||
| 1524 | |||
| 1525 | // Duplicates the picture of the hotspot |
||
| 1526 | $this->exportPicture($newQuestionId, $courseInfo); |
||
| 1527 | } |
||
| 1528 | |||
| 1529 | return $newQuestionId; |
||
| 1530 | } |
||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * @return string |
||
| 1534 | */ |
||
| 1535 | public function get_question_type_name() |
||
| 1536 | { |
||
| 1537 | $key = self::$questionTypes[$this->type]; |
||
| 1538 | |||
| 1539 | return get_lang($key[1]); |
||
| 1540 | } |
||
| 1541 | |||
| 1542 | /** |
||
| 1543 | * @param string $type |
||
| 1544 | */ |
||
| 1545 | public static function get_question_type($type) |
||
| 1546 | { |
||
| 1547 | if ($type == ORAL_EXPRESSION && api_get_setting('enable_record_audio') !== 'true') { |
||
| 1548 | return null; |
||
| 1549 | } |
||
| 1550 | |||
| 1551 | return self::$questionTypes[$type]; |
||
| 1552 | } |
||
| 1553 | |||
| 1554 | /** |
||
| 1555 | * @return array |
||
| 1556 | */ |
||
| 1557 | public static function getQuestionTypeList() |
||
| 1558 | { |
||
| 1559 | if (api_get_setting('enable_record_audio') !== 'true') { |
||
| 1560 | self::$questionTypes[ORAL_EXPRESSION] = null; |
||
| 1561 | unset(self::$questionTypes[ORAL_EXPRESSION]); |
||
| 1562 | } |
||
| 1563 | if (api_get_setting('enable_quiz_scenario') !== 'true') { |
||
| 1564 | self::$questionTypes[HOT_SPOT_DELINEATION] = null; |
||
| 1565 | unset(self::$questionTypes[HOT_SPOT_DELINEATION]); |
||
| 1566 | } |
||
| 1567 | |||
| 1568 | return self::$questionTypes; |
||
| 1569 | } |
||
| 1570 | |||
| 1571 | /** |
||
| 1572 | * Returns an instance of the class corresponding to the type. |
||
| 1573 | * |
||
| 1574 | * @param int $type the type of the question |
||
| 1575 | * |
||
| 1576 | * @return $this instance of a Question subclass (or of Questionc class by default) |
||
| 1577 | */ |
||
| 1578 | public static function getInstance($type) |
||
| 1592 | } |
||
| 1593 | |||
| 1594 | /** |
||
| 1595 | * Creates the form to create / edit a question |
||
| 1596 | * A subclass can redefine this function to add fields... |
||
| 1597 | * |
||
| 1598 | * @param FormValidator $form |
||
| 1599 | * @param Exercise $exercise |
||
| 1600 | */ |
||
| 1601 | public function createForm(&$form, $exercise) |
||
| 1763 | } |
||
| 1764 | |||
| 1765 | /*if (!empty($_REQUEST['myid'])) { |
||
| 1766 | $form->setDefaults($defaults); |
||
| 1767 | } else { |
||
| 1768 | if ($isContent == 1) { |
||
| 1769 | $form->setDefaults($defaults); |
||
| 1770 | } |
||
| 1771 | }*/ |
||
| 1772 | } |
||
| 1773 | |||
| 1774 | /** |
||
| 1775 | * function which process the creation of questions. |
||
| 1776 | * |
||
| 1777 | * @param FormValidator $form |
||
| 1778 | * @param Exercise $exercise |
||
| 1779 | */ |
||
| 1780 | public function processCreation($form, $exercise) |
||
| 1794 | } |
||
| 1795 | } |
||
| 1796 | |||
| 1797 | /** |
||
| 1798 | * abstract function which creates the form to create / edit the answers of the question. |
||
| 1799 | * |
||
| 1800 | * @param FormValidator $form |
||
| 1801 | */ |
||
| 1802 | abstract public function createAnswersForm($form); |
||
| 1803 | |||
| 1804 | /** |
||
| 1805 | * abstract function which process the creation of answers. |
||
| 1806 | * |
||
| 1807 | * @param FormValidator $form |
||
| 1808 | * @param Exercise $exercise |
||
| 1809 | */ |
||
| 1810 | abstract public function processAnswersCreation($form, $exercise); |
||
| 1811 | |||
| 1812 | /** |
||
| 1813 | * Displays the menu of question types. |
||
| 1814 | * |
||
| 1815 | * @param Exercise $objExercise |
||
| 1816 | */ |
||
| 1817 | public static function displayTypeMenu($objExercise) |
||
| 1818 | { |
||
| 1819 | $feedbackType = $objExercise->getFeedbackType(); |
||
| 1820 | $exerciseId = $objExercise->id; |
||
| 1821 | |||
| 1822 | // 1. by default we show all the question types |
||
| 1823 | $questionTypeList = self::getQuestionTypeList(); |
||
| 1824 | |||
| 1825 | if (!isset($feedbackType)) { |
||
| 1826 | $feedbackType = 0; |
||
| 1827 | } |
||
| 1828 | |||
| 1829 | switch ($feedbackType) { |
||
| 1830 | case EXERCISE_FEEDBACK_TYPE_DIRECT: |
||
| 1831 | $questionTypeList = [ |
||
| 1832 | UNIQUE_ANSWER => self::$questionTypes[UNIQUE_ANSWER], |
||
| 1833 | HOT_SPOT_DELINEATION => self::$questionTypes[HOT_SPOT_DELINEATION], |
||
| 1834 | ]; |
||
| 1835 | break; |
||
| 1836 | case EXERCISE_FEEDBACK_TYPE_POPUP: |
||
| 1837 | $questionTypeList = [ |
||
| 1838 | UNIQUE_ANSWER => self::$questionTypes[UNIQUE_ANSWER], |
||
| 1839 | MULTIPLE_ANSWER => self::$questionTypes[MULTIPLE_ANSWER], |
||
| 1840 | DRAGGABLE => self::$questionTypes[DRAGGABLE], |
||
| 1841 | HOT_SPOT_DELINEATION => self::$questionTypes[HOT_SPOT_DELINEATION], |
||
| 1842 | CALCULATED_ANSWER => self::$questionTypes[CALCULATED_ANSWER], |
||
| 1843 | ]; |
||
| 1844 | break; |
||
| 1845 | default: |
||
| 1846 | unset($questionTypeList[HOT_SPOT_DELINEATION]); |
||
| 1847 | break; |
||
| 1848 | } |
||
| 1849 | |||
| 1850 | echo '<div class="panel panel-default">'; |
||
| 1851 | echo '<div class="panel-body">'; |
||
| 1852 | echo '<ul class="question_menu">'; |
||
| 1853 | foreach ($questionTypeList as $i => $type) { |
||
| 1854 | /** @var Question $type */ |
||
| 1855 | $type = new $type[1](); |
||
| 1856 | $img = $type->getTypePicture(); |
||
| 1857 | $explanation = get_lang($type->getExplanation()); |
||
| 1858 | echo '<li>'; |
||
| 1859 | echo '<div class="icon-image">'; |
||
| 1860 | $icon = '<a href="admin.php?'.api_get_cidreq().'&newQuestion=yes&answerType='.$i.'">'. |
||
| 1861 | Display::return_icon($img, $explanation, null, ICON_SIZE_BIG).'</a>'; |
||
| 1862 | |||
| 1863 | if ($objExercise->force_edit_exercise_in_lp === false) { |
||
| 1864 | if ($objExercise->exercise_was_added_in_lp == true) { |
||
| 1865 | $img = pathinfo($img); |
||
| 1866 | $img = $img['filename'].'_na.'.$img['extension']; |
||
| 1867 | $icon = Display::return_icon($img, $explanation, null, ICON_SIZE_BIG); |
||
| 1868 | } |
||
| 1869 | } |
||
| 1870 | echo $icon; |
||
| 1871 | echo '</div>'; |
||
| 1872 | echo '</li>'; |
||
| 1873 | } |
||
| 1874 | |||
| 1875 | echo '<li>'; |
||
| 1876 | echo '<div class="icon_image_content">'; |
||
| 1877 | if ($objExercise->exercise_was_added_in_lp == true) { |
||
| 1878 | echo Display::return_icon( |
||
| 1879 | 'database_na.png', |
||
| 1880 | get_lang('GetExistingQuestion'), |
||
| 1881 | null, |
||
| 1882 | ICON_SIZE_BIG |
||
| 1883 | ); |
||
| 1884 | } else { |
||
| 1885 | if (in_array($feedbackType, [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP])) { |
||
| 1886 | echo $url = "<a href=\"question_pool.php?".api_get_cidreq()."&type=1&fromExercise=$exerciseId\">"; |
||
| 1887 | } else { |
||
| 1888 | echo $url = '<a href="question_pool.php?'.api_get_cidreq().'&fromExercise='.$exerciseId.'">'; |
||
| 1889 | } |
||
| 1890 | echo Display::return_icon( |
||
| 1891 | 'database.png', |
||
| 1892 | get_lang('GetExistingQuestion'), |
||
| 1893 | null, |
||
| 1894 | ICON_SIZE_BIG |
||
| 1895 | ); |
||
| 1896 | } |
||
| 1897 | echo '</a>'; |
||
| 1898 | echo '</div></li>'; |
||
| 1899 | echo '</ul>'; |
||
| 1900 | echo '</div>'; |
||
| 1901 | echo '</div>'; |
||
| 1902 | } |
||
| 1903 | |||
| 1904 | /** |
||
| 1905 | * @param int $question_id |
||
| 1906 | * @param string $name |
||
| 1907 | * @param int $course_id |
||
| 1908 | * @param int $position |
||
| 1909 | * |
||
| 1910 | * @return false|string |
||
| 1911 | */ |
||
| 1912 | public static function saveQuestionOption($question_id, $name, $course_id, $position = 0) |
||
| 1913 | { |
||
| 1914 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); |
||
| 1915 | $params['question_id'] = (int) $question_id; |
||
| 1916 | $params['name'] = $name; |
||
| 1917 | $params['position'] = $position; |
||
| 1918 | $params['c_id'] = $course_id; |
||
| 1919 | $result = self::readQuestionOption($question_id, $course_id); |
||
| 1920 | $last_id = Database::insert($table, $params); |
||
| 1921 | if ($last_id) { |
||
| 1922 | $sql = "UPDATE $table SET id = iid WHERE iid = $last_id"; |
||
| 1923 | Database::query($sql); |
||
| 1924 | } |
||
| 1925 | |||
| 1926 | return $last_id; |
||
| 1927 | } |
||
| 1928 | |||
| 1929 | /** |
||
| 1930 | * @param int $question_id |
||
| 1931 | * @param int $course_id |
||
| 1932 | */ |
||
| 1933 | public static function deleteAllQuestionOptions($question_id, $course_id) |
||
| 1934 | { |
||
| 1935 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); |
||
| 1936 | Database::delete( |
||
| 1937 | $table, |
||
| 1938 | [ |
||
| 1939 | 'c_id = ? AND question_id = ?' => [ |
||
| 1940 | $course_id, |
||
| 1941 | $question_id, |
||
| 1942 | ], |
||
| 1943 | ] |
||
| 1944 | ); |
||
| 1945 | } |
||
| 1946 | |||
| 1947 | /** |
||
| 1948 | * @param int $id |
||
| 1949 | * @param array $params |
||
| 1950 | * @param int $course_id |
||
| 1951 | * |
||
| 1952 | * @return bool|int |
||
| 1953 | */ |
||
| 1954 | public static function updateQuestionOption($id, $params, $course_id) |
||
| 1955 | { |
||
| 1956 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); |
||
| 1957 | $result = Database::update( |
||
| 1958 | $table, |
||
| 1959 | $params, |
||
| 1960 | ['c_id = ? AND id = ?' => [$course_id, $id]] |
||
| 1961 | ); |
||
| 1962 | |||
| 1963 | return $result; |
||
| 1964 | } |
||
| 1965 | |||
| 1966 | /** |
||
| 1967 | * @param int $question_id |
||
| 1968 | * @param int $course_id |
||
| 1969 | * |
||
| 1970 | * @return array |
||
| 1971 | */ |
||
| 1972 | public static function readQuestionOption($question_id, $course_id) |
||
| 1973 | { |
||
| 1974 | $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); |
||
| 1975 | $result = Database::select( |
||
| 1976 | '*', |
||
| 1977 | $table, |
||
| 1978 | [ |
||
| 1979 | 'where' => [ |
||
| 1980 | 'c_id = ? AND question_id = ?' => [ |
||
| 1981 | $course_id, |
||
| 1982 | $question_id, |
||
| 1983 | ], |
||
| 1984 | ], |
||
| 1985 | 'order' => 'id ASC', |
||
| 1986 | ] |
||
| 1987 | ); |
||
| 1988 | |||
| 1989 | return $result; |
||
| 1990 | } |
||
| 1991 | |||
| 1992 | /** |
||
| 1993 | * Shows question title an description. |
||
| 1994 | * |
||
| 1995 | * @param Exercise $exercise |
||
| 1996 | * @param int $counter |
||
| 1997 | * @param array $score |
||
| 1998 | * |
||
| 1999 | * @return string HTML string with the header of the question (before the answers table) |
||
| 2000 | */ |
||
| 2001 | public function return_header(Exercise $exercise, $counter = null, $score = []) |
||
| 2002 | { |
||
| 2003 | $counterLabel = ''; |
||
| 2004 | if (!empty($counter)) { |
||
| 2005 | $counterLabel = (int) $counter; |
||
| 2006 | } |
||
| 2007 | |||
| 2008 | $scoreLabel = get_lang('Wrong'); |
||
| 2009 | |||
| 2010 | if (in_array($exercise->results_disabled, [ |
||
| 2011 | RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER, |
||
| 2012 | RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING, |
||
| 2013 | ]) |
||
| 2014 | ) { |
||
| 2015 | $scoreLabel = get_lang('QuizWrongAnswerHereIsTheCorrectOne'); |
||
| 2016 | } |
||
| 2017 | |||
| 2018 | $class = 'error'; |
||
| 2019 | if (isset($score['pass']) && $score['pass'] == true) { |
||
| 2020 | $scoreLabel = get_lang('Correct'); |
||
| 2021 | |||
| 2022 | if (in_array($exercise->results_disabled, [ |
||
| 2023 | RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER, |
||
| 2024 | RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING, |
||
| 2025 | ]) |
||
| 2026 | ) { |
||
| 2027 | $scoreLabel = get_lang('CorrectAnswer'); |
||
| 2028 | } |
||
| 2029 | $class = 'success'; |
||
| 2030 | } |
||
| 2031 | |||
| 2032 | switch ($this->type) { |
||
| 2033 | case FREE_ANSWER: |
||
| 2034 | case ORAL_EXPRESSION: |
||
| 2035 | case ANNOTATION: |
||
| 2036 | $score['revised'] = isset($score['revised']) ? $score['revised'] : false; |
||
| 2037 | if ($score['revised'] == true) { |
||
| 2038 | $scoreLabel = get_lang('Revised'); |
||
| 2039 | $class = ''; |
||
| 2040 | } else { |
||
| 2041 | $scoreLabel = get_lang('NotRevised'); |
||
| 2042 | $class = 'warning'; |
||
| 2043 | if (isset($score['weight'])) { |
||
| 2044 | $weight = float_format($score['weight'], 1); |
||
| 2045 | $score['result'] = ' ? / '.$weight; |
||
| 2046 | } |
||
| 2047 | $model = ExerciseLib::getCourseScoreModel(); |
||
| 2048 | if (!empty($model)) { |
||
| 2049 | $score['result'] = ' ? '; |
||
| 2050 | } |
||
| 2051 | |||
| 2052 | $hide = api_get_configuration_value('hide_free_question_score'); |
||
| 2053 | if ($hide === true) { |
||
| 2054 | $score['result'] = '-'; |
||
| 2055 | } |
||
| 2056 | } |
||
| 2057 | break; |
||
| 2058 | case UNIQUE_ANSWER: |
||
| 2059 | if (in_array($exercise->results_disabled, [ |
||
| 2060 | RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER, |
||
| 2061 | RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING, |
||
| 2062 | ]) |
||
| 2063 | ) { |
||
| 2064 | if (isset($score['user_answered'])) { |
||
| 2065 | if ($score['user_answered'] === false) { |
||
| 2066 | $scoreLabel = get_lang('Unanswered'); |
||
| 2067 | $class = 'info'; |
||
| 2068 | } |
||
| 2069 | } |
||
| 2070 | } |
||
| 2071 | break; |
||
| 2072 | } |
||
| 2073 | |||
| 2074 | // display question category, if any |
||
| 2075 | $header = ''; |
||
| 2076 | if ($exercise->display_category_name) { |
||
| 2077 | $header = TestCategory::returnCategoryAndTitle($this->id); |
||
| 2078 | } |
||
| 2079 | $show_media = ''; |
||
| 2080 | if ($show_media) { |
||
| 2081 | $header .= $this->show_media_content(); |
||
| 2082 | } |
||
| 2083 | |||
| 2084 | $scoreCurrent = [ |
||
| 2085 | 'used' => isset($score['score']) ? $score['score'] : '', |
||
| 2086 | 'missing' => isset($score['weight']) ? $score['weight'] : '', |
||
| 2087 | ]; |
||
| 2088 | $header .= Display::page_subheader2($counterLabel.'. '.$this->question); |
||
| 2089 | |||
| 2090 | // dont display score for certainty degree questions |
||
| 2091 | if ($this->type != MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) { |
||
| 2092 | if (isset($score['result'])) { |
||
| 2093 | if (in_array($exercise->results_disabled, [ |
||
| 2094 | RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER, |
||
| 2095 | RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING, |
||
| 2096 | ]) |
||
| 2097 | ) { |
||
| 2098 | $score['result'] = null; |
||
| 2099 | } |
||
| 2100 | $header .= $exercise->getQuestionRibbon($class, $scoreLabel, $score['result'], $scoreCurrent); |
||
| 2101 | } |
||
| 2102 | } |
||
| 2103 | |||
| 2104 | if ($this->type != READING_COMPREHENSION) { |
||
| 2105 | // Do not show the description (the text to read) if the question is of type READING_COMPREHENSION |
||
| 2106 | $header .= Display::div( |
||
| 2107 | $this->description, |
||
| 2108 | ['class' => 'question_description'] |
||
| 2109 | ); |
||
| 2110 | } else { |
||
| 2111 | if ($score['pass'] == true) { |
||
| 2112 | $message = Display::div( |
||
| 2113 | sprintf( |
||
| 2114 | get_lang('ReadingQuestionCongratsSpeedXReachedForYWords'), |
||
| 2115 | ReadingComprehension::$speeds[$this->level], |
||
| 2116 | $this->getWordsCount() |
||
| 2117 | ) |
||
| 2118 | ); |
||
| 2119 | } else { |
||
| 2120 | $message = Display::div( |
||
| 2121 | sprintf( |
||
| 2122 | get_lang('ReadingQuestionCongratsSpeedXNotReachedForYWords'), |
||
| 2123 | ReadingComprehension::$speeds[$this->level], |
||
| 2124 | $this->getWordsCount() |
||
| 2125 | ) |
||
| 2126 | ); |
||
| 2127 | } |
||
| 2128 | $header .= $message.'<br />'; |
||
| 2129 | } |
||
| 2130 | |||
| 2131 | if (isset($score['pass']) && $score['pass'] === false) { |
||
| 2132 | if ($this->showFeedback($exercise)) { |
||
| 2133 | $header .= $this->returnFormatFeedback(); |
||
| 2134 | } |
||
| 2135 | } |
||
| 2136 | |||
| 2137 | return $header; |
||
| 2138 | } |
||
| 2139 | |||
| 2140 | /** |
||
| 2141 | * @deprecated |
||
| 2142 | * Create a question from a set of parameters. |
||
| 2143 | * |
||
| 2144 | * @param int Quiz ID |
||
| 2145 | * @param string Question name |
||
| 2146 | * @param int Maximum result for the question |
||
| 2147 | * @param int Type of question (see constants at beginning of question.class.php) |
||
| 2148 | * @param int Question level/category |
||
| 2149 | * @param string $quiz_id |
||
| 2150 | */ |
||
| 2151 | public function create_question( |
||
| 2152 | $quiz_id, |
||
| 2153 | $question_name, |
||
| 2154 | $question_description = '', |
||
| 2155 | $max_score = 0, |
||
| 2156 | $type = 1, |
||
| 2157 | $level = 1 |
||
| 2158 | ) { |
||
| 2159 | $course_id = api_get_course_int_id(); |
||
| 2160 | $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 2161 | $tbl_quiz_rel_question = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 2162 | |||
| 2163 | $quiz_id = intval($quiz_id); |
||
| 2164 | $max_score = (float) $max_score; |
||
| 2165 | $type = intval($type); |
||
| 2166 | $level = intval($level); |
||
| 2167 | |||
| 2168 | // Get the max position |
||
| 2169 | $sql = "SELECT max(position) as max_position |
||
| 2170 | FROM $tbl_quiz_question q |
||
| 2171 | INNER JOIN $tbl_quiz_rel_question r |
||
| 2172 | ON |
||
| 2173 | q.id = r.question_id AND |
||
| 2174 | exercice_id = $quiz_id AND |
||
| 2175 | q.c_id = $course_id AND |
||
| 2176 | r.c_id = $course_id"; |
||
| 2177 | $rs_max = Database::query($sql); |
||
| 2178 | $row_max = Database::fetch_object($rs_max); |
||
| 2179 | $max_position = $row_max->max_position + 1; |
||
| 2180 | |||
| 2181 | $params = [ |
||
| 2182 | 'c_id' => $course_id, |
||
| 2183 | 'question' => $question_name, |
||
| 2184 | 'description' => $question_description, |
||
| 2185 | 'ponderation' => $max_score, |
||
| 2186 | 'position' => $max_position, |
||
| 2187 | 'type' => $type, |
||
| 2188 | 'level' => $level, |
||
| 2189 | ]; |
||
| 2190 | $question_id = Database::insert($tbl_quiz_question, $params); |
||
| 2191 | |||
| 2192 | if ($question_id) { |
||
| 2193 | $sql = "UPDATE $tbl_quiz_question |
||
| 2194 | SET id = iid WHERE iid = $question_id"; |
||
| 2195 | Database::query($sql); |
||
| 2196 | |||
| 2197 | // Get the max question_order |
||
| 2198 | $sql = "SELECT max(question_order) as max_order |
||
| 2199 | FROM $tbl_quiz_rel_question |
||
| 2200 | WHERE c_id = $course_id AND exercice_id = $quiz_id "; |
||
| 2201 | $rs_max_order = Database::query($sql); |
||
| 2202 | $row_max_order = Database::fetch_object($rs_max_order); |
||
| 2203 | $max_order = $row_max_order->max_order + 1; |
||
| 2204 | // Attach questions to quiz |
||
| 2205 | $sql = "INSERT INTO $tbl_quiz_rel_question (c_id, question_id, exercice_id, question_order) |
||
| 2206 | VALUES($course_id, $question_id, $quiz_id, $max_order)"; |
||
| 2207 | Database::query($sql); |
||
| 2208 | } |
||
| 2209 | |||
| 2210 | return $question_id; |
||
| 2211 | } |
||
| 2212 | |||
| 2213 | /** |
||
| 2214 | * @return string |
||
| 2215 | */ |
||
| 2216 | public function getTypePicture() |
||
| 2219 | } |
||
| 2220 | |||
| 2221 | /** |
||
| 2222 | * @return string |
||
| 2223 | */ |
||
| 2224 | public function getExplanation() |
||
| 2225 | { |
||
| 2226 | return $this->explanationLangVar; |
||
| 2227 | } |
||
| 2228 | |||
| 2229 | /** |
||
| 2230 | * Get course medias. |
||
| 2231 | * |
||
| 2232 | * @param int $course_id |
||
| 2233 | * |
||
| 2234 | * @return array |
||
| 2235 | */ |
||
| 2236 | public static function get_course_medias( |
||
| 2237 | $course_id, |
||
| 2238 | $start = 0, |
||
| 2239 | $limit = 100, |
||
| 2240 | $sidx = "question", |
||
| 2241 | $sord = "ASC", |
||
| 2242 | $where_condition = [] |
||
| 2243 | ) { |
||
| 2244 | $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 2245 | $default_where = [ |
||
| 2246 | 'c_id = ? AND parent_id = 0 AND type = ?' => [ |
||
| 2247 | $course_id, |
||
| 2248 | MEDIA_QUESTION, |
||
| 2249 | ], |
||
| 2250 | ]; |
||
| 2251 | $result = Database::select( |
||
| 2252 | '*', |
||
| 2253 | $table_question, |
||
| 2254 | [ |
||
| 2255 | 'limit' => " $start, $limit", |
||
| 2256 | 'where' => $default_where, |
||
| 2257 | 'order' => "$sidx $sord", |
||
| 2258 | ] |
||
| 2259 | ); |
||
| 2260 | |||
| 2261 | return $result; |
||
| 2262 | } |
||
| 2263 | |||
| 2264 | /** |
||
| 2265 | * Get count course medias. |
||
| 2266 | * |
||
| 2267 | * @param int course id |
||
| 2268 | * |
||
| 2269 | * @return int |
||
| 2270 | */ |
||
| 2271 | public static function get_count_course_medias($course_id) |
||
| 2272 | { |
||
| 2273 | $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 2274 | $result = Database::select( |
||
| 2275 | 'count(*) as count', |
||
| 2276 | $table_question, |
||
| 2277 | [ |
||
| 2278 | 'where' => [ |
||
| 2279 | 'c_id = ? AND parent_id = 0 AND type = ?' => [ |
||
| 2280 | $course_id, |
||
| 2281 | MEDIA_QUESTION, |
||
| 2282 | ], |
||
| 2283 | ], |
||
| 2284 | ], |
||
| 2285 | 'first' |
||
| 2286 | ); |
||
| 2287 | |||
| 2288 | if ($result && isset($result['count'])) { |
||
| 2289 | return $result['count']; |
||
| 2290 | } |
||
| 2291 | |||
| 2292 | return 0; |
||
| 2293 | } |
||
| 2294 | |||
| 2295 | /** |
||
| 2296 | * @param int $course_id |
||
| 2297 | * |
||
| 2298 | * @return array |
||
| 2299 | */ |
||
| 2300 | public static function prepare_course_media_select($course_id) |
||
| 2301 | { |
||
| 2302 | $medias = self::get_course_medias($course_id); |
||
| 2303 | $media_list = []; |
||
| 2304 | $media_list[0] = get_lang('NoMedia'); |
||
| 2305 | |||
| 2306 | if (!empty($medias)) { |
||
| 2307 | foreach ($medias as $media) { |
||
| 2308 | $media_list[$media['id']] = empty($media['question']) ? get_lang('Untitled') : $media['question']; |
||
| 2309 | } |
||
| 2310 | } |
||
| 2311 | |||
| 2312 | return $media_list; |
||
| 2313 | } |
||
| 2314 | |||
| 2315 | /** |
||
| 2316 | * @return array |
||
| 2317 | */ |
||
| 2318 | public static function get_default_levels() |
||
| 2319 | { |
||
| 2320 | $levels = [ |
||
| 2321 | 1 => 1, |
||
| 2322 | 2 => 2, |
||
| 2323 | 3 => 3, |
||
| 2324 | 4 => 4, |
||
| 2325 | 5 => 5, |
||
| 2326 | ]; |
||
| 2327 | |||
| 2328 | return $levels; |
||
| 2329 | } |
||
| 2330 | |||
| 2331 | /** |
||
| 2332 | * @return string |
||
| 2333 | */ |
||
| 2334 | public function show_media_content() |
||
| 2335 | { |
||
| 2336 | $html = ''; |
||
| 2337 | if ($this->parent_id != 0) { |
||
| 2338 | $parent_question = self::read($this->parent_id); |
||
| 2339 | $html = $parent_question->show_media_content(); |
||
| 2340 | } else { |
||
| 2341 | $html .= Display::page_subheader($this->selectTitle()); |
||
| 2342 | $html .= $this->selectDescription(); |
||
| 2343 | } |
||
| 2344 | |||
| 2345 | return $html; |
||
| 2346 | } |
||
| 2347 | |||
| 2348 | /** |
||
| 2349 | * Swap between unique and multiple type answers. |
||
| 2350 | * |
||
| 2351 | * @return UniqueAnswer|MultipleAnswer |
||
| 2352 | */ |
||
| 2353 | public function swapSimpleAnswerTypes() |
||
| 2354 | { |
||
| 2355 | $oppositeAnswers = [ |
||
| 2356 | UNIQUE_ANSWER => MULTIPLE_ANSWER, |
||
| 2357 | MULTIPLE_ANSWER => UNIQUE_ANSWER, |
||
| 2358 | ]; |
||
| 2359 | $this->type = $oppositeAnswers[$this->type]; |
||
| 2360 | Database::update( |
||
| 2361 | Database::get_course_table(TABLE_QUIZ_QUESTION), |
||
| 2362 | ['type' => $this->type], |
||
| 2363 | ['c_id = ? AND id = ?' => [$this->course['real_id'], $this->id]] |
||
| 2364 | ); |
||
| 2365 | $answerClasses = [ |
||
| 2366 | UNIQUE_ANSWER => 'UniqueAnswer', |
||
| 2367 | MULTIPLE_ANSWER => 'MultipleAnswer', |
||
| 2368 | ]; |
||
| 2369 | $swappedAnswer = new $answerClasses[$this->type](); |
||
| 2370 | foreach ($this as $key => $value) { |
||
| 2371 | $swappedAnswer->$key = $value; |
||
| 2372 | } |
||
| 2373 | |||
| 2374 | return $swappedAnswer; |
||
| 2375 | } |
||
| 2376 | |||
| 2377 | /** |
||
| 2378 | * @param array $score |
||
| 2379 | * |
||
| 2380 | * @return bool |
||
| 2381 | */ |
||
| 2382 | public function isQuestionWaitingReview($score) |
||
| 2383 | { |
||
| 2384 | $isReview = false; |
||
| 2385 | if (!empty($score)) { |
||
| 2386 | if (!empty($score['comments']) || $score['score'] > 0) { |
||
| 2387 | $isReview = true; |
||
| 2388 | } |
||
| 2389 | } |
||
| 2390 | |||
| 2391 | return $isReview; |
||
| 2392 | } |
||
| 2393 | |||
| 2394 | /** |
||
| 2395 | * @param string $value |
||
| 2396 | */ |
||
| 2397 | public function setFeedback($value) |
||
| 2398 | { |
||
| 2399 | $this->feedback = $value; |
||
| 2400 | } |
||
| 2401 | |||
| 2402 | /** |
||
| 2403 | * @param Exercise $exercise |
||
| 2404 | * |
||
| 2405 | * @return bool |
||
| 2406 | */ |
||
| 2407 | public function showFeedback($exercise) |
||
| 2408 | { |
||
| 2409 | return |
||
| 2410 | in_array($this->type, $this->questionTypeWithFeedback) && |
||
| 2411 | $exercise->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM; |
||
| 2412 | } |
||
| 2413 | |||
| 2414 | /** |
||
| 2415 | * @return string |
||
| 2416 | */ |
||
| 2417 | public function returnFormatFeedback() |
||
| 2418 | { |
||
| 2419 | return '<br />'.Display::return_message($this->feedback, 'normal', false); |
||
| 2420 | } |
||
| 2421 | |||
| 2422 | /** |
||
| 2423 | * Check if this question exists in another exercise. |
||
| 2424 | * |
||
| 2425 | * @throws \Doctrine\ORM\Query\QueryException |
||
| 2426 | * |
||
| 2427 | * @return bool |
||
| 2428 | */ |
||
| 2429 | public function existsInAnotherExercise() |
||
| 2430 | { |
||
| 2431 | $count = $this->getCountExercise(); |
||
| 2432 | |||
| 2433 | return $count > 1; |
||
| 2434 | } |
||
| 2435 | |||
| 2436 | /** |
||
| 2437 | * @throws \Doctrine\ORM\Query\QueryException |
||
| 2438 | * |
||
| 2439 | * @return int |
||
| 2440 | */ |
||
| 2441 | public function getCountExercise() |
||
| 2442 | { |
||
| 2443 | $em = Database::getManager(); |
||
| 2444 | |||
| 2445 | $count = $em |
||
| 2446 | ->createQuery(' |
||
| 2447 | SELECT COUNT(qq.iid) FROM ChamiloCourseBundle:CQuizRelQuestion qq |
||
| 2448 | WHERE qq.questionId = :id |
||
| 2449 | ') |
||
| 2450 | ->setParameters(['id' => (int) $this->id]) |
||
| 2451 | ->getSingleScalarResult(); |
||
| 2452 | |||
| 2453 | return (int) $count; |
||
| 2454 | } |
||
| 2455 | |||
| 2456 | /** |
||
| 2457 | * Check if this question exists in another exercise. |
||
| 2458 | * |
||
| 2459 | * @throws \Doctrine\ORM\Query\QueryException |
||
| 2460 | * |
||
| 2461 | * @return mixed |
||
| 2462 | */ |
||
| 2463 | public function getExerciseListWhereQuestionExists() |
||
| 2478 | } |
||
| 2479 | |||
| 2480 | public function getHotSpotData() |
||
| 2481 | { |
||
| 2482 | } |
||
| 2483 | } |
||
| 2484 |