MDL-19057 simplified test table management
[moodle.git] / mod / data / backuplib.php
1 <?php
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)
21     //
22     //
23     //
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)
30     //
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, $DB;
42     $status = true;
44     // iterate
45     if ($datas = $DB->get_records('data', array('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;
53             }
54             if ($backup_mod_selected) {
55                 $status = data_backup_one_mod($bf,$preferences,$data);
56                 // backup files happens in backup_one_mod now too.
57             }
58         }
59     }
60     return $status;
61 }
63 function data_backup_one_mod($bf,$preferences,$data) {
64     global $CFG, $DB;
66     if (is_numeric($data)) { // backwards compatibility
67         $data = $DB->get_record('data', array('id'=>$data));
68     }
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("ASEARCHTEMPLATE",4,false,$data->asearchtemplate));
99     fwrite ($bf,full_tag("APPROVAL",4,false,$data->approval));
100     fwrite ($bf,full_tag("SCALE",4,false,$data->scale));
101     fwrite ($bf,full_tag("ASSESSED",4,false,$data->assessed));
102     fwrite ($bf,full_tag("DEFAULTSORT",4,false,$data->defaultsort));
103     fwrite ($bf,full_tag("DEFAULTSORTDIR",4,false,$data->defaultsortdir));
104     fwrite ($bf,full_tag("EDITANY",4,false,$data->editany));
105     fwrite ($bf,full_tag("NOTIFICATION",4,false,$data->notification));
107     // if we've selected to backup users info, then call any other functions we need
108     // including backing up individual files
110     $status = backup_data_fields($bf,$preferences,$data->id);
112     if (backup_userdata_selected($preferences,'data',$data->id)) {
113         //$status = backup_someuserdata_for_this_instance();
114         //$status = backup_somefiles_for_this_instance();
115         // ... etc
117         $status = backup_data_records($bf,$preferences,$data->id);
118         if ($status) {
119             $status = backup_data_files_instance($bf,$preferences,$data->id);    //recursive copy
120         }
121     }
122     fwrite ($bf,end_tag("MOD",3,true));
123     return $status;
128 function backup_data_fields($bf,$preferences,$dataid){
129     global $CFG, $DB;
130     $status = true;
132     $data_fields = $DB->get_records("data_fields", array("dataid"=>$dataid));
134         //If there is submissions
135         if ($data_fields) {
136             //Write start tag
137             $status =fwrite ($bf,start_tag("FIELDS",4,true));
138             //Iterate over each submission
139             foreach ($data_fields as $fie_sub) {
140                 //Start submission
141                 $status =fwrite ($bf,start_tag("FIELD",5,true));
142                 //Print submission contents
143                 fwrite ($bf,full_tag("ID",6,false,$fie_sub->id));
144                 fwrite ($bf,full_tag("DATAID",6,false,$fie_sub->dataid));
145                 fwrite ($bf,full_tag("TYPE",6,false,$fie_sub->type));
146                 fwrite ($bf,full_tag("NAME",6,false,$fie_sub->name));
147                 fwrite ($bf,full_tag("DESCRIPTION",6,false,$fie_sub->description));
148                 fwrite ($bf,full_tag("PARAM1",6,false,$fie_sub->param1));
149                 fwrite ($bf,full_tag("PARAM2",6,false,$fie_sub->param2));
150                 fwrite ($bf,full_tag("PARAM3",6,false,$fie_sub->param3));
151                 fwrite ($bf,full_tag("PARAM4",6,false,$fie_sub->param4));
152                 fwrite ($bf,full_tag("PARAM5",6,false,$fie_sub->param5));
153                 fwrite ($bf,full_tag("PARAM6",6,false,$fie_sub->param6));
154                 fwrite ($bf,full_tag("PARAM7",6,false,$fie_sub->param7));
155                 fwrite ($bf,full_tag("PARAM8",6,false,$fie_sub->param8));
156                 fwrite ($bf,full_tag("PARAM9",6,false,$fie_sub->param9));
157                 fwrite ($bf,full_tag("PARAM10",6,false,$fie_sub->param10));
159                 //End submission
160                 $status =fwrite ($bf,end_tag("FIELD",5,true));
161             }
162             //Write end tag
163             $status =fwrite ($bf,end_tag("FIELDS",4,true));
164         }
165         return $status;
168 function backup_data_content($bf,$preferences,$recordid){
169     global $CFG, $DB;
170     $status = true;
172     $data_contents = $DB->get_records("data_content", array("recordid"=>$recordid));
174         //If there is submissions
175         if ($data_contents) {
176             //Write start tag
177             $status =fwrite ($bf,start_tag("CONTENTS",6,true));
178             //Iterate over each submission
179             foreach ($data_contents as $cnt_sub) {
180                 //Start submission
181                 $status =fwrite ($bf,start_tag("CONTENT",7,true));
182                 //Print submission contents
183                 fwrite ($bf,full_tag("ID",8,false,$cnt_sub->id));
184                 fwrite ($bf,full_tag("RECORDID",8,false,$cnt_sub->recordid));
185                 fwrite ($bf,full_tag("FIELDID",8,false,$cnt_sub->fieldid));
186                 fwrite ($bf,full_tag("CONTENT",8,false,$cnt_sub->content));
187                 fwrite ($bf,full_tag("CONTENT1",8,false,$cnt_sub->content1));
188                 fwrite ($bf,full_tag("CONTENT2",8,false,$cnt_sub->content2));
189                 fwrite ($bf,full_tag("CONTENT3",8,false,$cnt_sub->content3));
190                 fwrite ($bf,full_tag("CONTENT4",8,false,$cnt_sub->content4));
191                 //End submission
192                 $status =fwrite ($bf,end_tag("CONTENT",7,true));
193             }
194             //Write end tag
195             $status =fwrite ($bf,end_tag("CONTENTS",6,true));
196         }
197         return $status;
200 function backup_data_ratings($bf,$preferences,$recordid){
201     global $CFG, $DB;
203     $status = true;
204     $data_ratings = $DB->get_records("data_ratings", array("recordid"=>$recordid));
206     //If there is submissions
207     if ($data_ratings) {
208         //Write start tag
209         $status =fwrite ($bf,start_tag("RATINGS",6,true));
210         //Iterate over each submission
211         foreach ($data_ratings as $rat_sub) {
212             //Start submission
213             $status =fwrite ($bf,start_tag("RATING",7,true));
214             //Print submission contents
215             fwrite ($bf,full_tag("ID",8,false,$rat_sub->id));
216             fwrite ($bf,full_tag("RECORDID",8,false,$rat_sub->recordid));
217             fwrite ($bf,full_tag("USERID",8,false,$rat_sub->userid));
218             fwrite ($bf,full_tag("RATING",8,false,$rat_sub->rating));
219             //End submission
220             $status =fwrite ($bf,end_tag("RATING",7,true));
221         }
222             //Write end tag
223         $status =fwrite ($bf,end_tag("RATINGS",6,true));
225     }
227     return $status;
229 function backup_data_comments($bf,$preferences,$recordid){
230     global $CFG, $DB;
232     $status = true;
234     $lastrecord   = $DB->get_record_sql('SELECT d.id AS dataid, d.course AS course FROM {data} d, {data_records} r
235                                           WHERE r.dataid = d.id AND r.id = ?', array($recordid));
237     $params = array();
238     $params[] = $recordid;
239     $sql = 'SELECT d.id, d.course FROM {data_records} r, {data} d WHERE r.dataid = d.id AND r.id=?';
240     $result = $DB->get_record_sql($sql, $params);
241     if ($cm = get_coursemodule_from_instance('data', $result->id, $result->course)) {
242         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
243     }
244     $data_comments = $DB->get_records('comments', array(
245                         'itemid'=>$recordid,
246                         'commentarea'=>'database_entry',
247                         'contextid'=>$context->id)
248                     );
250     //If there is submissions
251     if ($data_comments) {
252         //Write start tag
253         $status =fwrite ($bf,start_tag("COMMENTS",6,true));
254             //Iterate over each submission
255         foreach ($data_comments as $com_sub) {
256             //Start submission
257             $status =fwrite ($bf,start_tag("COMMENT",7,true));
259             //Print submission contents
260             fwrite ($bf,full_tag("ID",8,false,$com_sub->id));
261             fwrite ($bf,full_tag("USERID",8,false,$com_sub->userid));
262             fwrite ($bf,full_tag("CONTENT",8,false,$com_sub->content));
263             fwrite ($bf,full_tag("COMMENTAREA",8,false,'database_entry'));
264             fwrite ($bf,full_tag("FORMAT",8,false,$com_sub->format));
265             fwrite ($bf,full_tag("TIMECREATED",8,false,$com_sub->timecreated));
266             //End submission
268             $status =fwrite ($bf,end_tag("COMMENT",7,true));
269         }
270         //Write end tag
271         $status =fwrite ($bf,end_tag("COMMENTS",6,true));
272     }
273     return $status;
277 function backup_data_files_instance($bf,$preferences,$instanceid) {
279     global $CFG;
280     $status = true;
282         //First we check to moddata exists and create it as necessary
283         //in temp/backup/$backup_code  dir
284     $status = check_and_create_moddata_dir($preferences->backup_unique_code);
285     $status = check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data/",true);
286         //Now copy the data dir
287     if ($status) {
288             //Only if it exists !! Thanks to Daniel Miksik.
289         if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data/".$instanceid)) {
290             $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data/".$instanceid,
291                                            $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data/".$instanceid);
292         }
293     }
294     return $status;
297 function backup_data_records($bf,$preferences,$dataid){
298     global $CFG, $DB;
300     $status = true;
302     $data_records = $DB->get_records("data_records", array("dataid"=>$dataid));
303         //If there is submissions
304         if ($data_records) {
305             //Write start tag
306             $status =fwrite ($bf,start_tag("RECORDS",4,true));
307             //Iterate over each submission
308             foreach ($data_records as $rec_sub) {
309                 //Start submission
310                 $status =fwrite ($bf,start_tag("RECORD",5,true));
311                 //Print submission contents
312                 fwrite ($bf,full_tag("ID",6,false,$rec_sub->id));
313                 fwrite ($bf,full_tag("USERID",6,false,$rec_sub->userid));
314                 fwrite ($bf,full_tag("GROUPID",6,false,$rec_sub->groupid));
315                 fwrite ($bf,full_tag("DATAID",6,false,$rec_sub->dataid));
316                 fwrite ($bf,full_tag("TIMECREATED",6,false,$rec_sub->timecreated));
317                 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$rec_sub->timemodified));
318                 fwrite ($bf,full_tag("APPROVED",6,false,$rec_sub->approved));
319                 //End submission
321                 backup_data_content($bf,$preferences,$rec_sub->id);
322                 backup_data_ratings($bf,$preferences,$rec_sub->id);
323                 backup_data_comments($bf,$preferences,$rec_sub->id);
325                 $status =fwrite ($bf,end_tag("RECORD",5,true));
326             }
327             //Write end tag
328             $status =fwrite ($bf,end_tag("RECORDS",4,true));
329         }
330         return $status;
334 function backup_data_files($bf,$preferences) {
335     global $CFG;
337     $status = true;
339         //First we check to moddata exists and create it as necessary
340         //in temp/backup/$backup_code  dir
341     $status = check_and_create_moddata_dir($preferences->backup_unique_code);
342         //Now copy the data dir
343     if ($status) {
344             //Only if it exists !! Thanks to Daniel Miksik.
345         if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data")) {
346             $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data",
347                                                $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data");
348         }
349     }
351     return $status;
354 function backup_data_file_instance($bf,$preferences,$instanceid) {
355     global $CFG;
357     $status = true;
359         //First we check to moddata exists and create it as necessary
360         //in temp/backup/$backup_code  dir
361     $status = check_and_create_moddata_dir($preferences->backup_unique_code);
362     $status = check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data/",true);
363         //Now copy the data dir
364     if ($status) {
365             //Only if it exists !! Thanks to Daniel Miksik.
366         if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data/".$instanceid)) {
367             $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data/".$instanceid,
368                                            $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data/".$instanceid);
369         }
370     }
371     return $status;
374 function data_check_backup_mods_instances($instance,$backup_unique_code) {
375     $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
376     $info[$instance->id.'0'][1] = '';
377     if (!empty($instance->userdata)) {
378         // any other needed stuff
379     }
380     return $info;
383 function data_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
384     if (!empty($instances) && is_array($instances) && count($instances)) {
385         $info = array();
386         foreach ($instances as $id => $instance) {
387             $info += data_check_backup_mods_instances($instance,$backup_unique_code);
388         }
389         return $info;
390     }
392     // otherwise continue as normal
393     //First the course data
394     $info[0][0] = get_string("modulenameplural","data");
395     if ($ids = data_ids ($course)) {
396         $info[0][1] = count($ids);
397     } else {
398         $info[0][1] = 0;
399     }
401     //Now, if requested, the user_data
402     if ($user_data) {
403         // any other needed stuff
404     }
405     return $info;
409 /**
410  * Returns a content encoded to support interactivities linking. Every module
411  * should have its own. They are called automatically from the backup procedure.
412  *
413  * @param string $content content to be encoded
414  * @param object $preferences backup preferences in use
415  * @return string the content encoded
416  */
417 function data_encode_content_links ($content,$preferences) {
418     global $CFG;
420     $base = preg_quote($CFG->wwwroot,"/");
422 /// Link to one "record" of the database
423     $search="/(".$base."\/mod\/data\/view.php\?d\=)([0-9]+)\&rid\=([0-9]+)/";
424     $result= preg_replace($search,'$@DATAVIEWRECORD*$2*$3@$',$content);
426 /// Link to the list of databases
427     $search="/(".$base."\/mod\/data\/index.php\?id\=)([0-9]+)/";
428     $result= preg_replace($search,'$@DATAINDEX*$2@$',$result);
430 /// Link to database view by moduleid
431     $search="/(".$base."\/mod\/data\/view.php\?id\=)([0-9]+)/";
432     $result= preg_replace($search,'$@DATAVIEWBYID*$2@$',$result);
434 /// Link to database view by databaseid
435     $search="/(".$base."\/mod\/data\/view.php\?d\=)([0-9]+)/";
436     $result= preg_replace($search,'$@DATAVIEWBYD*$2@$',$result);
438     return $result;
441 function data_ids($course) {
442     // stub function, return number of modules
443     return 1;