From c98cb6bc57b301a51f32007b45d3372a863f67d5 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Sun, 20 Feb 2005 17:19:04 +0000 Subject: [PATCH] Now quiz_categories' parent and sortoreder is supported in backup. A mechanish to detect all the ancestors and to recover orphaned categories is now running ar backup time to force backups to be consistent. --- backup/backuplib.php | 32 ++++++++++++++++++++++++++++++++ mod/quiz/backuplib.php | 2 ++ 2 files changed, 34 insertions(+) diff --git a/backup/backuplib.php b/backup/backuplib.php index 54a74769c6e..8aecf5cd902 100644 --- a/backup/backuplib.php +++ b/backup/backuplib.php @@ -10,6 +10,7 @@ //Create missing categories and reasign orphaned questions fix_orphaned_questions($course); + //Detect used categories (by category in questions) $status = execute_sql("INSERT INTO {$CFG->prefix}backup_ids (backup_code, table_name, old_id) SELECT DISTINCT $backup_unique_code,'quiz_categories',t.category @@ -19,6 +20,37 @@ WHERE q.course = '$course' AND g.quiz = q.id AND g.question = t.id",false); + + //Now, foreach detected category, we look for their parents upto 0 (top category) + $categories = get_records_sql("SELECT old_id, old_id + FROM {$CFG->prefix}backup_ids + WHERE backup_code = $backup_unique_code AND + table_name = 'quiz_categories'"); + + if ($categories) { + foreach ($categories as $category) { + if ($dbcat = get_record('quiz_categories','id',$category->old_id)) { + //echo $dbcat->name; //Debug + //Go up to 0 + while ($dbcat->parent != 0) { + //echo '->'; //Debug + $current = $dbcat->id; + if ($dbcat = get_record('quiz_categories','id',$dbcat->parent)) { + //Found parent, add it to backup_ids (by using backup_putid + //we ensure no duplicates!) + $status = backup_putid($backup_unique_code,'quiz_categories',$dbcat->id,0); + //echo $dbcat->name; //Debug + } else { + //Parent not found, fix it (set its parent to 0) + set_field ('quiz_categories','parent',0,'id',$current); + //echo 'assigned to top!'; //Debug + } + } + //echo '
'; //Debug + } + } + } + return $status; } diff --git a/mod/quiz/backuplib.php b/mod/quiz/backuplib.php index 3644a59a277..3354a4517f5 100644 --- a/mod/quiz/backuplib.php +++ b/mod/quiz/backuplib.php @@ -119,6 +119,8 @@ fwrite($bf,full_tag("INFO",4,false,$category->info)); fwrite($bf,full_tag("PUBLISH",4,false,$category->publish)); fwrite($bf,full_tag("STAMP",4,false,$category->stamp)); + fwrite($bf,full_tag("PARENT",4,false,$category->parent)); + fwrite($bf,full_tag("SORTORDER",4,false,$category->sortorder)); //Now, backup their questions $status = quiz_backup_question($bf,$preferences,$category->id); //End category