PageRenderTime 35ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/quiz/restorelibpre15.php

https://bitbucket.org/ceu/moodle_demo
PHP | 2014 lines | 1237 code | 292 blank | 485 comment | 368 complexity | d9055e1a34d03a90ed67ce962e59eaf6 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  1. <?php // $Id: restorelibpre15.php,v 1.26.8.1 2007/11/02 16:19:57 tjhunt Exp $
  2. //This php script contains all the stuff to backup/restore
  3. //quiz mods
  4. //To see, put your terminal to 160cc
  5. //This is the "graphical" structure of the quiz mod:
  6. //
  7. // quiz question_categories
  8. // (CL,pk->id) (CL,pk->id)
  9. // | |
  10. // ------------------------------------------------------------------- |
  11. // | | | | |.......................................
  12. // | | | | | .
  13. // | | | | | .
  14. // quiz_attempts quiz_grades quiz_question_grades quiz_question_versions | ----question_datasets---- .
  15. // (UL,pk->id, fk->quiz) (UL,pk->id,fk->quiz) (CL,pk->id,fk->quiz) (CL,pk->id,fk->quiz) | | (CL,pk->id,fk->question, | .
  16. // | | . | | fk->dataset_definition) | .
  17. // | | . | | | .
  18. // | | . | | | .
  19. // | | . | | | .
  20. // quiz_responses | question question_dataset_definitions
  21. // (UL,pk->id, fk->attempt)----------------------------------------------------(CL,pk->id,fk->category,files) (CL,pk->id,fk->category)
  22. // | |
  23. // | |
  24. // | |
  25. // | question_dataset_items
  26. // | (CL,pk->id,fk->definition)
  27. // |
  28. // |
  29. // |
  30. // --------------------------------------------------------------------------------------------------------------
  31. // | | | | | | |
  32. // | | | | | | |
  33. // | | | | question_calculated | | question_randomsamatch
  34. // question_truefalse | question_multichoice | (CL,pl->id,fk->question) | |--(CL,pl->id,fk->question)
  35. // (CL,pl->id,fk->question) | (CL,pl->id,fk->question) | . | |
  36. // . | . | . | |
  37. // . question_shortanswer . question_numerical . question_multianswer. |
  38. // . (CL,pl->id,fk->question) . (CL,pl->id,fk->question) . (CL,pl->id,fk->question) | question_match
  39. // . . . . . . |--(CL,pl->id,fk->question)
  40. // . . . . . . | .
  41. // . . . . . . | .
  42. // . . . . . . | .
  43. // . . . . . . | question_match_sub
  44. // . . . . . . |--(CL,pl->id,fk->question)
  45. // ........................................................................................ |
  46. // . |
  47. // . |
  48. // . | question_numerical_units
  49. // question_answers |--(CL,pl->id,fk->question)
  50. // (CL,pk->id,fk->question)----------------------------------------------------------
  51. //
  52. // Meaning: pk->primary key field of the table
  53. // fk->foreign key to link with parent
  54. // nt->nested field (recursive data)
  55. // CL->course level info
  56. // UL->user level info
  57. // files->table may have files
  58. //
  59. //-----------------------------------------------------------
  60. //This module is special, because we make the restore in two steps:
  61. // 1.-We restore every category and their questions (complete structure). It includes this tables:
  62. // - question_categories
  63. // - question
  64. // - question_truefalse
  65. // - question_shortanswer
  66. // - question_multianswer
  67. // - question_multichoice
  68. // - question_numerical
  69. // - question_randomsamatch
  70. // - question_match
  71. // - question_match_sub
  72. // - question_calculated
  73. // - question_answers
  74. // - question_numerical_units
  75. // - question_datasets
  76. // - question_dataset_definitions
  77. // - question_dataset_items
  78. // All this backup info have its own section in moodle.xml (QUESTION_CATEGORIES) and it's generated
  79. // before every module backup standard invocation. And only if to restore quizzes has been selected !!
  80. // It's invoked with quiz_restore_question_categories. (course independent).
  81. // 2.-Standard module restore (Invoked via quiz_restore_mods). It includes this tables:
  82. // - quiz
  83. // - quiz_question_versions
  84. // - quiz_question_grades
  85. // - quiz_attempts
  86. // - quiz_grades
  87. // - quiz_responses
  88. // This step is the standard mod backup. (course dependent).
  89. //We are going to nedd quiz libs to be able to mimic the upgrade process
  90. require_once("$CFG->dirroot/mod/quiz/locallib.php");
  91. //STEP 1. Restore categories/questions and associated structures
  92. // (course independent)
  93. function quiz_restore_pre15_question_categories($category,$restore) {
  94. global $CFG;
  95. $status = true;
  96. //Get record from backup_ids
  97. $data = backup_getid($restore->backup_unique_code,"question_categories",$category->id);
  98. if ($data) {
  99. //Now get completed xmlized object
  100. $info = $data->info;
  101. //traverse_xmlize($info); //Debug
  102. //print_object ($GLOBALS['traverse_array']); //Debug
  103. //$GLOBALS['traverse_array']=""; //Debug
  104. //Now, build the question_categories record structure
  105. $quiz_cat->course = $restore->course_id;
  106. $quiz_cat->name = backup_todb($info['QUESTION_CATEGORY']['#']['NAME']['0']['#']);
  107. $quiz_cat->info = backup_todb($info['QUESTION_CATEGORY']['#']['INFO']['0']['#']);
  108. $quiz_cat->publish = backup_todb($info['QUESTION_CATEGORY']['#']['PUBLISH']['0']['#']);
  109. $quiz_cat->stamp = backup_todb($info['QUESTION_CATEGORY']['#']['STAMP']['0']['#']);
  110. $quiz_cat->parent = backup_todb($info['QUESTION_CATEGORY']['#']['PARENT']['0']['#']);
  111. $quiz_cat->sortorder = backup_todb($info['QUESTION_CATEGORY']['#']['SORTORDER']['0']['#']);
  112. if ($catfound = restore_get_best_question_category($quiz_cat, $restore->course_id)) {
  113. $newid = $catfound;
  114. } else {
  115. if (!$quiz_cat->stamp) {
  116. $quiz_cat->stamp = make_unique_id_code();
  117. }
  118. $newid = insert_record ("question_categories",$quiz_cat);
  119. }
  120. //Do some output
  121. if ($newid) {
  122. if (!defined('RESTORE_SILENTLY')) {
  123. echo "<li>".get_string('category', 'quiz')." \"".$quiz_cat->name."\"<br />";
  124. }
  125. } else {
  126. if (!defined('RESTORE_SILENTLY')) {
  127. //We must never arrive here !!
  128. echo "<li>".get_string('category', 'quiz')." \"".$quiz_cat->name."\" Error!<br />";
  129. }
  130. $status = false;
  131. }
  132. backup_flush(300);
  133. //Here category has been created or selected, so save results in backup_ids and start with questions
  134. if ($newid and $status) {
  135. //We have the newid, update backup_ids
  136. backup_putid($restore->backup_unique_code,"question_categories",
  137. $category->id, $newid);
  138. //Now restore question
  139. $status = quiz_restore_pre15_questions ($category->id, $newid,$info,$restore);
  140. } else {
  141. $status = false;
  142. }
  143. if (!defined('RESTORE_SILENTLY')) {
  144. echo '</li>';
  145. }
  146. }
  147. return $status;
  148. }
  149. function quiz_restore_pre15_questions ($old_category_id,$new_category_id,$info,$restore) {
  150. global $CFG;
  151. $status = true;
  152. //Get the questions array
  153. $questions = $info['QUESTION_CATEGORY']['#']['QUESTIONS']['0']['#']['QUESTION'];
  154. //Iterate over questions
  155. for($i = 0; $i < sizeof($questions); $i++) {
  156. $question = new object;
  157. $que_info = $questions[$i];
  158. //traverse_xmlize($que_info); //Debug
  159. //print_object ($GLOBALS['traverse_array']); //Debug
  160. //$GLOBALS['traverse_array']=""; //Debug
  161. //We'll need this later!!
  162. $oldid = backup_todb($que_info['#']['ID']['0']['#']);
  163. //Now, build the question record structure
  164. $question->category = $new_category_id;
  165. $question->parent = backup_todb($que_info['#']['PARENT']['0']['#']);
  166. $question->name = backup_todb($que_info['#']['NAME']['0']['#']);
  167. $question->questiontext = backup_todb($que_info['#']['QUESTIONTEXT']['0']['#']);
  168. $question->questiontextformat = backup_todb($que_info['#']['QUESTIONTEXTFORMAT']['0']['#']);
  169. $question->image = backup_todb($que_info['#']['IMAGE']['0']['#']);
  170. $question->defaultgrade = backup_todb($que_info['#']['DEFAULTGRADE']['0']['#']);
  171. if (isset($que_info['#']['PENALTY']['0']['#'])) { //Only if it's set, to apply DB default else.
  172. $question->penalty = backup_todb($que_info['#']['PENALTY']['0']['#']);
  173. }
  174. $question->qtype = backup_todb($que_info['#']['QTYPE']['0']['#']);
  175. if (isset($que_info['#']['LENGTH']['0']['#'])) { //Only if it's set, to apply DB default else.
  176. $question->length = backup_todb($que_info['#']['LENGTH']['0']['#']);
  177. }
  178. $question->stamp = backup_todb($que_info['#']['STAMP']['0']['#']);
  179. if (isset($que_info['#']['VERSION']['0']['#'])) { //Only if it's set, to apply DB default else.
  180. $question->version = backup_todb($que_info['#']['VERSION']['0']['#']);
  181. }
  182. if (isset($que_info['#']['HIDDEN']['0']['#'])) { //Only if it's set, to apply DB default else.
  183. $question->hidden = backup_todb($que_info['#']['HIDDEN']['0']['#']);
  184. }
  185. //Although only a few backups can have questions with parent, we try to recode it
  186. //if it contains something
  187. if ($question->parent and $parent = backup_getid($restore->backup_unique_code,"question",$question->parent)) {
  188. $question->parent = $parent->new_id;
  189. }
  190. // If it is a random question then hide it
  191. if ($question->qtype == RANDOM) {
  192. $question->hidden = 1;
  193. }
  194. //If it is a description question, length = 0
  195. if ($question->qtype == DESCRIPTION) {
  196. $question->length = 0;
  197. }
  198. //Check if the question exists
  199. //by category and stamp
  200. $question_exists = get_record ("question","category",$question->category,
  201. "stamp",$question->stamp);
  202. //If the stamp doesn't exists, check if question exists
  203. //by category, name and questiontext and calculate stamp
  204. //Mantains pre Beta 1.1 compatibility !!
  205. if (!$question->stamp) {
  206. $question->stamp = make_unique_id_code();
  207. $question->version = 1;
  208. $question_exists = get_record ("question","category",$question->category,
  209. "name",$question->name,
  210. "questiontext",$question->questiontext);
  211. }
  212. //If the question exists, only record its id
  213. if ($question_exists) {
  214. $newid = $question_exists->id;
  215. $creatingnewquestion = false;
  216. //Else, create a new question
  217. } else {
  218. //The structure is equal to the db, so insert the question
  219. $newid = insert_record ("question",$question);
  220. //If it is a random question, parent = id
  221. if ($newid && $question->qtype == RANDOM) {
  222. set_field ('question', 'parent', $newid, 'id', $newid);
  223. }
  224. $creatingnewquestion = true;
  225. }
  226. //Do some output
  227. if (($i+1) % 2 == 0) {
  228. if (!defined('RESTORE_SILENTLY')) {
  229. echo ".";
  230. if (($i+1) % 40 == 0) {
  231. echo "<br />";
  232. }
  233. }
  234. backup_flush(300);
  235. }
  236. //Save newid to backup tables
  237. if ($newid) {
  238. //We have the newid, update backup_ids
  239. backup_putid($restore->backup_unique_code,"question",$oldid,
  240. $newid);
  241. }
  242. //If it's a new question in the DB, restore it
  243. if ($creatingnewquestion) {
  244. //Now, restore every question_answers in this question
  245. $status = quiz_restore_pre15_answers($oldid,$newid,$que_info,$restore);
  246. //Now, depending of the type of questions, invoke different functions
  247. if ($question->qtype == "1") {
  248. $status = quiz_restore_pre15_shortanswer($oldid,$newid,$que_info,$restore);
  249. } else if ($question->qtype == "2") {
  250. $status = quiz_restore_pre15_truefalse($oldid,$newid,$que_info,$restore);
  251. } else if ($question->qtype == "3") {
  252. $status = quiz_restore_pre15_multichoice($oldid,$newid,$que_info,$restore);
  253. } else if ($question->qtype == "4") {
  254. //Random question. Nothing to do.
  255. } else if ($question->qtype == "5") {
  256. $status = quiz_restore_pre15_match($oldid,$newid,$que_info,$restore);
  257. } else if ($question->qtype == "6") {
  258. $status = quiz_restore_pre15_randomsamatch($oldid,$newid,$que_info,$restore);
  259. } else if ($question->qtype == "7") {
  260. //Description question. Nothing to do.
  261. } else if ($question->qtype == "8") {
  262. $status = quiz_restore_pre15_numerical($oldid,$newid,$que_info,$restore);
  263. } else if ($question->qtype == "9") {
  264. $status = quiz_restore_pre15_multianswer($oldid,$newid,$que_info,$restore);
  265. } else if ($question->qtype == "10") {
  266. $status = quiz_restore_pre15_calculated($oldid,$newid,$que_info,$restore);
  267. }
  268. } else {
  269. //We are NOT creating the question, but we need to know every question_answers
  270. //map between the XML file and the database to be able to restore the responses
  271. //in each attempt.
  272. $status = quiz_restore_pre15_map_answers($oldid,$newid,$que_info,$restore);
  273. //Now, depending of the type of questions, invoke different functions
  274. //to create the necessary mappings in backup_ids, because we are not
  275. //creating the question, but need some records in backup table
  276. if ($question->qtype == "1") {
  277. //Shortanswer question. Nothing to remap
  278. } else if ($question->qtype == "2") {
  279. //Truefalse question. Nothing to remap
  280. } else if ($question->qtype == "3") {
  281. //Multichoice question. Nothing to remap
  282. } else if ($question->qtype == "4") {
  283. //Random question. Nothing to remap
  284. } else if ($question->qtype == "5") {
  285. $status = quiz_restore_pre15_map_match($oldid,$newid,$que_info,$restore);
  286. } else if ($question->qtype == "6") {
  287. //Randomsamatch question. Nothing to remap
  288. } else if ($question->qtype == "7") {
  289. //Description question. Nothing to remap
  290. } else if ($question->qtype == "8") {
  291. //Numerical question. Nothing to remap
  292. } else if ($question->qtype == "9") {
  293. //Multianswer question. Nothing to remap
  294. } else if ($question->qtype == "10") {
  295. //Calculated question. Nothing to remap
  296. }
  297. }
  298. }
  299. return $status;
  300. }
  301. function quiz_restore_pre15_answers ($old_question_id,$new_question_id,$info,$restore) {
  302. global $CFG;
  303. $status = true;
  304. //Get the answers array
  305. if (isset($info['#']['ANSWERS']['0']['#']['ANSWER'])) {
  306. $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
  307. //Iterate over answers
  308. for($i = 0; $i < sizeof($answers); $i++) {
  309. $ans_info = $answers[$i];
  310. //traverse_xmlize($ans_info); //Debug
  311. //print_object ($GLOBALS['traverse_array']); //Debug
  312. //$GLOBALS['traverse_array']=""; //Debug
  313. //We'll need this later!!
  314. $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
  315. //Now, build the question_answers record structure
  316. $answer->question = $new_question_id;
  317. $answer->answer = backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
  318. $answer->fraction = backup_todb($ans_info['#']['FRACTION']['0']['#']);
  319. $answer->feedback = backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
  320. //The structure is equal to the db, so insert the question_answers
  321. $newid = insert_record ("question_answers",$answer);
  322. //Do some output
  323. if (($i+1) % 50 == 0) {
  324. if (!defined('RESTORE_SILENTLY')) {
  325. echo ".";
  326. if (($i+1) % 1000 == 0) {
  327. echo "<br />";
  328. }
  329. }
  330. backup_flush(300);
  331. }
  332. if ($newid) {
  333. //We have the newid, update backup_ids
  334. backup_putid($restore->backup_unique_code,"question_answers",$oldid,
  335. $newid);
  336. } else {
  337. $status = false;
  338. }
  339. }
  340. }
  341. return $status;
  342. }
  343. function quiz_restore_pre15_map_answers ($old_question_id,$new_question_id,$info,$restore) {
  344. global $CFG;
  345. $status = true;
  346. if (!isset($info['#']['ANSWERS'])) { // No answers in this question (eg random)
  347. return $status;
  348. }
  349. //Get the answers array
  350. $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
  351. //Iterate over answers
  352. for($i = 0; $i < sizeof($answers); $i++) {
  353. $ans_info = $answers[$i];
  354. //traverse_xmlize($ans_info); //Debug
  355. //print_object ($GLOBALS['traverse_array']); //Debug
  356. //$GLOBALS['traverse_array']=""; //Debug
  357. //We'll need this later!!
  358. $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
  359. //Now, build the question_answers record structure
  360. $answer->question = $new_question_id;
  361. $answer->answer = backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
  362. $answer->fraction = backup_todb($ans_info['#']['FRACTION']['0']['#']);
  363. $answer->feedback = backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
  364. //If we are in this method is because the question exists in DB, so its
  365. //answers must exist too.
  366. //Now, we are going to look for that answer in DB and to create the
  367. //mappings in backup_ids to use them later where restoring responses (user level).
  368. //Get the answer from DB (by question, answer and fraction)
  369. $db_answer = get_record ("question_answers","question",$new_question_id,
  370. "answer",$answer->answer,
  371. "fraction",$answer->fraction);
  372. //Do some output
  373. if (($i+1) % 50 == 0) {
  374. if (!defined('RESTORE_SILENTLY')) {
  375. echo ".";
  376. if (($i+1) % 1000 == 0) {
  377. echo "<br />";
  378. }
  379. }
  380. backup_flush(300);
  381. }
  382. if ($db_answer) {
  383. //We have the database answer, update backup_ids
  384. backup_putid($restore->backup_unique_code,"question_answers",$oldid,
  385. $db_answer->id);
  386. } else {
  387. $status = false;
  388. }
  389. }
  390. return $status;
  391. }
  392. function quiz_restore_pre15_shortanswer ($old_question_id,$new_question_id,$info,$restore,$restrictto = '') {
  393. global $CFG;
  394. $status = true;
  395. //Get the shortanswers array
  396. $shortanswers = $info['#']['SHORTANSWER'];
  397. //Iterate over shortanswers
  398. for($i = 0; $i < sizeof($shortanswers); $i++) {
  399. $sho_info = $shortanswers[$i];
  400. //traverse_xmlize($sho_info); //Debug
  401. //print_object ($GLOBALS['traverse_array']); //Debug
  402. //$GLOBALS['traverse_array']=""; //Debug
  403. //Now, build the question_shortanswer record structure
  404. $shortanswer->question = $new_question_id;
  405. $shortanswer->answers = backup_todb($sho_info['#']['ANSWERS']['0']['#']);
  406. $shortanswer->usecase = backup_todb($sho_info['#']['USECASE']['0']['#']);
  407. //We have to recode the answers field (a list of answers id)
  408. //Extracts answer id from sequence
  409. $answers_field = "";
  410. $in_first = true;
  411. $tok = strtok($shortanswer->answers,",");
  412. while ($tok) {
  413. //Get the answer from backup_ids
  414. $answer = backup_getid($restore->backup_unique_code,"question_answers",$tok);
  415. if ($answer) {
  416. if ($in_first) {
  417. $answers_field .= $answer->new_id;
  418. $in_first = false;
  419. } else {
  420. $answers_field .= ",".$answer->new_id;
  421. }
  422. }
  423. //check for next
  424. $tok = strtok(",");
  425. }
  426. //We have the answers field recoded to its new ids
  427. $shortanswer->answers = $answers_field;
  428. //The structure is equal to the db, so insert the question_shortanswer
  429. //Only if there aren't restrictions or there are restriction concordance
  430. if (empty($restrictto) || (!empty($restrictto) && $shortanswer->answers == $restrictto)) {
  431. $newid = insert_record ("question_shortanswer",$shortanswer);
  432. }
  433. //Do some output
  434. if (($i+1) % 50 == 0) {
  435. if (!defined('RESTORE_SILENTLY')) {
  436. echo ".";
  437. if (($i+1) % 1000 == 0) {
  438. echo "<br />";
  439. }
  440. }
  441. backup_flush(300);
  442. }
  443. if (!$newid && !$restrictto) {
  444. $status = false;
  445. }
  446. }
  447. return $status;
  448. }
  449. function quiz_restore_pre15_truefalse ($old_question_id,$new_question_id,$info,$restore) {
  450. global $CFG;
  451. $status = true;
  452. //Get the truefalse array
  453. $truefalses = $info['#']['TRUEFALSE'];
  454. //Iterate over truefalse
  455. for($i = 0; $i < sizeof($truefalses); $i++) {
  456. $tru_info = $truefalses[$i];
  457. //traverse_xmlize($tru_info); //Debug
  458. //print_object ($GLOBALS['traverse_array']); //Debug
  459. //$GLOBALS['traverse_array']=""; //Debug
  460. //Now, build the question_truefalse record structure
  461. $truefalse->question = $new_question_id;
  462. $truefalse->trueanswer = backup_todb($tru_info['#']['TRUEANSWER']['0']['#']);
  463. $truefalse->falseanswer = backup_todb($tru_info['#']['FALSEANSWER']['0']['#']);
  464. ////We have to recode the trueanswer field
  465. $answer = backup_getid($restore->backup_unique_code,"question_answers",$truefalse->trueanswer);
  466. if ($answer) {
  467. $truefalse->trueanswer = $answer->new_id;
  468. }
  469. ////We have to recode the falseanswer field
  470. $answer = backup_getid($restore->backup_unique_code,"question_answers",$truefalse->falseanswer);
  471. if ($answer) {
  472. $truefalse->falseanswer = $answer->new_id;
  473. }
  474. //The structure is equal to the db, so insert the question_truefalse
  475. $newid = insert_record ("question_truefalse",$truefalse);
  476. //Do some output
  477. if (($i+1) % 50 == 0) {
  478. if (!defined('RESTORE_SILENTLY')) {
  479. echo ".";
  480. if (($i+1) % 1000 == 0) {
  481. echo "<br />";
  482. }
  483. }
  484. backup_flush(300);
  485. }
  486. if (!$newid) {
  487. $status = false;
  488. }
  489. }
  490. return $status;
  491. }
  492. function quiz_restore_pre15_multichoice ($old_question_id,$new_question_id,$info,$restore, $restrictto = '') {
  493. global $CFG;
  494. $status = true;
  495. //Get the multichoices array
  496. $multichoices = $info['#']['MULTICHOICE'];
  497. //Iterate over multichoices
  498. for($i = 0; $i < sizeof($multichoices); $i++) {
  499. $mul_info = $multichoices[$i];
  500. //traverse_xmlize($mul_info); //Debug
  501. //print_object ($GLOBALS['traverse_array']); //Debug
  502. //$GLOBALS['traverse_array']=""; //Debug
  503. //Now, build the question_multichoice record structure
  504. $multichoice->question = $new_question_id;
  505. $multichoice->layout = backup_todb($mul_info['#']['LAYOUT']['0']['#']);
  506. $multichoice->answers = backup_todb($mul_info['#']['ANSWERS']['0']['#']);
  507. $multichoice->single = backup_todb($mul_info['#']['SINGLE']['0']['#']);
  508. //We have to recode the answers field (a list of answers id)
  509. //Extracts answer id from sequence
  510. $answers_field = "";
  511. $in_first = true;
  512. $tok = strtok($multichoice->answers,",");
  513. while ($tok) {
  514. //Get the answer from backup_ids
  515. $answer = backup_getid($restore->backup_unique_code,"question_answers",$tok);
  516. if ($answer) {
  517. if ($in_first) {
  518. $answers_field .= $answer->new_id;
  519. $in_first = false;
  520. } else {
  521. $answers_field .= ",".$answer->new_id;
  522. }
  523. }
  524. //check for next
  525. $tok = strtok(",");
  526. }
  527. //We have the answers field recoded to its new ids
  528. $multichoice->answers = $answers_field;
  529. //The structure is equal to the db, so insert the question_shortanswer
  530. //Only if there aren't restrictions or there are restriction concordance
  531. if (empty($restrictto) || (!empty($restrictto) && $multichoice->answers == $restrictto)) {
  532. $newid = insert_record ("question_multichoice",$multichoice);
  533. }
  534. //Do some output
  535. if (($i+1) % 50 == 0) {
  536. if (!defined('RESTORE_SILENTLY')) {
  537. echo ".";
  538. if (($i+1) % 1000 == 0) {
  539. echo "<br />";
  540. }
  541. }
  542. backup_flush(300);
  543. }
  544. if (!$newid && !$restrictto) {
  545. $status = false;
  546. }
  547. }
  548. return $status;
  549. }
  550. function quiz_restore_pre15_match ($old_question_id,$new_question_id,$info,$restore) {
  551. global $CFG;
  552. $status = true;
  553. //Get the matchs array
  554. $matchs = $info['#']['MATCHS']['0']['#']['MATCH'];
  555. //We have to build the subquestions field (a list of match_sub id)
  556. $subquestions_field = "";
  557. $in_first = true;
  558. //Iterate over matchs
  559. for($i = 0; $i < sizeof($matchs); $i++) {
  560. $mat_info = $matchs[$i];
  561. //traverse_xmlize($mat_info); //Debug
  562. //print_object ($GLOBALS['traverse_array']); //Debug
  563. //$GLOBALS['traverse_array']=""; //Debug
  564. //We'll need this later!!
  565. $oldid = backup_todb($mat_info['#']['ID']['0']['#']);
  566. //Now, build the question_match_SUB record structure
  567. $match_sub->question = $new_question_id;
  568. $match_sub->questiontext = backup_todb($mat_info['#']['QUESTIONTEXT']['0']['#']);
  569. $match_sub->answertext = backup_todb($mat_info['#']['ANSWERTEXT']['0']['#']);
  570. //The structure is equal to the db, so insert the question_match_sub
  571. $newid = insert_record ("question_match_sub",$match_sub);
  572. //Do some output
  573. if (($i+1) % 50 == 0) {
  574. if (!defined('RESTORE_SILENTLY')) {
  575. echo ".";
  576. if (($i+1) % 1000 == 0) {
  577. echo "<br />";
  578. }
  579. }
  580. backup_flush(300);
  581. }
  582. if ($newid) {
  583. //We have the newid, update backup_ids
  584. backup_putid($restore->backup_unique_code,"question_match_sub",$oldid,
  585. $newid);
  586. //We have a new match_sub, append it to subquestions_field
  587. if ($in_first) {
  588. $subquestions_field .= $newid;
  589. $in_first = false;
  590. } else {
  591. $subquestions_field .= ",".$newid;
  592. }
  593. } else {
  594. $status = false;
  595. }
  596. }
  597. //We have created every match_sub, now create the match
  598. $match->question = $new_question_id;
  599. $match->subquestions = $subquestions_field;
  600. //The structure is equal to the db, so insert the question_match_sub
  601. $newid = insert_record ("question_match",$match);
  602. if (!$newid) {
  603. $status = false;
  604. }
  605. return $status;
  606. }
  607. function quiz_restore_pre15_map_match ($old_question_id,$new_question_id,$info,$restore) {
  608. global $CFG;
  609. $status = true;
  610. //Get the matchs array
  611. $matchs = $info['#']['MATCHS']['0']['#']['MATCH'];
  612. //We have to build the subquestions field (a list of match_sub id)
  613. $subquestions_field = "";
  614. $in_first = true;
  615. //Iterate over matchs
  616. for($i = 0; $i < sizeof($matchs); $i++) {
  617. $mat_info = $matchs[$i];
  618. //traverse_xmlize($mat_info); //Debug
  619. //print_object ($GLOBALS['traverse_array']); //Debug
  620. //$GLOBALS['traverse_array']=""; //Debug
  621. //We'll need this later!!
  622. $oldid = backup_todb($mat_info['#']['ID']['0']['#']);
  623. //Now, build the question_match_SUB record structure
  624. $match_sub->question = $new_question_id;
  625. $match_sub->questiontext = backup_todb($mat_info['#']['QUESTIONTEXT']['0']['#']);
  626. $match_sub->answertext = backup_todb($mat_info['#']['ANSWERTEXT']['0']['#']);
  627. //If we are in this method is because the question exists in DB, so its
  628. //match_sub must exist too.
  629. //Now, we are going to look for that match_sub in DB and to create the
  630. //mappings in backup_ids to use them later where restoring responses (user level).
  631. //Get the match_sub from DB (by question, questiontext and answertext)
  632. $db_match_sub = get_record ("question_match_sub","question",$new_question_id,
  633. "questiontext",$match_sub->questiontext,
  634. "answertext",$match_sub->answertext);
  635. //Do some output
  636. if (($i+1) % 50 == 0) {
  637. if (!defined('RESTORE_SILENTLY')) {
  638. echo ".";
  639. if (($i+1) % 1000 == 0) {
  640. echo "<br />";
  641. }
  642. }
  643. backup_flush(300);
  644. }
  645. //We have the database match_sub, so update backup_ids
  646. if ($db_match_sub) {
  647. //We have the newid, update backup_ids
  648. backup_putid($restore->backup_unique_code,"question_match_sub",$oldid,
  649. $db_match_sub->id);
  650. } else {
  651. $status = false;
  652. }
  653. }
  654. return $status;
  655. }
  656. function quiz_restore_pre15_randomsamatch ($old_question_id,$new_question_id,$info,$restore) {
  657. global $CFG;
  658. $status = true;
  659. //Get the randomsamatchs array
  660. $randomsamatchs = $info['#']['RANDOMSAMATCH'];
  661. //Iterate over randomsamatchs
  662. for($i = 0; $i < sizeof($randomsamatchs); $i++) {
  663. $ran_info = $randomsamatchs[$i];
  664. //traverse_xmlize($ran_info); //Debug
  665. //print_object ($GLOBALS['traverse_array']); //Debug
  666. //$GLOBALS['traverse_array']=""; //Debug
  667. //Now, build the question_randomsamatch record structure
  668. $randomsamatch->question = $new_question_id;
  669. $randomsamatch->choose = backup_todb($ran_info['#']['CHOOSE']['0']['#']);
  670. //The structure is equal to the db, so insert the question_randomsamatch
  671. $newid = insert_record ("question_randomsamatch",$randomsamatch);
  672. //Do some output
  673. if (($i+1) % 50 == 0) {
  674. if (!defined('RESTORE_SILENTLY')) {
  675. echo ".";
  676. if (($i+1) % 1000 == 0) {
  677. echo "<br />";
  678. }
  679. }
  680. backup_flush(300);
  681. }
  682. if (!$newid) {
  683. $status = false;
  684. }
  685. }
  686. return $status;
  687. }
  688. function quiz_restore_pre15_numerical ($old_question_id,$new_question_id,$info,$restore, $restrictto = '') {
  689. global $CFG;
  690. $status = true;
  691. //Get the numerical array
  692. $numericals = $info['#']['NUMERICAL'];
  693. //Iterate over numericals
  694. for($i = 0; $i < sizeof($numericals); $i++) {
  695. $num_info = $numericals[$i];
  696. //traverse_xmlize($num_info); //Debug
  697. //print_object ($GLOBALS['traverse_array']); //Debug
  698. //$GLOBALS['traverse_array']=""; //Debug
  699. //Now, build the question_numerical record structure
  700. $numerical->question = $new_question_id;
  701. $numerical->answer = backup_todb($num_info['#']['ANSWER']['0']['#']);
  702. $numerical->min = backup_todb($num_info['#']['MIN']['0']['#']);
  703. $numerical->max = backup_todb($num_info['#']['MAX']['0']['#']);
  704. ////We have to recode the answer field
  705. $answer = backup_getid($restore->backup_unique_code,"question_answers",$numerical->answer);
  706. if ($answer) {
  707. $numerical->answer = $answer->new_id;
  708. }
  709. //Answer goes to answers in 1.5 (although it continues being only one!)
  710. //Changed 12-05 (chating with Gustav and Julian this remains = pre15 = answer)
  711. //$numerical->answers = $numerical->answer;
  712. //We have to calculate the tolerance field of the numerical question
  713. $numerical->tolerance = ($numerical->max - $numerical->min)/2;
  714. //The structure is equal to the db, so insert the question_numerical
  715. //Only if there aren't restrictions or there are restriction concordance
  716. if (empty($restrictto) || (!empty($restrictto) && in_array($numerical->answer,explode(",",$restrictto)))) {
  717. $newid = insert_record ("question_numerical",$numerical);
  718. }
  719. //Do some output
  720. if (($i+1) % 50 == 0) {
  721. if (!defined('RESTORE_SILENTLY')) {
  722. echo ".";
  723. if (($i+1) % 1000 == 0) {
  724. echo "<br />";
  725. }
  726. }
  727. backup_flush(300);
  728. }
  729. //Now restore numerical_units
  730. if ($newid) {
  731. $status = quiz_restore_pre15_numerical_units ($old_question_id,$new_question_id,$num_info,$restore);
  732. }
  733. if (!$newid && !$restrictto) {
  734. $status = false;
  735. }
  736. }
  737. return $status;
  738. }
  739. function quiz_restore_pre15_calculated ($old_question_id,$new_question_id,$info,$restore) {
  740. global $CFG;
  741. $status = true;
  742. //Get the calculated-s array
  743. $calculateds = $info['#']['CALCULATED'];
  744. //Iterate over calculateds
  745. for($i = 0; $i < sizeof($calculateds); $i++) {
  746. $cal_info = $calculateds[$i];
  747. //traverse_xmlize($cal_info); //Debug
  748. //print_object ($GLOBALS['traverse_array']); //Debug
  749. //$GLOBALS['traverse_array']=""; //Debug
  750. //Now, build the question_calculated record structure
  751. $calculated->question = $new_question_id;
  752. $calculated->answer = backup_todb($cal_info['#']['ANSWER']['0']['#']);
  753. $calculated->tolerance = backup_todb($cal_info['#']['TOLERANCE']['0']['#']);
  754. $calculated->tolerancetype = backup_todb($cal_info['#']['TOLERANCETYPE']['0']['#']);
  755. $calculated->correctanswerlength = backup_todb($cal_info['#']['CORRECTANSWERLENGTH']['0']['#']);
  756. $calculated->correctanswerformat = backup_todb($cal_info['#']['CORRECTANSWERFORMAT']['0']['#']);
  757. ////We have to recode the answer field
  758. $answer = backup_getid($restore->backup_unique_code,"question_answers",$calculated->answer);
  759. if ($answer) {
  760. $calculated->answer = $answer->new_id;
  761. }
  762. //If we haven't correctanswerformat, it defaults to 2 (in DB)
  763. if (empty($calculated->correctanswerformat)) {
  764. $calculated->correctanswerformat = 2;
  765. }
  766. //The structure is equal to the db, so insert the question_calculated
  767. $newid = insert_record ("question_calculated",$calculated);
  768. //Do some output
  769. if (($i+1) % 50 == 0) {
  770. if (!defined('RESTORE_SILENTLY')) {
  771. echo ".";
  772. if (($i+1) % 1000 == 0) {
  773. echo "<br />";
  774. }
  775. }
  776. backup_flush(300);
  777. }
  778. //Now restore numerical_units
  779. $status = quiz_restore_pre15_numerical_units ($old_question_id,$new_question_id,$cal_info,$restore);
  780. //Now restore dataset_definitions
  781. if ($status && $newid) {
  782. $status = quiz_restore_pre15_dataset_definitions ($old_question_id,$new_question_id,$cal_info,$restore);
  783. }
  784. if (!$newid) {
  785. $status = false;
  786. }
  787. }
  788. return $status;
  789. }
  790. function quiz_restore_pre15_multianswer ($old_question_id,$new_question_id,$info,$restore) {
  791. global $CFG;
  792. $status = true;
  793. //We need some question fields here so we get the full record from DB
  794. $parentquestion = get_record('question','id',$new_question_id);
  795. //We need to store all the positions with their created questions
  796. //to be able to calculate the sequence field
  797. $createdquestions = array();
  798. //Under 1.5, every multianswer record becomes a question itself
  799. //with its parent set to the cloze question. And there is only
  800. //ONE multianswer record with the sequence of questions used.
  801. //Get the multianswers array
  802. $multianswers_array = $info['#']['MULTIANSWERS']['0']['#']['MULTIANSWER'];
  803. //Iterate over multianswers_array
  804. for($i = 0; $i < sizeof($multianswers_array); $i++) {
  805. $mul_info = $multianswers_array[$i];
  806. //traverse_xmlize($mul_info); //Debug
  807. //print_object ($GLOBALS['traverse_array']); //Debug
  808. //$GLOBALS['traverse_array']=""; //Debug
  809. //We need this later
  810. $oldid = backup_todb($mul_info['#']['ID']['0']['#']);
  811. //Now, build the question_multianswer record structure
  812. $multianswer->question = $new_question_id;
  813. $multianswer->answers = backup_todb($mul_info['#']['ANSWERS']['0']['#']);
  814. $multianswer->positionkey = backup_todb($mul_info['#']['POSITIONKEY']['0']['#']);
  815. $multianswer->answertype = backup_todb($mul_info['#']['ANSWERTYPE']['0']['#']);
  816. $multianswer->norm = backup_todb($mul_info['#']['NORM']['0']['#']);
  817. //Saving multianswer and positionkey to use them later restoring states
  818. backup_putid ($restore->backup_unique_code,'multianswer-pos',$oldid,$multianswer->positionkey);
  819. //We have to recode all the answers to their new ids
  820. $ansarr = explode(",", $multianswer->answers);
  821. foreach ($ansarr as $key => $value) {
  822. //Get the answer from backup_ids
  823. $answer = backup_getid($restore->backup_unique_code,'question_answers',$value);
  824. $ansarr[$key] = $answer->new_id;
  825. }
  826. $multianswer->answers = implode(",",$ansarr);
  827. //Build the new question structure
  828. $question = new object;
  829. $question->category = $parentquestion->category;
  830. $question->parent = $parentquestion->id;
  831. $question->name = $parentquestion->name;
  832. $question->questiontextformat = $parentquestion->questiontextformat;
  833. $question->defaultgrade = $multianswer->norm;
  834. $question->penalty = $parentquestion->penalty;
  835. $question->qtype = $multianswer->answertype;
  836. $question->version = $parentquestion->version;
  837. $question->hidden = $parentquestion->hidden;
  838. $question->length = 0;
  839. $question->questiontext = '';
  840. $question->stamp = make_unique_id_code();
  841. //Save the new question to DB
  842. $newid = insert_record('question', $question);
  843. if ($newid) {
  844. $createdquestions[$multianswer->positionkey] = $newid;
  845. }
  846. //Do some output
  847. if (($i+1) % 50 == 0) {
  848. if (!defined('RESTORE_SILENTLY')) {
  849. echo ".";
  850. if (($i+1) % 1000 == 0) {
  851. echo "<br />";
  852. }
  853. }
  854. backup_flush(300);
  855. }
  856. //Remap question_answers records from the original multianswer question
  857. //to their newly created question
  858. if ($newid) {
  859. $answersdb = get_records_list('question_answers','id',$multianswer->answers);
  860. foreach ($answersdb as $answerdb) {
  861. set_field('question_answers','question',$newid,'id',$answerdb->id);
  862. }
  863. }
  864. //If we have created the question record, now, depending of the
  865. //answertype, delegate the restore to every qtype function
  866. if ($newid) {
  867. if ($multianswer->answertype == "1") {
  868. $status = quiz_restore_pre15_shortanswer ($old_question_id,$newid,$mul_info,$restore,$multianswer->answers);
  869. } else if ($multianswer->answertype == "3") {
  870. $status = quiz_restore_pre15_multichoice ($old_question_id,$newid,$mul_info,$restore,$multianswer->answers);
  871. } else if ($multianswer->answertype == "8") {
  872. $status = quiz_restore_pre15_numerical ($old_question_id,$newid,$mul_info,$restore,$multianswer->answers);
  873. }
  874. } else {
  875. $status = false;
  876. }
  877. }
  878. //Everything is created, just going to create the multianswer record
  879. if ($status) {
  880. ksort($createdquestions);
  881. $multianswerdb = new object;
  882. $multianswerdb->question = $parentquestion->id;
  883. $multianswerdb->sequence = implode(",",$createdquestions);
  884. $mid = insert_record('question_multianswer', $multianswerdb);
  885. if (!$mid) {
  886. $status = false;
  887. }
  888. }
  889. return $status;
  890. }
  891. function quiz_restore_pre15_numerical_units ($old_question_id,$new_question_id,$info,$restore) {
  892. global $CFG;
  893. $status = true;
  894. //Get the numerical array
  895. $numerical_units = $info['#']['NUMERICAL_UNITS']['0']['#']['NUMERICAL_UNIT'];
  896. //Iterate over numerical_units
  897. for($i = 0; $i < sizeof($numerical_units); $i++) {
  898. $nu_info = $numerical_units[$i];
  899. //traverse_xmlize($nu_info); //Debug
  900. //print_object ($GLOBALS['traverse_array']); //Debug
  901. //$GLOBALS['traverse_array']=""; //Debug
  902. //Now, build the question_numerical_UNITS record structure
  903. $numerical_unit->question = $new_question_id;
  904. $numerical_unit->multiplier = backup_todb($nu_info['#']['MULTIPLIER']['0']['#']);
  905. $numerical_unit->unit = backup_todb($nu_info['#']['UNIT']['0']['#']);
  906. //The structure is equal to the db, so insert the question_numerical_units
  907. $newid = insert_record ("question_numerical_units",$numerical_unit);
  908. if (!$newid) {
  909. $status = false;
  910. }
  911. }
  912. return $status;
  913. }
  914. function quiz_restore_pre15_dataset_definitions ($old_question_id,$new_question_id,$info,$restore) {
  915. global $CFG;
  916. $status = true;
  917. //Get the dataset_definitions array
  918. $dataset_definitions = $info['#']['DATASET_DEFINITIONS']['0']['#']['DATASET_DEFINITION'];
  919. //Iterate over dataset_definitions
  920. for($i = 0; $i < sizeof($dataset_definitions); $i++) {
  921. $dd_info = $dataset_definitions[$i];
  922. //traverse_xmlize($dd_info); //Debug
  923. //print_object ($GLOBALS['traverse_array']); //Debug
  924. //$GLOBALS['traverse_array']=""; //Debug
  925. //Now, build the question_dataset_DEFINITION record structure
  926. $dataset_definition->category = backup_todb($dd_info['#']['CATEGORY']['0']['#']);
  927. $dataset_definition->name = backup_todb($dd_info['#']['NAME']['0']['#']);
  928. $dataset_definition->type = backup_todb($dd_info['#']['TYPE']['0']['#']);
  929. $dataset_definition->options = backup_todb($dd_info['#']['OPTIONS']['0']['#']);
  930. $dataset_definition->itemcount = backup_todb($dd_info['#']['ITEMCOUNT']['0']['#']);
  931. //We have to recode the category field (only if the category != 0)
  932. if ($dataset_definition->category != 0) {
  933. $category = backup_getid($restore->backup_unique_code,"question_categories",$dataset_definition->category);
  934. if ($category) {
  935. $dataset_definition->category = $category->new_id;
  936. }
  937. }
  938. //Now, we hace to decide when to create the new records or reuse an existing one
  939. $create_definition = false;
  940. //If the dataset_definition->category = 0, it's a individual question dataset_definition, so we'll create it
  941. if ($dataset_definition->category == 0) {
  942. $create_definition = true;
  943. } else {
  944. //The category isn't 0, so it's a category question dataset_definition, we have to see if it exists
  945. //Look for a definition with the same category, name and type
  946. if ($definitionrec = get_record_sql("SELECT d.*
  947. FROM {$CFG->prefix}question_dataset_definitions d
  948. WHERE d.category = '$dataset_definition->category' AND
  949. d.name = '$dataset_definition->name' AND
  950. d.type = '$dataset_definition->type'")) {
  951. //Such dataset_definition exist. Now we must check if it has enough itemcount
  952. if ($definitionrec->itemcount < $dataset_definition->itemcount) {
  953. //We haven't enough itemcount, so we have to create the definition as an individual question one.
  954. $dataset_definition->category = 0;
  955. $create_definition = true;
  956. } else {
  957. //We have enough itemcount, so we'll reuse the existing definition
  958. $create_definition = false;
  959. $newid = $definitionrec->id;
  960. }
  961. } else {
  962. //Such dataset_definition doesn't exist. We'll create it.
  963. $create_definition = true;
  964. }
  965. }
  966. //If we've to create the definition, do it
  967. if ($create_definition) {
  968. //The structure is equal to the db, so insert the question_dataset_definitions
  969. $newid = insert_record ("question_dataset_definitions",$dataset_definition);
  970. if ($newid) {
  971. //Restore question_dataset_items
  972. $status = quiz_restore_pre15_dataset_items($newid,$dd_info,$restore);
  973. }
  974. }
  975. //Now, we must have a definition (created o reused). Its id is in newid. Create the question_datasets record
  976. //to join the question and the dataset_definition
  977. if ($newid) {
  978. $question_dataset->question = $new_question_id;
  979. $question_dataset->datasetdefinition = $newid;
  980. $newid = insert_record ("question_datasets",$question_dataset);
  981. }
  982. if (!$newid) {
  983. $status = false;
  984. }
  985. }
  986. return $status;
  987. }
  988. function quiz_restore_pre15_dataset_items ($definitionid,$info,$restore) {
  989. global $CFG;
  990. $status = true;
  991. //Get the items array
  992. $dataset_items = $info['#']['DATASET_ITEMS']['0']['#']['DATASET_ITEM'];
  993. //Iterate over dataset_items
  994. for($i = 0; $i < sizeof($dataset_items); $i++) {
  995. $di_info = $dataset_items[$i];
  996. //traverse_xmlize($di_info); //Debug
  997. //print_object ($GLOBALS['traverse_array']); //Debug
  998. //$GLOBALS['traverse_array']=""; //Debug
  999. //Now, build the question_dataset_ITEMS record structure
  1000. $dataset_item->definition = $definitionid;
  1001. $dataset_item->itemnumber = backup_todb($di_info['#']['NUMBER']['0']['#']);
  1002. $dataset_item->value = backup_todb($di_info['#']['VALUE']['0']['#']);
  1003. //The structure is equal to the db, so insert the question_dataset_items
  1004. $newid = insert_record ("question_dataset_items",$dataset_item);
  1005. if (!$newid) {
  1006. $status = false;
  1007. }
  1008. }
  1009. return $status;
  1010. }
  1011. //STEP 2. Restore quizzes and associated structures
  1012. // (course dependent)
  1013. function quiz_restore_pre15_mods($mod,$restore) {
  1014. global $CFG;
  1015. $status = true;
  1016. //Get record from backup_ids
  1017. $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
  1018. if ($data) {
  1019. //Now get completed xmlized object
  1020. $info = $data->info;
  1021. //if necessary, write to restorelog and adjust date/time fields
  1022. if ($restore->course_startdateoffset) {
  1023. restore_log_date_changes('Quiz', $restore, $info['MOD']['#'], array('TIMEOPEN', 'TIMECLOSE'));
  1024. }
  1025. //traverse_xmlize($info); //Debug
  1026. //print_object ($GLOBALS['traverse_array']); //Debug
  1027. //$GLOBALS['traverse_array']=""; //Debug
  1028. //Now, build the QUIZ record structure
  1029. $quiz->course = $restore->course_id;
  1030. $quiz->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
  1031. $quiz->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
  1032. $quiz->timeopen = backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
  1033. $quiz->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
  1034. $quiz->attempts = backup_todb($info['MOD']['#']['ATTEMPTS_NUMBER']['0']['#']);
  1035. $quiz->attemptonlast = backup_todb($info['MOD']['#']['ATTEMPTONLAST']['0']['#']);
  1036. $quiz->feedback = backup_todb($info['MOD']['#']['FEEDBACK']['0']['#']);
  1037. $quiz->correctanswers = backup_todb($info['MOD']['#']['CORRECTANSWERS']['0']['#']);
  1038. $quiz->grademethod = backup_todb($info['MOD']['#']['GRADEMETHOD']['0']['#']);
  1039. if (isset($info['MOD']['#']['DECIMALPOINTS']['0']['#'])) { //Only if it's set, to apply DB default else.
  1040. $quiz->decimalpoints = backup_todb($info['MOD']['#']['DECIMALPOINTS']['0']['#']);
  1041. }
  1042. $quiz->review = backup_todb($info['MOD']['#']['REVIEW']['0']['#']);
  1043. $quiz->questionsperpage = backup_todb($info['MOD']['#']['QUESTIONSPERPAGE']['0']['#']);
  1044. $quiz->shufflequestions = backup_todb($info['MOD']['#']['SHUFFLEQUESTIONS']['0']['#']);
  1045. $quiz->shuffleanswers = backup_todb($info['MOD']['#']['SHUFFLEANSWERS']['0']['#']);
  1046. $quiz->questions = backup_todb($info['MOD']['#']['QUESTIONS']['0']['#']);
  1047. $quiz->sumgrades = backup_todb($info['MOD']['#']['SUMGRADES']['0']['#']);
  1048. $quiz->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
  1049. $quiz->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
  1050. $quiz->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
  1051. $quiz->timelimit = backup_todb($info['MOD']['#']['TIMELIMIT']['0']['#']);
  1052. $quiz->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
  1053. $quiz->subnet = backup_todb($info['MOD']['#']['SUBNET']['0']['#']);
  1054. $quiz->popup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
  1055. //We have to recode the questions field (a list of questions id)
  1056. $newquestions = array();
  1057. if ($questionsarr = explode (",",$quiz->questions)) {
  1058. foreach ($questionsarr as $key => $value) {
  1059. if ($question = backup_getid($restore->backup_unique_code,"question",$value)) {
  1060. $newquestions[] = $question->new_id;
  1061. }
  1062. }
  1063. }
  1064. $quiz->questions = implode (",", $newquestions);
  1065. //Recalculate the questions field to include page breaks if necessary
  1066. $quiz->questions = quiz_repaginate($quiz->questions, $quiz->questionsperpage);
  1067. //Calculate the new review field contents (logic extracted from upgrade)
  1068. $review = (QUIZ_REVIEW_IMMEDIATELY & (QUIZ_REVIEW_RESPONSES + QUIZ_REVIEW_SCORES));
  1069. if ($quiz->feedback) {
  1070. $review += (QUIZ_REVIEW_IMMEDIATELY & QUIZ_REVIEW_FEEDBACK);
  1071. }
  1072. if ($quiz->correctanswers) {
  1073. $review += (QUIZ_REVIEW_IMMEDIATELY & QUIZ_REVIEW_ANSWERS);
  1074. }
  1075. if ($quiz->review & 1) {
  1076. $review += QUIZ_REVIEW_CLOSED;
  1077. }
  1078. if ($quiz->review & 2) {
  1079. $review += QUIZ_REVIEW_OPEN;
  1080. }
  1081. $quiz->review = $review;
  1082. //The structure is equal to the db, so insert the quiz
  1083. $newid = insert_record ("quiz",$quiz);
  1084. //Do some output
  1085. if (!defined('RESTORE_SILENTLY')) {
  1086. echo "<li>".get_string("modulename","quiz")." \"".format_string(stripslashes($quiz->name),true)."\"</li>";
  1087. }
  1088. backup_flush(300);
  1089. if ($newid) {
  1090. //We have the newid, update backup_ids
  1091. backup_putid($restore->backup_unique_code,$mod->modtype,
  1092. $mod->id, $newid);
  1093. //We have to restore the quiz_question_instances now (old quiz_question_grades, course level)
  1094. $status = quiz_question_instances_restore_pre15_mods($newid,$info,$restore);
  1095. //We have to restore the question_versions now (course level table)
  1096. $status = quiz_question_versions_restore_pre15_mods($newid,$info,$restore);
  1097. //Now check if want to restore user data and do it.
  1098. if (restore_userdata_selected($restore,'quiz',$mod->id)) {
  1099. //Restore quiz_attempts
  1100. $status = quiz_attempts_restore_pre15_mods ($newid,$info,$restore, $quiz->questions);
  1101. if ($status) {
  1102. //Restore quiz_grades
  1103. $status = quiz_grades_restore_pre15_mods ($newid,$info,$restore);
  1104. }
  1105. }
  1106. } else {
  1107. $status = false;
  1108. }
  1109. } else {
  1110. $status = false;
  1111. }
  1112. return $status;
  1113. }
  1114. //This function restores the quiz_question_instances (old quiz_question_grades)
  1115. function quiz_question_instances_restore_pre15_mods($quiz_id,$info,$restore) {
  1116. global $CFG;
  1117. $status = true;
  1118. //Get the quiz_question_grades array
  1119. $grades = $info['MOD']['#']['QUESTION_GRADES']['0']['#']['QUESTION_GRADE'];
  1120. //Iterate over question_grades
  1121. for($i = 0; $i < sizeof($grades); $i++) {
  1122. $gra_info = $grades[$i];
  1123. //traverse_xmlize($gra_info); //Debug
  1124. //print_object ($GLOBALS['traverse_array']); //Debug
  1125. //$GLOBALS['traverse_array']=""; //Debug
  1126. //We'll need this later!!
  1127. $oldid = backup_todb($gra_info['#']['ID']['0']['#']);
  1128. //Now, build the QUESTION_GRADES record structure
  1129. $grade->quiz = $quiz_id;
  1130. $grade->question = backup_todb($gra_info['#']['QUESTION']['0']['#']);
  1131. $grade->grade = backup_todb($gra_info['#']['GRADE']['0']['#']);
  1132. //We have to recode the question field
  1133. $question = backup_getid($restore->backup_unique_code,"question",$grade->question);
  1134. if ($question) {
  1135. $grade->question = $question->new_id;
  1136. }
  1137. //The structure is equal to the db, so insert the quiz_question_grades
  1138. $newid = insert_record ("quiz_question_instances",$grade);
  1139. //Do some output
  1140. if (($i+1) % 10 == 0) {
  1141. if (!defined('RESTORE_SILENTLY')) {
  1142. echo ".";
  1143. if (($i+1) % 200 == 0) {
  1144. echo "<br />";
  1145. }
  1146. }
  1147. backup_flush(300);
  1148. }
  1149. if ($newid) {
  1150. //We have the newid, update backup_ids
  1151. backup_putid($restore->backup_unique_code,"quiz_question_instances",$oldid,
  1152. $newid);
  1153. } else {
  1154. $status = false;
  1155. }
  1156. }
  1157. return $status;
  1158. }
  1159. //This function restores the quiz_question_versions
  1160. function quiz_question_versions_restore_pre15_mods($quiz_id,$info,$restore) {
  1161. global $CFG, $USER;
  1162. $status = true;
  1163. //Get the quiz_question_versions array
  1164. $versions = $info['MOD']['#']['QUESTION_VERSIONS']['0']['#']['QUESTION_VERSION'];
  1165. //Iterate over question_versions
  1166. for($i = 0; $i < sizeof($versions); $i++) {
  1167. $ver_info = $versions[$i];
  1168. //traverse_xmlize($ver_info); //Debug
  1169. //print_object ($GLOBALS['traverse_array']); //Debug
  1170. //$GLOBALS['traverse_array']=""; //Debug
  1171. //We'll need this later!!
  1172. $oldid = backup_todb($ver_info['#']['ID']['0']['#']);
  1173. //Now, build the QUESTION_VERSIONS record structure
  1174. $version->quiz = $quiz_id;
  1175. $version->oldquestion = backup_todb($ver_info['#']['OLDQUESTION']['0']['#']);
  1176. $version->newquestion = backup_todb($ver_info['#']['NEWQUESTION']['0']['#']);
  1177. $version->userid = backup_todb($ver_info['#']['USERID']['0']['#']);
  1178. $version->timestamp = backup_todb($ver_info['#']['TIMESTAMP']['0']['#']);
  1179. //We have to recode the oldquestion field
  1180. $question = backup_getid($restore->backup_unique_code,"question",$version->oldquestion);
  1181. if ($question) {
  1182. $version->oldquestion = $question->new_id;
  1183. }
  1184. //We have to recode the newquestion field
  1185. $question = backup_getid($restore->backup_unique_code,"question",$version->newquestion);
  1186. if ($question) {
  1187. $version->newquestion = $question->new_id;
  1188. }
  1189. //We have to recode the userid field
  1190. $user = backup_getid($restore->backup_unique_code,"user",$version->userid);
  1191. if ($user) {
  1192. $version->userid = $user->new_id;
  1193. } else { //Assign to current user
  1194. $version->userid = $USER->id;
  1195. }
  1196. //The structure is equal to the db, so insert the quiz_question_versions
  1197. $newid = insert_record ("quiz_question_versions",$version);
  1198. //Do some output
  1199. if (($i+1) % 10 == 0) {
  1200. if (!defined('RESTORE_SILENTLY')) {
  1201. echo ".";
  1202. if (($i+1) % 200 == 0) {
  1203. echo "<br />";
  1204. }
  1205. }
  1206. backup_flush(300);
  1207. }
  1208. if ($newid) {
  1209. //We have the newid, update backup_ids
  1210. backup_putid($restore->backup_unique_code,"quiz_question_versions",$oldid,
  1211. $newid);
  1212. } else {
  1213. $status = false;
  1214. }
  1215. }
  1216. return $status;
  1217. }
  1218. //This function restores the quiz_attempts
  1219. function quiz_attempts_restore_pre15_mods($quiz_id,$info,$restore,$quizquestions) {
  1220. global $CFG;
  1221. $status = true;
  1222. //Get the quiz_attempts array
  1223. $attempts = $info['MOD']['#']['ATTEMPTS']['0']['#']['ATTEMPT'];
  1224. //Iterate over attempts
  1225. for($i = 0; $i < sizeof($attempts); $i++) {
  1226. $att_info = $attempts[$i];
  1227. //traverse_xmlize($att_info); //Debug
  1228. //print_object ($GLOBALS['traverse_array']); //Debug
  1229. //$GLOBALS['traverse_array']=""; //Debug
  1230. //We'll need this later!!
  1231. $oldid = backup_todb($att_info['#']['ID']['0']['#']);
  1232. $olduserid = backup_todb($att_info['#']['USERID']['0']['#']);
  1233. //Now, build the ATTEMPTS record structure
  1234. $attempt->quiz = $quiz_id;
  1235. $attempt->userid = backup_todb($att_info['#']['USERID']['0']['#']);
  1236. $attempt->attempt = backup_todb($att_info['#']['ATTEMPTNUM']['0']['#']);
  1237. $attempt->sumgrades = backup_todb($att_info['#']['SUMGRADES']['0']['#']);
  1238. $attempt->timestart = backup_todb($att_info['#']['TIMESTART']['0']['#']);
  1239. $attempt->timefinish = backup_todb($att_info['#']['TIMEFINISH']['0']['#']);
  1240. $attempt->timemodified = backup_todb($att_info['#']['TIMEMODIFIED']['0']['#']);
  1241. //We have to recode the userid field
  1242. $user = backup_getid($restore->backup_unique_code,"user",$attempt->userid);
  1243. if ($user) {
  1244. $attempt->userid = $user->new_id;
  1245. }
  1246. //Set the layout field (inherited from quiz by default)
  1247. $attempt->layout = $quizquestions;
  1248. //Set the preview field (code from upgrade)
  1249. $cm = get_coursemodule_from_instance('quiz', $quiz_id);
  1250. if (has_capability('mod/quiz:preview', get_context_instance(CONTEXT_MODULE, $cm->id))) {
  1251. $attempt->preview = 1;
  1252. }
  1253. //Set the uniqueid field
  1254. $attempt->uniqueid = question_new_attempt_uniqueid();
  1255. //The structure is equal to the db, so insert the quiz_attempts
  1256. $newid = insert_record ("quiz_attempts",$attempt);
  1257. //Do some output
  1258. if (($i+1) % 10 == 0) {
  1259. if (!defined('RESTORE_SILENTLY')) {
  1260. echo ".";
  1261. if (($i+1) % 200 == 0) {
  1262. echo "<br />";
  1263. }
  1264. }
  1265. backup_flush(300);
  1266. }
  1267. if ($newid) {
  1268. //We have the newid, update backup_ids
  1269. backup_putid($restore->backup_unique_code,"quiz_attempts",$oldid,
  1270. $newid);
  1271. //Now process question_states (old quiz_responses table)
  1272. $status = question_states_restore_pre15_mods($newid,$att_info,$restore);
  1273. } else {
  1274. $status = false;
  1275. }
  1276. }
  1277. return $status;
  1278. }
  1279. //This function restores the question_states (old quiz_responses)
  1280. function question_states_restore_pre15_mods($attempt_id,$info,$restore) {
  1281. global $CFG;
  1282. $status = true;
  1283. //Get the quiz_responses array
  1284. $responses = $info['#']['RESPONSES']['0']['#']['RESPONSE'];
  1285. //Iterate over responses
  1286. for($i = 0; $i < sizeof($responses); $i++) {
  1287. $res_info = $responses[$i];
  1288. //traverse_xmlize($res_info); //Debug
  1289. //print_object ($GLOBALS['traverse_array']); //Debug
  1290. //$GLOBALS['traverse_array']=""; //Debug
  1291. //We'll need this later!!
  1292. $oldid = backup_todb($res_info['#']['ID']['0']['#']);
  1293. //Now, build the RESPONSES record structure
  1294. $response->attempt = $attempt_id;
  1295. $response->question = backup_todb($res_info['#']['QUESTION']['0']['#']);
  1296. $response->originalquestion = backup_todb($res_info['#']['ORIGINALQUESTION']['0']['#']);
  1297. $response->answer = backup_todb($res_info['#']['ANSWER']['0']['#']);
  1298. $response->grade = backup_todb($res_info['#']['GRADE']['0']['#']);
  1299. //We have to recode the question field
  1300. $question = backup_getid($restore->backup_unique_code,"question",$response->question);
  1301. if ($question) {
  1302. $response->question = $question->new_id;
  1303. }
  1304. //We have to recode the originalquestion field
  1305. $question = backup_getid($restore->backup_unique_code,"question",$response->originalquestion);
  1306. if ($question) {
  1307. $response->originalquestion = $question->new_id;
  1308. }
  1309. //Set the raw_grade field (default to the existing grade one, no penalty in pre15 backups)
  1310. $response->raw_grade = $response->grade;
  1311. //We have to recode the answer field
  1312. //It depends of the question type !!
  1313. //We get the question first
  1314. $question = get_record("question","id",$response->question);
  1315. //It exists
  1316. if ($question) {
  1317. //Depending of the qtype, we make different recodes
  1318. switch ($question->qtype) {
  1319. case 1: //SHORTANSWER QTYPE
  1320. //Nothing to do. The response is a text.
  1321. break;
  1322. case 2: //TRUEFALSE QTYPE
  1323. //The answer is one answer id. We must recode it
  1324. $answer = backup_getid($restore->backup_unique_code,"question_answers",$response->answer);
  1325. if ($answer) {
  1326. $response->answer = $answer->new_id;
  1327. }
  1328. break;
  1329. case 3: //MULTICHOICE QTYPE
  1330. //The answer is a comma separated list of answers. We must recode them
  1331. $answer_field = "";
  1332. $in_first = true;
  1333. $tok = strtok($response->answer,",");
  1334. while ($tok) {
  1335. //Get the answer from backup_ids
  1336. $answer = backup_getid($restore->backup_unique_code,"question_answers",$tok);
  1337. if ($answer) {
  1338. if ($in_first) {
  1339. $answer_field .= $answer->new_id;
  1340. $in_first = false;
  1341. } else {
  1342. $answer_field .= ",".$answer->new_id;
  1343. }
  1344. }
  1345. //check for next
  1346. $tok = strtok(",");
  1347. }
  1348. $response->answer = $answer_field;
  1349. break;
  1350. case 4: //RANDOM QTYPE
  1351. //The answer links to another question id, we must recode it
  1352. $answer_link = backup_getid($restore->backup_unique_code,"question",$response->answer);
  1353. if ($answer_link) {
  1354. $response->answer = $answer_link->new_id;
  1355. }
  1356. break;
  1357. case 5: //MATCH QTYPE
  1358. //The answer is a comma separated list of hypen separated math_subs (for question and answer)
  1359. $answer_field = "";
  1360. $in_first = true;
  1361. $tok = strtok($response->answer,",");
  1362. while ($tok) {
  1363. //Extract the match_sub for the question and the answer
  1364. $exploded = explode("-",$tok);
  1365. $match_question_id = $exploded[0];
  1366. $match_answer_id = $exploded[1];
  1367. //Get the match_sub from backup_ids (for the question)
  1368. $match_que = backup_getid($restore->backup_unique_code,"question_match_sub",$match_question_id);
  1369. //Get the match_sub from backup_ids (for the answer)
  1370. $match_ans = backup_getid($restore->backup_unique_code,"question_match_sub",$match_answer_id);
  1371. if ($match_que) {
  1372. //It the question hasn't response, it must be 0
  1373. if (!$match_ans and $match_answer_id == 0) {
  1374. $match_ans->new_id = 0;
  1375. }
  1376. if ($in_first) {
  1377. $answer_field .= $match_que->new_id."-".$match_ans->new_id;
  1378. $in_first = false;
  1379. } else {
  1380. $answer_field .= ",".$match_que->new_id."-".$match_ans->new_id;
  1381. }
  1382. }
  1383. //check for next
  1384. $tok = strtok(",");
  1385. }
  1386. $response->answer = $answer_field;
  1387. break;
  1388. case 6: //RANDOMSAMATCH QTYPE
  1389. //The answer is a comma separated list of hypen separated question_id and answer_id. We must recode them
  1390. $answer_field = "";
  1391. $in_first = true;
  1392. $tok = strtok($response->answer,",");
  1393. while ($tok) {
  1394. //Extract the question_id and the answer_id
  1395. $exploded = explode("-",$tok);
  1396. $question_id = $exploded[0];
  1397. $answer_id = $exploded[1];
  1398. //Get the question from backup_ids
  1399. $que = backup_getid($restore->backup_unique_code,"question",$question_id);
  1400. //Get the answer from backup_ids
  1401. $ans = backup_getid($restore->backup_unique_code,"question_answers",$answer_id);
  1402. if ($que) {
  1403. //It the question hasn't response, it must be 0
  1404. if (!$ans and $answer_id == 0) {
  1405. $ans->new_id = 0;
  1406. }
  1407. if ($in_first) {
  1408. $answer_field .= $que->new_id."-".$ans->new_id;
  1409. $in_first = false;
  1410. } else {
  1411. $answer_field .= ",".$que->new_id."-".$ans->new_id;
  1412. }
  1413. }
  1414. //check for next
  1415. $tok = strtok(",");
  1416. }
  1417. $response->answer = $answer_field;
  1418. break;
  1419. case 7: //DESCRIPTION QTYPE
  1420. //Nothing to do (there is no awser to this qtype)
  1421. //But this case must exist !!
  1422. break;
  1423. case 8: //NUMERICAL QTYPE
  1424. //Nothing to do. The response is a text.
  1425. break;
  1426. case 9: //MULTIANSWER QTYPE
  1427. //The answer is a comma separated list of hypen separated multianswer ids and answers. We must recode them.
  1428. //We need to have the sequence of questions here to be able to detect qtypes
  1429. $multianswerdb = get_record('question_multianswer','question',$response->question);
  1430. //Make an array of sequence to easy access
  1431. $sequencearr = explode(",",$multianswerdb->sequence);
  1432. $answer_field = "";
  1433. $in_first = true;
  1434. $tok = strtok($response->answer,",");
  1435. $counter = 1;
  1436. while ($tok) {
  1437. //Extract the multianswer_id and the answer
  1438. $exploded = explode("-",$tok);
  1439. $multianswer_id = $exploded[0];
  1440. $answer = $exploded[1];
  1441. //Get position key (if it fails, next iteration)
  1442. if ($oldposrec = backup_getid($restore->backup_unique_code,'multianswer-pos',$multianswer_id)) {
  1443. $positionkey = $oldposrec->new_id;
  1444. } else {
  1445. //Next iteration
  1446. $tok = strtok(",");
  1447. continue;
  1448. }
  1449. //Calculate question type
  1450. $questiondb = get_record('question','id',$sequencearr[$counter-1]);
  1451. $questiontype = $questiondb->qtype;
  1452. //Now, depending of the answertype field in question_multianswer
  1453. //we do diferent things
  1454. if ($questiontype == "1") {
  1455. //Shortanswer
  1456. //The answer is text, do nothing
  1457. } else if ($questiontype == "3") {
  1458. //Multichoice
  1459. //The answer is an answer_id, look for it in backup_ids
  1460. $ans = backup_getid($restore->backup_unique_code,"question_answers",$answer);
  1461. $answer = $ans->new_id;
  1462. } else if ($questiontype == "8") {
  1463. //Numeric
  1464. //The answer is text, do nothing
  1465. }
  1466. //Finaly, build the new answer field for each pair
  1467. if ($in_first) {
  1468. $answer_field .= $positionkey."-".$answer;
  1469. $in_first = false;
  1470. } else {
  1471. $answer_field .= ",".$positionkey."-".$answer;
  1472. }
  1473. //check for next
  1474. $tok = strtok(",");
  1475. $counter++;
  1476. }
  1477. $response->answer = $answer_field;
  1478. break;
  1479. case 10: //CALCULATED QTYPE
  1480. //Nothing to do. The response is a text.
  1481. break;
  1482. default: //UNMATCHED QTYPE.
  1483. //This is an error (unimplemented qtype)
  1484. $status = false;
  1485. break;
  1486. }
  1487. } else {
  1488. $status = false;
  1489. }
  1490. //The structure is equal to the db, so insert the question_states
  1491. $newid = insert_record ("question_states",$response);
  1492. //Do some output
  1493. if (($i+1) % 10 == 0) {
  1494. if (!defined('RESTORE_SILENTLY')) {
  1495. echo ".";
  1496. if (($i+1) % 200 == 0) {
  1497. echo "<br />";
  1498. }
  1499. }
  1500. backup_flush(300);
  1501. }
  1502. if ($newid) {
  1503. //We have the newid, update backup_ids
  1504. backup_putid($restore->backup_unique_code,"question_states",$oldid,
  1505. $newid);
  1506. } else {
  1507. $status = false;
  1508. }
  1509. }
  1510. return $status;
  1511. }
  1512. //This function restores the quiz_grades
  1513. function quiz_grades_restore_pre15_mods($quiz_id,$info,$restore) {
  1514. global $CFG;
  1515. $status = true;
  1516. //Get the quiz_grades array
  1517. $grades = $info['MOD']['#']['GRADES']['0']['#']['GRADE'];
  1518. //Iterate over grades
  1519. for($i = 0; $i < sizeof($grades); $i++) {
  1520. $gra_info = $grades[$i];
  1521. //traverse_xmlize($gra_info); //Debug
  1522. //print_object ($GLOBALS['traverse_array']); //Debug
  1523. //$GLOBALS['traverse_array']=""; //Debug
  1524. //We'll need this later!!
  1525. $oldid = backup_todb($gra_info['#']['ID']['0']['#']);
  1526. $olduserid = backup_todb($gra_info['#']['USERID']['0']['#']);
  1527. //Now, build the GRADES record structure
  1528. $grade->quiz = $quiz_id;
  1529. $grade->userid = backup_todb($gra_info['#']['USERID']['0']['#']);
  1530. $grade->grade = backup_todb($gra_info['#']['GRADEVAL']['0']['#']);
  1531. $grade->timemodified = backup_todb($gra_info['#']['TIMEMODIFIED']['0']['#']);
  1532. //We have to recode the userid field
  1533. $user = backup_getid($restore->backup_unique_code,"user",$grade->userid);
  1534. if ($user) {
  1535. $grade->userid = $user->new_id;
  1536. }
  1537. //The structure is equal to the db, so insert the quiz_grades
  1538. $newid = insert_record ("quiz_grades",$grade);
  1539. //Do some output
  1540. if (($i+1) % 10 == 0) {
  1541. if (!defined('RESTORE_SILENTLY')) {
  1542. echo ".";
  1543. if (($i+1) % 200 == 0) {
  1544. echo "<br />";
  1545. }
  1546. }
  1547. backup_flush(300);
  1548. }
  1549. if ($newid) {
  1550. //We have the newid, update backup_ids
  1551. backup_putid($restore->backup_unique_code,"quiz_grades",$oldid,
  1552. $newid);
  1553. } else {
  1554. $status = false;
  1555. }
  1556. }
  1557. return $status;
  1558. }
  1559. //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
  1560. //some texts in the module
  1561. function quiz_restore_pre15_wiki2markdown ($restore) {
  1562. global $CFG;
  1563. $status = true;
  1564. //Convert question->questiontext
  1565. if ($records = get_records_sql ("SELECT q.id, q.questiontext, q.questiontextformat
  1566. FROM {$CFG->prefix}question q,
  1567. {$CFG->prefix}backup_ids b
  1568. WHERE b.backup_code = $restore->backup_unique_code AND
  1569. b.table_name = 'question' AND
  1570. q.id = b.new_id AND
  1571. q.questiontextformat = ".FORMAT_WIKI)) {
  1572. foreach ($records as $record) {
  1573. //Rebuild wiki links
  1574. $record->questiontext = restore_decode_wiki_content($record->questiontext, $restore);
  1575. //Convert to Markdown
  1576. $wtm = new WikiToMarkdown();
  1577. $record->questiontext = $wtm->convert($record->questiontext, $restore->course_id);
  1578. $record->questiontextformat = FORMAT_MARKDOWN;
  1579. $status = update_record('question', addslashes_object($record));
  1580. //Do some output
  1581. $i++;
  1582. if (($i+1) % 1 == 0) {
  1583. if (!defined('RESTORE_SILENTLY')) {
  1584. echo ".";
  1585. if (($i+1) % 20 == 0) {
  1586. echo "<br />";
  1587. }
  1588. }
  1589. backup_flush(300);
  1590. }
  1591. }
  1592. }
  1593. return $status;
  1594. }
  1595. //This function returns a log record with all the necessay transformations
  1596. //done. It's used by restore_log_module() to restore modules log.
  1597. function quiz_restore_pre15_logs($restore,$log) {
  1598. $status = false;
  1599. //Depending of the action, we recode different things
  1600. switch ($log->action) {
  1601. case "add":
  1602. if ($log->cmid) {
  1603. //Get the new_id of the module (to recode the info field)
  1604. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  1605. if ($mod) {
  1606. $log->url = "view.php?id=".$log->cmid;
  1607. $log->info = $mod->new_id;
  1608. $status = true;
  1609. }
  1610. }
  1611. break;
  1612. case "update":
  1613. if ($log->cmid) {
  1614. //Get the new_id of the module (to recode the info field)
  1615. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  1616. if ($mod) {
  1617. $log->url = "view.php?id=".$log->cmid;
  1618. $log->info = $mod->new_id;
  1619. $status = true;
  1620. }
  1621. }
  1622. break;
  1623. case "view":
  1624. if ($log->cmid) {
  1625. //Get the new_id of the module (to recode the info field)
  1626. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  1627. if ($mod) {
  1628. $log->url = "view.php?id=".$log->cmid;
  1629. $log->info = $mod->new_id;
  1630. $status = true;
  1631. }
  1632. }
  1633. break;
  1634. case "view all":
  1635. $log->url = "index.php?id=".$log->course;
  1636. $status = true;
  1637. break;
  1638. case "report":
  1639. if ($log->cmid) {
  1640. //Get the new_id of the module (to recode the info field)
  1641. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  1642. if ($mod) {
  1643. $log->url = "report.php?id=".$log->cmid;
  1644. $log->info = $mod->new_id;
  1645. $status = true;
  1646. }
  1647. }
  1648. break;
  1649. case "attempt":
  1650. if ($log->cmid) {
  1651. //Get the new_id of the module (to recode the info field)
  1652. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  1653. if ($mod) {
  1654. //Extract the attempt id from the url field
  1655. $attid = substr(strrchr($log->url,"="),1);
  1656. //Get the new_id of the attempt (to recode the url field)
  1657. $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
  1658. if ($att) {
  1659. $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
  1660. $log->info = $mod->new_id;
  1661. $status = true;
  1662. }
  1663. }
  1664. }
  1665. break;
  1666. case "submit":
  1667. if ($log->cmid) {
  1668. //Get the new_id of the module (to recode the info field)
  1669. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  1670. if ($mod) {
  1671. //Extract the attempt id from the url field
  1672. $attid = substr(strrchr($log->url,"="),1);
  1673. //Get the new_id of the attempt (to recode the url field)
  1674. $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
  1675. if ($att) {
  1676. $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
  1677. $log->info = $mod->new_id;
  1678. $status = true;
  1679. }
  1680. }
  1681. }
  1682. break;
  1683. case "review":
  1684. if ($log->cmid) {
  1685. //Get the new_id of the module (to recode the info field)
  1686. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  1687. if ($mod) {
  1688. //Extract the attempt id from the url field
  1689. $attid = substr(strrchr($log->url,"="),1);
  1690. //Get the new_id of the attempt (to recode the url field)
  1691. $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
  1692. if ($att) {
  1693. $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
  1694. $log->info = $mod->new_id;
  1695. $status = true;
  1696. }
  1697. }
  1698. }
  1699. break;
  1700. case "editquestions":
  1701. if ($log->cmid) {
  1702. //Get the new_id of the module (to recode the url field)
  1703. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  1704. if ($mod) {
  1705. $log->url = "view.php?id=".$log->cmid;
  1706. $log->info = $mod->new_id;
  1707. $status = true;
  1708. }
  1709. }
  1710. break;
  1711. default:
  1712. if (!defined('RESTORE_SILENTLY')) {
  1713. echo "action (".$log->module."-".$log->action.") unknow. Not restored<br />"; //Debug
  1714. }
  1715. break;
  1716. }
  1717. if ($status) {
  1718. $status = $log;
  1719. }
  1720. return $status;
  1721. }
  1722. ?>