PageRenderTime 84ms 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

Large files files are truncated, but you can click here to view the full file

  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) {

Large files files are truncated, but you can click here to view the full file