mirrored from git://git.moodle.org/moodle.git
/
restorelib.php
1213 lines (1020 loc) · 53 KB
/
restorelib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?PHP //$Id$
//This php script contains all the stuff to backup/restore
//quiz mods
//To see, put your terminal to 132cc
//This is the "graphical" structure of the quiz mod:
//
// quiz quiz_categories
// (CL,pk->id) (CL,pk->id)
// | |
// ----------------------------------------------- |
// | | | |
// | | | |
// | | | |
// quiz_attempts quiz_grades quiz_question_grades |
// (UL,pk->id, fk->quiz) (UL,pk->id,fk->quiz) (CL,pk->id,fk->quiz) |
// | | |
// | | |
// | | |
// quiz_responses | quiz_questions
// (UL,pk->id, fk->attempt)----------------------------------------------------(CL,pk->id,fk->category,files)
// |
// |
// |
// --------------------------------------------------------------------------------------
// | | | | | |
// | | | | | |
// | | | | | | quiz_randomsamatch
// quiz_truefalse | quiz_multichoice | quiz_multianswer |--(CL,pl->id,fk->question)
// (CL,pl->id,fk->question) | (CL,pl->id,fk->question) | (CL,pl->id,fk->question) |
// . | . | . |
// . quiz_shortanswer . quiz_numerical . |
// . (CL,pl->id,fk->question) . (CL,pl->id,fk->question) . | quiz_match
// . . . . . |--(CL,pl->id,fk->question)
// . . . . . | .
// . . . . . | .
// . . . . . | .
// . . . . . | quiz_match_sub
// . . . . . |--(CL,pl->id,fk->question)
// ............................................................. |
// . |
// . |
// . |
// quiz_answers |
// (CL,pk->id,fk->question)----------------------------------
//
// Meaning: pk->primary key field of the table
// fk->foreign key to link with parent
// nt->nested field (recursive data)
// CL->course level info
// UL->user level info
// files->table may have files
//
//-----------------------------------------------------------
//This module is special, because we make the restore in two steps:
// 1.-We restore every category and their questions (complete structure). It includes this tables:
// - quiz_categories
// - quiz_questions
// - quiz_truefalse
// - quiz_shortanswer
// - quiz_multianswer
// - quiz_multichoice
// - quiz_numerical
// - quiz_randomsamatch
// - quiz_match
// - quiz_match_sub
// - quiz_answers
// All this backup info have its own section in moodle.xml (QUESTION_CATEGORIES) and it's generated
// before every module backup standard invocation. And only if to restore quizzes has been selected !!
// It's invoked with quiz_restore_question_categories. (course independent).
// 2.-Standard module restore (Invoked via quiz_restore_mods). It includes this tables:
// - quiz
// - quiz_question_grades
// - quiz_attempts
// - quiz_grades
// - quiz_responses
// This step is the standard mod backup. (course dependent).
//STEP 1. Restore categories/questions and associated structures
// (course independent)
function quiz_restore_question_categories($category,$restore) {
global $CFG;
$status = true;
//Get record from backup_ids
$data = backup_getid($restore->backup_unique_code,"quiz_categories",$category->id);
if ($data) {
//Now get completed xmlized object
$info = $data->info;
//traverse_xmlize($info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//Now, build the QUIZ_CATEGORIES record structure
$quiz_cat->course = $restore->course_id;
$quiz_cat->name = backup_todb($info['QUESTION_CATEGORY']['#']['NAME']['0']['#']);
$quiz_cat->info = backup_todb($info['QUESTION_CATEGORY']['#']['INFO']['0']['#']);
$quiz_cat->publish = backup_todb($info['QUESTION_CATEGORY']['#']['PUBLISH']['0']['#']);
//Now search it that category exists !!
$cat = get_record("quiz_categories","name",$quiz_cat->name);
//Create it if doesn't exists
if (!$cat) {
//The structure is equal to the db, so insert the quiz_categories
$newid = insert_record ("quiz_categories",$quiz_cat);
} else {
//Exists, so get its id
$newid = $cat->id;
//If the existing category course and $restore->course_id are different,
//we must publish the existing category
if ($cat->course != $restore->course_id and $cat->publish == 0) {
$cat->publish = 1;
update_record("quiz_categories",$cat);
}
}
//Do some output
echo "<ul><li>".get_string("category")." \"".$quiz_cat->name."\"<br>";
backup_flush(300);
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,"quiz_categories",
$category->id, $newid);
//Now restore quiz_questions
$status = quiz_restore_questions ($category->id, $newid,$info,$restore);
} else {
$status = false;
}
//Finalize ul
echo "</ul>";
}
return $status;
}
function quiz_restore_questions ($old_category_id,$new_category_id,$info,$restore) {
global $CFG;
$status = true;
//Get the questions array
$questions = $info['QUESTION_CATEGORY']['#']['QUESTIONS']['0']['#']['QUESTION'];
//Iterate over questions
for($i = 0; $i < sizeof($questions); $i++) {
$que_info = $questions[$i];
//traverse_xmlize($que_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($que_info['#']['ID']['0']['#']);
//Now, build the QUIZ_QUESTIONS record structure
$question->category = $new_category_id;
$question->name = backup_todb($que_info['#']['NAME']['0']['#']);
$question->questiontext = backup_todb($que_info['#']['QUESTIONTEXT']['0']['#']);
$question->image = backup_todb($que_info['#']['IMAGE']['0']['#']);
$question->defaultgrade = backup_todb($que_info['#']['DEFAULTGRADE']['0']['#']);
$question->qtype = backup_todb($que_info['#']['QTYPE']['0']['#']);
//Check if the question exists
//by category,questiontext and qtype
//But do it only if the question type isn't RANDOM (4) or RANDOMSAMATCH (6), because this
//types can have duplicates in the fields I'm searching !!
//I think that some modifications should be done here !!
//Detected by scott dot elliott at mchsi dot com in Bug 84
if ($question->qtype != 4 and $question->qtype != 6) {
$question_exists = get_record ("quiz_questions","category",$question->category,
"questiontext",$question->questiontext,
"qtype",$question->qtype);
} else {
$question_exists = false;
}
//If the question exists, only record its id
if ($question_exists) {
$newid = $question_exists->id;
$creatingnewquestion = false;
//Else, create a new question
} else {
//The structure is equal to the db, so insert the quiz_questions
$newid = insert_record ("quiz_questions",$question);
$creatingnewquestion = true;
}
//Do some output
if (($i+1) % 2 == 0) {
echo ".";
if (($i+1) % 40 == 0) {
echo "<br>";
}
backup_flush(300);
}
//Save newid to backup tables
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,"quiz_questions",$oldid,
$newid);
}
//If it's a new question in the DB, restore it
if ($creatingnewquestion) {
//Now, restore every quiz_answers in this question
$status = quiz_restore_answers($oldid,$newid,$que_info,$restore);
//Now, depending of the type of questions, invoke different functions
if ($question->qtype == "1") {
$status = quiz_restore_shortanswer($oldid,$newid,$que_info,$restore);
} else if ($question->qtype == "2") {
$status = quiz_restore_truefalse($oldid,$newid,$que_info,$restore);
} else if ($question->qtype == "3") {
$status = quiz_restore_multichoice($oldid,$newid,$que_info,$restore);
} else if ($question->qtype == "4") {
//Random question. Nothing to do.
} else if ($question->qtype == "5") {
$status = quiz_restore_match($oldid,$newid,$que_info,$restore);
} else if ($question->qtype == "6") {
$status = quiz_restore_randomsamatch($oldid,$newid,$que_info,$restore);
} else if ($question->qtype == "7") {
//Description question. Nothing to do.
} else if ($question->qtype == "8") {
$status = quiz_restore_numerical($oldid,$newid,$que_info,$restore);
}
} else {
//We are NOT creating the question, but we need to know every quiz_answers
//map between the XML file and the database to be able to restore the responses
//in each attempt.
$status = quiz_restore_map_answers($oldid,$newid,$que_info,$restore);
//Now, depending of the type of questions, invoke different functions
//to create the necessary mappings in backup_ids, because we are not
//creating the question, but need some records in backup table
if ($question->qtype == "1") {
//Shortanswer question. Nothing to remap
} else if ($question->qtype == "2") {
//Truefalse question. Nothing to remap
} else if ($question->qtype == "3") {
//Multichoice question. Nothing to remap
} else if ($question->qtype == "4") {
//Random question. Nothing to remap
} else if ($question->qtype == "5") {
$status = quiz_restore_map_match($oldid,$newid,$que_info,$restore);
} else if ($question->qtype == "6") {
//Randomsamatch question. Nothing to remap
} else if ($question->qtype == "7") {
//Description question. Nothing to remap
} else if ($question->qtype == "8") {
//Numerical question. Nothing to remap
}
}
}
return $status;
}
function quiz_restore_answers ($old_question_id,$new_question_id,$info,$restore) {
global $CFG;
$status = true;
//Get the answers array
$answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
//Iterate over answers
for($i = 0; $i < sizeof($answers); $i++) {
$ans_info = $answers[$i];
//traverse_xmlize($ans_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($ans_info['#']['ID']['0']['#']);
//Now, build the QUIZ_ANSWERS record structure
$answer->question = $new_question_id;
$answer->answer = backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
$answer->fraction = backup_todb($ans_info['#']['FRACTION']['0']['#']);
$answer->feedback = backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
//The structure is equal to the db, so insert the quiz_answers
$newid = insert_record ("quiz_answers",$answer);
//Do some output
if (($i+1) % 50 == 0) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br>";
}
backup_flush(300);
}
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,"quiz_answers",$oldid,
$newid);
} else {
$status = false;
}
}
return $status;
}
function quiz_restore_map_answers ($old_question_id,$new_question_id,$info,$restore) {
global $CFG;
$status = true;
//Get the answers array
$answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
//Iterate over answers
for($i = 0; $i < sizeof($answers); $i++) {
$ans_info = $answers[$i];
//traverse_xmlize($ans_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($ans_info['#']['ID']['0']['#']);
//Now, build the QUIZ_ANSWERS record structure
$answer->question = $new_question_id;
$answer->answer = backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
$answer->fraction = backup_todb($ans_info['#']['FRACTION']['0']['#']);
$answer->feedback = backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
//If we are in this method is because the question exists in DB, so its
//answers must exist too.
//Now, we are going to look for that answer in DB and to create the
//mappings in backup_ids to use them later where restoring responses (user level).
//Get the answer from DB (by question and answer)
$db_answer = get_record ("quiz_answers","question",$new_question_id,
"answer",$answer->answer);
//Do some output
if (($i+1) % 50 == 0) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br>";
}
backup_flush(300);
}
if ($db_answer) {
//We have the database answer, update backup_ids
backup_putid($restore->backup_unique_code,"quiz_answers",$oldid,
$db_answer->id);
} else {
$status = false;
}
}
return $status;
}
function quiz_restore_shortanswer ($old_question_id,$new_question_id,$info,$restore) {
global $CFG;
$status = true;
//Get the shortanswers array
$shortanswers = $info['#']['SHORTANSWER'];
//Iterate over shortanswers
for($i = 0; $i < sizeof($shortanswers); $i++) {
$sho_info = $shortanswers[$i];
//traverse_xmlize($sho_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//Now, build the QUIZ_SHORTANSWER record structure
$shortanswer->question = $new_question_id;
$shortanswer->answers = backup_todb($sho_info['#']['ANSWERS']['0']['#']);
$shortanswer->usecase = backup_todb($sho_info['#']['USECASE']['0']['#']);
//We have to recode the answers field (a list of answers id)
//Extracts answer id from sequence
$answers_field = "";
$in_first = true;
$tok = strtok($shortanswer->answers,",");
while ($tok) {
//Get the answer from backup_ids
$answer = backup_getid($restore->backup_unique_code,"quiz_answers",$tok);
if ($answer) {
if ($in_first) {
$answers_field .= $answer->new_id;
$in_first = false;
} else {
$answers_field .= ",".$answer->new_id;
}
}
//check for next
$tok = strtok(",");
}
//We have the answers field recoded to its new ids
$shortanswer->answers = $answers_field;
//The structure is equal to the db, so insert the quiz_shortanswer
$newid = insert_record ("quiz_shortanswer",$shortanswer);
//Do some output
if (($i+1) % 50 == 0) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br>";
}
backup_flush(300);
}
if (!$newid) {
$status = false;
}
}
return $status;
}
function quiz_restore_truefalse ($old_question_id,$new_question_id,$info,$restore) {
global $CFG;
$status = true;
//Get the truefalse array
$truefalses = $info['#']['TRUEFALSE'];
//Iterate over truefalse
for($i = 0; $i < sizeof($truefalses); $i++) {
$tru_info = $truefalses[$i];
//traverse_xmlize($tru_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//Now, build the QUIZ_TRUEFALSE record structure
$truefalse->question = $new_question_id;
$truefalse->trueanswer = backup_todb($tru_info['#']['TRUEANSWER']['0']['#']);
$truefalse->falseanswer = backup_todb($tru_info['#']['FALSEANSWER']['0']['#']);
////We have to recode the trueanswer field
$answer = backup_getid($restore->backup_unique_code,"quiz_answers",$truefalse->trueanswer);
if ($answer) {
$truefalse->trueanswer = $answer->new_id;
}
////We have to recode the falseanswer field
$answer = backup_getid($restore->backup_unique_code,"quiz_answers",$truefalse->falseanswer);
if ($answer) {
$truefalse->falseanswer = $answer->new_id;
}
//The structure is equal to the db, so insert the quiz_truefalse
$newid = insert_record ("quiz_truefalse",$truefalse);
//Do some output
if (($i+1) % 50 == 0) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br>";
}
backup_flush(300);
}
if (!$newid) {
$status = false;
}
}
return $status;
}
function quiz_restore_multichoice ($old_question_id,$new_question_id,$info,$restore) {
global $CFG;
$status = true;
//Get the multichoices array
$multichoices = $info['#']['MULTICHOICE'];
//Iterate over multichoices
for($i = 0; $i < sizeof($multichoices); $i++) {
$mul_info = $multichoices[$i];
//traverse_xmlize($mul_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//Now, build the QUIZ_MULTICHOICE record structure
$multichoice->question = $new_question_id;
$multichoice->layout = backup_todb($mul_info['#']['LAYOUT']['0']['#']);
$multichoice->answers = backup_todb($mul_info['#']['ANSWERS']['0']['#']);
$multichoice->single = backup_todb($mul_info['#']['SINGLE']['0']['#']);
//We have to recode the answers field (a list of answers id)
//Extracts answer id from sequence
$answers_field = "";
$in_first = true;
$tok = strtok($multichoice->answers,",");
while ($tok) {
//Get the answer from backup_ids
$answer = backup_getid($restore->backup_unique_code,"quiz_answers",$tok);
if ($answer) {
if ($in_first) {
$answers_field .= $answer->new_id;
$in_first = false;
} else {
$answers_field .= ",".$answer->new_id;
}
}
//check for next
$tok = strtok(",");
}
//We have the answers field recoded to its new ids
$multichoice->answers = $answers_field;
//The structure is equal to the db, so insert the quiz_shortanswer
$newid = insert_record ("quiz_multichoice",$multichoice);
//Do some output
if (($i+1) % 50 == 0) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br>";
}
backup_flush(300);
}
if (!$newid) {
$status = false;
}
}
return $status;
}
function quiz_restore_match ($old_question_id,$new_question_id,$info,$restore) {
global $CFG;
$status = true;
//Get the matchs array
$matchs = $info['#']['MATCHS']['0']['#']['MATCH'];
//We have to build the subquestions field (a list of match_sub id)
$subquestions_field = "";
$in_first = true;
//Iterate over matchs
for($i = 0; $i < sizeof($matchs); $i++) {
$mat_info = $matchs[$i];
//traverse_xmlize($mat_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($mat_info['#']['ID']['0']['#']);
//Now, build the QUIZ_MATCH_SUB record structure
$match_sub->question = $new_question_id;
$match_sub->questiontext = backup_todb($mat_info['#']['QUESTIONTEXT']['0']['#']);
$match_sub->answertext = backup_todb($mat_info['#']['ANSWERTEXT']['0']['#']);
//The structure is equal to the db, so insert the quiz_match_sub
$newid = insert_record ("quiz_match_sub",$match_sub);
//Do some output
if (($i+1) % 50 == 0) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br>";
}
backup_flush(300);
}
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,"quiz_match_sub",$oldid,
$newid);
//We have a new match_sub, append it to subquestions_field
if ($in_first) {
$subquestions_field .= $newid;
$in_first = false;
} else {
$subquestions_field .= ",".$newid;
}
} else {
$status = false;
}
}
//We have created every match_sub, now create the match
$match->question = $new_question_id;
$match->subquestions = $subquestions_field;
//The structure is equal to the db, so insert the quiz_match_sub
$newid = insert_record ("quiz_match",$match);
if (!$newid) {
$status = false;
}
return $status;
}
function quiz_restore_map_match ($old_question_id,$new_question_id,$info,$restore) {
global $CFG;
$status = true;
//Get the matchs array
$matchs = $info['#']['MATCHS']['0']['#']['MATCH'];
//We have to build the subquestions field (a list of match_sub id)
$subquestions_field = "";
$in_first = true;
//Iterate over matchs
for($i = 0; $i < sizeof($matchs); $i++) {
$mat_info = $matchs[$i];
//traverse_xmlize($mat_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($mat_info['#']['ID']['0']['#']);
//Now, build the QUIZ_MATCH_SUB record structure
$match_sub->question = $new_question_id;
$match_sub->questiontext = backup_todb($mat_info['#']['QUESTIONTEXT']['0']['#']);
$match_sub->answertext = backup_todb($mat_info['#']['ANSWERTEXT']['0']['#']);
//If we are in this method is because the question exists in DB, so its
//match_sub must exist too.
//Now, we are going to look for that match_sub in DB and to create the
//mappings in backup_ids to use them later where restoring responses (user level).
//Get the match_sub from DB (by question, questiontext and answertext)
$db_match_sub = get_record ("quiz_match_sub","question",$new_question_id,
"questiontext",$match_sub->questiontext,
"answertext",$match_sub->answertext);
//Do some output
if (($i+1) % 50 == 0) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br>";
}
backup_flush(300);
}
//We have the database match_sub, so update backup_ids
if ($db_match_sub) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,"quiz_match_sub",$oldid,
$db_match_sub->id);
} else {
$status = false;
}
}
return $status;
}
function quiz_restore_randomsamatch ($old_question_id,$new_question_id,$info,$restore) {
global $CFG;
$status = true;
//Get the randomsamatchs array
$randomsamatchs = $info['#']['RANDOMSAMATCH'];
//Iterate over randomsamatchs
for($i = 0; $i < sizeof($randomsamatchs); $i++) {
$ran_info = $randomsamatchs[$i];
//traverse_xmlize($ran_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//Now, build the QUIZ_RANDOMSAMATCH record structure
$randomsamatch->question = $new_question_id;
$randomsamatch->choose = backup_todb($ran_info['#']['CHOOSE']['0']['#']);
//The structure is equal to the db, so insert the quiz_randomsamatch
$newid = insert_record ("quiz_randomsamatch",$randomsamatch);
//Do some output
if (($i+1) % 50 == 0) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br>";
}
backup_flush(300);
}
if (!$newid) {
$status = false;
}
}
return $status;
}
function quiz_restore_numerical ($old_question_id,$new_question_id,$info,$restore) {
global $CFG;
$status = true;
//Get the numerical array
$numericals = $info['#']['NUMERICAL'];
//Iterate over numericals
for($i = 0; $i < sizeof($numericals); $i++) {
$num_info = $numericals[$i];
//traverse_xmlize($num_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//Now, build the QUIZ_NUMERICAL record structure
$numerical->question = $new_question_id;
$numerical->answer = backup_todb($num_info['#']['ANSWER']['0']['#']);
$numerical->min = backup_todb($num_info['#']['MIN']['0']['#']);
$numerical->max = backup_todb($num_info['#']['MAX']['0']['#']);
////We have to recode the answer field
$answer = backup_getid($restore->backup_unique_code,"quiz_answers",$numerical->answer);
if ($answer) {
$numerical->answer = $answer->new_id;
}
//The structure is equal to the db, so insert the quiz_numerical
$newid = insert_record ("quiz_numerical",$numerical);
//Do some output
if (($i+1) % 50 == 0) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br>";
}
backup_flush(300);
}
if (!$newid) {
$status = false;
}
}
return $status;
}
//STEP 2. Restore quizzes and associated structures
// (course dependent)
function quiz_restore_mods($mod,$restore) {
global $CFG;
$status = true;
//Get record from backup_ids
$data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
if ($data) {
//Now get completed xmlized object
$info = $data->info;
//traverse_xmlize($info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//Now, build the QUIZ record structure
$quiz->course = $restore->course_id;
$quiz->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
$quiz->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
$quiz->timeopen = backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
$quiz->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
$quiz->attempts = backup_todb($info['MOD']['#']['ATTEMPTS_NUMBER']['0']['#']);
$quiz->attemptonlast = backup_todb($info['MOD']['#']['ATTEMPTONLAST']['0']['#']);
$quiz->feedback = backup_todb($info['MOD']['#']['FEEDBACK']['0']['#']);
$quiz->correctanswers = backup_todb($info['MOD']['#']['CORRECTANSWERS']['0']['#']);
$quiz->grademethod = backup_todb($info['MOD']['#']['GRADEMETHOD']['0']['#']);
$quiz->review = backup_todb($info['MOD']['#']['REVIEW']['0']['#']);
$quiz->shufflequestions = backup_todb($info['MOD']['#']['SHUFFLEQUESTIONS']['0']['#']);
$quiz->shuffleanswers = backup_todb($info['MOD']['#']['SHUFFLEANSWERS']['0']['#']);
$quiz->questions = backup_todb($info['MOD']['#']['QUESTIONS']['0']['#']);
$quiz->sumgrades = backup_todb($info['MOD']['#']['SUMGRADES']['0']['#']);
$quiz->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
$quiz->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
$quiz->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
//We have to recode the questions field (a list of questions id)
//Extracts question id from sequence
$questions_field = "";
$in_first = true;
$tok = strtok($quiz->questions,",");
while ($tok) {
//Get the question from backup_ids
$question = backup_getid($restore->backup_unique_code,"quiz_questions",$tok);
if ($question) {
if ($in_first) {
$questions_field .= $question->new_id;
$in_first = false;
} else {
$questions_field .= ",".$question->new_id;
}
}
//check for next
$tok = strtok(",");
}
//We have the questions field recoded to its new ids
$quiz->questions = $questions_field;
//The structure is equal to the db, so insert the quiz
$newid = insert_record ("quiz",$quiz);
//Do some output
echo "<ul><li>".get_string("modulename","quiz")." \"".$quiz->name."\"<br>";
backup_flush(300);
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,$mod->modtype,
$mod->id, $newid);
//We have to restore the question_grades now (course level table)
$status = quiz_question_grades_restore_mods($newid,$info,$restore);
//Now check if want to restore user data and do it.
if ($restore->mods[quiz]->userinfo) {
//Restore quiz_attempts
$status = quiz_attempts_restore_mods ($newid,$info,$restore);
if ($status) {
//Restore quiz_grades
$status = quiz_grades_restore_mods ($newid,$info,$restore);
}
}
} else {
$status = false;
}
//Finalize ul
echo "</ul>";
} else {
$status = false;
}
return $status;
}
//This function restores the quiz_question_grades
function quiz_question_grades_restore_mods($quiz_id,$info,$restore) {
global $CFG;
$status = true;
//Get the quiz_question_grades array
$grades = $info['MOD']['#']['QUESTION_GRADES']['0']['#']['QUESTION_GRADE'];
//Iterate over question_grades
for($i = 0; $i < sizeof($grades); $i++) {
$gra_info = $grades[$i];
//traverse_xmlize($gra_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($gra_info['#']['ID']['0']['#']);
//Now, build the QUESTION_GRADES record structure
$grade->quiz = $quiz_id;
$grade->question = backup_todb($gra_info['#']['QUESTION']['0']['#']);
$grade->grade = backup_todb($gra_info['#']['GRADE']['0']['#']);
//We have to recode the question field
$question = backup_getid($restore->backup_unique_code,"quiz_questions",$grade->question);
if ($question) {
$grade->question = $question->new_id;
}
//The structure is equal to the db, so insert the quiz_question_grades
$newid = insert_record ("quiz_question_grades",$grade);
//Do some output
if (($i+1) % 10 == 0) {
echo ".";
if (($i+1) % 200 == 0) {
echo "<br>";
}
backup_flush(300);
}
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,"quiz_question_grades",$oldid,
$newid);
} else {
$status = false;
}
}
return $status;
}
//This function restores the quiz_attempts
function quiz_attempts_restore_mods($quiz_id,$info,$restore) {
global $CFG;
$status = true;
//Get the quiz_attempts array
$attempts = $info['MOD']['#']['ATTEMPTS']['0']['#']['ATTEMPT'];
//Iterate over attempts
for($i = 0; $i < sizeof($attempts); $i++) {
$att_info = $attempts[$i];
//traverse_xmlize($att_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($att_info['#']['ID']['0']['#']);
$olduserid = backup_todb($att_info['#']['USERID']['0']['#']);
//Now, build the ATTEMPTS record structure
$attempt->quiz = $quiz_id;
$attempt->userid = backup_todb($att_info['#']['USERID']['0']['#']);
$attempt->attempt = backup_todb($att_info['#']['ATTEMPTNUM']['0']['#']);
$attempt->sumgrades = backup_todb($att_info['#']['SUMGRADES']['0']['#']);
$attempt->timestart = backup_todb($att_info['#']['TIMESTART']['0']['#']);
$attempt->timefinish = backup_todb($att_info['#']['TIMEFINISH']['0']['#']);
$attempt->timemodified = backup_todb($att_info['#']['TIMEMODIFIED']['0']['#']);
//We have to recode the userid field
$user = backup_getid($restore->backup_unique_code,"user",$attempt->userid);
if ($user) {
$attempt->userid = $user->new_id;
}
//The structure is equal to the db, so insert the quiz_attempts
$newid = insert_record ("quiz_attempts",$attempt);
//Do some output
if (($i+1) % 10 == 0) {
echo ".";
if (($i+1) % 200 == 0) {
echo "<br>";
}
backup_flush(300);
}
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,"quiz_attempts",$oldid,
$newid);
//Now process quiz_responses
$status = quiz_responses_restore_mods($newid,$att_info,$restore);
} else {
$status = false;
}
}
return $status;
}
//This function restores the quiz_responses
function quiz_responses_restore_mods($attempt_id,$info,$restore) {
global $CFG;
$status = true;
//Get the quiz_responses array
$responses = $info['#']['RESPONSES']['0']['#']['RESPONSE'];
//Iterate over responses
for($i = 0; $i < sizeof($responses); $i++) {
$res_info = $responses[$i];
//traverse_xmlize($res_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($res_info['#']['ID']['0']['#']);
//Now, build the RESPONSES record structure
$response->attempt = $attempt_id;
$response->question = backup_todb($res_info['#']['QUESTION']['0']['#']);
$response->answer = backup_todb($res_info['#']['ANSWER']['0']['#']);
$response->grade = backup_todb($res_info['#']['GRADE']['0']['#']);