PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/forum/restorelib.php

https://bitbucket.org/ceu/moodle_demo
PHP | 1203 lines | 806 code | 131 blank | 266 comment | 177 complexity | 4b99c9a8f2e8441d2573656a26554628 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php //$Id: restorelib.php,v 1.60.4.6 2011/02/23 20:23:53 moodlerobot Exp $
  2. //This php script contains all the stuff to backup/restore
  3. //forum mods
  4. //This is the "graphical" structure of the forum mod:
  5. //
  6. // forum
  7. // (CL,pk->id)
  8. // |
  9. // ---------------------------------------------------
  10. // | |
  11. // subscriptions forum_discussions
  12. //(UL,pk->id, fk->forum) ---------------(UL,pk->id, fk->forum)
  13. // | |
  14. // | |
  15. // | |
  16. // | forum_posts
  17. // |-------------(UL,pk->id,fk->discussion,
  18. // | nt->parent,files)
  19. // | |
  20. // | |
  21. // | |
  22. // forum_read forum_ratings
  23. // (UL,pk->id,fk->post (UL,pk->id,fk->post)
  24. //
  25. // Meaning: pk->primary key field of the table
  26. // fk->foreign key to link with parent
  27. // nt->nested field (recursive data)
  28. // CL->course level info
  29. // UL->user level info
  30. // files->table may have files)
  31. //
  32. //-----------------------------------------------------------
  33. function forum_restore_mods($mod,$restore) {
  34. global $CFG,$db;
  35. $status = true;
  36. //Get record from backup_ids
  37. $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
  38. if ($data) {
  39. //Now get completed xmlized object
  40. $info = $data->info;
  41. //if necessary, write to restorelog and adjust date/time fields
  42. if ($restore->course_startdateoffset) {
  43. restore_log_date_changes('Forum', $restore, $info['MOD']['#'], array('ASSESSTIMESTART', 'ASSESSTIMEFINISH'));
  44. }
  45. //traverse_xmlize($info); //Debug
  46. //print_object ($GLOBALS['traverse_array']); //Debug
  47. //$GLOBALS['traverse_array']=""; //Debug
  48. //Now, build the FORUM record structure
  49. $forum->course = $restore->course_id;
  50. $forum->type = backup_todb($info['MOD']['#']['TYPE']['0']['#']);
  51. $forum->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
  52. $forum->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
  53. // These get dropped in Moodle 1.7 when the new Roles System gets
  54. // set up. Therefore they might or not be there depending on whether
  55. // we are restoring a 1.6+ forum or a 1.7 or later forum backup.
  56. if (isset($info['MOD']['#']['OPEN']['0']['#'])) {
  57. $forum->open = backup_todb($info['MOD']['#']['OPEN']['0']['#']);
  58. }
  59. if (isset($info['MOD']['#']['ASSESSPUBLIC']['0']['#'])) {
  60. $forum->assesspublic = backup_todb($info['MOD']['#']['ASSESSPUBLIC']['0']['#']);
  61. }
  62. $forum->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
  63. $forum->assesstimestart = backup_todb($info['MOD']['#']['ASSESSTIMESTART']['0']['#']);
  64. $forum->assesstimefinish = backup_todb($info['MOD']['#']['ASSESSTIMEFINISH']['0']['#']);
  65. $forum->maxbytes = backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']);
  66. $forum->scale = backup_todb($info['MOD']['#']['SCALE']['0']['#']);
  67. $forum->forcesubscribe = backup_todb($info['MOD']['#']['FORCESUBSCRIBE']['0']['#']);
  68. $forum->trackingtype = backup_todb($info['MOD']['#']['TRACKINGTYPE']['0']['#']);
  69. $forum->rsstype = backup_todb($info['MOD']['#']['RSSTYPE']['0']['#']);
  70. $forum->rssarticles = backup_todb($info['MOD']['#']['RSSARTICLES']['0']['#']);
  71. $forum->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
  72. $forum->warnafter = isset($info['MOD']['#']['WARNAFTER']['0']['#'])?backup_todb($info['MOD']['#']['WARNAFTER']['0']['#']):'';
  73. $forum->blockafter = isset($info['MOD']['#']['BLOCKAFTER']['0']['#'])?backup_todb($info['MOD']['#']['BLOCKAFTER']['0']['#']):'';
  74. $forum->blockperiod = isset($info['MOD']['#']['BLOCKPERIOD']['0']['#'])?backup_todb($info['MOD']['#']['BLOCKPERIOD']['0']['#']):'';
  75. //We have to recode the scale field if it's <0 (positive is a grade, not a scale)
  76. if ($forum->scale < 0) {
  77. $scale = backup_getid($restore->backup_unique_code,"scale",abs($forum->scale));
  78. if ($scale) {
  79. $forum->scale = -($scale->new_id);
  80. }
  81. }
  82. $newid = insert_record("forum", $forum);
  83. //Do some output
  84. if (!defined('RESTORE_SILENTLY')) {
  85. echo "<li>".get_string("modulename","forum")." \"".format_string(stripslashes($forum->name),true)."\"</li>";
  86. }
  87. backup_flush(300);
  88. if ($newid) {
  89. //We have the newid, update backup_ids
  90. backup_putid($restore->backup_unique_code,$mod->modtype,
  91. $mod->id, $newid);
  92. $forum->id = $newid;
  93. //Now check if want to restore user data and do it.
  94. if (restore_userdata_selected($restore,'forum',$mod->id)) {
  95. //Restore forum_subscriptions
  96. $status = forum_subscriptions_restore_mods ($newid,$info,$restore);
  97. if ($status) {
  98. //Restore forum_discussions
  99. $status = forum_discussions_restore_mods ($newid,$info,$restore);
  100. }
  101. if ($status) {
  102. //Restore forum_read
  103. $status = forum_read_restore_mods ($newid,$info,$restore);
  104. }
  105. }
  106. // If forum type is single, just recreate the initial discussion/post automatically
  107. // if it hasn't been created still (because no user data was selected on backup or
  108. // restore.
  109. if ($forum->type == 'single' && !record_exists('forum_discussions', 'forum', $newid)) {
  110. //Load forum/lib.php
  111. require_once ($CFG->dirroot.'/mod/forum/lib.php');
  112. // Calculate the default format
  113. if (can_use_html_editor()) {
  114. $defaultformat = FORMAT_HTML;
  115. } else {
  116. $defaultformat = FORMAT_MOODLE;
  117. }
  118. //Create discussion/post data
  119. $sd = new stdClass;
  120. $sd->course = $forum->course;
  121. $sd->forum = $newid;
  122. $sd->name = $forum->name;
  123. $sd->intro = $forum->intro;
  124. $sd->assessed = $forum->assessed;
  125. $sd->format = $defaultformat;
  126. $sd->mailnow = false;
  127. //Insert dicussion/post data
  128. $sdid = forum_add_discussion($sd, $sd->intro, $forum);
  129. //Now, mark the initial post of the discussion as mailed!
  130. if ($sdid) {
  131. set_field ('forum_posts','mailed', '1', 'discussion', $sdid);
  132. }
  133. }
  134. } else {
  135. $status = false;
  136. }
  137. // If the backup contained $forum->open and $forum->assesspublic,
  138. // we need to convert the forum to use Roles. It means the backup
  139. // was made pre Moodle 1.7.
  140. if (isset($forum->open) && isset($forum->assesspublic)) {
  141. $forummod = get_record('modules', 'name', 'forum');
  142. if (!$teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) {
  143. notice('Default teacher role was not found. Roles and permissions '.
  144. 'for all your forums will have to be manually set.');
  145. }
  146. if (!$studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
  147. notice('Default student role was not found. Roles and permissions '.
  148. 'for all your forums will have to be manually set.');
  149. }
  150. if (!$guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
  151. notice('Default guest role was not found. Roles and permissions '.
  152. 'for teacher forums will have to be manually set.');
  153. }
  154. require_once($CFG->dirroot.'/mod/forum/lib.php');
  155. forum_convert_to_roles($forum, $forummod->id,
  156. $teacherroles, $studentroles, $guestroles,
  157. $restore->mods['forum']->instances[$mod->id]->restored_as_course_module);
  158. }
  159. } else {
  160. $status = false;
  161. }
  162. return $status;
  163. }
  164. //This function restores the forum_subscriptions
  165. function forum_subscriptions_restore_mods($forum_id,$info,$restore) {
  166. global $CFG;
  167. $status = true;
  168. //Get the discussions array
  169. $subscriptions = array();
  170. if (isset($info['MOD']['#']['SUBSCRIPTIONS']['0']['#']['SUBSCRIPTION'])) {
  171. $subscriptions = $info['MOD']['#']['SUBSCRIPTIONS']['0']['#']['SUBSCRIPTION'];
  172. }
  173. //Iterate over subscriptions
  174. for($i = 0; $i < sizeof($subscriptions); $i++) {
  175. $sus_info = $subscriptions[$i];
  176. //traverse_xmlize($sus_info); //Debug
  177. //print_object ($GLOBALS['traverse_array']); //Debug
  178. //$GLOBALS['traverse_array']=""; //Debug
  179. //We'll need this later!!
  180. $oldid = backup_todb($sus_info['#']['ID']['0']['#']);
  181. $olduserid = backup_todb($sus_info['#']['USERID']['0']['#']);
  182. //Now, build the FORUM_SUBSCRIPTIONS record structure
  183. $subscription->forum = $forum_id;
  184. $subscription->userid = backup_todb($sus_info['#']['USERID']['0']['#']);
  185. //We have to recode the userid field
  186. $user = backup_getid($restore->backup_unique_code,"user",$subscription->userid);
  187. if ($user) {
  188. $subscription->userid = $user->new_id;
  189. }
  190. //The structure is equal to the db, so insert the forum_subscription
  191. $newid = insert_record ("forum_subscriptions",$subscription);
  192. //Do some output
  193. if (($i+1) % 50 == 0) {
  194. if (!defined('RESTORE_SILENTLY')) {
  195. echo ".";
  196. if (($i+1) % 1000 == 0) {
  197. echo "<br />";
  198. }
  199. }
  200. backup_flush(300);
  201. }
  202. if ($newid) {
  203. //We have the newid, update backup_ids
  204. backup_putid($restore->backup_unique_code,"forum_subscriptions",$oldid,
  205. $newid);
  206. } else {
  207. $status = false;
  208. }
  209. }
  210. return $status;
  211. }
  212. //This function restores the forum_discussions
  213. function forum_discussions_restore_mods($forum_id,$info,$restore) {
  214. global $CFG;
  215. $status = true;
  216. //Get the discussions array
  217. $discussions = array();
  218. if (!empty($info['MOD']['#']['DISCUSSIONS']['0']['#']['DISCUSSION'])) {
  219. $discussions = $info['MOD']['#']['DISCUSSIONS']['0']['#']['DISCUSSION'];
  220. }
  221. //Iterate over discussions
  222. for($i = 0; $i < sizeof($discussions); $i++) {
  223. $dis_info = $discussions[$i];
  224. //traverse_xmlize($dis_info); //Debug
  225. //print_object ($GLOBALS['traverse_array']); //Debug
  226. //$GLOBALS['traverse_array']=""; //Debug
  227. //We'll need this later!!
  228. $oldid = backup_todb($dis_info['#']['ID']['0']['#']);
  229. $olduserid = backup_todb($dis_info['#']['USERID']['0']['#']);
  230. //Now, build the FORUM_DISCUSSIONS record structure
  231. $discussion = new object();
  232. $discussion->forum = $forum_id;
  233. $discussion->course = $restore->course_id;
  234. $discussion->name = backup_todb($dis_info['#']['NAME']['0']['#']);
  235. $discussion->firstpost = backup_todb($dis_info['#']['FIRSTPOST']['0']['#']);
  236. $discussion->userid = backup_todb($dis_info['#']['USERID']['0']['#']);
  237. $discussion->groupid = backup_todb($dis_info['#']['GROUPID']['0']['#']);
  238. $discussion->assessed = backup_todb($dis_info['#']['ASSESSED']['0']['#']);
  239. $discussion->timemodified = backup_todb($dis_info['#']['TIMEMODIFIED']['0']['#']);
  240. $discussion->timemodified += $restore->course_startdateoffset;
  241. $discussion->usermodified = backup_todb($dis_info['#']['USERMODIFIED']['0']['#']);
  242. $discussion->timestart = backup_todb($dis_info['#']['TIMESTART']['0']['#']);
  243. $discussion->timestart += $restore->course_startdateoffset;
  244. $discussion->timeend = backup_todb($dis_info['#']['TIMEEND']['0']['#']);
  245. $discussion->timeend += $restore->course_startdateoffset;
  246. //We have to recode the userid field
  247. $user = backup_getid($restore->backup_unique_code,"user",$discussion->userid);
  248. if ($user) {
  249. $discussion->userid = $user->new_id;
  250. }
  251. //We have to recode the groupid field
  252. $group = restore_group_getid($restore, $discussion->groupid);
  253. if ($group) {
  254. $discussion->groupid = $group->new_id;
  255. }
  256. //We have to recode the usermodified field
  257. $user = backup_getid($restore->backup_unique_code,"user",$discussion->usermodified);
  258. if ($user) {
  259. $discussion->usermodified = $user->new_id;
  260. }
  261. //The structure is equal to the db, so insert the forum_discussions
  262. $newid = insert_record ("forum_discussions",$discussion);
  263. //Do some output
  264. if (($i+1) % 50 == 0) {
  265. if (!defined('RESTORE_SILENTLY')) {
  266. echo ".";
  267. if (($i+1) % 1000 == 0) {
  268. echo "<br />";
  269. }
  270. }
  271. backup_flush(300);
  272. }
  273. if ($newid) {
  274. //We have the newid, update backup_ids
  275. backup_putid($restore->backup_unique_code,"forum_discussions",$oldid,
  276. $newid);
  277. //Restore forum_posts
  278. $status = forum_posts_restore_mods ($forum_id,$newid,$dis_info,$restore);
  279. //Now recalculate firstpost field
  280. $old_firstpost = $discussion->firstpost;
  281. //Get its new post_id from backup_ids table
  282. $rec = backup_getid($restore->backup_unique_code,"forum_posts",$old_firstpost);
  283. if ($rec) {
  284. //Put its new firstpost
  285. $discussion->firstpost = $rec->new_id;
  286. if ($post = get_record("forum_posts", "id", $discussion->firstpost)) {
  287. $discussion->userid = $post->userid;
  288. }
  289. } else {
  290. $discussion->firstpost = 0;
  291. $discussion->userid = 0;
  292. }
  293. //Create temp discussion record
  294. $temp_discussion->id = $newid;
  295. $temp_discussion->firstpost = $discussion->firstpost;
  296. $temp_discussion->userid = $discussion->userid;
  297. //Update discussion (only firstpost and userid will be changed)
  298. $status = update_record("forum_discussions",$temp_discussion);
  299. //echo "Updated firstpost ".$old_firstpost." to ".$temp_discussion->firstpost."<br />"; //Debug
  300. } else {
  301. $status = false;
  302. }
  303. }
  304. return $status;
  305. }
  306. //This function restores the forum_read
  307. function forum_read_restore_mods($forum_id,$info,$restore) {
  308. global $CFG;
  309. $status = true;
  310. //Get the read array
  311. $readposts = array();
  312. if (isset($info['MOD']['#']['READPOSTS']['0']['#']['READ'])) {
  313. $readposts = $info['MOD']['#']['READPOSTS']['0']['#']['READ'];
  314. }
  315. //Iterate over readposts
  316. for($i = 0; $i < sizeof($readposts); $i++) {
  317. $rea_info = $readposts[$i];
  318. //traverse_xmlize($rea_info); //Debug
  319. //print_object ($GLOBALS['traverse_array']); //Debug
  320. //$GLOBALS['traverse_array']=""; //Debug
  321. //We'll need this later!!
  322. $oldid = backup_todb($rea_info['#']['ID']['0']['#']);
  323. //Now, build the FORUM_READ record structure
  324. $read->forumid = $forum_id;
  325. $read->userid = backup_todb($rea_info['#']['USERID']['0']['#']);
  326. $read->discussionid = backup_todb($rea_info['#']['DISCUSSIONID']['0']['#']);
  327. $read->postid = backup_todb($rea_info['#']['POSTID']['0']['#']);
  328. $read->firstread = backup_todb($rea_info['#']['FIRSTREAD']['0']['#']);
  329. $read->lastread = backup_todb($rea_info['#']['LASTREAD']['0']['#']);
  330. //We have to recode the userid field
  331. $user = backup_getid($restore->backup_unique_code,"user",$read->userid);
  332. if ($user) {
  333. $read->userid = $user->new_id;
  334. } else {
  335. continue; // Skip this forum_read record
  336. }
  337. //We have to recode the discussionid field
  338. $discussion = backup_getid($restore->backup_unique_code,"forum_discussions",$read->discussionid);
  339. if ($discussion) {
  340. $read->discussionid = $discussion->new_id;
  341. } else {
  342. continue; // Skip this forum_read record
  343. }
  344. //We have to recode the postid field
  345. $post = backup_getid($restore->backup_unique_code,"forum_posts",$read->postid);
  346. if ($post) {
  347. $read->postid = $post->new_id;
  348. } else {
  349. continue; // Skip this forum_read record
  350. }
  351. // Arrived here, the structure is equal to the db, so insert the forum_read
  352. $newid = 0;
  353. $newid = insert_record ("forum_read",$read);
  354. //Do some output
  355. if (($i+1) % 50 == 0) {
  356. if (!defined('RESTORE_SILENTLY')) {
  357. echo ".";
  358. if (($i+1) % 1000 == 0) {
  359. echo "<br />";
  360. }
  361. }
  362. backup_flush(300);
  363. }
  364. if ($newid) {
  365. //We have the newid, update backup_ids
  366. backup_putid($restore->backup_unique_code,"forum_read",$oldid,
  367. $newid);
  368. } else {
  369. $status = false;
  370. }
  371. }
  372. return $status;
  373. }
  374. //This function restores the forum_posts
  375. function forum_posts_restore_mods($new_forum_id,$discussion_id,$info,$restore) {
  376. global $CFG;
  377. $status = true;
  378. //Get the posts array
  379. $posts = $info['#']['POSTS']['0']['#']['POST'];
  380. //Iterate over posts
  381. for($i = 0; $i < sizeof($posts); $i++) {
  382. $pos_info = $posts[$i];
  383. //traverse_xmlize($pos_info); //Debug
  384. //print_object ($GLOBALS['traverse_array']); //Debug
  385. //$GLOBALS['traverse_array']=""; //Debug
  386. //We'll need this later!!
  387. $oldid = backup_todb($pos_info['#']['ID']['0']['#']);
  388. $olduserid = backup_todb($pos_info['#']['USERID']['0']['#']);
  389. //Now, build the FORUM_POSTS record structure
  390. $post->discussion = $discussion_id;
  391. $post->parent = backup_todb($pos_info['#']['PARENT']['0']['#']);
  392. $post->userid = backup_todb($pos_info['#']['USERID']['0']['#']);
  393. $post->created = backup_todb($pos_info['#']['CREATED']['0']['#']);
  394. $post->created += $restore->course_startdateoffset;
  395. $post->modified = backup_todb($pos_info['#']['MODIFIED']['0']['#']);
  396. $post->modified += $restore->course_startdateoffset;
  397. $post->mailed = backup_todb($pos_info['#']['MAILED']['0']['#']);
  398. $post->subject = backup_todb($pos_info['#']['SUBJECT']['0']['#']);
  399. $post->message = backup_todb($pos_info['#']['MESSAGE']['0']['#']);
  400. $post->format = backup_todb($pos_info['#']['FORMAT']['0']['#']);
  401. $post->attachment = backup_todb($pos_info['#']['ATTACHMENT']['0']['#']);
  402. $post->totalscore = backup_todb($pos_info['#']['TOTALSCORE']['0']['#']);
  403. $post->mailnow = backup_todb($pos_info['#']['MAILNOW']['0']['#']);
  404. //We have to recode the userid field
  405. $user = backup_getid($restore->backup_unique_code,"user",$post->userid);
  406. if ($user) {
  407. $post->userid = $user->new_id;
  408. }
  409. //The structure is equal to the db, so insert the forum_posts
  410. $newid = insert_record ("forum_posts",$post);
  411. //Do some output
  412. if (($i+1) % 50 == 0) {
  413. if (!defined('RESTORE_SILENTLY')) {
  414. echo ".";
  415. if (($i+1) % 1000 == 0) {
  416. echo "<br />";
  417. }
  418. }
  419. backup_flush(300);
  420. }
  421. if ($newid) {
  422. //We have the newid, update backup_ids
  423. backup_putid($restore->backup_unique_code,"forum_posts",$oldid,
  424. $newid);
  425. //Get old forum id from backup_ids
  426. $rec = get_record("backup_ids","backup_code",$restore->backup_unique_code,
  427. "table_name","forum",
  428. "new_id",$new_forum_id);
  429. //Now copy moddata associated files
  430. $status = forum_restore_files ($rec->old_id, $new_forum_id,
  431. $oldid, $newid, $restore);
  432. //Now restore post ratings
  433. $status = forum_ratings_restore_mods($newid,$pos_info,$restore);
  434. } else {
  435. $status = false;
  436. }
  437. }
  438. //Now we get every post in this discussion_id and recalculate its parent post
  439. $posts = get_records ("forum_posts","discussion",$discussion_id);
  440. if ($posts) {
  441. //Iterate over each post
  442. foreach ($posts as $post) {
  443. //Get its parent
  444. $old_parent = $post->parent;
  445. //Get its new post_id from backup_ids table
  446. $rec = backup_getid($restore->backup_unique_code,"forum_posts",$old_parent);
  447. if ($rec) {
  448. //Put its new parent
  449. $post->parent = $rec->new_id;
  450. } else {
  451. $post->parent = 0;
  452. }
  453. //Create temp post record
  454. $temp_post->id = $post->id;
  455. $temp_post->parent = $post->parent;
  456. //echo "Updated parent ".$old_parent." to ".$temp_post->parent."<br />"; //Debug
  457. //Update post (only parent will be changed)
  458. $status = update_record("forum_posts",$temp_post);
  459. }
  460. }
  461. return $status;
  462. }
  463. //This function copies the forum related info from backup temp dir to course moddata folder,
  464. //creating it if needed and recoding everything (forum id and post id)
  465. function forum_restore_files ($oldforid, $newforid, $oldpostid, $newpostid, $restore) {
  466. global $CFG;
  467. $status = true;
  468. $todo = false;
  469. $moddata_path = "";
  470. $forum_path = "";
  471. $temp_path = "";
  472. //First, we check to "course_id" exists and create is as necessary
  473. //in CFG->dataroot
  474. $dest_dir = $CFG->dataroot."/".$restore->course_id;
  475. $status = check_dir_exists($dest_dir,true);
  476. //First, locate course's moddata directory
  477. $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
  478. //Check it exists and create it
  479. $status = check_dir_exists($moddata_path,true);
  480. //Now, locate forum directory
  481. if ($status) {
  482. $forum_path = $moddata_path."/forum";
  483. //Check it exists and create it
  484. $status = check_dir_exists($forum_path,true);
  485. }
  486. //Now locate the temp dir we are restoring from
  487. if ($status) {
  488. $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
  489. "/moddata/forum/".$oldforid."/".$oldpostid;
  490. //Check it exists
  491. if (is_dir($temp_path)) {
  492. $todo = true;
  493. }
  494. }
  495. //If todo, we create the neccesary dirs in course moddata/forum
  496. if ($status and $todo) {
  497. //First this forum id
  498. $this_forum_path = $forum_path."/".$newforid;
  499. $status = check_dir_exists($this_forum_path,true);
  500. //Now this post id
  501. $post_forum_path = $this_forum_path."/".$newpostid;
  502. //And now, copy temp_path to post_forum_path
  503. $status = backup_copy_file($temp_path, $post_forum_path);
  504. }
  505. return $status;
  506. }
  507. //This function restores the forum_ratings
  508. function forum_ratings_restore_mods($new_post_id,$info,$restore) {
  509. global $CFG;
  510. $status = true;
  511. //Get the ratings array
  512. $ratings = array();
  513. if (isset($info['#']['RATINGS']['0']['#']['RATING'])) {
  514. $ratings = $info['#']['RATINGS']['0']['#']['RATING'];
  515. }
  516. //Iterate over ratings
  517. for($i = 0; $i < sizeof($ratings); $i++) {
  518. $rat_info = $ratings[$i];
  519. //traverse_xmlize($rat_info); //Debug
  520. //print_object ($GLOBALS['traverse_array']); //Debug
  521. //$GLOBALS['traverse_array']=""; //Debug
  522. //We'll need this later!!
  523. $oldid = backup_todb($rat_info['#']['ID']['0']['#']);
  524. $olduserid = backup_todb($rat_info['#']['USERID']['0']['#']);
  525. //Now, build the FORM_RATINGS record structure
  526. $rating->post = $new_post_id;
  527. $rating->userid = backup_todb($rat_info['#']['USERID']['0']['#']);
  528. $rating->time = backup_todb($rat_info['#']['TIME']['0']['#']);
  529. $rating->rating = backup_todb($rat_info['#']['POST_RATING']['0']['#']);
  530. //We have to recode the userid field
  531. $user = backup_getid($restore->backup_unique_code,"user",$rating->userid);
  532. if ($user) {
  533. $rating->userid = $user->new_id;
  534. }
  535. //The structure is equal to the db, so insert the forum_ratings
  536. $newid = insert_record ("forum_ratings",$rating);
  537. //Do some output
  538. if (($i+1) % 50 == 0) {
  539. if (!defined('RESTORE_SILENTLY')) {
  540. echo ".";
  541. if (($i+1) % 1000 == 0) {
  542. echo "<br />";
  543. }
  544. }
  545. backup_flush(300);
  546. }
  547. if ($newid) {
  548. //We have the newid, update backup_ids
  549. backup_putid($restore->backup_unique_code,"forum_ratings",$oldid,
  550. $newid);
  551. } else {
  552. $status = false;
  553. }
  554. }
  555. return $status;
  556. }
  557. //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
  558. //some texts in the module
  559. function forum_restore_wiki2markdown ($restore) {
  560. global $CFG;
  561. $status = true;
  562. //Convert forum_posts->message
  563. if ($records = get_records_sql ("SELECT p.id, p.message, p.format
  564. FROM {$CFG->prefix}forum_posts p,
  565. {$CFG->prefix}forum_discussions d,
  566. {$CFG->prefix}forum f,
  567. {$CFG->prefix}backup_ids b
  568. WHERE d.id = p.discussion AND
  569. f.id = d.forum AND
  570. f.course = $restore->course_id AND
  571. p.format = ".FORMAT_WIKI. " AND
  572. b.backup_code = $restore->backup_unique_code AND
  573. b.table_name = 'forum_posts' AND
  574. b.new_id = p.id")) {
  575. foreach ($records as $record) {
  576. //Rebuild wiki links
  577. $record->message = restore_decode_wiki_content($record->message, $restore);
  578. //Convert to Markdown
  579. $wtm = new WikiToMarkdown();
  580. $record->message = $wtm->convert($record->message, $restore->course_id);
  581. $record->format = FORMAT_MARKDOWN;
  582. $status = update_record('forum_posts', addslashes_object($record));
  583. //Do some output
  584. $i++;
  585. if (($i+1) % 1 == 0) {
  586. if (!defined('RESTORE_SILENTLY')) {
  587. echo ".";
  588. if (($i+1) % 20 == 0) {
  589. echo "<br />";
  590. }
  591. }
  592. backup_flush(300);
  593. }
  594. }
  595. }
  596. return $status;
  597. }
  598. //This function returns a log record with all the necessay transformations
  599. //done. It's used by restore_log_module() to restore modules log.
  600. function forum_restore_logs($restore,$log) {
  601. $status = false;
  602. //Depending of the action, we recode different things
  603. switch ($log->action) {
  604. case "add":
  605. if ($log->cmid) {
  606. //Get the new_id of the module (to recode the info field)
  607. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  608. if ($mod) {
  609. $log->url = "view.php?id=".$log->cmid;
  610. $log->info = $mod->new_id;
  611. $status = true;
  612. }
  613. }
  614. break;
  615. case "mark read":
  616. if ($log->cmid) {
  617. //Get the new_id of the module (to recode the url and info fields)
  618. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  619. if ($mod) {
  620. $log->url = "view.php?f=".$mod->new_id;
  621. $log->info = $mod->new_id;
  622. $status = true;
  623. }
  624. }
  625. break;
  626. case "start tracking":
  627. if ($log->cmid) {
  628. //Get the new_id of the module (to recode the url and info fields)
  629. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  630. if ($mod) {
  631. $log->url = "view.php?f=".$mod->new_id;
  632. $log->info = $mod->new_id;
  633. $status = true;
  634. }
  635. }
  636. break;
  637. case "stop tracking":
  638. if ($log->cmid) {
  639. //Get the new_id of the module (to recode the url and info fields)
  640. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  641. if ($mod) {
  642. $log->url = "view.php?f=".$mod->new_id;
  643. $log->info = $mod->new_id;
  644. $status = true;
  645. }
  646. }
  647. break;
  648. case "update":
  649. if ($log->cmid) {
  650. //Get the new_id of the module (to recode the info field)
  651. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  652. if ($mod) {
  653. $log->url = "view.php?id=".$log->cmid;
  654. $log->info = $mod->new_id;
  655. $status = true;
  656. }
  657. }
  658. break;
  659. case "view forum":
  660. if ($log->cmid) {
  661. //Get the new_id of the module (to recode the info field)
  662. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  663. if ($mod) {
  664. $log->url = "view.php?id=".$log->cmid;
  665. $log->info = $mod->new_id;
  666. $status = true;
  667. }
  668. }
  669. break;
  670. case "view forums":
  671. $log->url = "index.php?id=".$log->course;
  672. $status = true;
  673. break;
  674. case "subscribeall":
  675. $log->url = "index.php?id=".$log->course;
  676. $status = true;
  677. break;
  678. case "unsubscribeall":
  679. $log->url = "index.php?id=".$log->course;
  680. $status = true;
  681. break;
  682. case "subscribe":
  683. if ($log->cmid) {
  684. //Get the new_id of the module (to recode the info and url field)
  685. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  686. if ($mod) {
  687. $log->url = "view.php?f=".$mod->new_id;
  688. $log->info = $mod->new_id;
  689. $status = true;
  690. }
  691. }
  692. break;
  693. case "view subscriber":
  694. case "view subscribers":
  695. if ($log->cmid) {
  696. //Get the new_id of the module (to recode the info and field)
  697. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  698. if ($mod) {
  699. $log->url = "subscribers.php?id=".$mod->new_id;
  700. $log->info = $mod->new_id;
  701. $status = true;
  702. }
  703. }
  704. break;
  705. case "unsubscribe":
  706. if ($log->cmid) {
  707. //Get the new_id of the module (to recode the info and url field)
  708. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  709. if ($mod) {
  710. $log->url = "view.php?f=".$mod->new_id;
  711. $log->info = $mod->new_id;
  712. $status = true;
  713. }
  714. }
  715. break;
  716. case "add discussion":
  717. if ($log->cmid) {
  718. //Get the new_id of the discussion (to recode the info and url field)
  719. $dis = backup_getid($restore->backup_unique_code,"forum_discussions",$log->info);
  720. if ($dis) {
  721. $log->url = "discuss.php?d=".$dis->new_id;
  722. $log->info = $dis->new_id;
  723. $status = true;
  724. }
  725. }
  726. break;
  727. case "view discussion":
  728. if ($log->cmid) {
  729. //Get the new_id of the discussion (to recode the info and url field)
  730. $dis = backup_getid($restore->backup_unique_code,"forum_discussions",$log->info);
  731. if ($dis) {
  732. $log->url = "discuss.php?d=".$dis->new_id;
  733. $log->info = $dis->new_id;
  734. $status = true;
  735. }
  736. }
  737. break;
  738. case "move discussion":
  739. if ($log->cmid) {
  740. //Get the new_id of the discussion (to recode the info and url field)
  741. $dis = backup_getid($restore->backup_unique_code,"forum_discussions",$log->info);
  742. if ($dis) {
  743. $log->url = "discuss.php?d=".$dis->new_id;
  744. $log->info = $dis->new_id;
  745. $status = true;
  746. }
  747. }
  748. break;
  749. case "delete discussi":
  750. case "delete discussion":
  751. if ($log->cmid) {
  752. //Get the new_id of the module (to recode the info field)
  753. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  754. if ($mod) {
  755. $log->url = "view.php?id=".$log->cmid;
  756. $log->info = $mod->new_id;
  757. $status = true;
  758. }
  759. }
  760. break;
  761. case "add post":
  762. if ($log->cmid) {
  763. //Get the new_id of the post (to recode the url and info field)
  764. $pos = backup_getid($restore->backup_unique_code,"forum_posts",$log->info);
  765. if ($pos) {
  766. //Get the post record from database
  767. $dbpos = get_record("forum_posts","id","$pos->new_id");
  768. if ($dbpos) {
  769. $log->url = "discuss.php?d=".$dbpos->discussion."&parent=".$pos->new_id;
  770. $log->info = $pos->new_id;
  771. $status = true;
  772. }
  773. }
  774. }
  775. break;
  776. case "prune post":
  777. if ($log->cmid) {
  778. //Get the new_id of the post (to recode the url and info field)
  779. $pos = backup_getid($restore->backup_unique_code,"forum_posts",$log->info);
  780. if ($pos) {
  781. //Get the post record from database
  782. $dbpos = get_record("forum_posts","id","$pos->new_id");
  783. if ($dbpos) {
  784. $log->url = "discuss.php?d=".$dbpos->discussion;
  785. $log->info = $pos->new_id;
  786. $status = true;
  787. }
  788. }
  789. }
  790. break;
  791. case "update post":
  792. if ($log->cmid) {
  793. //Get the new_id of the post (to recode the url and info field)
  794. $pos = backup_getid($restore->backup_unique_code,"forum_posts",$log->info);
  795. if ($pos) {
  796. //Get the post record from database
  797. $dbpos = get_record("forum_posts","id","$pos->new_id");
  798. if ($dbpos) {
  799. $log->url = "discuss.php?d=".$dbpos->discussion."&parent=".$pos->new_id;
  800. $log->info = $pos->new_id;
  801. $status = true;
  802. }
  803. }
  804. }
  805. break;
  806. case "delete post":
  807. if ($log->cmid) {
  808. //Extract the discussion id from the url field
  809. $disid = substr(strrchr($log->url,"="),1);
  810. //Get the new_id of the discussion (to recode the url field)
  811. $dis = backup_getid($restore->backup_unique_code,"quiz_discussions",$disid);
  812. if ($dis) {
  813. $log->url = "discuss.php?d=".$dis->new_id;
  814. $status = true;
  815. }
  816. }
  817. break;
  818. case "user report":
  819. //Extract mode from url
  820. $mode = substr(strrchr($log->url,"="),1);
  821. //Get new user id
  822. if ($use = backup_getid($restore->backup_unique_code, 'user', $log->info)) {
  823. $log->url = 'user.php?course=' . $log->course . '&id=' . $use->new_id . '&mode=' . $mode;
  824. $log->info = $use->new_id;
  825. $status = true;
  826. }
  827. break;
  828. case "search":
  829. $log->url = "search.php?id=".$log->course."&search=".urlencode($log->info);
  830. $status = true;
  831. break;
  832. default:
  833. if (!defined('RESTORE_SILENTLY')) {
  834. echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
  835. }
  836. break;
  837. }
  838. if ($status) {
  839. $status = $log;
  840. }
  841. return $status;
  842. }
  843. //Return a content decoded to support interactivities linking. Every module
  844. //should have its own. They are called automatically from
  845. //forum_decode_content_links_caller() function in each module
  846. //in the restore process
  847. function forum_decode_content_links ($content,$restore) {
  848. global $CFG;
  849. $result = $content;
  850. //Link to the list of forums
  851. $searchstring='/\$@(FORUMINDEX)\*([0-9]+)@\$/';
  852. //We look for it
  853. preg_match_all($searchstring,$content,$foundset);
  854. //If found, then we are going to look for its new id (in backup tables)
  855. if ($foundset[0]) {
  856. //print_object($foundset); //Debug
  857. //Iterate over foundset[2]. They are the old_ids
  858. foreach($foundset[2] as $old_id) {
  859. //We get the needed variables here (course id)
  860. $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
  861. //Personalize the searchstring
  862. $searchstring='/\$@(FORUMINDEX)\*('.$old_id.')@\$/';
  863. //If it is a link to this course, update the link to its new location
  864. if($rec->new_id) {
  865. //Now replace it
  866. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/index.php?id='.$rec->new_id,$result);
  867. } else {
  868. //It's a foreign link so leave it as original
  869. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/forum/index.php?id='.$old_id,$result);
  870. }
  871. }
  872. }
  873. //Link to forum view by moduleid
  874. $searchstring='/\$@(FORUMVIEWBYID)\*([0-9]+)@\$/';
  875. //We look for it
  876. preg_match_all($searchstring,$result,$foundset);
  877. //If found, then we are going to look for its new id (in backup tables)
  878. if ($foundset[0]) {
  879. //print_object($foundset); //Debug
  880. //Iterate over foundset[2]. They are the old_ids
  881. foreach($foundset[2] as $old_id) {
  882. //We get the needed variables here (course_modules id)
  883. $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
  884. //Personalize the searchstring
  885. $searchstring='/\$@(FORUMVIEWBYID)\*('.$old_id.')@\$/';
  886. //If it is a link to this course, update the link to its new location
  887. if($rec->new_id) {
  888. //Now replace it
  889. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/view.php?id='.$rec->new_id,$result);
  890. } else {
  891. //It's a foreign link so leave it as original
  892. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/forum/view.php?id='.$old_id,$result);
  893. }
  894. }
  895. }
  896. //Link to forum view by forumid
  897. $searchstring='/\$@(FORUMVIEWBYF)\*([0-9]+)@\$/';
  898. //We look for it
  899. preg_match_all($searchstring,$result,$foundset);
  900. //If found, then we are going to look for its new id (in backup tables)
  901. if ($foundset[0]) {
  902. //print_object($foundset); //Debug
  903. //Iterate over foundset[2]. They are the old_ids
  904. foreach($foundset[2] as $old_id) {
  905. //We get the needed variables here (forum id)
  906. $rec = backup_getid($restore->backup_unique_code,"forum",$old_id);
  907. //Personalize the searchstring
  908. $searchstring='/\$@(FORUMVIEWBYF)\*('.$old_id.')@\$/';
  909. //If it is a link to this course, update the link to its new location
  910. if($rec->new_id) {
  911. //Now replace it
  912. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/view.php?f='.$rec->new_id,$result);
  913. } else {
  914. //It's a foreign link so leave it as original
  915. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/forum/view.php?f='.$old_id,$result);
  916. }
  917. }
  918. }
  919. //Link to forum discussion by discussionid
  920. $searchstring='/\$@(FORUMDISCUSSIONVIEW)\*([0-9]+)@\$/';
  921. //We look for it
  922. preg_match_all($searchstring,$result,$foundset);
  923. //If found, then we are going to look for its new id (in backup tables)
  924. if ($foundset[0]) {
  925. //print_object($foundset); //Debug
  926. //Iterate over foundset[2]. They are the old_ids
  927. foreach($foundset[2] as $old_id) {
  928. //We get the needed variables here (discussion id)
  929. $rec = backup_getid($restore->backup_unique_code,"forum_discussions",$old_id);
  930. //Personalize the searchstring
  931. $searchstring='/\$@(FORUMDISCUSSIONVIEW)\*('.$old_id.')@\$/';
  932. //If it is a link to this course, update the link to its new location
  933. if($rec->new_id) {
  934. //Now replace it
  935. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/discuss.php?d='.$rec->new_id,$result);
  936. } else {
  937. //It's a foreign link so leave it as original
  938. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/forum/discuss.php?d='.$old_id,$result);
  939. }
  940. }
  941. }
  942. //Link to forum discussion with parent syntax
  943. $searchstring='/\$@(FORUMDISCUSSIONVIEWPARENT)\*([0-9]+)\*([0-9]+)@\$/';
  944. //We look for it
  945. preg_match_all($searchstring,$result,$foundset);
  946. //If found, then we are going to look for its new id (in backup tables)
  947. if ($foundset[0]) {
  948. //print_object($foundset); //Debug
  949. //Iterate over foundset[2] and foundset[3]. They are the old_ids
  950. foreach($foundset[2] as $key => $old_id) {
  951. $old_id2 = $foundset[3][$key];
  952. //We get the needed variables here (discussion id and post id)
  953. $rec = backup_getid($restore->backup_unique_code,"forum_discussions",$old_id);
  954. $rec2 = backup_getid($restore->backup_unique_code,"forum_posts",$old_id2);
  955. //Personalize the searchstring
  956. $searchstring='/\$@(FORUMDISCUSSIONVIEWPARENT)\*('.$old_id.')\*('.$old_id2.')@\$/';
  957. //If it is a link to this course, update the link to its new location
  958. if($rec->new_id && $rec2->new_id) {
  959. //Now replace it
  960. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/discuss.php?d='.$rec->new_id.'&parent='.$rec2->new_id,$result);
  961. } else {
  962. //It's a foreign link so leave it as original
  963. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/forum/discuss.php?d='.$old_id.'&parent='.$old_id2,$result);

Large files files are truncated, but you can click here to view the full file