Automatic installer.php lang files by installer_builder (20070313)
[moodle.git] / mod / glossary / backuplib.php
blob1b1d12726a5d3e6f69c8978edb59889574148460
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //glossary mods
5 //This is the "graphical" structure of the glossary mod:
6 //
7 // glossary ----------------------------------------- glossary_categories
8 // (CL,pk->id) (CL,pk->id,fk->glossaryid)
9 // | |
10 // | |
11 // | |
12 // glossary_entries --------------------------------glossary_entries_categories
13 // (UL,pk->id, fk->glossaryid, files) | (UL, pk->categoryid,entryid)
14 // | |
15 // | |--------------------glossary_ratings
16 // | | (UL, pk->id, pk->entryid)
17 // glossary_comments |
18 // (UL,pk->id, fk->entryid) |---------------------glossary_alias
19 // (UL, pk->id, pk->entryid)
22 // Meaning: pk->primary key field of the table
23 // fk->foreign key to link with parent
24 // nt->nested field (recursive data)
25 // CL->course level info
26 // UL->user level info
27 // files->table may have files)
29 //----------------------------------------------------------------------------------
31 function glossary_backup_mods($bf,$preferences) {
33 global $CFG;
35 $status = true;
37 //Iterate over glossary table
38 $glossaries = get_records ("glossary","course",$preferences->backup_course,"mainglossary");
39 if ($glossaries) {
40 foreach ($glossaries as $glossary) {
41 if (backup_mod_selected($preferences,'glossary',$glossary->id)) {
42 $status = glossary_backup_one_mod($bf,$preferences,$glossary);
46 return $status;
49 function glossary_backup_one_mod($bf,$preferences,$glossary) {
51 global $CFG;
53 if (is_numeric($glossary)) {
54 $glossary = get_record('glossary','id',$glossary);
57 $status = true;
59 //Start mod
60 fwrite ($bf,start_tag("MOD",3,true));
61 //Print glossary data
62 fwrite ($bf,full_tag("ID",4,false,$glossary->id));
63 fwrite ($bf,full_tag("MODTYPE",4,false,"glossary"));
64 fwrite ($bf,full_tag("NAME",4,false,$glossary->name));
65 fwrite ($bf,full_tag("INTRO",4,false,$glossary->intro));
66 fwrite ($bf,full_tag("STUDENTCANPOST",4,false,$glossary->studentcanpost));
67 fwrite ($bf,full_tag("ALLOWDUPLICATEDENTRIES",4,false,$glossary->allowduplicatedentries));
68 fwrite ($bf,full_tag("DISPLAYFORMAT",4,false,$glossary->displayformat));
69 fwrite ($bf,full_tag("MAINGLOSSARY",4,false,$glossary->mainglossary));
70 fwrite ($bf,full_tag("SHOWSPECIAL",4,false,$glossary->showspecial));
71 fwrite ($bf,full_tag("SHOWALPHABET",4,false,$glossary->showalphabet));
72 fwrite ($bf,full_tag("SHOWALL",4,false,$glossary->showall));
73 fwrite ($bf,full_tag("ALLOWCOMMENTS",4,false,$glossary->allowcomments));
74 fwrite ($bf,full_tag("ALLOWPRINTVIEW",4,false,$glossary->allowprintview));
75 fwrite ($bf,full_tag("USEDYNALINK",4,false,$glossary->usedynalink));
76 fwrite ($bf,full_tag("DEFAULTAPPROVAL",4,false,$glossary->defaultapproval));
77 fwrite ($bf,full_tag("GLOBALGLOSSARY",4,false,$glossary->globalglossary));
78 fwrite ($bf,full_tag("ENTBYPAGE",4,false,$glossary->entbypage));
79 fwrite ($bf,full_tag("EDITALWAYS",4,false,$glossary->editalways));
80 fwrite ($bf,full_tag("RSSTYPE",4,false,$glossary->rsstype));
81 fwrite ($bf,full_tag("RSSARTICLES",4,false,$glossary->rssarticles));
82 fwrite ($bf,full_tag("TIMECREATED",4,false,$glossary->timecreated));
83 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$glossary->timemodified));
84 fwrite ($bf,full_tag("ASSESSED",4,false,$glossary->assessed));
85 fwrite ($bf,full_tag("ASSESSTIMESTART",4,false,$glossary->assesstimestart));
86 fwrite ($bf,full_tag("ASSESSTIMEFINISH",4,false,$glossary->assesstimefinish));
87 fwrite ($bf,full_tag("SCALE",4,false,$glossary->scale));
89 //Only if preferences->backup_users != 2 (none users). Else, teachers entries will be included.
90 if ($preferences->backup_users != 2) {
91 backup_glossary_entries($bf,$preferences,$glossary->id, $preferences->mods["glossary"]->userinfo);
94 backup_glossary_categories($bf,$preferences,$glossary->id, $preferences->mods["glossary"]->userinfo);
96 //End mod
97 $status =fwrite ($bf,end_tag("MOD",3,true));
99 return $status;
102 //Backup glossary_categories and entries_categories contents (executed from glossary_backup_mods)
103 function backup_glossary_categories ($bf,$preferences,$glossary, $userinfo) {
105 global $CFG;
107 $status = true;
109 $glossary_categories = get_records("glossary_categories","glossaryid",$glossary,"id");
110 //If there is categories
111 if ($glossary_categories) {
112 $status =fwrite ($bf,start_tag("CATEGORIES",4,true));
114 //Iterate over each category
115 foreach ($glossary_categories as $glo_cat) {
116 //Start category
117 //Print category contents
118 $status =fwrite ($bf,start_tag("CATEGORY",5,true));
120 fwrite ($bf,full_tag("ID",6,false,$glo_cat->id));
121 fwrite ($bf,full_tag("GLOSSARYID",6,false,$glo_cat->glossaryid));
122 fwrite ($bf,full_tag("NAME",6,false,$glo_cat->name));
123 fwrite ($bf,full_tag("USEDYNALINK",6,false,$glo_cat->usedynalink));
125 //Only if preferences->backup_users != 2 (none users). Else, teachers entries will be included.
126 if ($preferences->backup_users != 2) {
127 $status = backup_glossary_entries_categories ($bf,$preferences,$glo_cat->id);
130 $status =fwrite ($bf,end_tag("CATEGORY",5,true));
133 //Write end tag
134 $status =fwrite ($bf,end_tag("CATEGORIES",4,true));
136 return $status;
139 //Backup entries_categories contents (executed from backup_glossary_categories)
140 function backup_glossary_entries_categories ($bf,$preferences,$categoryid) {
142 global $CFG;
144 $status = true;
146 $entries = get_records("glossary_entries_categories","categoryid",$categoryid);
147 if ($entries) {
148 $status =fwrite ($bf,start_tag("ENTRIES",6,true));
149 foreach ($entries as $entry) {
150 fwrite ($bf,start_tag("ENTRY",7,true));
151 fwrite ($bf,full_tag("ENTRYID",8,false,$entry->entryid));
152 $status =fwrite ($bf,end_tag("ENTRY",7,true));
154 $status =fwrite ($bf,end_tag("ENTRIES",6,true));
156 return $status;
159 //Backup glossary_entries contents (executed from glossary_backup_mods)
160 function backup_glossary_entries ($bf,$preferences,$glossary, $userinfo) {
162 global $CFG;
164 $status = true;
166 $glossary_entries = get_records("glossary_entries","glossaryid",$glossary,"id");
167 //If there is entries
168 if ($glossary_entries) {
169 $dumped_entries = 0;
171 //Iterate over each entry
172 foreach ($glossary_entries as $glo_ent) {
173 //Start entry
174 //Print submission contents
175 if ($glo_ent->teacherentry or $userinfo) {
176 $dumped_entries++;
177 if ($dumped_entries == 1) {
178 //Write start tag
179 $status =fwrite ($bf,start_tag("ENTRIES",4,true));
181 $status =fwrite ($bf,start_tag("ENTRY",5,true));
183 fwrite ($bf,full_tag("ID",6,false,$glo_ent->id));
184 fwrite ($bf,full_tag("USERID",6,false,$glo_ent->userid));
185 fwrite ($bf,full_tag("CONCEPT",6,false,trim($glo_ent->concept)));
186 fwrite ($bf,full_tag("DEFINITION",6,false,$glo_ent->definition));
187 fwrite ($bf,full_tag("FORMAT",6,false,$glo_ent->format));
188 fwrite ($bf,full_tag("ATTACHMENT",6,false,$glo_ent->attachment));
189 fwrite ($bf,full_tag("SOURCEGLOSSARYID",6,false,$glo_ent->sourceglossaryid));
190 fwrite ($bf,full_tag("USEDYNALINK",6,false,$glo_ent->usedynalink));
191 fwrite ($bf,full_tag("CASESENSITIVE",6,false,$glo_ent->casesensitive));
192 fwrite ($bf,full_tag("FULLMATCH",6,false,$glo_ent->fullmatch));
193 fwrite ($bf,full_tag("APPROVED",6,false,$glo_ent->approved));
194 fwrite ($bf,full_tag("TIMECREATED",6,false,$glo_ent->timecreated));
195 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$glo_ent->timemodified));
196 fwrite ($bf,full_tag("TEACHERENTRY",6,false,$glo_ent->teacherentry));
198 $status = backup_glossary_aliases ($bf,$preferences,$glo_ent->id);
200 if ( $userinfo ) {
201 $status = backup_glossary_comments ($bf,$preferences,$glo_ent->id);
202 $status = backup_glossary_ratings ($bf,$preferences,$glo_ent->id);
205 $status =fwrite ($bf,end_tag("ENTRY",5,true));
207 //Now include entry attachment in backup (if it exists)
208 if ($glo_ent->attachment) {
209 $status = backup_glossary_files($bf,$preferences,$glossary,$glo_ent->id);
213 if ( $dumped_entries > 0 ) {
214 //Write end tag
215 $status =fwrite ($bf,end_tag("ENTRIES",4,true));
218 return $status;
221 //Backup glossary_comments contents (executed from backup_glossary_entries)
222 function backup_glossary_comments ($bf,$preferences,$entryid) {
224 global $CFG;
226 $status = true;
228 $comments = get_records("glossary_comments","entryid",$entryid);
229 if ($comments) {
230 $status =fwrite ($bf,start_tag("COMMENTS",6,true));
231 foreach ($comments as $comment) {
232 $status =fwrite ($bf,start_tag("COMMENT",7,true));
234 fwrite ($bf,full_tag("ID",8,false,$comment->id));
235 fwrite ($bf,full_tag("USERID",8,false,$comment->userid));
236 fwrite ($bf,full_tag("COMMENT",8,false,$comment->comment));
237 fwrite ($bf,full_tag("FORMAT",8,false,$comment->format));
238 fwrite ($bf,full_tag("TIMEMODIFIED",8,false,$comment->timemodified));
240 $status =fwrite ($bf,end_tag("COMMENT",7,true));
242 $status =fwrite ($bf,end_tag("COMMENTS",6,true));
244 return $status;
247 //Backup glossary_ratings contents (executed from backup_glossary_entries)
248 function backup_glossary_ratings ($bf,$preferences,$entryid) {
250 global $CFG;
252 $status = true;
254 $ratings = get_records("glossary_ratings","entryid",$entryid);
255 if ($ratings) {
256 $status =fwrite ($bf,start_tag("RATINGS",6,true));
257 foreach ($ratings as $rating) {
258 $status =fwrite ($bf,start_tag("RATING",7,true));
260 fwrite ($bf,full_tag("ID",8,false,$rating->id));
261 fwrite ($bf,full_tag("USERID",8,false,$rating->userid));
262 fwrite ($bf,full_tag("TIME",8,false,$rating->time));
263 fwrite ($bf,full_tag("RATING",8,false,$rating->rating));
265 $status =fwrite ($bf,end_tag("RATING",7,true));
267 $status =fwrite ($bf,end_tag("RATINGS",6,true));
269 return $status;
272 //Backup glossary_alias contents (executed from backup_glossary_entries)
273 function backup_glossary_aliases ($bf,$preferences,$entryid) {
275 global $CFG;
277 $status = true;
279 $aliases = get_records("glossary_alias","entryid",$entryid);
280 if ($aliases) {
281 $status =fwrite ($bf,start_tag("ALIASES",6,true));
282 foreach ($aliases as $alias) {
283 $status =fwrite ($bf,start_tag("ALIAS",7,true));
285 fwrite ($bf,full_tag("ALIAS_TEXT",8,false,trim($alias->alias)));
287 $status =fwrite ($bf,end_tag("ALIAS",7,true));
289 $status =fwrite ($bf,end_tag("ALIASES",6,true));
291 return $status;
294 //Backup glossary files because we've selected to backup user info
295 //or current entry is a teacher entry
296 function backup_glossary_files($bf,$preferences,$glossary,$entry) {
298 global $CFG;
300 $status = true;
302 //First we check to moddata exists and create it as necessary
303 //in temp/backup/$backup_code dir
304 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
306 //Now we check that moddata/glossary dir exists and create it as necessary
307 //in temp/backup/$backup_code/moddata dir
308 $glo_dir_to = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code.
309 "/".$CFG->moddata."/glossary";
310 //Let's create it as necessary
311 $status = check_dir_exists($glo_dir_to,true);
313 //Now we check that the moddata/glossary/$glossary dir exists and create it as necessary
314 //in temp/backup/$backup_code/moddata/glossary
315 $status = check_dir_exists($glo_dir_to."/".$glossary,true);
317 //Now copy the moddata/glossary/$glossary/$entry to
318 //temp/backup/$backup_code/moddata/glossary/$glossary/$entry
319 if ($status) {
320 //Calculate moddata/glossary dir
321 $glo_dir_from = $CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/glossary";
322 //Only if it exists !!
323 if (is_dir($glo_dir_from."/".$glossary."/".$entry)) {
324 $status = backup_copy_file($glo_dir_from."/".$glossary."/".$entry,
325 $glo_dir_to."/".$glossary."/".$entry);
329 return $status;
333 ////Return an array of info (name,value)
334 function glossary_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
335 if (!empty($instances) && is_array($instances) && count($instances)) {
336 $info = array();
337 foreach ($instances as $id => $instance) {
338 $info += glossary_check_backup_mods_instances($instance,$backup_unique_code);
340 return $info;
342 //First the course data
343 $info[0][0] = get_string("modulenameplural","glossary");
344 if ($ids = glossary_ids ($course)) {
345 $info[0][1] = count($ids);
346 } else {
347 $info[0][1] = 0;
350 //Now, if requested, the user_data
351 if ($user_data) {
352 $info[1][0] = get_string("concepts","glossary");
353 if ($ids = glossary_entries_ids_by_course ($course)) {
354 $info[1][1] = count($ids);
355 } else {
356 $info[1][1] = 0;
359 return $info;
362 ////Return an array of info (name,value)
363 function glossary_check_backup_mods_instances($instance,$backup_unique_code) {
364 //First the course data
365 $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
366 $info[$instance->id.'0'][1] = '';
368 //Now, if requested, the user_data
369 if (!empty($instance->userdata)) {
370 $info[$instance->id.'1'][0] = get_string("concepts","glossary");
371 if ($ids = glossary_entries_ids_by_instance ($instance->id)) {
372 $info[$instance->id.'1'][1] = count($ids);
373 } else {
374 $info[$instance->id.'1'][1] = 0;
377 return $info;
380 //Return a content encoded to support interactivities linking. Every module
381 //should have its own. They are called automatically from the backup procedure.
382 function glossary_encode_content_links ($content,$preferences) {
384 global $CFG;
386 $base = preg_quote($CFG->wwwroot,"/");
388 //Link to the list of glossarys
389 $buscar="/(".$base."\/mod\/glossary\/index.php\?id\=)([0-9]+)/";
390 $result= preg_replace($buscar,'$@GLOSSARYINDEX*$2@$',$content);
392 //Link to glossary view by moduleid
393 $buscar="/(".$base."\/mod\/glossary\/view.php\?id\=)([0-9]+)/";
394 $result= preg_replace($buscar,'$@GLOSSARYVIEWBYID*$2@$',$result);
396 return $result;
399 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
401 //Returns an array of glossaries id
402 function glossary_ids ($course) {
404 global $CFG;
406 return get_records_sql ("SELECT a.id, a.course
407 FROM {$CFG->prefix}glossary a
408 WHERE a.course = '$course'");
411 //Returns an array of glossary_answers id
412 function glossary_entries_ids_by_course ($course) {
414 global $CFG;
416 return get_records_sql ("SELECT s.id , s.glossaryid
417 FROM {$CFG->prefix}glossary_entries s,
418 {$CFG->prefix}glossary a
419 WHERE a.course = '$course' AND
420 s.glossaryid = a.id");
423 //Returns an array of glossary_answers id
424 function glossary_entries_ids_by_instance ($instanceid) {
426 global $CFG;
428 return get_records_sql ("SELECT s.id , s.glossaryid
429 FROM {$CFG->prefix}glossary_entries s
430 WHERE s.glossaryid = $instanceid");