Fix test category quiz export see BT#10891

pull/2487/head
jmontoyaa 9 years ago
parent df55823254
commit 4bea5f9431
  1. 9
      main/exercise/TestCategory.php
  2. 49
      src/Chamilo/CourseBundle/Component/CourseCopy/CourseRestorer.php

@ -25,15 +25,16 @@ class TestCategory
/**
* return the TestCategory object with id=in_id
* @param int $id
*
* @param int $courseId
* @return TestCategory
*/
public function getCategory($id)
public function getCategory($id, $courseId = 0)
{
$table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$id = intval($id);
$courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId;
$sql = "SELECT * FROM $table
WHERE id = $id AND c_id=".api_get_course_int_id();
WHERE id = $id AND c_id = ".$courseId;
$res = Database::query($sql);
if (Database::num_rows($res)) {
@ -46,7 +47,7 @@ class TestCategory
return $this;
}
return false;
return false;
}
/**

@ -2104,12 +2104,12 @@ class CourseRestorer
{
$course_id = api_get_course_int_id();
// Let's restore the categories
$tab_test_category_id_old_new = array(); // used to build the quiz_question_rel_category table
$categoryOldVsNewList = array(); // used to build the quiz_question_rel_category table
if ($this->course->has_resources(RESOURCE_TEST_CATEGORY)) {
$resources = $this->course->resources;
$destinationCourseId = $this->destination_course_info['real_id'];
foreach ($resources[RESOURCE_TEST_CATEGORY] as $id => $courseCopyTestCategory) {
$tab_test_category_id_old_new[$courseCopyTestCategory->source_id] = $id;
$categoryOldVsNewList[$courseCopyTestCategory->source_id] = $id;
// check if this test_category already exist in the destination BDD
// do not Database::escape_string $title and $description, it will be done later
$title = $courseCopyTestCategory->title;
@ -2128,15 +2128,19 @@ class CourseRestorer
$test_category->name = $new_title;
$test_category->description = $description;
$new_id = $test_category->save($destinationCourseId);
$tab_test_category_id_old_new[$courseCopyTestCategory->source_id] = $new_id;
$categoryOldVsNewList[$courseCopyTestCategory->source_id] = $new_id;
break;
case FILE_OVERWRITE:
$id = TestCategory::get_category_id_for_title($title);
$my_cat = new TestCategory();
$my_cat = $my_cat->getCategory($id);
$my_cat->name = $title;
$my_cat->modifyCategory($destinationCourseId);
$tab_test_category_id_old_new[$courseCopyTestCategory->source_id] = $id;
// get category from source
$destinationCategoryId = TestCategory::get_category_id_for_title($title, $destinationCourseId);
if ($destinationCategoryId) {
$my_cat = new TestCategory();
$my_cat = $my_cat->getCategory($destinationCategoryId, $destinationCourseId);
$my_cat->name = $title;
$my_cat->description = $description;
$my_cat->modifyCategory($destinationCourseId);
$categoryOldVsNewList[$courseCopyTestCategory->source_id] = $destinationCategoryId;
}
break;
}
} else {
@ -2145,29 +2149,30 @@ class CourseRestorer
$test_category->name = $title;
$test_category->description = $description;
$new_id = $test_category->save($destinationCourseId);
$tab_test_category_id_old_new[$courseCopyTestCategory->source_id] = $new_id;
$categoryOldVsNewList[$courseCopyTestCategory->source_id] = $new_id;
}
$this->course->resources[RESOURCE_TEST_CATEGORY][$id]->destination_id = $tab_test_category_id_old_new[$courseCopyTestCategory->source_id];
$this->course->resources[RESOURCE_TEST_CATEGORY][$id]->destination_id = $categoryOldVsNewList[$courseCopyTestCategory->source_id];
}
}
// lets check if quizzes-question are restored too, to redo the link between test_category and quizzes question for questions restored
// lets check if quizzes-question are restored too,
// to redo the link between test_category and quizzes question for questions restored
// we can use the source_id field
// question source_id => category source_id
if ($this->course->has_resources(RESOURCE_QUIZQUESTION)) {
// check the category number of each question restored
if (!empty($resources[RESOURCE_QUIZQUESTION])) {
foreach ($resources[RESOURCE_QUIZQUESTION] as $id => $courseCopyQuestion) {
$new_quiz_question_id = $resources[RESOURCE_QUIZQUESTION][$id]->destination_id;
$question_category = $courseCopyQuestion->question_category;
if ($question_category > 0 &&
isset($tab_test_category_id_old_new[$question_category])
) {
TestCategory::add_category_for_question_id(
$tab_test_category_id_old_new[$question_category],
$new_quiz_question_id,
$course_id
);
$newQuestionId = $resources[RESOURCE_QUIZQUESTION][$id]->destination_id;
$questionCategoryId = $courseCopyQuestion->question_category;
if ($newQuestionId > 0) {
if ($questionCategoryId > 0 && isset($categoryOldVsNewList[$questionCategoryId])) {
TestCategory::add_category_for_question_id(
$categoryOldVsNewList[$questionCategoryId],
$newQuestionId,
$destinationCourseId
);
}
}
}
}

Loading…
Cancel
Save