afbe3de8 |
1 | <?PHP //$Id$ |
2 | //This file contains all the function needed in the backup/restore utility |
3 | //except the mod-related funtions that are into every backuplib.php inside |
4 | //every mod directory |
5 | |
6 | //Insert necessary category ids to backup_ids table |
7 | function insert_category_ids ($course,$backup_unique_code) { |
8 | global $CFG; |
9 | $status = true; |
10 | $status = execute_sql("INSERT INTO {$CFG->prefix}backup_ids |
11 | (backup_code, table_name, old_id) |
12 | SELECT DISTINCT '$backup_unique_code','quiz_categories',t.category |
13 | FROM {$CFG->prefix}quiz_questions t, |
14 | {$CFG->prefix}quiz_question_grades g, |
15 | {$CFG->prefix}quiz q |
16 | WHERE q.course = '$course' AND |
17 | g.quiz = q.id AND |
18 | g.question = t.id",false); |
19 | return $status; |
20 | } |
21 | |
22 | //Delete category ids from backup_ids table |
23 | function delete_category_ids ($backup_unique_code) { |
24 | global $CFG; |
25 | $status = true; |
26 | $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids |
27 | WHERE backup_code = '$backup_unique_code'",false); |
28 | return $status; |
29 | } |
1b502431 |
30 | |
31 | //Calculate the number of users to backup and put their ids in backup_ids |
32 | //Return an array of info (name,value) |
33 | function user_check_backup($course,$backup_unique_code,$backup_users) { |
34 | //$backup_users=0-->all |
35 | // 1-->course |
36 | // 2-->needed-->NOT IMPLEMEMTED |
37 | |
38 | global $CFG; |
cfb9c525 |
39 | global $db; |
1b502431 |
40 | |
cfb9c525 |
41 | $count_users = 0; |
42 | |
43 | //Select all users from user |
44 | $users = get_records ("user"); |
45 | //Iterate over users putting their roles |
46 | foreach ($users as $user) { |
47 | $user->info = ""; |
48 | //Is Admin in tables (not is_admin()) !! |
49 | if (record_exists("user_admins","userid",$user->id)) { |
50 | $user->info .= "admin"; |
51 | $user->role_admin = true; |
52 | } |
53 | //Is Course Creator in tables (not is_coursecreator()) !! |
54 | if (record_exists("user_coursecreators","userid",$user->id)) { |
55 | $user->info .= "coursecreator"; |
56 | $user->role_coursecreator = true; |
57 | } |
58 | //Is Teacher in tables (not is_teacher()) !! |
59 | if (record_exists("user_teachers","course",$course,"userid",$user->id)) { |
60 | $user->info .= "teacher"; |
61 | $user->role_teacher = true; |
62 | } |
63 | //Is Student in tables (not is_student()) !! |
64 | if (record_exists("user_students","course",$course,"userid",$user->id)) { |
65 | $user->info .= "student"; |
66 | $user->role_student = true; |
67 | } |
68 | //Now create the backup_id record |
69 | $backupids_rec->backup_code = $backup_unique_code; |
70 | $backupids_rec->table_name = "user"; |
71 | $backupids_rec->old_id = $user->id; |
72 | $backupids_rec->info = $user->info; |
73 | |
74 | //Insert the record id. backup_users decide it. |
75 | //When all users |
76 | if ($backup_users == 0) { |
77 | $status = insert_record("backup_ids",$backupids_rec,false); |
78 | $count_users++; |
79 | //When course users |
80 | } else if ($backup_users == 1) { |
81 | //Only if user has any role |
82 | if ($backupids_rec->info) { |
83 | $status = insert_record("backup_ids",$backupids_rec,false); |
84 | $count_users++; |
85 | } |
86 | } |
1b502431 |
87 | } |
cfb9c525 |
88 | |
89 | //Prepare Info |
1b502431 |
90 | //Gets the user data |
91 | $info[0][0] = get_string("users"); |
cfb9c525 |
92 | $info[0][1] = $count_users; |
1b502431 |
93 | |
94 | return $info; |
95 | } |
afbe3de8 |
96 | |
3868b9c6 |
97 | //Calculate the number of log entries to backup |
98 | //Return an array of info (name,value) |
99 | function log_check_backup($course) { |
100 | |
101 | global $CFG; |
102 | |
103 | //Execute the insert |
104 | $status = execute_sql($sql_insert,false); |
105 | |
106 | //Now execute the select |
107 | $ids = get_records_sql("SELECT DISTINCT l.id,l.course |
108 | FROM {$CFG->prefix}log l |
109 | WHERE l.course = '$course'"); |
110 | //Gets the user data |
111 | $info[0][0] = get_string("logs"); |
112 | if ($ids) { |
113 | $info[0][1] = count($ids); |
114 | } else { |
115 | $info[0][1] = 0; |
116 | } |
117 | |
118 | return $info; |
119 | } |
120 | |
121 | //Calculate the number of user files to backup |
122 | //Under $CFG->dataroot/users |
123 | //and put them (their path) in backup_ids |
124 | //Return an array of info (name,value) |
125 | function user_files_check_backup($course,$backup_unique_code) { |
126 | |
127 | global $CFG; |
128 | |
129 | $rootdir = $CFG->dataroot."/users"; |
52b53862 |
130 | //Check if directory exists |
131 | if (is_dir($rootdir)) { |
132 | $coursedirs = get_directory_list($rootdir); |
133 | foreach ($coursedirs as $dir) { |
134 | //Extracts user id from file path |
135 | $tok = strtok($dir,"/"); |
136 | if ($tok) { |
137 | $userid = $tok; |
138 | } else { |
139 | $tok = ""; |
140 | } |
141 | //Insert them into backup_files |
142 | $status = execute_sql("INSERT INTO {$CFG->prefix}backup_files |
143 | (backup_code, file_type, path, old_id) |
144 | VALUES |
145 | ('$backup_unique_code','user','$dir','$userid')",false); |
3868b9c6 |
146 | } |
3868b9c6 |
147 | } |
148 | |
149 | //Now execute the select |
150 | $ids = get_records_sql("SELECT DISTINCT b.path, b.old_id |
151 | FROM {$CFG->prefix}backup_files b |
152 | WHERE backup_code = '$backup_unique_code' AND |
153 | file_type = 'user'"); |
154 | //Gets the user data |
155 | $info[0][0] = get_string("files"); |
156 | if ($ids) { |
157 | $info[0][1] = count($ids); |
158 | } else { |
159 | $info[0][1] = 0; |
160 | } |
161 | |
162 | return $info; |
163 | } |
164 | |
165 | //Calculate the number of course files to backup |
166 | //under $CFG->dataroot/$course, except $CFG->moddata |
167 | //and put them (their path) in backup_ids |
168 | //Return an array of info (name,value) |
169 | function course_files_check_backup($course,$backup_unique_code) { |
170 | |
171 | global $CFG; |
172 | |
173 | $rootdir = $CFG->dataroot."/$course"; |
52b53862 |
174 | //Check if directory exists |
175 | if (is_dir($rootdir)) { |
176 | $coursedirs = get_directory_list($rootdir,$CFG->moddata); |
177 | foreach ($coursedirs as $dir) { |
178 | //Insert them into backup_files |
179 | $status = execute_sql("INSERT INTO {$CFG->prefix}backup_files |
180 | (backup_code, file_type, path) |
181 | VALUES |
182 | ('$backup_unique_code','course','$dir')",false); |
183 | } |
3868b9c6 |
184 | } |
185 | |
186 | //Now execute the select |
187 | $ids = get_records_sql("SELECT DISTINCT b.path, b.old_id |
188 | FROM {$CFG->prefix}backup_files b |
189 | WHERE backup_code = '$backup_unique_code' AND |
190 | file_type = 'course'"); |
191 | //Gets the user data |
192 | $info[0][0] = get_string("files"); |
193 | if ($ids) { |
194 | $info[0][1] = count($ids); |
195 | } else { |
196 | $info[0][1] = 0; |
197 | } |
198 | |
199 | return $info; |
200 | } |
b0778a76 |
201 | |
202 | //Delete old data in backup tables (if exists) |
203 | //Two days seems to be apropiate |
204 | function backup_delete_old_data() { |
674b30f5 |
205 | |
206 | global $CFG; |
207 | |
b0778a76 |
208 | //Change this if you want !! |
32ad5774 |
209 | $days = 2; |
b0778a76 |
210 | //End change this |
674b30f5 |
211 | $seconds = $days * 24 * 60 * 60; |
b0778a76 |
212 | $delete_from = time()-$seconds; |
213 | //Now delete from tables |
674b30f5 |
214 | $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids |
215 | WHERE backup_code < '$delete_from'",false); |
216 | if ($status) { |
217 | $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files |
218 | WHERE backup_code < '$delete_from'",false); |
219 | } |
3b8bad6f |
220 | //Now, delete old directory (if exists) |
221 | if ($status) { |
222 | $status = backup_delete_old_dirs($delete_from); |
223 | } |
b0778a76 |
224 | return($status); |
225 | } |
674b30f5 |
226 | |
3b8bad6f |
227 | //Function to delete dirs/files into temp/backup directory |
228 | //older than $delete_from |
229 | function backup_delete_old_dirs($delete_from) { |
230 | |
231 | global $CFG; |
232 | |
233 | $status = true; |
234 | $list = get_directory_list($CFG->dataroot."/temp/backup", "", false); |
235 | foreach ($list as $file) { |
236 | $file_path = $CFG->dataroot."/temp/backup/".$file; |
237 | $moddate = filemtime($file_path); |
238 | if ($status and $moddate < $delete_from) { |
cfb9c525 |
239 | //If directory, recurse |
240 | if (is_dir($file_path)) { |
241 | $status = delete_dir_contents($file_path); |
242 | //There is nothing, delete the directory itself |
243 | if ($status) { |
244 | $status = rmdir($file_path); |
245 | } |
246 | //If file |
247 | } else { |
248 | unlink("$file_path"); |
3b8bad6f |
249 | } |
250 | } |
251 | } |
252 | |
253 | return $status; |
254 | } |
255 | |
674b30f5 |
256 | //Function to check if a directory exists |
257 | //and, optionally, create it |
258 | function check_dir_exists($dir,$create=false) { |
259 | |
260 | global $CFG; |
261 | |
262 | $status = true; |
263 | if(!is_dir($dir)) { |
264 | if (!$create) { |
265 | $status = false; |
266 | } else { |
267 | $status = mkdir ($dir,$CFG->directorypermissions); |
268 | } |
269 | } |
270 | return $status; |
271 | } |
272 | |
273 | //Function to check and create the needed dir to |
274 | //save all the backup |
275 | function check_and_create_backup_dir($backup_unique_code) { |
276 | |
277 | global $CFG; |
278 | |
279 | $status = check_dir_exists($CFG->dataroot."/temp",true); |
280 | if ($status) { |
281 | $status = check_dir_exists($CFG->dataroot."/temp/backup",true); |
282 | } |
283 | if ($status) { |
284 | $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code,true); |
285 | } |
286 | |
287 | return $status; |
288 | } |
289 | |
efead3b9 |
290 | //Function to check and create the needed moddata dir to |
291 | //save all the mod backup files. We always name it moddata |
292 | //to be able to restore it, but in restore we check for |
293 | //$CFG->moddata !! |
294 | function check_and_create_moddata_dir($backup_unique_code) { |
295 | |
296 | global $CFG; |
297 | |
298 | $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/moddata",true); |
299 | |
300 | return $status; |
301 | } |
302 | |
674b30f5 |
303 | //Function to delete all the directory contents recursively |
304 | //Copied from admin/delete.php |
305 | function delete_dir_contents ($rootdir) { |
306 | |
307 | $dir = opendir($rootdir); |
308 | |
309 | $status = true; |
310 | |
311 | while ($file = readdir($dir)) { |
312 | if ($file != "." and $file != "..") { |
313 | $fullfile = "$rootdir/$file"; |
314 | if (filetype($fullfile) == "dir") { |
315 | delete_dir_contents($fullfile); |
316 | if (!rmdir($fullfile)) { |
3b8bad6f |
317 | $status = false; |
674b30f5 |
318 | } |
319 | } else { |
320 | if (!unlink("$fullfile")) { |
3b8bad6f |
321 | $status = false; |
674b30f5 |
322 | } |
323 | } |
324 | } |
325 | } |
326 | closedir($dir); |
327 | |
328 | return $status; |
329 | |
330 | } |
331 | |
332 | //Function to clear (empty) the contents of the backup_dir |
333 | //Copied from admin/delete.php |
334 | function clear_backup_dir($backup_unique_code) { |
335 | |
336 | global $CFG; |
337 | |
338 | $rootdir = $CFG->dataroot."/temp/backup/".$backup_unique_code; |
339 | |
340 | //Delete recursively |
341 | $status = delete_dir_contents($rootdir); |
342 | |
343 | return $status; |
344 | } |
3b8bad6f |
345 | |
346 | //Function to create, open and write header of the xml file |
347 | function backup_open_xml($backup_unique_code) { |
348 | |
349 | global $CFG; |
350 | |
351 | $status = true; |
352 | |
353 | //Open for writing |
cfb9c525 |
354 | |
3b8bad6f |
355 | $file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml"; |
356 | $backup_file = fopen($file,"w"); |
357 | //Writes the header |
358 | $status = fwrite ($backup_file,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
359 | if ($status) { |
360 | $status = fwrite ($backup_file,start_tag("MOODLE_BACKUP",0,true)); |
361 | } |
362 | if ($status) { |
363 | return $backup_file; |
364 | } else { |
365 | return false; |
366 | } |
367 | } |
368 | |
369 | //Close the file |
370 | function backup_close_xml($backup_file) { |
371 | $status = fwrite ($backup_file,end_tag("MOODLE_BACKUP",0,true)); |
372 | return fclose($backup_file); |
373 | } |
374 | |
375 | //Return the xml start tag |
376 | function start_tag($tag,$level=0,$endline=false) { |
377 | if ($endline) { |
378 | $endchar = "\n"; |
379 | } else { |
380 | $endchar = ""; |
381 | } |
382 | return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar; |
383 | } |
384 | |
385 | //Return the xml end tag |
386 | function end_tag($tag,$level=0,$endline=true) { |
387 | if ($endline) { |
388 | $endchar = "\n"; |
389 | } else { |
390 | $endchar = ""; |
391 | } |
392 | return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar; |
393 | } |
394 | |
395 | //Return the start tag, the contents and the end tag |
396 | function full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) { |
397 | $st = start_tag($tag,$level,$endline); |
398 | $co=""; |
399 | if ($to_utf) { |
52b53862 |
400 | $co = utf8_encode(htmlspecialchars($content)); |
3b8bad6f |
401 | } else { |
52b53862 |
402 | $co = htmlspecialchars($content); |
3b8bad6f |
403 | } |
404 | $et = end_tag($tag,0,true); |
405 | return $st.$co.$et; |
406 | } |
407 | |
408 | //Prints General info about the course |
409 | //name, moodle_version (internal and release), backup_version, date, info in file... |
410 | function backup_general_info ($bf,$preferences) { |
674b30f5 |
411 | |
3b8bad6f |
412 | global $CFG; |
413 | |
414 | fwrite ($bf,start_tag("INFO",1,true)); |
415 | |
416 | //The name of the backup |
417 | fwrite ($bf,full_tag("NAME",2,false,$preferences->backup_name)); |
418 | //The moodle_version |
419 | fwrite ($bf,full_tag("MOODLE_VERSION",2,false,$preferences->moodle_version)); |
420 | fwrite ($bf,full_tag("MOODLE_RELEASE",2,false,$preferences->moodle_release)); |
421 | //The backup_version |
422 | fwrite ($bf,full_tag("BACKUP_VERSION",2,false,$preferences->backup_version)); |
423 | fwrite ($bf,full_tag("BACKUP_RELEASE",2,false,$preferences->backup_release)); |
424 | //The date |
425 | fwrite ($bf,full_tag("DATE",2,false,$preferences->backup_unique_code)); |
426 | //Te includes tag |
427 | fwrite ($bf,start_tag("DETAILS",2,true)); |
428 | //Now, go to mod element of preferences to print its status |
429 | foreach ($preferences->mods as $element) { |
430 | //Calculate info |
431 | $included = "false"; |
432 | $userinfo = "false"; |
433 | if ($element->backup) { |
434 | $included = "true"; |
435 | if ($element->userinfo) { |
436 | $userinfo = "true"; |
437 | } |
438 | } |
439 | //Prints the mod start |
440 | fwrite ($bf,start_tag("MOD",3,true)); |
441 | fwrite ($bf,full_tag("NAME",4,false,$element->name)); |
442 | fwrite ($bf,full_tag("INCLUDED",4,false,$included)); |
443 | fwrite ($bf,full_tag("USERINFO",4,false,$userinfo)); |
444 | |
445 | //Print the end |
446 | fwrite ($bf,end_tag("MOD",3,true)); |
447 | } |
448 | //The user in backup |
449 | if ($preferences->backup_users == 1) { |
52b53862 |
450 | fwrite ($bf,full_tag("USERS",3,false,"course")); |
3b8bad6f |
451 | } else { |
52b53862 |
452 | fwrite ($bf,full_tag("USERS",3,false,"all")); |
3b8bad6f |
453 | } |
454 | //The logs in backup |
455 | if ($preferences->backup_logs == 1) { |
456 | fwrite ($bf,full_tag("LOGS",3,false,"true")); |
457 | } else { |
458 | fwrite ($bf,full_tag("LOGS",3,false,"false")); |
459 | } |
460 | //The user files |
461 | if ($preferences->backup_user_files == 1) { |
462 | fwrite ($bf,full_tag("USERFILES",3,false,"true")); |
463 | } else { |
464 | fwrite ($bf,full_tag("USERFILES",3,false,"false")); |
465 | } |
466 | //The course files |
467 | if ($preferences->backup_course_files == 1) { |
468 | fwrite ($bf,full_tag("COURSEFILES",3,false,"true")); |
469 | } else { |
470 | fwrite ($bf,full_tag("COURSEFILES",3,false,"false")); |
471 | } |
472 | |
473 | fwrite ($bf,end_tag("DETAILS",2,true)); |
474 | |
475 | |
52b53862 |
476 | $status = fwrite ($bf,end_tag("INFO",1,true)); |
477 | |
478 | return $status; |
479 | } |
480 | |
481 | //Prints course's general info (table course) |
482 | function backup_course_start ($bf,$preferences) { |
483 | |
484 | global $CFG; |
485 | |
486 | $status = true; |
487 | |
488 | //Course open tag |
489 | fwrite ($bf,start_tag("COURSE",1,true)); |
490 | |
491 | //Get info from course |
492 | $course=false; |
493 | if ($courses = get_records("course","id",$preferences->backup_course)) { |
494 | $course = $courses[$preferences->backup_course]; |
495 | } |
496 | if ($course) { |
497 | //Prints course info |
498 | fwrite ($bf,full_tag("ID",2,false,$course->id)); |
499 | //Obtain the category |
500 | $category = false; |
501 | if ($categories = get_records("course_categories","id","$course->category")) { |
502 | $category = $categories[$course->category]; |
503 | } |
504 | if ($category) { |
505 | //Prints category info |
506 | fwrite ($bf,start_tag("CATEGORY",2,true)); |
507 | fwrite ($bf,full_tag("ID",3,false,$course->category)); |
508 | fwrite ($bf,full_tag("NAME",3,false,$category->name)); |
509 | fwrite ($bf,end_tag("CATEGORY",2,true)); |
510 | } |
511 | //Continues with the course |
512 | fwrite ($bf,full_tag("PASSWORD",2,false,$course->password)); |
513 | fwrite ($bf,full_tag("FULLNAME",2,false,$course->fullname)); |
514 | fwrite ($bf,full_tag("SHORTNAME",2,false,$course->shortname)); |
515 | fwrite ($bf,full_tag("SUMMARY",2,false,$course->summary)); |
516 | fwrite ($bf,full_tag("FORMAT",2,false,$course->format)); |
517 | fwrite ($bf,full_tag("NEWSITEMS",2,false,$course->newsitems)); |
518 | fwrite ($bf,full_tag("TEACHER",2,false,$course->teacher)); |
519 | fwrite ($bf,full_tag("TEACHERS",2,false,$course->teachers)); |
520 | fwrite ($bf,full_tag("STUDENT",2,false,$course->student)); |
521 | fwrite ($bf,full_tag("STUDENTS",2,false,$course->students)); |
522 | fwrite ($bf,full_tag("GUEST",2,false,$course->guest)); |
523 | fwrite ($bf,full_tag("STARDATE",2,false,$course->stardate)); |
524 | fwrite ($bf,full_tag("NUMSECTIONS",2,false,$course->numsections)); |
525 | fwrite ($bf,full_tag("SHOWRECENT",2,false,$course->showrecent)); |
526 | fwrite ($bf,full_tag("MARKER",2,false,$course->marker)); |
527 | fwrite ($bf,full_tag("TIMECREATED",2,false,$course->timecreated)); |
528 | $status = fwrite ($bf,full_tag("TIMEMODIFIED",2,false,$course->timemodified)); |
529 | } else { |
530 | $status = false; |
531 | } |
532 | |
533 | return $status; |
534 | } |
535 | |
536 | //Prints course's end tag |
537 | function backup_course_end ($bf,$preferences) { |
538 | |
539 | //Course end tag |
540 | $status = fwrite ($bf,end_tag("COURSE",1,true)); |
541 | |
542 | return $status; |
543 | |
544 | } |
545 | |
546 | //Prints course's sections info (table course_sections) |
cfb9c525 |
547 | function backup_course_sections ($bf,$preferences) { |
52b53862 |
548 | |
549 | global $CFG; |
550 | |
551 | $status = true; |
552 | |
553 | |
554 | //Get info from sections |
555 | $section=false; |
556 | if ($sections = get_records("course_sections","course",$preferences->backup_course,"section")) { |
557 | //Section open tag |
558 | fwrite ($bf,start_tag("SECTIONS",2,true)); |
559 | //Iterate over every section (ordered by section) |
560 | foreach ($sections as $section) { |
561 | //Begin Section |
562 | fwrite ($bf,start_tag("SECTION",3,true)); |
563 | fwrite ($bf,full_tag("ID",4,false,$section->id)); |
564 | fwrite ($bf,full_tag("NUMBER",4,false,$section->section)); |
565 | fwrite ($bf,full_tag("SUMMARY",4,false,$section->summary)); |
566 | fwrite ($bf,full_tag("VISIBLE",4,false,$section->visible)); |
cfb9c525 |
567 | //Now print the mods in section |
568 | backup_course_modules ($bf,$preferences,$section); |
52b53862 |
569 | //End section |
cfb9c525 |
570 | fwrite ($bf,end_tag("SECTION",3,true)); |
52b53862 |
571 | } |
572 | //Section close tag |
573 | $status = fwrite ($bf,end_tag("SECTIONS",2,true)); |
574 | } |
575 | |
576 | return $status; |
577 | |
3b8bad6f |
578 | } |
cfb9c525 |
579 | |
580 | //Prints course's modules info (table course_modules) |
581 | //Only for selected mods in preferences |
582 | function backup_course_modules ($bf,$preferences,$section) { |
583 | |
584 | global $CFG; |
585 | |
586 | $status = true; |
587 | |
588 | $first_record = true; |
589 | |
590 | //Now print the mods in section |
591 | //Extracts mod id from sequence |
592 | $tok = strtok($section->sequence,","); |
593 | while ($tok) { |
594 | //Get module's type |
595 | $moduletype = get_module_type ($preferences->backup_course,$tok); |
596 | //Check if we've selected to backup that type |
597 | if ($moduletype and $preferences->mods[$moduletype]->backup) { |
598 | $selected = true; |
599 | } else { |
600 | $selected = false; |
601 | } |
602 | |
603 | if ($selected) { |
604 | //Gets course_module data from db |
605 | $course_module = get_records ("course_modules","id",$tok); |
606 | //If it's the first, pring MODS tag |
607 | if ($first_record) { |
608 | fwrite ($bf,start_tag("MODS",4,true)); |
609 | $first_record = false; |
610 | } |
611 | //Print mod info from course_modules |
612 | fwrite ($bf,start_tag("MOD",5,true)); |
613 | //Save neccesary info to backup_ids |
614 | fwrite ($bf,full_tag("ID",6,false,$tok)); |
615 | fwrite ($bf,full_tag("TYPE",6,false,$moduletype)); |
616 | fwrite ($bf,full_tag("INSTANCE",6,false,$course_module[$tok]->instance)); |
617 | fwrite ($bf,full_tag("DELETED",6,false,$course_module[$tok]->deleted)); |
618 | fwrite ($bf,full_tag("SCORE",6,false,$course_module[$tok]->score)); |
619 | fwrite ($bf,full_tag("VISIBLE",6,false,$course_module[$tok]->visible)); |
620 | fwrite ($bf,end_tag("MOD",5,true)); |
621 | } |
622 | //check for next |
623 | $tok = strtok(","); |
624 | } |
625 | |
626 | //Si ha habido modulos, final de MODS |
627 | if (!$first_record) { |
628 | $status =fwrite ($bf,end_tag("MODS",4,true)); |
629 | } |
630 | |
631 | return $status; |
632 | } |
633 | |
634 | //Returns the module type of a course_module's id in a course |
635 | function get_module_type ($courseid,$moduleid) { |
636 | |
637 | global $CFG; |
638 | |
639 | $results = get_records_sql ("SELECT cm.id, m.name |
640 | FROM {$CFG->prefix}course_modules cm, |
641 | {$CFG->prefix}modules m |
642 | WHERE cm.course = '$courseid' AND |
643 | cm.id = '$moduleid' AND |
644 | m.id = cm.module"); |
645 | |
646 | if ($results) { |
647 | $name = $results[$moduleid]->name; |
648 | } else { |
649 | $name = false; |
650 | } |
651 | |
652 | |
653 | return $name; |
654 | } |
655 | |
656 | //Print users to xml |
657 | //Only users previously calculated in backup_ids will output |
658 | // |
659 | function backup_user_info ($bf,$preferences) { |
660 | |
661 | global $CFG; |
662 | |
663 | $status = true; |
664 | |
665 | $users = get_records_sql("SELECT u.old_id, u.table_name,u.info |
666 | FROM {$CFG->prefix}backup_ids u |
667 | WHERE u.backup_code = '$preferences->backup_unique_code' AND |
668 | u.table_name = 'user'"); |
669 | |
670 | //If we have users to backup |
671 | if ($users) { |
672 | //Begin Users tag |
673 | fwrite ($bf,start_tag("USERS",2,true)); |
674 | //With every user |
675 | foreach ($users as $user) { |
676 | //Get user data from table |
677 | $user_data = get_record("user","id",$user->old_id); |
678 | //Begin User tag |
679 | fwrite ($bf,start_tag("USER",3,true)); |
680 | //Output all user data |
681 | fwrite ($bf,full_tag("ID",4,false,$user_data->id)); |
682 | fwrite ($bf,full_tag("CONFIRMED",4,false,$user_data->confirmed)); |
683 | fwrite ($bf,full_tag("DELETED",4,false,$user_data->deleted)); |
684 | fwrite ($bf,full_tag("USERNAME",4,false,$user_data->username)); |
685 | fwrite ($bf,full_tag("PASSWORD",4,false,$user_data->password)); |
686 | fwrite ($bf,full_tag("IDNUMBER",4,false,$user_data->idnumber)); |
687 | fwrite ($bf,full_tag("FIRSTNAME",4,false,$user_data->firsname)); |
688 | fwrite ($bf,full_tag("LASTNAME",4,false,$user_data->lastname)); |
689 | fwrite ($bf,full_tag("EMAIL",4,false,$user_data->email)); |
690 | fwrite ($bf,full_tag("ICQ",4,false,$user_data->icq)); |
691 | fwrite ($bf,full_tag("PHONE1",4,false,$user_data->phone1)); |
692 | fwrite ($bf,full_tag("PHONE2",4,false,$user_data->phone2)); |
693 | fwrite ($bf,full_tag("INSTITUTION",4,false,$user_data->institution)); |
694 | fwrite ($bf,full_tag("DEPARTMENT",4,false,$user_data->department)); |
695 | fwrite ($bf,full_tag("ADDRESS",4,false,$user_data->address)); |
696 | fwrite ($bf,full_tag("CITY",4,false,$user_data->city)); |
697 | fwrite ($bf,full_tag("COUNTRY",4,false,$user_data->country)); |
698 | fwrite ($bf,full_tag("LANG",4,false,$user_data->lang)); |
699 | fwrite ($bf,full_tag("TIMEZONE",4,false,$user_data->timezone)); |
700 | fwrite ($bf,full_tag("FIRSTACCESS",4,false,$user_data->firstaccess)); |
701 | fwrite ($bf,full_tag("LASTACCESS",4,false,$user_data->lastaccess)); |
702 | fwrite ($bf,full_tag("LASTLOGIN",4,false,$user_data->lastlogin)); |
703 | fwrite ($bf,full_tag("CURRENTLOGIN",4,false,$user_data->currentlogin)); |
704 | fwrite ($bf,full_tag("LASTIP",4,false,$user_data->lastIP)); |
705 | fwrite ($bf,full_tag("SECRET",4,false,$user_data->secret)); |
706 | fwrite ($bf,full_tag("PICTURE",4,false,$user_data->picture)); |
707 | fwrite ($bf,full_tag("URL",4,false,$user_data->url)); |
708 | fwrite ($bf,full_tag("DESCRIPTION",4,false,$user_data->description)); |
709 | fwrite ($bf,full_tag("MAILFORMAT",4,false,$user_data->mailformat)); |
710 | fwrite ($bf,full_tag("MAILDISPLAY",4,false,$user_data->maildisplay)); |
711 | fwrite ($bf,full_tag("HTMLEDITOR",4,false,$user_data->htmleditor)); |
712 | fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$user_data->timemodified)); |
713 | |
714 | //Output every user role (with its associated info) |
715 | $user->isadmin = strpos($user->info,"admin"); |
716 | $user->iscoursecreator = strpos($user->info,"coursecreator"); |
717 | $user->isteacher = strpos($user->info,"teacher"); |
718 | $user->isstudent = strpos($user->info,"student"); |
719 | if ($user->isadmin!==false or |
720 | $user->iscoursecreator!==false or |
721 | $user->isteacher!==false or |
722 | $user->isstudent!==false) { |
723 | //Begin ROLES tag |
724 | fwrite ($bf,start_tag("ROLES",4,true)); |
725 | //PRINT ROLE INFO |
726 | //Admins |
727 | if ($user->isadmin!==false) { |
728 | //Print ROLE start |
729 | fwrite ($bf,start_tag("ROLE",5,true)); |
730 | //Print Role info |
731 | fwrite ($bf,full_tag("TYPE",6,false,"admin")); |
732 | //Print ROLE end |
733 | fwrite ($bf,end_tag("ROLE",5,true)); |
734 | } |
735 | //CourseCreator |
736 | if ($user->iscoursecreator!==false) { |
737 | //Print ROLE start |
738 | fwrite ($bf,start_tag("ROLE",5,true)); |
739 | //Print Role info |
740 | fwrite ($bf,full_tag("TYPE",6,false,"coursecreator")); |
741 | //Print ROLE end |
742 | fwrite ($bf,end_tag("ROLE",5,true)); |
743 | } |
744 | //Teacher |
745 | if ($user->isteacher!==false) { |
746 | //Print ROLE start |
747 | fwrite ($bf,start_tag("ROLE",5,true)); |
748 | //Print Role info |
749 | fwrite ($bf,full_tag("TYPE",6,false,"teacher")); |
750 | //Get specific info for teachers |
751 | $tea = get_record("user_teachers","userid",$user->old_id,"course",$preferences->backup_course); |
752 | fwrite ($bf,full_tag("AUTHORITY",6,false,$tea->authority)); |
753 | fwrite ($bf,full_tag("TEA_ROLE",6,false,$tea->role)); |
754 | //Print ROLE end |
755 | fwrite ($bf,end_tag("ROLE",5,true)); |
756 | } |
757 | //Student |
758 | if ($user->isstudent!==false) { |
759 | //Print ROLE start |
760 | fwrite ($bf,start_tag("ROLE",5,true)); |
761 | //Print Role info |
762 | fwrite ($bf,full_tag("TYPE",6,false,"student")); |
763 | //Get specific info for students |
764 | $stu = get_record("user_students","userid",$user->old_id,"course",$preferences->backup_course); |
765 | fwrite ($bf,full_tag("TIMESTART",6,false,$stu->timestart)); |
766 | fwrite ($bf,full_tag("TIMEEND",6,false,$stu->timeend)); |
767 | fwrite ($bf,full_tag("TIME",6,false,$stu->time)); |
768 | //Print ROLE end |
769 | fwrite ($bf,end_tag("ROLE",5,true)); |
770 | } |
771 | |
772 | |
773 | //End ROLES tag |
774 | fwrite ($bf,end_tag("ROLES",4,true)); |
775 | } |
776 | //End User tag |
777 | fwrite ($bf,end_tag("USER",3,true)); |
778 | } |
779 | //End Users tag |
780 | fwrite ($bf,end_tag("USERS",2,true)); |
781 | } else { |
782 | //There isn't users. Impossible !! |
783 | $status = false; |
784 | } |
785 | |
786 | return $status; |
537718f8 |
787 | } |
788 | |
789 | //Backup log info (time ordered) |
790 | function backup_log_info($bf,$preferences) { |
cfb9c525 |
791 | |
537718f8 |
792 | global $CFG; |
793 | |
794 | $status = true; |
795 | |
efead3b9 |
796 | $logs = get_records ("log","course",$preferences->backup_course,"time"); |
537718f8 |
797 | |
798 | //We have logs |
799 | if ($logs) { |
800 | //Pring logs header |
801 | fwrite ($bf,start_tag("LOGS",2,true)); |
802 | //Iterate |
803 | foreach ($logs as $log) { |
804 | //See if it is a valid module to backup |
805 | if ($log->module == "course" or |
efead3b9 |
806 | $log->module == "user" or |
807 | $preferences->mods[$log->module]->backup == 1) { |
537718f8 |
808 | //Begin log tag |
809 | fwrite ($bf,start_tag("LOG",3,true)); |
810 | |
efead3b9 |
811 | //Output log tag |
537718f8 |
812 | fwrite ($bf,full_tag("ID",4,false,$log->id)); |
813 | fwrite ($bf,full_tag("TIME",4,false,$log->time)); |
814 | fwrite ($bf,full_tag("USERID",4,false,$log->userid)); |
815 | fwrite ($bf,full_tag("IP",4,false,$log->ip)); |
816 | fwrite ($bf,full_tag("MODULE",4,false,$log->module)); |
817 | fwrite ($bf,full_tag("ACTION",4,false,$log->action)); |
818 | fwrite ($bf,full_tag("URL",4,false,$log->url)); |
819 | fwrite ($bf,full_tag("INFO",4,false,$log->info)); |
820 | |
efead3b9 |
821 | //End log tag |
537718f8 |
822 | fwrite ($bf,end_tag("LOG",3,true)); |
823 | } |
824 | } |
efead3b9 |
825 | //End logs tag |
537718f8 |
826 | $status = fwrite ($bf,end_tag("LOGS",2,true)); |
827 | } |
efead3b9 |
828 | return $status; |
829 | } |
537718f8 |
830 | |
efead3b9 |
831 | //Start the modules tag |
832 | function backup_modules_start ($bf,$preferences) { |
833 | |
834 | return fwrite ($bf,start_tag("MODULES",2,true)); |
835 | } |
836 | |
837 | //End the modules tag |
838 | function backup_modules_end ($bf,$preferences) { |
839 | |
840 | return fwrite ($bf,end_tag("MODULES",2,true)); |
841 | } |
842 | |
843 | //This function makes all the necesary calls to every mod |
844 | //to export itself and its files !!! |
845 | function backup_module($bf,$preferences,$module) { |
846 | |
847 | global $CFG; |
848 | |
849 | $status = true; |
850 | |
851 | //First, re-check if necessary functions exists |
852 | $modbackup = $module."_backup_mods"; |
853 | if (function_exists($modbackup)) { |
854 | //Call the function |
855 | $status = $modbackup($bf,$preferences); |
856 | } else { |
857 | //Something was wrong. Function should exist. |
858 | $status = false; |
859 | } |
860 | |
861 | return $status; |
537718f8 |
862 | |
efead3b9 |
863 | } |
537718f8 |
864 | |
efead3b9 |
865 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
866 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
867 | //This functions are used to copy any file or directory ($from_file) |
868 | //to a new file or directory ($to_file). It works recursively and |
869 | //mantains file perms. |
870 | //I've copied it from: http://www.php.net/manual/en/function.copy.php |
871 | //Little modifications done |
872 | |
873 | function backup_copy_file ($from_file,$to_file) { |
874 | if (is_file($from_file)) { |
875 | $perms=fileperms($from_file); |
876 | return copy($from_file,$to_file) && chmod($to_file,$perms); |
877 | } |
878 | else if (is_dir($from_file)) { |
879 | return backup_copy_dir($from_file,$to_file); |
880 | } |
881 | else{ |
882 | return false; |
883 | } |
cfb9c525 |
884 | } |
efead3b9 |
885 | |
886 | function backup_copy_dir($from_file,$to_file) { |
887 | if (!is_dir($to_file)) { |
888 | mkdir($to_file); |
889 | chmod("$to_file",0777); |
890 | } |
891 | $dir = opendir($from_file); |
892 | while ($file=readdir($dir)) { |
893 | if ($file=="." || $file=="..") { |
894 | continue; |
895 | } |
4f6ae69e |
896 | $status = backup_copy_file ("$from_file/$file","$to_file/$file"); |
efead3b9 |
897 | } |
4f6ae69e |
898 | closedir($dir); |
899 | return $status; |
efead3b9 |
900 | } |
901 | ///Ends copy file/dirs functions |
902 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
903 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
904 | |
905 | |
906 | |
907 | |
afbe3de8 |
908 | ?> |