MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / glossary / backuplib.php
blob2f0c0b3f9fda4a714fe132d1f4b6b4362b8e2fd6
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("ALLOWDUPLICATEDENTRIES",4,false,$glossary->allowduplicatedentries));
67 fwrite ($bf,full_tag("DISPLAYFORMAT",4,false,$glossary->displayformat));
68 fwrite ($bf,full_tag("MAINGLOSSARY",4,false,$glossary->mainglossary));
69 fwrite ($bf,full_tag("SHOWSPECIAL",4,false,$glossary->showspecial));
70 fwrite ($bf,full_tag("SHOWALPHABET",4,false,$glossary->showalphabet));
71 fwrite ($bf,full_tag("SHOWALL",4,false,$glossary->showall));
72 fwrite ($bf,full_tag("ALLOWCOMMENTS",4,false,$glossary->allowcomments));
73 fwrite ($bf,full_tag("ALLOWPRINTVIEW",4,false,$glossary->allowprintview));
74 fwrite ($bf,full_tag("USEDYNALINK",4,false,$glossary->usedynalink));
75 fwrite ($bf,full_tag("DEFAULTAPPROVAL",4,false,$glossary->defaultapproval));
76 fwrite ($bf,full_tag("GLOBALGLOSSARY",4,false,$glossary->globalglossary));
77 fwrite ($bf,full_tag("ENTBYPAGE",4,false,$glossary->entbypage));
78 fwrite ($bf,full_tag("EDITALWAYS",4,false,$glossary->editalways));
79 fwrite ($bf,full_tag("RSSTYPE",4,false,$glossary->rsstype));
80 fwrite ($bf,full_tag("RSSARTICLES",4,false,$glossary->rssarticles));
81 fwrite ($bf,full_tag("TIMECREATED",4,false,$glossary->timecreated));
82 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$glossary->timemodified));
83 fwrite ($bf,full_tag("ASSESSED",4,false,$glossary->assessed));
84 fwrite ($bf,full_tag("ASSESSTIMESTART",4,false,$glossary->assesstimestart));
85 fwrite ($bf,full_tag("ASSESSTIMEFINISH",4,false,$glossary->assesstimefinish));
86 fwrite ($bf,full_tag("SCALE",4,false,$glossary->scale));
88 //Only if preferences->backup_users != 2 (none users). Else, teachers entries will be included.
89 if ($preferences->backup_users != 2) {
90 backup_glossary_entries($bf,$preferences,$glossary->id, $preferences->mods["glossary"]->userinfo);
93 backup_glossary_categories($bf,$preferences,$glossary->id, $preferences->mods["glossary"]->userinfo);
95 //End mod
96 $status =fwrite ($bf,end_tag("MOD",3,true));
98 return $status;
101 //Backup glossary_categories and entries_categories contents (executed from glossary_backup_mods)
102 function backup_glossary_categories ($bf,$preferences,$glossary, $userinfo) {
104 global $CFG;
106 $status = true;
108 $glossary_categories = get_records("glossary_categories","glossaryid",$glossary,"id");
109 //If there is categories
110 if ($glossary_categories) {
111 $status =fwrite ($bf,start_tag("CATEGORIES",4,true));
113 //Iterate over each category
114 foreach ($glossary_categories as $glo_cat) {
115 //Start category
116 //Print category contents
117 $status =fwrite ($bf,start_tag("CATEGORY",5,true));
119 fwrite ($bf,full_tag("ID",6,false,$glo_cat->id));
120 fwrite ($bf,full_tag("GLOSSARYID",6,false,$glo_cat->glossaryid));
121 fwrite ($bf,full_tag("NAME",6,false,$glo_cat->name));
122 fwrite ($bf,full_tag("USEDYNALINK",6,false,$glo_cat->usedynalink));
124 //Only if preferences->backup_users != 2 (none users). Else, teachers entries will be included.
125 if ($preferences->backup_users != 2) {
126 $status = backup_glossary_entries_categories ($bf,$preferences,$glo_cat->id);
129 $status =fwrite ($bf,end_tag("CATEGORY",5,true));
132 //Write end tag
133 $status =fwrite ($bf,end_tag("CATEGORIES",4,true));
135 return $status;
138 //Backup entries_categories contents (executed from backup_glossary_categories)
139 function backup_glossary_entries_categories ($bf,$preferences,$categoryid) {
141 global $CFG;
143 $status = true;
145 $entries = get_records("glossary_entries_categories","categoryid",$categoryid);
146 if ($entries) {
147 $status =fwrite ($bf,start_tag("ENTRIES",6,true));
148 foreach ($entries as $entry) {
149 fwrite ($bf,start_tag("ENTRY",7,true));
150 fwrite ($bf,full_tag("ENTRYID",8,false,$entry->entryid));
151 $status =fwrite ($bf,end_tag("ENTRY",7,true));
153 $status =fwrite ($bf,end_tag("ENTRIES",6,true));
155 return $status;
158 //Backup glossary_entries contents (executed from glossary_backup_mods)
159 function backup_glossary_entries ($bf,$preferences,$glossary, $userinfo) {
161 global $CFG;
163 $status = true;
165 $glossary_entries = get_records("glossary_entries","glossaryid",$glossary,"id");
166 //If there is entries
167 if ($glossary_entries) {
168 $dumped_entries = 0;
170 //Iterate over each entry
171 foreach ($glossary_entries as $glo_ent) {
172 //Start entry
173 //Print submission contents
174 if ($glo_ent->teacherentry or $userinfo) {
175 $dumped_entries++;
176 if ($dumped_entries == 1) {
177 //Write start tag
178 $status =fwrite ($bf,start_tag("ENTRIES",4,true));
180 $status =fwrite ($bf,start_tag("ENTRY",5,true));
182 fwrite ($bf,full_tag("ID",6,false,$glo_ent->id));
183 fwrite ($bf,full_tag("USERID",6,false,$glo_ent->userid));
184 fwrite ($bf,full_tag("CONCEPT",6,false,trim($glo_ent->concept)));
185 fwrite ($bf,full_tag("DEFINITION",6,false,$glo_ent->definition));
186 fwrite ($bf,full_tag("FORMAT",6,false,$glo_ent->format));
187 fwrite ($bf,full_tag("ATTACHMENT",6,false,$glo_ent->attachment));
188 fwrite ($bf,full_tag("SOURCEGLOSSARYID",6,false,$glo_ent->sourceglossaryid));
189 fwrite ($bf,full_tag("USEDYNALINK",6,false,$glo_ent->usedynalink));
190 fwrite ($bf,full_tag("CASESENSITIVE",6,false,$glo_ent->casesensitive));
191 fwrite ($bf,full_tag("FULLMATCH",6,false,$glo_ent->fullmatch));
192 fwrite ($bf,full_tag("APPROVED",6,false,$glo_ent->approved));
193 fwrite ($bf,full_tag("TIMECREATED",6,false,$glo_ent->timecreated));
194 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$glo_ent->timemodified));
195 fwrite ($bf,full_tag("TEACHERENTRY",6,false,$glo_ent->teacherentry));
197 $status = backup_glossary_aliases ($bf,$preferences,$glo_ent->id);
199 if ( $userinfo ) {
200 $status = backup_glossary_comments ($bf,$preferences,$glo_ent->id);
201 $status = backup_glossary_ratings ($bf,$preferences,$glo_ent->id);
204 $status =fwrite ($bf,end_tag("ENTRY",5,true));
206 //Now include entry attachment in backup (if it exists)
207 if ($glo_ent->attachment) {
208 $status = backup_glossary_files($bf,$preferences,$glossary,$glo_ent->id);
212 if ( $dumped_entries > 0 ) {
213 //Write end tag
214 $status =fwrite ($bf,end_tag("ENTRIES",4,true));
217 return $status;
220 //Backup glossary_comments contents (executed from backup_glossary_entries)
221 function backup_glossary_comments ($bf,$preferences,$entryid) {
223 global $CFG;
225 $status = true;
227 $comments = get_records("glossary_comments","entryid",$entryid);
228 if ($comments) {
229 $status =fwrite ($bf,start_tag("COMMENTS",6,true));
230 foreach ($comments as $comment) {
231 $status =fwrite ($bf,start_tag("COMMENT",7,true));
233 fwrite ($bf,full_tag("ID",8,false,$comment->id));
234 fwrite ($bf,full_tag("USERID",8,false,$comment->userid));
235 fwrite ($bf,full_tag("ENTRYCOMMENT",8,false,$comment->entrycomment));
236 fwrite ($bf,full_tag("FORMAT",8,false,$comment->format));
237 fwrite ($bf,full_tag("TIMEMODIFIED",8,false,$comment->timemodified));
239 $status =fwrite ($bf,end_tag("COMMENT",7,true));
241 $status =fwrite ($bf,end_tag("COMMENTS",6,true));
243 return $status;
246 //Backup glossary_ratings contents (executed from backup_glossary_entries)
247 function backup_glossary_ratings ($bf,$preferences,$entryid) {
249 global $CFG;
251 $status = true;
253 $ratings = get_records("glossary_ratings","entryid",$entryid);
254 if ($ratings) {
255 $status =fwrite ($bf,start_tag("RATINGS",6,true));
256 foreach ($ratings as $rating) {
257 $status =fwrite ($bf,start_tag("RATING",7,true));
259 fwrite ($bf,full_tag("ID",8,false,$rating->id));
260 fwrite ($bf,full_tag("USERID",8,false,$rating->userid));
261 fwrite ($bf,full_tag("TIME",8,false,$rating->time));
262 fwrite ($bf,full_tag("RATING",8,false,$rating->rating));
264 $status =fwrite ($bf,end_tag("RATING",7,true));
266 $status =fwrite ($bf,end_tag("RATINGS",6,true));
268 return $status;
271 //Backup glossary_alias contents (executed from backup_glossary_entries)
272 function backup_glossary_aliases ($bf,$preferences,$entryid) {
274 global $CFG;
276 $status = true;
278 $aliases = get_records("glossary_alias","entryid",$entryid);
279 if ($aliases) {
280 $status =fwrite ($bf,start_tag("ALIASES",6,true));
281 foreach ($aliases as $alias) {
282 $status =fwrite ($bf,start_tag("ALIAS",7,true));
284 fwrite ($bf,full_tag("ALIAS_TEXT",8,false,trim($alias->alias)));
286 $status =fwrite ($bf,end_tag("ALIAS",7,true));
288 $status =fwrite ($bf,end_tag("ALIASES",6,true));
290 return $status;
293 //Backup glossary files because we've selected to backup user info
294 //or current entry is a teacher entry
295 function backup_glossary_files($bf,$preferences,$glossary,$entry) {
297 global $CFG;
299 $status = true;
301 //First we check to moddata exists and create it as necessary
302 //in temp/backup/$backup_code dir
303 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
305 //Now we check that moddata/glossary dir exists and create it as necessary
306 //in temp/backup/$backup_code/moddata dir
307 $glo_dir_to = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code.
308 "/".$CFG->moddata."/glossary";
309 //Let's create it as necessary
310 $status = check_dir_exists($glo_dir_to,true);
312 //Now we check that the moddata/glossary/$glossary dir exists and create it as necessary
313 //in temp/backup/$backup_code/moddata/glossary
314 $status = check_dir_exists($glo_dir_to."/".$glossary,true);
316 //Now copy the moddata/glossary/$glossary/$entry to
317 //temp/backup/$backup_code/moddata/glossary/$glossary/$entry
318 if ($status) {
319 //Calculate moddata/glossary dir
320 $glo_dir_from = $CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/glossary";
321 //Only if it exists !!
322 if (is_dir($glo_dir_from."/".$glossary."/".$entry)) {
323 $status = backup_copy_file($glo_dir_from."/".$glossary."/".$entry,
324 $glo_dir_to."/".$glossary."/".$entry);
328 return $status;
332 ////Return an array of info (name,value)
333 function glossary_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
334 if (!empty($instances) && is_array($instances) && count($instances)) {
335 $info = array();
336 foreach ($instances as $id => $instance) {
337 $info += glossary_check_backup_mods_instances($instance,$backup_unique_code);
339 return $info;
341 //First the course data
342 $info[0][0] = get_string("modulenameplural","glossary");
343 if ($ids = glossary_ids ($course)) {
344 $info[0][1] = count($ids);
345 } else {
346 $info[0][1] = 0;
349 //Now, if requested, the user_data
350 if ($user_data) {
351 $info[1][0] = get_string("concepts","glossary");
352 if ($ids = glossary_entries_ids_by_course ($course)) {
353 $info[1][1] = count($ids);
354 } else {
355 $info[1][1] = 0;
358 return $info;
361 ////Return an array of info (name,value)
362 function glossary_check_backup_mods_instances($instance,$backup_unique_code) {
363 //First the course data
364 $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
365 $info[$instance->id.'0'][1] = '';
367 //Now, if requested, the user_data
368 if (!empty($instance->userdata)) {
369 $info[$instance->id.'1'][0] = get_string("concepts","glossary");
370 if ($ids = glossary_entries_ids_by_instance ($instance->id)) {
371 $info[$instance->id.'1'][1] = count($ids);
372 } else {
373 $info[$instance->id.'1'][1] = 0;
376 return $info;
379 //Return a content encoded to support interactivities linking. Every module
380 //should have its own. They are called automatically from the backup procedure.
381 function glossary_encode_content_links ($content,$preferences) {
383 global $CFG;
385 $base = preg_quote($CFG->wwwroot,"/");
387 //Link to the list of glossarys
388 $buscar="/(".$base."\/mod\/glossary\/index.php\?id\=)([0-9]+)/";
389 $result= preg_replace($buscar,'$@GLOSSARYINDEX*$2@$',$content);
391 //Link to glossary view by moduleid
392 $buscar="/(".$base."\/mod\/glossary\/view.php\?id\=)([0-9]+)/";
393 $result= preg_replace($buscar,'$@GLOSSARYVIEWBYID*$2@$',$result);
395 return $result;
398 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
400 //Returns an array of glossaries id
401 function glossary_ids ($course) {
403 global $CFG;
405 return get_records_sql ("SELECT a.id, a.course
406 FROM {$CFG->prefix}glossary a
407 WHERE a.course = '$course'");
410 //Returns an array of glossary_answers id
411 function glossary_entries_ids_by_course ($course) {
413 global $CFG;
415 return get_records_sql ("SELECT s.id , s.glossaryid
416 FROM {$CFG->prefix}glossary_entries s,
417 {$CFG->prefix}glossary a
418 WHERE a.course = '$course' AND
419 s.glossaryid = a.id");
422 //Returns an array of glossary_answers id
423 function glossary_entries_ids_by_instance ($instanceid) {
425 global $CFG;
427 return get_records_sql ("SELECT s.id , s.glossaryid
428 FROM {$CFG->prefix}glossary_entries s
429 WHERE s.glossaryid = $instanceid");