diff --git a/main/coursecopy/classes/CourseRestorer.class.php b/main/coursecopy/classes/CourseRestorer.class.php index 6fd8432c12..eaf439c2df 100644 --- a/main/coursecopy/classes/CourseRestorer.class.php +++ b/main/coursecopy/classes/CourseRestorer.class.php @@ -1140,7 +1140,7 @@ class CourseRestorer 'text_when_finished' => $quiz->text_when_finished, 'expired_time' => (int)$quiz->expired_time, ); - + if ($respect_base_content) { $my_session_id = $quiz->session_id; if (!empty($quiz->session_id)) { @@ -1159,17 +1159,24 @@ class CourseRestorer $new_id = -1; } - $this->course->resources[RESOURCE_QUIZ][$id]->obj->destination_id = $new_id; + if ($new_id) { + // updates the question position + $exercise = new Exercise($this->destination_course_id); + $exercise->read($new_id); + $exercise->add_exercise_to_order_table(); + + $this->course->resources[RESOURCE_QUIZ][$id]->obj->destination_id = $new_id; - $order = 0; - if (!empty($quiz->question_ids)) { - foreach ($quiz->question_ids as $index => $question_id) { - $qid = $this->restore_quiz_question($question_id); - $question_order = $quiz->question_orders[$index] ? $quiz->question_orders[$index] : ++$order; - $sql = "INSERT IGNORE INTO ".$table_rel." SET c_id = ".$this->destination_course_id.", question_id = ".$qid.", exercice_id = ".$new_id.", question_order = ".$question_order; - Database::query($sql); + $order = 0; + if (!empty($quiz->question_ids)) { + foreach ($quiz->question_ids as $index => $question_id) { + $qid = $this->restore_quiz_question($question_id); + $question_order = $quiz->question_orders[$index] ? $quiz->question_orders[$index] : ++$order; + $sql = "INSERT IGNORE INTO ".$table_rel." SET c_id = ".$this->destination_course_id.", question_id = ".$qid.", exercice_id = ".$new_id.", question_order = ".$question_order; + Database::query($sql); + } } - } + } } } } diff --git a/main/exercice/exercise.class.php b/main/exercice/exercise.class.php index 947fbde3c6..43f8a5f36a 100644 --- a/main/exercice/exercise.class.php +++ b/main/exercice/exercise.class.php @@ -780,6 +780,8 @@ class Exercise { Database::query($sql); $this->id = Database::insert_id(); + $this->add_exercise_to_order_table(); + // insert into the item_property table api_item_property_update($this->course, TOOL_QUIZ, $this->id, 'QuizAdded', api_get_user_id()); api_set_default_visibility($this->id, TOOL_QUIZ); @@ -870,6 +872,7 @@ class Exercise { $sql="UPDATE $TBL_EXERCICES SET active='-1' WHERE c_id = ".$this->course_id." AND id='".Database::escape_string($this->id)."'"; Database::query($sql); api_item_property_update($this->course, TOOL_QUIZ, $this->id,'QuizDeleted',api_get_user_id()); + $this->delete_exercise_order(); if (api_get_setting('search_enabled')=='true' && extension_loaded('xapian') ) { $this->search_engine_delete(); @@ -1474,6 +1477,98 @@ class Exercise { return $i; } + + function get_last_exercise_order() { + $table = Database::get_course_table(TABLE_QUIZ_ORDER); + $sql = "SELECT exercise_order FROM $table ORDER BY exercise_order DESC LIMIT 1"; + $result = Database::query($sql); + if (Database::num_rows($result)) { + $row = Database::fetch_array($result); + return $row['exercise_order']; + } + return 0; + } + + function get_exercise_order() { + $table = Database::get_course_table(TABLE_QUIZ_ORDER); + $sql = "SELECT exercise_order FROM $table WHERE exercise_id = {$this->id}"; + $result = Database::query($sql); + if (Database::num_rows($result)) { + $row = Database::fetch_array($result); + return $row['exercise_order']; + } + return false; + } + + function add_exercise_to_order_table() { + $table = Database::get_course_table(TABLE_QUIZ_ORDER); + $last_order = $this->get_last_exercise_order(); + $course_id = $this->course_id; + + if ($last_order == 0) { + Database::insert($table, array( + 'exercise_id' => $this->id, + 'exercise_order' => 1, + 'c_id' => $course_id, + 'session_id' => api_get_session_id(), + )); + } else { + $current_exercise_order = $this->get_exercise_order(); + if ($current_exercise_order == false) { + Database::insert($table, array( + 'exercise_id' => $this->id, + 'exercise_order' => $last_order + 1, + 'c_id' => $course_id, + 'session_id' => api_get_session_id(), + )); + } + } + } + + function update_exercise_list_order($new_exercise_list, $course_id, $session_id) { + $table = Database::get_course_table(TABLE_QUIZ_ORDER); + $counter = 1; + //Drop all + $session_id = intval($session_id); + $course_id = intval($course_id); + + Database::query("DELETE FROM $table WHERE session_id = $session_id AND c_id = $course_id"); + //Insert all + foreach ($new_exercise_list as $new_order_id) { + Database::insert($table, array('exercise_order' => $counter, 'session_id' => $session_id, 'exercise_id' => intval($new_order_id), 'c_id' => $course_id)); + $counter++; + } + } + + function delete_exercise_order() { + $table = Database::get_course_table(TABLE_QUIZ_ORDER); + $session_id = api_get_session_id(); + $course_id = $this->course_id; + Database::query("DELETE FROM $table WHERE exercise_id = {$this->id} AND session_id = $session_id AND c_id = $course_id"); + } + + function save_exercise_list_order($course_id, $session_id) { + $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST); + $ordered_list = $this->get_exercise_list_ordered(); + $ordered_count = count($ordered_list); + + $session_id = intval($session_id); + $course_id = intval($course_id); + + //Check if order exists and matchs the current status + $sql = "SELECT iid FROM $TBL_EXERCICES WHERE c_id = $course_id AND active = '1' AND session_id = $session_id ORDER BY title"; + $result = Database::query($sql); + $unordered_count = Database::num_rows($result); + + if ($unordered_count != $ordered_count) { + $exercise_list = array(); + while($row = Database::fetch_array($result)) { + $exercise_list[] = $row['iid']; + } + $this->update_exercise_list_order($exercise_list, $course_id, $session_id); + } + } + /** * Copies an exercise (duplicate all questions and answers) */ @@ -1489,6 +1584,8 @@ class Exercise { $exercise_obj->updateId(0); $exercise_obj->save(); + $exercise_obj->save_exercise_list_order($this->course['real_id'], api_get_session_id()); + $new_exercise_id = $exercise_obj->selectId(); $question_list = $exercise_obj->selectQuestionList();