Index: restorelibpre15.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/quiz/restorelibpre15.php,v retrieving revision 1.26.8.1 diff -u -r1.26.8.1 restorelibpre15.php --- restorelibpre15.php 2 Nov 2007 16:19:57 -0000 1.26.8.1 +++ restorelibpre15.php 24 Jul 2008 00:53:11 -0000 @@ -81,6 +81,7 @@ // 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). + // This is performed by question/restorelib.php !! // 2.-Standard module restore (Invoked via quiz_restore_mods). It includes this tables: // - quiz @@ -94,1124 +95,6 @@ //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 "
  • ".get_string('category', 'quiz')." \"".$quiz_cat->name."\"
    "; - } - } else { - if (!defined('RESTORE_SILENTLY')) { - //We must never arrive here !! - echo "
  • ".get_string('category', 'quiz')." \"".$quiz_cat->name."\" Error!
    "; - } - $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 '
  • '; - } - } - - 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 "
    "; - } - } - 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 "
    "; - } - } - 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 "
    "; - } - } - 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 "
    "; - } - } - 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 "
    "; - } - } - 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 "
    "; - } - } - 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 "
    "; - } - } - 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 "
    "; - } - } - 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 "
    "; - } - } - 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 "
    "; - } - } - 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 "
    "; - } - } - 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 "
    "; - } - } - 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) { - set_field('question_answers','question',$newid,'id',$answerdb->id); - } - } - - //If we have created the question record, now, depending of the - //answertype, delegate the restore to every qtype function - if ($newid) { - if ($multianswer->answertype == "1") { - $status = quiz_restore_pre15_shortanswer ($old_question_id,$newid,$mul_info,$restore,$multianswer->answers); - } else if ($multianswer->answertype == "3") { - $status = quiz_restore_pre15_multichoice ($old_question_id,$newid,$mul_info,$restore,$multianswer->answers); - } else if ($multianswer->answertype == "8") { - $status = quiz_restore_pre15_numerical ($old_question_id,$newid,$mul_info,$restore,$multianswer->answers); - } - } else { - $status = false; - } - } - - //Everything is created, just going to create the multianswer record - if ($status) { - ksort($createdquestions); - - $multianswerdb = new object; - $multianswerdb->question = $parentquestion->id; - $multianswerdb->sequence = implode(",",$createdquestions); - $mid = insert_record('question_multianswer', $multianswerdb); - - if (!$mid) { - $status = false; - } - } - - return $status; - } - - function quiz_restore_pre15_numerical_units ($old_question_id,$new_question_id,$info,$restore) { - - global $CFG; - - $status = true; - - //Get the numerical array - $numerical_units = $info['#']['NUMERICAL_UNITS']['0']['#']['NUMERICAL_UNIT']; - - //Iterate over numerical_units - for($i = 0; $i < sizeof($numerical_units); $i++) { - $nu_info = $numerical_units[$i]; - //traverse_xmlize($nu_info); //Debug - //print_object ($GLOBALS['traverse_array']); //Debug - //$GLOBALS['traverse_array']=""; //Debug - - //Now, build the question_numerical_UNITS record structure - $numerical_unit->question = $new_question_id; - $numerical_unit->multiplier = backup_todb($nu_info['#']['MULTIPLIER']['0']['#']); - $numerical_unit->unit = backup_todb($nu_info['#']['UNIT']['0']['#']); - - //The structure is equal to the db, so insert the question_numerical_units - $newid = insert_record ("question_numerical_units",$numerical_unit); - - if (!$newid) { - $status = false; - } - } - - return $status; - } - - function quiz_restore_pre15_dataset_definitions ($old_question_id,$new_question_id,$info,$restore) { - - global $CFG; - - $status = true; - - //Get the dataset_definitions array - $dataset_definitions = $info['#']['DATASET_DEFINITIONS']['0']['#']['DATASET_DEFINITION']; - - //Iterate over dataset_definitions - for($i = 0; $i < sizeof($dataset_definitions); $i++) { - $dd_info = $dataset_definitions[$i]; - //traverse_xmlize($dd_info); //Debug - //print_object ($GLOBALS['traverse_array']); //Debug - //$GLOBALS['traverse_array']=""; //Debug - - //Now, build the question_dataset_DEFINITION record structure - $dataset_definition->category = backup_todb($dd_info['#']['CATEGORY']['0']['#']); - $dataset_definition->name = backup_todb($dd_info['#']['NAME']['0']['#']); - $dataset_definition->type = backup_todb($dd_info['#']['TYPE']['0']['#']); - $dataset_definition->options = backup_todb($dd_info['#']['OPTIONS']['0']['#']); - $dataset_definition->itemcount = backup_todb($dd_info['#']['ITEMCOUNT']['0']['#']); - - //We have to recode the category field (only if the category != 0) - if ($dataset_definition->category != 0) { - $category = backup_getid($restore->backup_unique_code,"question_categories",$dataset_definition->category); - if ($category) { - $dataset_definition->category = $category->new_id; - } - } - - //Now, we hace to decide when to create the new records or reuse an existing one - $create_definition = false; - - //If the dataset_definition->category = 0, it's a individual question dataset_definition, so we'll create it - if ($dataset_definition->category == 0) { - $create_definition = true; - } else { - //The category isn't 0, so it's a category question dataset_definition, we have to see if it exists - //Look for a definition with the same category, name and type - if ($definitionrec = get_record_sql("SELECT d.* - FROM {$CFG->prefix}question_dataset_definitions d - WHERE d.category = '$dataset_definition->category' AND - d.name = '$dataset_definition->name' AND - d.type = '$dataset_definition->type'")) { - //Such dataset_definition exist. Now we must check if it has enough itemcount - if ($definitionrec->itemcount < $dataset_definition->itemcount) { - //We haven't enough itemcount, so we have to create the definition as an individual question one. - $dataset_definition->category = 0; - $create_definition = true; - } else { - //We have enough itemcount, so we'll reuse the existing definition - $create_definition = false; - $newid = $definitionrec->id; - } - } else { - //Such dataset_definition doesn't exist. We'll create it. - $create_definition = true; - } - } - - //If we've to create the definition, do it - if ($create_definition) { - //The structure is equal to the db, so insert the question_dataset_definitions - $newid = insert_record ("question_dataset_definitions",$dataset_definition); - if ($newid) { - //Restore question_dataset_items - $status = quiz_restore_pre15_dataset_items($newid,$dd_info,$restore); - } - } - - //Now, we must have a definition (created o reused). Its id is in newid. Create the question_datasets record - //to join the question and the dataset_definition - if ($newid) { - $question_dataset->question = $new_question_id; - $question_dataset->datasetdefinition = $newid; - $newid = insert_record ("question_datasets",$question_dataset); - } - - if (!$newid) { - $status = false; - } - } - - return $status; - } - - function quiz_restore_pre15_dataset_items ($definitionid,$info,$restore) { - - global $CFG; - - $status = true; - - //Get the items array - $dataset_items = $info['#']['DATASET_ITEMS']['0']['#']['DATASET_ITEM']; - - //Iterate over dataset_items - for($i = 0; $i < sizeof($dataset_items); $i++) { - $di_info = $dataset_items[$i]; - //traverse_xmlize($di_info); //Debug - //print_object ($GLOBALS['traverse_array']); //Debug - //$GLOBALS['traverse_array']=""; //Debug - - //Now, build the question_dataset_ITEMS record structure - $dataset_item->definition = $definitionid; - $dataset_item->itemnumber = backup_todb($di_info['#']['NUMBER']['0']['#']); - $dataset_item->value = backup_todb($di_info['#']['VALUE']['0']['#']); - - //The structure is equal to the db, so insert the question_dataset_items - $newid = insert_record ("question_dataset_items",$dataset_item); - - if (!$newid) { - $status = false; - } - } - - return $status; - } - //STEP 2. Restore quizzes and associated structures // (course dependent) function quiz_restore_pre15_mods($mod,$restore) { @@ -1584,9 +467,11 @@ //Depending of the qtype, we make different recodes switch ($question->qtype) { case 1: //SHORTANSWER QTYPE + case SHORTANSWER: //Nothing to do. The response is a text. break; case 2: //TRUEFALSE QTYPE + case TRUEFALSE: //The answer is one answer id. We must recode it $answer = backup_getid($restore->backup_unique_code,"question_answers",$response->answer); if ($answer) { @@ -1594,6 +479,7 @@ } break; case 3: //MULTICHOICE QTYPE + case MULTICHOICE: //The answer is a comma separated list of answers. We must recode them $answer_field = ""; $in_first = true; @@ -1615,6 +501,7 @@ $response->answer = $answer_field; break; case 4: //RANDOM QTYPE + case RANDOM: //The answer links to another question id, we must recode it $answer_link = backup_getid($restore->backup_unique_code,"question",$response->answer); if ($answer_link) { @@ -1622,6 +509,7 @@ } break; case 5: //MATCH QTYPE + case MATCH: //The answer is a comma separated list of hypen separated math_subs (for question and answer) $answer_field = ""; $in_first = true; @@ -1653,6 +541,7 @@ $response->answer = $answer_field; break; case 6: //RANDOMSAMATCH QTYPE + case RANDOMSAMATCH: //The answer is a comma separated list of hypen separated question_id and answer_id. We must recode them $answer_field = ""; $in_first = true; @@ -1684,13 +573,16 @@ $response->answer = $answer_field; break; case 7: //DESCRIPTION QTYPE + case DESCRIPTION: //Nothing to do (there is no awser to this qtype) //But this case must exist !! break; case 8: //NUMERICAL QTYPE + case NUMERICAL: //Nothing to do. The response is a text. break; case 9: //MULTIANSWER QTYPE + case MULTIANSWER: //The answer is a comma separated list of hypen separated multianswer ids and answers. We must recode them. //We need to have the sequence of questions here to be able to detect qtypes $multianswerdb = get_record('question_multianswer','question',$response->question); @@ -1718,15 +610,15 @@ $questiontype = $questiondb->qtype; //Now, depending of the answertype field in question_multianswer //we do diferent things - if ($questiontype == "1") { + if ($questiontype == "1" || $questiontype == SHORTANSWER) { //Shortanswer //The answer is text, do nothing - } else if ($questiontype == "3") { + } else if ($questiontype == "3" || $questiontype == MULTICHOICE) { //Multichoice //The answer is an answer_id, look for it in backup_ids $ans = backup_getid($restore->backup_unique_code,"question_answers",$answer); $answer = $ans->new_id; - } else if ($questiontype == "8") { + } else if ($questiontype == "8" || $questiontype == NUMERICAL) { //Numeric //The answer is text, do nothing } @@ -1745,6 +637,7 @@ $response->answer = $answer_field; break; case 10: //CALCULATED QTYPE + case CALCULATED: //Nothing to do. The response is a text. break; default: //UNMATCHED QTYPE.