3 //This php script contains all the stuff to backup/restore data mod
5 //This is the "graphical" structure of the data mod:
12 // ---------------------------------------------------------------------------------
14 //data_records (UL,pk->id, fk->data) data_fields (pk->id, fk->data)
17 // ----------------------------------------------------------------------------- |
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) {
45 if ($datas = get_records('data','course',$preferences->backup_course
,"id")) {
46 foreach ($datas as $data) {
47 if (function_exists('backup_mod_selected')) {
49 $backup_mod_selected = backup_mod_selected($preferences, 'data', $data->id
);
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.
63 function data_backup_one_mod($bf,$preferences,$data) {
66 if (is_numeric($data)) { // backwards compatibility
67 $data = get_record('data','id',$data);
69 $instanceid = $data->id
;
74 fwrite ($bf,start_tag("MOD",3,true));
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();
116 $status = backup_data_records($bf,$preferences,$data->id
);
118 $status = backup_data_files_instance($bf,$preferences,$data->id
); //recursive copy
121 fwrite ($bf,end_tag("MOD",3,true));
127 function backup_data_fields($bf,$preferences,$dataid){
131 $data_fields = get_records("data_fields","dataid",$dataid);
133 //If there is submissions
136 $status =fwrite ($bf,start_tag("FIELDS",4,true));
137 //Iterate over each submission
138 foreach ($data_fields as $fie_sub) {
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
));
159 $status =fwrite ($bf,end_tag("FIELD",5,true));
162 $status =fwrite ($bf,end_tag("FIELDS",4,true));
167 function backup_data_content($bf,$preferences,$recordid){
171 $data_contents = get_records("data_content","recordid",$recordid);
173 //If there is submissions
174 if ($data_contents) {
176 $status =fwrite ($bf,start_tag("CONTENTS",6,true));
177 //Iterate over each submission
178 foreach ($data_contents as $cnt_sub) {
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
));
191 $status =fwrite ($bf,end_tag("CONTENT",7,true));
194 $status =fwrite ($bf,end_tag("CONTENTS",6,true));
199 function backup_data_ratings($bf,$preferences,$recordid){
202 $data_ratings = get_records("data_ratings","recordid",$recordid);
204 //If there is submissions
207 $status =fwrite ($bf,start_tag("RATINGS",6,true));
208 //Iterate over each submission
209 foreach ($data_ratings as $rat_sub) {
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
));
218 $status =fwrite ($bf,end_tag("RATING",7,true));
221 $status =fwrite ($bf,end_tag("RATINGS",6,true));
227 function backup_data_comments($bf,$preferences,$recordid){
230 $data_comments = get_records("data_comments","recordid",$recordid);
232 //If there is submissions
233 if ($data_comments) {
235 $status =fwrite ($bf,start_tag("COMMENTS",6,true));
236 //Iterate over each submission
237 foreach ($data_comments as $com_sub) {
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
));
248 $status =fwrite ($bf,end_tag("COMMENT",7,true));
251 $status =fwrite ($bf,end_tag("COMMENTS",6,true));
257 function backup_data_files_instance($bf,$preferences,$instanceid) {
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
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);
277 function backup_data_records($bf,$preferences,$dataid){
282 $data_records = get_records("data_records","dataid",$dataid);
283 //If there is submissions
286 $status =fwrite ($bf,start_tag("RECORDS",4,true));
287 //Iterate over each submission
288 foreach ($data_records as $rec_sub) {
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
));
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));
308 $status =fwrite ($bf,end_tag("RECORDS",4,true));
314 function backup_data_files($bf,$preferences) {
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
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");
335 function backup_data_file_instance($bf,$preferences,$instanceid) {
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
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);
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
364 function data_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
365 if (!empty($instances) && is_array($instances) && count($instances)) {
367 foreach ($instances as $id => $instance) {
368 $info +
= data_check_backup_mods_instances($instance,$backup_unique_code);
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);
382 //Now, if requested, the user_data
384 // any other needed stuff
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) {
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);
423 function data_ids($course) {
424 // stub function, return number of modules