MDL-19057 simplified test table management
[moodle.git] / mod / data / backuplib.php
CommitLineData
1adbd2c3 1<?php
dfe9354f 2
c65d1af5 3//This php script contains all the stuff to backup/restore data mod
816039b5 4
c65d1af5 5 //This is the "graphical" structure of the data mod:
816039b5 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 //-----------------------------------------------------------
32
c65d1af5 33 //Backup data files because we've selected to backup user info
816039b5 34 //and files are user info's level
35
36
37 //Return a content encoded to support interactivities linking. Every module
aab98aaf 38
1ecea976 39function data_backup_mods($bf,$preferences) {
a656d951 40 global $CFG, $DB;
1ecea976 41
42 $status = true;
aab98aaf 43
1ecea976 44 // iterate
a656d951 45 if ($datas = $DB->get_records('data', array('course'=>$preferences->backup_course),"id")) {
1ecea976 46 foreach ($datas as $data) {
3cdd4eeb 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) {
1ecea976 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}
62
63function data_backup_one_mod($bf,$preferences,$data) {
a656d951 64 global $CFG, $DB;
aab98aaf 65
1ecea976 66 if (is_numeric($data)) { // backwards compatibility
a656d951 67 $data = $DB->get_record('data', array('id'=>$data));
1ecea976 68 }
69 $instanceid = $data->id;
aab98aaf 70
1ecea976 71 $status = true;
72
75785350 73
816039b5 74 fwrite ($bf,start_tag("MOD",3,true));
c65d1af5 75 //Print data data
816039b5 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));
816039b5 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));
816039b5 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));
8ac49714 91 fwrite ($bf,full_tag("LISTTEMPLATEHEADER",4,false,$data->listtemplateheader));
92 fwrite ($bf,full_tag("LISTTEMPLATEFOOTER",4,false,$data->listtemplatefooter));
816039b5 93 fwrite ($bf,full_tag("ADDTEMPLATE",4,false,$data->addtemplate));
94 fwrite ($bf,full_tag("RSSTEMPLATE",4,false,$data->rsstemplate));
7dbc6b58 95 fwrite ($bf,full_tag("RSSTITLETEMPLATE",4,false,$data->rsstitletemplate));
8ac49714 96 fwrite ($bf,full_tag("CSSTEMPLATE",4,false,$data->csstemplate));
97 fwrite ($bf,full_tag("JSTEMPLATE",4,false,$data->jstemplate));
f12c9573 98 fwrite ($bf,full_tag("ASEARCHTEMPLATE",4,false,$data->asearchtemplate));
cf3e199b 99 fwrite ($bf,full_tag("APPROVAL",4,false,$data->approval));
4d3d87f0 100 fwrite ($bf,full_tag("SCALE",4,false,$data->scale));
101 fwrite ($bf,full_tag("ASSESSED",4,false,$data->assessed));
b6aeb9ba 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));
aab98aaf 106
1ecea976 107 // if we've selected to backup users info, then call any other functions we need
108 // including backing up individual files
aab98aaf 109
5fdb12fe 110 $status = backup_data_fields($bf,$preferences,$data->id);
aab98aaf 111
1ecea976 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
75785350 116
aab98aaf 117 $status = backup_data_records($bf,$preferences,$data->id);
816039b5 118 if ($status) {
c65d1af5 119 $status = backup_data_files_instance($bf,$preferences,$data->id); //recursive copy
816039b5 120 }
121 }
122 fwrite ($bf,end_tag("MOD",3,true));
123 return $status;
aab98aaf 124
816039b5 125}
816039b5 126
816039b5 127
816039b5 128function backup_data_fields($bf,$preferences,$dataid){
a656d951 129 global $CFG, $DB;
816039b5 130 $status = true;
131
a656d951 132 $data_fields = $DB->get_records("data_fields", array("dataid"=>$dataid));
816039b5 133
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));
aab98aaf 158
816039b5 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;
166}
167
168function backup_data_content($bf,$preferences,$recordid){
a656d951 169 global $CFG, $DB;
816039b5 170 $status = true;
171
a656d951 172 $data_contents = $DB->get_records("data_content", array("recordid"=>$recordid));
816039b5 173
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));
dfe9354f 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));
816039b5 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;
198
199}
200function backup_data_ratings($bf,$preferences,$recordid){
a656d951 201 global $CFG, $DB;
202
816039b5 203 $status = true;
a656d951 204 $data_ratings = $DB->get_records("data_ratings", array("recordid"=>$recordid));
816039b5 205
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));
75785350 224
1ecea976 225 }
75785350 226
816039b5 227 return $status;
228}
229function backup_data_comments($bf,$preferences,$recordid){
a656d951 230 global $CFG, $DB;
231
816039b5 232 $status = true;
e998effa
DC
233
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));
236
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 );
816039b5 249
250 //If there is submissions
5ae56899 251 if ($data_comments) {
816039b5 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));
e998effa 258
816039b5 259 //Print submission contents
260 fwrite ($bf,full_tag("ID",8,false,$com_sub->id));
816039b5 261 fwrite ($bf,full_tag("USERID",8,false,$com_sub->userid));
262 fwrite ($bf,full_tag("CONTENT",8,false,$com_sub->content));
e998effa
DC
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));
816039b5 266 //End submission
e998effa 267
816039b5 268 $status =fwrite ($bf,end_tag("COMMENT",7,true));
269 }
270 //Write end tag
271 $status =fwrite ($bf,end_tag("COMMENTS",6,true));
272 }
1ecea976 273 return $status;
816039b5 274}
275
816039b5 276
277function backup_data_files_instance($bf,$preferences,$instanceid) {
278
279 global $CFG;
280 $status = true;
281
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);
c65d1af5 286 //Now copy the data dir
816039b5 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;
1ecea976 295}
dfe9354f 296
297function backup_data_records($bf,$preferences,$dataid){
a656d951 298 global $CFG, $DB;
dfe9354f 299
dfe9354f 300 $status = true;
75785350 301
a656d951 302 $data_records = $DB->get_records("data_records", array("dataid"=>$dataid));
dfe9354f 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
75785350 310 $status =fwrite ($bf,start_tag("RECORD",5,true));
dfe9354f 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));
cf3e199b 318 fwrite ($bf,full_tag("APPROVED",6,false,$rec_sub->approved));
dfe9354f 319 //End submission
75785350 320
dfe9354f 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);
dfe9354f 324
75785350 325 $status =fwrite ($bf,end_tag("RECORD",5,true));
dfe9354f 326 }
327 //Write end tag
75785350 328 $status =fwrite ($bf,end_tag("RECORDS",4,true));
dfe9354f 329 }
330 return $status;
dfe9354f 331
332}
333
334function backup_data_files($bf,$preferences) {
dfe9354f 335 global $CFG;
336
337 $status = true;
338
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);
c65d1af5 342 //Now copy the data dir
dfe9354f 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 }
aab98aaf 350
dfe9354f 351 return $status;
352}
353
354function backup_data_file_instance($bf,$preferences,$instanceid) {
dfe9354f 355 global $CFG;
a656d951 356
dfe9354f 357 $status = true;
358
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);
c65d1af5 363 //Now copy the data dir
dfe9354f 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;
372}
1ecea976 373
374function 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;
381}
382
383function 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 }
aab98aaf 391
1ecea976 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 }
aab98aaf 400
1ecea976 401 //Now, if requested, the user_data
402 if ($user_data) {
403 // any other needed stuff
404 }
405 return $info;
406
407}
408
e1b1f331 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 */
417function data_encode_content_links ($content,$preferences) {
e1b1f331 418 global $CFG;
419
420 $base = preg_quote($CFG->wwwroot,"/");
421
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);
425
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);
429
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);
433
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);
437
438 return $result;
439}
440
1ecea976 441function data_ids($course) {
442 // stub function, return number of modules
443 return 1;
444}