MDL-11515:
[moodle-linuxchix.git] / mod / data / backuplib.php
blobc8a1e1e108f5908c9b2e02785bdc32d216f2467d
1 <?php //Id:$
3 //This php script contains all the stuff to backup/restore data mod
5 //This is the "graphical" structure of the data mod:
6 //
7 // data
8 // (CL,pk->id)
9 // |
10 // |
11 // |
12 // ---------------------------------------------------------------------------------
13 // | |
14 //data_records (UL,pk->id, fk->data) data_fields (pk->id, fk->data)
15 // | |
16 // | |
17 // ----------------------------------------------------------------------------- |
18 // | | | |
19 //data_ratings(fk->recordid, pk->id) data_comments (fk->recordid, pk->id) | |
20 // data_content(pk->id, fk->recordid, fk->fieldid)
24 // Meaning: pk->primary key field of the table
25 // fk->foreign key to link with parent
26 // nt->nested field (recursive data)
27 // CL->course level info
28 // UL->user level info
29 // files->table may have files)
31 //-----------------------------------------------------------
33 //Backup data files because we've selected to backup user info
34 //and files are user info's level
37 //Return a content encoded to support interactivities linking. Every module
39 function data_backup_mods($bf,$preferences) {
40 global $CFG;
42 $status = true;
44 // iterate
45 if ($datas = get_records('data','course',$preferences->backup_course,"id")) {
46 foreach ($datas as $data) {
47 if (function_exists('backup_mod_selected')) {
48 // Moodle 1.6
49 $backup_mod_selected = backup_mod_selected($preferences, 'data', $data->id);
50 } else {
51 // Moodle 1.5
52 $backup_mod_selected = true;
54 if ($backup_mod_selected) {
55 $status = data_backup_one_mod($bf,$preferences,$data);
56 // backup files happens in backup_one_mod now too.
60 return $status;
63 function data_backup_one_mod($bf,$preferences,$data) {
64 global $CFG;
66 if (is_numeric($data)) { // backwards compatibility
67 $data = get_record('data','id',$data);
69 $instanceid = $data->id;
71 $status = true;
74 fwrite ($bf,start_tag("MOD",3,true));
75 //Print data data
76 fwrite ($bf,full_tag("ID",4,false,$data->id));
77 fwrite ($bf,full_tag("MODTYPE",4,false,"data"));
78 fwrite ($bf,full_tag("NAME",4,false,$data->name));
79 fwrite ($bf,full_tag("INTRO",4,false,$data->intro));
80 fwrite ($bf,full_tag("COMMENTS",4,false,$data->comments));
81 fwrite ($bf,full_tag("TIMEAVAILABLEFROM",4,false,$data->timeavailablefrom));
82 fwrite ($bf,full_tag("TIMEAVAILABLETO",4,false,$data->timeavailableto));
83 fwrite ($bf,full_tag("TIMEVIEWFROM",4,false,$data->timeviewfrom));
84 fwrite ($bf,full_tag("TIMEVIEWTO",4,false,$data->timeviewto));
85 fwrite ($bf,full_tag("REQUIREDENTRIES",4,false,$data->requiredentries));
86 fwrite ($bf,full_tag("REQUIREDENTRIESTOVIEW",4,false,$data->requiredentriestoview));
87 fwrite ($bf,full_tag("MAXENTRIES",4,false,$data->maxentries));
88 fwrite ($bf,full_tag("RSSARTICLES",4,false,$data->rssarticles));
89 fwrite ($bf,full_tag("SINGLETEMPLATE",4,false,$data->singletemplate));
90 fwrite ($bf,full_tag("LISTTEMPLATE",4,false,$data->listtemplate));
91 fwrite ($bf,full_tag("LISTTEMPLATEHEADER",4,false,$data->listtemplateheader));
92 fwrite ($bf,full_tag("LISTTEMPLATEFOOTER",4,false,$data->listtemplatefooter));
93 fwrite ($bf,full_tag("ADDTEMPLATE",4,false,$data->addtemplate));
94 fwrite ($bf,full_tag("RSSTEMPLATE",4,false,$data->rsstemplate));
95 fwrite ($bf,full_tag("RSSTITLETEMPLATE",4,false,$data->rsstitletemplate));
96 fwrite ($bf,full_tag("CSSTEMPLATE",4,false,$data->csstemplate));
97 fwrite ($bf,full_tag("JSTEMPLATE",4,false,$data->jstemplate));
98 fwrite ($bf,full_tag("APPROVAL",4,false,$data->approval));
99 fwrite ($bf,full_tag("SCALE",4,false,$data->scale));
100 fwrite ($bf,full_tag("ASSESSED",4,false,$data->assessed));
101 fwrite ($bf,full_tag("DEFAULTSORT",4,false,$data->defaultsort));
102 fwrite ($bf,full_tag("DEFAULTSORTDIR",4,false,$data->defaultsortdir));
103 fwrite ($bf,full_tag("EDITANY",4,false,$data->editany));
104 fwrite ($bf,full_tag("NOTIFICATION",4,false,$data->notification));
106 // if we've selected to backup users info, then call any other functions we need
107 // including backing up individual files
109 $status = backup_data_fields($bf,$preferences,$data->id);
111 if (backup_userdata_selected($preferences,'data',$data->id)) {
112 //$status = backup_someuserdata_for_this_instance();
113 //$status = backup_somefiles_for_this_instance();
114 // ... etc
116 $status = backup_data_records($bf,$preferences,$data->id);
117 if ($status) {
118 $status = backup_data_files_instance($bf,$preferences,$data->id); //recursive copy
121 fwrite ($bf,end_tag("MOD",3,true));
122 return $status;
127 function backup_data_fields($bf,$preferences,$dataid){
128 global $CFG;
129 $status = true;
131 $data_fields = get_records("data_fields","dataid",$dataid);
133 //If there is submissions
134 if ($data_fields) {
135 //Write start tag
136 $status =fwrite ($bf,start_tag("FIELDS",4,true));
137 //Iterate over each submission
138 foreach ($data_fields as $fie_sub) {
139 //Start submission
140 $status =fwrite ($bf,start_tag("FIELD",5,true));
141 //Print submission contents
142 fwrite ($bf,full_tag("ID",6,false,$fie_sub->id));
143 fwrite ($bf,full_tag("DATAID",6,false,$fie_sub->dataid));
144 fwrite ($bf,full_tag("TYPE",6,false,$fie_sub->type));
145 fwrite ($bf,full_tag("NAME",6,false,$fie_sub->name));
146 fwrite ($bf,full_tag("DESCRIPTION",6,false,$fie_sub->description));
147 fwrite ($bf,full_tag("PARAM1",6,false,$fie_sub->param1));
148 fwrite ($bf,full_tag("PARAM2",6,false,$fie_sub->param2));
149 fwrite ($bf,full_tag("PARAM3",6,false,$fie_sub->param3));
150 fwrite ($bf,full_tag("PARAM4",6,false,$fie_sub->param4));
151 fwrite ($bf,full_tag("PARAM5",6,false,$fie_sub->param5));
152 fwrite ($bf,full_tag("PARAM6",6,false,$fie_sub->param6));
153 fwrite ($bf,full_tag("PARAM7",6,false,$fie_sub->param7));
154 fwrite ($bf,full_tag("PARAM8",6,false,$fie_sub->param8));
155 fwrite ($bf,full_tag("PARAM9",6,false,$fie_sub->param9));
156 fwrite ($bf,full_tag("PARAM10",6,false,$fie_sub->param10));
158 //End submission
159 $status =fwrite ($bf,end_tag("FIELD",5,true));
161 //Write end tag
162 $status =fwrite ($bf,end_tag("FIELDS",4,true));
164 return $status;
167 function backup_data_content($bf,$preferences,$recordid){
168 global $CFG;
169 $status = true;
171 $data_contents = get_records("data_content","recordid",$recordid);
173 //If there is submissions
174 if ($data_contents) {
175 //Write start tag
176 $status =fwrite ($bf,start_tag("CONTENTS",6,true));
177 //Iterate over each submission
178 foreach ($data_contents as $cnt_sub) {
179 //Start submission
180 $status =fwrite ($bf,start_tag("CONTENT",7,true));
181 //Print submission contents
182 fwrite ($bf,full_tag("ID",8,false,$cnt_sub->id));
183 fwrite ($bf,full_tag("RECORDID",8,false,$cnt_sub->recordid));
184 fwrite ($bf,full_tag("FIELDID",8,false,$cnt_sub->fieldid));
185 fwrite ($bf,full_tag("CONTENT",8,false,$cnt_sub->content));
186 fwrite ($bf,full_tag("CONTENT1",8,false,$cnt_sub->content1));
187 fwrite ($bf,full_tag("CONTENT2",8,false,$cnt_sub->content2));
188 fwrite ($bf,full_tag("CONTENT3",8,false,$cnt_sub->content3));
189 fwrite ($bf,full_tag("CONTENT4",8,false,$cnt_sub->content4));
190 //End submission
191 $status =fwrite ($bf,end_tag("CONTENT",7,true));
193 //Write end tag
194 $status =fwrite ($bf,end_tag("CONTENTS",6,true));
196 return $status;
199 function backup_data_ratings($bf,$preferences,$recordid){
200 global $CFG;
201 $status = true;
202 $data_ratings = get_records("data_ratings","recordid",$recordid);
204 //If there is submissions
205 if ($data_ratings) {
206 //Write start tag
207 $status =fwrite ($bf,start_tag("RATINGS",6,true));
208 //Iterate over each submission
209 foreach ($data_ratings as $rat_sub) {
210 //Start submission
211 $status =fwrite ($bf,start_tag("RATING",7,true));
212 //Print submission contents
213 fwrite ($bf,full_tag("ID",8,false,$rat_sub->id));
214 fwrite ($bf,full_tag("RECORDID",8,false,$rat_sub->recordid));
215 fwrite ($bf,full_tag("USERID",8,false,$rat_sub->userid));
216 fwrite ($bf,full_tag("RATING",8,false,$rat_sub->rating));
217 //End submission
218 $status =fwrite ($bf,end_tag("RATING",7,true));
220 //Write end tag
221 $status =fwrite ($bf,end_tag("RATINGS",6,true));
225 return $status;
227 function backup_data_comments($bf,$preferences,$recordid){
228 global $CFG;
229 $status = true;
230 $data_comments = get_records("data_comments","recordid",$recordid);
232 //If there is submissions
233 if ($data_comments) {
234 //Write start tag
235 $status =fwrite ($bf,start_tag("COMMENTS",6,true));
236 //Iterate over each submission
237 foreach ($data_comments as $com_sub) {
238 //Start submission
239 $status =fwrite ($bf,start_tag("COMMENT",7,true));
240 //Print submission contents
241 fwrite ($bf,full_tag("ID",8,false,$com_sub->id));
242 fwrite ($bf,full_tag("RECORDID",8,false,$com_sub->recordid));
243 fwrite ($bf,full_tag("USERID",8,false,$com_sub->userid));
244 fwrite ($bf,full_tag("CONTENT",8,false,$com_sub->content));
245 fwrite ($bf,full_tag("CREATED",8,false,$com_sub->created));
246 fwrite ($bf,full_tag("MODIFIED",8,false,$com_sub->modified));
247 //End submission
248 $status =fwrite ($bf,end_tag("COMMENT",7,true));
250 //Write end tag
251 $status =fwrite ($bf,end_tag("COMMENTS",6,true));
253 return $status;
257 function backup_data_files_instance($bf,$preferences,$instanceid) {
259 global $CFG;
260 $status = true;
262 //First we check to moddata exists and create it as necessary
263 //in temp/backup/$backup_code dir
264 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
265 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data/",true);
266 //Now copy the data dir
267 if ($status) {
268 //Only if it exists !! Thanks to Daniel Miksik.
269 if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data/".$instanceid)) {
270 $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data/".$instanceid,
271 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data/".$instanceid);
274 return $status;
277 function backup_data_records($bf,$preferences,$dataid){
279 global $CFG;
280 $status = true;
282 $data_records = get_records("data_records","dataid",$dataid);
283 //If there is submissions
284 if ($data_records) {
285 //Write start tag
286 $status =fwrite ($bf,start_tag("RECORDS",4,true));
287 //Iterate over each submission
288 foreach ($data_records as $rec_sub) {
289 //Start submission
290 $status =fwrite ($bf,start_tag("RECORD",5,true));
291 //Print submission contents
292 fwrite ($bf,full_tag("ID",6,false,$rec_sub->id));
293 fwrite ($bf,full_tag("USERID",6,false,$rec_sub->userid));
294 fwrite ($bf,full_tag("GROUPID",6,false,$rec_sub->groupid));
295 fwrite ($bf,full_tag("DATAID",6,false,$rec_sub->dataid));
296 fwrite ($bf,full_tag("TIMECREATED",6,false,$rec_sub->timecreated));
297 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$rec_sub->timemodified));
298 fwrite ($bf,full_tag("APPROVED",6,false,$rec_sub->approved));
299 //End submission
301 backup_data_content($bf,$preferences,$rec_sub->id);
302 backup_data_ratings($bf,$preferences,$rec_sub->id);
303 backup_data_comments($bf,$preferences,$rec_sub->id);
305 $status =fwrite ($bf,end_tag("RECORD",5,true));
307 //Write end tag
308 $status =fwrite ($bf,end_tag("RECORDS",4,true));
310 return $status;
314 function backup_data_files($bf,$preferences) {
316 global $CFG;
318 $status = true;
320 //First we check to moddata exists and create it as necessary
321 //in temp/backup/$backup_code dir
322 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
323 //Now copy the data dir
324 if ($status) {
325 //Only if it exists !! Thanks to Daniel Miksik.
326 if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data")) {
327 $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data",
328 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data");
332 return $status;
335 function backup_data_file_instance($bf,$preferences,$instanceid) {
337 global $CFG;
338 $status = true;
340 //First we check to moddata exists and create it as necessary
341 //in temp/backup/$backup_code dir
342 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
343 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data/",true);
344 //Now copy the data dir
345 if ($status) {
346 //Only if it exists !! Thanks to Daniel Miksik.
347 if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data/".$instanceid)) {
348 $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data/".$instanceid,
349 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data/".$instanceid);
352 return $status;
355 function data_check_backup_mods_instances($instance,$backup_unique_code) {
356 $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
357 $info[$instance->id.'0'][1] = '';
358 if (!empty($instance->userdata)) {
359 // any other needed stuff
361 return $info;
364 function data_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
365 if (!empty($instances) && is_array($instances) && count($instances)) {
366 $info = array();
367 foreach ($instances as $id => $instance) {
368 $info += data_check_backup_mods_instances($instance,$backup_unique_code);
370 return $info;
373 // otherwise continue as normal
374 //First the course data
375 $info[0][0] = get_string("modulenameplural","data");
376 if ($ids = data_ids ($course)) {
377 $info[0][1] = count($ids);
378 } else {
379 $info[0][1] = 0;
382 //Now, if requested, the user_data
383 if ($user_data) {
384 // any other needed stuff
386 return $info;
391 * Returns a content encoded to support interactivities linking. Every module
392 * should have its own. They are called automatically from the backup procedure.
394 * @param string $content content to be encoded
395 * @param object $preferences backup preferences in use
396 * @return string the content encoded
398 function data_encode_content_links ($content,$preferences) {
400 global $CFG;
402 $base = preg_quote($CFG->wwwroot,"/");
404 /// Link to one "record" of the database
405 $search="/(".$base."\/mod\/data\/view.php\?d\=)([0-9]+)\&rid\=([0-9]+)/";
406 $result= preg_replace($search,'$@DATAVIEWRECORD*$2*$3@$',$content);
408 /// Link to the list of databases
409 $search="/(".$base."\/mod\/data\/index.php\?id\=)([0-9]+)/";
410 $result= preg_replace($search,'$@DATAINDEX*$2@$',$result);
412 /// Link to database view by moduleid
413 $search="/(".$base."\/mod\/data\/view.php\?id\=)([0-9]+)/";
414 $result= preg_replace($search,'$@DATAVIEWBYID*$2@$',$result);
416 /// Link to database view by databaseid
417 $search="/(".$base."\/mod\/data\/view.php\?d\=)([0-9]+)/";
418 $result= preg_replace($search,'$@DATAVIEWBYD*$2@$',$result);
420 return $result;
423 function data_ids($course) {
424 // stub function, return number of modules
425 return 1;