Skip to content

Commit

Permalink
Backup quizzes COMPLETED. Two setps finished.
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jun 10, 2003
1 parent c3add1a commit 290a1b4
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 8 deletions.
9 changes: 4 additions & 5 deletions backup/STATUS.txt
Expand Up @@ -4,8 +4,7 @@ STATUS $Id$
This documment shows the current status of the backup/restore option. This documment shows the current status of the backup/restore option.


Backup: WORKING. COMPLETED but only assignments, resources, Backup: WORKING. COMPLETED but only assignments, resources,
choices, forums, journals, quizzes (step 1) choices, forums, journals, quizzes and surveys.
and surveys.


Restore: WORKING. COMPLETED but only assignments, resources, Restore: WORKING. COMPLETED but only assignments, resources,
choices and surveys. choices and surveys.
Expand Down Expand Up @@ -51,9 +50,9 @@ Backup Details:
- Course.......................................DONE - Course.......................................DONE
- All..........................................DONE - All..........................................DONE
- Logs...............................................DONE - Logs...............................................DONE
- Categories and Questions.(STEP 1)..................IN PROGRESS - Categories and Questions.(STEP 1)..................DONE
+ categories......................................IN PROGRESS + categories......................................DONE
+ questions structure.............................IN PROGRESS + questions structure.............................DONE
- Mods Info..........................................IN PROGRESS - Mods Info..........................................IN PROGRESS
x assignments.....................................DONE x assignments.....................................DONE
+ course data..................................DONE + course data..................................DONE
Expand Down
7 changes: 7 additions & 0 deletions backup/backup_execute.html
Expand Up @@ -170,6 +170,13 @@
if ($status) { if ($status) {
$status = backup_user_info($backup_file,$preferences); $status = backup_user_info($backup_file,$preferences);
} }

//If we have selected to backup quizzes, backup categories and
//questions structure (step 1). See notes on mod/quiz/backuplib.php
if ($status and $preferences->mods[quiz]->backup) {
echo "<li>Writing categories and questions";
$status = quiz_backup_question_categories($backup_file,$preferences);
}


