forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestorelib.php
848 lines (749 loc) · 37.5 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
<?php //$Id$
//This php script contains all the stuff to restore quiz mods
// Todo:
// whereever it says "/// We have to recode the .... field" we should put in a check
// to see if the recoding was successful and throw an appropriate error otherwise
//This is the "graphical" structure of the quiz mod:
//To see, put your terminal to 160cc
//
// quiz
// (CL,pk->id)
// |
// -------------------------------------------------------------------
// | | | |
// | quiz_grades | quiz_question_versions
// | (UL,pk->id,fk->quiz) | (CL,pk->id,fk->quiz)
// | |
// quiz_attempts quiz_question_instances
// (UL,pk->id,fk->quiz) (CL,pk->id,fk->quiz,question)
//
// Meaning: pk->primary key field of the table
// fk->foreign key to link with parent
// nt->nested field (recursive data)
// SL->site level info
// CL->course level info
// UL->user level info
// files->table may have files
//
//-----------------------------------------------------------
// When we restore a quiz we also need to restore the questions and possibly
// the data about student interaction with the questions. The functions to do
// that are included with the following library
include_once("$CFG->dirroot/question/restorelib.php");
function quiz_restore_mods($mod,$restore) {
global $CFG;
$status = true;
//Hook to call Moodle < 1.5 Quiz Restore
if ($restore->backup_version < 2005043000) {
include_once("restorelibpre15.php");
return quiz_restore_pre15_mods($mod,$restore);
}
//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;
//First, we check to "course_id" folder exists and create is as necessary in CFG->dataroot
$dest_dir = $CFG->dataroot."/".$restore->course_id;
check_dir_exists($dest_dir,true);
$file = $dest_dir."/restorelog.html";
$restorelog_file = fopen($file,"a");
//traverse_xmlize($info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//Now, build the QUIZ record structure
$quiz = new stdClass;
$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']['#']);
$date = usergetdate($quiz->timeopen);
fwrite ($restorelog_file,"<br>The Quiz - ".$quiz->name." <br>");
fwrite ($restorelog_file,"The TIMEOPEN was " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."");
$quiz->timeopen += $restore->course_startdateoffset;
$date = usergetdate($quiz->timeopen);
fwrite ($restorelog_file," the TIMEOPEN is now " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."<br>");
$quiz->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
$date = usergetdate($quiz->timeclose);
fwrite ($restorelog_file,"The TIMECLOSE was " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."");
$quiz->timeclose += $restore->course_startdateoffset;
$date = usergetdate($quiz->timeclose);
fwrite ($restorelog_file," the TIMECLOSE is now " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."<br><br>");
$quiz->optionflags = backup_todb($info['MOD']['#']['OPTIONFLAGS']['0']['#']);
$quiz->penaltyscheme = backup_todb($info['MOD']['#']['PENALTYSCHEME']['0']['#']);
$quiz->attempts = backup_todb($info['MOD']['#']['ATTEMPTS_NUMBER']['0']['#']);
$quiz->attemptonlast = backup_todb($info['MOD']['#']['ATTEMPTONLAST']['0']['#']);
$quiz->grademethod = backup_todb($info['MOD']['#']['GRADEMETHOD']['0']['#']);
$quiz->decimalpoints = backup_todb($info['MOD']['#']['DECIMALPOINTS']['0']['#']);
$quiz->review = backup_todb($info['MOD']['#']['REVIEW']['0']['#']);
$quiz->questionsperpage = backup_todb($info['MOD']['#']['QUESTIONSPERPAGE']['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']['#']);
$quiz->timelimit = backup_todb($info['MOD']['#']['TIMELIMIT']['0']['#']);
$quiz->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
$quiz->subnet = backup_todb($info['MOD']['#']['SUBNET']['0']['#']);
$quiz->popup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
$quiz->delay1 = backup_todb($info['MOD']['#']['DELAY1']['0']['#']);
$quiz->delay2 = backup_todb($info['MOD']['#']['DELAY2']['0']['#']);
//We have to recode the questions field (a list of questions id and pagebreaks)
$quiz->questions = quiz_recode_layout($quiz->questions, $restore);
//The structure is equal to the db, so insert the quiz
$newid = insert_record ("quiz",$quiz);
//Do some output
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("modulename","quiz")." \"".format_string(stripslashes($quiz->name),true)."\"</li>";
}
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_instances now (course level table)
$status = quiz_question_instances_restore_mods($newid,$info,$restore);
//We have to restore the feedback now (course level table)
$status = quiz_feedback_restore_mods($newid, $info, $restore, $quiz);
//We have to restore the question_versions now (course level table)
$status = quiz_question_versions_restore_mods($newid,$info,$restore);
//Now check if want to restore user data and do it.
if (restore_userdata_selected($restore,'quiz',$mod->id)) {
//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;
}
} else {
$status = false;
}
return $status;
}
//This function restores the quiz_question_instances
function quiz_question_instances_restore_mods($quiz_id,$info,$restore) {
global $CFG;
$status = true;
//Get the quiz_question_instances array
if (array_key_exists('QUESTION_INSTANCES', $info['MOD']['#'])) {
$instances = $info['MOD']['#']['QUESTION_INSTANCES']['0']['#']['QUESTION_INSTANCE'];
} else {
$instances = array();
}
//Iterate over question_instances
for($i = 0; $i < sizeof($instances); $i++) {
$gra_info = $instances[$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_INSTANCES record structure
$instance = new stdClass;
$instance->quiz = $quiz_id;
$instance->question = backup_todb($gra_info['#']['QUESTION']['0']['#']);
$instance->grade = backup_todb($gra_info['#']['GRADE']['0']['#']);
//We have to recode the question field
$question = backup_getid($restore->backup_unique_code,"question",$instance->question);
if ($question) {
$instance->question = $question->new_id;
}
//The structure is equal to the db, so insert the quiz_question_instances
$newid = insert_record ("quiz_question_instances",$instance);
//Do some output
if (($i+1) % 10 == 0) {
if (!defined('RESTORE_SILENTLY')) {
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_instances",$oldid,
$newid);
} else {
$status = false;
}
}
return $status;
}
//This function restores the quiz_question_instances
function quiz_feedback_restore_mods($quiz_id, $info, $restore, $quiz) {
$status = true;
//Get the quiz_feedback array
if (array_key_exists('FEEDBACKS', $info['MOD']['#'])) {
$feedbacks = $info['MOD']['#']['FEEDBACKS']['0']['#']['FEEDBACK'];
//Iterate over the feedbacks
foreach ($feedbacks as $feedback_info) {
//traverse_xmlize($feedback_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($feedback_info['#']['ID']['0']['#']);
//Now, build the quiz_feedback record structure
$feedback = new stdClass();
$feedback->quizid = $quiz_id;
$feedback->feedbacktext = backup_todb($feedback_info['#']['FEEDBACKTEXT']['0']['#']);
$feedback->mingrade = backup_todb($feedback_info['#']['MINGRADE']['0']['#']);
$feedback->maxgrade = backup_todb($feedback_info['#']['MAXGRADE']['0']['#']);
//The structure is equal to the db, so insert the quiz_question_instances
$newid = insert_record('quiz_feedback', $feedback);
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code, 'quiz_feedback', $oldid, $newid);
} else {
$status = false;
}
}
} else {
$feedback = new stdClass();
$feedback->quizid = $quiz_id;
$feedback->feedbacktext = '';
$feedback->mingrade = 0;
$feedback->maxgrade = $quiz->grade + 1;
insert_record('quiz_feedback', $feedback);
}
return $status;
}
//This function restores the quiz_question_versions
function quiz_question_versions_restore_mods($quiz_id,$info,$restore) {
global $CFG, $USER;
$status = true;
//Get the quiz_question_versions array
if (!empty($info['MOD']['#']['QUESTION_VERSIONS'])) {
$versions = $info['MOD']['#']['QUESTION_VERSIONS']['0']['#']['QUESTION_VERSION'];
} else {
$versions = array();
}
//Iterate over question_versions
for($i = 0; $i < sizeof($versions); $i++) {
$ver_info = $versions[$i];
//traverse_xmlize($ver_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($ver_info['#']['ID']['0']['#']);
//Now, build the QUESTION_VERSIONS record structure
$version = new stdClass;
$version->quiz = $quiz_id;
$version->oldquestion = backup_todb($ver_info['#']['OLDQUESTION']['0']['#']);
$version->newquestion = backup_todb($ver_info['#']['NEWQUESTION']['0']['#']);
$version->originalquestion = backup_todb($ver_info['#']['ORIGINALQUESTION']['0']['#']);
$version->userid = backup_todb($ver_info['#']['USERID']['0']['#']);
$version->timestamp = backup_todb($ver_info['#']['TIMESTAMP']['0']['#']);
//We have to recode the oldquestion field
$question = backup_getid($restore->backup_unique_code,"question",$version->oldquestion);
if ($question) {
$version->oldquestion = $question->new_id;
}
//We have to recode the newquestion field
$question = backup_getid($restore->backup_unique_code,"question",$version->newquestion);
if ($question) {
$version->newquestion = $question->new_id;
}
//We have to recode the originalquestion field
$question = backup_getid($restore->backup_unique_code,"question",$version->originalquestion);
if ($question) {
$version->newquestion = $question->new_id;
}
//We have to recode the userid field
$user = backup_getid($restore->backup_unique_code,"user",$version->userid);
if ($user) {
$version->userid = $user->new_id;
} else { //Assign to current user
$version->userid = $USER->id;
}
//The structure is equal to the db, so insert the quiz_question_versions
$newid = insert_record ("quiz_question_versions",$version);
//Do some output
if (($i+1) % 10 == 0) {
if (!defined('RESTORE_SILENTLY')) {
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_versions",$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 = new stdClass;
$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']['#']);
$attempt->layout = backup_todb($att_info['#']['LAYOUT']['0']['#']);
$attempt->preview = backup_todb($att_info['#']['PREVIEW']['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;
}
//Set the uniqueid field
$attempt->uniqueid = question_new_attempt_uniqueid();
//We have to recode the layout field (a list of questions id and pagebreaks)
$attempt->layout = quiz_recode_layout($attempt->layout, $restore);
//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) {
if (!defined('RESTORE_SILENTLY')) {
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 question_states
// This function is defined in question/restorelib.php
$status = question_states_restore_mods($attempt->uniqueid,$att_info,$restore);
} else {
$status = false;
}
}
return $status;
}
//This function restores the quiz_grades
function quiz_grades_restore_mods($quiz_id,$info,$restore) {
global $CFG;
$status = true;
//Get the quiz_grades array
$grades = $info['MOD']['#']['GRADES']['0']['#']['GRADE'];
//Iterate over 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']['#']);
$olduserid = backup_todb($gra_info['#']['USERID']['0']['#']);
//Now, build the GRADES record structure
$grade = new stdClass;
$grade->quiz = $quiz_id;
$grade->userid = backup_todb($gra_info['#']['USERID']['0']['#']);
$grade->grade = backup_todb($gra_info['#']['GRADEVAL']['0']['#']);
$grade->timemodified = backup_todb($gra_info['#']['TIMEMODIFIED']['0']['#']);
//We have to recode the userid field
$user = backup_getid($restore->backup_unique_code,"user",$grade->userid);
if ($user) {
$grade->userid = $user->new_id;
}
//The structure is equal to the db, so insert the quiz_grades
$newid = insert_record ("quiz_grades",$grade);
//Do some output
if (($i+1) % 10 == 0) {
if (!defined('RESTORE_SILENTLY')) {
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_grades",$oldid,
$newid);
} else {
$status = false;
}
}
return $status;
}
//Return a content decoded to support interactivities linking. Every module
//should have its own. They are called automatically from
//quiz_decode_content_links_caller() function in each module
//in the restore process
function quiz_decode_content_links ($content,$restore) {
global $CFG;
$result = $content;
//Link to the list of quizs
$searchstring='/\$@(QUIZINDEX)\*([0-9]+)@\$/';
//We look for it
preg_match_all($searchstring,$content,$foundset);
//If found, then we are going to look for its new id (in backup tables)
if ($foundset[0]) {
//print_object($foundset); //Debug
//Iterate over foundset[2]. They are the old_ids
foreach($foundset[2] as $old_id) {
//We get the needed variables here (course id)
$rec = backup_getid($restore->backup_unique_code,"course",$old_id);
//Personalize the searchstring
$searchstring='/\$@(QUIZINDEX)\*('.$old_id.')@\$/';
//If it is a link to this course, update the link to its new location
if($rec->new_id) {
//Now replace it
$result= preg_replace($searchstring,$CFG->wwwroot.'/mod/quiz/index.php?id='.$rec->new_id,$result);
} else {
//It's a foreign link so leave it as original
$result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/quiz/index.php?id='.$old_id,$result);
}
}
}
//Link to quiz view by moduleid
$searchstring='/\$@(QUIZVIEWBYID)\*([0-9]+)@\$/';
//We look for it
preg_match_all($searchstring,$result,$foundset);
//If found, then we are going to look for its new id (in backup tables)
if ($foundset[0]) {
//print_object($foundset); //Debug
//Iterate over foundset[2]. They are the old_ids
foreach($foundset[2] as $old_id) {
//We get the needed variables here (course_modules id)
$rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
//Personalize the searchstring
$searchstring='/\$@(QUIZVIEWBYID)\*('.$old_id.')@\$/';
//If it is a link to this course, update the link to its new location
if($rec->new_id) {
//Now replace it
$result= preg_replace($searchstring,$CFG->wwwroot.'/mod/quiz/view.php?id='.$rec->new_id,$result);
} else {
//It's a foreign link so leave it as original
$result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/quiz/view.php?id='.$old_id,$result);
}
}
}
return $result;
}
//This function makes all the necessary calls to xxxx_decode_content_links()
//function in each module, passing them the desired contents to be decoded
//from backup format to destination site/course in order to mantain inter-activities
//working in the backup/restore process. It's called from restore_decode_content_links()
//function in restore process
function quiz_decode_content_links_caller($restore) {
global $CFG;
$status = true;
if ($quizs = get_records_sql ("SELECT q.id, q.intro
FROM {$CFG->prefix}quiz q
WHERE q.course = $restore->course_id")) {
//Iterate over each quiz->intro
$i = 0; //Counter to send some output to the browser to avoid timeouts
foreach ($quizs as $quiz) {
//Increment counter
$i++;
$content = $quiz->intro;
$result = restore_decode_content_links_worker($content,$restore);
if ($result != $content) {
//Update record
$quiz->intro = addslashes($result);
$status = update_record("quiz",$quiz);
if ($CFG->debug>7) {
if (!defined('RESTORE_SILENTLY')) {
echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
}
}
}
//Do some output
if (($i+1) % 5 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if (($i+1) % 100 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
return $status;
}
//This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
//some texts in the module
function quiz_restore_wiki2markdown ($restore) {
global $CFG;
$status = true;
//Convert question->questiontext
if ($records = get_records_sql ("SELECT q.id, q.questiontext, q.questiontextformat
FROM {$CFG->prefix}question q,
{$CFG->prefix}backup_ids b
WHERE b.backup_code = $restore->backup_unique_code AND
b.table_name = 'question' AND
q.id = b.new_id AND
q.questiontextformat = ".FORMAT_WIKI)) {
$i = 0;
foreach ($records as $record) {
//Rebuild wiki links
$record->questiontext = restore_decode_wiki_content($record->questiontext, $restore);
//Convert to Markdown
$wtm = new WikiToMarkdown();
$record->questiontext = $wtm->convert($record->questiontext, $restore->course_id);
$record->questiontextformat = FORMAT_MARKDOWN;
$status = update_record('question', addslashes_object($record));
//Do some output
$i++;
if (($i+1) % 1 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if (($i+1) % 20 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
return $status;
}
//This function returns a log record with all the necessay transformations
//done. It's used by restore_log_module() to restore modules log.
function quiz_restore_logs($restore,$log) {
$status = false;
//Depending of the action, we recode different things
switch ($log->action) {
case "add":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "view.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "update":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "view.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "view":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "view.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "view all":
$log->url = "index.php?id=".$log->course;
$status = true;
break;
case "report":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "report.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "attempt":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
//Extract the attempt id from the url field
$attid = substr(strrchr($log->url,"="),1);
//Get the new_id of the attempt (to recode the url field)
$att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
if ($att) {
$log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
$log->info = $mod->new_id;
$status = true;
}
}
}
break;
case "submit":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
//Extract the attempt id from the url field
$attid = substr(strrchr($log->url,"="),1);
//Get the new_id of the attempt (to recode the url field)
$att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
if ($att) {
$log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
$log->info = $mod->new_id;
$status = true;
}
}
}
break;
case "review":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
//Extract the attempt id from the url field
$attid = substr(strrchr($log->url,"="),1);
//Get the new_id of the attempt (to recode the url field)
$att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
if ($att) {
$log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
$log->info = $mod->new_id;
$status = true;
}
}
}
break;
case "editquestions":
if ($log->cmid) {
//Get the new_id of the module (to recode the url field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "view.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "preview":
if ($log->cmid) {
//Get the new_id of the module (to recode the url field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "attempt.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "start attempt":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
//Extract the attempt id from the url field
$attid = substr(strrchr($log->url,"="),1);
//Get the new_id of the attempt (to recode the url field)
$att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
if ($att) {
$log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
$log->info = $mod->new_id;
$status = true;
}
}
}
break;
case "close attempt":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
//Extract the attempt id from the url field
$attid = substr(strrchr($log->url,"="),1);
//Get the new_id of the attempt (to recode the url field)
$att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
if ($att) {
$log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
$log->info = $mod->new_id;
$status = true;
}
}
}
break;
case "continue attempt":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
//Extract the attempt id from the url field
$attid = substr(strrchr($log->url,"="),1);
//Get the new_id of the attempt (to recode the url field)
$att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
if ($att) {
$log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
$log->info = $mod->new_id;
$status = true;
}
}
}
break;
case "continue attemp":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
//Extract the attempt id from the url field
$attid = substr(strrchr($log->url,"="),1);
//Get the new_id of the attempt (to recode the url field)
$att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
if ($att) {
$log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
$log->info = $mod->new_id;
$log->action = "continue attempt"; //To recover some bad actions
$status = true;
}
}
}
break;
default:
if (!defined('RESTORE_SILENTLY')) {
echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
}
break;
}
if ($status) {
$status = $log;
}
return $status;
}
function quiz_recode_layout($layout, $restore) {
//Recodes the quiz layout (a list of questions id and pagebreaks)
//Extracts question id from sequence
if ($questionids = explode(',', $layout)) {
foreach ($questionids as $id => $questionid) {
if ($questionid) { // If it is zero then this is a pagebreak, don't translate
$newq = backup_getid($restore->backup_unique_code,"question",$questionid);
$questionids[$id] = $newq->new_id;
}
}
}
return implode(',', $questionids);
}
?>