/** * Build the Quiz-Questions * @param int $courseId Internal course ID */ public function build_quiz_questions($courseId = 0) { $table_qui = Database::get_course_table(TABLE_QUIZ_TEST); $table_rel = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); $table_que = Database::get_course_table(TABLE_QUIZ_QUESTION); $table_ans = Database::get_course_table(TABLE_QUIZ_ANSWER); // Building normal tests. $sql = "SELECT * FROM {$table_que}\n WHERE c_id = {$courseId} "; $result = Database::query($sql); while ($obj = Database::fetch_object($result)) { // find the question category // @todo : need to be adapted for multi category questions in 1.10 $question_category_id = TestCategory::getCategoryForQuestion($obj->id, $courseId); // build the backup resource question object $question = new QuizQuestion($obj->id, $obj->question, $obj->description, $obj->ponderation, $obj->type, $obj->position, $obj->picture, $obj->level, $obj->extra, $question_category_id); $sql = 'SELECT * FROM ' . $table_ans . ' WHERE c_id = ' . $courseId . ' AND question_id = ' . $obj->id; $db_result2 = Database::query($sql); while ($obj2 = Database::fetch_object($db_result2)) { $question->add_answer($obj2->id, $obj2->answer, $obj2->correct, $obj2->comment, $obj2->ponderation, $obj2->position, $obj2->hotspot_coordinates, $obj2->hotspot_type); if ($obj->type == MULTIPLE_ANSWER_TRUE_FALSE) { $table_options = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION); $sql = 'SELECT * FROM ' . $table_options . ' WHERE c_id = ' . $courseId . ' AND question_id = ' . $obj->id; $db_result3 = Database::query($sql); while ($obj3 = Database::fetch_object($db_result3)) { $question_option = new QuizQuestionOption($obj3); $question->add_option($question_option); } } } $this->course->add_resource($question); } // Building a fictional test for collecting orphan questions. // When a course is emptied this option should be activated (true). $build_orphan_questions = !empty($_POST['recycle_option']); // 1st union gets the orphan questions from deleted exercises // 2nd union gets the orphan questions from question that were deleted in a exercise. $sql = " (\n SELECT question_id, q.* FROM {$table_que} q INNER JOIN {$table_rel} r\n ON (q.c_id = r.c_id AND q.id = r.question_id)\n INNER JOIN {$table_qui} ex\n ON (ex.id = r.exercice_id AND ex.c_id = r.c_id )\n WHERE ex.c_id = {$courseId} AND ex.active = '-1'\n )\n UNION\n (\n SELECT question_id, q.* FROM {$table_que} q left\n OUTER JOIN {$table_rel} r\n ON (q.c_id = r.c_id AND q.id = r.question_id)\n WHERE q.c_id = {$courseId} AND r.question_id is null\n )\n UNION\n (\n SELECT question_id, q.* FROM {$table_que} q\n INNER JOIN {$table_rel} r\n ON (q.c_id = r.c_id AND q.id = r.question_id)\n WHERE r.c_id = {$courseId} AND (r.exercice_id = '-1' OR r.exercice_id = '0')\n )\n "; $result = Database::query($sql); if (Database::num_rows($result) > 0) { $build_orphan_questions = true; $orphanQuestionIds = array(); while ($obj = Database::fetch_object($result)) { // Orphan questions if (!empty($obj->question_id)) { $obj->id = $obj->question_id; } // Avoid adding the same question twice if (!isset($this->course->resources[$obj->id])) { // find the question category // @todo : need to be adapted for multi category questions in 1.10 $question_category_id = TestCategory::getCategoryForQuestion($obj->id, $courseId); $question = new QuizQuestion($obj->id, $obj->question, $obj->description, $obj->ponderation, $obj->type, $obj->position, $obj->picture, $obj->level, $obj->extra, $question_category_id); $sql = "SELECT * FROM {$table_ans}\n WHERE c_id = {$courseId} AND question_id = " . $obj->id; $db_result2 = Database::query($sql); if (Database::num_rows($db_result2)) { while ($obj2 = Database::fetch_object($db_result2)) { $question->add_answer($obj2->id, $obj2->answer, $obj2->correct, $obj2->comment, $obj2->ponderation, $obj2->position, $obj2->hotspot_coordinates, $obj2->hotspot_type); } $orphanQuestionIds[] = $obj->id; } $this->course->add_resource($question); } } } if ($build_orphan_questions) { $obj = array('id' => -1, 'title' => get_lang('OrphanQuestions', ''), 'type' => 2); $newQuiz = new Quiz((object) $obj); if (!empty($orphanQuestionIds)) { foreach ($orphanQuestionIds as $index => $orphanId) { $order = $index + 1; $newQuiz->add_question($orphanId, $order); } } $this->course->add_resource($newQuiz); } }