id) (CL,pk->id) // | | // ----------------------------------------------- | // | | | | // | | | | // | | | | // quiz_attempts quiz_grades quiz_question_grades | // (UL,pk->id, fk->quiz) (UL,pk->id,fk->quiz) (CL,pk->id,fk->quiz) | // | | | // | | | // | | | // quiz_responses | quiz_questions // (UL,pk->id, fk->attempt)----------------------------------------------------(CL,pk->id,fk->category,files) // | // | // | // -------------------------------------------------------------------------------------- // | | | | | | // | | | | | | // | | | | | | quiz_randomsamatch // quiz_truefalse | quiz_multichoice | quiz_multianswer |--(CL,pl->id,fk->question) // (CL,pl->id,fk->question) | (CL,pl->id,fk->question) | (CL,pl->id,fk->question) | // . | . | . | // . quiz_shortanswer . quiz_numerical . | // . (CL,pl->id,fk->question) . (CL,pl->id,fk->question) . | quiz_match // . . . . . |--(CL,pl->id,fk->question) // . . . . . | . // . . . . . | . // . . . . . | . // . . . . . | quiz_match_sub // . . . . . |--(CL,pl->id,fk->question) // ............................................................. | // . | // . | // . | // quiz_answers | // (CL,pk->id,fk->question)---------------------------------- // // Meaning: pk->primary key field of the table // fk->foreign key to link with parent // nt->nested field (recursive data) // CL->course level info // UL->user level info // files->table may have files // //----------------------------------------------------------- //This module is special, because we make the restore in two steps: // 1.-We restore every category and their questions (complete structure). It includes this tables: // - quiz_categories // - quiz_questions // - quiz_truefalse // - quiz_shortanswer // - quiz_multianswer // - quiz_multichoice // - quiz_numerical // - quiz_randomsamatch // - quiz_match // - quiz_match_sub // - quiz_answers // All this backup info have its own section in moodle.xml (QUESTION_CATEGORIES) and it's generated // before every module backup standard invocation. And only if to restore quizzes has been selected !! // It's invoked with quiz_restore_question_categories. (course independent). // 2.-Standard module restore (Invoked via quiz_restore_mods). It includes this tables: // - quiz // - quiz_question_grades // - quiz_attempts // - quiz_grades // - quiz_responses // This step is the standard mod backup. (course dependent). //STEP 1. Restore categories/questions and associated structures // (course independent) function quiz_restore_question_categories($category,$restore) { global $CFG; $status = true; //Get record from backup_ids $data = backup_getid($restore->backup_unique_code,"quiz_categories",$category->id); if ($data) { //Now get completed xmlized object $info = $data->info; //traverse_xmlize($info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug //Now, build the QUIZ_CATEGORIES record structure $quiz_cat->course = $restore->course_id; $quiz_cat->name = backup_todb($info['QUESTION_CATEGORY']['#']['NAME']['0']['#']); $quiz_cat->info = backup_todb($info['QUESTION_CATEGORY']['#']['INFO']['0']['#']); $quiz_cat->publish = backup_todb($info['QUESTION_CATEGORY']['#']['PUBLISH']['0']['#']); $quiz_cat->stamp = backup_todb($info['QUESTION_CATEGORY']['#']['STAMP']['0']['#']); //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. //If backup contains category_stamps then everythig is done by stamp (current approach from 1.1 final) //else, everything is done by name (old approach). This mantains backward compatibility. if ($quiz_cat->stamp) { //STAMP exists, do things using it (1.1) //Check for categories and their properties, storing in temporary variables //Count categories with the same stamp $count_cat = count_records("quiz_categories","stamp",$quiz_cat->stamp); //Count categories with the same stamp in the same course $count_cat_same_course = count_records("quiz_categories","course",$restore->course_id,"stamp",$quiz_cat->stamp); //Count categories with the same stamp in other course $count_cat_other_course = $count_cat - $count_cat_same_course; //Get categories with the same stamp in the same course if ($count_cat_same_course > 0) { //Eloy's NOTE: Due to this select *must* be retrive only one record, we could have used get_record(), but // mantain this to be as simmilar as possible with old code (comparing by name) to be // able to modify both in the same manner. $cats_same_course = get_records_sql("SELECT c.* FROM {$CFG->prefix}quiz_categories c WHERE c.course = '$restore->course_id' AND c.stamp = '$quiz_cat->stamp' ORDER BY c.id DESC"); } else { $cats_same_course = false; } //Get category with the same stamp 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.stamp = '$quiz_cat->stamp' 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 { //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 the 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; } } } else { //STAMP doesn't exists, do things by name (pre 1.1) //and calculate and insert STAMP too !! //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. //First, calculate the STAMP field $quiz_cat->stamp = make_unique_id_code(); //The structure is equal to the db, so insert the quiz_categories $newid = insert_record ("quiz_categories",$quiz_cat); } else { //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 the publish field if ($other_course_cat->publish == 0) { //The category has its publish to No (0). Create a new local one. //First, calculate the STAMP field $quiz_cat->stamp = make_unique_id_code(); //The structure is equal to the db, so insert the quiz_categories $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; } } } //Do some output if ($status) { echo "