adding some strings
[moodle-linuxchix.git] / mod / resource / backuplib.php
blobd7866e677b82df86d94a608c8cf7e8826a689493
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //resource mods
5 //This is the "graphical" structure of the resource mod:
6 //
7 // resource
8 // (CL,pk->id,files)
9 //
10 // Meaning: pk->primary key field of the table
11 // fk->foreign key to link with parent
12 // nt->nested field (recursive data)
13 // CL->course level info
14 // UL->user level info
15 // files->table may have files)
17 //-----------------------------------------------------------
19 //This function executes all the backup procedure about this mod
20 function resource_backup_mods($bf,$preferences) {
21 global $CFG;
23 $status = true;
25 ////Iterate over resource table
26 $resources = get_records ("resource","course",$preferences->backup_course,"id");
27 if ($resources) {
28 foreach ($resources as $resource) {
29 if (backup_mod_selected($preferences,'resource',$resource->id)) {
30 $status = resource_backup_one_mod($bf,$preferences,$resource);
34 return $status;
37 function resource_backup_one_mod($bf,$preferences,$resource) {
39 global $CFG;
41 if (is_numeric($resource)) {
42 $resource = get_record('resource','id',$resource);
45 $status = true;
47 //Start mod
48 fwrite ($bf,start_tag("MOD",3,true));
49 //Print assignment data
50 fwrite ($bf,full_tag("ID",4,false,$resource->id));
51 fwrite ($bf,full_tag("MODTYPE",4,false,"resource"));
52 fwrite ($bf,full_tag("NAME",4,false,$resource->name));
53 fwrite ($bf,full_tag("TYPE",4,false,$resource->type));
54 fwrite ($bf,full_tag("REFERENCE",4,false,$resource->reference));
55 fwrite ($bf,full_tag("SUMMARY",4,false,$resource->summary));
56 fwrite ($bf,full_tag("ALLTEXT",4,false,$resource->alltext));
57 fwrite ($bf,full_tag("POPUP",4,false,$resource->popup));
58 fwrite ($bf,full_tag("OPTIONS",4,false,$resource->options));
59 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$resource->timemodified));
60 //End mod
61 $status = fwrite ($bf,end_tag("MOD",3,true));
63 if ($status && ($resource->type == 'file' || $resource->type == 'directory' || $resource->type == 'ims')) { // more should go here later!
64 // backup files for this resource.
65 $status = resource_backup_files($bf,$preferences,$resource);
68 return $status;
71 ////Return an array of info (name,value)
72 function resource_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
73 if (!empty($instances) && is_array($instances) && count($instances)) {
74 $info = array();
75 foreach ($instances as $id => $instance) {
76 $info += resource_check_backup_mods_instances($instance,$backup_unique_code);
78 return $info;
80 //First the course data
81 $info[0][0] = get_string("modulenameplural","resource");
82 if ($ids = resource_ids ($course)) {
83 $info[0][1] = count($ids);
84 } else {
85 $info[0][1] = 0;
88 return $info;
91 ////Return an array of info (name,value)
92 function resource_check_backup_mods_instances($instance,$backup_unique_code) {
93 //First the course data
94 $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
95 $info[$instance->id.'0'][1] = '';
97 return $info;
100 //Return a content encoded to support interactivities linking. Every module
101 //should have its own. They are called automatically from the backup procedure.
102 function resource_encode_content_links ($content,$preferences) {
104 global $CFG;
106 $base = preg_quote($CFG->wwwroot,"/");
108 //Link to the list of resources
109 $buscar="/(".$base."\/mod\/resource\/index.php\?id\=)([0-9]+)/";
110 $result= preg_replace($buscar,'$@RESOURCEINDEX*$2@$',$content);
112 //Link to resource view by moduleid
113 $buscar="/(".$base."\/mod\/resource\/view.php\?id\=)([0-9]+)/";
114 $result= preg_replace($buscar,'$@RESOURCEVIEWBYID*$2@$',$result);
116 //Link to resource view by resourceid
117 $buscar="/(".$base."\/mod\/resource\/view.php\?r\=)([0-9]+)/";
118 $result= preg_replace($buscar,'$@RESOURCEVIEWBYR*$2@$',$result);
120 return $result;
123 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
125 //Returns an array of resources id
126 function resource_ids ($course) {
128 global $CFG;
130 return get_records_sql ("SELECT a.id, a.course
131 FROM {$CFG->prefix}resource a
132 WHERE a.course = '$course'");
135 function resource_backup_files($bf,$preferences,$resource) {
136 global $CFG;
137 $status = true;
139 if (!file_exists($CFG->dataroot.'/'.$preferences->backup_course.'/'.$resource->reference)) {
140 return true ; // doesn't exist but we don't want to halt the entire process so still return true.
143 $status = $status && check_and_create_course_files_dir($preferences->backup_unique_code);
145 // if this is somewhere deeply nested we need to do all the structure stuff first.....
146 $bits = explode('/',$resource->reference);
147 $newbit = '';
148 for ($i = 0; $i< count($bits)-1; $i++) {
149 $newbit .= $bits[$i].'/';
150 $status = $status && check_dir_exists($CFG->dataroot.'/temp/backup/'.$preferences->backup_unique_code.'/course_files/'.$newbit,true);
153 if ($resource->reference === '') {
154 $status = $status && backup_copy_course_files($preferences); // copy while ignoring backupdata and moddata!!!
155 } else if (strpos($resource->reference, 'backupdata') === 0 or strpos($resource->reference, $CFG->moddata) === 0) {
156 // no copying - these directories must not be shared anyway!
157 } else {
158 $status = $status && backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$resource->reference,
159 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/course_files/".$resource->reference);
162 // now, just in case we check moddata ( going forwards, resources should use this )
163 $status = $status && check_and_create_moddata_dir($preferences->backup_unique_code);
164 $status = $status && check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/".$CFG->moddata."/resource/",true);
166 if ($status) {
167 //Only if it exists !! Thanks to Daniel Miksik.
168 $instanceid = $resource->id;
169 if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/resource/".$instanceid)) {
170 $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/resource/".$instanceid,
171 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/resource/".$instanceid);
175 return $status;