Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Now multianswer questions are working too.

Too complicated !!
  • Loading branch information...
commit f53119698ed2cea7d7b016ca1aad158296321e2f 1 parent 30c49aa
stronk7 authored
Showing with 254 additions and 19 deletions.
  1. +70 −19 mod/quiz/backuplib.php
  2. +184 −0 mod/quiz/restorelib.php
View
89 mod/quiz/backuplib.php
@@ -161,6 +161,8 @@ function quiz_backup_question($bf,$preferences,$category) {
//Description question. Nothing to write.
} else if ($question->qtype == "8") {
$status = quiz_backup_numerical($bf,$preferences,$question->id);
+ } else if ($question->qtype == "9") {
+ $status = quiz_backup_multianswer($bf,$preferences,$question->id);
}
//End question
$status =fwrite ($bf,end_tag("QUESTION",5,true));
@@ -207,7 +209,7 @@ function quiz_backup_truefalse($bf,$preferences,$question) {
//This function backups the data in a shortanswer question (qtype=1) and its
//asociated data
- function quiz_backup_shortanswer($bf,$preferences,$question) {
+ function quiz_backup_shortanswer($bf,$preferences,$question,$level=6,$include_answers=true) {
global $CFG;
@@ -218,21 +220,23 @@ function quiz_backup_shortanswer($bf,$preferences,$question) {
if ($shortanswers) {
//Iterate over each shortanswer
foreach ($shortanswers as $shortanswer) {
- $status =fwrite ($bf,start_tag("SHORTANSWER",6,true));
+ $status =fwrite ($bf,start_tag("SHORTANSWER",$level,true));
//Print shortanswer contents
- fwrite ($bf,full_tag("ANSWERS",7,false,$shortanswer->answers));
- fwrite ($bf,full_tag("USECASE",7,false,$shortanswer->usecase));
- $status =fwrite ($bf,end_tag("SHORTANSWER",6,true));
+ fwrite ($bf,full_tag("ANSWERS",$level+1,false,$shortanswer->answers));
+ fwrite ($bf,full_tag("USECASE",$level+1,false,$shortanswer->usecase));
+ $status =fwrite ($bf,end_tag("SHORTANSWER",$level,true));
}
//Now print quiz_answers
- $status = quiz_backup_answers($bf,$preferences,$question);
+ if ($include_answers) {
+ $status = quiz_backup_answers($bf,$preferences,$question);
+ }
}
return $status;
}
//This function backups the data in a multichoice question (qtype=3) and its
//asociated data
- function quiz_backup_multichoice($bf,$preferences,$question) {
+ function quiz_backup_multichoice($bf,$preferences,$question,$level=6,$include_answers=true) {
global $CFG;
@@ -243,15 +247,17 @@ function quiz_backup_multichoice($bf,$preferences,$question) {
if ($multichoices) {
//Iterate over each multichoice
foreach ($multichoices as $multichoice) {
- $status =fwrite ($bf,start_tag("MULTICHOICE",6,true));
+ $status =fwrite ($bf,start_tag("MULTICHOICE",$level,true));
//Print multichoice contents
- fwrite ($bf,full_tag("LAYOUT",7,false,$multichoice->layout));
- fwrite ($bf,full_tag("ANSWERS",7,false,$multichoice->answers));
- fwrite ($bf,full_tag("SINGLE",7,false,$multichoice->single));
- $status =fwrite ($bf,end_tag("MULTICHOICE",6,true));
+ fwrite ($bf,full_tag("LAYOUT",$level+1,false,$multichoice->layout));
+ fwrite ($bf,full_tag("ANSWERS",$level+1,false,$multichoice->answers));
+ fwrite ($bf,full_tag("SINGLE",$level+1,false,$multichoice->single));
+ $status =fwrite ($bf,end_tag("MULTICHOICE",$level,true));
}
//Now print quiz_answers
- $status = quiz_backup_answers($bf,$preferences,$question);
+ if ($include_answers) {
+ $status = quiz_backup_answers($bf,$preferences,$question);
+ }
}
return $status;
}
@@ -306,7 +312,7 @@ function quiz_backup_match($bf,$preferences,$question) {
//This function backups the data in a numerical question (qtype=8) and its
//asociated data
- function quiz_backup_numerical($bf,$preferences,$question) {
+ function quiz_backup_numerical($bf,$preferences,$question,$level=6,$include_answers=true) {
global $CFG;
@@ -317,13 +323,58 @@ function quiz_backup_numerical($bf,$preferences,$question) {
if ($numericals) {
//Iterate over each numerical
foreach ($numericals as $numerical) {
- $status =fwrite ($bf,start_tag("NUMERICAL",6,true));
+ $status =fwrite ($bf,start_tag("NUMERICAL",$level,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));
+ fwrite ($bf,full_tag("ANSWER",$level+1,false,$numerical->answer));
+ fwrite ($bf,full_tag("MIN",$level+1,false,$numerical->min));
+ fwrite ($bf,full_tag("MAX",$level+1,false,$numerical->max));
+ $status =fwrite ($bf,end_tag("NUMERICAL",$level,true));
+ }
+ //Now print quiz_answers
+ if ($include_answers) {
+ $status = quiz_backup_answers($bf,$preferences,$question);
+ }
+ }
+ return $status;
+ }
+
+ //This function backups the data in a multianswer question (qtype=9) and its
+ //asociated data
+ function quiz_backup_multianswer($bf,$preferences,$question) {
+
+ global $CFG;
+
+ $status = true;
+
+ $multianswers = get_records("quiz_multianswers","question",$question,"id");
+ //If there are multianswers
+ if ($multianswers) {
+ //Print multianswers header
+ $status =fwrite ($bf,start_tag("MULTIANSWERS",6,true));
+ //Iterate over each multianswer
+ foreach ($multianswers as $multianswer) {
+ $status =fwrite ($bf,start_tag("MULTIANSWER",7,true));
+ //Print multianswer contents
+ fwrite ($bf,full_tag("ID",8,false,$multianswer->id));
+ fwrite ($bf,full_tag("ANSWERS",8,false,$multianswer->answers));
+ fwrite ($bf,full_tag("POSITIONKEY",8,false,$multianswer->positionkey));
+ fwrite ($bf,full_tag("ANSWERTYPE",8,false,$multianswer->answertype));
+ fwrite ($bf,full_tag("NORM",8,false,$multianswer->norm));
+ //Depending of the ANSWERTYPE, we must encode different info
+ //to be able to re-create records in quiz_shortanswer, quiz_multichoice and
+ //quiz_numerical
+ if ($multianswer->answertype == "1") {
+ $status = quiz_backup_shortanswer($bf,$preferences,$question,8,false);
+ } else if ($multianswer->answertype == "3") {
+ $status = quiz_backup_multichoice($bf,$preferences,$question,8,false);
+ } else if ($multianswer->answertype == "8") {
+ $status = quiz_backup_numerical($bf,$preferences,$question,8,false);
+ }
+
+ $status =fwrite ($bf,end_tag("MULTIANSWER",7,true));
}
+ //Print multianswers footer
+ $status =fwrite ($bf,end_tag("MULTIANSWERS",6,true));
//Now print quiz_answers
$status = quiz_backup_answers($bf,$preferences,$question);
}
View
184 mod/quiz/restorelib.php
@@ -228,6 +228,8 @@ function quiz_restore_questions ($old_category_id,$new_category_id,$info,$restor
//Description question. Nothing to do.
} else if ($question->qtype == "8") {
$status = quiz_restore_numerical($oldid,$newid,$que_info,$restore);
+ } else if ($question->qtype == "9") {
+ $status = quiz_restore_multianswer($oldid,$newid,$que_info,$restore);
}
} else {
//We are NOT creating the question, but we need to know every quiz_answers
@@ -253,6 +255,8 @@ function quiz_restore_questions ($old_category_id,$new_category_id,$info,$restor
//Description question. Nothing to remap
} else if ($question->qtype == "8") {
//Numerical question. Nothing to remap
+ } else if ($question->qtype == "9") {
+ $status = quiz_restore_map_multianswer($oldid,$newid,$que_info,$restore);
}
}
}
@@ -672,6 +676,61 @@ function quiz_restore_map_match ($old_question_id,$new_question_id,$info,$restor
return $status;
}
+ function quiz_restore_map_multianswer ($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 QUIZ_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 responses (user level).
+
+ //Get the multianswer from DB (by question and positionkey)
+ $db_multianswer = get_record ("quiz_multianswers","question",$new_question_id,
+ "positionkey",$multianswer->positionkey);
+ //Do some output
+ if (($i+1) % 50 == 0) {
+ 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,"quiz_multianswers",$oldid,
+ $db_multianswer->id);
+ } else {
+ $status = false;
+ }
+ }
+
+ return $status;
+ }
+
function quiz_restore_randomsamatch ($old_question_id,$new_question_id,$info,$restore) {
global $CFG;
@@ -760,6 +819,89 @@ function quiz_restore_numerical ($old_question_id,$new_question_id,$info,$restor
return $status;
}
+ function quiz_restore_multianswer ($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 QUIZ_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']['#']);
+
+ //We have to recode the answers field (a list of answers id)
+ //Extracts answer id from sequence
+ $answers_field = "";
+ $in_first = true;
+ $tok = strtok($multianswer->answers,",");
+ while ($tok) {
+ //Get the answer from backup_ids
+ $answer = backup_getid($restore->backup_unique_code,"quiz_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
+ $multianswer->answers = $answers_field;
+
+ //The structure is equal to the db, so insert the quiz_multianswers
+ $newid = insert_record ("quiz_multianswers",$multianswer);
+
+ //Save ids in backup_ids
+ if ($newid) {
+ backup_putid($restore->backup_unique_code,"quiz_multianswers",
+ $oldid, $newid);
+ }
+
+ //Do some output
+ if (($i+1) % 50 == 0) {
+ echo ".";
+ if (($i+1) % 1000 == 0) {
+ echo "<br>";
+ }
+ backup_flush(300);
+ }
+
+ //If we have created the quiz_multianswers 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;
+ }
+
//STEP 2. Restore quizzes and associated structures
// (course dependent)
@@ -1120,6 +1262,48 @@ function quiz_responses_restore_mods($attempt_id,$info,$restore) {
case 8: //NUMERICAL QTYPE
//Nothing to do. The response is a text.
break;
+ case 9: //MULTIANSWER QTYPE
+ //The answer is a comma separated list of hypen separated multianswer_id and answers. We must recode them.
+ $answer_field = "";
+ $in_first = true;
+ $tok = strtok($response->answer,",");
+ while ($tok) {
+ //Extract the multianswer_id and the answer
+ $exploded = explode("-",$tok);
+ $multianswer_id = $exploded[0];
+ $answer = $exploded[1];
+ //Get the multianswer from backup_ids
+ $mul = backup_getid($restore->backup_unique_code,"quiz_multianswers",$multianswer_id);
+ if ($mul) {
+ //Now, depending of the answertype field in quiz_multianswers
+ //we do diferent things
+ $mul_db = get_record ("quiz_multianswers","id",$mul->new_id);
+ if ($mul_db->answertype == "1") {
+ //Shortanswer
+ //The answer is text, do nothing
+ } else if ($mul_db->answertype == "3") {
+ //Multichoice
+ //The answer is an answer_id, look for it in backup_ids
+ $ans = backup_getid($restore->backup_unique_code,"quiz_answers",$answer);
+ $answer = $ans->new_id;
+ } else if ($mul_db->answertype == "8") {
+ //Numeric
+ //The answer is text, do nothing
+ }
+
+ //Finaly, build the new answer field for each pair
+ if ($in_first) {
+ $answer_field .= $mul->new_id."-".$answer;
+ $in_first = false;
+ } else {
+ $answer_field .= ",".$mul->new_id."-".$answer;
+ }
+ }
+ //check for next
+ $tok = strtok(",");
+ }
+ $response->answer = $answer_field;
+ break;
default: //UNMATCHED QTYPE.
//This is an error (unimplemented qtype)
$status = false;
Please sign in to comment.
Something went wrong with that request. Please try again.