forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackuplib.php
3468 lines (3060 loc) · 151 KB
/
backuplib.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 file contains all the function needed in the backup utility
//except the mod-related funtions that are into every backuplib.php inside
//every mod directory
/**
* This function calculates the users to be added to backup based in the
* settings defined at backup. All the resulting user ids are sent to
* backup_ids for later usage.
* @param int $courseid id of the course to backup
* @param int $backup_unique_code unique code of the backup being executed
* @param int $backup_unique_code setting specifying what users to export (0=all, 1=needed, 2=none)
* @param int $backup_messages flag (true/false) defining if messages must be
* considered to extract needed users
* @param int $backup_blogs flag (true/false) defining if blogs must be
* considered to extract needed users
* @return array one array (key, value) sumarizing the result of the function (number of users)
*/
function user_check_backup($courseid,$backup_unique_code,$backup_users,$backup_messages,$backup_blogs) {
global $DB;
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$count_users = 0;
$backupable_users = array();
if ($backup_users == 0) { /// All users
$backupable_users = backup_get_all_users();
} else if ($backup_users == 1) { /// Needed users
/// Calculate needed users (calling every xxxx_get_participants function + scales users
/// + messages users + blogs users)
$needed_users = backup_get_needed_users($courseid, $backup_messages, $backup_blogs);
/// Calculate enrolled users (having course:view cap)
$enrolled_users = backup_get_enrolled_users($courseid);
/// Calculate backupable users (needed + enrolled)
/// First, needed
$backupable_users = $needed_users;
/// Now, enrolled
if ($enrolled_users) {
foreach ($enrolled_users as $enrolled_user) {
$backupable_users[$enrolled_user->id]->id = $enrolled_user->id;
}
}
}
/// If we have backupable users
if ($backupable_users) {
/// Iterate over users putting their roles
foreach ($backupable_users as $backupable_user) {
$backupable_user->info = "";
/// Is needed user or enrolled user, mark it as needed
if (isset($needed_users[$backupable_user->id]) || isset($enrolled_users[$backupable_user->id])) {
$backupable_user->info .= "needed";
} /// Yu: also needed because they can view course
/// might need another variable
/// Now create the backup_id record
$backupids_rec->backup_code = $backup_unique_code;
$backupids_rec->table_name = "user";
$backupids_rec->old_id = $backupable_user->id;
$backupids_rec->info = $backupable_user->info;
/// TODO: Change this call inserting to a standard backup_putid() call
/// And read data acordingly with backup_getid() when needed.
/// TODO: Also analyse it the "needed" info is really needed for anything. Drop if not.
/// Insert the user to the backup_ids table. backup_user_info() will use that info
$status = $DB->insert_record('backup_ids', $backupids_rec, false);
$count_users++;
}
/// Do some output
backup_flush(30);
}
/// Prepare Info
/// Gets the user data
$info[0][0] = get_string("users");
$info[0][1] = $count_users;
return $info;
}
//Returns every needed user (participant) in a course
//It uses the xxxx_get_participants() function
//plus users needed to backup scales.
//Also it search for users having messages and
//users having blogs
//WARNING: It returns only NEEDED users, not every
// every student and teacher in the course, so it
//must be merged with backup_get_enrrolled_users !!
function backup_get_needed_users ($courseid, $includemessages=false, $includeblogs=false) {
global $CFG, $DB;
$result = false;
$course_modules = $DB->get_records_sql("SELECT cm.id, m.name, cm.instance
FROM {modules} m, {course_modules} cm
WHERE m.id = cm.module and
cm.course = ?", array($courseid));
if ($course_modules) {
//Iterate over each module
foreach ($course_modules as $course_module) {
$modlib = "$CFG->dirroot/mod/$course_module->name/lib.php";
$modgetparticipants = $course_module->name."_get_participants";
if (file_exists($modlib)) {
include_once($modlib);
if (function_exists($modgetparticipants)) {
$module_participants = $modgetparticipants($course_module->instance);
//Add them to result
if ($module_participants) {
foreach ($module_participants as $module_participant) {
$result[$module_participant->id]->id = $module_participant->id;
}
}
}
}
}
}
//Now, add scale users (from site and course scales)
//Get users
$scaleusers = $DB->get_records_sql("SELECT DISTINCT userid,userid
FROM {scale}
WHERE courseid = 0 or courseid = ?", array($courseid));
//Add scale users to results
if ($scaleusers) {
foreach ($scaleusers as $scaleuser) {
//If userid != 0
if ($scaleuser->userid != 0) {
$result[$scaleuser->userid]->id = $scaleuser->userid;
}
}
}
//Now, add message users if necessary
if ($includemessages) {
include_once("$CFG->dirroot/message/lib.php");
//Get users
$messageusers = message_get_participants();
//Add message users to results
if ($messageusers) {
foreach ($messageusers as $messageuser) {
//If id != 0
if ($messageuser->id !=0) {
$result[$messageuser->id]->id = $messageuser->id;
}
}
}
}
//Now, add blog users if necessary
if ($includeblogs) {
include_once("$CFG->dirroot/blog/lib.php");
//Get users
$blogusers = blog_get_participants();
//Add blog users to results
if ($blogusers) {
foreach ($blogusers as $bloguser) {
//If id != 0
if ($bloguser->id !=0) {
$result[$bloguser->id]->id = $bloguser->id;
}
}
}
}
return $result;
}
//Returns every enrolled user (student and teacher) in a course
function backup_get_enrolled_users ($courseid) {
global $CFG;
// get all users with moodle/course:view capability, this will include people
// assigned at cat level, or site level
// but it should be ok if they have no direct assignment at course, mod, block level
return get_users_by_capability(get_context_instance(CONTEXT_COURSE, $courseid), 'moodle/course:view');
}
//Returns all users ids (every record in users table)
function backup_get_all_users() {
global $DB;
return $DB->get_records('user', null, '', 'id, id');
}
//Calculate the number of log entries to backup
//Return an array of info (name,value)
function log_check_backup($course) {
global $CFG, $DB;
//Now execute the count
$ids = $DB->count_records("log", array("course"=>$course));
//Gets the user data
$info[0][0] = get_string("logs");
if ($ids) {
$info[0][1] = $ids;
} else {
$info[0][1] = 0;
}
return $info;
}
//Calculate the number of user files to backup
//Under $CFG->dataroot/users
//Return an array of info (name,value)
function user_files_check_backup($course,$backup_unique_code) {
global $CFG, $DB;
$count = 0;
$backup_users = $DB->get_recordset("backup_ids",
array('backup_code'=>$backup_unique_code, 'table_name'=>'user'), "", "id, old_id");
foreach ($backup_users as $user) {
//Is this user needed in the backup?
$userdir = make_user_directory($user->old_id, true);
if (check_dir_exists($userdir)) {
$count++;
}
//Do some output
backup_flush(30);
}
$backup_users->close();
//Gets the user data
$info[0][0] = get_string("userswithfiles");
$info[0][1] = $count;
return $info;
}
/**
* Calculate the number of course files to backup
* under $CFG->dataroot/$course, except $CFG->moddata, and backupdata
* and put them (their path) in backup_ids
* Return an array of info (name,value)
*/
function course_files_check_backup($course, $backup_unique_code) {
global $CFG, $DB;
$rootdir = $CFG->dataroot."/$course";
//Check if directory exists
if (is_dir($rootdir)) {
//Get files and directories without descend
$coursedirs = get_directory_list($rootdir,$CFG->moddata,false,true,true);
$backupdata_dir = "backupdata";
foreach ($coursedirs as $dir) {
//Check it isn't backupdata_dir
if (strpos($dir,$backupdata_dir)!==0) {
//Insert them into backup_files
$file = new object();
$file->backup_code = $backup_unique_code;
$file->file_type = 'course';
$file->path = $dir;
$status = $DB->insert_record("backup_files", $file);
}
//Do some output
backup_flush(30);
}
}
//Now execute the select
$ids = $DB->get_records_sql("SELECT DISTINCT b.path, b.old_id
FROM {backup_files} b
WHERE backup_code = ? AND
file_type = 'course'", array($backup_unique_code));
//Gets the user data
$info = array();
$info[0] = array();
$info[0][0] = get_string("files");
if ($ids) {
$info[0][1] = count($ids);
} else {
$info[0][1] = 0;
}
return $info;
}
/**
* Calculate the number of site files to backup
* under $CFG->dataroot/SITEID
* Their path is already in backup_ids, put there by modules check_backup functions.
* Modules only put in paths of files that are used.
*
* Return an array of info (name,value)
*/
function site_files_check_backup($course, $backup_unique_code) {
global $CFG, $DB;
//execute the select, records have been inserted by modules during their ****_check_backup_mods function.
$ids = $DB->get_records_sql("SELECT DISTINCT b.path
FROM {backup_files} b
WHERE backup_code = ? AND
file_type = 'site'", array($backup_unique_code));
//Gets the user data
$info = array();
$info[0] = array();
$info[0][0] = get_string('files');
if ($ids) {
$info[0][1] = count($ids);
} else {
$info[0][1] = 0;
}
return $info;
}
//Function to check and create the needed moddata dir to
//save all the mod backup files. We always name it moddata
//to be able to restore it, but in restore we check for
//$CFG->moddata !!
function check_and_create_moddata_dir($backup_unique_code) {
global $CFG;
$status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/moddata",true);
return $status;
}
//Function to check and create the "user_files" dir to
//save all the user files we need from "users" dir
function check_and_create_user_files_dir($backup_unique_code) {
global $CFG;
$status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/user_files",true);
return $status;
}
//Function to check and create the "group_files" dir to
//save all the user files we need from "groups" dir
function check_and_create_group_files_dir($backup_unique_code) {
global $CFG;
$status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/group_files",true);
return $status;
}
//Function to check and create the "course_files" dir to
//save all the course files we need from "CFG->datadir/course" dir
function check_and_create_course_files_dir($backup_unique_code) {
global $CFG;
$status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/course_files",true);
return $status;
}
//Function to check and create the "site_files" dir to
//save all the course files we need from "CFG->datadir/SITEID" dir
function check_and_create_site_files_dir($backup_unique_code) {
global $CFG;
$status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/site_files",true);
return $status;
}
//Function to create, open and write header of the xml file
function backup_open_xml($backup_unique_code) {
global $CFG;
$status = true;
//Open for writing
$file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
$backup_file = fopen($file,"w");
//Writes the header
$status = fwrite ($backup_file,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
if ($status) {
$status = fwrite ($backup_file,start_tag("MOODLE_BACKUP",0,true));
}
if ($status) {
return $backup_file;
} else {
return false;
}
}
//Close the file
function backup_close_xml($backup_file) {
$status = fwrite ($backup_file,end_tag("MOODLE_BACKUP",0,true));
return fclose($backup_file);
}
//Return the xml start tag
function start_tag($tag,$level=0,$endline=false,$attributes=null) {
if ($endline) {
$endchar = "\n";
} else {
$endchar = "";
}
$attrstring = '';
if (!empty($attributes) && is_array($attributes)) {
foreach ($attributes as $key => $value) {
$attrstring .= " ".xml_tag_safe_content($key)."=\"".
xml_tag_safe_content($value)."\"";
}
}
return str_repeat(" ",$level*2)."<".strtoupper($tag).$attrstring.">".$endchar;
}
//Return the xml end tag
function end_tag($tag,$level=0,$endline=true) {
if ($endline) {
$endchar = "\n";
} else {
$endchar = "";
}
return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
}
//Return the start tag, the contents and the end tag
function full_tag($tag,$level=0,$endline=true,$content,$attributes=null) {
global $CFG;
//Here we encode absolute links
// MDL-10770
if (is_null($content)) {
$content = '$@NULL@$';
} else {
$content = backup_encode_absolute_links($content);
}
$st = start_tag($tag,$level,$endline,$attributes);
$co = xml_tag_safe_content($content);
$et = end_tag($tag,0,true);
return $st.$co.$et;
}
function xml_tag_safe_content($content) {
global $CFG;
//If enabled, we strip all the control chars (\x0-\x1f) from the text but tabs (\x9),
//newlines (\xa) and returns (\xd). The delete control char (\x7f) is also included.
//because they are forbiden in XML 1.0 specs. The expression below seems to be
//UTF-8 safe too because it simply ignores the rest of characters.
$content = preg_replace("/[\x-\x8\xb-\xc\xe-\x1f\x7f]/is","",$content);
$content = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
return $content;
}
//Prints General info about the course
//name, moodle_version (internal and release), backup_version, date, info in file...
function backup_general_info ($bf,$preferences) {
global $CFG, $DB;
fwrite ($bf,start_tag("INFO",1,true));
//The name of the backup
fwrite ($bf,full_tag("NAME",2,false,$preferences->backup_name));
//The moodle_version
fwrite ($bf,full_tag("MOODLE_VERSION",2,false,$preferences->moodle_version));
fwrite ($bf,full_tag("MOODLE_RELEASE",2,false,$preferences->moodle_release));
//The backup_version
fwrite ($bf,full_tag("BACKUP_VERSION",2,false,$preferences->backup_version));
fwrite ($bf,full_tag("BACKUP_RELEASE",2,false,$preferences->backup_release));
//The date
fwrite ($bf,full_tag("DATE",2,false,$preferences->backup_unique_code));
//The original site wwwroot
fwrite ($bf,full_tag("ORIGINAL_WWWROOT",2,false,$CFG->wwwroot));
//Indicate if it includes external MNET users
$sql = "SELECT b.old_id
FROM {backup_ids} b
JOIN {user} u ON b.old_id=u.id
WHERE b.backup_code = ?
AND b.table_name = 'user' AND u.mnethostid != ?";
if ($DB->record_exists_sql($sql, array($preferences->backup_unique_code, $CFG->mnet_localhost_id))) {
fwrite ($bf,full_tag("MNET_REMOTEUSERS",2,false,'true'));
}
//Te includes tag
fwrite ($bf,start_tag("DETAILS",2,true));
//Now, go to mod element of preferences to print its status
foreach ($preferences->mods as $element) {
//Calculate info
$included = "false";
$userinfo = "false";
if ($element->backup) {
$included = "true";
if ($element->userinfo) {
$userinfo = "true";
}
}
//Prints the mod start
fwrite ($bf,start_tag("MOD",3,true));
fwrite ($bf,full_tag("NAME",4,false,$element->name));
fwrite ($bf,full_tag("INCLUDED",4,false,$included));
fwrite ($bf,full_tag("USERINFO",4,false,$userinfo));
if (isset($preferences->mods[$element->name]->instances)
&& is_array($preferences->mods[$element->name]->instances)
&& count($preferences->mods[$element->name]->instances)) {
fwrite ($bf, start_tag("INSTANCES",4,true));
foreach ($preferences->mods[$element->name]->instances as $id => $object) {
if (!empty($object->backup)) {
//Calculate info
$included = "false";
$userinfo = "false";
if ($object->backup) {
$included = "true";
if ($object->userinfo) {
$userinfo = "true";
}
}
fwrite ($bf, start_tag("INSTANCE",5,true));
fwrite ($bf, full_tag("ID",5,false,$id));
fwrite ($bf, full_tag("NAME",5,false,$object->name));
fwrite ($bf, full_tag("INCLUDED",5,false,$included)) ;
fwrite ($bf, full_tag("USERINFO",5,false,$userinfo));
fwrite ($bf, end_tag("INSTANCE",5,true));
}
}
fwrite ($bf, end_tag("INSTANCES",4,true));
}
//Print the end
fwrite ($bf,end_tag("MOD",3,true));
}
//The metacourse in backup
if ($preferences->backup_metacourse == 1) {
fwrite ($bf,full_tag("METACOURSE",3,false,"true"));
} else {
fwrite ($bf,full_tag("METACOURSE",3,false,"false"));
}
//The user in backup
if ($preferences->backup_users == 1) {
fwrite ($bf,full_tag("USERS",3,false,"course"));
} else if ($preferences->backup_users == 0) {
fwrite ($bf,full_tag("USERS",3,false,"all"));
} else {
fwrite ($bf,full_tag("USERS",3,false,"none"));
}
//The logs in backup
if ($preferences->backup_logs == 1) {
fwrite ($bf,full_tag("LOGS",3,false,"true"));
} else {
fwrite ($bf,full_tag("LOGS",3,false,"false"));
}
//The user files
if ($preferences->backup_user_files == 1) {
fwrite ($bf,full_tag("USERFILES",3,false,"true"));
} else {
fwrite ($bf,full_tag("USERFILES",3,false,"false"));
}
//The course files
if ($preferences->backup_course_files == 1) {
fwrite ($bf,full_tag("COURSEFILES",3,false,"true"));
} else {
fwrite ($bf,full_tag("COURSEFILES",3,false,"false"));
}
//The site files
if ($preferences->backup_site_files == 1) {
fwrite ($bf,full_tag("SITEFILES",3,false,"true"));
} else {
fwrite ($bf,full_tag("SITEFILES",3,false,"false"));
}
//The gradebook histories
if ($preferences->backup_gradebook_history == 1) {
fwrite ($bf,full_tag("GRADEBOOKHISTORIES",3,false,"true"));
} else {
fwrite ($bf,full_tag("GRADEBOOKHISTORIES",3,false,"false"));
}
//The messages in backup
if ($preferences->backup_messages == 1 && $preferences->backup_course == SITEID) {
fwrite ($bf,full_tag("MESSAGES",3,false,"true"));
} else {
fwrite ($bf,full_tag("MESSAGES",3,false,"false"));
}
//The blogs in backup
if ($preferences->backup_blogs == 1 && $preferences->backup_course == SITEID) {
fwrite ($bf,full_tag("BLOGS",3,false,"true"));
} else {
fwrite ($bf,full_tag("BLOGS",3,false,"false"));
}
//The mode of writing the block data
fwrite ($bf,full_tag('BLOCKFORMAT',3,false,'instances'));
fwrite ($bf,end_tag("DETAILS",2,true));
$status = fwrite ($bf,end_tag("INFO",1,true));
///Roles stuff goes in here
fwrite ($bf, start_tag('ROLES', 1, true));
$roles = backup_fetch_roles($preferences);
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
$coursecontext = get_context_instance(CONTEXT_COURSE, $preferences->backup_course);
foreach ($roles as $role) {
fwrite ($bf,start_tag('ROLE',2,true));
fwrite ($bf,full_tag('ID', 3, false, $role->id));
fwrite ($bf,full_tag('NAME',3,false,$role->name));
fwrite ($bf,full_tag('SHORTNAME',3,false,$role->shortname));
/// Calculate $role name in course
$nameincourse = role_get_name($role, $coursecontext);
if ($nameincourse != $role->name) {
fwrite ($bf,full_tag('NAMEINCOURSE', 3, false, $nameincourse));
}
// find and write all default capabilities
fwrite ($bf,start_tag('CAPABILITIES',3,true));
// pull out all default (site context) capabilities
if ($capabilities = role_context_capabilities($role->id, $sitecontext)) {
foreach ($capabilities as $capability=>$value) {
fwrite ($bf,start_tag('CAPABILITY',4,true));
fwrite ($bf,full_tag('NAME', 5, false, $capability));
fwrite ($bf,full_tag('PERMISSION', 5, false, $value));
// use this to pull out the other info (timemodified and modifierid)
$cap = $DB->get_record_sql("SELECT *
FROM {role_capabilities}
WHERE capability = ?
AND contextid = ?
AND roleid = ?", array($capability, $sitecontext->id, $role->id));
fwrite ($bf, full_tag("TIMEMODIFIED", 5, false, $cap->timemodified));
fwrite ($bf, full_tag("MODIFIERID", 5, false, $cap->modifierid));
fwrite ($bf,end_tag('CAPABILITY',4,true));
}
}
fwrite ($bf,end_tag('CAPABILITIES',3,true));
fwrite ($bf,end_tag('ROLE',2,true));
}
fwrite ($bf,end_tag('ROLES', 1, true));
return $status;
}
//Prints course's general info (table course)
function backup_course_start ($bf,$preferences) {
global $CFG, $DB;
$status = true;
//Course open tag
fwrite ($bf,start_tag("COURSE",1,true));
//Header open tag
fwrite ($bf,start_tag("HEADER",2,true));
//Get info from course
$course = $DB->get_record("course", array("id"=>$preferences->backup_course));
$context = get_context_instance(CONTEXT_COURSE, $course->id);
if ($course) {
//Prints course info
fwrite ($bf,full_tag("ID",3,false,$course->id));
//Obtain the category
$category = $DB->get_record("course_categories", array("id"=>"$course->category"));
if ($category) {
//Prints category info
fwrite ($bf,start_tag("CATEGORY",3,true));
fwrite ($bf,full_tag("ID",4,false,$course->category));
fwrite ($bf,full_tag("NAME",4,false,$category->name));
fwrite ($bf,end_tag("CATEGORY",3,true));
}
//Continues with the course
fwrite ($bf,full_tag("PASSWORD",3,false,$course->password));
fwrite ($bf,full_tag("FULLNAME",3,false,$course->fullname));
fwrite ($bf,full_tag("SHORTNAME",3,false,$course->shortname));
fwrite ($bf,full_tag("IDNUMBER",3,false,$course->idnumber));
fwrite ($bf,full_tag("SUMMARY",3,false,$course->summary));
fwrite ($bf,full_tag("FORMAT",3,false,$course->format));
fwrite ($bf,full_tag("SHOWGRADES",3,false,$course->showgrades));
fwrite ($bf,full_tag("NEWSITEMS",3,false,$course->newsitems));
fwrite ($bf,full_tag("TEACHER",3,false,$course->teacher));
fwrite ($bf,full_tag("TEACHERS",3,false,$course->teachers));
fwrite ($bf,full_tag("STUDENT",3,false,$course->student));
fwrite ($bf,full_tag("STUDENTS",3,false,$course->students));
fwrite ($bf,full_tag("GUEST",3,false,$course->guest));
fwrite ($bf,full_tag("STARTDATE",3,false,$course->startdate));
fwrite ($bf,full_tag("NUMSECTIONS",3,false,$course->numsections));
//fwrite ($bf,full_tag("SHOWRECENT",3,false,$course->showrecent)); INFO: This is out in 1.3
fwrite ($bf,full_tag("MAXBYTES",3,false,$course->maxbytes));
fwrite ($bf,full_tag("SHOWREPORTS",3,false,$course->showreports));
fwrite ($bf,full_tag("GROUPMODE",3,false,$course->groupmode));
fwrite ($bf,full_tag("GROUPMODEFORCE",3,false,$course->groupmodeforce));
fwrite ($bf,full_tag("DEFAULTGROUPINGID",3,false,$course->defaultgroupingid));
fwrite ($bf,full_tag("LANG",3,false,$course->lang));
fwrite ($bf,full_tag("THEME",3,false,$course->theme));
fwrite ($bf,full_tag("COST",3,false,$course->cost));
fwrite ($bf,full_tag("CURRENCY",3,false,$course->currency));
fwrite ($bf,full_tag("MARKER",3,false,$course->marker));
fwrite ($bf,full_tag("VISIBLE",3,false,$course->visible));
fwrite ($bf,full_tag("HIDDENSECTIONS",3,false,$course->hiddensections));
fwrite ($bf,full_tag("TIMECREATED",3,false,$course->timecreated));
fwrite ($bf,full_tag("TIMEMODIFIED",3,false,$course->timemodified));
//If not selected, force metacourse to 0
if (!$preferences->backup_metacourse) {
$status = fwrite ($bf,full_tag("METACOURSE",3,false,'0'));
//else, export the field as is in DB
} else {
$status = fwrite ($bf,full_tag("METACOURSE",3,false,$course->metacourse));
}
fwrite ($bf,full_tag("EXPIRENOTIFY",3,false,$course->expirynotify));
fwrite ($bf,full_tag("NOTIFYSTUDENTS",3,false,$course->notifystudents));
fwrite ($bf,full_tag("EXPIRYTHRESHOLD",3,false,$course->expirythreshold));
fwrite ($bf,full_tag("ENROLLABLE",3,false,$course->enrollable));
fwrite ($bf,full_tag("ENROLSTARTDATE",3,false,$course->enrolstartdate));
fwrite ($bf,full_tag("ENROLENDDATE",3,false,$course->enrolenddate));
fwrite ($bf,full_tag("ENROLPERIOD",3,false,$course->enrolperiod));
fwrite ($bf,full_tag("ENABLECOMPLETION",3,false,$course->enablecompletion));
/// write local course overrides here?
write_role_overrides_xml($bf, $context, 3);
/// write role_assign code here
write_role_assignments_xml($bf, $preferences, $context, 3);
//Print header end
fwrite ($bf,end_tag("HEADER",2,true));
} else {
$status = false;
}
return $status;
}
//Prints course's end tag
function backup_course_end ($bf,$preferences) {
//Course end tag
$status = fwrite ($bf,end_tag("COURSE",1,true));
return $status;
}
//Prints course's metacourse info (table course_meta)
function backup_course_metacourse ($bf,$preferences) {
global $CFG, $DB;
$status = true;
//Get info from meta
$parents = $DB->get_records_sql ("SELECT m.*, c.idnumber, c.shortname
FROM {course_meta} m, {course} c
WHERE m.child_course = ? AND
m.parent_course = c.id", array($preferences->backup_course));
$childs = $DB->get_records_sql ("SELECT m.*, c.idnumber, c.shortname
FROM {course_meta} m, {course} c
WHERE m.parent_course = ? AND
m.child_course = c.id", array($preferences->backup_course));
if ($parents || $childs) {
//metacourse open tag
fwrite ($bf,start_tag("METACOURSE",2,true));
if ($parents) {
fwrite($bf, start_tag("PARENTS",3,true));
//Iterate over every parent
foreach ($parents as $parent) {
//Begin parent
fwrite ($bf,start_tag("PARENT",4,true));
fwrite ($bf,full_tag("ID",5,false,$parent->parent_course));
fwrite ($bf,full_tag("IDNUMBER",5,false,$parent->idnumber));
fwrite ($bf,full_tag("SHORTNAME",5,false,$parent->shortname));
//End parent
fwrite ($bf,end_tag("PARENT",4,true));
}
fwrite ($bf,end_tag("PARENTS",3,true));
}
if ($childs) {
fwrite($bf, start_tag("CHILDS",3,true));
//Iterate over every child
foreach ($childs as $child) {
//Begin parent
fwrite ($bf,start_tag("CHILD",4,true));
fwrite ($bf,full_tag("ID",5,false,$child->child_course));
fwrite ($bf,full_tag("IDNUMBER",5,false,$child->idnumber));
fwrite ($bf,full_tag("SHORTNAME",5,false,$child->shortname));
//End parent
fwrite ($bf,end_tag("CHILD",4,true));
}
fwrite ($bf,end_tag("CHILDS",3,true));
}
//metacourse close tag
$status = fwrite ($bf,end_tag("METACOURSE",3,true));
}
return $status;
}
//Prints course's messages info (tables message, message_read and message_contacts)
function backup_messages ($bf,$preferences) {
global $CFG, $DB;
$status = true;
/// Check we have something to backup
$unreads = $DB->count_records ('message');
$reads = $DB->count_records ('message_read');
$contacts= $DB->count_records ('message_contacts');
if ($unreads || $reads || $contacts) {
$counter = 0;
/// message open tag
fwrite ($bf,start_tag("MESSAGES",2,true));
if ($unreads) {
$rs_unreads = $DB->get_recordset('message');
/// Iterate over every unread
foreach ($rs_unreads as $unread) {
/// start message
fwrite($bf, start_tag("MESSAGE",3,true));
fwrite ($bf,full_tag("ID",4,false,$unread->id));
fwrite ($bf,full_tag("STATUS",4,false,"UNREAD"));
fwrite ($bf,full_tag("USERIDFROM",4,false,$unread->useridfrom));
fwrite ($bf,full_tag("USERIDTO",4,false,$unread->useridto));
fwrite ($bf,full_tag("MESSAGE",4,false,$unread->message));
fwrite ($bf,full_tag("FORMAT",4,false,$unread->format));
fwrite ($bf,full_tag("TIMECREATED",4,false,$unread->timecreated));
fwrite ($bf,full_tag("MESSAGETYPE",4,false,$unread->messagetype));
/// end message
fwrite ($bf,end_tag("MESSAGE",3,true));
/// Do some output
$counter++;
if ($counter % 20 == 0) {
echo ".";
if ($counter % 400 == 0) {
echo "<br />";
}
backup_flush(300);
}
}
$rs_unreads->close();
}
if ($reads) {
$rs_reads = $DB->get_recordset('message_read');
/// Iterate over every unread
foreach ($rs_reads as $read) {
/// start message
fwrite($bf, start_tag("MESSAGE",3,true));
fwrite ($bf,full_tag("ID",4,false,$read->id));
fwrite ($bf,full_tag("STATUS",4,false,"READ"));
fwrite ($bf,full_tag("USERIDFROM",4,false,$read->useridfrom));
fwrite ($bf,full_tag("USERIDTO",4,false,$read->useridto));
fwrite ($bf,full_tag("MESSAGE",4,false,$read->message));
fwrite ($bf,full_tag("FORMAT",4,false,$read->format));
fwrite ($bf,full_tag("TIMECREATED",4,false,$read->timecreated));
fwrite ($bf,full_tag("MESSAGETYPE",4,false,$read->messagetype));
fwrite ($bf,full_tag("TIMEREAD",4,false,$read->timeread));
fwrite ($bf,full_tag("MAILED",4,false,$read->mailed));
/// end message
fwrite ($bf,end_tag("MESSAGE",3,true));
/// Do some output
$counter++;
if ($counter % 20 == 0) {
echo ".";
if ($counter % 400 == 0) {
echo "<br />";
}
backup_flush(300);
}
}
$rs_reads->close();
}
if ($contacts) {
fwrite($bf, start_tag("CONTACTS",3,true));
$rs_contacts = $DB->get_recordset('message_contacts');
/// Iterate over every contact
foreach ($rs_contacts as $contact) {
/// start contact
fwrite($bf, start_tag("CONTACT",4,true));
fwrite ($bf,full_tag("ID",5,false,$contact->id));
fwrite ($bf,full_tag("USERID",5,false,$contact->userid));
fwrite ($bf,full_tag("CONTACTID",5,false,$contact->contactid));
fwrite ($bf,full_tag("BLOCKED",5,false,$contact->blocked));
/// end contact
fwrite ($bf,end_tag("CONTACT",4,true));
/// Do some output
$counter++;
if ($counter % 20 == 0) {
echo ".";
if ($counter % 400 == 0) {
echo "<br />";
}
backup_flush(300);
}
}
$rs_contacts->close();
fwrite($bf, end_tag("CONTACTS",3,true));
}
/// messages close tag
$status = fwrite ($bf,end_tag("MESSAGES",2,true));
}
return $status;
}
//Print blogs info (post table, module=blog, course=0)
function backup_blogs($bf, $preferences) {
global $CFG, $DB;
$status = true;
/// Check we have something to backup
$siteblogs = $DB->count_records('post', array('module'=>'blog', 'courseid'=>0));
if ($siteblogs) {
$counter = 0;
/// blogs open tag
fwrite ($bf, start_tag("BLOGS",2,true));
if ($siteblogs) {
$rs_blogs = $DB->get_recordset('post', array('module'=>'blog', 'courseid'=>0));
/// Iterate over every blog
foreach ($rs_blogs as $blog) {
/// start blog
fwrite($bf, start_tag("BLOG",3,true));
/// blog body
fwrite ($bf,full_tag("ID",4,false,$blog->id));
fwrite ($bf,full_tag("MODULE",4,false,$blog->module));
fwrite ($bf,full_tag("USERID",4,false,$blog->userid));
fwrite ($bf,full_tag("COURSEID",4,false,$blog->courseid));
fwrite ($bf,full_tag("GROUPID",4,false,$blog->groupid));
fwrite ($bf,full_tag("MODULEID",4,false,$blog->moduleid));
fwrite ($bf,full_tag("COURSEMODULEID",4,false,$blog->coursemoduleid));
fwrite ($bf,full_tag("SUBJECT",4,false,$blog->subject));
fwrite ($bf,full_tag("SUMMARY",4,false,$blog->summary));
fwrite ($bf,full_tag("CONTENT",4,false,$blog->content));
fwrite ($bf,full_tag("UNIQUEHASH",4,false,$blog->uniquehash));
fwrite ($bf,full_tag("RATING",4,false,$blog->rating));
fwrite ($bf,full_tag("FORMAT",4,false,$blog->format));
fwrite ($bf,full_tag("ATTACHMENT",4,false,$blog->attachment));
fwrite ($bf,full_tag("PUBLISHSTATE",4,false,$blog->publishstate));
fwrite ($bf,full_tag("LASTMODIFIED",4,false,$blog->lastmodified));
fwrite ($bf,full_tag("CREATED",4,false,$blog->created));
fwrite ($bf,full_tag("USERMODIFIED",4,false,$blog->usermodified));
/// Blog tags
/// Check if we have blog tags to backup
if (!empty($CFG->usetags)) {
if ($tags = tag_get_tags('post', $blog->id)) { //This return them ordered by default
/// Start BLOG_TAGS tag
fwrite ($bf,start_tag("BLOG_TAGS",4,true));
/// Write blog tags fields
foreach ($tags as $tag) {
fwrite ($bf,start_tag("BLOG_TAG",5,true));
fwrite ($bf,full_tag("NAME",6,false,$tag->name));
fwrite ($bf,full_tag("RAWNAME",6,false,$tag->rawname));
fwrite ($bf,end_tag("BLOG_TAG",5,true));
}
/// End BLOG_TAGS tag
fwrite ($bf,end_tag("BLOG_TAGS",4,true));
}
}
/// Blog comments
/// TODO: Blog comments go here (2.0)
/// end blog
fwrite($bf, end_tag("BLOG",3,true));
/// Do some output
$counter++;
if ($counter % 20 == 0) {
echo ".";
if ($counter % 400 == 0) {
echo "<br />";
}
backup_flush(300);
}
}
$rs_blogs-close();
}
/// blogs close tag
$status = fwrite($bf, end_tag("BLOGS",2,true));
}
return $status;
}
/**
* Prints course's blocks info (table block_instance)
*/
function backup_course_blocks ($bf,$preferences) {
global $CFG, $DB;
$status = true;
// Read all of the block table
$blocks = blocks_get_record();
$pages = array();