restorelib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * This script contains all the functionality allowing SLOODLE data in Moodle to be restored from a backup.
  4. *
  5. * @package sloodle
  6. *
  7. */
  8. require_once($CFG->dirroot.'/mod/sloodle/init.php');
  9. require_once(SLOODLE_LIBROOT.'/modules.php');
  10. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  11. /**
  12. * Restore everything from the given backup.
  13. */
  14. function sloodle_restore_mods($mod, $restore)
  15. {
  16. global $CFG;
  17. $status = true;
  18. //Get record from backup_ids
  19. $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
  20. if ($data) {
  21. // Get the XML backup data as an array
  22. $info = $data->info;
  23. // Build our SLOODLE DB record
  24. $sloodle = new object();
  25. $sloodle->course = $restore->course_id;
  26. $sloodle->type = backup_todb($info['MOD']['#']['SUBTYPE']['0']['#']);
  27. $sloodle->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
  28. $sloodle->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
  29. $sloodle->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
  30. $sloodle->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
  31. $newid = sloodle_insert_record("sloodle", $sloodle);
  32. // Inform the user what we are restoring
  33. if (!defined('RESTORE_SILENTLY')) {
  34. echo "<li>".get_string("modulename", "sloodle")." \"".format_string(stripslashes($sloodle->name),true)."\"</li>";
  35. }
  36. backup_flush(300);
  37. if ($newid) {
  38. // We have the newid, update backup_ids
  39. backup_putid($restore->backup_unique_code, $mod->modtype, $mod->id, $newid);
  40. // Should we restore userdata?
  41. $includeuserdata = restore_userdata_selected($restore, 'sloodle', $mod->id);
  42. // Attempt to get a SloodleModule object for this module sub-type
  43. $dummysession = new SloodleSession(false); // We need to provide this to keep the module happy!
  44. $moduleobj = sloodle_load_module($sloodle->type, $dummysession);
  45. if ($moduleobj != false) {
  46. // Attempt to restore this module's secondary data
  47. if (!$moduleobj->restore($newid, $info['MOD']['#']['SECONDARYDATA']['0']['#'], $includeuserdata)) $status = false;
  48. } else {
  49. echo "<li>Failed to fully restore SLOODLE module type {$sloodle->type}. This type may not be available on your installation.</li>";
  50. }
  51. } else {
  52. $status = false;
  53. }
  54. } else {
  55. $status = false;
  56. }
  57. return $status;
  58. }
  59. /**
  60. * Return content decoded to support interactivities linking. Every module
  61. * should have its own. They are called automatically from
  62. * sloodle_decode_content_links_caller() function in each module
  63. * in the restore process.
  64. * @todo This probably needs to be expanded to account for non-standard view URLs (notably those starting with parameter "_type")
  65. */
  66. function sloodle_decode_content_links ($content,$restore) {
  67. global $CFG;
  68. $result = $content;
  69. //Link to the list of chats
  70. $searchstring='/\$@(SLOODLEINDEX)\*([0-9]+)@\$/';
  71. //We look for it
  72. preg_match_all($searchstring,$content,$foundset);
  73. //If found, then we are going to look for its new id (in backup tables)
  74. if ($foundset[0]) {
  75. //print_object($foundset); //Debug
  76. //Iterate over foundset[2]. They are the old_ids
  77. foreach($foundset[2] as $old_id) {
  78. //We get the needed variables here (course id)
  79. $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
  80. //Personalize the searchstring
  81. $searchstring='/\$@(SLOODLEINDEX)\*('.$old_id.')@\$/';
  82. //If it is a link to this course, update the link to its new location
  83. if($rec->new_id) {
  84. //Now replace it
  85. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/sloodle/index.php?id='.$rec->new_id,$result);
  86. } else {
  87. //It's a foreign link so leave it as original
  88. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/chat/index.php?id='.$old_id,$result);
  89. }
  90. }
  91. }
  92. //Link to chat view by moduleid
  93. $searchstring='/\$@(SLOODLEVIEWBYID)\*([0-9]+)@\$/';
  94. //We look for it
  95. preg_match_all($searchstring,$result,$foundset);
  96. //If found, then we are going to look for its new id (in backup tables)
  97. if ($foundset[0]) {
  98. //print_object($foundset); //Debug
  99. //Iterate over foundset[2]. They are the old_ids
  100. foreach($foundset[2] as $old_id) {
  101. //We get the needed variables here (course_modules id)
  102. $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
  103. //Personalize the searchstring
  104. $searchstring='/\$@(SLOODLEVIEWBYID)\*('.$old_id.')@\$/';
  105. //If it is a link to this course, update the link to its new location
  106. if($rec->new_id) {
  107. //Now replace it
  108. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/sloodle/view.php?id='.$rec->new_id,$result);
  109. } else {
  110. //It's a foreign link so leave it as original
  111. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/sloodle/view.php?id='.$old_id,$result);
  112. }
  113. }
  114. }
  115. return $result;
  116. }
  117. /**
  118. * This function makes all the necessary calls to xxxx_decode_content_links()
  119. * function in each module, passing them the desired contents to be decoded
  120. * from backup format to destination site/course in order to mantain inter-activities
  121. * working in the backup/restore process. It's called from restore_decode_content_links()
  122. * function in restore process
  123. */
  124. function sloodle_decode_content_links_caller($restore)
  125. {
  126. global $CFG;
  127. $status = true;
  128. $sloodles = sloodle_get_records_sql_params("
  129. SELECT s.id, s.intro
  130. FROM {$CFG->prefix}sloodle s
  131. WHERE s.course = ?
  132. ", array($restore->course_id) );
  133. if ($sloodles) {
  134. $i = 0; //Counter to send some output to the browser to avoid timeouts
  135. foreach ($sloodles as $sloodle) {
  136. //Increment counter
  137. $i++;
  138. $content = $sloodle->intro;
  139. $result = restore_decode_content_links_worker($content,$restore);
  140. if ($result != $content) {
  141. //Update record
  142. $sloodle->intro = $result;
  143. $status = sloodle_update_record("sloodle", $sloodle);
  144. if (debugging()) {
  145. if (!defined('RESTORE_SILENTLY')) {
  146. echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
  147. }
  148. }
  149. }
  150. //Do some output
  151. if (($i+1) % 5 == 0) {
  152. if (!defined('RESTORE_SILENTLY')) {
  153. echo ".";
  154. if (($i+1) % 100 == 0) {
  155. echo "<br />";
  156. }
  157. }
  158. backup_flush(300);
  159. }
  160. }
  161. }
  162. return $status;
  163. }
  164. /**
  165. * This function returns a log record with all the necessay transformations done.
  166. * It's used by restore_log_module() to restore modules log.
  167. * This has not been modified for SLOODLE -- it comes from the chat module.
  168. * SLOODLE doesn't really use logs correctly all the time, so this may not work anyway.
  169. */
  170. function sloodle_restore_logs($restore,$log)
  171. {
  172. $status = false;
  173. //Depending of the action, we recode different things
  174. switch ($log->action) {
  175. case "add":
  176. if ($log->cmid) {
  177. //Get the new_id of the module (to recode the info field)
  178. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  179. if ($mod) {
  180. $log->url = "view.php?id=".$log->cmid;
  181. $log->info = $mod->new_id;
  182. $status = true;
  183. }
  184. }
  185. break;
  186. case "update":
  187. if ($log->cmid) {
  188. //Get the new_id of the module (to recode the info field)
  189. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  190. if ($mod) {
  191. $log->url = "view.php?id=".$log->cmid;
  192. $log->info = $mod->new_id;
  193. $status = true;
  194. }
  195. }
  196. break;
  197. case "talk":
  198. if ($log->cmid) {
  199. //Get the new_id of the module (to recode the info field)
  200. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  201. if ($mod) {
  202. $log->url = "view.php?id=".$log->cmid;
  203. $log->info = $mod->new_id;
  204. $status = true;
  205. }
  206. }
  207. break;
  208. case "view":
  209. if ($log->cmid) {
  210. //Get the new_id of the module (to recode the info field)
  211. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  212. if ($mod) {
  213. $log->url = "view.php?id=".$log->cmid;
  214. $log->info = $mod->new_id;
  215. $status = true;
  216. }
  217. }
  218. break;
  219. case "view all":
  220. $log->url = "index.php?id=".$log->course;
  221. $status = true;
  222. break;
  223. case "report":
  224. if ($log->cmid) {
  225. //Get the new_id of the module (to recode the info field)
  226. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  227. if ($mod) {
  228. $log->url = "report.php?id=".$log->cmid;
  229. $log->info = $mod->new_id;
  230. $status = true;
  231. }
  232. }
  233. break;
  234. default:
  235. if (!defined('RESTORE_SILENTLY')) {
  236. echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
  237. }
  238. break;
  239. }
  240. if ($status) {
  241. $status = $log;
  242. }
  243. return $status;
  244. }
  245. ?>