//Print logs if selected //Print logs if selected
if ($status) { if ($status) {
Expand Down
241 changes: 238 additions & 3 deletions backup/mod/quiz/backuplib.php
Expand Up @@ -77,8 +77,241 @@
// (course independent) // (course independent)
function quiz_backup_question_categories($bf,$preferences) { function quiz_backup_question_categories($bf,$preferences) {


global $CFG;

$status = true;

//First, we get the used categories from backup_ids
$categories = quiz_category_ids_by_backup ($preferences->backup_unique_code);

//If we've categories
if ($categories) {
//Write start tag
$status = fwrite($bf,start_tag("QUESTION_CATEGORIES",2,true));
//Iterate over each category
foreach ($categories as $cat) {
//Start category
$status =fwrite ($bf,start_tag("QUESTION_CATEGORY",3,true));
//Get category data from quiz_categories
$category = get_record ("quiz_categories","id",$cat->old_id);
//Print category contents
fwrite($bf,full_tag("ID",4,false,$category->id));
fwrite($bf,full_tag("NAME",4,false,$category->name));
fwrite($bf,full_tag("INFO",4,false,$category->info));
fwrite($bf,full_tag("PUBLISH",4,false,$category->publish));
//Now, backup their questions
$status = quiz_backup_question($bf,$preferences,$category->id);
//End category
$status =fwrite ($bf,end_tag("QUESTION_CATEGORY",3,true));
}
//Write end tag
$status =fwrite ($bf,end_tag("QUESTION_CATEGORIES",2,true));
}

return $status;
}

//This function backups all the questions in selected category and their
//asociated data
function quiz_backup_question($bf,$preferences,$category) {

global $CFG;

$status = true;

$questions = get_records("quiz_questions","category",$category,"id");
//If there is questions
if ($questions) {
//Write start tag
$status =fwrite ($bf,start_tag("QUESTIONS",4,true));
//Iterate over each question
foreach ($questions as $question) {
//Start question
$status =fwrite ($bf,start_tag("QUESTION",5,true));
//Print question contents
fwrite ($bf,full_tag("ID",6,false,$question->id));
fwrite ($bf,full_tag("NAME",6,false,$question->name));
fwrite ($bf,full_tag("QUESTIONTEXT",6,false,$question->questiontext));
fwrite ($bf,full_tag("IMAGE",6,false,$question->image));
fwrite ($bf,full_tag("DEFAULTGRADE",6,false,$question->defaultgrade));
fwrite ($bf,full_tag("QTYPE",6,false,$question->qtype));
//Now, depending of the qtype, call one function or other
if ($question->qtype == "1") {
$status = quiz_backup_shortanswer($bf,$preferences,$question->id);
} else if ($question->qtype == "2") {
$status = quiz_backup_truefalse($bf,$preferences,$question->id);
} else if ($question->qtype == "3") {
$status = quiz_backup_multichoice($bf,$preferences,$question->id);
} else if ($question->qtype == "4") {
//Random question. Nothing to write.
} else if ($question->qtype == "5") {
$status = quiz_backup_match($bf,$preferences,$question->id);
} else if ($question->qtype == "6") {
$status = quiz_backup_randomsamatch($bf,$preferences,$question->id);
}
//End question
$status =fwrite ($bf,end_tag("QUESTION",5,true));
}
//Write end tag
$status =fwrite ($bf,end_tag("QUESTIONS",4,true));
}
return $status;
}

//This function backups the data in a truefalse question (qtype=2) and its
//asociated data
function quiz_backup_truefalse($bf,$preferences,$question) {

global $CFG;

$status = true;

$truefalses = get_records("quiz_truefalse","question",$question,"id");
//If there are truefalses
if ($truefalses) {
//Iterate over each truefalse
foreach ($truefalses as $truefalse) {
$status =fwrite ($bf,start_tag("TRUEFALSE",6,true));
//Print truefalse contents
fwrite ($bf,full_tag("TRUEANSWER",7,false,$truefalse->trueanswer));
fwrite ($bf,full_tag("FALSEANSWER",7,false,$truefalse->falseanswer));
$status =fwrite ($bf,end_tag("TRUEFALSE",6,true));
}
//Now print quiz_answers
$status = quiz_backup_answers($bf,$preferences,$question);
}
return $status;
} }


//This function backups the data in a shortanswer question (qtype=1) and its
//asociated data
function quiz_backup_shortanswer($bf,$preferences,$question) {

global $CFG;

$status = true;

$shortanswers = get_records("quiz_shortanswer","question",$question,"id");
//If there are shortanswers
if ($shortanswers) {
//Iterate over each shortanswer
foreach ($shortanswers as $shortanswer) {
$status =fwrite ($bf,start_tag("SHORTANSWER",6,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));
}
//Now print quiz_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) {

global $CFG;

$status = true;

$multichoices = get_records("quiz_multichoice","question",$question,"id");
//If there are multichoices
if ($multichoices) {
//Iterate over each multichoice
foreach ($multichoices as $multichoice) {
$status =fwrite ($bf,start_tag("MULTICHOICE",6,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));
}
//Now print quiz_answers
$status = quiz_backup_answers($bf,$preferences,$question);
}
return $status;
}

//This function backups the data in a randomsamatch question (qtype=6) and its
//asociated data
function quiz_backup_randomsamatch($bf,$preferences,$question) {

global $CFG;

$status = true;

$randomsamatchs = get_records("quiz_randomsamatch","question",$question,"id");
//If there are randomsamatchs
if ($randomsamatchs) {
//Iterate over each randomsamatch
foreach ($randomsamatchs as $randomsamatch) {
$status =fwrite ($bf,start_tag("RANDOMSAMATCH",6,true));
//Print randomsamatch contents
fwrite ($bf,full_tag("CHOOSE",7,false,$randomsamatch->choose));
$status =fwrite ($bf,end_tag("RANDOMSAMATCH",6,true));
}
}
return $status;
}

//This function backups the data in a match question (qtype=5) and its
//asociated data
function quiz_backup_match($bf,$preferences,$question) {

global $CFG;

$status = true;

$matchs = get_records("quiz_match_sub","question",$question,"id");
//If there are matchs
if ($matchs) {
$status =fwrite ($bf,start_tag("MATCHS",6,true));
//Iterate over each match
foreach ($matchs as $match) {
$status =fwrite ($bf,start_tag("MATCH",7,true));
//Print match contents
fwrite ($bf,full_tag("QUESTIONTEXT",8,false,$match->questiontext));
fwrite ($bf,full_tag("ANSWERTEXT",8,false,$match->answertext));
$status =fwrite ($bf,end_tag("MATCH",7,true));
}
$status =fwrite ($bf,end_tag("MATCHS",6,true));
}
return $status;
}

//This function backups the answers data in some question types
//(truefalse, shortanswer,multichoice)
function quiz_backup_answers($bf,$preferences,$question) {

global $CFG;

$status = true;

$answers = get_records("quiz_answers","question",$question,"id");
//If there are answers
if ($answers) {
$status =fwrite ($bf,start_tag("ANSWERS",6,true));
//Iterate over each answer
foreach ($answers as $answer) {
$status =fwrite ($bf,start_tag("ANSWER",7,true));
//Print answer contents
fwrite ($bf,full_tag("ID",8,false,$answer->id));
fwrite ($bf,full_tag("ANSWER_TEXT",8,false,$answer->answer));
fwrite ($bf,full_tag("FRACTION",8,false,$answer->fraction));
fwrite ($bf,full_tag("FEEDBACK",8,false,$answer->feedback));
$status =fwrite ($bf,end_tag("ANSWER",7,true));
}
$status =fwrite ($bf,end_tag("ANSWERS",6,true));
}
return $status;
}





//STEP 2. Backup quizzes and associated structures //STEP 2. Backup quizzes and associated structures
// (course dependent) // (course dependent)
function quiz_backup_mods($bf,$preferences) { function quiz_backup_mods($bf,$preferences) {
Expand Down Expand Up @@ -325,7 +558,8 @@ function quiz_category_ids_by_backup ($backup_unique_code) {


return get_records_sql ("SELECT a.old_id, a.backup_code return get_records_sql ("SELECT a.old_id, a.backup_code
FROM {$CFG->prefix}backup_ids a FROM {$CFG->prefix}backup_ids a
WHERE a.backup_code = '$backup_unique_code'"); WHERE a.backup_code = '$backup_unique_code' AND
a.table_name = 'quiz_categories'");
} }


function quiz_question_ids_by_backup ($backup_unique_code) { function quiz_question_ids_by_backup ($backup_unique_code) {
Expand All @@ -335,8 +569,9 @@ function quiz_question_ids_by_backup ($backup_unique_code) {
return get_records_sql ("SELECT q.id, q.category return get_records_sql ("SELECT q.id, q.category
FROM {$CFG->prefix}backup_ids a, FROM {$CFG->prefix}backup_ids a,
{$CFG->prefix}quiz_questions q {$CFG->prefix}quiz_questions q
WHERE a.backup_code = '$backup_unique_code' and WHERE a.backup_code = '$backup_unique_code' AND
q.category = a.old_id"); q.category = a.old_id AND
a.table_name = 'quiz_categories'");
} }


function quiz_grade_ids_by_course ($course) { function quiz_grade_ids_by_course ($course) {
Expand Down

0 comments on commit 290a1b4

Please sign in to comment.