PageRenderTime 327ms CodeModel.GetById 48ms RepoModel.GetById 8ms app.codeStats 0ms

/mod/quiz/restorelib.php

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