fix for bug 324. we are now checking to see if there is a conflict caused by
[moodle.git] / backup / lib.php
CommitLineData
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;
39
40 if ($backup_users == 0) {
41 //Insert all users (from user)
42 $sql_insert = "INSERT INTO {$CFG->prefix}backup_ids
43 (backup_code, table_name, old_id)
44 SELECT DISTINCT '$backup_unique_code','user',u.id
45 FROM {$CFG->prefix}user u";
46 } else {
47 //Insert only course users (from user_students and user_teachers)
48 $sql_insert = "INSERT INTO {$CFG->prefix}backup_ids
49 (backup_code, table_name, old_id)
50 SELECT DISTINCT '$backup_unique_code','user',u.id
51 FROM {$CFG->prefix}user u,
52 {$CFG->prefix}user_students s,
53 {$CFG->prefix}user_teachers t
54 WHERE s.course = '$course' AND
55 t.course = s.course AND
56 (s.userid = u.id OR t.userid = u.id)";
57 }
58 //Execute the insert
59 $status = execute_sql($sql_insert,false);
60
61 //Now execute the select
62 $ids = get_records_sql("SELECT DISTINCT u.old_id,u.table_name
63 FROM {$CFG->prefix}backup_ids u
64 WHERE backup_code = '$backup_unique_code' AND
65 table_name ='user'");
66
67 //Gets the user data
68 $info[0][0] = get_string("users");
69 if ($ids) {
70 $info[0][1] = count($ids);
71 } else {
72 $info[0][1] = 0;
73 }
74
75 return $info;
76 }
afbe3de8 77
3868b9c6 78 //Calculate the number of log entries to backup
79 //Return an array of info (name,value)
80 function log_check_backup($course) {
81
82 global $CFG;
83
84 //Execute the insert
85 $status = execute_sql($sql_insert,false);
86
87 //Now execute the select
88 $ids = get_records_sql("SELECT DISTINCT l.id,l.course
89 FROM {$CFG->prefix}log l
90 WHERE l.course = '$course'");
91 //Gets the user data
92 $info[0][0] = get_string("logs");
93 if ($ids) {
94 $info[0][1] = count($ids);
95 } else {
96 $info[0][1] = 0;
97 }
98
99 return $info;
100 }
101
102 //Calculate the number of user files to backup
103 //Under $CFG->dataroot/users
104 //and put them (their path) in backup_ids
105 //Return an array of info (name,value)
106 function user_files_check_backup($course,$backup_unique_code) {
107
108 global $CFG;
109
110 $rootdir = $CFG->dataroot."/users";
111 $coursedirs = get_directory_list($rootdir);
112 foreach ($coursedirs as $dir) {
113 //Extracts user id from file path
114 $tok = strtok($dir,"/");
115 if ($tok) {
116 $userid = $tok;
117 } else {
118 $tok = "";
119 }
120 //Insert them into backup_files
121 $status = execute_sql("INSERT INTO {$CFG->prefix}backup_files
122 (backup_code, file_type, path, old_id)
123 VALUES
124 ('$backup_unique_code','user','$dir','$userid')",false);
125 }
126
127 //Now execute the select
128 $ids = get_records_sql("SELECT DISTINCT b.path, b.old_id
129 FROM {$CFG->prefix}backup_files b
130 WHERE backup_code = '$backup_unique_code' AND
131 file_type = 'user'");
132 //Gets the user data
133 $info[0][0] = get_string("files");
134 if ($ids) {
135 $info[0][1] = count($ids);
136 } else {
137 $info[0][1] = 0;
138 }
139
140 return $info;
141 }
142
143 //Calculate the number of course files to backup
144 //under $CFG->dataroot/$course, except $CFG->moddata
145 //and put them (their path) in backup_ids
146 //Return an array of info (name,value)
147 function course_files_check_backup($course,$backup_unique_code) {
148
149 global $CFG;
150
151 $rootdir = $CFG->dataroot."/$course";
152 $coursedirs = get_directory_list($rootdir,$CFG->moddata);
153 foreach ($coursedirs as $dir) {
154 //Insert them into backup_files
155 $status = execute_sql("INSERT INTO {$CFG->prefix}backup_files
156 (backup_code, file_type, path)
157 VALUES
158 ('$backup_unique_code','course','$dir')",false);
159 }
160
161 //Now execute the select
162 $ids = get_records_sql("SELECT DISTINCT b.path, b.old_id
163 FROM {$CFG->prefix}backup_files b
164 WHERE backup_code = '$backup_unique_code' AND
165 file_type = 'course'");
166 //Gets the user data
167 $info[0][0] = get_string("files");
168 if ($ids) {
169 $info[0][1] = count($ids);
170 } else {
171 $info[0][1] = 0;
172 }
173
174 return $info;
175 }
b0778a76 176
177 //Delete old data in backup tables (if exists)
178 //Two days seems to be apropiate
179 function backup_delete_old_data() {
674b30f5 180
181 global $CFG;
182
b0778a76 183 //Change this if you want !!
3b8bad6f 184 $days = 1;
b0778a76 185 //End change this
674b30f5 186 $seconds = $days * 24 * 60 * 60;
b0778a76 187 $delete_from = time()-$seconds;
188 //Now delete from tables
674b30f5 189 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
190 WHERE backup_code < '$delete_from'",false);
191 if ($status) {
192 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files
193 WHERE backup_code < '$delete_from'",false);
194 }
3b8bad6f 195 //Now, delete old directory (if exists)
196 if ($status) {
197 $status = backup_delete_old_dirs($delete_from);
198 }
b0778a76 199 return($status);
200 }
674b30f5 201
3b8bad6f 202 //Function to delete dirs/files into temp/backup directory
203 //older than $delete_from
204 function backup_delete_old_dirs($delete_from) {
205
206 global $CFG;
207
208 $status = true;
209 $list = get_directory_list($CFG->dataroot."/temp/backup", "", false);
210 foreach ($list as $file) {
211 $file_path = $CFG->dataroot."/temp/backup/".$file;
212 $moddate = filemtime($file_path);
213 if ($status and $moddate < $delete_from) {
214 $status = delete_dir_contents($file_path);
215 //There is nothing, delete the directory itself
216 if ($status) {
217 $status = rmdir($file_path);
218 }
219 }
220 }
221
222 return $status;
223 }
224
674b30f5 225 //Function to check if a directory exists
226 //and, optionally, create it
227 function check_dir_exists($dir,$create=false) {
228
229 global $CFG;
230
231 $status = true;
232 if(!is_dir($dir)) {
233 if (!$create) {
234 $status = false;
235 } else {
236 $status = mkdir ($dir,$CFG->directorypermissions);
237 }
238 }
239 return $status;
240 }
241
242 //Function to check and create the needed dir to
243 //save all the backup
244 function check_and_create_backup_dir($backup_unique_code) {
245
246 global $CFG;
247
248 $status = check_dir_exists($CFG->dataroot."/temp",true);
249 if ($status) {
250 $status = check_dir_exists($CFG->dataroot."/temp/backup",true);
251 }
252 if ($status) {
253 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code,true);
254 }
255
256 return $status;
257 }
258
259 //Function to delete all the directory contents recursively
260 //Copied from admin/delete.php
261 function delete_dir_contents ($rootdir) {
262
263 $dir = opendir($rootdir);
264
265 $status = true;
266
267 while ($file = readdir($dir)) {
268 if ($file != "." and $file != "..") {
269 $fullfile = "$rootdir/$file";
270 if (filetype($fullfile) == "dir") {
271 delete_dir_contents($fullfile);
272 if (!rmdir($fullfile)) {
3b8bad6f 273 $status = false;
674b30f5 274 }
275 } else {
276 if (!unlink("$fullfile")) {
3b8bad6f 277 $status = false;
674b30f5 278 }
279 }
280 }
281 }
282 closedir($dir);
283
284 return $status;
285
286 }
287
288 //Function to clear (empty) the contents of the backup_dir
289 //Copied from admin/delete.php
290 function clear_backup_dir($backup_unique_code) {
291
292 global $CFG;
293
294 $rootdir = $CFG->dataroot."/temp/backup/".$backup_unique_code;
295
296 //Delete recursively
297 $status = delete_dir_contents($rootdir);
298
299 return $status;
300 }
3b8bad6f 301
302 //Function to create, open and write header of the xml file
303 function backup_open_xml($backup_unique_code) {
304
305 global $CFG;
306
307 $status = true;
308
309 //Open for writing
310 $file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
311 $backup_file = fopen($file,"w");
312 //Writes the header
313 $status = fwrite ($backup_file,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
314 if ($status) {
315 $status = fwrite ($backup_file,start_tag("MOODLE_BACKUP",0,true));
316 }
317 if ($status) {
318 return $backup_file;
319 } else {
320 return false;
321 }
322 }
323
324 //Close the file
325 function backup_close_xml($backup_file) {
326 $status = fwrite ($backup_file,end_tag("MOODLE_BACKUP",0,true));
327 return fclose($backup_file);
328 }
329
330 //Return the xml start tag
331 function start_tag($tag,$level=0,$endline=false) {
332 if ($endline) {
333 $endchar = "\n";
334 } else {
335 $endchar = "";
336 }
337 return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar;
338 }
339
340 //Return the xml end tag
341 function end_tag($tag,$level=0,$endline=true) {
342 if ($endline) {
343 $endchar = "\n";
344 } else {
345 $endchar = "";
346 }
347 return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
348 }
349
350 //Return the start tag, the contents and the end tag
351 function full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
352 $st = start_tag($tag,$level,$endline);
353 $co="";
354 if ($to_utf) {
355 $co = $content;
356 } else {
357 $co = $content;
358 }
359 $et = end_tag($tag,0,true);
360 return $st.$co.$et;
361 }
362
363 //Prints General info about the course
364 //name, moodle_version (internal and release), backup_version, date, info in file...
365 function backup_general_info ($bf,$preferences) {
674b30f5 366
3b8bad6f 367 global $CFG;
368
369 fwrite ($bf,start_tag("INFO",1,true));
370
371 //The name of the backup
372 fwrite ($bf,full_tag("NAME",2,false,$preferences->backup_name));
373 //The moodle_version
374 fwrite ($bf,full_tag("MOODLE_VERSION",2,false,$preferences->moodle_version));
375 fwrite ($bf,full_tag("MOODLE_RELEASE",2,false,$preferences->moodle_release));
376 //The backup_version
377 fwrite ($bf,full_tag("BACKUP_VERSION",2,false,$preferences->backup_version));
378 fwrite ($bf,full_tag("BACKUP_RELEASE",2,false,$preferences->backup_release));
379 //The date
380 fwrite ($bf,full_tag("DATE",2,false,$preferences->backup_unique_code));
381 //Te includes tag
382 fwrite ($bf,start_tag("DETAILS",2,true));
383 //Now, go to mod element of preferences to print its status
384 foreach ($preferences->mods as $element) {
385 //Calculate info
386 $included = "false";
387 $userinfo = "false";
388 if ($element->backup) {
389 $included = "true";
390 if ($element->userinfo) {
391 $userinfo = "true";
392 }
393 }
394 //Prints the mod start
395 fwrite ($bf,start_tag("MOD",3,true));
396 fwrite ($bf,full_tag("NAME",4,false,$element->name));
397 fwrite ($bf,full_tag("INCLUDED",4,false,$included));
398 fwrite ($bf,full_tag("USERINFO",4,false,$userinfo));
399
400 //Print the end
401 fwrite ($bf,end_tag("MOD",3,true));
402 }
403 //The user in backup
404 if ($preferences->backup_users == 1) {
405 fwrite ($bf,full_tag("USERS",3,false,"COURSE"));
406 } else {
407 fwrite ($bf,full_tag("USERS",3,false,"ALL"));
408 }
409 //The logs in backup
410 if ($preferences->backup_logs == 1) {
411 fwrite ($bf,full_tag("LOGS",3,false,"true"));
412 } else {
413 fwrite ($bf,full_tag("LOGS",3,false,"false"));
414 }
415 //The user files
416 if ($preferences->backup_user_files == 1) {
417 fwrite ($bf,full_tag("USERFILES",3,false,"true"));
418 } else {
419 fwrite ($bf,full_tag("USERFILES",3,false,"false"));
420 }
421 //The course files
422 if ($preferences->backup_course_files == 1) {
423 fwrite ($bf,full_tag("COURSEFILES",3,false,"true"));
424 } else {
425 fwrite ($bf,full_tag("COURSEFILES",3,false,"false"));
426 }
427
428 fwrite ($bf,end_tag("DETAILS",2,true));
429
430
431 fwrite ($bf,end_tag("INFO",1,true));
432 }
afbe3de8 433?>