Removed from CVS. Changed to backup_check.html and backup_form.html
[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() {
180 //Change this if you want !!
181 $days = 2;
182 //End change this
183 $seconds = days * 24 * 60 * 60;
184 $delete_from = time()-$seconds;
185 //Now delete from tables
186 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids b
187 WHERE b.backup_code < '$delete_from'",false);
188 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files b
189 WHERE b.backup_code < '$delete_from'",false);
190 return($status);
191 }
afbe3de8 192?>