forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestorelib.php
2558 lines (2339 loc) · 135 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
//Functions used in restore
require_once($CFG->libdir.'/gradelib.php');
/**
* Group backup/restore constants, 0.
*/
define('RESTORE_GROUPS_NONE', 0);
/**
* Group backup/restore constants, 1.
*/
define('RESTORE_GROUPS_ONLY', 1);
/**
* Group backup/restore constants, 2.
*/
define('RESTORE_GROUPINGS_ONLY', 2);
/**
* Group backup/restore constants, course/all.
*/
define('RESTORE_GROUPS_GROUPINGS', 3);
//This function iterates over all modules in backup file, searching for a
//MODNAME_refresh_events() to execute. Perhaps it should ve moved to central Moodle...
function restore_refresh_events($restore) {
global $CFG;
$status = true;
//Take all modules in backup
$modules = $restore->mods;
//Iterate
foreach($modules as $name => $module) {
//Only if the module is being restored
if (isset($module->restore) && $module->restore == 1) {
//Include module library
include_once("$CFG->dirroot/mod/$name/lib.php");
//If module_refresh_events exists
$function_name = $name."_refresh_events";
if (function_exists($function_name)) {
$status = $function_name($restore->course_id);
}
}
}
return $status;
}
//Called to set up any course-format specific data that may be in the file
function restore_set_format_data($restore,$xml_file) {
global $CFG, $DB;
$status = true;
//Check it exists
if (!file_exists($xml_file)) {
return false;
}
//Load data from XML to info
if(!($info = restore_read_xml_formatdata($xml_file))) {
return false;
}
//Process format data if there is any
if (isset($info->format_data)) {
if(!$format=$DB->get_field('course','format', array('id'=>$restore->course_id))) {
return false;
}
// If there was any data then it must have a restore method
$file=$CFG->dirroot."/course/format/$format/restorelib.php";
if(!file_exists($file)) {
return false;
}
require_once($file);
$function=$format.'_restore_format_data';
if(!function_exists($function)) {
return false;
}
return $function($restore,$info->format_data);
}
// If we got here then there's no data, but that's cool
return true;
}
/**
* This function creates all the gradebook data from xml
*/
function restore_create_gradebook($restore,$xml_file) {
global $CFG, $DB;
$status = true;
//Check it exists
if (!file_exists($xml_file)) {
return false;
}
// Get info from xml
// info will contain the number of record to process
$info = restore_read_xml_gradebook($restore, $xml_file);
// If we have info, then process
if (empty($info)) {
return $status;
}
if (empty($CFG->disablegradehistory) and isset($info->gradebook_histories) and $info->gradebook_histories == "true") {
$restore_histories = true;
} else {
$restore_histories = false;
}
// make sure top course category exists
$course_category = grade_category::fetch_course_category($restore->course_id);
$course_category->load_grade_item();
// we need to know if all grade items that were backed up are being restored
// if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
// i.e. the aggregated grades of that category
$restoreall = true; // set to false if any grade_item is not selected/restored or already exist
$importing = !empty($SESSION->restore->importing);
if ($importing) {
$restoreall = false;
} else {
$prev_grade_items = grade_item::fetch_all(array('courseid'=>$restore->course_id));
$prev_grade_cats = grade_category::fetch_all(array('courseid'=>$restore->course_id));
// if any categories already present, skip restore of categories from backup - course item or category already exist
if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) {
$restoreall = false;
}
unset($prev_grade_items);
unset($prev_grade_cats);
if ($restoreall) {
if ($recs = $DB->get_records("backup_ids", array('table_name'=>'grade_items', 'backup_code'=>$restore->backup_unique_code), "", "old_id")) {
foreach ($recs as $rec) {
if ($data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id)) {
$info = $data->info;
// do not restore if this grade_item is a mod, and
$itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
if ($itemtype == 'mod') {
$olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
$itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
if (empty($restore->mods[$itemmodule]->granular)) {
continue;
} else if (!empty($restore->mods[$itemmodule]->instances[$olditeminstance]->restore)) {
continue;
}
// at least one activity should not be restored - do not restore categories and manual items at all
$restoreall = false;
break;
}
}
}
}
}
}
// Start ul
if (!defined('RESTORE_SILENTLY')) {
echo '<ul>';
}
// array of restored categories - speedup ;-)
$cached_categories = array();
$outcomes = array();
/// Process letters
$context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
// respect current grade letters if defined
if ($status and $restoreall and !$DB->record_exists('grade_letters', array('contextid'=>$context->id))) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('gradeletters','grades').'</li>';
}
// Fetch recordset_size records in each iteration
$recs = $DB->get_records("backup_ids", array('table_name'=>'grade_letters', 'backup_code'=>$restore->backup_unique_code),
"",
"old_id");
if ($recs) {
foreach ($recs as $rec) {
// Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_letters',$rec->old_id);
if ($data) {
$info = $data->info;
$dbrec = new object();
$dbrec->contextid = $context->id;
$dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['LOWERBOUNDARY']['0']['#']);
$dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']);
$DB->insert_record('grade_letters', $dbrec);
}
}
}
}
/// Process grade items and grades
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('gradeitems','grades').'</li>';
}
$counter = 0;
//Fetch recordset_size records in each iteration
$recs = $DB->get_records("backup_ids", array('table_name'=>'grade_items', 'backup_code'=>$restore->backup_unique_code),
"id", // restore in the backup order
"old_id");
if ($recs) {
foreach ($recs as $rec) {
//Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id);
if ($data) {
$info = $data->info;
// first find out if category or normal item
$itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
if ($itemtype == 'course' or $itemtype == 'category') {
if (!$restoreall or $importing) {
continue;
}
$oldcat = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false);
if (!$cdata = backup_getid($restore->backup_unique_code,'grade_categories',$oldcat)) {
continue;
}
$cinfo = $cdata->info;
unset($cdata);
if ($itemtype == 'course') {
$course_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false);
$course_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false);
$course_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false);
$course_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false);
$course_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false);
$course_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false);
$course_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false);
$course_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false);
$course_category->update('restore');
$status = backup_putid($restore->backup_unique_code,'grade_categories',$oldcat,$course_category->id) && $status;
$cached_categories[$oldcat] = $course_category;
$grade_item = $course_category->get_grade_item();
} else {
$oldparent = backup_todb($cinfo['GRADE_CATEGORY']['#']['PARENT']['0']['#'], false);
if (empty($cached_categories[$oldparent])) {
debugging('parent not found '.$oldparent);
continue; // parent not found, sorry
}
$grade_category = new grade_category();
$grade_category->courseid = $restore->course_id;
$grade_category->parent = $cached_categories[$oldparent]->id;
$grade_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false);
$grade_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false);
$grade_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false);
$grade_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false);
$grade_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false);
$grade_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false);
$grade_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false);
$grade_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false);
$grade_category->insert('restore');
$status = backup_putid($restore->backup_unique_code,'grade_categories',$oldcat,$grade_category->id) && $status;
$cached_categories[$oldcat] = $grade_category;
$grade_item = $grade_category->get_grade_item(); // creates grade_item too
}
unset($cinfo);
$idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
if (grade_verify_idnumber($idnumber, $restore->course_id)) {
$grade_item->idnumber = $idnumber;
}
$grade_item->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
$grade_item->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false);
$grade_item->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false);
$grade_item->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false);
$grade_item->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false);
$grade_item->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false);
$grade_item->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false);
$grade_item->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false);
$grade_item->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false);
$grade_item->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false);
$grade_item->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false);
$grade_item->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false);
$grade_item->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false);
$grade_item->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false);
$grade_item->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false);
$grade_item->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false);
if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) {
$scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false));
$grade_item->scaleid = $scale->new_id;
}
if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false)) {
$outcome = backup_getid($restore->backup_unique_code,"grade_outcomes",backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false));
$grade_item->outcomeid = $outcome->new_id;
}
$grade_item->update('restore');
$status = backup_putid($restore->backup_unique_code,"grade_items", $rec->old_id, $grade_item->id) && $status;
} else {
if ($itemtype != 'mod' and (!$restoreall or $importing)) {
// not extra gradebook stuff if restoring individual activities or something already there
continue;
}
$dbrec = new object();
$dbrec->courseid = $restore->course_id;
$dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
$dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false);
if ($itemtype == 'mod') {
// iteminstance should point to new mod
$olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false);
$mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $olditeminstance);
$dbrec->iteminstance = $mod->new_id;
if (!$cm = get_coursemodule_from_instance($dbrec->itemmodule, $mod->new_id)) {
// item not restored - no item
continue;
}
// keep in sync with activity idnumber
$dbrec->idnumber = $cm->idnumber;
} else {
$idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
if (grade_verify_idnumber($idnumber, $restore->course_id)) {
//make sure the new idnumber is unique
$dbrec->idnumber = $idnumber;
}
}
$dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
$dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
$dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false);
$dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#'], false);
$dbrec->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false);
$dbrec->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false);
$dbrec->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false);
$dbrec->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false);
$dbrec->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false);
$dbrec->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false);
$dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false);
$dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false);
$dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false);
$dbrec->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false);
$dbrec->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false);
$dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false);
$dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false);
$dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false);
$dbrec->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false);
if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) {
$scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false));
$dbrec->scaleid = $scale->new_id;
}
if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'])) {
$oldoutcome = backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']);
if (empty($outcomes[$oldoutcome])) {
continue; // error!
}
if (empty($outcomes[$oldoutcome]->id)) {
$outcomes[$oldoutcome]->insert('restore');
$outcomes[$oldoutcome]->use_in($restore->course_id);
backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $outcomes[$oldoutcome]->id);
}
$dbrec->outcomeid = $outcomes[$oldoutcome]->id;
}
$grade_item = new grade_item($dbrec, false);
$grade_item->insert('restore');
if ($restoreall) {
// set original parent if restored
$oldcat = $info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'];
if (!empty($cached_categories[$oldcat])) {
$grade_item->set_parent($cached_categories[$oldcat]->id);
}
}
$status = backup_putid($restore->backup_unique_code,"grade_items", $rec->old_id, $grade_item->id) && $status;
}
// no need to restore grades if user data is not selected or importing activities
if ($importing
or ($grade_item->itemtype == 'mod' and !restore_userdata_selected($restore, $grade_item->itemmodule, $olditeminstance))) {
// module instance not selected when restored using granular
// skip this item
continue;
}
/// now, restore grade_grades
if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
//Iterate over items
foreach ($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'] as $g_info) {
$grade = new grade_grade();
$grade->itemid = $grade_item->id;
$olduser = backup_todb($g_info['#']['USERID']['0']['#'], false);
$user = backup_getid($restore->backup_unique_code,"user",$olduser);
$grade->userid = $user->new_id;
$grade->rawgrade = backup_todb($g_info['#']['RAWGRADE']['0']['#'], false);
$grade->rawgrademax = backup_todb($g_info['#']['RAWGRADEMAX']['0']['#'], false);
$grade->rawgrademin = backup_todb($g_info['#']['RAWGRADEMIN']['0']['#'], false);
// need to find scaleid
if (backup_todb($g_info['#']['RAWSCALEID']['0']['#'])) {
$scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($g_info['#']['RAWSCALEID']['0']['#'], false));
$grade->rawscaleid = $scale->new_id;
}
if (backup_todb($g_info['#']['USERMODIFIED']['0']['#'])) {
if ($modifier = backup_getid($restore->backup_unique_code,"user", backup_todb($g_info['#']['USERMODIFIED']['0']['#'], false))) {
$grade->usermodified = $modifier->new_id;
}
}
$grade->finalgrade = backup_todb($g_info['#']['FINALGRADE']['0']['#'], false);
$grade->hidden = backup_todb($g_info['#']['HIDDEN']['0']['#'], false);
$grade->locked = backup_todb($g_info['#']['LOCKED']['0']['#'], false);
$grade->locktime = backup_todb($g_info['#']['LOCKTIME']['0']['#'], false);
$grade->exported = backup_todb($g_info['#']['EXPORTED']['0']['#'], false);
$grade->overridden = backup_todb($g_info['#']['OVERRIDDEN']['0']['#'], false);
$grade->excluded = backup_todb($g_info['#']['EXCLUDED']['0']['#'], false);
$grade->feedback = backup_todb($g_info['#']['FEEDBACK']['0']['#'], false);
$grade->feedbackformat = backup_todb($g_info['#']['FEEDBACKFORMAT']['0']['#'], false);
$grade->information = backup_todb($g_info['#']['INFORMATION']['0']['#'], false);
$grade->informationformat = backup_todb($g_info['#']['INFORMATIONFORMAT']['0']['#'], false);
$grade->timecreated = backup_todb($g_info['#']['TIMECREATED']['0']['#'], false);
$grade->timemodified = backup_todb($g_info['#']['TIMEMODIFIED']['0']['#'], false);
$grade->insert('restore');
backup_putid($restore->backup_unique_code,"grade_grades", backup_todb($g_info['#']['ID']['0']['#']), $grade->id);
$counter++;
if ($counter % 20 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if ($counter % 400 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
}
}
}
}
/// add outcomes that are not used when doing full restore
if ($status and $restoreall) {
foreach ($outcomes as $oldoutcome=>$grade_outcome) {
if (empty($grade_outcome->id)) {
$grade_outcome->insert('restore');
$grade_outcome->use_in($restore->course_id);
backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $grade_outcome->id);
}
}
}
if ($status and !$importing and $restore_histories) {
/// following code is very inefficient
$gchcount = $DB->count_records('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'grade_categories_history'));
$gghcount = $DB->count_records('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'grade_grades_history'));
$gihcount = $DB->count_records('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'grade_items_history'));
$gohcount = $DB->count_records('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'grade_outcomes_history'));
// Number of records to get in every chunk
$recordset_size = 2;
// process histories
if ($gchcount && $status) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('gradecategoryhistory','grades').'</li>';
}
$counter = 0;
while ($counter < $gchcount) {
//Fetch recordset_size records in each iteration
$recs = $DB->get_records("backup_ids",array('table_name'=>'grade_categories_history', 'backup_code'=>$restore->backup_unique_code),
"old_id",
"old_id",
$counter,
$recordset_size);
if ($recs) {
foreach ($recs as $rec) {
//Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_categories_history',$rec->old_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
$oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['OLDID']['0']['#']));
if (empty($oldobj->new_id)) {
// if the old object is not being restored, can't restoring its history
$counter++;
continue;
}
$dbrec->oldid = $oldobj->new_id;
$dbrec->action = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['ACTION']['0']['#']);
$dbrec->source = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['SOURCE']['0']['#']);
$dbrec->timemodified = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
// loggeduser might not be restored, e.g. admin
if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
$dbrec->loggeduser = $oldobj->new_id;
}
// this item might not have a parent at all, do not skip it if no parent is specified
if (backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])) {
$oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#']));
if (empty($oldobj->new_id)) {
// if the parent category not restored
$counter++;
continue;
}
}
$dbrec->parent = $oldobj->new_id;
$dbrec->depth = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DEPTH']['0']['#']);
// path needs to be rebuilt
if ($path = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PATH']['0']['#'])) {
// to preserve the path and make it work, we need to replace the categories one by one
// we first get the list of categories in current path
if ($paths = explode("/", $path)) {
$newpath = '';
foreach ($paths as $catid) {
if ($catid) {
// find the new corresponding path
$oldpath = backup_getid($restore->backup_unique_code,"grade_categories", $catid);
$newpath .= "/$oldpath->new_id";
}
}
$dbrec->path = $newpath;
}
}
$dbrec->fullname = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['FULLNAME']['0']['#']);
$dbrec->aggregation = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGRETGATION']['0']['#']);
$dbrec->keephigh = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['KEEPHIGH']['0']['#']);
$dbrec->droplow = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DROPLOW']['0']['#']);
$dbrec->aggregateonlygraded = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEONLYGRADED']['0']['#']);
$dbrec->aggregateoutcomes = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEOUTCOMES']['0']['#']);
$dbrec->aggregatesubcats = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATESUBCATS']['0']['#']);
$dbrec->courseid = $restore->course_id;
$DB->insert_record('grade_categories_history', $dbrec);
unset($dbrec);
}
//Increment counters
$counter++;
//Do some output
if ($counter % 1 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if ($counter % 20 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
}
}
// process histories
if ($gghcount && $status) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('gradegradeshistory','grades').'</li>';
}
$counter = 0;
while ($counter < $gghcount) {
//Fetch recordset_size records in each iteration
$recs = $DB->get_records("backup_ids", array('table_name'=>'grade_grades_history', 'backup_code'=>$restore->backup_unique_code),
"old_id",
"old_id",
$counter,
$recordset_size);
if ($recs) {
foreach ($recs as $rec) {
//Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_grades_history',$rec->old_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
$oldobj = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($info['GRADE_GRADES_HISTORY']['#']['OLDID']['0']['#']));
if (empty($oldobj->new_id)) {
// if the old object is not being restored, can't restoring its history
$counter++;
continue;
}
$dbrec->oldid = $oldobj->new_id;
$dbrec->action = backup_todb($info['GRADE_GRADES_HISTORY']['#']['ACTION']['0']['#']);
$dbrec->source = backup_todb($info['GRADE_GRADES_HISTORY']['#']['SOURCE']['0']['#']);
$dbrec->timemodified = backup_todb($info['GRADE_GRADES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
$dbrec->loggeduser = $oldobj->new_id;
}
$oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_GRADES_HISTORY']['#']['ITEMID']['0']['#']));
$dbrec->itemid = $oldobj->new_id;
if (empty($dbrec->itemid)) {
$counter++;
continue; // grade item not being restored
}
$oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERID']['0']['#']));
$dbrec->userid = $oldobj->new_id;
$dbrec->rawgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADE']['0']['#']);
$dbrec->rawgrademax = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMAX']['0']['#']);
$dbrec->rawgrademin = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMIN']['0']['#']);
if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
$dbrec->usermodified = $oldobj->new_id;
}
if (backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#'])) {
$scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#']));
$dbrec->rawscaleid = $scale->new_id;
}
$dbrec->finalgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['FINALGRADE']['0']['#']);
$dbrec->hidden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['HIDDEN']['0']['#']);
$dbrec->locked = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKED']['0']['#']);
$dbrec->locktime = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKTIME']['0']['#']);
$dbrec->exported = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXPORTED']['0']['#']);
$dbrec->overridden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['OVERRIDDEN']['0']['#']);
$dbrec->excluded = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXCLUDED']['0']['#']);
$dbrec->feedback = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACK']['0']['#']);
$dbrec->feedbackformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACKFORMAT']['0']['#']);
$dbrec->information = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATION']['0']['#']);
$dbrec->informationformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATIONFORMAT']['0']['#']);
$DB->insert_record('grade_grades_history', $dbrec);
unset($dbrec);
}
//Increment counters
$counter++;
//Do some output
if ($counter % 1 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if ($counter % 20 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
}
}
// process histories
if ($gihcount && $status) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('gradeitemshistory','grades').'</li>';
}
$counter = 0;
while ($counter < $gihcount) {
//Fetch recordset_size records in each iteration
$recs = $DB->get_records("backup_ids", array('table_name'=>'grade_items_history', 'backup_code'=>$restore->backup_unique_code),
"old_id",
"old_id",
$counter,
$recordset_size);
if ($recs) {
foreach ($recs as $rec) {
//Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_items_history',$rec->old_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
$oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OLDID']['0']['#']));
if (empty($oldobj->new_id)) {
// if the old object is not being restored, can't restoring its history
$counter++;
continue;
}
$dbrec->oldid = $oldobj->new_id;
$dbrec->action = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ACTION']['0']['#']);
$dbrec->source = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SOURCE']['0']['#']);
$dbrec->timemodified = backup_todb($info['GRADE_ITEM_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
$dbrec->loggeduser = $oldobj->new_id;
}
$dbrec->courseid = $restore->course_id;
$oldobj = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_ITEM_HISTORY']['#']['CATEGORYID']['0']['#']));
$oldobj->categoryid = $category->new_id;
if (empty($oldobj->categoryid)) {
$counter++;
continue; // category not restored
}
$dbrec->itemname= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNAME']['0']['#']);
$dbrec->itemtype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMTYPE']['0']['#']);
$dbrec->itemmodule = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMMODULE']['0']['#']);
// code from grade_items restore
$iteminstance = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINSTANCE']['0']['#']);
// do not restore if this grade_item is a mod, and
if ($dbrec->itemtype == 'mod') {
if (!restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
// module instance not selected when restored using granular
// skip this item
$counter++;
continue;
}
// iteminstance should point to new mod
$mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $iteminstance);
$dbrec->iteminstance = $mod->new_id;
} else if ($dbrec->itemtype == 'category') {
// the item instance should point to the new grade category
// only proceed if we are restoring all grade items
if ($restoreall) {
$category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
$dbrec->iteminstance = $category->new_id;
} else {
// otherwise we can safely ignore this grade item and subsequent
// grade_raws, grade_finals etc
continue;
}
} elseif ($dbrec->itemtype == 'course') { // We don't restore course type to avoid duplicate course items
if ($restoreall) {
// TODO any special code needed here to restore course item without duplicating it?
// find the course category with depth 1, and course id = current course id
// this would have been already restored
$cat = $DB->get_record('grade_categories', array('depth'=>1, 'courseid'=>$restore->course_id));
$dbrec->iteminstance = $cat->id;
} else {
$counter++;
continue;
}
}
$dbrec->itemnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNUMBER']['0']['#']);
$dbrec->iteminfo = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINFO']['0']['#']);
$dbrec->idnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['IDNUMBER']['0']['#']);
$dbrec->calculation = backup_todb($info['GRADE_ITEM_HISTORY']['#']['CALCULATION']['0']['#']);
$dbrec->gradetype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADETYPE']['0']['#']);
$dbrec->grademax = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMAX']['0']['#']);
$dbrec->grademin = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMIN']['0']['#']);
if ($oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_ITEM_HISTORY']['#']['SCALEID']['0']['#']))) {
// scaleid is optional
$dbrec->scaleid = $oldobj->new_id;
}
if ($oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OUTCOMEID']['0']['#']))) {
// outcome is optional
$dbrec->outcomeid = $oldobj->new_id;
}
$dbrec->gradepass = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEPASS']['0']['#']);
$dbrec->multfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['MULTFACTOR']['0']['#']);
$dbrec->plusfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['PLUSFACTOR']['0']['#']);
$dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM_HISTORY']['#']['AGGREGATIONCOEF']['0']['#']);
$dbrec->sortorder = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SORTORDER']['0']['#']);
$dbrec->display = backup_todb($info['GRADE_ITEM_HISTORY']['#']['DISPLAY']['0']['#']);
$dbrec->decimals = backup_todb($info['GRADE_ITEM_HISTORY']['#']['DECIMALS']['0']['#']);
$dbrec->hidden = backup_todb($info['GRADE_ITEM_HISTORY']['#']['HIDDEN']['0']['#']);
$dbrec->locked = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKED']['0']['#']);
$dbrec->locktime = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKTIME']['0']['#']);
$dbrec->needsupdate = backup_todb($info['GRADE_ITEM_HISTORY']['#']['NEEDSUPDATE']['0']['#']);
$DB->insert_record('grade_items_history', $dbrec);
unset($dbrec);
}
//Increment counters
$counter++;
//Do some output
if ($counter % 1 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if ($counter % 20 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
}
}
// process histories
if ($gohcount && $status) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('gradeoutcomeshistory','grades').'</li>';
}
$counter = 0;
while ($counter < $gohcount) {
//Fetch recordset_size records in each iteration
$recs = $DB->get_records("backup_ids", array('table_name'=>'grade_outcomes_history', 'backup_code'=>$restore->backup_unique_code),
"old_id",
"old_id",
$counter,
$recordset_size);
if ($recs) {
foreach ($recs as $rec) {
//Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_outcomes_history',$rec->old_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
$oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['OLDID']['0']['#']));
if (empty($oldobj->new_id)) {
// if the old object is not being restored, can't restoring its history
$counter++;
continue;
}
$dbrec->oldid = $oldobj->new_id;
$dbrec->action = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['ACTION']['0']['#']);
$dbrec->source = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SOURCE']['0']['#']);
$dbrec->timemodified = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
$dbrec->loggeduser = $oldobj->new_id;
}
$dbrec->courseid = $restore->course_id;
$dbrec->shortname = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SHORTNAME']['0']['#']);
$dbrec->fullname= backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['FULLNAME']['0']['#']);
$oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SCALEID']['0']['#']));
$dbrec->scaleid = $oldobj->new_id;
$dbrec->description = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['DESCRIPTION']['0']['#']);
$DB->insert_record('grade_outcomes_history', $dbrec);
unset($dbrec);
}
//Increment counters
$counter++;
//Do some output
if ($counter % 1 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if ($counter % 20 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
}
}
}
if (!defined('RESTORE_SILENTLY')) {
//End ul
echo '</ul>';
}
return $status;
}
//This function creates all the structures messages and contacts
function restore_create_messages($restore,$xml_file) {
global $CFG, $DB;
$status = true;
//Check it exists
if (!file_exists($xml_file)) {
$status = false;
}
//Get info from xml
if ($status) {
//info will contain the id and name of every table
//(message, message_read and message_contacts)
//in backup_ids->info will be the real info (serialized)
$info = restore_read_xml_messages($restore,$xml_file);
//If we have info, then process messages & contacts
if ($info > 0) {
//Count how many we have
$unreadcount = $DB->count_records ('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'message'));
$readcount = $DB->count_records ('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'message_read'));
$contactcount = $DB->count_records ('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'message_contacts'));
if ($unreadcount || $readcount || $contactcount) {
//Start ul
if (!defined('RESTORE_SILENTLY')) {
echo '<ul>';
}
//Number of records to get in every chunk
$recordset_size = 4;
//Process unread
if ($unreadcount) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('unreadmessages','message').'</li>';
}
$counter = 0;
while ($counter < $unreadcount) {
//Fetch recordset_size records in each iteration
$recs = $DB->get_records("backup_ids", array('table_name'=>'message', 'backup_code'=>$restore->backup_unique_code),"old_id","old_id",$counter,$recordset_size);
if ($recs) {
foreach ($recs as $rec) {
//Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,"message",$rec->old_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 MESSAGE record structure
$dbrec = new object();
$dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
$dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
$dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
$dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
$dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
$dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
//We have to recode the useridfrom field
$user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
if ($user) {
//echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
$dbrec->useridfrom = $user->new_id;
}
//We have to recode the useridto field
$user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
if ($user) {
//echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
$dbrec->useridto = $user->new_id;
}
//Check if the record doesn't exist in DB!
$exist = $DB->get_record('message', array('useridfrom'=>$dbrec->useridfrom,
'useridto'=>$dbrec->useridto,
'timecreated'=>$dbrec->timecreated));
if (!$exist) {
//Not exist. Insert
$status = $DB->insert_record('message',$dbrec);
} else {
//Duplicate. Do nothing
}
}
//Do some output
$counter++;
if ($counter % 10 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if ($counter % 200 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
}
}
//Process read
if ($readcount) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('readmessages','message').'</li>';
}
$counter = 0;
while ($counter < $readcount) {
//Fetch recordset_size records in each iteration
$recs = $DB->get_records("backup_ids", array('table_name'=>'message_read', 'backup_code'=>$restore->backup_unique_code),"old_id","old_id",$counter,$recordset_size);
if ($recs) {
foreach ($recs as $rec) {
//Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,"message_read",$rec->old_id);