From bb7e09a453d7641c08302eaef4ddc012324ef761 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Wed, 6 Jul 2005 09:05:17 +0000 Subject: [PATCH] Latest changes merged from MOODLE_15_STABLE Moving the MERGED tag for the whole quiz directory as talked with Gustav... --- mod/quiz/backuplib.php | 41 +++++---- mod/quiz/restorelib.php | 186 +++++++++++++++++++++++++++++++++------- 2 files changed, 176 insertions(+), 51 deletions(-) diff --git a/mod/quiz/backuplib.php b/mod/quiz/backuplib.php index fa4afbcf1c5..096edf3a8c1 100644 --- a/mod/quiz/backuplib.php +++ b/mod/quiz/backuplib.php @@ -121,7 +121,7 @@ g.question = t.id",false); //Now, foreach detected category, we look for their parents upto 0 (top category) - $categories = get_records_sql("SELECT old_id, old_id + $categories = get_records_sql("SELECT old_id, old_id FROM {$CFG->prefix}backup_ids WHERE backup_code = $backup_unique_code AND table_name = 'quiz_categories'"); @@ -190,7 +190,7 @@ } } } - + //Delete category ids from backup_ids table function delete_category_ids ($backup_unique_code) { global $CFG; @@ -459,7 +459,7 @@ foreach ($numericals as $numerical) { $status =fwrite ($bf,start_tag("NUMERICAL",$level,true)); //Print numerical contents - fwrite ($bf,full_tag("ANSWERS",$level+1,false,$numerical->answers)); + fwrite ($bf,full_tag("ANSWER",$level+1,false,$numerical->answer)); fwrite ($bf,full_tag("TOLERANCE",$level+1,false,$numerical->tolerance)); //Now backup numerical_units $status = quiz_backup_numerical_units($bf,$preferences,$question,7); @@ -491,21 +491,8 @@ $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); - } - + fwrite ($bf,full_tag("QUESTION",8,false,$multianswer->question)); + fwrite ($bf,full_tag("SEQUENCE",8,false,$multianswer->sequence)); $status =fwrite ($bf,end_tag("MULTIANSWER",7,true)); } //Print multianswers footer @@ -1011,6 +998,24 @@ return $info; } + //Return a content encoded to support interactivities linking. Every module + //should have its own. They are called automatically from the backup procedure. + function quiz_encode_content_links ($content,$preferences) { + + global $CFG; + + $base = preg_quote($CFG->wwwroot,"/"); + + //Link to the list of quizs + $buscar="/(".$base."\/mod\/quiz\/index.php\?id\=)([0-9]+)/"; + $result= preg_replace($buscar,'$@QUIZINDEX*$2@$',$content); + + //Link to quiz view by moduleid + $buscar="/(".$base."\/mod\/quiz\/view.php\?id\=)([0-9]+)/"; + $result= preg_replace($buscar,'$@QUIZVIEWBYID*$2@$',$result); + + return $result; + } // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE diff --git a/mod/quiz/restorelib.php b/mod/quiz/restorelib.php index 80e93ec2c41..df0764da0e6 100644 --- a/mod/quiz/restorelib.php +++ b/mod/quiz/restorelib.php @@ -4,9 +4,9 @@ // Todo: // the restoration of the parent and sortorder fields in the category table needs - // a lot more thought. We should probably use a library function to add the category + // a lot more thought. We should probably use a library function to add the category // rather than just writing it to the database - + // whereever it says "/// We have to recode the .... field" we should put in a check // to see if the recoding was successful and throw an appropriate error otherwise @@ -135,7 +135,7 @@ $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_quiz_category($quiz_cat, $restore->course)) { + if ($catfound = restore_get_best_quiz_category($quiz_cat, $restore->course)) { $newid = $catfound; } else { if (!$quiz_cat->stamp) { @@ -175,6 +175,7 @@ global $CFG; $status = true; + $restored_questions = array(); //Get the questions array $questions = $info['QUESTION_CATEGORY']['#']['QUESTIONS']['0']['#']['QUESTION']; @@ -206,7 +207,7 @@ ////We have to recode the parent field // This should work alright because we ordered the questions appropriately during backup so that - // questions that can be parents are restored first + // questions that can be parents are restored first if ($question->parent and $parent = backup_getid($restore->backup_unique_code,"quiz_questions",$question->parent)) { $question->parent = $parent->new_id; } @@ -227,22 +228,32 @@ $creatingnewquestion = true; } - //Do some output - if (($i+1) % 2 == 0) { - 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,"quiz_questions",$oldid, $newid); } + + $restored_questions[$i] = new stdClass; + $restored_questions[$i]->newid = $newid; + $restored_questions[$i]->oldid = $oldid; + $restored_questions[$i]->qtype = $question->qtype; + $restored_questions[$i]->is_new = $creatingnewquestion; + } + + // Loop again, now all the question id mappings exist, so everything can + // be restored. + for($i = 0; $i < sizeof($questions); $i++) { + $que_info = $questions[$i]; + + $newid = $restored_questions[$i]->newid; + $oldid = $restored_questions[$i]->oldid; + $question->qtype = $restored_questions[$i]->qtype; + + //If it's a new question in the DB, restore it - if ($creatingnewquestion) { + if ($restored_questions[$i]->is_new) { //Now, restore every quiz_answers in this question $status = quiz_restore_answers($oldid,$newid,$que_info,$restore); //Now, depending of the type of questions, invoke different functions @@ -268,8 +279,7 @@ $status = quiz_restore_calculated($oldid,$newid,$que_info,$restore); } else if ($question->qtype == "11") { $status = quiz_restore_rqp($oldid,$newid,$que_info,$restore); - } - + } } else { //We are NOT creating the question, but we need to know every quiz_answers //map between the XML file and the database to be able to restore the states @@ -300,6 +310,15 @@ //Calculated question. Nothing to remap } } + + //Do some output + if (($i+1) % 2 == 0) { + echo "."; + if (($i+1) % 40 == 0) { + echo "
"; + } + backup_flush(300); + } } return $status; } @@ -836,13 +855,13 @@ //Now, build the QUIZ_NUMERICAL record structure $numerical->question = $new_question_id; - $numerical->answers = backup_todb($num_info['#']['ANSWERS']['0']['#']); + $numerical->answer = backup_todb($num_info['#']['ANSWER']['0']['#']); $numerical->tolerance = backup_todb($num_info['#']['TOLERANCE']['0']['#']); ////We have to recode the answer field - $answer = backup_getid($restore->backup_unique_code,"quiz_answers",$numerical->answers); + $answer = backup_getid($restore->backup_unique_code,"quiz_answers",$numerical->answer); if ($answer) { - $numerical->answers = $answer->new_id; + $numerical->answer = $answer->new_id; } //The structure is equal to the db, so insert the quiz_numerical @@ -946,33 +965,29 @@ //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']['#']); + $multianswer->sequence = backup_todb($mul_info['#']['SEQUENCE']['0']['#']); - //We have to recode the answers field (a list of answers id) - //Extracts answer id from sequence - $answers_field = ""; + //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->answers,","); + $tok = strtok($multianswer->sequence,","); while ($tok) { //Get the answer from backup_ids - $answer = backup_getid($restore->backup_unique_code,"quiz_answers",$tok); - if ($answer) { + $question = backup_getid($restore->backup_unique_code,"quiz_questions",$tok); + if ($question) { if ($in_first) { - $answers_field .= $answer->new_id; + $sequence_field .= $question->new_id; $in_first = false; } else { - $answers_field .= ",".$answer->new_id; + $sequence_field .= ",".$question->new_id; } } //check for next $tok = strtok(","); } //We have the answers field recoded to its new ids - $multianswer->answers = $answers_field; - + $multianswer->sequence = $sequence_field; //The structure is equal to the db, so insert the quiz_multianswers $newid = insert_record ("quiz_multianswers",$multianswer); @@ -990,7 +1005,7 @@ } 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) { @@ -1004,6 +1019,7 @@ } else { $status = false; } +*/ } return $status; @@ -1848,6 +1864,110 @@ return $status; } + //Return a content decoded to support interactivities linking. Every module + //should have its own. They are called automatically from + //quiz_decode_content_links_caller() function in each module + //in the restore process + function quiz_decode_content_links ($content,$restore) { + + global $CFG; + + $result = $content; + + //Link to the list of quizs + + $searchstring='/\$@(QUIZINDEX)\*([0-9]+)@\$/'; + //We look for it + preg_match_all($searchstring,$content,$foundset); + //If found, then we are going to look for its new id (in backup tables) + if ($foundset[0]) { + //print_object($foundset); //Debug + //Iterate over foundset[2]. They are the old_ids + foreach($foundset[2] as $old_id) { + //We get the needed variables here (course id) + $rec = backup_getid($restore->backup_unique_code,"course",$old_id); + //Personalize the searchstring + $searchstring='/\$@(QUIZINDEX)\*('.$old_id.')@\$/'; + //If it is a link to this course, update the link to its new location + if($rec->new_id) { + //Now replace it + $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/quiz/index.php?id='.$rec->new_id,$result); + } else { + //It's a foreign link so leave it as original + $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/quiz/index.php?id='.$old_id,$result); + } + } + } + + //Link to quiz view by moduleid + + $searchstring='/\$@(QUIZVIEWBYID)\*([0-9]+)@\$/'; + //We look for it + preg_match_all($searchstring,$result,$foundset); + //If found, then we are going to look for its new id (in backup tables) + if ($foundset[0]) { + //print_object($foundset); //Debug + //Iterate over foundset[2]. They are the old_ids + foreach($foundset[2] as $old_id) { + //We get the needed variables here (course_modules id) + $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id); + //Personalize the searchstring + $searchstring='/\$@(QUIZVIEWBYID)\*('.$old_id.')@\$/'; + //If it is a link to this course, update the link to its new location + if($rec->new_id) { + //Now replace it + $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/quiz/view.php?id='.$rec->new_id,$result); + } else { + //It's a foreign link so leave it as original + $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/quiz/view.php?id='.$old_id,$result); + } + } + } + + return $result; + } + + //This function makes all the necessary calls to xxxx_decode_content_links() + //function in each module, passing them the desired contents to be decoded + //from backup format to destination site/course in order to mantain inter-activities + //working in the backup/restore process. It's called from restore_decode_content_links() + //function in restore process + function quiz_decode_content_links_caller($restore) { + global $CFG; + $status = true; + + if ($quizs = get_records_sql ("SELECT q.id, q.intro + FROM {$CFG->prefix}quiz q + WHERE q.course = $restore->course_id")) { + //Iterate over each quiz->intro + $i = 0; //Counter to send some output to the browser to avoid timeouts + foreach ($quizs as $quiz) { + //Increment counter + $i++; + $content = $quiz->intro; + $result = restore_decode_content_links_worker($content,$restore); + if ($result != $content) { + //Update record + $quiz->intro = addslashes($result); + $status = update_record("quiz",$quiz); + if ($CFG->debug>7) { + echo "

".$content."
changed to
".$result."

"; + } + } + //Do some output + if (($i+1) % 5 == 0) { + echo "."; + if (($i+1) % 100 == 0) { + echo "
"; + } + backup_flush(300); + } + } + } + + return $status; + } + //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for //some texts in the module function quiz_restore_wiki2markdown ($restore) {