From 461da6bdb6c46ad9313b38660ba3a76abd86f684 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Wed, 27 Aug 2003 00:40:14 +0000 Subject: [PATCH] Now existing categories are used only if their publish field is set to yes. Else, a new "local" category is created. Added some lines of code to mantain old compatibility when detecting existing questions (do it by name if the stamp is not found) and insert the stamp too !! Please, test. --- backup/version.php | 4 +- mod/quiz/restorelib.php | 97 ++++++++++++++++++++++++++++++++++------- 2 files changed, 84 insertions(+), 17 deletions(-) diff --git a/backup/version.php b/backup/version.php index c895238084..b50d1d00c5 100644 --- a/backup/version.php +++ b/backup/version.php @@ -5,6 +5,6 @@ // database (backup_version) to determine whether upgrades should // be performed (see db/backup_*.php) -$backup_version = 2003082600; // The current version is a date (YYYYMMDDXX) +$backup_version = 2003082700; // The current version is a date (YYYYMMDDXX) -$backup_release = "1.1 Beta 1"; // User-friendly version number +$backup_release = "1.1 Beta++"; // User-friendly version number diff --git a/mod/quiz/restorelib.php b/mod/quiz/restorelib.php index f2f9b75a54..22852b18ad 100644 --- a/mod/quiz/restorelib.php +++ b/mod/quiz/restorelib.php @@ -103,20 +103,77 @@ $quiz_cat->info = backup_todb($info['QUESTION_CATEGORY']['#']['INFO']['0']['#']); $quiz_cat->publish = backup_todb($info['QUESTION_CATEGORY']['#']['PUBLISH']['0']['#']); - //Now search it that category exists !! - $cat = get_record("quiz_categories","name",$quiz_cat->name); - //Create it if doesn't exists - if (!$cat) { + //Now, we are going to do some calculations to decide when to create the category or no. + //Based in the next logic: + // + If the category doesn't exists, create it in $restore->course_id course and remap questions here. + // + If the category exists: + // - If it belongs to $restore->course_id course simply remap questions here. + // - If it doesn't belongs to $restore->course_id course: + // - If its publish field is set to No (0), create a new category in $restore->course_id course and remap questions here. + // - If its publish field is set to Yes (1), simply remap questions here. + // + //This was decided 2003/08/26, Eloy and Martin + + //Eloy's NOTE: I could be done code below more compact, but I've preffered do this to allow + //easy future modifications. + + //Check for categories and their properties, storing in temporary variables + //Count categories with the same name + + $count_cat = count_records("quiz_categories","name",$quiz_cat->name); + //Count categories with the same name in the same course + $count_cat_same_course = count_records("quiz_categories","course",$restore->course_id,"name",$quiz_cat->name); + //Count categories with the same name in other course + $count_cat_other_course = $count_cat - $count_cat_same_course; + + //Get categories with the same name in the same course + //The last record will be the oldest category + if ($count_cat_same_course > 0) { + $cats_same_course = get_records_sql("SELECT c.* FROM {$CFG->prefix}quiz_categories c + WHERE c.course = '$restore->course_id' AND + c.name = '$quiz_cat->name' + ORDER BY c.id DESC"); + } else { + $cats_same_course = false; + } + //Get categories with the same name in other course + //The last record will be the oldest category with publish=1 + if ($count_cat_other_course > 0) { + $cats_other_course = get_records_sql("SELECT c.* FROM {$CFG->prefix}quiz_categories c + WHERE c.course != '$restore->course_id' AND + c.name = '$quiz_cat->name' + ORDER BY c.publish ASC, c.id DESC"); + } else { + $cats_other_course = false; + } + + if ($count_cat == 0) { + //The category doesn't exist, create it. //The structure is equal to the db, so insert the quiz_categories $newid = insert_record ("quiz_categories",$quiz_cat); } else { - //Exists, so get its id - $newid = $cat->id; - //If the existing category course and $restore->course_id are different, - //we must publish the existing category - if ($cat->course != $restore->course_id and $cat->publish == 0) { - $cat->publish = 1; - update_record("quiz_categories",$cat); + //The category exist, check if it belongs to the same course + if ($count_cat_same_course > 0) { + //The category belongs to the same course, get the last record (oldest) + foreach ($cats_same_course as $cat) { + $newid = $cat->id; + } + } else if ($count_cat_other_course > 0) { + //The category belongs to other course, get the last record (oldest) + foreach ($cats_other_course as $cat) { + $other_course_cat = $cat; + } + //Now check th publish field + if ($other_course_cat->publish == 0) { + //The category has its publish to No (0). Create a new local one. + $newid = insert_record ("quiz_categories",$quiz_cat); + } else { + //The category has its publish to Yes(1). Use it. + $newid = $other_course_cat->id; + } + } else { + //We must never arrive here !! + $status = false; } } @@ -124,7 +181,7 @@ echo "