93e2d1b9f1dd7d368e2736ca6f5338d38ee19470
[moodle.git] / backup / backuplib.php
1 <?PHP //$Id$
2     //This file contains all the function needed in the backup utility
3     //except the mod-related funtions that are into every backuplib.php inside
4     //every mod directory
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     }
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 (needed + enrolled)
36         //              2-->none
38         global $CFG;
39         global $db;
41         $count_users = 0;
43         //If we've selected none, simply return 0
44         if ($backup_users == 0 or $backup_users == 1) {
45         
46             //Calculate needed users (calling every xxxx_get_participants function + scales users)
47             $needed_users = backup_get_needed_users($course);
49             //Calculate enrolled users (students + teachers)
50             $enrolled_users = backup_get_enrolled_users($course);
52             //Calculate all users (every record in users table)
53             $all_users = backup_get_all_users();
55             //Calculate course users (needed + enrolled)
56             //First, needed
57             $course_users = $needed_users;
58         
59             //Now, enrolled
60             if ($enrolled_users) {
61                 foreach ($enrolled_users as $enrolled_user) {
62                     $course_users[$enrolled_user->id]->id = $enrolled_user->id; 
63                 }
64             }
65        
66             //Now, depending of parameters, create $backupable_users
67             if ($backup_users == 0) {
68                 $backupable_users = $all_users;
69             } else {
70                 $backupable_users = $course_users;
71             }
73             //If we have backupable users
74             if ($backupable_users) {
75                 //Iterate over users putting their roles
76                 foreach ($backupable_users as $backupable_user) {
77                     $backupable_user->info = "";
78                     //Is Admin in tables (not is_admin()) !!
79                     if (record_exists("user_admins","userid",$backupable_user->id)) {
80                         $backupable_user->info .= "admin";
81                     }
82                     //Is Course Creator in tables (not is_coursecreator()) !!
83                     if (record_exists("user_coursecreators","userid",$backupable_user->id)) {
84                         $backupable_user->info .= "coursecreator";
85                     }
86                     //Is Teacher in tables (not is_teacher()) !!
87                     if (record_exists("user_teachers","course",$course,"userid",$backupable_user->id)) {
88                         $backupable_user->info .= "teacher";
89                     }
90                     //Is Student in tables (not is_student()) !!
91                     if (record_exists("user_students","course",$course,"userid",$backupable_user->id)) {
92                         $backupable_user->info .= "student";
93                     }
94                     //Is needed user (exists in needed_users) 
95                     if (isset($needed_users[$backupable_user->id])) {
96                         $backupable_user->info .= "needed";
97                     }
98                     //Now create the backup_id record
99                     $backupids_rec->backup_code = $backup_unique_code;
100                     $backupids_rec->table_name = "user";
101                     $backupids_rec->old_id = $backupable_user->id;
102                     $backupids_rec->info = $backupable_user->info;
103         
104                     //Insert the record id. backup_users decide it.
105                     //When all users
106                     $status = insert_record("backup_ids",$backupids_rec,false,"backup_code");
107                     $count_users++;
108                 }
109                 //Do some output     
110                 backup_flush(30);
111             }
112         }
114         //Prepare Info
115         //Gets the user data
116         $info[0][0] = get_string("users");
117         $info[0][1] = $count_users;
119         return $info;
120     }
122     //Returns every needed user (participant) in a course
123     //It uses the xxxx_get_participants() function
124     //plus users needed to backup scales.
125     //WARNING: It returns only NEEDED users, not every 
126     //   every student and teacher in the course, so it
127     //must be merged with backup_get_enrrolled_users !!
129     function backup_get_needed_users ($courseid) {
130         
131         global $CFG, $THEME;
133         $result = false;
135         $course_modules = get_records_sql ("SELECT cm.id, m.name, cm.instance
136                                             FROM {$CFG->prefix}modules m,
137                                                  {$CFG->prefix}course_modules cm
138                                             WHERE m.id = cm.module and
139                                                   cm.course = '$courseid'");
141         if ($course_modules) {
142             //Iterate over each module
143             foreach ($course_modules as $course_module) {
144                 $modlib = "$CFG->dirroot/mod/$course_module->name/lib.php";
145                 $modgetparticipants = $course_module->name."_get_participants";
146                 if (file_exists($modlib)) {
147                     include_once($modlib);
148                     if (function_exists($modgetparticipants)) {
149                         $module_participants = $modgetparticipants($course_module->instance);
150                         //Add them to result
151                         if ($module_participants) {
152                             foreach ($module_participants as $module_participant) {
153                                 $result[$module_participant->id]->id = $module_participant->id; 
154                             }
155                         }
156                     }
157                  }            
158             }
159         }
161         //Now, add scales users (from site and course scales)
162         //Get users
163         $scaleusers = get_records_sql("SELECT DISTINCT userid,userid
164                                        FROM {$CFG->prefix}scale
165                                        WHERE courseid = '0' or courseid = '$courseid'");
166         //Add scale users to results
167         if ($scaleusers) {
168             foreach ($scaleusers as $scaleuser) {
169                 //If userid != 0
170                 if ($scaleuser->userid != 0) {
171                     $result[$scaleuser->userid]->id = $scaleuser->userid;
172                 }
173             }
174         }
175     
176         return $result;
178     }
180     //Returns every enrolled user (student and teacher) in a course
182     function backup_get_enrolled_users ($courseid) {
184         global $CFG;
186         $result = false;
187         
188         //Get teachers
189         $teachers = get_records_sql("SELECT DISTINCT userid,userid
190                      FROM {$CFG->prefix}user_teachers
191                      WHERE course = '$courseid'");
192         //Get students
193         $students = get_records_sql("SELECT DISTINCT userid,userid
194                      FROM {$CFG->prefix}user_students
195                      WHERE course = '$courseid'");
196         //Add teachers
197         if ($teachers) {
198             foreach ($teachers as $teacher) {
199                 $result[$teacher->userid]->id = $teacher->userid;
200             }
201         }
202         //Add students
203         if ($students) {
204             foreach ($students as $student) {
205                 $result[$student->userid]->id = $student->userid;
206             }
207         }
209         return $result;
210     }
212     //Returns all users (every record in users table)
214     function backup_get_all_users() {
216         global $CFG;
218         $result = false;
220         //Get users
221         $users = get_records_sql("SELECT DISTINCT id,id
222                                   FROM {$CFG->prefix}user");
223         //Add users
224         if ($users) {
225             foreach ($users as $user) {
226                 $result[$user->id]->id = $user->id;
227             }
228         }
230         return $result;
231     }
233     //Calculate the number of log entries to backup
234     //Return an array of info (name,value)
235     function log_check_backup($course) {
237         global $CFG;
239         //Now execute the count
240         $ids = count_records("log","course",$course);
242         //Gets the user data
243         $info[0][0] = get_string("logs");
244         if ($ids) {
245             $info[0][1] = $ids;
246         } else {
247             $info[0][1] = 0;
248         }
250         return $info;
251     }
253     //Calculate the number of user files to backup
254     //Under $CFG->dataroot/users
255     //and put them (their path) in backup_ids
256     //Return an array of info (name,value)
257     function user_files_check_backup($course,$backup_unique_code) {
259         global $CFG;
261         $rootdir = $CFG->dataroot."/users";
262         //Check if directory exists
263         if (is_dir($rootdir)) {
264             $coursedirs = get_directory_list($rootdir);
265             foreach ($coursedirs as $dir) {
266                 //Extracts user id from file path
267                 $tok = strtok($dir,"/");
268                 if ($tok) {
269                    $userid = $tok;
270                 } else {
271                    $tok = "";
272                 }
273                 //Look it is a backupable user
274                 $data = get_record ("backup_ids","backup_code","$backup_unique_code",
275                                                  "table_name","user",
276                                                  "old_id",$userid);
277                 if ($data) {
278                     //Insert them into backup_files
279                     $status = execute_sql("INSERT INTO {$CFG->prefix}backup_files
280                                                (backup_code, file_type, path, old_id)
281                                            VALUES
282                                                ('$backup_unique_code','user','$dir','$userid')",false);
283                 }
284                 //Do some output
285                 backup_flush(30);
286             }
287         }
289         //Now execute the select
290         $ids = get_records_sql("SELECT DISTINCT b.path, b.old_id
291                                 FROM {$CFG->prefix}backup_files b
292                                 WHERE backup_code = '$backup_unique_code' AND
293                                       file_type = 'user'");
294         //Gets the user data
295         $info[0][0] = get_string("files");
296         if ($ids) {
297             $info[0][1] = count($ids);
298         } else {
299             $info[0][1] = 0;
300         }
302         return $info;  
303     }
305     //Calculate the number of course files to backup
306     //under $CFG->dataroot/$course, except $CFG->moddata, and backupdata
307     //and put them (their path) in backup_ids
308     //Return an array of info (name,value)
309     function course_files_check_backup($course,$backup_unique_code) {
311         global $CFG;
313         $rootdir = $CFG->dataroot."/$course";
314         //Check if directory exists
315         if (is_dir($rootdir)) {
316             $coursedirs = get_directory_list($rootdir,$CFG->moddata);
317             $backupdata_dir = "backupdata";
318             foreach ($coursedirs as $dir) {
319                 //Check it isn't backupdata_dir
320                 if (strpos($dir,$backupdata_dir)!==0) {
321                     //Insert them into backup_files
322                     $status = execute_sql("INSERT INTO {$CFG->prefix}backup_files
323                                                   (backup_code, file_type, path)
324                                            VALUES
325                                               ('$backup_unique_code','course','$dir')",false);
326                 }
327             //Do some output
328             backup_flush(30);
329             }
330         }
332         //Now execute the select
333         $ids = get_records_sql("SELECT DISTINCT b.path, b.old_id
334                                 FROM {$CFG->prefix}backup_files b
335                                 WHERE backup_code = '$backup_unique_code' AND
336                                       file_type = 'course'");
337         //Gets the user data
338         $info[0][0] = get_string("files");
339         if ($ids) {
340             $info[0][1] = count($ids);
341         } else {
342             $info[0][1] = 0;
343         }
345         return $info; 
346     }
347    
348     //Function to check and create the needed moddata dir to
349     //save all the mod backup files. We always name it moddata
350     //to be able to restore it, but in restore we check for
351     //$CFG->moddata !!
352     function check_and_create_moddata_dir($backup_unique_code) {
353   
354         global $CFG;
356             $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/moddata",true);
358         return $status;
359     }
361     //Function to check and create the "user_files" dir to
362     //save all the user files we need from "users" dir
363     function check_and_create_user_files_dir($backup_unique_code) {
364  
365         global $CFG;
367             $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/user_files",true);
369         return $status;
370     }
372     //Function to check and create the "course_files" dir to
373     //save all the course files we need from "CFG->datadir/course" dir
374     function check_and_create_course_files_dir($backup_unique_code) {
376         global $CFG;
378             $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/course_files",true);
380         return $status;
381     }
383     //Function to create, open and write header of the xml file
384     function backup_open_xml($backup_unique_code) {
386         global $CFG;
387         
388         $status = true;
390         //Open for writing
392         $file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
393         $backup_file = fopen($file,"w");
394         //Writes the header
395         $status = fwrite ($backup_file,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
396         if ($status) {
397             $status = fwrite ($backup_file,start_tag("MOODLE_BACKUP",0,true));
398         }
399         if ($status) {
400             return $backup_file;
401         } else {
402             return false;
403         }
404     }
406     //Close the file
407     function backup_close_xml($backup_file) {
408         $status = fwrite ($backup_file,end_tag("MOODLE_BACKUP",0,true));
409         return fclose($backup_file);
410     }
412     //Return the xml start tag 
413     function start_tag($tag,$level=0,$endline=false) {
414         if ($endline) {
415            $endchar = "\n";
416         } else {
417            $endchar = "";
418         }
419         return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar;
420     }
421     
422     //Return the xml end tag 
423     function end_tag($tag,$level=0,$endline=true) {
424         if ($endline) {
425            $endchar = "\n";
426         } else {
427            $endchar = "";
428         }
429         return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
430     }
431     
432     //Return the start tag, the contents and the end tag
433     function full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
434         //Here we encode absolute links
435         $content = backup_encode_absolute_links($content);
436         $st = start_tag($tag,$level,$endline);
437         $co="";
438         if ($to_utf) {
439             $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
440         } else {
441             $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
442         }
443         $et = end_tag($tag,0,true);
444         return $st.$co.$et;
445     }
447     //Prints General info about the course
448     //name, moodle_version (internal and release), backup_version, date, info in file...
449     function backup_general_info ($bf,$preferences) {
450     
451         global $CFG;
452         
453         fwrite ($bf,start_tag("INFO",1,true));
455         //The name of the backup
456         fwrite ($bf,full_tag("NAME",2,false,$preferences->backup_name));
457         //The moodle_version
458         fwrite ($bf,full_tag("MOODLE_VERSION",2,false,$preferences->moodle_version));
459         fwrite ($bf,full_tag("MOODLE_RELEASE",2,false,$preferences->moodle_release));
460         //The backup_version
461         fwrite ($bf,full_tag("BACKUP_VERSION",2,false,$preferences->backup_version));
462         fwrite ($bf,full_tag("BACKUP_RELEASE",2,false,$preferences->backup_release));
463         //The date
464         fwrite ($bf,full_tag("DATE",2,false,$preferences->backup_unique_code));
465         //The original site wwwroot
466         fwrite ($bf,full_tag("ORIGINAL_WWWROOT",2,false,$CFG->wwwroot));
467         //Te includes tag
468         fwrite ($bf,start_tag("DETAILS",2,true));
469         //Now, go to mod element of preferences to print its status
470         foreach ($preferences->mods as $element) {
471             //Calculate info
472             $included = "false";
473             $userinfo = "false";
474             if ($element->backup) {
475                 $included = "true";
476                 if ($element->userinfo) {
477                     $userinfo = "true";
478                 }
479             }
480             //Prints the mod start
481             fwrite ($bf,start_tag("MOD",3,true));
482             fwrite ($bf,full_tag("NAME",4,false,$element->name));
483             fwrite ($bf,full_tag("INCLUDED",4,false,$included));
484             fwrite ($bf,full_tag("USERINFO",4,false,$userinfo));
485                  
486             //Print the end
487             fwrite ($bf,end_tag("MOD",3,true));
488         }
489         //The user in backup
490         if ($preferences->backup_users == 1) {
491             fwrite ($bf,full_tag("USERS",3,false,"course"));
492         } else if ($preferences->backup_users == 0) {
493             fwrite ($bf,full_tag("USERS",3,false,"all"));
494         } else {
495             fwrite ($bf,full_tag("USERS",3,false,"none"));
496         }
497         //The logs in backup
498         if ($preferences->backup_logs == 1) {
499             fwrite ($bf,full_tag("LOGS",3,false,"true"));
500         } else {
501             fwrite ($bf,full_tag("LOGS",3,false,"false"));
502         }
503         //The user files
504         if ($preferences->backup_user_files == 1) {
505             fwrite ($bf,full_tag("USERFILES",3,false,"true"));
506         } else {
507             fwrite ($bf,full_tag("USERFILES",3,false,"false"));
508         }
509         //The course files
510         if ($preferences->backup_course_files == 1) {
511             fwrite ($bf,full_tag("COURSEFILES",3,false,"true"));
512         } else {
513             fwrite ($bf,full_tag("COURSEFILES",3,false,"false"));
514         }
516         fwrite ($bf,end_tag("DETAILS",2,true));
519         $status = fwrite ($bf,end_tag("INFO",1,true)); 
521         return $status;
522     }
523     
524     //Prints course's general info (table course)
525     function backup_course_start ($bf,$preferences) {
527         global $CFG;
529         $status = true;
530         
531         //Course open tag
532         fwrite ($bf,start_tag("COURSE",1,true));
533         //Header open tag
534         fwrite ($bf,start_tag("HEADER",2,true));
536         //Get info from course
537         $course=false;
538         if ($courses = get_records("course","id",$preferences->backup_course)) {
539             $course = $courses[$preferences->backup_course];
540         }
541         if ($course) {
542             //Prints course info
543             fwrite ($bf,full_tag("ID",3,false,$course->id));
544             //Obtain the category
545             $category = false;
546             if ($categories = get_records("course_categories","id","$course->category")) {
547                 $category = $categories[$course->category];
548             }
549             if ($category) {
550                 //Prints category info
551                 fwrite ($bf,start_tag("CATEGORY",3,true));
552                 fwrite ($bf,full_tag("ID",4,false,$course->category));
553                 fwrite ($bf,full_tag("NAME",4,false,$category->name));
554                 fwrite ($bf,end_tag("CATEGORY",3,true));
555             }
556             //Continues with the course
557             fwrite ($bf,full_tag("PASSWORD",3,false,$course->password));
558             fwrite ($bf,full_tag("FULLNAME",3,false,$course->fullname));
559             fwrite ($bf,full_tag("SHORTNAME",3,false,$course->shortname));
560             fwrite ($bf,full_tag("SUMMARY",3,false,$course->summary));
561             fwrite ($bf,full_tag("FORMAT",3,false,$course->format));
562             fwrite ($bf,full_tag("SHOWGRADES",3,false,$course->showgrades));
563             fwrite ($bf,full_tag("BLOCKINFO",3,false,blocks_get_block_names($course->blockinfo)));
564             fwrite ($bf,full_tag("NEWSITEMS",3,false,$course->newsitems));
565             fwrite ($bf,full_tag("TEACHER",3,false,$course->teacher));
566             fwrite ($bf,full_tag("TEACHERS",3,false,$course->teachers));
567             fwrite ($bf,full_tag("STUDENT",3,false,$course->student));
568             fwrite ($bf,full_tag("STUDENTS",3,false,$course->students));
569             fwrite ($bf,full_tag("GUEST",3,false,$course->guest));
570             fwrite ($bf,full_tag("STARTDATE",3,false,$course->startdate));
571             fwrite ($bf,full_tag("NUMSECTIONS",3,false,$course->numsections));
572             //fwrite ($bf,full_tag("SHOWRECENT",3,false,$course->showrecent));    INFO: This is out in 1.3
573             fwrite ($bf,full_tag("MAXBYTES",3,false,$course->maxbytes));
574             fwrite ($bf,full_tag("SHOWREPORTS",3,false,$course->showreports));
575             fwrite ($bf,full_tag("GROUPMODE",3,false,$course->groupmode));
576             fwrite ($bf,full_tag("GROUPMODEFORCE",3,false,$course->groupmodeforce));
577             fwrite ($bf,full_tag("LANG",3,false,$course->lang));
578             fwrite ($bf,full_tag("MARKER",3,false,$course->marker));
579             fwrite ($bf,full_tag("VISIBLE",3,false,$course->visible));
580             fwrite ($bf,full_tag("HIDDENSECTIONS",3,false,$course->hiddensections));
581             fwrite ($bf,full_tag("TIMECREATED",3,false,$course->timecreated));
582             $status = fwrite ($bf,full_tag("TIMEMODIFIED",3,false,$course->timemodified));
583             //Print header end
584             fwrite ($bf,end_tag("HEADER",2,true));
585         } else { 
586            $status = false;
587         } 
589        return $status;
590     }
592     //Prints course's end tag
593     function backup_course_end ($bf,$preferences) {
595         //Course end tag
596         $status = fwrite ($bf,end_tag("COURSE",1,true)); 
597     
598         return $status;
600     }
602     //Prints course's sections info (table course_sections)
603     function backup_course_sections ($bf,$preferences) {
605         global $CFG;
607         $status = true;
610         //Get info from sections
611         $section=false;
612         if ($sections = get_records("course_sections","course",$preferences->backup_course,"section")) {
613             //Section open tag
614             fwrite ($bf,start_tag("SECTIONS",2,true));
615             //Iterate over every section (ordered by section)     
616             foreach ($sections as $section) {
617                 //Begin Section
618                 fwrite ($bf,start_tag("SECTION",3,true));
619                 fwrite ($bf,full_tag("ID",4,false,$section->id));
620                 fwrite ($bf,full_tag("NUMBER",4,false,$section->section));
621                 fwrite ($bf,full_tag("SUMMARY",4,false,$section->summary));
622                 fwrite ($bf,full_tag("VISIBLE",4,false,$section->visible));
623                 //Now print the mods in section 
624                 backup_course_modules ($bf,$preferences,$section);
625                 //End section
626                 fwrite ($bf,end_tag("SECTION",3,true));
627             }
628             //Section close tag
629             $status = fwrite ($bf,end_tag("SECTIONS",2,true));
630         }
632         return $status;
634     }
636     //Prints course's modules info (table course_modules)
637     //Only for selected mods in preferences
638     function backup_course_modules ($bf,$preferences,$section) {
640         global $CFG;
642         $status = true;
644         $first_record = true;
646         //Now print the mods in section
647         //Extracts mod id from sequence
648         $tok = strtok($section->sequence,",");
649         while ($tok) {
650            //Get module's type
651            $moduletype = get_module_type ($preferences->backup_course,$tok);
652            //Check if we've selected to backup that type
653            if ($moduletype and $preferences->mods[$moduletype]->backup) {
654                $selected = true;
655            } else {
656                $selected = false;
657            }
659            if ($selected) {
660                //Gets course_module data from db
661                $course_module = get_records ("course_modules","id",$tok);
662                //If it's the first, pring MODS tag
663                if ($first_record) {
664                    fwrite ($bf,start_tag("MODS",4,true));
665                    $first_record = false;
666                }
667                //Print mod info from course_modules
668                fwrite ($bf,start_tag("MOD",5,true));
669                //Save neccesary info to backup_ids
670                fwrite ($bf,full_tag("ID",6,false,$tok));
671                fwrite ($bf,full_tag("TYPE",6,false,$moduletype));
672                fwrite ($bf,full_tag("INSTANCE",6,false,$course_module[$tok]->instance));
673                fwrite ($bf,full_tag("ADDED",6,false,$course_module[$tok]->added));
674                fwrite ($bf,full_tag("DELETED",6,false,$course_module[$tok]->deleted));
675                fwrite ($bf,full_tag("SCORE",6,false,$course_module[$tok]->score));
676                fwrite ($bf,full_tag("INDENT",6,false,$course_module[$tok]->indent));
677                fwrite ($bf,full_tag("VISIBLE",6,false,$course_module[$tok]->visible));
678                fwrite ($bf,full_tag("GROUPMODE",6,false,$course_module[$tok]->groupmode));
679                fwrite ($bf,end_tag("MOD",5,true));
680             }
681            //check for next
682            $tok = strtok(",");
683         }
685         //Si ha habido modulos, final de MODS
686         if (!$first_record) {
687             $status =fwrite ($bf,end_tag("MODS",4,true));
688         }
690         return $status;
691     }
693     //Print users to xml
694     //Only users previously calculated in backup_ids will output
695     //
696     function backup_user_info ($bf,$preferences) {
697     
698         global $CFG;
700         $status = true;
702         $users = get_records_sql("SELECT u.old_id, u.table_name,u.info
703                               FROM {$CFG->prefix}backup_ids u
704                               WHERE u.backup_code = '$preferences->backup_unique_code' AND
705                                     u.table_name = 'user'");
707         //If we have users to backup
708         if ($users) {
709             //Begin Users tag
710             fwrite ($bf,start_tag("USERS",2,true));
711             $counter = 0;
712             //With every user
713             foreach ($users as $user) {
714                 //Get user data from table
715                 $user_data = get_record("user","id",$user->old_id);
716                 //Begin User tag
717                 fwrite ($bf,start_tag("USER",3,true));
718                 //Output all user data
719                 fwrite ($bf,full_tag("ID",4,false,$user_data->id));
720                 fwrite ($bf,full_tag("CONFIRMED",4,false,$user_data->confirmed));
721                 fwrite ($bf,full_tag("DELETED",4,false,$user_data->deleted));
722                 fwrite ($bf,full_tag("USERNAME",4,false,$user_data->username));
723                 fwrite ($bf,full_tag("PASSWORD",4,false,$user_data->password));
724                 fwrite ($bf,full_tag("IDNUMBER",4,false,$user_data->idnumber));
725                 fwrite ($bf,full_tag("FIRSTNAME",4,false,$user_data->firstname));
726                 fwrite ($bf,full_tag("LASTNAME",4,false,$user_data->lastname));
727                 fwrite ($bf,full_tag("EMAIL",4,false,$user_data->email));
728                 fwrite ($bf,full_tag("EMAILSTOP",4,false,$user_data->emailstop));
729                 fwrite ($bf,full_tag("ICQ",4,false,$user_data->icq));
730                 fwrite ($bf,full_tag("PHONE1",4,false,$user_data->phone1));
731                 fwrite ($bf,full_tag("PHONE2",4,false,$user_data->phone2));
732                 fwrite ($bf,full_tag("INSTITUTION",4,false,$user_data->institution));
733                 fwrite ($bf,full_tag("DEPARTMENT",4,false,$user_data->department));
734                 fwrite ($bf,full_tag("ADDRESS",4,false,$user_data->address));
735                 fwrite ($bf,full_tag("CITY",4,false,$user_data->city));
736                 fwrite ($bf,full_tag("COUNTRY",4,false,$user_data->country));
737                 fwrite ($bf,full_tag("LANG",4,false,$user_data->lang));
738                 fwrite ($bf,full_tag("TIMEZONE",4,false,$user_data->timezone));
739                 fwrite ($bf,full_tag("FIRSTACCESS",4,false,$user_data->firstaccess));
740                 fwrite ($bf,full_tag("LASTACCESS",4,false,$user_data->lastaccess));
741                 fwrite ($bf,full_tag("LASTLOGIN",4,false,$user_data->lastlogin));
742                 fwrite ($bf,full_tag("CURRENTLOGIN",4,false,$user_data->currentlogin));
743                 fwrite ($bf,full_tag("LASTIP",4,false,$user_data->lastIP));
744                 fwrite ($bf,full_tag("SECRET",4,false,$user_data->secret));
745                 fwrite ($bf,full_tag("PICTURE",4,false,$user_data->picture));
746                 fwrite ($bf,full_tag("URL",4,false,$user_data->url));
747                 fwrite ($bf,full_tag("DESCRIPTION",4,false,$user_data->description));
748                 fwrite ($bf,full_tag("MAILFORMAT",4,false,$user_data->mailformat));
749                 fwrite ($bf,full_tag("MAILDISPLAY",4,false,$user_data->maildisplay));
750                 fwrite ($bf,full_tag("HTMLEDITOR",4,false,$user_data->htmleditor));
751                 fwrite ($bf,full_tag("AUTOSUBSCRIBE",4,false,$user_data->autosubscribe));
752                 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$user_data->timemodified));
754                 //Output every user role (with its associated info) 
755                 $user->isadmin = strpos($user->info,"admin");
756                 $user->iscoursecreator = strpos($user->info,"coursecreator");
757                 $user->isteacher = strpos($user->info,"teacher");
758                 $user->isstudent = strpos($user->info,"student");
759                 $user->isneeded = strpos($user->info,"needed");
760                 if ($user->isadmin!==false or 
761                     $user->iscoursecreator!==false or 
762                     $user->isteacher!==false or 
763                     $user->isstudent!==false or
764                     $user->isneeded!==false) {
765                     //Begin ROLES tag
766                     fwrite ($bf,start_tag("ROLES",4,true));
767                     //PRINT ROLE INFO
768                     //Admins
769                     if ($user->isadmin!==false) {
770                         //Print ROLE start
771                         fwrite ($bf,start_tag("ROLE",5,true));
772                         //Print Role info
773                         fwrite ($bf,full_tag("TYPE",6,false,"admin"));
774                         //Print ROLE end
775                         fwrite ($bf,end_tag("ROLE",5,true));
776                     }
777                     //CourseCreator
778                     if ($user->iscoursecreator!==false) {
779                         //Print ROLE start 
780                         fwrite ($bf,start_tag("ROLE",5,true)); 
781                         //Print Role info 
782                         fwrite ($bf,full_tag("TYPE",6,false,"coursecreator"));
783                         //Print ROLE end
784                         fwrite ($bf,end_tag("ROLE",5,true));   
785                     }
786                     //Teacher
787                     if ($user->isteacher!==false) {
788                         //Print ROLE start 
789                         fwrite ($bf,start_tag("ROLE",5,true)); 
790                         //Print Role info 
791                         fwrite ($bf,full_tag("TYPE",6,false,"teacher"));
792                         //Get specific info for teachers
793                         $tea = get_record("user_teachers","userid",$user->old_id,"course",$preferences->backup_course);
794                         fwrite ($bf,full_tag("AUTHORITY",6,false,$tea->authority));
795                         fwrite ($bf,full_tag("TEA_ROLE",6,false,$tea->role));
796                         fwrite ($bf,full_tag("EDITALL",6,false,$tea->editall));
797                         fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$tea->timemodified));
798                         fwrite ($bf,full_tag("TIMEACCESS",6,false,$tea->timeaccess));
799                         //Print ROLE end
800                         fwrite ($bf,end_tag("ROLE",5,true));   
801                     }
802                     //Student
803                     if ($user->isstudent!==false) {
804                         //Print ROLE start 
805                         fwrite ($bf,start_tag("ROLE",5,true)); 
806                         //Print Role info 
807                         fwrite ($bf,full_tag("TYPE",6,false,"student"));
808                         //Get specific info for students
809                         $stu = get_record("user_students","userid",$user->old_id,"course",$preferences->backup_course);
810                         fwrite ($bf,full_tag("TIMESTART",6,false,$stu->timestart));
811                         fwrite ($bf,full_tag("TIMEEND",6,false,$stu->timeend));
812                         fwrite ($bf,full_tag("TIME",6,false,$stu->time));
813                         fwrite ($bf,full_tag("TIMEACCESS",6,false,$stu->timeaccess));
814                         //Print ROLE end
815                         fwrite ($bf,end_tag("ROLE",5,true));   
816                     }
817                     //Needed
818                     if ($user->isneeded!==false) {
819                         //Print ROLE start
820                         fwrite ($bf,start_tag("ROLE",5,true));
821                         //Print Role info
822                         fwrite ($bf,full_tag("TYPE",6,false,"needed"));
823                         //Print ROLE end
824                         fwrite ($bf,end_tag("ROLE",5,true));
825                     }
827                     //End ROLES tag
828                     fwrite ($bf,end_tag("ROLES",4,true));
829  
830                     //Check if we have user_preferences to backup
831                     if ($preferences_data = get_records("user_preferences","userid",$user->old_id)) {
832                         //Start USER_PREFERENCES tag
833                         fwrite ($bf,start_tag("USER_PREFERENCES",4,true));
834                         //Write each user_preference
835                         foreach ($preferences_data as $user_preference) {
836                             fwrite ($bf,start_tag("USER_PREFERENCE",5,true));
837                             fwrite ($bf,full_tag("NAME",6,false,$user_preference->name));
838                             fwrite ($bf,full_tag("VALUE",6,false,$user_preference->value));
839                             fwrite ($bf,end_tag("USER_PREFERENCE",5,true));
840                         }
841                         //End USER_PREFERENCES tag
842                         fwrite ($bf,end_tag("USER_PREFERENCES",4,true));
843                     }
844                     
845                 }
846                 //End User tag
847                 fwrite ($bf,end_tag("USER",3,true));
848                 //Do some output
849                 $counter++;
850                 if ($counter % 10 == 0) {
851                     echo ".";   
852                     if ($counter % 200 == 0) {
853                         echo "<br>";
854                     }
855                     backup_flush(300);
856                 }
857             }
858             //End Users tag
859             fwrite ($bf,end_tag("USERS",2,true));
860         } else {
861             // There aren't any users.
862             $status = true;
863         }
865         return $status;
866     }
868     //Backup log info (time ordered)
869     function backup_log_info($bf,$preferences) {
871         global $CFG;
873         //Number of records to get in every chunk
874         $recordset_size = 1000;
875   
876         $status = true;
877  
878         //Counter, points to current record
879         $counter = 0;
881         //Count records
882         $count_logs = count_records("log","course",$preferences->backup_course);
884         //Pring logs header
885         if ($count_logs > 0 ) {
886             fwrite ($bf,start_tag("LOGS",2,true));
887         }
888         while ($counter < $count_logs) {
889             //Get a chunk of records
890             $logs = get_records ("log","course",$preferences->backup_course,"time","*",$counter,$recordset_size);
892             //We have logs
893             if ($logs) {
894                 //Iterate 
895                 foreach ($logs as $log) {
896                     //See if it is a valid module to backup
897                     if ($log->module == "course" or 
898                         $log->module == "user" or
899                         $preferences->mods[$log->module]->backup == 1) {
900                         //Begin log tag
901                          fwrite ($bf,start_tag("LOG",3,true));
902     
903                         //Output log tag
904                         fwrite ($bf,full_tag("ID",4,false,$log->id));
905                         fwrite ($bf,full_tag("TIME",4,false,$log->time));
906                         fwrite ($bf,full_tag("USERID",4,false,$log->userid));
907                         fwrite ($bf,full_tag("IP",4,false,$log->ip));
908                         fwrite ($bf,full_tag("MODULE",4,false,$log->module));
909                         fwrite ($bf,full_tag("CMID",4,false,$log->cmid));
910                         fwrite ($bf,full_tag("ACTION",4,false,$log->action));
911                         fwrite ($bf,full_tag("URL",4,false,$log->url));
912                         fwrite ($bf,full_tag("INFO",4,false,$log->info));
913     
914                         //End log tag
915                          fwrite ($bf,end_tag("LOG",3,true));
916                     }
917                     //Do some output
918                     $counter++;
919                     if ($counter % 10 == 0) {
920                         echo ".";
921                         if ($counter % 200 == 0) {
922                             echo "<br>";
923                         }
924                         backup_flush(300);
925                     }
926                 }
927             }
928         }
929         //End logs tag
930         if ($count_logs > 0 ) {
931             $status = fwrite ($bf,end_tag("LOGS",2,true));
932         }
933         return $status;
934     }
937     //Backup scales info (common and course scales)
938     function backup_scales_info($bf,$preferences) {
940         global $CFG;
942         $status = true;
944         //Counter, points to current record
945         $counter = 0;
947         //Get scales (common and course scales)
948         $scales = get_records_sql("SELECT id, courseid, userid, name, scale, description, timemodified
949                                    FROM {$CFG->prefix}scale
950                                    WHERE courseid = '0' or courseid = $preferences->backup_course");
952         //Copy only used scales to $backupscales. They will be in backup (unused no). See Bug 1223.
953         $backupscales = array();
954         if ($scales) {
955             foreach ($scales as $scale) {
956                 if (course_scale_used($preferences->backup_course, $scale->id)) {
957                     $backupscales[] = $scale;
958                 }
959             }
960         }
962         //Pring scales header
963         if ($backupscales) {
964             //Pring scales header
965             fwrite ($bf,start_tag("SCALES",2,true));
966             //Iterate
967             foreach ($backupscales as $scale) {
968                 //Begin scale tag
969                 fwrite ($bf,start_tag("SCALE",3,true));
970                 //Output scale tag
971                 fwrite ($bf,full_tag("ID",4,false,$scale->id));
972                 fwrite ($bf,full_tag("COURSEID",4,false,$scale->courseid));
973                 fwrite ($bf,full_tag("USERID",4,false,$scale->userid));
974                 fwrite ($bf,full_tag("NAME",4,false,$scale->name));
975                 fwrite ($bf,full_tag("SCALETEXT",4,false,$scale->scale));
976                 fwrite ($bf,full_tag("DESCRIPTION",4,false,$scale->description));
977                 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$scale->timemodified));
978                 //End scale tag
979                 fwrite ($bf,end_tag("SCALE",3,true));
980             }
981             //End scales tag
982             $status = fwrite ($bf,end_tag("SCALES",2,true));
983         }
984         return $status;
985     }
987     //Backup events info (course events)
988     function backup_events_info($bf,$preferences) {
990         global $CFG;
992         $status = true;
994         //Counter, points to current record
995         $counter = 0;
997         //Get events (course events)
998         $events = get_records_select("event","courseid='$preferences->backup_course' AND instance='0'","id");
1000         //Pring events header
1001         if ($events) {
1002             //Pring events header
1003             fwrite ($bf,start_tag("EVENTS",2,true));
1004             //Iterate
1005             foreach ($events as $event) {
1006                 //Begin event tag
1007                 fwrite ($bf,start_tag("EVENT",3,true));
1008                 //Output event tag
1009                 fwrite ($bf,full_tag("ID",4,false,$event->id));
1010                 fwrite ($bf,full_tag("NAME",4,false,$event->name));
1011                 fwrite ($bf,full_tag("DESCRIPTION",4,false,$event->description));
1012                 fwrite ($bf,full_tag("FORMAT",4,false,$event->format));
1013                 fwrite ($bf,full_tag("GROUPID",4,false,$event->groupid));
1014                 fwrite ($bf,full_tag("USERID",4,false,$event->userid));
1015                 fwrite ($bf,full_tag("EVENTTYPE",4,false,$event->eventtype));
1016                 fwrite ($bf,full_tag("TIMESTART",4,false,$event->timestart));
1017                 fwrite ($bf,full_tag("TIMEDURATION",4,false,$event->timeduration));
1018                 fwrite ($bf,full_tag("VISIBLE",4,false,$event->visible));
1019                 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$event->timemodified));
1020                 //End event tag
1021                 fwrite ($bf,end_tag("EVENT",3,true));
1022             }
1023             //End events tag
1024             $status = fwrite ($bf,end_tag("EVENTS",2,true));
1025         }
1026         return $status;
1027     }
1029     //Backup groups info
1030     function backup_groups_info($bf,$preferences) {
1031     
1032         global $CFG;
1034         $status = true;
1035         $status2 = true;
1037         //Get groups 
1038         $groups = get_records("groups","courseid",$preferences->backup_course);
1040         //Pring groups header
1041         if ($groups) {
1042             //Pring groups header
1043             fwrite ($bf,start_tag("GROUPS",2,true));
1044             //Iterate
1045             foreach ($groups as $group) {
1046                 //Begin group tag
1047                 fwrite ($bf,start_tag("GROUP",3,true));
1048                 //Output group contents
1049                 fwrite ($bf,full_tag("ID",4,false,$group->id));
1050                 fwrite ($bf,full_tag("COURSEID",4,false,$group->courseid));
1051                 fwrite ($bf,full_tag("NAME",4,false,$group->name));
1052                 fwrite ($bf,full_tag("DESCRIPTION",4,false,$group->description));
1053                 fwrite ($bf,full_tag("LANG",4,false,$group->lang));
1054                 fwrite ($bf,full_tag("PICTURE",4,false,$group->picture));
1055                 fwrite ($bf,full_tag("HIDEPICTURE",4,false,$group->hidepicture));
1056                 fwrite ($bf,full_tag("TIMECREATED",4,false,$group->timecreated));
1057                 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$group->timemodified));
1058                 
1059                 //Now, backup groups_members
1060                 $status2 = backup_groups_members_info($bf,$preferences,$group->id);
1062                 //End group tag
1063                 fwrite ($bf,end_tag("GROUP",3,true));
1064             }
1065             //End groups tag
1066             $status = fwrite ($bf,end_tag("GROUPS",2,true));
1067         }
1068         return ($status && $status2);
1069     }
1071     //Backup groups_members info
1072     function backup_groups_members_info($bf,$preferences,$groupid) {
1073   
1074         global $CFG;
1075         
1076         $status = true;
1078         //Get groups_members
1079         $groups_members = get_records("groups_members","groupid",$groupid);
1080         
1081         //Pring groups_members header
1082         if ($groups_members) {
1083             //Pring groups_members header
1084             fwrite ($bf,start_tag("MEMBERS",4,true));
1085             //Iterate
1086             foreach ($groups_members as $group_member) {
1087                 //Begin group_member tag
1088                 fwrite ($bf,start_tag("MEMBER",5,true));
1089                 //Output group_member contents
1090                 fwrite ($bf,full_tag("USERID",6,false,$group_member->userid));
1091                 fwrite ($bf,full_tag("TIMEADDED",6,false,$group_member->timeadded));
1092                 //End group_member tag
1093                 fwrite ($bf,end_tag("MEMBER",5,true));
1094             }
1095             //End groups_members tag
1096             $status = fwrite ($bf,end_tag("MEMBERS",4,true));
1097         }
1098         return $status;
1099     }
1101     //Start the modules tag
1102     function backup_modules_start ($bf,$preferences) {
1103       
1104         return fwrite ($bf,start_tag("MODULES",2,true));
1105     }
1107     //End the modules tag
1108     function backup_modules_end ($bf,$preferences) {
1110         return fwrite ($bf,end_tag("MODULES",2,true));
1111     }
1113     //This function makes all the necesary calls to every mod
1114     //to export itself and its files !!!
1115     function backup_module($bf,$preferences,$module) {
1116          
1117         global $CFG;
1119         $status = true;
1121         //First, re-check if necessary functions exists
1122         $modbackup = $module."_backup_mods";
1123         if (function_exists($modbackup)) {
1124             //Call the function
1125             $status = $modbackup($bf,$preferences);
1126         } else {
1127             //Something was wrong. Function should exist.
1128             $status = false;
1129         }
1130    
1131         return $status; 
1132         
1133     }
1135     //This function encode things to make backup multi-site fully functional
1136     //It does this conversions:
1137     //    - $CFG->wwwroot/file.php/courseid ----------------------> $@FILEPHP@$
1138     //    - Links to forums everywhere (DB) are encoded.
1139     //
1140     function backup_encode_absolute_links($content) {
1142         global $CFG,$preferences;
1144         //Check if preferences is ok. If it isn't set, we are 
1145         //in a scheduled_backup to we are able to get a copy
1146         //from CFG->backup_preferences
1147         if (!isset($preferences)) {
1148             $mypreferences = $CFG->backup_preferences;
1149         } else {
1150             //We are in manual backups so global preferences must exist!!
1151             $mypreferences = $preferences;
1152         }
1154         //First, we check for every call to file.php inside the course
1155         $search = array($CFG->wwwroot."/file.php/".$mypreferences->backup_course);
1157         $replace = array("$@FILEPHP@$");
1159         $result = str_replace($search,$replace,$content);
1161         foreach ($mypreferences->mods as $name => $info) {
1162             //Check if the xxxx_encode_content_links exists
1163             $function_name = $name."_encode_content_links";
1164             if (function_exists($function_name)) {
1165                 $result = $function_name($result,$mypreferences);
1166             }
1167         }
1169         if ($result != $content && $CFG->debug>7) {                                  //Debug
1170             echo "<br><hr>".$content."<br>changed to<br>".$result."<hr><br>";        //Debug
1171         }                                                                            //Debug
1173         return $result;
1174     }
1176     //This function copies all the needed files under the "users" directory to the "user_files"
1177     //directory under temp/backup
1178     function backup_copy_user_files ($preferences) {
1180         global $CFG;
1182         $status = true;
1184         //First we check to "user_files" exists and create it as necessary
1185         //in temp/backup/$backup_code  dir
1186         $status = check_and_create_user_files_dir($preferences->backup_unique_code);
1187  
1188         //Now iterate over directories under "users" to check if that user must be 
1189         //copied to backup
1190         
1191         $rootdir = $CFG->dataroot."/users";
1192         //Check if directory exists
1193         if (is_dir($rootdir)) {
1194             $list = list_directories ($rootdir);
1195             if ($list) {
1196                 //Iterate
1197                 foreach ($list as $dir) {
1198                     //Look for dir like username in backup_ids
1199                     $data = get_record ("backup_ids","backup_code",$preferences->backup_unique_code,
1200                                                      "table_name","user", 
1201                                                      "old_id",$dir);
1202                     //If exists, copy it
1203                     if ($data) {
1204                         $status = backup_copy_file($rootdir."/".$dir,
1205                                        $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/user_files/".$dir);
1206                     }
1207                 }
1208             }
1209         }
1210         return $status;
1211     }
1213     //This function copies all the course files under the course directory (except the moddata
1214     //directory to the "course_files" directory under temp/backup
1215     function backup_copy_course_files ($preferences) {
1217         global $CFG;
1219         $status = true;
1221         //First we check to "course_files" exists and create it as necessary
1222         //in temp/backup/$backup_code  dir
1223         $status = check_and_create_course_files_dir($preferences->backup_unique_code);
1225         //Now iterate over files and directories except $CFG->moddata and backupdata to be
1226         //copied to backup
1228         $rootdir = $CFG->dataroot."/".$preferences->backup_course;
1230         $name_moddata = $CFG->moddata;
1231         $name_backupdata = "backupdata";
1232         //Check if directory exists
1233         if (is_dir($rootdir)) {
1234             $list = list_directories_and_files ($rootdir);
1235             if ($list) {
1236                 //Iterate
1237                 foreach ($list as $dir) {
1238                     if ($dir !== $name_moddata and $dir !== $name_backupdata) {
1239                         $status = backup_copy_file($rootdir."/".$dir,
1240                                        $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/course_files/".$dir);
1241                     }
1242                 }
1243             }
1244         }
1245         return $status;
1246     }
1248     //This function creates the zip file containing all the backup info
1249     //moodle.xml, moddata, user_files, course_files.
1250     //The zipped file is created in the backup directory and named with
1251     //the "oficial" name of the backup
1252     //It uses "pclzip" if available or system "zip" (unix only)
1253     function backup_zip ($preferences) {
1254     
1255         global $CFG;
1257         $status = true;
1259         //Base dir where everything happens
1260         $basedir = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code;
1261         //Backup zip file name
1262         $name = $preferences->backup_name;
1263         //List base_dir files and directories
1264         $filelist = list_directories_and_files ($basedir);
1266         if (empty($CFG->zip)) {    // Use built-in php-based zip function
1267             //echo "<br>Using pclzip";                                    //Debug
1268             $files = array();
1269             foreach ($filelist as $file) {
1270                 //If directory, append "/"
1271                 //Commented. Not needed wit version 2.0 of pclzip !!
1272                 //if (is_dir($basedir."/".$file)) {
1273                 //    $file = $file."/";
1274                 //}
1275                 //Include into array
1276                 //echo "<br>Adding file/dir ".$file;                       //Debug
1277                 $files[] = $basedir."/".$file;
1278             }
1279             include_once("$CFG->dirroot/lib/pclzip/pclzip.lib.php");
1280             //include_once("$CFG->dirroot/lib/pclzip/pclerror.lib.php");   //Debug
1281             //include_once("$CFG->dirroot/lib/pclzip/pcltrace.lib.php");   //Debug
1282             //PclTraceOn(2);                                               //Debug
1283             $archive = new PclZip("$basedir/$name");
1284             if (($list = $archive->create($files,PCLZIP_OPT_REMOVE_PATH,$basedir)) == 0) {
1285                 error($archive->errorInfo(true));
1286                 $status = false;
1287             } 
1288             //PclTraceDisplay();                                           //Debug
1289             //PclTraceOff();                                               //Debug
1290         } else {                   // Use external zip program
1291             //echo "<br>Using external zip";                               //Debug
1292             $files = "";
1293             foreach ($filelist as $file) {
1294                 $files .= basename($file);
1295                 $files .= " ";
1296             }
1297             $command = "cd $basedir ; $CFG->zip -r $name $files";
1298             //echo "<br>Executing command: ".$command;                     //Debug
1299             $status = Exec($command);
1300         }
1302         //echo "<br>Status: ".$status;                                     //Debug
1303         return $status;
1305     } 
1307     //This function copies the final zip to the course dir
1308     function copy_zip_to_course_dir ($preferences) {
1309     
1310         global $CFG;
1312         $status = true;
1314         //Define zip location (from)
1315         $from_zip_file = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/".$preferences->backup_name;
1317         //Initialise $to_zip_file
1318         $to_zip_file="";
1320         //If $preferences->backup_destination isn't empty, then copy to custom directory
1321         if (!empty($preferences->backup_destination)) {
1322             $to_zip_file = $preferences->backup_destination."/".$preferences->backup_name;
1323         } else {
1324             //Define zip destination (course dir)
1325             $to_zip_file = $CFG->dataroot."/".$preferences->backup_course;
1326     
1327             //echo "<p>From: ".$from_zip_file."<br>";                                              //Debug
1328     
1329             //echo "<p>Checking: ".$to_zip_file."<br>";                                          //Debug
1330     
1331             //Checks course dir exists
1332             $status = check_dir_exists($to_zip_file,true);
1333     
1334             //Define zip destination (backup dir)
1335             $to_zip_file = $to_zip_file."/backupdata";
1336     
1337             //echo "<p>Checking: ".$to_zip_file."<br>";                                          //Debug
1338     
1339             //Checks backup dir exists
1340             $status = check_dir_exists($to_zip_file,true);
1342             //Define zip destination (zip file)
1343             $to_zip_file = $to_zip_file."/".$preferences->backup_name;
1344         }
1346         //echo "<p>To: ".$to_zip_file."<br>";                                              //Debug
1348         //Copy zip file
1349         if ($status) {
1350             $status = backup_copy_file ($from_zip_file,$to_zip_file);
1351         }
1353         return $status;
1354     }
1355 ?>