[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/mod/quiz/ -> restorelib.php (source)

   1  <?php // $Id: restorelib.php,v 1.90.6.2 2007/12/21 15:38:38 tjhunt Exp $
   2      //This php script contains all the stuff to restore quiz mods
   3  
   4  // Todo:
   5  
   6      // whereever it says "/// We have to recode the .... field" we should put in a check
   7      // to see if the recoding was successful and throw an appropriate error otherwise
   8  
   9  //This is the "graphical" structure of the quiz mod:
  10      //To see, put your terminal to 160cc
  11  
  12      //
  13      //                           quiz
  14      //                        (CL,pk->id)
  15      //                            |
  16      //           -------------------------------------------------------------------
  17      //           |                    |                        |                    |
  18      //           |               quiz_grades                   |        quiz_question_versions
  19      //           |           (UL,pk->id,fk->quiz)              |         (CL,pk->id,fk->quiz)
  20      //           |                                             |
  21      //      quiz_attempts                          quiz_question_instances
  22      //  (UL,pk->id,fk->quiz)                    (CL,pk->id,fk->quiz,question)
  23      //
  24      // Meaning: pk->primary key field of the table
  25      //          fk->foreign key to link with parent
  26      //          nt->nested field (recursive data)
  27      //          SL->site level info
  28      //          CL->course level info
  29      //          UL->user level info
  30      //          files->table may have files
  31      //
  32      //-----------------------------------------------------------
  33  
  34      // When we restore a quiz we also need to restore the questions and possibly
  35      // the data about student interaction with the questions. The functions to do
  36      // that are included with the following library
  37      include_once("$CFG->dirroot/question/restorelib.php");
  38  
  39      function quiz_restore_mods($mod,$restore) {
  40  
  41          global $CFG;
  42  
  43          $status = true;
  44  
  45          //Hook to call Moodle < 1.5 Quiz Restore
  46          if ($restore->backup_version < 2005043000) {
  47              include_once ("restorelibpre15.php");
  48              return quiz_restore_pre15_mods($mod,$restore);
  49          }
  50  
  51          //Get record from backup_ids
  52          $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
  53  
  54          if ($data) {
  55              //Now get completed xmlized object
  56              $info = $data->info;
  57              //if necessary, write to restorelog and adjust date/time fields
  58              if ($restore->course_startdateoffset) {
  59                  restore_log_date_changes('Quiz', $restore, $info['MOD']['#'], array('TIMEOPEN', 'TIMECLOSE'));
  60              }            
  61              //traverse_xmlize($info);                                                                     //Debug
  62              //print_object ($GLOBALS['traverse_array']);                                                  //Debug
  63              //$GLOBALS['traverse_array']="";                                                              //Debug
  64  
  65              //Now, build the QUIZ record structure
  66              $quiz = new stdClass;
  67              $quiz->course = $restore->course_id;
  68              $quiz->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
  69              $quiz->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']); 
  70              $quiz->timeopen = backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
  71              $quiz->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
  72              $quiz->optionflags = backup_todb($info['MOD']['#']['OPTIONFLAGS']['0']['#']);
  73              $quiz->penaltyscheme = backup_todb($info['MOD']['#']['PENALTYSCHEME']['0']['#']);
  74              $quiz->attempts = backup_todb($info['MOD']['#']['ATTEMPTS_NUMBER']['0']['#']);
  75              $quiz->attemptonlast = backup_todb($info['MOD']['#']['ATTEMPTONLAST']['0']['#']);
  76              $quiz->grademethod = backup_todb($info['MOD']['#']['GRADEMETHOD']['0']['#']);
  77              $quiz->decimalpoints = backup_todb($info['MOD']['#']['DECIMALPOINTS']['0']['#']);
  78              $quiz->review = backup_todb($info['MOD']['#']['REVIEW']['0']['#']);
  79              $quiz->questionsperpage = backup_todb($info['MOD']['#']['QUESTIONSPERPAGE']['0']['#']);
  80              $quiz->shufflequestions = backup_todb($info['MOD']['#']['SHUFFLEQUESTIONS']['0']['#']);
  81              $quiz->shuffleanswers = backup_todb($info['MOD']['#']['SHUFFLEANSWERS']['0']['#']);
  82              $quiz->questions = backup_todb($info['MOD']['#']['QUESTIONS']['0']['#']);
  83              $quiz->sumgrades = backup_todb($info['MOD']['#']['SUMGRADES']['0']['#']);
  84              $quiz->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
  85              $quiz->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
  86              $quiz->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
  87              $quiz->timelimit = backup_todb($info['MOD']['#']['TIMELIMIT']['0']['#']);
  88              $quiz->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
  89              $quiz->subnet = backup_todb($info['MOD']['#']['SUBNET']['0']['#']);
  90              $quiz->popup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
  91              $quiz->delay1 = isset($info['MOD']['#']['DELAY1']['0']['#'])?backup_todb($info['MOD']['#']['DELAY1']['0']['#']):'';
  92              $quiz->delay2 = isset($info['MOD']['#']['DELAY2']['0']['#'])?backup_todb($info['MOD']['#']['DELAY2']['0']['#']):'';
  93              //We have to recode the questions field (a list of questions id and pagebreaks)
  94              $quiz->questions = quiz_recode_layout($quiz->questions, $restore);
  95  
  96              //The structure is equal to the db, so insert the quiz
  97              $newid = insert_record ("quiz",$quiz);
  98  
  99              //Do some output
 100              if (!defined('RESTORE_SILENTLY')) {
 101                  echo "<li>".get_string("modulename","quiz")." \"".format_string(stripslashes($quiz->name),true)."\"</li>";
 102              }
 103              backup_flush(300);
 104  
 105              if ($newid) {
 106                  //We have the newid, update backup_ids
 107                  backup_putid($restore->backup_unique_code,$mod->modtype,
 108                               $mod->id, $newid);
 109                  //We have to restore the question_instances now (course level table)
 110                  $status = quiz_question_instances_restore_mods($newid,$info,$restore);
 111                  //We have to restore the feedback now (course level table)
 112                  $status = quiz_feedback_restore_mods($newid, $info, $restore, $quiz);
 113                  //We have to restore the question_versions now (course level table)
 114                  $status = quiz_question_versions_restore_mods($newid,$info,$restore);
 115                  //Now check if want to restore user data and do it.
 116                  if (restore_userdata_selected($restore,'quiz',$mod->id)) {
 117                      //Restore quiz_attempts
 118                      $status = quiz_attempts_restore_mods ($newid,$info,$restore);
 119                      if ($status) {
 120                          //Restore quiz_grades
 121                          $status = quiz_grades_restore_mods ($newid,$info,$restore);
 122                      }
 123                  }
 124              } else {
 125                  $status = false;
 126              }
 127          } else {
 128              $status = false;
 129          }
 130  
 131          return $status;
 132      }
 133  
 134      //This function restores the quiz_question_instances
 135      function quiz_question_instances_restore_mods($quiz_id,$info,$restore) {
 136  
 137          global $CFG;
 138  
 139          $status = true;
 140  
 141          //Get the quiz_question_instances array
 142          if (array_key_exists('QUESTION_INSTANCES', $info['MOD']['#'])) {
 143              $instances = $info['MOD']['#']['QUESTION_INSTANCES']['0']['#']['QUESTION_INSTANCE'];
 144          } else {
 145              $instances = array();
 146          }
 147  
 148          //Iterate over question_instances
 149          for($i = 0; $i < sizeof($instances); $i++) {
 150              $gra_info = $instances[$i];
 151              //traverse_xmlize($gra_info);                                                                 //Debug
 152              //print_object ($GLOBALS['traverse_array']);                                                  //Debug
 153              //$GLOBALS['traverse_array']="";                                                              //Debug
 154  
 155              //We'll need this later!!
 156              $oldid = backup_todb($gra_info['#']['ID']['0']['#']);
 157  
 158              //Now, build the QUESTION_INSTANCES record structure
 159              $instance = new stdClass;
 160              $instance->quiz = $quiz_id;
 161              $instance->question = backup_todb($gra_info['#']['QUESTION']['0']['#']);
 162              $instance->grade = backup_todb($gra_info['#']['GRADE']['0']['#']);
 163  
 164              //We have to recode the question field
 165              $question = backup_getid($restore->backup_unique_code,"question",$instance->question);
 166              if ($question) {
 167                  $instance->question = $question->new_id;
 168              }
 169  
 170              //The structure is equal to the db, so insert the quiz_question_instances
 171              $newid = insert_record ("quiz_question_instances",$instance);
 172  
 173              //Do some output
 174              if (($i+1) % 10 == 0) {
 175                  if (!defined('RESTORE_SILENTLY')) {
 176                      echo ".";
 177                      if (($i+1) % 200 == 0) {
 178                          echo "<br />";
 179                      }
 180                  }
 181                  backup_flush(300);
 182              }
 183  
 184              if ($newid) {
 185                  //We have the newid, update backup_ids
 186                  backup_putid($restore->backup_unique_code,"quiz_question_instances",$oldid,
 187                               $newid);
 188              } else {
 189                  $status = false;
 190              }
 191          }
 192  
 193          return $status;
 194      }
 195  
 196      //This function restores the quiz_question_instances
 197      function quiz_feedback_restore_mods($quiz_id, $info, $restore, $quiz) {
 198          $status = true;
 199  
 200          //Get the quiz_feedback array
 201          if (array_key_exists('FEEDBACKS', $info['MOD']['#'])) {
 202              $feedbacks = $info['MOD']['#']['FEEDBACKS']['0']['#']['FEEDBACK'];
 203  
 204              //Iterate over the feedbacks
 205              foreach ($feedbacks as $feedback_info) {
 206                  //traverse_xmlize($feedback_info);                                                            //Debug
 207                  //print_object ($GLOBALS['traverse_array']);                                                  //Debug
 208                  //$GLOBALS['traverse_array']="";                                                              //Debug
 209      
 210                  //We'll need this later!!
 211                  $oldid = backup_todb($feedback_info['#']['ID']['0']['#']);
 212      
 213                  //Now, build the quiz_feedback record structure
 214                  $feedback = new stdClass();
 215                  $feedback->quizid = $quiz_id;
 216                  $feedback->feedbacktext = backup_todb($feedback_info['#']['FEEDBACKTEXT']['0']['#']);
 217                  $feedback->mingrade = backup_todb($feedback_info['#']['MINGRADE']['0']['#']);
 218                  $feedback->maxgrade = backup_todb($feedback_info['#']['MAXGRADE']['0']['#']);
 219      
 220                  //The structure is equal to the db, so insert the quiz_question_instances
 221                  $newid = insert_record('quiz_feedback', $feedback);
 222      
 223                  if ($newid) {
 224                      //We have the newid, update backup_ids
 225                      backup_putid($restore->backup_unique_code, 'quiz_feedback', $oldid, $newid);
 226                  } else {
 227                      $status = false;
 228                  }
 229              }
 230          } else {
 231              $feedback = new stdClass();
 232              $feedback->quizid = $quiz_id;
 233              $feedback->feedbacktext = '';
 234              $feedback->mingrade = 0;
 235              $feedback->maxgrade = $quiz->grade + 1;
 236              insert_record('quiz_feedback', $feedback);
 237          }
 238  
 239          return $status;
 240      }
 241  
 242      //This function restores the quiz_question_versions
 243      function quiz_question_versions_restore_mods($quiz_id,$info,$restore) {
 244  
 245          global $CFG, $USER;
 246  
 247          $status = true;
 248  
 249          //Get the quiz_question_versions array
 250          if (!empty($info['MOD']['#']['QUESTION_VERSIONS'])) {
 251              $versions = $info['MOD']['#']['QUESTION_VERSIONS']['0']['#']['QUESTION_VERSION'];
 252          } else {
 253              $versions = array();
 254          }
 255          
 256          //Iterate over question_versions
 257          for($i = 0; $i < sizeof($versions); $i++) {
 258              $ver_info = $versions[$i];
 259              //traverse_xmlize($ver_info);                                                                 //Debug
 260              //print_object ($GLOBALS['traverse_array']);                                                  //Debug
 261              //$GLOBALS['traverse_array']="";                                                              //Debug
 262  
 263              //We'll need this later!!
 264              $oldid = backup_todb($ver_info['#']['ID']['0']['#']);
 265  
 266              //Now, build the QUESTION_VERSIONS record structure
 267              $version = new stdClass;
 268              $version->quiz = $quiz_id;
 269              $version->oldquestion = backup_todb($ver_info['#']['OLDQUESTION']['0']['#']);
 270              $version->newquestion = backup_todb($ver_info['#']['NEWQUESTION']['0']['#']);
 271              $version->originalquestion = backup_todb($ver_info['#']['ORIGINALQUESTION']['0']['#']);
 272              $version->userid = backup_todb($ver_info['#']['USERID']['0']['#']);
 273              $version->timestamp = backup_todb($ver_info['#']['TIMESTAMP']['0']['#']);
 274  
 275              //We have to recode the oldquestion field
 276              $question = backup_getid($restore->backup_unique_code,"question",$version->oldquestion);
 277              if ($question) {
 278                  $version->oldquestion = $question->new_id;
 279              }
 280  
 281              //We have to recode the newquestion field
 282              $question = backup_getid($restore->backup_unique_code,"question",$version->newquestion);
 283              if ($question) {
 284                  $version->newquestion = $question->new_id;
 285              }
 286  
 287              //We have to recode the originalquestion field
 288              $question = backup_getid($restore->backup_unique_code,"question",$version->originalquestion);
 289              if ($question) {
 290                  $version->newquestion = $question->new_id;
 291              }
 292  
 293              //We have to recode the userid field
 294              $user = backup_getid($restore->backup_unique_code,"user",$version->userid);
 295              if ($user) {
 296                  $version->userid = $user->new_id;
 297              } else {  //Assign to current user
 298                  $version->userid = $USER->id;
 299              }
 300  
 301              //The structure is equal to the db, so insert the quiz_question_versions
 302              $newid = insert_record ("quiz_question_versions",$version);
 303  
 304              //Do some output
 305              if (($i+1) % 10 == 0) {
 306                  if (!defined('RESTORE_SILENTLY')) {
 307                      echo ".";
 308                      if (($i+1) % 200 == 0) {
 309                          echo "<br />";
 310                      }
 311                  }
 312                  backup_flush(300);
 313              }
 314  
 315              if ($newid) {
 316                  //We have the newid, update backup_ids
 317                  backup_putid($restore->backup_unique_code,"quiz_question_versions",$oldid,
 318                               $newid);
 319              } else {
 320                  $status = false;
 321              }
 322          }
 323  
 324          return $status;
 325      }
 326  
 327      //This function restores the quiz_attempts
 328      function quiz_attempts_restore_mods($quiz_id,$info,$restore) {
 329  
 330          global $CFG;
 331  
 332          $status = true;
 333  
 334          //Get the quiz_attempts array
 335          if (array_key_exists('ATTEMPTS', $info['MOD']['#'])) {
 336              $attempts = $info['MOD']['#']['ATTEMPTS']['0']['#']['ATTEMPT'];
 337          } else {
 338              $attempts = array();
 339          }
 340  
 341          //Iterate over attempts
 342          for($i = 0; $i < sizeof($attempts); $i++) {
 343              $att_info = $attempts[$i];
 344              //traverse_xmlize($att_info);                                                                 //Debug
 345              //print_object ($GLOBALS['traverse_array']);                                                  //Debug
 346              //$GLOBALS['traverse_array']="";                                                              //Debug
 347  
 348              //We'll need this later!!
 349              $oldid = backup_todb($att_info['#']['ID']['0']['#']);
 350              $olduserid = backup_todb($att_info['#']['USERID']['0']['#']);
 351  
 352              //Now, build the ATTEMPTS record structure
 353              $attempt = new stdClass;
 354              $attempt->quiz = $quiz_id;
 355              $attempt->userid = backup_todb($att_info['#']['USERID']['0']['#']);
 356              $attempt->attempt = backup_todb($att_info['#']['ATTEMPTNUM']['0']['#']);
 357              $attempt->sumgrades = backup_todb($att_info['#']['SUMGRADES']['0']['#']);
 358              $attempt->timestart = backup_todb($att_info['#']['TIMESTART']['0']['#']);
 359              $attempt->timefinish = backup_todb($att_info['#']['TIMEFINISH']['0']['#']);
 360              $attempt->timemodified = backup_todb($att_info['#']['TIMEMODIFIED']['0']['#']);
 361              $attempt->layout = backup_todb($att_info['#']['LAYOUT']['0']['#']);
 362              $attempt->preview = backup_todb($att_info['#']['PREVIEW']['0']['#']);
 363  
 364              //We have to recode the userid field
 365              $user = backup_getid($restore->backup_unique_code,"user",$attempt->userid);
 366              if ($user) {
 367                  $attempt->userid = $user->new_id;
 368              }
 369  
 370              //Set the uniqueid field
 371              $attempt->uniqueid = question_new_attempt_uniqueid();
 372  
 373              //We have to recode the layout field (a list of questions id and pagebreaks)
 374              $attempt->layout = quiz_recode_layout($attempt->layout, $restore);
 375  
 376              //The structure is equal to the db, so insert the quiz_attempts
 377              $newid = insert_record ("quiz_attempts",$attempt);
 378  
 379              //Do some output
 380              if (($i+1) % 10 == 0) {
 381                  if (!defined('RESTORE_SILENTLY')) {
 382                      echo ".";
 383                      if (($i+1) % 200 == 0) {
 384                          echo "<br />";
 385                      }
 386                  }
 387                  backup_flush(300);
 388              }
 389  
 390              if ($newid) {
 391                  //We have the newid, update backup_ids
 392                  backup_putid($restore->backup_unique_code,"quiz_attempts",$oldid,
 393                               $newid);
 394                  //Now process question_states
 395                  // This function is defined in question/restorelib.php
 396                  $status = question_states_restore_mods($attempt->uniqueid,$att_info,$restore);
 397              } else {
 398                  $status = false;
 399              }
 400          }
 401  
 402          return $status;
 403      }
 404  
 405      //This function restores the quiz_grades
 406      function quiz_grades_restore_mods($quiz_id,$info,$restore) {
 407  
 408          global $CFG;
 409  
 410          $status = true;
 411  
 412          //Get the quiz_grades array
 413          if (array_key_exists('GRADES', $info['MOD']['#'])) {
 414              $grades = $info['MOD']['#']['GRADES']['0']['#']['GRADE'];
 415          } else {
 416              $grades = array();
 417          }
 418  
 419          //Iterate over grades
 420          for($i = 0; $i < sizeof($grades); $i++) {
 421              $gra_info = $grades[$i];
 422              //traverse_xmlize($gra_info);                                                                 //Debug
 423              //print_object ($GLOBALS['traverse_array']);                                                  //Debug
 424              //$GLOBALS['traverse_array']="";                                                              //Debug
 425  
 426              //We'll need this later!!
 427              $oldid = backup_todb($gra_info['#']['ID']['0']['#']);
 428              $olduserid = backup_todb($gra_info['#']['USERID']['0']['#']);
 429  
 430              //Now, build the GRADES record structure
 431              $grade = new stdClass;
 432              $grade->quiz = $quiz_id;
 433              $grade->userid = backup_todb($gra_info['#']['USERID']['0']['#']);
 434              $grade->grade = backup_todb($gra_info['#']['GRADEVAL']['0']['#']);
 435              $grade->timemodified = backup_todb($gra_info['#']['TIMEMODIFIED']['0']['#']);
 436  
 437              //We have to recode the userid field
 438              $user = backup_getid($restore->backup_unique_code,"user",$grade->userid);
 439              if ($user) {
 440                  $grade->userid = $user->new_id;
 441              }
 442  
 443              //The structure is equal to the db, so insert the quiz_grades
 444              $newid = insert_record ("quiz_grades",$grade);
 445  
 446              //Do some output
 447              if (($i+1) % 10 == 0) {
 448                  if (!defined('RESTORE_SILENTLY')) {
 449                      echo ".";
 450                      if (($i+1) % 200 == 0) {
 451                          echo "<br />";
 452                      }
 453                  }
 454                  backup_flush(300);
 455              }
 456  
 457              if ($newid) {
 458                  //We have the newid, update backup_ids
 459                  backup_putid($restore->backup_unique_code,"quiz_grades",$oldid,
 460                               $newid);
 461              } else {
 462                  $status = false;
 463              }
 464          }
 465  
 466          return $status;
 467      }
 468  
 469      //Return a content decoded to support interactivities linking. Every module
 470      //should have its own. They are called automatically from
 471      //quiz_decode_content_links_caller() function in each module
 472      //in the restore process
 473      function quiz_decode_content_links ($content,$restore) {
 474              
 475          global $CFG;
 476              
 477          $result = $content;
 478                  
 479          //Link to the list of quizs
 480                  
 481          $searchstring='/\$@(QUIZINDEX)\*([0-9]+)@\$/';
 482          //We look for it
 483          preg_match_all($searchstring,$content,$foundset);
 484          //If found, then we are going to look for its new id (in backup tables)
 485          if ($foundset[0]) {
 486              //print_object($foundset);                                     //Debug
 487              //Iterate over foundset[2]. They are the old_ids
 488              foreach($foundset[2] as $old_id) {
 489                  //We get the needed variables here (course id)
 490                  $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
 491                  //Personalize the searchstring
 492                  $searchstring='/\$@(QUIZINDEX)\*('.$old_id.')@\$/';
 493                  //If it is a link to this course, update the link to its new location
 494                  if($rec->new_id) {
 495                      //Now replace it
 496                      $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/quiz/index.php?id='.$rec->new_id,$result);
 497                  } else { 
 498                      //It's a foreign link so leave it as original
 499                      $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/quiz/index.php?id='.$old_id,$result);
 500                  }
 501              }
 502          }
 503  
 504          //Link to quiz view by moduleid
 505          $searchstring='/\$@(QUIZVIEWBYID)\*([0-9]+)@\$/';
 506          //We look for it
 507          preg_match_all($searchstring,$result,$foundset);
 508          //If found, then we are going to look for its new id (in backup tables)
 509          if ($foundset[0]) {
 510              //print_object($foundset);                                     //Debug
 511              //Iterate over foundset[2]. They are the old_ids
 512              foreach($foundset[2] as $old_id) {
 513                  //We get the needed variables here (course_modules id)
 514                  $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
 515                  //Personalize the searchstring
 516                  $searchstring='/\$@(QUIZVIEWBYID)\*('.$old_id.')@\$/';
 517                  //If it is a link to this course, update the link to its new location
 518                  if($rec->new_id) {
 519                      //Now replace it
 520                      $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/quiz/view.php?id='.$rec->new_id,$result);
 521                  } else {
 522                      //It's a foreign link so leave it as original
 523                      $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/quiz/view.php?id='.$old_id,$result);
 524                  }
 525              }
 526          }
 527  
 528          //Link to quiz view by quizid
 529          $searchstring='/\$@(QUIZVIEWBYQ)\*([0-9]+)@\$/';
 530          //We look for it
 531          preg_match_all($searchstring,$result,$foundset);
 532          //If found, then we are going to look for its new id (in backup tables)
 533          if ($foundset[0]) {
 534              //print_object($foundset);                                     //Debug
 535              //Iterate over foundset[2]. They are the old_ids
 536              foreach($foundset[2] as $old_id) {
 537                  //We get the needed variables here (course_modules id)
 538                  $rec = backup_getid($restore->backup_unique_code,'quiz',$old_id);
 539                  //Personalize the searchstring
 540                  $searchstring='/\$@(QUIZVIEWBYQ)\*('.$old_id.')@\$/';
 541                  //If it is a link to this course, update the link to its new location
 542                  if($rec->new_id) {
 543                      //Now replace it
 544                      $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/quiz/view.php?q='.$rec->new_id,$result);
 545                  } else {
 546                      //It's a foreign link so leave it as original
 547                      $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/quiz/view.php?q='.$old_id,$result);
 548                  }
 549              }
 550          }
 551  
 552          return $result;
 553      }
 554  
 555      //This function makes all the necessary calls to xxxx_decode_content_links()
 556      //function in each module, passing them the desired contents to be decoded
 557      //from backup format to destination site/course in order to mantain inter-activities
 558      //working in the backup/restore process. It's called from restore_decode_content_links()
 559      //function in restore process
 560      function quiz_decode_content_links_caller($restore) {
 561          global $CFG;
 562          $status = true;
 563          
 564          if ($quizs = get_records_sql ("SELECT q.id, q.intro
 565                                     FROM {$CFG->prefix}quiz q
 566                                     WHERE q.course = $restore->course_id")) {
 567                                                 //Iterate over each quiz->intro
 568              $i = 0;   //Counter to send some output to the browser to avoid timeouts
 569              foreach ($quizs as $quiz) {
 570                  //Increment counter
 571                  $i++;
 572                  $content = $quiz->intro;
 573                  $result = restore_decode_content_links_worker($content,$restore);
 574                  if ($result != $content) {
 575                      //Update record
 576                      $quiz->intro = addslashes($result);
 577                      $status = update_record("quiz",$quiz);
 578                      if (debugging()) {
 579                          if (!defined('RESTORE_SILENTLY')) {
 580                              echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
 581                          }
 582                      }
 583                  }
 584                  //Do some output
 585                  if (($i+1) % 5 == 0) {
 586                      if (!defined('RESTORE_SILENTLY')) {
 587                          echo ".";
 588                          if (($i+1) % 100 == 0) {
 589                              echo "<br />";
 590                          }
 591                      }
 592                      backup_flush(300);
 593                  }
 594              }
 595          }
 596  
 597          return $status;
 598      }
 599  
 600      //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
 601      //some texts in the module
 602      function quiz_restore_wiki2markdown ($restore) {
 603  
 604          global $CFG;
 605  
 606          $status = true;
 607  
 608          //Convert question->questiontext
 609          if ($records = get_records_sql ("SELECT q.id, q.questiontext, q.questiontextformat
 610                                           FROM {$CFG->prefix}question q,
 611                                                {$CFG->prefix}backup_ids b
 612                                           WHERE b.backup_code = $restore->backup_unique_code AND
 613                                                 b.table_name = 'question' AND
 614                                                 q.id = b.new_id AND
 615                                                 q.questiontextformat = ".FORMAT_WIKI)) {
 616              $i = 0;
 617              foreach ($records as $record) {
 618                  //Rebuild wiki links
 619                  $record->questiontext = restore_decode_wiki_content($record->questiontext, $restore);
 620                  //Convert to Markdown
 621                  $wtm = new WikiToMarkdown();
 622                  $record->questiontext = $wtm->convert($record->questiontext, $restore->course_id);
 623                  $record->questiontextformat = FORMAT_MARKDOWN;
 624                  $status = update_record('question', addslashes_object($record));
 625                  //Do some output
 626                  $i++;
 627                  if (($i+1) % 1 == 0) {
 628                      if (!defined('RESTORE_SILENTLY')) {
 629                          echo ".";
 630                          if (($i+1) % 20 == 0) {
 631                              echo "<br />";
 632                          }
 633                      }
 634                      backup_flush(300);
 635                  }
 636              }
 637          }
 638          return $status;
 639      }
 640  
 641      //This function returns a log record with all the necessay transformations
 642      //done. It's used by restore_log_module() to restore modules log.
 643      function quiz_restore_logs($restore,$log) {
 644  
 645          $status = false;
 646  
 647          //Depending of the action, we recode different things
 648          switch ($log->action) {
 649          case "add":
 650              if ($log->cmid) {
 651                  //Get the new_id of the module (to recode the info field)
 652                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 653                  if ($mod) {
 654                      $log->url = "view.php?id=".$log->cmid;
 655                      $log->info = $mod->new_id;
 656                      $status = true;
 657                  }
 658              }
 659              break;
 660          case "update":
 661              if ($log->cmid) {
 662                  //Get the new_id of the module (to recode the info field)
 663                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 664                  if ($mod) {
 665                      $log->url = "view.php?id=".$log->cmid;
 666                      $log->info = $mod->new_id;
 667                      $status = true;
 668                  }
 669              }
 670              break;
 671          case "view":
 672              if ($log->cmid) {
 673                  //Get the new_id of the module (to recode the info field)
 674                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 675                  if ($mod) {
 676                      $log->url = "view.php?id=".$log->cmid;
 677                      $log->info = $mod->new_id;
 678                      $status = true;
 679                  }
 680              }
 681              break;
 682          case "view all":
 683              $log->url = "index.php?id=".$log->course;
 684              $status = true;
 685              break;
 686          case "report":
 687              if ($log->cmid) {
 688                  //Get the new_id of the module (to recode the info field)
 689                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 690                  if ($mod) {
 691                      $log->url = "report.php?id=".$log->cmid;
 692                      $log->info = $mod->new_id;
 693                      $status = true;
 694                  }
 695              }
 696              break;
 697          case "attempt":
 698              if ($log->cmid) {
 699                  //Get the new_id of the module (to recode the info field)
 700                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 701                  if ($mod) {
 702                      //Extract the attempt id from the url field
 703                      $attid = substr(strrchr($log->url,"="),1);
 704                      //Get the new_id of the attempt (to recode the url field)
 705                      $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
 706                      if ($att) {
 707                          $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
 708                          $log->info = $mod->new_id;
 709                          $status = true;
 710                      }
 711                  }
 712              }
 713              break;
 714          case "submit":
 715              if ($log->cmid) {
 716                  //Get the new_id of the module (to recode the info field)
 717                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 718                  if ($mod) {
 719                      //Extract the attempt id from the url field
 720                      $attid = substr(strrchr($log->url,"="),1);
 721                      //Get the new_id of the attempt (to recode the url field)
 722                      $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
 723                      if ($att) {
 724                          $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
 725                          $log->info = $mod->new_id;
 726                          $status = true;
 727                      }
 728                  }
 729              }
 730              break;
 731          case "review":
 732              if ($log->cmid) {
 733                  //Get the new_id of the module (to recode the info field)
 734                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 735                  if ($mod) {
 736                      //Extract the attempt id from the url field
 737                      $attid = substr(strrchr($log->url,"="),1);
 738                      //Get the new_id of the attempt (to recode the url field)
 739                      $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
 740                      if ($att) {
 741                          $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
 742                          $log->info = $mod->new_id;
 743                          $status = true;
 744                      }
 745                  }
 746              }
 747              break;
 748          case "editquestions":
 749              if ($log->cmid) {
 750                  //Get the new_id of the module (to recode the url field)
 751                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 752                  if ($mod) {
 753                      $log->url = "view.php?id=".$log->cmid;
 754                      $log->info = $mod->new_id;
 755                      $status = true;
 756                  }
 757              }
 758              break;
 759          case "preview":
 760              if ($log->cmid) {
 761                  //Get the new_id of the module (to recode the url field)
 762                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 763                  if ($mod) {
 764                      $log->url = "attempt.php?id=".$log->cmid;
 765                      $log->info = $mod->new_id;
 766                      $status = true;
 767                  }
 768              }
 769              break;
 770          case "start attempt":
 771              if ($log->cmid) {
 772                  //Get the new_id of the module (to recode the info field)
 773                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 774                  if ($mod) {
 775                      //Extract the attempt id from the url field
 776                      $attid = substr(strrchr($log->url,"="),1);
 777                      //Get the new_id of the attempt (to recode the url field)
 778                      $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
 779                      if ($att) {
 780                          $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
 781                          $log->info = $mod->new_id;
 782                          $status = true;
 783                      }
 784                  }
 785              }
 786              break;
 787          case "close attempt":
 788              if ($log->cmid) {
 789                  //Get the new_id of the module (to recode the info field)
 790                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 791                  if ($mod) {
 792                      //Extract the attempt id from the url field
 793                      $attid = substr(strrchr($log->url,"="),1);
 794                      //Get the new_id of the attempt (to recode the url field)
 795                      $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
 796                      if ($att) {
 797                          $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
 798                          $log->info = $mod->new_id;
 799                          $status = true;
 800                      }
 801                  }
 802              }
 803              break;
 804          case "continue attempt":
 805              if ($log->cmid) {
 806                  //Get the new_id of the module (to recode the info field)
 807                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 808                  if ($mod) {
 809                      //Extract the attempt id from the url field
 810                      $attid = substr(strrchr($log->url,"="),1);
 811                      //Get the new_id of the attempt (to recode the url field)
 812                      $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
 813                      if ($att) {
 814                          $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
 815                          $log->info = $mod->new_id;
 816                          $status = true;
 817                      }
 818                  }
 819              }
 820              break;
 821          case "continue attemp":
 822              if ($log->cmid) {
 823                  //Get the new_id of the module (to recode the info field)
 824                  $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
 825                  if ($mod) {
 826                      //Extract the attempt id from the url field
 827                      $attid = substr(strrchr($log->url,"="),1);
 828                      //Get the new_id of the attempt (to recode the url field)
 829                      $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
 830                      if ($att) {
 831                          $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
 832                          $log->info = $mod->new_id;
 833                          $log->action = "continue attempt";  //To recover some bad actions
 834                          $status = true;
 835                      }
 836                  }
 837              }
 838              break;
 839          default:
 840              if (!defined('RESTORE_SILENTLY')) {
 841                  echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />";                 //Debug
 842              }
 843              break;
 844          }
 845  
 846          if ($status) {
 847              $status = $log;
 848          }
 849          return $status;
 850      }
 851  
 852      function quiz_recode_layout($layout, $restore) {
 853          //Recodes the quiz layout (a list of questions id and pagebreaks)
 854  
 855          //Extracts question id from sequence
 856          if ($questionids = explode(',', $layout)) {
 857              foreach ($questionids as $id => $questionid) {
 858                  if ($questionid) { // If it is zero then this is a pagebreak, don't translate
 859                      $newq = backup_getid($restore->backup_unique_code,"question",$questionid);
 860                      $questionids[$id] = $newq->new_id;
 861                  }
 862              }
 863          }
 864          return implode(',', $questionids);
 865      }
 866  
 867  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7