MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / glossary / restorelib.php
blob7c93daa4d0c652c6368967ab4aa3f6d012d993bd
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 //This function executes all the restore procedure about this mod
32 function glossary_restore_mods($mod,$restore) {
34 global $CFG;
36 $status = true;
38 //Get record from backup_ids
39 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
41 if ($data) {
42 //Now get completed xmlized object
43 $info = $data->info;
44 // if necessary, write to restorelog and adjust date/time fields
45 if ($restore->course_startdateoffset) {
46 restore_log_date_changes('Glossary', $restore, $info['MOD']['#'], array('ASSESSTIMESTART', 'ASSESSTIMEFINISH'));
48 //traverse_xmlize($info); //Debug
49 //print_object ($GLOBALS['traverse_array']); //Debug
50 //$GLOBALS['traverse_array']=""; //Debug
52 //Now, build the glossary record structure
53 $glossary->course = $restore->course_id;
54 $glossary->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
55 $glossary->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
56 $glossary->allowduplicatedentries = backup_todb($info['MOD']['#']['ALLOWDUPLICATEDENTRIES']['0']['#']);
57 $glossary->displayformat = backup_todb($info['MOD']['#']['DISPLAYFORMAT']['0']['#']);
58 $glossary->mainglossary = backup_todb($info['MOD']['#']['MAINGLOSSARY']['0']['#']);
59 $glossary->showspecial = backup_todb($info['MOD']['#']['SHOWSPECIAL']['0']['#']);
60 $glossary->showalphabet = backup_todb($info['MOD']['#']['SHOWALPHABET']['0']['#']);
61 $glossary->showall = backup_todb($info['MOD']['#']['SHOWALL']['0']['#']);
62 $glossary->allowcomments = backup_todb($info['MOD']['#']['ALLOWCOMMENTS']['0']['#']);
63 $glossary->allowprintview = backup_todb($info['MOD']['#']['ALLOWPRINTVIEW']['0']['#']);
64 $glossary->usedynalink = backup_todb($info['MOD']['#']['USEDYNALINK']['0']['#']);
65 $glossary->defaultapproval = backup_todb($info['MOD']['#']['DEFAULTAPPROVAL']['0']['#']);
66 $glossary->globalglossary = backup_todb($info['MOD']['#']['GLOBALGLOSSARY']['0']['#']);
67 $glossary->entbypage = backup_todb($info['MOD']['#']['ENTBYPAGE']['0']['#']);
68 $glossary->editalways = backup_todb($info['MOD']['#']['EDITALWAYS']['0']['#']);
69 $glossary->rsstype = backup_todb($info['MOD']['#']['RSSTYPE']['0']['#']);
70 $glossary->rssarticles = backup_todb($info['MOD']['#']['RSSARTICLES']['0']['#']);
71 $glossary->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
72 $glossary->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
74 $glossary->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
75 $glossary->assesstimestart = backup_todb($info['MOD']['#']['ASSESSTIMESTART']['0']['#']);
76 $glossary->assesstimefinish = backup_todb($info['MOD']['#']['ASSESSTIMEFINISH']['0']['#']);
77 $glossary->scale = backup_todb($info['MOD']['#']['SCALE']['0']['#']);
79 //We have to recode the scale field if it's <0 (positive is a grade, not a scale)
80 if ($glossary->scale < 0) {
81 $scale = backup_getid($restore->backup_unique_code,"scale",abs($glossary->scale));
82 if ($scale) {
83 $glossary->scale = -($scale->new_id);
87 //To mantain upwards compatibility (pre 1.4) we have to check the displayformat field
88 //If it's numeric (0-6) we have to convert it to its new formatname.
89 //Define current 0-6 format names
90 $formatnames = array('dictionary','continuous','fullwithauthor','encyclopedia',
91 'faq','fullwithoutauthor','entrylist');
92 //If it's numeric, we are restoring a pre 1.4 course, do the conversion
93 if (is_numeric($glossary->displayformat)) {
94 $displayformat = 'dictionary'; //Default format
95 if ($glossary->displayformat >= 0 && $glossary->displayformat <= 6) {
96 $displayformat = $formatnames[$glossary->displayformat];
98 $glossary->displayformat = $displayformat;
101 //Now check that the displayformat exists in the server, else default to dictionary
102 $formats = get_list_of_plugins('mod/glossary/formats');
103 if (!in_array($glossary->displayformat,$formats)) {
104 $glossary->displayformat = 'dictionary';
107 //If the backup file doesn't include the editalways field, activate it
108 //in secondary glossaries (old behaviour, pre 1.4)
109 if (! isset($info['MOD']['#']['EDITALWAYS']['0']['#'])) { //It's a pre-14 backup file
110 if ($glossary->mainglossary == '0') {
111 $glossary->editalways = '1';
115 //The structure is equal to the db, so insert the glossary
116 $newid = insert_record ("glossary",$glossary);
118 //Do some output
119 if (!defined('RESTORE_SILENTLY')) {
120 echo "<li>".get_string("modulename","glossary")." \"".format_string(stripslashes($glossary->name),true)."\"</li>";
122 backup_flush(300);
124 if ($newid) {
125 //We have the newid, update backup_ids
126 backup_putid($restore->backup_unique_code,$mod->modtype,
127 $mod->id, $newid);
128 //Restore glossary_entries
129 $status = glossary_entries_restore_mods($mod->id,$newid,$info,$restore);
130 //Restore glossary_categories and glossary_category_entries
131 $status = glossary_categories_restore_mods($mod->id,$newid,$info,$restore);
132 } else {
133 $status = false;
135 } else {
136 $status = false;
139 return $status;
142 //This function restores the glossary_entries
143 function glossary_entries_restore_mods($old_glossary_id,$new_glossary_id,$info,$restore) {
145 global $CFG;
147 $status = true;
149 //Get the entries array
150 $entries = isset($info['MOD']['#']['ENTRIES']['0']['#']['ENTRY'])?$info['MOD']['#']['ENTRIES']['0']['#']['ENTRY']:array();
152 //Iterate over entries
153 for($i = 0; $i < sizeof($entries); $i++) {
154 $ent_info = $entries[$i];
155 //traverse_xmlize($ent_info); //Debug
156 //print_object ($GLOBALS['traverse_array']); //Debug
157 //$GLOBALS['traverse_array']=""; //Debug
159 //We'll need this later!!
160 $oldid = backup_todb($ent_info['#']['ID']['0']['#']);
161 $olduserid = backup_todb($ent_info['#']['USERID']['0']['#']);
163 //Now, build the GLOSSARY_ENTRIES record structure
164 $entry->glossaryid = $new_glossary_id;
165 $entry->userid = backup_todb($ent_info['#']['USERID']['0']['#']);
166 $entry->concept = backup_todb(trim($ent_info['#']['CONCEPT']['0']['#']));
167 $entry->definition = backup_todb($ent_info['#']['DEFINITION']['0']['#']);
168 $entry->format = backup_todb($ent_info['#']['FORMAT']['0']['#']);
169 $entry->attachment = backup_todb($ent_info['#']['ATTACHMENT']['0']['#']);
170 $entry->sourceglossaryid = backup_todb($ent_info['#']['SOURCEGLOSSARYID']['0']['#']);
171 $entry->usedynalink = backup_todb($ent_info['#']['USEDYNALINK']['0']['#']);
172 $entry->casesensitive = backup_todb($ent_info['#']['CASESENSITIVE']['0']['#']);
173 $entry->fullmatch = backup_todb($ent_info['#']['FULLMATCH']['0']['#']);
174 $entry->approved = backup_todb($ent_info['#']['APPROVED']['0']['#']);
175 $entry->timecreated = backup_todb($ent_info['#']['TIMECREATED']['0']['#']);
176 $entry->timemodified = backup_todb($ent_info['#']['TIMEMODIFIED']['0']['#']);
177 $entry->teacherentry = backup_todb($ent_info['#']['TEACHERENTRY']['0']['#']);
179 //We have to recode the userid field
180 $user = backup_getid($restore->backup_unique_code,"user",$entry->userid);
181 if ($user) {
182 $entry->userid = $user->new_id;
184 //We have to recode the sourceglossaryid field
185 $source = backup_getid($restore->backup_unique_code,"glossary",$entry->sourceglossaryid);
186 if ($source) {
187 $entry->sourceglossaryid = $source->new_id;
189 //If it's a teacher entry or userinfo was selected, restore the entry
190 if ($entry->teacherentry or restore_userdata_selected($restore,'glossary',$old_glossary_id)) {
191 //The structure is equal to the db, so insert the glossary_entries
192 $newid = insert_record ("glossary_entries",$entry);
194 //Do some output
195 if (($i+1) % 50 == 0) {
196 if (!defined('RESTORE_SILENTLY')) {
197 echo ".";
198 if (($i+1) % 1000 == 0) {
199 echo "<br />";
202 backup_flush(300);
204 if ($newid) {
205 //We have the newid, update backup_ids
206 backup_putid($restore->backup_unique_code,"glossary_entries",$oldid,$newid);
207 //Restore glossary_alias
208 $status = glossary_alias_restore_mods($oldid,$newid,$ent_info,$restore);
209 //Now restore glossary_comments
210 $status = glossary_comments_restore_mods($oldid,$newid,$ent_info,$restore);
211 //Now restore glossary_ratings
212 $status = glossary_ratings_restore_mods($oldid,$newid,$ent_info,$restore);
213 //Now copy moddata associated files if needed
214 if ($entry->attachment) {
215 $status = glossary_restore_files ($old_glossary_id, $new_glossary_id,
216 $oldid, $newid, $restore);
218 } else {
219 $status = false;
224 return $status;
227 //This function restores the glossary_comments
228 function glossary_comments_restore_mods($old_entry_id,$new_entry_id,$info,$restore) {
230 global $CFG;
232 $status = true;
234 //Get the comments array
235 $comments = isset($info['#']['COMMENTS']['0']['#']['COMMENT'])?$info['#']['COMMENTS']['0']['#']['COMMENT']:array();
237 //Iterate over comments
238 for($i = 0; $i < sizeof($comments); $i++) {
239 $com_info = $comments[$i];
240 //traverse_xmlize($com_info); //Debug
241 //print_object ($GLOBALS['traverse_array']); //Debug
242 //$GLOBALS['traverse_array']=""; //Debug
244 //We'll need this later!!
245 $oldid = backup_todb($com_info['#']['ID']['0']['#']);
247 //Now, build the GLOSSARY_COMMENTS record structure
248 $comment->entryid = $new_entry_id;
249 $comment->userid = backup_todb($com_info['#']['USERID']['0']['#']);
250 if (isset($com_info['#']['COMMENT']['0']['#'])) {
251 $comment->entrycomment = backup_todb($com_info['#']['COMMENT']['0']['#']);
252 } else {
253 $comment->entrycomment = backup_todb($com_info['#']['ENTRYCOMMENT']['0']['#']);
255 $comment->timemodified = backup_todb($com_info['#']['TIMEMODIFIED']['0']['#']);
256 $comment->format = backup_todb($com_info['#']['FORMAT']['0']['#']);
258 //We have to recode the userid field
259 $user = backup_getid($restore->backup_unique_code,"user",$comment->userid);
260 if ($user) {
261 $comment->userid = $user->new_id;
264 //The structure is equal to the db, so insert the glossary_comments
265 $newid = insert_record ("glossary_comments",$comment);
267 //Do some output
268 if (($i+1) % 50 == 0) {
269 if (!defined('RESTORE_SILENTLY')) {
270 echo ".";
271 if (($i+1) % 1000 == 0) {
272 echo "<br />";
275 backup_flush(300);
277 if ($newid) {
278 //We have the newid, update backup_ids
279 backup_putid($restore->backup_unique_code,"glossary_comments",$oldid,$newid);
280 } else {
281 $status = false;
285 return $status;
288 //This function restores the glossary_ratings
289 function glossary_ratings_restore_mods($old_entry_id,$new_entry_id,$info,$restore) {
291 global $CFG;
293 $status = true;
295 //Get the ratings array
296 $ratings = isset($info['#']['RATINGS']['0']['#']['RATING'])?$info['#']['RATINGS']['0']['#']['RATING']:array();
298 //Iterate over ratings
299 for($i = 0; $i < sizeof($ratings); $i++) {
300 $rat_info = $ratings[$i];
301 //traverse_xmlize($rat_info); //Debug
302 //print_object ($GLOBALS['traverse_array']); //Debug
303 //$GLOBALS['traverse_array']=""; //Debug
305 //Now, build the GLOSSARY_RATINGS record structure
306 $rating->entryid = $new_entry_id;
307 $rating->userid = backup_todb($rat_info['#']['USERID']['0']['#']);
308 $rating->time = backup_todb($rat_info['#']['TIME']['0']['#']);
309 $rating->rating = backup_todb($rat_info['#']['RATING']['0']['#']);
311 //We have to recode the userid field
312 $user = backup_getid($restore->backup_unique_code,"user",$rating->userid);
313 if ($user) {
314 $rating->userid = $user->new_id;
317 //The structure is equal to the db, so insert the glossary_ratings
318 $newid = insert_record ("glossary_ratings",$rating);
320 //Do some output
321 if (($i+1) % 50 == 0) {
322 if (!defined('RESTORE_SILENTLY')) {
323 echo ".";
324 if (($i+1) % 1000 == 0) {
325 echo "<br />";
328 backup_flush(300);
331 if (!$newid) {
332 $status = false;
336 return $status;
339 //This function restores the glossary_alias table
340 function glossary_alias_restore_mods($old_entry_id,$new_entry_id,$info,$restore) {
342 global $CFG;
344 $status = true;
346 //Get the comments array
347 $aliases = isset($info['#']['ALIASES']['0']['#']['ALIAS'])?$info['#']['ALIASES']['0']['#']['ALIAS']:array();
349 //Iterate over comments
350 for($i = 0; $i < sizeof($aliases); $i++) {
351 $alias_info = $aliases[$i];
353 //Now, build the GLOSSARY_ALIAS record structure
354 $alias->entryid = $new_entry_id;
355 $alias->alias = backup_todb(trim($alias_info['#']['ALIAS_TEXT']['0']['#']));
357 //The structure is equal to the db, so insert the glossary_comments
358 $newid = insert_record ("glossary_alias",$alias);
360 //Do some output
361 if (($i+1) % 50 == 0) {
362 if (!defined('RESTORE_SILENTLY')) {
363 echo ".";
364 if (($i+1) % 1000 == 0) {
365 echo "<br />";
368 backup_flush(300);
371 if (!$newid) {
372 $status = false;
376 return $status;
379 //This function restores the glossary_categories
380 function glossary_categories_restore_mods($old_glossary_id,$new_glossary_id,$info,$restore) {
382 global $CFG;
384 $status = true;
386 //Get the categories array
387 $categories = isset($info['MOD']['#']['CATEGORIES']['0']['#']['CATEGORY'])?$info['MOD']['#']['CATEGORIES']['0']['#']['CATEGORY']:array();
389 //Iterate over categories
390 for($i = 0; $i < sizeof($categories); $i++) {
391 $cat_info = $categories[$i];
392 //traverse_xmlize($cat_info); //Debug
393 //print_object ($GLOBALS['traverse_array']); //Debug
394 //$GLOBALS['traverse_array']=""; //Debug
396 //We'll need this later!!
397 $oldid = backup_todb($cat_info['#']['ID']['0']['#']);
399 //Now, build the GLOSSARY_CATEGORIES record structure
400 $category->glossaryid = $new_glossary_id;
401 $category->name = backup_todb($cat_info['#']['NAME']['0']['#']);
402 $category->usedynalink = backup_todb($cat_info['#']['USEDYNALINK']['0']['#']);
404 $newid = insert_record ("glossary_categories",$category);
406 //Do some output
407 if (($i+1) % 50 == 0) {
408 if (!defined('RESTORE_SILENTLY')) {
409 echo ".";
410 if (($i+1) % 1000 == 0) {
411 echo "<br />";
414 backup_flush(300);
417 if ($newid) {
418 //We have the newid, update backup_ids
419 backup_putid($restore->backup_unique_code,"glossary_categories",$oldid,$newid);
420 //Now restore glossary_entries_categories
421 $status = glossary_entries_categories_restore_mods($oldid,$newid,$cat_info,$restore);
422 } else {
423 $status = false;
427 return $status;
431 //This function restores the glossary_entries_categories
432 function glossary_entries_categories_restore_mods($old_category_id,$new_category_id,$info,$restore) {
434 global $CFG;
436 $status = true;
438 //Get the entryids array
439 $entryids = isset($info['#']['ENTRIES']['0']['#']['ENTRY'])?$info['#']['ENTRIES']['0']['#']['ENTRY']:array();
441 //Iterate over entryids
442 for($i = 0; $i < sizeof($entryids); $i++) {
443 $ent_info = $entryids[$i];
444 //traverse_xmlize($ent_info); //Debug
445 //print_object ($GLOBALS['traverse_array']); //Debug
446 //$GLOBALS['traverse_array']=""; //Debug
448 //Now, build the GLOSSARY_ENTRIES_CATEGORIES record structure
449 $entry_category->categoryid = $new_category_id;
450 $entry_category->entryid = backup_todb($ent_info['#']['ENTRYID']['0']['#']);
452 //We have to recode the entryid field
453 $entry = backup_getid($restore->backup_unique_code,"glossary_entries",$entry_category->entryid);
454 if ($entry) {
455 $entry_category->entryid = $entry->new_id;
458 $newid = insert_record ("glossary_entries_categories",$entry_category);
460 //Do some output
461 if (($i+1) % 50 == 0) {
462 if (!defined('RESTORE_SILENTLY')) {
463 echo ".";
464 if (($i+1) % 1000 == 0) {
465 echo "<br />";
468 backup_flush(300);
471 if (!$newid) {
472 $status = false;
476 return $status;
479 //This function copies the glossary related info from backup temp dir to course moddata folder,
480 //creating it if needed and recoding everything (glossary id and entry id)
481 function glossary_restore_files ($oldgloid, $newgloid, $oldentryid, $newentryid, $restore) {
483 global $CFG;
485 $status = true;
486 $todo = false;
487 $moddata_path = "";
488 $glossary_path = "";
489 $temp_path = "";
491 //First, we check to "course_id" exists and create is as necessary
492 //in CFG->dataroot
493 $dest_dir = $CFG->dataroot."/".$restore->course_id;
494 $status = check_dir_exists($dest_dir,true);
496 //Now, locate course's moddata directory
497 $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
499 //Check it exists and create it
500 $status = check_dir_exists($moddata_path,true);
502 //Now, locate glossary directory
503 if ($status) {
504 $glossary_path = $moddata_path."/glossary";
505 //Check it exists and create it
506 $status = check_dir_exists($glossary_path,true);
509 //Now locate the temp dir we are restoring from
510 if ($status) {
511 $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
512 "/moddata/glossary/".$oldgloid."/".$oldentryid;
513 //Check it exists
514 if (is_dir($temp_path)) {
515 $todo = true;
519 //If todo, we create the neccesary dirs in course moddata/glossary
520 if ($status and $todo) {
521 //First this glossary id
522 $this_glossary_path = $glossary_path."/".$newgloid;
523 $status = check_dir_exists($this_glossary_path,true);
524 //Now this entry id
525 $entry_glossary_path = $this_glossary_path."/".$newentryid;
526 //And now, copy temp_path to entry_glossary_path
527 $status = backup_copy_file($temp_path, $entry_glossary_path);
530 return $status;
533 //Return a content decoded to support interactivities linking. Every module
534 //should have its own. They are called automatically from
535 //glossary_decode_content_links_caller() function in each module
536 //in the restore process
537 function glossary_decode_content_links ($content,$restore) {
539 global $CFG;
541 $result = $content;
543 //Link to the list of glossarys
545 $searchstring='/\$@(GLOSSARYINDEX)\*([0-9]+)@\$/';
546 //We look for it
547 preg_match_all($searchstring,$content,$foundset);
548 //If found, then we are going to look for its new id (in backup tables)
549 if ($foundset[0]) {
550 //print_object($foundset); //Debug
551 //Iterate over foundset[2]. They are the old_ids
552 foreach($foundset[2] as $old_id) {
553 //We get the needed variables here (course id)
554 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
555 //Personalize the searchstring
556 $searchstring='/\$@(GLOSSARYINDEX)\*('.$old_id.')@\$/';
557 //If it is a link to this course, update the link to its new location
558 if($rec->new_id) {
559 //Now replace it
560 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/glossary/index.php?id='.$rec->new_id,$result);
561 } else {
562 //It's a foreign link so leave it as original
563 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/glossary/index.php?id='.$old_id,$result);
568 //Link to glossary view by moduleid
570 $searchstring='/\$@(GLOSSARYVIEWBYID)\*([0-9]+)@\$/';
571 //We look for it
572 preg_match_all($searchstring,$result,$foundset);
573 //If found, then we are going to look for its new id (in backup tables)
574 if ($foundset[0]) {
575 //print_object($foundset); //Debug
576 //Iterate over foundset[2]. They are the old_ids
577 foreach($foundset[2] as $old_id) {
578 //We get the needed variables here (course_modules id)
579 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
580 //Personalize the searchstring
581 $searchstring='/\$@(GLOSSARYVIEWBYID)\*('.$old_id.')@\$/';
582 //If it is a link to this course, update the link to its new location
583 if($rec->new_id) {
584 //Now replace it
585 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/glossary/view.php?id='.$rec->new_id,$result);
586 } else {
587 //It's a foreign link so leave it as original
588 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/glossary/view.php?id='.$old_id,$result);
593 return $result;
596 //This function makes all the necessary calls to xxxx_decode_content_links()
597 //function in each module, passing them the desired contents to be decoded
598 //from backup format to destination site/course in order to mantain inter-activities
599 //working in the backup/restore process. It's called from restore_decode_content_links()
600 //function in restore process
601 function glossary_decode_content_links_caller($restore) {
602 global $CFG;
603 $status = true;
605 //Process every glossary ENTRY in the course
606 if ($entries = get_records_sql ("SELECT e.id, e.definition
607 FROM {$CFG->prefix}glossary_entries e,
608 {$CFG->prefix}glossary g
609 WHERE g.course = $restore->course_id AND
610 e.glossaryid = g.id")) {
611 //Iterate over each post->message
612 $i = 0; //Counter to send some output to the browser to avoid timeouts
613 foreach ($entries as $entry) {
614 //Increment counter
615 $i++;
616 $content = $entry->definition;
617 $result = restore_decode_content_links_worker($content,$restore);
618 if ($result != $content) {
619 //Update record
620 $entry->definition = addslashes($result);
621 $status = update_record("glossary_entries",$entry);
622 if (debugging()) {
623 if (!defined('RESTORE_SILENTLY')) {
624 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
628 //Do some output
629 if (($i+1) % 5 == 0) {
630 if (!defined('RESTORE_SILENTLY')) {
631 echo ".";
632 if (($i+1) % 100 == 0) {
633 echo "<br />";
636 backup_flush(300);
641 //Process every glossary (intro) in the course
642 if ($glossarys = get_records_sql ("SELECT g.id, g.intro
643 FROM {$CFG->prefix}glossary g
644 WHERE g.course = $restore->course_id")) {
645 //Iterate over each glossary->intro
646 $i = 0; //Counter to send some output to the browser to avoid timeouts
647 foreach ($glossarys as $glossary) {
648 //Increment counter
649 $i++;
650 $content = $glossary->intro;
651 $result = restore_decode_content_links_worker($content,$restore);
652 if ($result != $content) {
653 //Update record
654 $glossary->intro = addslashes($result);
655 $status = update_record("glossary",$glossary);
656 if (debugging()) {
657 if (!defined('RESTORE_SILENTLY')) {
658 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
662 //Do some output
663 if (($i+1) % 5 == 0) {
664 if (!defined('RESTORE_SILENTLY')) {
665 echo ".";
666 if (($i+1) % 100 == 0) {
667 echo "<br />";
670 backup_flush(300);
675 return $status;
678 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
679 //some texts in the module
680 function glossary_restore_wiki2markdown ($restore) {
682 global $CFG;
684 $status = true;
686 //Convert glossary_comments->entrycomment
687 if ($records = get_records_sql ("SELECT c.id, c.entrycomment, c.format
688 FROM {$CFG->prefix}glossary_comments c,
689 {$CFG->prefix}glossary_entries e,
690 {$CFG->prefix}glossary g,
691 {$CFG->prefix}backup_ids b
692 WHERE e.id = c.entryid AND
693 g.id = e.glossaryid AND
694 g.course = $restore->course_id AND
695 c.format = ".FORMAT_WIKI. " AND
696 b.backup_code = $restore->backup_unique_code AND
697 b.table_name = 'glossary_comments' AND
698 b.new_id = c.id")) {
699 foreach ($records as $record) {
700 //Rebuild wiki links
701 $record->entrycomment = restore_decode_wiki_content($record->entrycomment, $restore);
702 //Convert to Markdown
703 $wtm = new WikiToMarkdown();
704 $record->entrycomment = $wtm->convert($record->entrycomment, $restore->course_id);
705 $record->format = FORMAT_MARKDOWN;
706 $status = update_record('glossary_comments', addslashes_object($record));
707 //Do some output
708 $i++;
709 if (($i+1) % 1 == 0) {
710 if (!defined('RESTORE_SILENTLY')) {
711 echo ".";
712 if (($i+1) % 20 == 0) {
713 echo "<br />";
716 backup_flush(300);
722 //Convert glossary_entries->definition
723 if ($records = get_records_sql ("SELECT e.id, e.definition, e.format
724 FROM {$CFG->prefix}glossary_entries e,
725 {$CFG->prefix}glossary g,
726 {$CFG->prefix}backup_ids b
727 WHERE g.id = e.glossaryid AND
728 g.course = $restore->course_id AND
729 e.format = ".FORMAT_WIKI. " AND
730 b.backup_code = $restore->backup_unique_code AND
731 b.table_name = 'glossary_entries' AND
732 b.new_id = e.id")) {
733 foreach ($records as $record) {
734 //Rebuild wiki links
735 $record->definition = restore_decode_wiki_content($record->definition, $restore);
736 //Convert to Markdown
737 $wtm = new WikiToMarkdown();
738 $record->definition = $wtm->convert($record->definition, $restore->course_id);
739 $record->format = FORMAT_MARKDOWN;
740 $status = update_record('glossary_entries', addslashes_object($record));
741 //Do some output
742 $i++;
743 if (($i+1) % 1 == 0) {
744 if (!defined('RESTORE_SILENTLY')) {
745 echo ".";
746 if (($i+1) % 20 == 0) {
747 echo "<br />";
750 backup_flush(300);
756 return $status;
759 //This function returns a log record with all the necessay transformations
760 //done. It's used by restore_log_module() to restore modules log.
761 function glossary_restore_logs($restore,$log) {
763 $status = false;
765 //Depending of the action, we recode different things
766 switch ($log->action) {
767 case "add":
768 if ($log->cmid) {
769 //Get the new_id of the module (to recode the info field)
770 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
771 if ($mod) {
772 $log->url = "view.php?id=".$log->cmid;
773 $log->info = $mod->new_id;
774 $status = true;
777 break;
778 case "update":
779 if ($log->cmid) {
780 //Get the new_id of the module (to recode the info field)
781 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
782 if ($mod) {
783 $log->url = "view.php?id=".$log->cmid;
784 $log->info = $mod->new_id;
785 $status = true;
788 break;
789 case "view":
790 if ($log->cmid) {
791 //Get the new_id of the module (to recode the info field)
792 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
793 if ($mod) {
794 $log->url = "view.php?id=".$log->cmid;
795 $log->info = $mod->new_id;
796 $status = true;
799 break;
800 case "view all":
801 $log->url = "index.php?id=".$log->course;
802 $status = true;
803 break;
804 case "add category":
805 if ($log->cmid) {
806 //Get the new_id of the glossary_category (to recode the info field)
807 $cat = backup_getid($restore->backup_unique_code,"glossary_categories",$log->info);
808 if ($cat) {
809 $log->url = "editcategories.php?id=".$log->cmid;
810 $log->info = $cat->new_id;
811 $status = true;
814 break;
815 case "edit category":
816 if ($log->cmid) {
817 //Get the new_id of the glossary_category (to recode the info field)
818 $cat = backup_getid($restore->backup_unique_code,"glossary_categories",$log->info);
819 if ($cat) {
820 $log->url = "editcategories.php?id=".$log->cmid;
821 $log->info = $cat->new_id;
822 $status = true;
825 break;
826 case "delete category":
827 if ($log->cmid) {
828 //Get the new_id of the glossary_category (to recode the info field)
829 $cat = backup_getid($restore->backup_unique_code,"glossary_categories",$log->info);
830 if ($cat) {
831 $log->url = "editcategories.php?id=".$log->cmid;
832 $log->info = $cat->new_id;
833 $status = true;
836 break;
837 case "add entry":
838 if ($log->cmid) {
839 //Get the new_id of the glossary_entry (to recode the info and url field)
840 $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$log->info);
841 if ($ent) {
842 $log->url = "view.php?id=".$log->cmid."&mode=entry&hook=".$ent->new_id;
843 $log->info = $ent->new_id;
844 $status = true;
847 break;
848 case "update entry":
849 if ($log->cmid) {
850 //Get the new_id of the glossary_entry (to recode the info and url field)
851 $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$log->info);
852 if ($ent) {
853 $log->url = "view.php?id=".$log->cmid."&mode=entry&hook=".$ent->new_id;
854 $log->info = $ent->new_id;
855 $status = true;
858 break;
859 case "delete entry":
860 if ($log->cmid) {
861 //Get the new_id of the glossary_entry (to recode the info and url field)
862 $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$log->info);
863 if ($ent) {
864 $log->url = "view.php?id=".$log->cmid."&mode=entry&hook=".$ent->new_id;
865 $log->info = $ent->new_id;
866 $status = true;
869 break;
870 case "approve entry":
871 if ($log->cmid) {
872 //Get the new_id of the glossary_entry (to recode the info and url field)
873 $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$log->info);
874 if ($ent) {
875 $log->url = "showentry.php?id=".$log->cmid."&eid=".$ent->new_id;
876 $log->info = $ent->new_id;
877 $status = true;
880 break;
881 case "view entry":
882 if ($log->cmid) {
883 //Get the new_id of the glossary_entry (to recode the info and url field)
884 $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$log->info);
885 if ($ent) {
886 $log->url = "showentry.php?&eid=".$ent->new_id;
887 $log->info = $ent->new_id;
888 $status = true;
891 break;
892 case "add comment":
893 if ($log->cmid) {
894 //Extract the entryid from the url field
895 $entid = substr(strrchr($log->url,"="),1);
896 //Get the new_id of the glossary_entry (to recode the url field)
897 $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$entid);
898 //Get the new_id of the glossary_comment (to recode the info field)
899 $com = backup_getid($restore->backup_unique_code,"glossary_comments",$log->info);
900 if ($ent and $com) {
901 $log->url = "comments.php?id=".$log->cmid."&eid=".$ent->new_id;
902 $log->info = $com->new_id;
903 $status = true;
906 break;
907 case "update comment":
908 if ($log->cmid) {
909 //Extract the entryid from the url field
910 $entid = substr(strrchr($log->url,"="),1);
911 //Get the new_id of the glossary_entry (to recode the url field)
912 $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$entid);
913 //Get the new_id of the glossary_comment (to recode the info field)
914 $com = backup_getid($restore->backup_unique_code,"glossary_comments",$log->info);
915 if ($ent and $com) {
916 $log->url = "comments.php?id=".$log->cmid."&eid=".$ent->new_id;
917 $log->info = $com->new_id;
918 $status = true;
921 break;
922 case "delete comment":
923 if ($log->cmid) {
924 //Extract the entryid from the url field
925 $entid = substr(strrchr($log->url,"="),1);
926 //Get the new_id of the glossary_entry (to recode the url field)
927 $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$entid);
928 //Get the new_id of the glossary_comment (to recode the info field)
929 $com = backup_getid($restore->backup_unique_code,"glossary_comments",$log->info);
930 if ($ent and $com) {
931 $log->url = "comments.php?id=".$log->cmid."&eid=".$ent->new_id;
932 $log->info = $com->new_id;
933 $status = true;
936 break;
937 default:
938 if (!defined('RESTORE_SILENTLY')) {
939 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
941 break;
944 if ($status) {
945 $status = $log;
947 return $status;