/mod/quiz/restorelibpre15.php
PHP | 2014 lines | 1237 code | 292 blank | 485 comment | 368 complexity | d9055e1a34d03a90ed67ce962e59eaf6 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- <?php // $Id: restorelibpre15.php,v 1.26.8.1 2007/11/02 16:19:57 tjhunt Exp $
- //This php script contains all the stuff to backup/restore
- //quiz mods
-
- //To see, put your terminal to 160cc
-
- //This is the "graphical" structure of the quiz mod:
- //
- // quiz question_categories
- // (CL,pk->id) (CL,pk->id)
- // | |
- // ------------------------------------------------------------------- |
- // | | | | |.......................................
- // | | | | | .
- // | | | | | .
- // quiz_attempts quiz_grades quiz_question_grades quiz_question_versions | ----question_datasets---- .
- // (UL,pk->id, fk->quiz) (UL,pk->id,fk->quiz) (CL,pk->id,fk->quiz) (CL,pk->id,fk->quiz) | | (CL,pk->id,fk->question, | .
- // | | . | | fk->dataset_definition) | .
- // | | . | | | .
- // | | . | | | .
- // | | . | | | .
- // quiz_responses | question question_dataset_definitions
- // (UL,pk->id, fk->attempt)----------------------------------------------------(CL,pk->id,fk->category,files) (CL,pk->id,fk->category)
- // | |
- // | |
- // | |
- // | question_dataset_items
- // | (CL,pk->id,fk->definition)
- // |
- // |
- // |
- // --------------------------------------------------------------------------------------------------------------
- // | | | | | | |
- // | | | | | | |
- // | | | | question_calculated | | question_randomsamatch
- // question_truefalse | question_multichoice | (CL,pl->id,fk->question) | |--(CL,pl->id,fk->question)
- // (CL,pl->id,fk->question) | (CL,pl->id,fk->question) | . | |
- // . | . | . | |
- // . question_shortanswer . question_numerical . question_multianswer. |
- // . (CL,pl->id,fk->question) . (CL,pl->id,fk->question) . (CL,pl->id,fk->question) | question_match
- // . . . . . . |--(CL,pl->id,fk->question)
- // . . . . . . | .
- // . . . . . . | .
- // . . . . . . | .
- // . . . . . . | question_match_sub
- // . . . . . . |--(CL,pl->id,fk->question)
- // ........................................................................................ |
- // . |
- // . |
- // . | question_numerical_units
- // question_answers |--(CL,pl->id,fk->question)
- // (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:
- // - question_categories
- // - question
- // - question_truefalse
- // - question_shortanswer
- // - question_multianswer
- // - question_multichoice
- // - question_numerical
- // - question_randomsamatch
- // - question_match
- // - question_match_sub
- // - question_calculated
- // - question_answers
- // - question_numerical_units
- // - question_datasets
- // - question_dataset_definitions
- // - question_dataset_items
- // 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_versions
- // - quiz_question_grades
- // - quiz_attempts
- // - quiz_grades
- // - quiz_responses
- // This step is the standard mod backup. (course dependent).
-
- //We are going to nedd quiz libs to be able to mimic the upgrade process
- require_once("$CFG->dirroot/mod/quiz/locallib.php");
-
- //STEP 1. Restore categories/questions and associated structures
- // (course independent)
- function quiz_restore_pre15_question_categories($category,$restore) {
-
- global $CFG;
-
- $status = true;
-
- //Get record from backup_ids
- $data = backup_getid($restore->backup_unique_code,"question_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 question_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']['#']);
- $quiz_cat->parent = backup_todb($info['QUESTION_CATEGORY']['#']['PARENT']['0']['#']);
- $quiz_cat->sortorder = backup_todb($info['QUESTION_CATEGORY']['#']['SORTORDER']['0']['#']);
-
- if ($catfound = restore_get_best_question_category($quiz_cat, $restore->course_id)) {
- $newid = $catfound;
- } else {
- if (!$quiz_cat->stamp) {
- $quiz_cat->stamp = make_unique_id_code();
- }
- $newid = insert_record ("question_categories",$quiz_cat);
- }
-
- //Do some output
- if ($newid) {
- if (!defined('RESTORE_SILENTLY')) {
- echo "<li>".get_string('category', 'quiz')." \"".$quiz_cat->name."\"<br />";
- }
- } else {
- if (!defined('RESTORE_SILENTLY')) {
- //We must never arrive here !!
- echo "<li>".get_string('category', 'quiz')." \"".$quiz_cat->name."\" Error!<br />";
- }
- $status = false;
- }
- backup_flush(300);
-
- //Here category has been created or selected, so save results in backup_ids and start with questions
- if ($newid and $status) {
- //We have the newid, update backup_ids
- backup_putid($restore->backup_unique_code,"question_categories",
- $category->id, $newid);
- //Now restore question
- $status = quiz_restore_pre15_questions ($category->id, $newid,$info,$restore);
- } else {
- $status = false;
- }
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- }
-
- return $status;
- }
-
- function quiz_restore_pre15_questions ($old_category_id,$new_category_id,$info,$restore) {
-
- global $CFG;
-
- $status = true;
-
- //Get the questions array
- $questions = $info['QUESTION_CATEGORY']['#']['QUESTIONS']['0']['#']['QUESTION'];
-
- //Iterate over questions
- for($i = 0; $i < sizeof($questions); $i++) {
- $question = new object;
- $que_info = $questions[$i];
- //traverse_xmlize($que_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //We'll need this later!!
- $oldid = backup_todb($que_info['#']['ID']['0']['#']);
-
- //Now, build the question record structure
- $question->category = $new_category_id;
- $question->parent = backup_todb($que_info['#']['PARENT']['0']['#']);
- $question->name = backup_todb($que_info['#']['NAME']['0']['#']);
- $question->questiontext = backup_todb($que_info['#']['QUESTIONTEXT']['0']['#']);
- $question->questiontextformat = backup_todb($que_info['#']['QUESTIONTEXTFORMAT']['0']['#']);
- $question->image = backup_todb($que_info['#']['IMAGE']['0']['#']);
- $question->defaultgrade = backup_todb($que_info['#']['DEFAULTGRADE']['0']['#']);
- if (isset($que_info['#']['PENALTY']['0']['#'])) { //Only if it's set, to apply DB default else.
- $question->penalty = backup_todb($que_info['#']['PENALTY']['0']['#']);
- }
- $question->qtype = backup_todb($que_info['#']['QTYPE']['0']['#']);
- if (isset($que_info['#']['LENGTH']['0']['#'])) { //Only if it's set, to apply DB default else.
- $question->length = backup_todb($que_info['#']['LENGTH']['0']['#']);
- }
- $question->stamp = backup_todb($que_info['#']['STAMP']['0']['#']);
- if (isset($que_info['#']['VERSION']['0']['#'])) { //Only if it's set, to apply DB default else.
- $question->version = backup_todb($que_info['#']['VERSION']['0']['#']);
- }
- if (isset($que_info['#']['HIDDEN']['0']['#'])) { //Only if it's set, to apply DB default else.
- $question->hidden = backup_todb($que_info['#']['HIDDEN']['0']['#']);
- }
-
- //Although only a few backups can have questions with parent, we try to recode it
- //if it contains something
- if ($question->parent and $parent = backup_getid($restore->backup_unique_code,"question",$question->parent)) {
- $question->parent = $parent->new_id;
- }
-
- // If it is a random question then hide it
- if ($question->qtype == RANDOM) {
- $question->hidden = 1;
- }
-
- //If it is a description question, length = 0
- if ($question->qtype == DESCRIPTION) {
- $question->length = 0;
- }
-
- //Check if the question exists
- //by category and stamp
- $question_exists = get_record ("question","category",$question->category,
- "stamp",$question->stamp);
- //If the stamp doesn't exists, check if question exists
- //by category, name and questiontext and calculate stamp
- //Mantains pre Beta 1.1 compatibility !!
- if (!$question->stamp) {
- $question->stamp = make_unique_id_code();
- $question->version = 1;
- $question_exists = get_record ("question","category",$question->category,
- "name",$question->name,
- "questiontext",$question->questiontext);
- }
-
- //If the question exists, only record its id
- if ($question_exists) {
- $newid = $question_exists->id;
- $creatingnewquestion = false;
- //Else, create a new question
- } else {
- //The structure is equal to the db, so insert the question
- $newid = insert_record ("question",$question);
- //If it is a random question, parent = id
- if ($newid && $question->qtype == RANDOM) {
- set_field ('question', 'parent', $newid, 'id', $newid);
- }
- $creatingnewquestion = true;
- }
-
- //Do some output
- if (($i+1) % 2 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 40 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
- //Save newid to backup tables
- if ($newid) {
- //We have the newid, update backup_ids
- backup_putid($restore->backup_unique_code,"question",$oldid,
- $newid);
- }
- //If it's a new question in the DB, restore it
- if ($creatingnewquestion) {
- //Now, restore every question_answers in this question
- $status = quiz_restore_pre15_answers($oldid,$newid,$que_info,$restore);
- //Now, depending of the type of questions, invoke different functions
- if ($question->qtype == "1") {
- $status = quiz_restore_pre15_shortanswer($oldid,$newid,$que_info,$restore);
- } else if ($question->qtype == "2") {
- $status = quiz_restore_pre15_truefalse($oldid,$newid,$que_info,$restore);
- } else if ($question->qtype == "3") {
- $status = quiz_restore_pre15_multichoice($oldid,$newid,$que_info,$restore);
- } else if ($question->qtype == "4") {
- //Random question. Nothing to do.
- } else if ($question->qtype == "5") {
- $status = quiz_restore_pre15_match($oldid,$newid,$que_info,$restore);
- } else if ($question->qtype == "6") {
- $status = quiz_restore_pre15_randomsamatch($oldid,$newid,$que_info,$restore);
- } else if ($question->qtype == "7") {
- //Description question. Nothing to do.
- } else if ($question->qtype == "8") {
- $status = quiz_restore_pre15_numerical($oldid,$newid,$que_info,$restore);
- } else if ($question->qtype == "9") {
- $status = quiz_restore_pre15_multianswer($oldid,$newid,$que_info,$restore);
- } else if ($question->qtype == "10") {
- $status = quiz_restore_pre15_calculated($oldid,$newid,$que_info,$restore);
- }
- } else {
- //We are NOT creating the question, but we need to know every question_answers
- //map between the XML file and the database to be able to restore the responses
- //in each attempt.
- $status = quiz_restore_pre15_map_answers($oldid,$newid,$que_info,$restore);
- //Now, depending of the type of questions, invoke different functions
- //to create the necessary mappings in backup_ids, because we are not
- //creating the question, but need some records in backup table
- if ($question->qtype == "1") {
- //Shortanswer question. Nothing to remap
- } else if ($question->qtype == "2") {
- //Truefalse question. Nothing to remap
- } else if ($question->qtype == "3") {
- //Multichoice question. Nothing to remap
- } else if ($question->qtype == "4") {
- //Random question. Nothing to remap
- } else if ($question->qtype == "5") {
- $status = quiz_restore_pre15_map_match($oldid,$newid,$que_info,$restore);
- } else if ($question->qtype == "6") {
- //Randomsamatch question. Nothing to remap
- } else if ($question->qtype == "7") {
- //Description question. Nothing to remap
- } else if ($question->qtype == "8") {
- //Numerical question. Nothing to remap
- } else if ($question->qtype == "9") {
- //Multianswer question. Nothing to remap
- } else if ($question->qtype == "10") {
- //Calculated question. Nothing to remap
- }
- }
- }
- return $status;
- }
-
- function quiz_restore_pre15_answers ($old_question_id,$new_question_id,$info,$restore) {
-
- global $CFG;
-
- $status = true;
-
- //Get the answers array
- if (isset($info['#']['ANSWERS']['0']['#']['ANSWER'])) {
- $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
-
- //Iterate over answers
- for($i = 0; $i < sizeof($answers); $i++) {
- $ans_info = $answers[$i];
- //traverse_xmlize($ans_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //We'll need this later!!
- $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
-
- //Now, build the question_answers record structure
- $answer->question = $new_question_id;
- $answer->answer = backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
- $answer->fraction = backup_todb($ans_info['#']['FRACTION']['0']['#']);
- $answer->feedback = backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
-
- //The structure is equal to the db, so insert the question_answers
- $newid = insert_record ("question_answers",$answer);
-
- //Do some output
- if (($i+1) % 50 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 1000 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
-
- if ($newid) {
- //We have the newid, update backup_ids
- backup_putid($restore->backup_unique_code,"question_answers",$oldid,
- $newid);
- } else {
- $status = false;
- }
- }
- }
-
- return $status;
- }
-
- function quiz_restore_pre15_map_answers ($old_question_id,$new_question_id,$info,$restore) {
-
- global $CFG;
-
- $status = true;
-
- if (!isset($info['#']['ANSWERS'])) { // No answers in this question (eg random)
- return $status;
- }
-
- //Get the answers array
- $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
-
- //Iterate over answers
- for($i = 0; $i < sizeof($answers); $i++) {
- $ans_info = $answers[$i];
- //traverse_xmlize($ans_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //We'll need this later!!
- $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
-
- //Now, build the question_answers record structure
- $answer->question = $new_question_id;
- $answer->answer = backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
- $answer->fraction = backup_todb($ans_info['#']['FRACTION']['0']['#']);
- $answer->feedback = backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
-
- //If we are in this method is because the question exists in DB, so its
- //answers must exist too.
- //Now, we are going to look for that answer in DB and to create the
- //mappings in backup_ids to use them later where restoring responses (user level).
-
- //Get the answer from DB (by question, answer and fraction)
- $db_answer = get_record ("question_answers","question",$new_question_id,
- "answer",$answer->answer,
- "fraction",$answer->fraction);
-
- //Do some output
- if (($i+1) % 50 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 1000 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
-
- if ($db_answer) {
- //We have the database answer, update backup_ids
- backup_putid($restore->backup_unique_code,"question_answers",$oldid,
- $db_answer->id);
- } else {
- $status = false;
- }
- }
-
- return $status;
- }
-
- function quiz_restore_pre15_shortanswer ($old_question_id,$new_question_id,$info,$restore,$restrictto = '') {
-
- global $CFG;
-
- $status = true;
-
- //Get the shortanswers array
- $shortanswers = $info['#']['SHORTANSWER'];
-
- //Iterate over shortanswers
- for($i = 0; $i < sizeof($shortanswers); $i++) {
- $sho_info = $shortanswers[$i];
- //traverse_xmlize($sho_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //Now, build the question_shortanswer record structure
- $shortanswer->question = $new_question_id;
- $shortanswer->answers = backup_todb($sho_info['#']['ANSWERS']['0']['#']);
- $shortanswer->usecase = backup_todb($sho_info['#']['USECASE']['0']['#']);
-
- //We have to recode the answers field (a list of answers id)
- //Extracts answer id from sequence
- $answers_field = "";
- $in_first = true;
- $tok = strtok($shortanswer->answers,",");
- while ($tok) {
- //Get the answer from backup_ids
- $answer = backup_getid($restore->backup_unique_code,"question_answers",$tok);
- if ($answer) {
- if ($in_first) {
- $answers_field .= $answer->new_id;
- $in_first = false;
- } else {
- $answers_field .= ",".$answer->new_id;
- }
- }
- //check for next
- $tok = strtok(",");
- }
- //We have the answers field recoded to its new ids
- $shortanswer->answers = $answers_field;
-
- //The structure is equal to the db, so insert the question_shortanswer
- //Only if there aren't restrictions or there are restriction concordance
- if (empty($restrictto) || (!empty($restrictto) && $shortanswer->answers == $restrictto)) {
- $newid = insert_record ("question_shortanswer",$shortanswer);
- }
-
- //Do some output
- if (($i+1) % 50 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 1000 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
-
- if (!$newid && !$restrictto) {
- $status = false;
- }
- }
-
- return $status;
- }
-
- function quiz_restore_pre15_truefalse ($old_question_id,$new_question_id,$info,$restore) {
-
- global $CFG;
-
- $status = true;
-
- //Get the truefalse array
- $truefalses = $info['#']['TRUEFALSE'];
-
- //Iterate over truefalse
- for($i = 0; $i < sizeof($truefalses); $i++) {
- $tru_info = $truefalses[$i];
- //traverse_xmlize($tru_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //Now, build the question_truefalse record structure
- $truefalse->question = $new_question_id;
- $truefalse->trueanswer = backup_todb($tru_info['#']['TRUEANSWER']['0']['#']);
- $truefalse->falseanswer = backup_todb($tru_info['#']['FALSEANSWER']['0']['#']);
-
- ////We have to recode the trueanswer field
- $answer = backup_getid($restore->backup_unique_code,"question_answers",$truefalse->trueanswer);
- if ($answer) {
- $truefalse->trueanswer = $answer->new_id;
- }
-
- ////We have to recode the falseanswer field
- $answer = backup_getid($restore->backup_unique_code,"question_answers",$truefalse->falseanswer);
- if ($answer) {
- $truefalse->falseanswer = $answer->new_id;
- }
-
- //The structure is equal to the db, so insert the question_truefalse
- $newid = insert_record ("question_truefalse",$truefalse);
-
- //Do some output
- if (($i+1) % 50 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 1000 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
-
- if (!$newid) {
- $status = false;
- }
- }
-
- return $status;
- }
-
- function quiz_restore_pre15_multichoice ($old_question_id,$new_question_id,$info,$restore, $restrictto = '') {
-
- global $CFG;
-
- $status = true;
-
- //Get the multichoices array
- $multichoices = $info['#']['MULTICHOICE'];
-
- //Iterate over multichoices
- for($i = 0; $i < sizeof($multichoices); $i++) {
- $mul_info = $multichoices[$i];
- //traverse_xmlize($mul_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //Now, build the question_multichoice record structure
- $multichoice->question = $new_question_id;
- $multichoice->layout = backup_todb($mul_info['#']['LAYOUT']['0']['#']);
- $multichoice->answers = backup_todb($mul_info['#']['ANSWERS']['0']['#']);
- $multichoice->single = backup_todb($mul_info['#']['SINGLE']['0']['#']);
-
- //We have to recode the answers field (a list of answers id)
- //Extracts answer id from sequence
- $answers_field = "";
- $in_first = true;
- $tok = strtok($multichoice->answers,",");
- while ($tok) {
- //Get the answer from backup_ids
- $answer = backup_getid($restore->backup_unique_code,"question_answers",$tok);
- if ($answer) {
- if ($in_first) {
- $answers_field .= $answer->new_id;
- $in_first = false;
- } else {
- $answers_field .= ",".$answer->new_id;
- }
- }
- //check for next
- $tok = strtok(",");
- }
- //We have the answers field recoded to its new ids
- $multichoice->answers = $answers_field;
-
- //The structure is equal to the db, so insert the question_shortanswer
- //Only if there aren't restrictions or there are restriction concordance
- if (empty($restrictto) || (!empty($restrictto) && $multichoice->answers == $restrictto)) {
- $newid = insert_record ("question_multichoice",$multichoice);
- }
-
- //Do some output
- if (($i+1) % 50 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 1000 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
-
- if (!$newid && !$restrictto) {
- $status = false;
- }
- }
-
- return $status;
- }
-
- function quiz_restore_pre15_match ($old_question_id,$new_question_id,$info,$restore) {
-
- global $CFG;
-
- $status = true;
-
- //Get the matchs array
- $matchs = $info['#']['MATCHS']['0']['#']['MATCH'];
-
- //We have to build the subquestions field (a list of match_sub id)
- $subquestions_field = "";
- $in_first = true;
-
- //Iterate over matchs
- for($i = 0; $i < sizeof($matchs); $i++) {
- $mat_info = $matchs[$i];
- //traverse_xmlize($mat_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //We'll need this later!!
- $oldid = backup_todb($mat_info['#']['ID']['0']['#']);
-
- //Now, build the question_match_SUB record structure
- $match_sub->question = $new_question_id;
- $match_sub->questiontext = backup_todb($mat_info['#']['QUESTIONTEXT']['0']['#']);
- $match_sub->answertext = backup_todb($mat_info['#']['ANSWERTEXT']['0']['#']);
-
- //The structure is equal to the db, so insert the question_match_sub
- $newid = insert_record ("question_match_sub",$match_sub);
-
- //Do some output
- if (($i+1) % 50 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 1000 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
-
- if ($newid) {
- //We have the newid, update backup_ids
- backup_putid($restore->backup_unique_code,"question_match_sub",$oldid,
- $newid);
- //We have a new match_sub, append it to subquestions_field
- if ($in_first) {
- $subquestions_field .= $newid;
- $in_first = false;
- } else {
- $subquestions_field .= ",".$newid;
- }
- } else {
- $status = false;
- }
- }
-
- //We have created every match_sub, now create the match
- $match->question = $new_question_id;
- $match->subquestions = $subquestions_field;
-
- //The structure is equal to the db, so insert the question_match_sub
- $newid = insert_record ("question_match",$match);
-
- if (!$newid) {
- $status = false;
- }
-
- return $status;
- }
-
- function quiz_restore_pre15_map_match ($old_question_id,$new_question_id,$info,$restore) {
-
- global $CFG;
-
- $status = true;
-
- //Get the matchs array
- $matchs = $info['#']['MATCHS']['0']['#']['MATCH'];
-
- //We have to build the subquestions field (a list of match_sub id)
- $subquestions_field = "";
- $in_first = true;
-
- //Iterate over matchs
- for($i = 0; $i < sizeof($matchs); $i++) {
- $mat_info = $matchs[$i];
- //traverse_xmlize($mat_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //We'll need this later!!
- $oldid = backup_todb($mat_info['#']['ID']['0']['#']);
-
- //Now, build the question_match_SUB record structure
- $match_sub->question = $new_question_id;
- $match_sub->questiontext = backup_todb($mat_info['#']['QUESTIONTEXT']['0']['#']);
- $match_sub->answertext = backup_todb($mat_info['#']['ANSWERTEXT']['0']['#']);
-
- //If we are in this method is because the question exists in DB, so its
- //match_sub must exist too.
- //Now, we are going to look for that match_sub in DB and to create the
- //mappings in backup_ids to use them later where restoring responses (user level).
-
- //Get the match_sub from DB (by question, questiontext and answertext)
- $db_match_sub = get_record ("question_match_sub","question",$new_question_id,
- "questiontext",$match_sub->questiontext,
- "answertext",$match_sub->answertext);
- //Do some output
- if (($i+1) % 50 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 1000 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
-
- //We have the database match_sub, so update backup_ids
- if ($db_match_sub) {
- //We have the newid, update backup_ids
- backup_putid($restore->backup_unique_code,"question_match_sub",$oldid,
- $db_match_sub->id);
- } else {
- $status = false;
- }
- }
-
- return $status;
- }
-
- function quiz_restore_pre15_randomsamatch ($old_question_id,$new_question_id,$info,$restore) {
-
- global $CFG;
-
- $status = true;
-
- //Get the randomsamatchs array
- $randomsamatchs = $info['#']['RANDOMSAMATCH'];
-
- //Iterate over randomsamatchs
- for($i = 0; $i < sizeof($randomsamatchs); $i++) {
- $ran_info = $randomsamatchs[$i];
- //traverse_xmlize($ran_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //Now, build the question_randomsamatch record structure
- $randomsamatch->question = $new_question_id;
- $randomsamatch->choose = backup_todb($ran_info['#']['CHOOSE']['0']['#']);
-
- //The structure is equal to the db, so insert the question_randomsamatch
- $newid = insert_record ("question_randomsamatch",$randomsamatch);
-
- //Do some output
- if (($i+1) % 50 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 1000 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
-
- if (!$newid) {
- $status = false;
- }
- }
-
- return $status;
- }
-
- function quiz_restore_pre15_numerical ($old_question_id,$new_question_id,$info,$restore, $restrictto = '') {
-
- global $CFG;
-
- $status = true;
-
- //Get the numerical array
- $numericals = $info['#']['NUMERICAL'];
-
- //Iterate over numericals
- for($i = 0; $i < sizeof($numericals); $i++) {
- $num_info = $numericals[$i];
- //traverse_xmlize($num_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //Now, build the question_numerical record structure
- $numerical->question = $new_question_id;
- $numerical->answer = backup_todb($num_info['#']['ANSWER']['0']['#']);
- $numerical->min = backup_todb($num_info['#']['MIN']['0']['#']);
- $numerical->max = backup_todb($num_info['#']['MAX']['0']['#']);
-
- ////We have to recode the answer field
- $answer = backup_getid($restore->backup_unique_code,"question_answers",$numerical->answer);
- if ($answer) {
- $numerical->answer = $answer->new_id;
- }
-
- //Answer goes to answers in 1.5 (although it continues being only one!)
- //Changed 12-05 (chating with Gustav and Julian this remains = pre15 = answer)
- //$numerical->answers = $numerical->answer;
-
- //We have to calculate the tolerance field of the numerical question
- $numerical->tolerance = ($numerical->max - $numerical->min)/2;
-
- //The structure is equal to the db, so insert the question_numerical
- //Only if there aren't restrictions or there are restriction concordance
- if (empty($restrictto) || (!empty($restrictto) && in_array($numerical->answer,explode(",",$restrictto)))) {
- $newid = insert_record ("question_numerical",$numerical);
- }
-
- //Do some output
- if (($i+1) % 50 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 1000 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
-
- //Now restore numerical_units
- if ($newid) {
- $status = quiz_restore_pre15_numerical_units ($old_question_id,$new_question_id,$num_info,$restore);
- }
-
- if (!$newid && !$restrictto) {
- $status = false;
- }
- }
-
- return $status;
- }
-
- function quiz_restore_pre15_calculated ($old_question_id,$new_question_id,$info,$restore) {
-
- global $CFG;
-
- $status = true;
-
- //Get the calculated-s array
- $calculateds = $info['#']['CALCULATED'];
-
- //Iterate over calculateds
- for($i = 0; $i < sizeof($calculateds); $i++) {
- $cal_info = $calculateds[$i];
- //traverse_xmlize($cal_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //Now, build the question_calculated record structure
- $calculated->question = $new_question_id;
- $calculated->answer = backup_todb($cal_info['#']['ANSWER']['0']['#']);
- $calculated->tolerance = backup_todb($cal_info['#']['TOLERANCE']['0']['#']);
- $calculated->tolerancetype = backup_todb($cal_info['#']['TOLERANCETYPE']['0']['#']);
- $calculated->correctanswerlength = backup_todb($cal_info['#']['CORRECTANSWERLENGTH']['0']['#']);
- $calculated->correctanswerformat = backup_todb($cal_info['#']['CORRECTANSWERFORMAT']['0']['#']);
-
- ////We have to recode the answer field
- $answer = backup_getid($restore->backup_unique_code,"question_answers",$calculated->answer);
- if ($answer) {
- $calculated->answer = $answer->new_id;
- }
-
- //If we haven't correctanswerformat, it defaults to 2 (in DB)
- if (empty($calculated->correctanswerformat)) {
- $calculated->correctanswerformat = 2;
- }
-
- //The structure is equal to the db, so insert the question_calculated
- $newid = insert_record ("question_calculated",$calculated);
-
- //Do some output
- if (($i+1) % 50 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 1000 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
-
- //Now restore numerical_units
- $status = quiz_restore_pre15_numerical_units ($old_question_id,$new_question_id,$cal_info,$restore);
-
- //Now restore dataset_definitions
- if ($status && $newid) {
- $status = quiz_restore_pre15_dataset_definitions ($old_question_id,$new_question_id,$cal_info,$restore);
- }
-
- if (!$newid) {
- $status = false;
- }
- }
-
- return $status;
- }
-
- function quiz_restore_pre15_multianswer ($old_question_id,$new_question_id,$info,$restore) {
-
- global $CFG;
-
- $status = true;
-
- //We need some question fields here so we get the full record from DB
- $parentquestion = get_record('question','id',$new_question_id);
-
- //We need to store all the positions with their created questions
- //to be able to calculate the sequence field
- $createdquestions = array();
-
- //Under 1.5, every multianswer record becomes a question itself
- //with its parent set to the cloze question. And there is only
- //ONE multianswer record with the sequence of questions used.
-
- //Get the multianswers array
- $multianswers_array = $info['#']['MULTIANSWERS']['0']['#']['MULTIANSWER'];
- //Iterate over multianswers_array
- for($i = 0; $i < sizeof($multianswers_array); $i++) {
- $mul_info = $multianswers_array[$i];
- //traverse_xmlize($mul_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //We need this later
- $oldid = backup_todb($mul_info['#']['ID']['0']['#']);
-
- //Now, build the question_multianswer record structure
- $multianswer->question = $new_question_id;
- $multianswer->answers = backup_todb($mul_info['#']['ANSWERS']['0']['#']);
- $multianswer->positionkey = backup_todb($mul_info['#']['POSITIONKEY']['0']['#']);
- $multianswer->answertype = backup_todb($mul_info['#']['ANSWERTYPE']['0']['#']);
- $multianswer->norm = backup_todb($mul_info['#']['NORM']['0']['#']);
-
- //Saving multianswer and positionkey to use them later restoring states
- backup_putid ($restore->backup_unique_code,'multianswer-pos',$oldid,$multianswer->positionkey);
-
- //We have to recode all the answers to their new ids
- $ansarr = explode(",", $multianswer->answers);
- foreach ($ansarr as $key => $value) {
- //Get the answer from backup_ids
- $answer = backup_getid($restore->backup_unique_code,'question_answers',$value);
- $ansarr[$key] = $answer->new_id;
- }
- $multianswer->answers = implode(",",$ansarr);
-
- //Build the new question structure
- $question = new object;
- $question->category = $parentquestion->category;
- $question->parent = $parentquestion->id;
- $question->name = $parentquestion->name;
- $question->questiontextformat = $parentquestion->questiontextformat;
- $question->defaultgrade = $multianswer->norm;
- $question->penalty = $parentquestion->penalty;
- $question->qtype = $multianswer->answertype;
- $question->version = $parentquestion->version;
- $question->hidden = $parentquestion->hidden;
- $question->length = 0;
- $question->questiontext = '';
- $question->stamp = make_unique_id_code();
-
- //Save the new question to DB
- $newid = insert_record('question', $question);
-
- if ($newid) {
- $createdquestions[$multianswer->positionkey] = $newid;
- }
-
- //Do some output
- if (($i+1) % 50 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 1000 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
-
- //Remap question_answers records from the original multianswer question
- //to their newly created question
- if ($newid) {
- $answersdb = get_records_list('question_answers','id',$multianswer->answers);
- foreach ($answersdb as $answerdb) {
- …
Large files files are truncated, but you can click here to view the full file