PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/glossary/restorelib.php

https://bitbucket.org/ceu/moodle_demo
PHP | 949 lines | 634 code | 115 blank | 200 comment | 142 complexity | f52af4dc9bd446a3e7df0b529529557f MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  1. <?php //$Id: restorelib.php,v 1.53 2006/11/15 03:15:59 toyomoyo Exp $
  2. //This php script contains all the stuff to backup/restore
  3. //glossary mods
  4. //This is the "graphical" structure of the glossary mod:
  5. //
  6. // glossary ----------------------------------------- glossary_categories
  7. // (CL,pk->id) (CL,pk->id,fk->glossaryid)
  8. // | |
  9. // | |
  10. // | |
  11. // glossary_entries --------------------------------glossary_entries_categories
  12. // (UL,pk->id, fk->glossaryid, files) | (UL, pk->categoryid,entryid)
  13. // | |
  14. // | |--------------------glossary_ratings
  15. // | | (UL, pk->id, pk->entryid)
  16. // glossary_comments |
  17. // (UL,pk->id, fk->entryid) |---------------------glossary_alias
  18. // (UL, pk->id, pk->entryid)
  19. //
  20. //
  21. // Meaning: pk->primary key field of the table
  22. // fk->foreign key to link with parent
  23. // nt->nested field (recursive data)
  24. // CL->course level info
  25. // UL->user level info
  26. // files->table may have files)
  27. //
  28. //-----------------------------------------------------------
  29. //This function executes all the restore procedure about this mod
  30. function glossary_restore_mods($mod,$restore) {
  31. global $CFG;
  32. $status = true;
  33. //Get record from backup_ids
  34. $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
  35. if ($data) {
  36. //Now get completed xmlized object
  37. $info = $data->info;
  38. // if necessary, write to restorelog and adjust date/time fields
  39. if ($restore->course_startdateoffset) {
  40. restore_log_date_changes('Glossary', $restore, $info['MOD']['#'], array('ASSESSTIMESTART', 'ASSESSTIMEFINISH'));
  41. }
  42. //traverse_xmlize($info); //Debug
  43. //print_object ($GLOBALS['traverse_array']); //Debug
  44. //$GLOBALS['traverse_array']=""; //Debug
  45. //Now, build the glossary record structure
  46. $glossary->course = $restore->course_id;
  47. $glossary->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
  48. $glossary->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
  49. $glossary->allowduplicatedentries = backup_todb($info['MOD']['#']['ALLOWDUPLICATEDENTRIES']['0']['#']);
  50. $glossary->displayformat = backup_todb($info['MOD']['#']['DISPLAYFORMAT']['0']['#']);
  51. $glossary->mainglossary = backup_todb($info['MOD']['#']['MAINGLOSSARY']['0']['#']);
  52. $glossary->showspecial = backup_todb($info['MOD']['#']['SHOWSPECIAL']['0']['#']);
  53. $glossary->showalphabet = backup_todb($info['MOD']['#']['SHOWALPHABET']['0']['#']);
  54. $glossary->showall = backup_todb($info['MOD']['#']['SHOWALL']['0']['#']);
  55. $glossary->allowcomments = backup_todb($info['MOD']['#']['ALLOWCOMMENTS']['0']['#']);
  56. $glossary->allowprintview = backup_todb($info['MOD']['#']['ALLOWPRINTVIEW']['0']['#']);
  57. $glossary->usedynalink = backup_todb($info['MOD']['#']['USEDYNALINK']['0']['#']);
  58. $glossary->defaultapproval = backup_todb($info['MOD']['#']['DEFAULTAPPROVAL']['0']['#']);
  59. $glossary->globalglossary = backup_todb($info['MOD']['#']['GLOBALGLOSSARY']['0']['#']);
  60. $glossary->entbypage = backup_todb($info['MOD']['#']['ENTBYPAGE']['0']['#']);
  61. $glossary->editalways = backup_todb($info['MOD']['#']['EDITALWAYS']['0']['#']);
  62. $glossary->rsstype = backup_todb($info['MOD']['#']['RSSTYPE']['0']['#']);
  63. $glossary->rssarticles = backup_todb($info['MOD']['#']['RSSARTICLES']['0']['#']);
  64. $glossary->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
  65. $glossary->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
  66. $glossary->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
  67. $glossary->assesstimestart = backup_todb($info['MOD']['#']['ASSESSTIMESTART']['0']['#']);
  68. $glossary->assesstimefinish = backup_todb($info['MOD']['#']['ASSESSTIMEFINISH']['0']['#']);
  69. $glossary->scale = backup_todb($info['MOD']['#']['SCALE']['0']['#']);
  70. //We have to recode the scale field if it's <0 (positive is a grade, not a scale)
  71. if ($glossary->scale < 0) {
  72. $scale = backup_getid($restore->backup_unique_code,"scale",abs($glossary->scale));
  73. if ($scale) {
  74. $glossary->scale = -($scale->new_id);
  75. }
  76. }
  77. //To mantain upwards compatibility (pre 1.4) we have to check the displayformat field
  78. //If it's numeric (0-6) we have to convert it to its new formatname.
  79. //Define current 0-6 format names
  80. $formatnames = array('dictionary','continuous','fullwithauthor','encyclopedia',
  81. 'faq','fullwithoutauthor','entrylist');
  82. //If it's numeric, we are restoring a pre 1.4 course, do the conversion
  83. if (is_numeric($glossary->displayformat)) {
  84. $displayformat = 'dictionary'; //Default format
  85. if ($glossary->displayformat >= 0 && $glossary->displayformat <= 6) {
  86. $displayformat = $formatnames[$glossary->displayformat];
  87. }
  88. $glossary->displayformat = $displayformat;
  89. }
  90. //Now check that the displayformat exists in the server, else default to dictionary
  91. $formats = get_list_of_plugins('mod/glossary/formats');
  92. if (!in_array($glossary->displayformat,$formats)) {
  93. $glossary->displayformat = 'dictionary';
  94. }
  95. //If the backup file doesn't include the editalways field, activate it
  96. //in secondary glossaries (old behaviour, pre 1.4)
  97. if (! isset($info['MOD']['#']['EDITALWAYS']['0']['#'])) { //It's a pre-14 backup file
  98. if ($glossary->mainglossary == '0') {
  99. $glossary->editalways = '1';
  100. }
  101. }
  102. //The structure is equal to the db, so insert the glossary
  103. $newid = insert_record ("glossary",$glossary);
  104. //Do some output
  105. if (!defined('RESTORE_SILENTLY')) {
  106. echo "<li>".get_string("modulename","glossary")." \"".format_string(stripslashes($glossary->name),true)."\"</li>";
  107. }
  108. backup_flush(300);
  109. if ($newid) {
  110. //We have the newid, update backup_ids
  111. backup_putid($restore->backup_unique_code,$mod->modtype,
  112. $mod->id, $newid);
  113. //Restore glossary_entries
  114. $status = glossary_entries_restore_mods($mod->id,$newid,$info,$restore);
  115. //Restore glossary_categories and glossary_category_entries
  116. $status = glossary_categories_restore_mods($mod->id,$newid,$info,$restore);
  117. } else {
  118. $status = false;
  119. }
  120. } else {
  121. $status = false;
  122. }
  123. return $status;
  124. }
  125. //This function restores the glossary_entries
  126. function glossary_entries_restore_mods($old_glossary_id,$new_glossary_id,$info,$restore) {
  127. global $CFG;
  128. $status = true;
  129. //Get the entries array
  130. $entries = isset($info['MOD']['#']['ENTRIES']['0']['#']['ENTRY'])?$info['MOD']['#']['ENTRIES']['0']['#']['ENTRY']:array();
  131. //Iterate over entries
  132. for($i = 0; $i < sizeof($entries); $i++) {
  133. $ent_info = $entries[$i];
  134. //traverse_xmlize($ent_info); //Debug
  135. //print_object ($GLOBALS['traverse_array']); //Debug
  136. //$GLOBALS['traverse_array']=""; //Debug
  137. //We'll need this later!!
  138. $oldid = backup_todb($ent_info['#']['ID']['0']['#']);
  139. $olduserid = backup_todb($ent_info['#']['USERID']['0']['#']);
  140. //Now, build the GLOSSARY_ENTRIES record structure
  141. $entry->glossaryid = $new_glossary_id;
  142. $entry->userid = backup_todb($ent_info['#']['USERID']['0']['#']);
  143. $entry->concept = backup_todb(trim($ent_info['#']['CONCEPT']['0']['#']));
  144. $entry->definition = backup_todb($ent_info['#']['DEFINITION']['0']['#']);
  145. $entry->format = backup_todb($ent_info['#']['FORMAT']['0']['#']);
  146. $entry->attachment = backup_todb($ent_info['#']['ATTACHMENT']['0']['#']);
  147. $entry->sourceglossaryid = backup_todb($ent_info['#']['SOURCEGLOSSARYID']['0']['#']);
  148. $entry->usedynalink = backup_todb($ent_info['#']['USEDYNALINK']['0']['#']);
  149. $entry->casesensitive = backup_todb($ent_info['#']['CASESENSITIVE']['0']['#']);
  150. $entry->fullmatch = backup_todb($ent_info['#']['FULLMATCH']['0']['#']);
  151. $entry->approved = backup_todb($ent_info['#']['APPROVED']['0']['#']);
  152. $entry->timecreated = backup_todb($ent_info['#']['TIMECREATED']['0']['#']);
  153. $entry->timemodified = backup_todb($ent_info['#']['TIMEMODIFIED']['0']['#']);
  154. $entry->teacherentry = backup_todb($ent_info['#']['TEACHERENTRY']['0']['#']);
  155. //We have to recode the userid field
  156. $user = backup_getid($restore->backup_unique_code,"user",$entry->userid);
  157. if ($user) {
  158. $entry->userid = $user->new_id;
  159. }
  160. //We have to recode the sourceglossaryid field
  161. $source = backup_getid($restore->backup_unique_code,"glossary",$entry->sourceglossaryid);
  162. if ($source) {
  163. $entry->sourceglossaryid = $source->new_id;
  164. }
  165. //If it's a teacher entry or userinfo was selected, restore the entry
  166. if ($entry->teacherentry or restore_userdata_selected($restore,'glossary',$old_glossary_id)) {
  167. //The structure is equal to the db, so insert the glossary_entries
  168. $newid = insert_record ("glossary_entries",$entry);
  169. //Do some output
  170. if (($i+1) % 50 == 0) {
  171. if (!defined('RESTORE_SILENTLY')) {
  172. echo ".";
  173. if (($i+1) % 1000 == 0) {
  174. echo "<br />";
  175. }
  176. }
  177. backup_flush(300);
  178. }
  179. if ($newid) {
  180. //We have the newid, update backup_ids
  181. backup_putid($restore->backup_unique_code,"glossary_entries",$oldid,$newid);
  182. //Restore glossary_alias
  183. $status = glossary_alias_restore_mods($oldid,$newid,$ent_info,$restore);
  184. //Now restore glossary_comments
  185. $status = glossary_comments_restore_mods($oldid,$newid,$ent_info,$restore);
  186. //Now restore glossary_ratings
  187. $status = glossary_ratings_restore_mods($oldid,$newid,$ent_info,$restore);
  188. //Now copy moddata associated files if needed
  189. if ($entry->attachment) {
  190. $status = glossary_restore_files ($old_glossary_id, $new_glossary_id,
  191. $oldid, $newid, $restore);
  192. }
  193. } else {
  194. $status = false;
  195. }
  196. }
  197. }
  198. return $status;
  199. }
  200. //This function restores the glossary_comments
  201. function glossary_comments_restore_mods($old_entry_id,$new_entry_id,$info,$restore) {
  202. global $CFG;
  203. $status = true;
  204. //Get the comments array
  205. $comments = isset($info['#']['COMMENTS']['0']['#']['COMMENT'])?$info['#']['COMMENTS']['0']['#']['COMMENT']:array();
  206. //Iterate over comments
  207. for($i = 0; $i < sizeof($comments); $i++) {
  208. $com_info = $comments[$i];
  209. //traverse_xmlize($com_info); //Debug
  210. //print_object ($GLOBALS['traverse_array']); //Debug
  211. //$GLOBALS['traverse_array']=""; //Debug
  212. //We'll need this later!!
  213. $oldid = backup_todb($com_info['#']['ID']['0']['#']);
  214. //Now, build the GLOSSARY_COMMENTS record structure
  215. $comment->entryid = $new_entry_id;
  216. $comment->userid = backup_todb($com_info['#']['USERID']['0']['#']);
  217. if (isset($com_info['#']['COMMENT']['0']['#'])) {
  218. $comment->entrycomment = backup_todb($com_info['#']['COMMENT']['0']['#']);
  219. } else {
  220. $comment->entrycomment = backup_todb($com_info['#']['ENTRYCOMMENT']['0']['#']);
  221. }
  222. $comment->timemodified = backup_todb($com_info['#']['TIMEMODIFIED']['0']['#']);
  223. $comment->format = backup_todb($com_info['#']['FORMAT']['0']['#']);
  224. //We have to recode the userid field
  225. $user = backup_getid($restore->backup_unique_code,"user",$comment->userid);
  226. if ($user) {
  227. $comment->userid = $user->new_id;
  228. }
  229. //The structure is equal to the db, so insert the glossary_comments
  230. $newid = insert_record ("glossary_comments",$comment);
  231. //Do some output
  232. if (($i+1) % 50 == 0) {
  233. if (!defined('RESTORE_SILENTLY')) {
  234. echo ".";
  235. if (($i+1) % 1000 == 0) {
  236. echo "<br />";
  237. }
  238. }
  239. backup_flush(300);
  240. }
  241. if ($newid) {
  242. //We have the newid, update backup_ids
  243. backup_putid($restore->backup_unique_code,"glossary_comments",$oldid,$newid);
  244. } else {
  245. $status = false;
  246. }
  247. }
  248. return $status;
  249. }
  250. //This function restores the glossary_ratings
  251. function glossary_ratings_restore_mods($old_entry_id,$new_entry_id,$info,$restore) {
  252. global $CFG;
  253. $status = true;
  254. //Get the ratings array
  255. $ratings = isset($info['#']['RATINGS']['0']['#']['RATING'])?$info['#']['RATINGS']['0']['#']['RATING']:array();
  256. //Iterate over ratings
  257. for($i = 0; $i < sizeof($ratings); $i++) {
  258. $rat_info = $ratings[$i];
  259. //traverse_xmlize($rat_info); //Debug
  260. //print_object ($GLOBALS['traverse_array']); //Debug
  261. //$GLOBALS['traverse_array']=""; //Debug
  262. //Now, build the GLOSSARY_RATINGS record structure
  263. $rating->entryid = $new_entry_id;
  264. $rating->userid = backup_todb($rat_info['#']['USERID']['0']['#']);
  265. $rating->time = backup_todb($rat_info['#']['TIME']['0']['#']);
  266. $rating->rating = backup_todb($rat_info['#']['RATING']['0']['#']);
  267. //We have to recode the userid field
  268. $user = backup_getid($restore->backup_unique_code,"user",$rating->userid);
  269. if ($user) {
  270. $rating->userid = $user->new_id;
  271. }
  272. //The structure is equal to the db, so insert the glossary_ratings
  273. $newid = insert_record ("glossary_ratings",$rating);
  274. //Do some output
  275. if (($i+1) % 50 == 0) {
  276. if (!defined('RESTORE_SILENTLY')) {
  277. echo ".";
  278. if (($i+1) % 1000 == 0) {
  279. echo "<br />";
  280. }
  281. }
  282. backup_flush(300);
  283. }
  284. if (!$newid) {
  285. $status = false;
  286. }
  287. }
  288. return $status;
  289. }
  290. //This function restores the glossary_alias table
  291. function glossary_alias_restore_mods($old_entry_id,$new_entry_id,$info,$restore) {
  292. global $CFG;
  293. $status = true;
  294. //Get the comments array
  295. $aliases = isset($info['#']['ALIASES']['0']['#']['ALIAS'])?$info['#']['ALIASES']['0']['#']['ALIAS']:array();
  296. //Iterate over comments
  297. for($i = 0; $i < sizeof($aliases); $i++) {
  298. $alias_info = $aliases[$i];
  299. //Now, build the GLOSSARY_ALIAS record structure
  300. $alias->entryid = $new_entry_id;
  301. $alias->alias = backup_todb(trim($alias_info['#']['ALIAS_TEXT']['0']['#']));
  302. //The structure is equal to the db, so insert the glossary_comments
  303. $newid = insert_record ("glossary_alias",$alias);
  304. //Do some output
  305. if (($i+1) % 50 == 0) {
  306. if (!defined('RESTORE_SILENTLY')) {
  307. echo ".";
  308. if (($i+1) % 1000 == 0) {
  309. echo "<br />";
  310. }
  311. }
  312. backup_flush(300);
  313. }
  314. if (!$newid) {
  315. $status = false;
  316. }
  317. }
  318. return $status;
  319. }
  320. //This function restores the glossary_categories
  321. function glossary_categories_restore_mods($old_glossary_id,$new_glossary_id,$info,$restore) {
  322. global $CFG;
  323. $status = true;
  324. //Get the categories array
  325. $categories = isset($info['MOD']['#']['CATEGORIES']['0']['#']['CATEGORY'])?$info['MOD']['#']['CATEGORIES']['0']['#']['CATEGORY']:array();
  326. //Iterate over categories
  327. for($i = 0; $i < sizeof($categories); $i++) {
  328. $cat_info = $categories[$i];
  329. //traverse_xmlize($cat_info); //Debug
  330. //print_object ($GLOBALS['traverse_array']); //Debug
  331. //$GLOBALS['traverse_array']=""; //Debug
  332. //We'll need this later!!
  333. $oldid = backup_todb($cat_info['#']['ID']['0']['#']);
  334. //Now, build the GLOSSARY_CATEGORIES record structure
  335. $category->glossaryid = $new_glossary_id;
  336. $category->name = backup_todb($cat_info['#']['NAME']['0']['#']);
  337. $category->usedynalink = backup_todb($cat_info['#']['USEDYNALINK']['0']['#']);
  338. $newid = insert_record ("glossary_categories",$category);
  339. //Do some output
  340. if (($i+1) % 50 == 0) {
  341. if (!defined('RESTORE_SILENTLY')) {
  342. echo ".";
  343. if (($i+1) % 1000 == 0) {
  344. echo "<br />";
  345. }
  346. }
  347. backup_flush(300);
  348. }
  349. if ($newid) {
  350. //We have the newid, update backup_ids
  351. backup_putid($restore->backup_unique_code,"glossary_categories",$oldid,$newid);
  352. //Now restore glossary_entries_categories
  353. $status = glossary_entries_categories_restore_mods($oldid,$newid,$cat_info,$restore);
  354. } else {
  355. $status = false;
  356. }
  357. }
  358. return $status;
  359. }
  360. //This function restores the glossary_entries_categories
  361. function glossary_entries_categories_restore_mods($old_category_id,$new_category_id,$info,$restore) {
  362. global $CFG;
  363. $status = true;
  364. //Get the entryids array
  365. $entryids = isset($info['#']['ENTRIES']['0']['#']['ENTRY'])?$info['#']['ENTRIES']['0']['#']['ENTRY']:array();
  366. //Iterate over entryids
  367. for($i = 0; $i < sizeof($entryids); $i++) {
  368. $ent_info = $entryids[$i];
  369. //traverse_xmlize($ent_info); //Debug
  370. //print_object ($GLOBALS['traverse_array']); //Debug
  371. //$GLOBALS['traverse_array']=""; //Debug
  372. //Now, build the GLOSSARY_ENTRIES_CATEGORIES record structure
  373. $entry_category->categoryid = $new_category_id;
  374. $entry_category->entryid = backup_todb($ent_info['#']['ENTRYID']['0']['#']);
  375. //We have to recode the entryid field
  376. $entry = backup_getid($restore->backup_unique_code,"glossary_entries",$entry_category->entryid);
  377. if ($entry) {
  378. $entry_category->entryid = $entry->new_id;
  379. }
  380. $newid = insert_record ("glossary_entries_categories",$entry_category);
  381. //Do some output
  382. if (($i+1) % 50 == 0) {
  383. if (!defined('RESTORE_SILENTLY')) {
  384. echo ".";
  385. if (($i+1) % 1000 == 0) {
  386. echo "<br />";
  387. }
  388. }
  389. backup_flush(300);
  390. }
  391. if (!$newid) {
  392. $status = false;
  393. }
  394. }
  395. return $status;
  396. }
  397. //This function copies the glossary related info from backup temp dir to course moddata folder,
  398. //creating it if needed and recoding everything (glossary id and entry id)
  399. function glossary_restore_files ($oldgloid, $newgloid, $oldentryid, $newentryid, $restore) {
  400. global $CFG;
  401. $status = true;
  402. $todo = false;
  403. $moddata_path = "";
  404. $glossary_path = "";
  405. $temp_path = "";
  406. //First, we check to "course_id" exists and create is as necessary
  407. //in CFG->dataroot
  408. $dest_dir = $CFG->dataroot."/".$restore->course_id;
  409. $status = check_dir_exists($dest_dir,true);
  410. //Now, locate course's moddata directory
  411. $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
  412. //Check it exists and create it
  413. $status = check_dir_exists($moddata_path,true);
  414. //Now, locate glossary directory
  415. if ($status) {
  416. $glossary_path = $moddata_path."/glossary";
  417. //Check it exists and create it
  418. $status = check_dir_exists($glossary_path,true);
  419. }
  420. //Now locate the temp dir we are restoring from
  421. if ($status) {
  422. $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
  423. "/moddata/glossary/".$oldgloid."/".$oldentryid;
  424. //Check it exists
  425. if (is_dir($temp_path)) {
  426. $todo = true;
  427. }
  428. }
  429. //If todo, we create the neccesary dirs in course moddata/glossary
  430. if ($status and $todo) {
  431. //First this glossary id
  432. $this_glossary_path = $glossary_path."/".$newgloid;
  433. $status = check_dir_exists($this_glossary_path,true);
  434. //Now this entry id
  435. $entry_glossary_path = $this_glossary_path."/".$newentryid;
  436. //And now, copy temp_path to entry_glossary_path
  437. $status = backup_copy_file($temp_path, $entry_glossary_path);
  438. }
  439. return $status;
  440. }
  441. //Return a content decoded to support interactivities linking. Every module
  442. //should have its own. They are called automatically from
  443. //glossary_decode_content_links_caller() function in each module
  444. //in the restore process
  445. function glossary_decode_content_links ($content,$restore) {
  446. global $CFG;
  447. $result = $content;
  448. //Link to the list of glossarys
  449. $searchstring='/\$@(GLOSSARYINDEX)\*([0-9]+)@\$/';
  450. //We look for it
  451. preg_match_all($searchstring,$content,$foundset);
  452. //If found, then we are going to look for its new id (in backup tables)
  453. if ($foundset[0]) {
  454. //print_object($foundset); //Debug
  455. //Iterate over foundset[2]. They are the old_ids
  456. foreach($foundset[2] as $old_id) {
  457. //We get the needed variables here (course id)
  458. $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
  459. //Personalize the searchstring
  460. $searchstring='/\$@(GLOSSARYINDEX)\*('.$old_id.')@\$/';
  461. //If it is a link to this course, update the link to its new location
  462. if($rec->new_id) {
  463. //Now replace it
  464. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/glossary/index.php?id='.$rec->new_id,$result);
  465. } else {
  466. //It's a foreign link so leave it as original
  467. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/glossary/index.php?id='.$old_id,$result);
  468. }
  469. }
  470. }
  471. //Link to glossary view by moduleid
  472. $searchstring='/\$@(GLOSSARYVIEWBYID)\*([0-9]+)@\$/';
  473. //We look for it
  474. preg_match_all($searchstring,$result,$foundset);
  475. //If found, then we are going to look for its new id (in backup tables)
  476. if ($foundset[0]) {
  477. //print_object($foundset); //Debug
  478. //Iterate over foundset[2]. They are the old_ids
  479. foreach($foundset[2] as $old_id) {
  480. //We get the needed variables here (course_modules id)
  481. $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
  482. //Personalize the searchstring
  483. $searchstring='/\$@(GLOSSARYVIEWBYID)\*('.$old_id.')@\$/';
  484. //If it is a link to this course, update the link to its new location
  485. if($rec->new_id) {
  486. //Now replace it
  487. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/glossary/view.php?id='.$rec->new_id,$result);
  488. } else {
  489. //It's a foreign link so leave it as original
  490. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/glossary/view.php?id='.$old_id,$result);
  491. }
  492. }
  493. }
  494. return $result;
  495. }
  496. //This function makes all the necessary calls to xxxx_decode_content_links()
  497. //function in each module, passing them the desired contents to be decoded
  498. //from backup format to destination site/course in order to mantain inter-activities
  499. //working in the backup/restore process. It's called from restore_decode_content_links()
  500. //function in restore process
  501. function glossary_decode_content_links_caller($restore) {
  502. global $CFG;
  503. $status = true;
  504. //Process every glossary ENTRY in the course
  505. if ($entries = get_records_sql ("SELECT e.id, e.definition
  506. FROM {$CFG->prefix}glossary_entries e,
  507. {$CFG->prefix}glossary g
  508. WHERE g.course = $restore->course_id AND
  509. e.glossaryid = g.id")) {
  510. //Iterate over each post->message
  511. $i = 0; //Counter to send some output to the browser to avoid timeouts
  512. foreach ($entries as $entry) {
  513. //Increment counter
  514. $i++;
  515. $content = $entry->definition;
  516. $result = restore_decode_content_links_worker($content,$restore);
  517. if ($result != $content) {
  518. //Update record
  519. $entry->definition = addslashes($result);
  520. $status = update_record("glossary_entries",$entry);
  521. if (debugging()) {
  522. if (!defined('RESTORE_SILENTLY')) {
  523. echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
  524. }
  525. }
  526. }
  527. //Do some output
  528. if (($i+1) % 5 == 0) {
  529. if (!defined('RESTORE_SILENTLY')) {
  530. echo ".";
  531. if (($i+1) % 100 == 0) {
  532. echo "<br />";
  533. }
  534. }
  535. backup_flush(300);
  536. }
  537. }
  538. }
  539. //Process every glossary (intro) in the course
  540. if ($glossarys = get_records_sql ("SELECT g.id, g.intro
  541. FROM {$CFG->prefix}glossary g
  542. WHERE g.course = $restore->course_id")) {
  543. //Iterate over each glossary->intro
  544. $i = 0; //Counter to send some output to the browser to avoid timeouts
  545. foreach ($glossarys as $glossary) {
  546. //Increment counter
  547. $i++;
  548. $content = $glossary->intro;
  549. $result = restore_decode_content_links_worker($content,$restore);
  550. if ($result != $content) {
  551. //Update record
  552. $glossary->intro = addslashes($result);
  553. $status = update_record("glossary",$glossary);
  554. if (debugging()) {
  555. if (!defined('RESTORE_SILENTLY')) {
  556. echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
  557. }
  558. }
  559. }
  560. //Do some output
  561. if (($i+1) % 5 == 0) {
  562. if (!defined('RESTORE_SILENTLY')) {
  563. echo ".";
  564. if (($i+1) % 100 == 0) {
  565. echo "<br />";
  566. }
  567. }
  568. backup_flush(300);
  569. }
  570. }
  571. }
  572. return $status;
  573. }
  574. //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
  575. //some texts in the module
  576. function glossary_restore_wiki2markdown ($restore) {
  577. global $CFG;
  578. $status = true;
  579. //Convert glossary_comments->entrycomment
  580. if ($records = get_records_sql ("SELECT c.id, c.entrycomment, c.format
  581. FROM {$CFG->prefix}glossary_comments c,
  582. {$CFG->prefix}glossary_entries e,
  583. {$CFG->prefix}glossary g,
  584. {$CFG->prefix}backup_ids b
  585. WHERE e.id = c.entryid AND
  586. g.id = e.glossaryid AND
  587. g.course = $restore->course_id AND
  588. c.format = ".FORMAT_WIKI. " AND
  589. b.backup_code = $restore->backup_unique_code AND
  590. b.table_name = 'glossary_comments' AND
  591. b.new_id = c.id")) {
  592. foreach ($records as $record) {
  593. //Rebuild wiki links
  594. $record->entrycomment = restore_decode_wiki_content($record->entrycomment, $restore);
  595. //Convert to Markdown
  596. $wtm = new WikiToMarkdown();
  597. $record->entrycomment = $wtm->convert($record->entrycomment, $restore->course_id);
  598. $record->format = FORMAT_MARKDOWN;
  599. $status = update_record('glossary_comments', addslashes_object($record));
  600. //Do some output
  601. $i++;
  602. if (($i+1) % 1 == 0) {
  603. if (!defined('RESTORE_SILENTLY')) {
  604. echo ".";
  605. if (($i+1) % 20 == 0) {
  606. echo "<br />";
  607. }
  608. }
  609. backup_flush(300);
  610. }
  611. }
  612. }
  613. //Convert glossary_entries->definition
  614. if ($records = get_records_sql ("SELECT e.id, e.definition, e.format
  615. FROM {$CFG->prefix}glossary_entries e,
  616. {$CFG->prefix}glossary g,
  617. {$CFG->prefix}backup_ids b
  618. WHERE g.id = e.glossaryid AND
  619. g.course = $restore->course_id AND
  620. e.format = ".FORMAT_WIKI. " AND
  621. b.backup_code = $restore->backup_unique_code AND
  622. b.table_name = 'glossary_entries' AND
  623. b.new_id = e.id")) {
  624. foreach ($records as $record) {
  625. //Rebuild wiki links
  626. $record->definition = restore_decode_wiki_content($record->definition, $restore);
  627. //Convert to Markdown
  628. $wtm = new WikiToMarkdown();
  629. $record->definition = $wtm->convert($record->definition, $restore->course_id);
  630. $record->format = FORMAT_MARKDOWN;
  631. $status = update_record('glossary_entries', addslashes_object($record));
  632. //Do some output
  633. $i++;
  634. if (($i+1) % 1 == 0) {
  635. if (!defined('RESTORE_SILENTLY')) {
  636. echo ".";
  637. if (($i+1) % 20 == 0) {
  638. echo "<br />";
  639. }
  640. }
  641. backup_flush(300);
  642. }
  643. }
  644. }
  645. return $status;
  646. }
  647. //This function returns a log record with all the necessay transformations
  648. //done. It's used by restore_log_module() to restore modules log.
  649. function glossary_restore_logs($restore,$log) {
  650. $status = false;
  651. //Depending of the action, we recode different things
  652. switch ($log->action) {
  653. case "add":
  654. if ($log->cmid) {
  655. //Get the new_id of the module (to recode the info field)
  656. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  657. if ($mod) {
  658. $log->url = "view.php?id=".$log->cmid;
  659. $log->info = $mod->new_id;
  660. $status = true;
  661. }
  662. }
  663. break;
  664. case "update":
  665. if ($log->cmid) {
  666. //Get the new_id of the module (to recode the info field)
  667. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  668. if ($mod) {
  669. $log->url = "view.php?id=".$log->cmid;
  670. $log->info = $mod->new_id;
  671. $status = true;
  672. }
  673. }
  674. break;
  675. case "view":
  676. if ($log->cmid) {
  677. //Get the new_id of the module (to recode the info field)
  678. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  679. if ($mod) {
  680. $log->url = "view.php?id=".$log->cmid;
  681. $log->info = $mod->new_id;
  682. $status = true;
  683. }
  684. }
  685. break;
  686. case "view all":
  687. $log->url = "index.php?id=".$log->course;
  688. $status = true;
  689. break;
  690. case "add category":
  691. if ($log->cmid) {
  692. //Get the new_id of the glossary_category (to recode the info field)
  693. $cat = backup_getid($restore->backup_unique_code,"glossary_categories",$log->info);
  694. if ($cat) {
  695. $log->url = "editcategories.php?id=".$log->cmid;
  696. $log->info = $cat->new_id;
  697. $status = true;
  698. }
  699. }
  700. break;
  701. case "edit category":
  702. if ($log->cmid) {
  703. //Get the new_id of the glossary_category (to recode the info field)
  704. $cat = backup_getid($restore->backup_unique_code,"glossary_categories",$log->info);
  705. if ($cat) {
  706. $log->url = "editcategories.php?id=".$log->cmid;
  707. $log->info = $cat->new_id;
  708. $status = true;
  709. }
  710. }
  711. break;
  712. case "delete category":
  713. if ($log->cmid) {
  714. //Get the new_id of the glossary_category (to recode the info field)
  715. $cat = backup_getid($restore->backup_unique_code,"glossary_categories",$log->info);
  716. if ($cat) {
  717. $log->url = "editcategories.php?id=".$log->cmid;
  718. $log->info = $cat->new_id;
  719. $status = true;
  720. }
  721. }
  722. break;
  723. case "add entry":
  724. if ($log->cmid) {
  725. //Get the new_id of the glossary_entry (to recode the info and url field)
  726. $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$log->info);
  727. if ($ent) {
  728. $log->url = "view.php?id=".$log->cmid."&mode=entry&hook=".$ent->new_id;
  729. $log->info = $ent->new_id;
  730. $status = true;
  731. }
  732. }
  733. break;
  734. case "update entry":
  735. if ($log->cmid) {
  736. //Get the new_id of the glossary_entry (to recode the info and url field)
  737. $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$log->info);
  738. if ($ent) {
  739. $log->url = "view.php?id=".$log->cmid."&mode=entry&hook=".$ent->new_id;
  740. $log->info = $ent->new_id;
  741. $status = true;
  742. }
  743. }
  744. break;
  745. case "delete entry":
  746. if ($log->cmid) {
  747. //Get the new_id of the glossary_entry (to recode the info and url field)
  748. $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$log->info);
  749. if ($ent) {
  750. $log->url = "view.php?id=".$log->cmid."&mode=entry&hook=".$ent->new_id;
  751. $log->info = $ent->new_id;
  752. $status = true;
  753. }
  754. }
  755. break;
  756. case "approve entry":
  757. if ($log->cmid) {
  758. //Get the new_id of the glossary_entry (to recode the info and url field)
  759. $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$log->info);
  760. if ($ent) {
  761. $log->url = "showentry.php?id=".$log->cmid."&eid=".$ent->new_id;
  762. $log->info = $ent->new_id;
  763. $status = true;
  764. }
  765. }
  766. break;
  767. case "view entry":
  768. if ($log->cmid) {
  769. //Get the new_id of the glossary_entry (to recode the info and url field)
  770. $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$log->info);
  771. if ($ent) {
  772. $log->url = "showentry.php?&eid=".$ent->new_id;
  773. $log->info = $ent->new_id;
  774. $status = true;
  775. }
  776. }
  777. break;
  778. case "add comment":
  779. if ($log->cmid) {
  780. //Extract the entryid from the url field
  781. $entid = substr(strrchr($log->url,"="),1);
  782. //Get the new_id of the glossary_entry (to recode the url field)
  783. $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$entid);
  784. //Get the new_id of the glossary_comment (to recode the info field)
  785. $com = backup_getid($restore->backup_unique_code,"glossary_comments",$log->info);
  786. if ($ent and $com) {
  787. $log->url = "comments.php?id=".$log->cmid."&eid=".$ent->new_id;
  788. $log->info = $com->new_id;
  789. $status = true;
  790. }
  791. }
  792. break;
  793. case "update comment":
  794. if ($log->cmid) {
  795. //Extract the entryid from the url field
  796. $entid = substr(strrchr($log->url,"="),1);
  797. //Get the new_id of the glossary_entry (to recode the url field)
  798. $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$entid);
  799. //Get the new_id of the glossary_comment (to recode the info field)
  800. $com = backup_getid($restore->backup_unique_code,"glossary_comments",$log->info);
  801. if ($ent and $com) {
  802. $log->url = "comments.php?id=".$log->cmid."&eid=".$ent->new_id;
  803. $log->info = $com->new_id;
  804. $status = true;
  805. }
  806. }
  807. break;
  808. case "delete comment":
  809. if ($log->cmid) {
  810. //Extract the entryid from the url field
  811. $entid = substr(strrchr($log->url,"="),1);
  812. //Get the new_id of the glossary_entry (to recode the url field)
  813. $ent = backup_getid($restore->backup_unique_code,"glossary_entries",$entid);
  814. //Get the new_id of the glossary_comment (to recode the info field)
  815. $com = backup_getid($restore->backup_unique_code,"glossary_comments",$log->info);
  816. if ($ent and $com) {
  817. $log->url = "comments.php?id=".$log->cmid."&eid=".$ent->new_id;
  818. $log->info = $com->new_id;
  819. $status = true;
  820. }
  821. }
  822. break;
  823. default:
  824. if (!defined('RESTORE_SILENTLY')) {
  825. echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
  826. }
  827. break;
  828. }
  829. if ($status) {
  830. $status = $log;
  831. }
  832. return $status;
  833. }
  834. ?>