/**
  * Build the Quiz-Questions
  */
 public function build_quiz_questions($course_code = null)
 {
     $course_info = api_get_course_info($course_code);
     $course_id = $course_info['real_id'];
     $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} WHERE c_id = {$course_id} ";
     $db_result = Database::query($sql);
     while ($obj = Database::fetch_object($db_result)) {
         $categories = Testcategory::getCategoryForQuestionWithCategoryData($obj->iid, $course_id, true);
         $parent_info = array();
         if (isset($obj->parent_id) && !empty($obj->parent_id)) {
             $parent_info = (array) Question::read($obj->parent_id, $course_id);
         }
         $question = new QuizQuestion($obj->iid, $obj->question, $obj->description, $obj->ponderation, $obj->type, $obj->position, $obj->picture, $obj->level, $obj->extra, $parent_info, $categories);
         $sql = 'SELECT * FROM ' . $table_ans . ' WHERE question_id = ' . $obj->iid;
         $db_result2 = Database::query($sql);
         while ($obj2 = Database::fetch_object($db_result2)) {
             $question->add_answer($obj2->iid, $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 = ' . $course_id . ' AND question_id = ' . $obj->iid;
                 $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.
     $build_orphan_questions = !empty($_POST['recycle_option']);
     // When a course is emptied this option should be activated (true).
     //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 q.* FROM {$table_que} q INNER JOIN {$table_rel} r\n                    ON (q.c_id = r.c_id AND q.iid = r.question_id)\n                    INNER JOIN {$table_qui} ex\n                    ON (ex.iid = r.exercice_id AND ex.c_id = r.c_id )\n                    WHERE ex.c_id = {$course_id} AND ex.active = '-1'\n                 )\n                 UNION\n                 (\n                    SELECT q.* FROM {$table_que} q left\n                    OUTER JOIN {$table_rel} r\n                    ON (q.c_id = r.c_id AND q.iid = r.question_id)\n                    WHERE q.c_id = {$course_id} AND r.question_id is null\n                 )\n                 UNION\n                 (\n                    SELECT q.* FROM {$table_que} q\n                    INNER JOIN {$table_rel} r\n                    ON (q.c_id = r.c_id AND q.iid = r.question_id)\n                    WHERE r.c_id = {$course_id} AND (r.exercice_id = '-1' OR r.exercice_id = '0')\n                 )\n        ";
     $db_result = Database::query($sql);
     if (Database::num_rows($db_result) > 0) {
         $build_orphan_questions = true;
         $orphanQuestionIds = array();
         while ($obj = Database::fetch_object($db_result)) {
             $categories = Testcategory::getCategoryForQuestionWithCategoryData($obj->iid, $course_id, true);
             $parent_info = array();
             if (isset($obj->parent_id) && !empty($obj->parent_id)) {
                 $parent_info = (array) Question::read($obj->parent_id, $course_id);
             }
             //Avoid adding the same question twice
             if (!isset($this->course->resources[$obj->iid])) {
                 $question = new QuizQuestion($obj->iid, $obj->question, $obj->description, $obj->ponderation, $obj->type, $obj->position, $obj->picture, $obj->level, $obj->extra, $parent_info, $categories);
                 $sql = "SELECT * FROM {$table_ans} WHERE c_id = {$course_id} AND question_id = " . $obj->iid;
                 $db_result2 = Database::query($sql);
                 if (Database::num_rows($db_result2)) {
                     while ($obj2 = Database::fetch_object($db_result2)) {
                         $question->add_answer($obj2->iid, $obj2->answer, $obj2->correct, $obj2->comment, $obj2->ponderation, $obj2->position, $obj2->hotspot_coordinates, $obj2->hotspot_type);
                     }
                     $orphanQuestionIds[] = $obj->iid;
                 }
                 $this->course->add_resource($question);
             }
         }
     }
     if ($build_orphan_questions) {
         $obj = array('iid' => -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);
     }
 }