Skip to content
Browse files

Now numerical questions are included in backup and restore too !!

  • Loading branch information...
1 parent 8fa367a commit e2bad68d231ce1ae9d1f195016793d1d21304788 stronk7 committed
Showing with 150 additions and 35 deletions.
  1. +46 −17 mod/quiz/backuplib.php
  2. +104 −18 mod/quiz/restorelib.php
View
63 mod/quiz/backuplib.php
@@ -2,7 +2,7 @@
//This php script contains all the stuff to backup/restore
//quiz mods
- //To see, put your terminal to 132cc
+ //To see, put your terminal to 132cc
//This is the "graphical" structure of the quiz mod:
//
@@ -23,21 +23,21 @@
// |
// |
// |
- // ------------------------------------------------------------------------------------------------------
- // | | | |
- // | | | |
- // | | | | quiz_randomsamatch
- // quiz_truefalse quiz_shortanswer quiz_multichoice |---------(CL,pl->id,fk->question)
- // (CL,pl->id,fk->question) (CL,pl->id,fk->question) (CL,pl->id,fk->question) |
- // . . . |
- // . . . |
- // . . . | quiz_match
- // .................................................... |---------(CL,pl->id,fk->question)
- // . | .
- // . | .
- // . | .
- // quiz_answers | quiz_match_sub
- // (CL,pk->id,fk->question)----------------------------|---------(CL,pl->id,fk->question)
+ // --------------------------------------------------------------------------------------
+ // | | | |
+ // | | | |
+ // | | | | quiz_randomsamatch
+ // quiz_truefalse quiz_shortanswer quiz_multichoice quiz_numerical |--(CL,pl->id,fk->question)
+ // (CL,pl->id,fk->question)(CL,pl->id,fk->question)(CL,pl->id,fk->question)(CL,pl->id,fk->question) |
+ // . . . . |
+ // . . . . |
+ // . . . . | quiz_match
+ // ...................................................................... |--(CL,pl->id,fk->question)
+ // . | .
+ // . | .
+ // . | .
+ // quiz_answers | quiz_match_sub
+ // (CL,pk->id,fk->question)---------------------------------|--(CL,pl->id,fk->question)
//
// Meaning: pk->primary key field of the table
// fk->foreign key to link with parent
@@ -57,6 +57,7 @@
// - quiz_truefalse
// - quiz_shortanswer
// - quiz_multichoice
+ // - quiz_numerical
// - quiz_randomsamatch
// - quiz_match
// - quiz_match_sub
@@ -151,6 +152,8 @@ function quiz_backup_question($bf,$preferences,$category) {
$status = quiz_backup_randomsamatch($bf,$preferences,$question->id);
} else if ($question->qtype == "7") {
//Description question. Nothing to write.
+ } else if ($question->qtype == "8") {
+ $status = quiz_backup_numerical($bf,$preferences,$question->id);
}
//End question
$status =fwrite ($bf,end_tag("QUESTION",5,true));
@@ -294,8 +297,34 @@ function quiz_backup_match($bf,$preferences,$question) {
return $status;
}
+ //This function backups the data in a numerical question (qtype=8) and its
+ //asociated data
+ function quiz_backup_numerical($bf,$preferences,$question) {
+
+ global $CFG;
+
+ $status = true;
+
+ $numericals = get_records("quiz_numerical","question",$question,"id");
+ //If there are numericals
+ if ($numericals) {
+ //Iterate over each numerical
+ foreach ($numericals as $numerical) {
+ $status =fwrite ($bf,start_tag("NUMERICAL",6,true));
+ //Print numerical contents
+ fwrite ($bf,full_tag("ANSWER",7,false,$numerical->answer));
+ fwrite ($bf,full_tag("MIN",7,false,$numerical->min));
+ fwrite ($bf,full_tag("MAX",7,false,$numerical->max));
+ $status =fwrite ($bf,end_tag("NUMERICAL",6,true));
+ }
+ //Now print quiz_answers
+ $status = quiz_backup_answers($bf,$preferences,$question);
+ }
+ return $status;
+ }
+
//This function backups the answers data in some question types
- //(truefalse, shortanswer,multichoice)
+ //(truefalse, shortanswer,multichoice,numerical)
function quiz_backup_answers($bf,$preferences,$question) {
global $CFG;
View
122 mod/quiz/restorelib.php
@@ -2,7 +2,7 @@
//This php script contains all the stuff to backup/restore
//quiz mods
- //To see, put your terminal to 132cc
+ //To see, put your terminal to 132cc
//This is the "graphical" structure of the quiz mod:
//
@@ -18,27 +18,61 @@
// | | |
// | | |
// | | |
- // quiz_responses | quiz_questions
+ // quiz_responses | quiz_questions
// (UL,pk->id, fk->attempt)----------------------------------------------------(CL,pk->id,fk->category,files)
// |
// |
// |
- // ------------------------------------------------------------------------------------------------------
- // | | | |
- // | | | |
- // | | | | quiz_randomsamatch
- // quiz_truefalse quiz_shortanswer quiz_multichoice |---------(CL,pl->id,fk->question)
- // (CL,pl->id,fk->question) (CL,pl->id,fk->question) (CL,pl->id,fk->question) |
- // . . . |
- // . . . |
- // . . . | quiz_match
- // .................................................... |---------(CL,pl->id,fk->question)
- // . | .
- // . | .
- // . | .
- // quiz_answers | quiz_match_sub
- // (CL,pk->id,fk->question)----------------------------|---------(CL,pl->id,fk->question)
- //
+ // --------------------------------------------------------------------------------------
+ // | | | |
+ // | | | |
+ // | | | | quiz_randomsamatch
+ // quiz_truefalse quiz_shortanswer quiz_multichoice quiz_numerical |--(CL,pl->id,fk->question)
+ // (CL,pl->id,fk->question)(CL,pl->id,fk->question)(CL,pl->id,fk->question)(CL,pl->id,fk->question) |
+ // . . . . |
+ // . . . . |
+ // . . . . | quiz_match
+ // ...................................................................... |--(CL,pl->id,fk->question)
+ // . | .
+ // . | .
+ // . | .
+ // quiz_answers | quiz_match_sub
+ // (CL,pk->id,fk->question)---------------------------------|--(CL,pl->id,fk->question)
+ // //This is the "graphical" structure of the quiz mod:
+ //
+ // quiz quiz_categories
+ // (CL,pk->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_shortanswer quiz_multichoice quiz_numerical |--(CL,pl->id,fk->question)
+ // (CL,pl->id,fk->question)(CL,pl->id,fk->question)(CL,pl->id,fk->question)(CL,pl->id,fk->question) |
+ // . . . . |
+ // . . . . |
+ // . . . . | quiz_match
+ // ...................................................................... |--(CL,pl->id,fk->question)
+ // . | .
+ // . | .
+ // . | .
+ // quiz_answers | quiz_match_sub
+ // (CL,pk->id,fk->question)---------------------------------|--(CL,pl->id,fk->question)
+ //
// Meaning: pk->primary key field of the table
// fk->foreign key to link with parent
// nt->nested field (recursive data)
@@ -55,6 +89,7 @@
// - quiz_truefalse
// - quiz_shortanswer
// - quiz_multichoice
+ // - quiz_numerical
// - quiz_randomsamatch
// - quiz_match
// - quiz_match_sub
@@ -188,6 +223,8 @@ function quiz_restore_questions ($old_category_id,$new_category_id,$info,$restor
$status = quiz_restore_randomsamatch($oldid,$newid,$que_info,$restore);
} else if ($question->qtype == "7") {
//Description question. Nothing to do.
+ } else if ($question->qtype == "8") {
+ $status = quiz_restore_numerical($oldid,$newid,$que_info,$restore);
}
} else {
$status = false;
@@ -536,7 +573,53 @@ function quiz_restore_randomsamatch ($old_question_id,$new_question_id,$info,$re
return $status;
}
+ function quiz_restore_numerical ($old_question_id,$new_question_id,$info,$restore) {
+
+ 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 QUIZ_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,"quiz_answers",$numerical->answer);
+ if ($answer) {
+ $numerical->answer = $answer->new_id;
+ }
+
+ //The structure is equal to the db, so insert the quiz_numerical
+ $newid = insert_record ("quiz_numerical",$numerical);
+
+ //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;
+ }
//STEP 2. Restore quizzes and associated structures
@@ -886,6 +969,9 @@ function quiz_responses_restore_mods($attempt_id,$info,$restore) {
//Nothing to do (there is no awser to this qtype)
//But this case must exist !!
break;
+ case 8: //NUMERICAL QTYPE
+ //Nothing to do. The response is a text.
+ break;
default: //UNMATCHED QTYPE.
//This is an error (unimplemented qtype)
$status = false;

0 comments on commit e2bad68

Please sign in to comment.
Something went wrong with that request. Please try again.