Permalink
Browse files

Every questiontype should have its own backup and restore code. This …

…is a first step in that direction. I have created the individual restore libraries for all question types but they are not yet used.
  • Loading branch information...
1 parent 32a189d commit b2b97d6834b28a47ab9560443926e327e6f8c25f gustav_delius committed Mar 12, 2006
@@ -0,0 +1,61 @@
+<?php
+ function question_calculated_restore($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;
+ }
+
+ //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 = question_restore_numerical_units ($old_question_id,$new_question_id,$cal_info,$restore);
+
+ //Now restore dataset_definitions
+ if ($status && $newid) {
+ $status = question_restore_dataset_definitions ($old_question_id,$new_question_id,$cal_info,$restore);
+ }
+
+ if (!$newid) {
+ $status = false;
+ }
+ }
+
+ return $status;
+ }
+?>
@@ -0,0 +1,47 @@
+<?php
+ function question_essay_restore($old_question_id,$new_question_id,$info,$restore) {
+
+ global $CFG;
+
+ $status = true;
+
+ //Get the truefalse array
+ $essays = $info['#']['ESSAY'];
+
+ //Iterate over truefalse
+ for($i = 0; $i < sizeof($essays); $i++) {
+ $essay_info = $essays[$i];
+ //traverse_xmlize($tru_info); //Debug
+ //print_object ($GLOBALS['traverse_array']); //Debug
+ //$GLOBALS['traverse_array']=""; //Debug
+
+ //Now, build the question_essay record structure
+ $essay->question = $new_question_id;
+ $essay->answer = backup_todb($essay_info['#']['ANSWER']['0']['#']);
+
+ ////We have to recode the answer field
+ $answer = backup_getid($restore->backup_unique_code,"question_answers",$essay->answer);
+ if ($answer) {
+ $essay->answer = $answer->new_id;
+ }
+
+ //The structure is equal to the db, so insert the question_essay
+ $newid = insert_record ("question_essay",$essay);
+
+ //Do some output
+ if (($i+1) % 50 == 0) {
+ echo ".";
+ if (($i+1) % 1000 == 0) {
+ echo "<br />";
+ }
+ backup_flush(300);
+ }
+
+ if (!$newid) {
+ $status = false;
+ }
+ }
+
+ return $status;
+ }
+?>
@@ -0,0 +1,134 @@
+<?php
+ function question_match_restore($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 question_match_restore_map($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 states (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;
+ }
+?>
@@ -0,0 +1,140 @@
+<?php
+ function question_multianswer_restore($old_question_id,$new_question_id,$info,$restore) {
+
+ global $CFG;
+
+ $status = true;
+
+ //Get the multianswers array
+ $multianswers = $info['#']['MULTIANSWERS']['0']['#']['MULTIANSWER'];
+ //Iterate over multianswers
+ for($i = 0; $i < sizeof($multianswers); $i++) {
+ $mul_info = $multianswers[$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->sequence = backup_todb($mul_info['#']['SEQUENCE']['0']['#']);
+
+ //We have to recode the sequence field (a list of question ids)
+ //Extracts question id from sequence
+ $sequence_field = "";
+ $in_first = true;
+ $tok = strtok($multianswer->sequence,",");
+ while ($tok) {
+ //Get the answer from backup_ids
+ $question = backup_getid($restore->backup_unique_code,"question",$tok);
+ if ($question) {
+ if ($in_first) {
+ $sequence_field .= $question->new_id;
+ $in_first = false;
+ } else {
+ $sequence_field .= ",".$question->new_id;
+ }
+ }
+ //check for next
+ $tok = strtok(",");
+ }
+ //We have the answers field recoded to its new ids
+ $multianswer->sequence = $sequence_field;
+ //The structure is equal to the db, so insert the question_multianswer
+ $newid = insert_record ("question_multianswer",$multianswer);
+
+ //Save ids in backup_ids
+ if ($newid) {
+ backup_putid($restore->backup_unique_code,"question_multianswer",
+ $oldid, $newid);
+ }
+
+ //Do some output
+ if (($i+1) % 50 == 0) {
+ if (!defined('RESTORE_SILENTLY')) {
+ echo ".";
+ if (($i+1) % 1000 == 0) {
+ echo "<br />";
+ }
+ }
+ backup_flush(300);
+ }
+/*
+ //If we have created the question_multianswer record, now, depending of the
+ //answertype, delegate the restore to every qtype function
+ if ($newid) {
+ if ($multianswer->answertype == "1") {
+ $status = quiz_restore_shortanswer ($old_question_id,$new_question_id,$mul_info,$restore);
+ } else if ($multianswer->answertype == "3") {
+ $status = quiz_restore_multichoice ($old_question_id,$new_question_id,$mul_info,$restore);
+ } else if ($multianswer->answertype == "8") {
+ $status = quiz_restore_numerical ($old_question_id,$new_question_id,$mul_info,$restore);
+ }
+ } else {
+ $status = false;
+ }
+*/
+ }
+
+ return $status;
+ }
+
+ function question_multianswer_restore_map($old_question_id,$new_question_id,$info,$restore) {
+
+ global $CFG;
+
+ $status = true;
+
+ //Get the multianswers array
+ $multianswers = $info['#']['MULTIANSWERS']['0']['#']['MULTIANSWER'];
+ //Iterate over multianswers
+ for($i = 0; $i < sizeof($multianswers); $i++) {
+ $mul_info = $multianswers[$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']['#']);
+
+ //If we are in this method is because the question exists in DB, so its
+ //multianswer must exist too.
+ //Now, we are going to look for that multianswer in DB and to create the
+ //mappings in backup_ids to use them later where restoring states (user level).
+
+ //Get the multianswer from DB (by question and positionkey)
+ $db_multianswer = get_record ("question_multianswer","question",$new_question_id,
+ "positionkey",$multianswer->positionkey);
+ //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 multianswer, so update backup_ids
+ if ($db_multianswer) {
+ //We have the newid, update backup_ids
+ backup_putid($restore->backup_unique_code,"question_multianswer",$oldid,
+ $db_multianswer->id);
+ } else {
+ $status = false;
+ }
+ }
+
+ return $status;
+ }
+?>
Oops, something went wrong.

0 comments on commit b2b97d6

Please sign in to comment.