PageRenderTime 91ms CodeModel.GetById 48ms RepoModel.GetById 14ms app.codeStats 0ms

/mod/data/restorelib.php

https://bitbucket.org/ceu/moodle_demo
PHP | 766 lines | 494 code | 117 blank | 155 comment | 119 complexity | 91b400904731caf9f1b351ef08ccaca7 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  1. <?php // $Id
  2. //This php script contains all the stuff to backup/restore data mod
  3. //This is the "graphical" structure of the data mod:
  4. //
  5. // data
  6. // (CL,pk->id)
  7. // |
  8. // |
  9. // |
  10. // ---------------------------------------------------------------------------------
  11. // | |
  12. //data_records (UL,pk->id, fk->data) data_fields (pk->id, fk->data)
  13. // | |
  14. // | |
  15. // ----------------------------------------------------------------------------- |
  16. // | | | |
  17. //data_ratings(fk->recordid, pk->id) data_comments (fk->recordid, pk->id) | |
  18. // data_content(pk->id, fk->recordid, fk->fieldid)
  19. //
  20. //
  21. //
  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)
  28. //
  29. //-----------------------------------------------------------
  30. //Backup data files because we've selected to backup user info
  31. //and files are user info's level
  32. $fieldids = array(); //array in the format of $fieldids[$oldid]=$newid. This is needed because of double dependencies of multiple tables.
  33. //Return a content encoded to support interactivities linking. Every module
  34. function data_restore_mods($mod,$restore) {
  35. global $CFG;
  36. $status = true;
  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('Database', $restore, $info['MOD']['#'], array('TIMEAVAILABLEFROM', 'TIMEAVAILABLETO','TIMEVIEWFROM', 'TIMEVIEWTO'));
  44. }
  45. //traverse_xmlize($info); //Debug
  46. //print_object ($GLOBALS['traverse_array']); //Debug
  47. //$GLOBALS['traverse_array']=""; //Debug
  48. $database->course = $restore->course_id;
  49. $database->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
  50. $database->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
  51. // Only relevant for restoring backups from 1.6 in a 1.7 install.
  52. if (isset($info['MOD']['#']['RATINGS']['0']['#'])) {
  53. $database->ratings = backup_todb($info['MOD']['#']['RATINGS']['0']['#']);
  54. }
  55. $database->comments = backup_todb($info['MOD']['#']['COMMENTS']['0']['#']);
  56. $database->timeavailablefrom = backup_todb($info['MOD']['#']['TIMEAVAILABLEFROM']['0']['#']);
  57. $database->timeavailableto = backup_todb($info['MOD']['#']['TIMEAVAILABLETO']['0']['#']);
  58. $database->timeviewfrom = backup_todb($info['MOD']['#']['TIMEVIEWFROM']['0']['#']);
  59. $database->timeviewto = backup_todb($info['MOD']['#']['TIMEVIEWTO']['0']['#']);
  60. // Only relevant for restoring backups from 1.6 in a 1.7 install.
  61. if (isset($info['MOD']['#']['PARTICIPANTS']['0']['#'])) {
  62. $database->participants = backup_todb($info['MOD']['#']['PARTICIPANTS']['0']['#']);
  63. }
  64. $database->requiredentries = backup_todb($info['MOD']['#']['REQUIREDENTRIES']['0']['#']);
  65. $database->requiredentriestoview = backup_todb($info['MOD']['#']['REQUIREDENTRIESTOVIEW']['0']['#']);
  66. $database->maxentries = backup_todb($info['MOD']['#']['MAXENTRIES']['0']['#']);
  67. $database->rssarticles = backup_todb($info['MOD']['#']['RSSARTICLES']['0']['#']);
  68. $database->singletemplate = backup_todb($info['MOD']['#']['SINGLETEMPLATE']['0']['#']);
  69. $database->listtemplate = backup_todb($info['MOD']['#']['LISTTEMPLATE']['0']['#']);
  70. $database->listtemplateheader = backup_todb($info['MOD']['#']['LISTTEMPLATEHEADER']['0']['#']);
  71. $database->listtemplatefooter = backup_todb($info['MOD']['#']['LISTTEMPLATEFOOTER']['0']['#']);
  72. $database->addtemplate = backup_todb($info['MOD']['#']['ADDTEMPLATE']['0']['#']);
  73. $database->rsstemplate = backup_todb($info['MOD']['#']['RSSTEMPLATE']['0']['#']);
  74. $database->rsstitletemplate = backup_todb($info['MOD']['#']['RSSTITLETEMPLATE']['0']['#']);
  75. $database->csstemplate = backup_todb($info['MOD']['#']['CSSTEMPLATE']['0']['#']);
  76. $database->jstemplate = backup_todb($info['MOD']['#']['JSTEMPLATE']['0']['#']);
  77. $database->asearchtemplate = backup_todb($info['MOD']['#']['ASEARCHTEMPLATE']['0']['#']);
  78. $database->approval = backup_todb($info['MOD']['#']['APPROVAL']['0']['#']);
  79. $database->scale = backup_todb($info['MOD']['#']['SCALE']['0']['#']);
  80. $database->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
  81. // Only relevant for restoring backups from 1.6 in a 1.7 install.
  82. if (isset($info['MOD']['#']['ASSESSPUBLIC']['0']['#'])) {
  83. $database->assesspublic = backup_todb($info['MOD']['#']['ASSESSPUBLIC']['0']['#']);
  84. }
  85. $database->defaultsort = backup_todb($info['MOD']['#']['DEFAULTSORT']['0']['#']);
  86. $database->defaultsortdir = backup_todb($info['MOD']['#']['DEFAULTSORTDIR']['0']['#']);
  87. $database->editany = backup_todb($info['MOD']['#']['EDITANY']['0']['#']);
  88. $database->notification = backup_todb($info['MOD']['#']['NOTIFICATION']['0']['#']);
  89. //fix related to MDL-24470: if someone restore a backup previous to this fix it would fail
  90. //if notification = NULL as the column can not be NULL anymore
  91. if (empty($database->notification)) {
  92. $database->notification = 0;
  93. }
  94. // We have to recode the scale field if it's <0 (positive is a grade, not a scale)
  95. if ($database->scale < 0) {
  96. $scale = backup_getid($restore->backup_unique_code, 'scale', abs($database->scale));
  97. if ($scale) {
  98. $database->scale = -($scale->new_id);
  99. }
  100. }
  101. $newid = insert_record ('data', $database);
  102. //Do some output
  103. if (!defined('RESTORE_SILENTLY')) {
  104. echo "<li>".get_string("modulename","data")." \"".format_string(stripslashes($database->name),true)."\"</li>";
  105. }
  106. if ($newid) {
  107. //We have the newid, update backup_ids
  108. backup_putid($restore->backup_unique_code,$mod->modtype,
  109. $mod->id, $newid);
  110. //Now check if want to restore user data and do it.
  111. if (function_exists('restore_userdata_selected')) {
  112. // Moodle 1.6
  113. $restore_userdata_selected = restore_userdata_selected($restore, 'data', $mod->id);
  114. } else {
  115. // Moodle 1.5
  116. $restore_userdata_selected = $restore->mods['data']->userinfo;
  117. }
  118. global $fieldids;
  119. //Restore data_fields first!!! need to hold an array of [oldid]=>newid due to double dependencies
  120. $status = $status and data_fields_restore_mods ($mod->id, $newid, $info, $restore);
  121. // now use the new field in the defaultsort
  122. $newdefaultsort = empty($fieldids[$database->defaultsort]) ? 0 : $fieldids[$database->defaultsort];
  123. set_field('data', 'defaultsort', $newdefaultsort, 'id', $newid);
  124. if ($restore_userdata_selected) {
  125. $status = $status and data_records_restore_mods ($mod->id, $newid, $info, $restore);
  126. }
  127. // If the backup contained $data->participants, $data->assesspublic
  128. // and $data->groupmode, we need to convert the data to use Roles.
  129. // It means the backup was made pre Moodle 1.7. We check the
  130. // backup_version to make sure.
  131. if (isset($database->participants) && isset($database->assesspublic)) {
  132. if (!$teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) {
  133. notice('Default teacher role was not found. Roles and permissions '.
  134. 'for your database modules will have to be manually set.');
  135. }
  136. if (!$studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
  137. notice('Default student role was not found. Roles and permissions '.
  138. 'for all your database modules will have to be manually set.');
  139. }
  140. if (!$guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
  141. notice('Default guest role was not found. Roles and permissions '.
  142. 'for all your database modules will have to be manually set.');
  143. }
  144. require_once($CFG->dirroot.'/mod/data/lib.php');
  145. data_convert_to_roles($database, $teacherroles, $studentroles,
  146. $restore->mods['data']->instances[$mod->id]->restored_as_course_module);
  147. }
  148. } else {
  149. $status = false;
  150. }
  151. } else {
  152. $status = false;
  153. }
  154. return $status;
  155. }
  156. function data_fields_restore_mods ($old_data_id, $new_data_id, $info, $restore) {
  157. global $CFG, $fieldids;
  158. $fields = $info['MOD']['#']['FIELDS']['0']['#']['FIELD'];
  159. for ($i = 0; $i < sizeof($fields); $i++) {
  160. $fie_info = $fields[$i];
  161. $oldid = backup_todb($fie_info['#']['ID']['0']['#']);
  162. $field -> dataid = $new_data_id;
  163. $field -> type = backup_todb($fie_info['#']['TYPE']['0']['#']);
  164. $field -> name = backup_todb($fie_info['#']['NAME']['0']['#']);
  165. $field -> description = backup_todb($fie_info['#']['DESCRIPTION']['0']['#']);
  166. $field -> param1 = backup_todb($fie_info['#']['PARAM1']['0']['#']);
  167. $field -> param2 = backup_todb($fie_info['#']['PARAM2']['0']['#']);
  168. $field -> param3 = backup_todb($fie_info['#']['PARAM3']['0']['#']);
  169. $field -> param4 = backup_todb($fie_info['#']['PARAM4']['0']['#']);
  170. $field -> param5 = backup_todb($fie_info['#']['PARAM5']['0']['#']);
  171. $field -> param6 = backup_todb($fie_info['#']['PARAM6']['0']['#']);
  172. $field -> param7 = backup_todb($fie_info['#']['PARAM7']['0']['#']);
  173. $field -> param8 = backup_todb($fie_info['#']['PARAM8']['0']['#']);
  174. $field -> param9 = backup_todb($fie_info['#']['PARAM9']['0']['#']);
  175. $field -> param10 = backup_todb($fie_info['#']['PARAM10']['0']['#']);
  176. $newid = insert_record ("data_fields",$field);
  177. $fieldids[$oldid] = $newid; //so we can use them in sub tables that depends on both fieldid and recordid
  178. //Do some output
  179. if (($i+1) % 50 == 0) {
  180. if (!defined('RESTORE_SILENTLY')) {
  181. echo ".";
  182. if (($i+1) % 1000 == 0) {
  183. echo "<br />";
  184. }
  185. }
  186. backup_flush(300);
  187. }
  188. if ($newid) {
  189. //We have the newid, update backup_ids
  190. $status = backup_putid($restore->backup_unique_code,"data_fields",$oldid, $newid);
  191. } else {
  192. $status = false;
  193. }
  194. }
  195. return $status;
  196. }
  197. function data_records_restore_mods ($old_data_id, $new_data_id, $info, $restore) {
  198. global $CFG, $fieldids;
  199. $status = true;
  200. $records = isset($info['MOD']['#']['RECORDS']['0']['#']['RECORD']) ? $info['MOD']['#']['RECORDS']['0']['#']['RECORD'] : array();
  201. if (empty($records)) { // no records to restore
  202. return true;
  203. }
  204. for ($i = 0; $i < sizeof($records); $i++) {
  205. $rec_info = $records[$i];
  206. $oldid = backup_todb($rec_info['#']['ID']['0']['#']);
  207. $record = new object();
  208. $record -> dataid = $new_data_id;
  209. $record -> userid = backup_todb($rec_info['#']['USERID']['0']['#']);
  210. $record -> groupid = backup_todb($rec_info['#']['GROUPID']['0']['#']);
  211. $record -> timecreated = backup_todb($rec_info['#']['TIMECREATED']['0']['#']);
  212. $record -> timemodified = backup_todb($rec_info['#']['TIMEMODIFIED']['0']['#']);
  213. $record -> approved = backup_todb($rec_info['#']['APPROVED']['0']['#']);
  214. $user = backup_getid($restore->backup_unique_code,"user",$record->userid);
  215. $group= restore_group_getid($restore, $record->groupid);
  216. if ($user) {
  217. $record->userid = $user->new_id;
  218. }
  219. if ($group) {
  220. $record->groupid= $group->new_id;
  221. }
  222. $newid = insert_record ("data_records",$record);
  223. //Do some output
  224. if (($i+1) % 50 == 0) {
  225. if (!defined('RESTORE_SILENTLY')) {
  226. echo ".";
  227. if (($i+1) % 1000 == 0) {
  228. echo "<br />";
  229. }
  230. }
  231. backup_flush(300);
  232. }
  233. if ($newid) {
  234. //We have the newid, update backup_ids
  235. $status = $status and backup_putid($restore->backup_unique_code,"data_records",$oldid, $newid);
  236. $status = $status and data_content_restore_mods ($oldid, $newid, $old_data_id, $new_data_id, $rec_info, $restore);
  237. $status = $status and data_ratings_restore_mods ($oldid, $newid, $info, $rec_info);
  238. $status = $status and data_comments_restore_mods ($oldid, $newid, $info, $rec_info);
  239. } else {
  240. $status = false;
  241. }
  242. }
  243. return $status;
  244. }
  245. function data_content_restore_mods ($old_record_id, $new_record_id, $old_data_id, $new_data_id, $recinfo, $restore) {
  246. global $CFG, $fieldids;
  247. $status = true;
  248. $contents = $recinfo['#']['CONTENTS']['0']['#']['CONTENT'];
  249. for ($i = 0; $i < sizeof($contents); $i++) {
  250. $con_info = $contents[$i];
  251. $oldid = backup_todb($con_info['#']['ID']['0']['#']);
  252. $oldfieldid = backup_todb($con_info['#']['FIELDID']['0']['#']);
  253. $oldrecordid = backup_todb($con_info['#']['RECORDID']['0']['#']);
  254. $content -> recordid = $new_record_id;
  255. $content -> fieldid = $fieldids[$oldfieldid];
  256. $content -> content = backup_todb($con_info['#']['CONTENT']['0']['#']);
  257. $content -> content1 = backup_todb($con_info['#']['CONTENT1']['0']['#']);
  258. $content -> content2 = backup_todb($con_info['#']['CONTENT2']['0']['#']);
  259. $content -> content3 = backup_todb($con_info['#']['CONTENT3']['0']['#']);
  260. $content -> content4 = backup_todb($con_info['#']['CONTENT4']['0']['#']);
  261. $newid = insert_record ("data_content",$content);
  262. //Do some output
  263. if (($i+1) % 50 == 0) {
  264. if (!defined('RESTORE_SILENTLY')) {
  265. echo ".";
  266. if (($i+1) % 1000 == 0) {
  267. echo "<br />";
  268. }
  269. }
  270. backup_flush(300);
  271. }
  272. if ($newid) {
  273. //We have the newid, update backup_ids
  274. $status = $status and data_restore_files ($old_data_id, $new_data_id, $oldfieldid, $content->fieldid, $oldrecordid, $content->recordid, $recinfo, $restore);
  275. $status = $status and backup_putid($restore->backup_unique_code,"data_content",$oldid, $newid);
  276. } else {
  277. $status = false;
  278. }
  279. }
  280. return $status;
  281. }
  282. function data_restore_files ($old_data_id, $new_data_id, $old_field_id, $new_field_id, $old_record_id, $new_record_id, $recinfo, $restore) {
  283. global $CFG, $db;
  284. $status = true;
  285. $todo = false;
  286. $moddata_path = "";
  287. $data_path = "";
  288. $temp_path = "";
  289. //First, we check to "course_id" exists and create is as necessary
  290. //in CFG->dataroot
  291. $dest_dir = $CFG->dataroot."/".$restore->course_id;
  292. $status = check_dir_exists($dest_dir,true);
  293. //Now, locate course's moddata directory
  294. $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
  295. //Check it exists and create it
  296. $status = check_dir_exists($moddata_path,true);
  297. //Now, locate data directory
  298. if ($status) {
  299. $data_path = $moddata_path."/data";
  300. //Check it exists and create it
  301. $status = check_dir_exists($data_path,true);
  302. }
  303. //Now locate the temp dir we are gong to restore
  304. if ($status) {
  305. $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
  306. "/moddata/data/".$old_data_id."/".$old_field_id."/".$old_record_id;
  307. $todo = check_dir_exists($temp_path);
  308. }
  309. //If todo, we create the neccesary dirs in course moddata/data
  310. if ($status and $todo) {
  311. //First this data id
  312. $this_data_path = $data_path."/".$new_data_id;
  313. $status = check_dir_exists($this_data_path,true);
  314. //Now this user id
  315. $this_field_path = $this_data_path."/".$new_field_id;
  316. $status = check_dir_exists($this_field_path,true);
  317. $this_record_path = $this_field_path = $this_field_path."/".$new_record_id;
  318. $status = check_dir_exists($this_record_path,true);
  319. //And now, copy temp_path to user_data_path
  320. $status = @backup_copy_file($temp_path, $this_record_path);
  321. }
  322. return $status;
  323. }
  324. function data_ratings_restore_mods ($oldid, $newid, $info, $rec_info) {
  325. global $CFG;
  326. $status = true;
  327. $ratings= isset($rec_info['#']['RATINGS']['0']['#']['RATING']) ? $rec_info['#']['RATINGS']['0']['#']['RATING'] : array();
  328. if (empty($ratings)) { // no ratings to restore
  329. return true;
  330. }
  331. for ($i = 0; $i < sizeof($ratings); $i++) {
  332. $rat_info = $ratings[$i];
  333. $rating->recordid = $newid;
  334. $rating->userid = backup_todb($rat_info['#']['USERID']['0']['#']);
  335. $rating->rating = backup_todb($rat_info['#']['RATING']['0']['#']);
  336. // Need to remap the user
  337. $user = backup_getid($restore->backup_unique_code,"user",$rating->userid);
  338. if ($user) {
  339. $rating->userid = $user->new_id;
  340. }
  341. if (! insert_record ("data_ratings",$rating)) {
  342. $status = false;
  343. }
  344. }
  345. return $status;
  346. }
  347. function data_comments_restore_mods ($oldid, $newid, $info, $rec_info) {
  348. global $CFG;
  349. $status = true;
  350. $comments= isset($rec_info['#']['COMMENTS']['0']['#']['COMMENT']) ? $rec_info['#']['COMMENTS']['0']['#']['COMMENT'] : array();
  351. if (empty($comments)) { // no comments to restore
  352. return true;
  353. }
  354. for ($i = 0; $i < sizeof($comments); $i++) {
  355. $com_info = $comments[$i];
  356. $comment->recordid = $newid;
  357. $comment->userid = backup_todb($com_info['#']['USERID']['0']['#']);
  358. $comment->content = backup_todb($com_info['#']['CONTENT']['0']['#']);
  359. $comment->format = backup_todb($com_info['#']['FORMAT']['0']['#']);
  360. $comment->created = backup_todb($com_info['#']['CREATED']['0']['#']);
  361. $comment->modified = backup_todb($com_info['#']['MODIFIED']['0']['#']);
  362. // Need to remap the user
  363. $user = backup_getid($restore->backup_unique_code,"user",$comment->userid);
  364. if ($user) {
  365. $comment->userid = $user->new_id;
  366. }
  367. if (! insert_record ("data_comments",$comment)) {
  368. $status = false;
  369. }
  370. }
  371. return $status;
  372. }
  373. /**
  374. * Returns a content decoded to support interactivities linking. Every module
  375. * should have its own. They are called automatically from
  376. * xxxx_decode_content_links_caller() function in each module
  377. * in the restore process.
  378. *
  379. * @param string $content the content to be decoded
  380. * @param object $restore the preferences used in restore
  381. * @return string the decoded string
  382. */
  383. function data_decode_content_links ($content,$restore) {
  384. global $CFG;
  385. $result = $content;
  386. /// Link to the list of datas
  387. $searchstring='/\$@(DATAINDEX)\*([0-9]+)@\$/';
  388. /// We look for it
  389. preg_match_all($searchstring,$content,$foundset);
  390. /// If found, then we are going to look for its new id (in backup tables)
  391. if ($foundset[0]) {
  392. /// print_object($foundset); //Debug
  393. /// Iterate over foundset[2]. They are the old_ids
  394. foreach($foundset[2] as $old_id) {
  395. /// We get the needed variables here (course id)
  396. $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
  397. /// Personalize the searchstring
  398. $searchstring='/\$@(DATAINDEX)\*('.$old_id.')@\$/';
  399. /// If it is a link to this course, update the link to its new location
  400. if($rec->new_id) {
  401. /// Now replace it
  402. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/data/index.php?id='.$rec->new_id,$result);
  403. } else {
  404. /// It's a foreign link so leave it as original
  405. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/data/index.php?id='.$old_id,$result);
  406. }
  407. }
  408. }
  409. /// Link to data view by moduleid
  410. $searchstring='/\$@(DATAVIEWBYID)\*([0-9]+)@\$/';
  411. /// We look for it
  412. preg_match_all($searchstring,$result,$foundset);
  413. /// If found, then we are going to look for its new id (in backup tables)
  414. if ($foundset[0]) {
  415. /// print_object($foundset); //Debug
  416. /// Iterate over foundset[2]. They are the old_ids
  417. foreach($foundset[2] as $old_id) {
  418. /// We get the needed variables here (course_modules id)
  419. $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
  420. /// Personalize the searchstring
  421. $searchstring='/\$@(DATAVIEWBYID)\*('.$old_id.')@\$/';
  422. /// If it is a link to this course, update the link to its new location
  423. if($rec->new_id) {
  424. /// Now replace it
  425. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/data/view.php?id='.$rec->new_id,$result);
  426. } else {
  427. /// It's a foreign link so leave it as original
  428. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/data/view.php?id='.$old_id,$result);
  429. }
  430. }
  431. }
  432. /// Link to data view by dataid
  433. $searchstring='/\$@(DATAVIEWBYD)\*([0-9]+)@\$/';
  434. /// We look for it
  435. preg_match_all($searchstring,$result,$foundset);
  436. /// If found, then we are going to look for its new id (in backup tables)
  437. if ($foundset[0]) {
  438. /// print_object($foundset); //Debug
  439. /// Iterate over foundset[2]. They are the old_ids
  440. foreach($foundset[2] as $old_id) {
  441. /// We get the needed variables here (data id)
  442. $rec = backup_getid($restore->backup_unique_code,"data",$old_id);
  443. /// Personalize the searchstring
  444. $searchstring='/\$@(DATAVIEWBYD)\*('.$old_id.')@\$/';
  445. /// If it is a link to this course, update the link to its new location
  446. if($rec->new_id) {
  447. /// Now replace it
  448. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/data/view.php?d='.$rec->new_id,$result);
  449. } else {
  450. /// It's a foreign link so leave it as original
  451. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/data/view.php?d='.$old_id,$result);
  452. }
  453. }
  454. }
  455. /// Link to data record (element)
  456. $searchstring='/\$@(DATAVIEWRECORD)\*([0-9]+)\*([0-9]+)@\$/';
  457. /// We look for it
  458. preg_match_all($searchstring,$result,$foundset);
  459. /// If found, then we are going to look for its new id (in backup tables)
  460. if ($foundset[0]) {
  461. /// print_object($foundset); //Debug
  462. /// Iterate over foundset[2] and foundset[3]. They are the old_ids
  463. foreach($foundset[2] as $key => $old_id) {
  464. $old_id2 = $foundset[3][$key];
  465. /// We get the needed variables here (data id and record id)
  466. $rec = backup_getid($restore->backup_unique_code,"data",$old_id);
  467. $rec2 = backup_getid($restore->backup_unique_code,"data_records",$old_id2);
  468. /// Personalize the searchstring
  469. $searchstring='/\$@(DATAVIEWRECORD)\*('.$old_id.')\*('.$old_id2.')@\$/';
  470. /// If it is a link to this course, update the link to its new location
  471. if($rec->new_id && $rec2->new_id) {
  472. /// Now replace it
  473. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/data/view.php?d='.$rec->new_id.'&amp;rid='.$rec2->new_id,$result);
  474. } else {
  475. /// It's a foreign link so leave it as original
  476. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/data/view.php?d='.$old_id.'&amp;rid='.$old_id2,$result);
  477. }
  478. }
  479. }
  480. return $result;
  481. }
  482. /**
  483. * This function makes all the necessary calls to xxxx_decode_content_links()
  484. * function in each module, passing them the desired contents to be decoded
  485. * from backup format to destination site/course in order to mantain inter-activities
  486. * working in the backup/restore process. It's called from restore_decode_content_links()
  487. * function in restore process
  488. *
  489. * @param object $restore the preferences used in restore
  490. * @return boolean status of the execution
  491. */
  492. function data_decode_content_links_caller($restore) {
  493. global $CFG;
  494. $status = true;
  495. /// Process every DATA (intro, all HTML templates) in the course
  496. /// Supported fields for main table:
  497. $supportedfields = array('intro','singletemplate','listtemplate',
  498. 'listtemplateheader','addtemplate','rsstemplate','rsstitletemplate');
  499. if ($datas = get_records_sql ("SELECT d.id, ".implode(',',$supportedfields)."
  500. FROM {$CFG->prefix}data d
  501. WHERE d.course = $restore->course_id")) {
  502. /// Iterate over each data
  503. $i = 0; //Counter to send some output to the browser to avoid timeouts
  504. foreach ($datas as $data) {
  505. /// Increment counter
  506. $i++;
  507. /// Make a new copy of the data object with nothing in, to use if
  508. /// changes are necessary (allows us to do update_record without
  509. /// worrying about every single field being included and needing
  510. /// slashes).
  511. $newdata = new stdClass;
  512. $newdata->id=$data->id;
  513. /// Loop through handling each supported field
  514. $changed = false;
  515. foreach($supportedfields as $field) {
  516. $result = restore_decode_content_links_worker($data->{$field},$restore);
  517. if ($result != $data->{$field}) {
  518. $newdata->{$field} = addslashes($result);
  519. $changed = true;
  520. if (debugging()) {
  521. if (!defined('RESTORE_SILENTLY')) {
  522. echo '<br /><hr />'.s($data->{$field}).'<br />changed to<br />'.s($result).'<hr /><br />';
  523. }
  524. }
  525. }
  526. }
  527. /// Update record if any field changed
  528. if($changed) {
  529. $status = update_record("data",$newdata);
  530. }
  531. /// Do some output
  532. if (($i+1) % 5 == 0) {
  533. if (!defined('RESTORE_SILENTLY')) {
  534. echo ".";
  535. if (($i+1) % 100 == 0) {
  536. echo "<br />";
  537. }
  538. }
  539. backup_flush(300);
  540. }
  541. }
  542. }
  543. /// Process every COMMENT (content) in the course
  544. if ($comments = get_records_sql ("SELECT dc.id, dc.content
  545. FROM {$CFG->prefix}data d,
  546. {$CFG->prefix}data_records dr,
  547. {$CFG->prefix}data_comments dc
  548. WHERE d.course = $restore->course_id
  549. AND dr.dataid = d.id
  550. AND dc.recordid = dr.id")) {
  551. /// Iterate over each data_comments->content
  552. $i = 0; //Counter to send some output to the browser to avoid timeouts
  553. foreach ($comments as $comment) {
  554. /// Increment counter
  555. $i++;
  556. $content = $comment->content;
  557. $result = restore_decode_content_links_worker($content,$restore);
  558. if ($result != $content) {
  559. /// Update record
  560. $comment->content = addslashes($result);
  561. $status = update_record("data_comments",$comment);
  562. if (debugging()) {
  563. if (!defined('RESTORE_SILENTLY')) {
  564. echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
  565. }
  566. }
  567. }
  568. /// Do some output
  569. if (($i+1) % 5 == 0) {
  570. if (!defined('RESTORE_SILENTLY')) {
  571. echo ".";
  572. if (($i+1) % 100 == 0) {
  573. echo "<br />";
  574. }
  575. }
  576. backup_flush(300);
  577. }
  578. }
  579. }
  580. /// Process every CONTENT (content, content1, content2, content3, content4) in the course
  581. if ($contents = get_records_sql ("SELECT dc.id, dc.content, dc.content1, dc.content2, dc.content3, dc.content4
  582. FROM {$CFG->prefix}data d,
  583. {$CFG->prefix}data_records dr,
  584. {$CFG->prefix}data_content dc
  585. WHERE d.course = $restore->course_id
  586. AND dr.dataid = d.id
  587. AND dc.recordid = dr.id")) {
  588. /// Iterate over each data_content->content, content1, content2, content3 and content4
  589. $i = 0; //Counter to send some output to the browser to avoid timeouts
  590. foreach ($contents as $cnt) {
  591. /// Increment counter
  592. $i++;
  593. $content = $cnt->content;
  594. $content1 = $cnt->content1;
  595. $content2 = $cnt->content2;
  596. $content3 = $cnt->content3;
  597. $content4 = $cnt->content4;
  598. $result = restore_decode_content_links_worker($content,$restore);
  599. $result1 = restore_decode_content_links_worker($content1,$restore);
  600. $result2 = restore_decode_content_links_worker($content2,$restore);
  601. $result3 = restore_decode_content_links_worker($content3,$restore);
  602. $result4 = restore_decode_content_links_worker($content4,$restore);
  603. if ($result != $content || $result1 != $content1 || $result2 != $content2 ||
  604. $result3 != $content3 || $result4 != $content4) {
  605. /// Unset fields to update only the necessary ones
  606. unset($cnt->content);
  607. unset($cnt->content1);
  608. unset($cnt->content2);
  609. unset($cnt->content3);
  610. unset($cnt->content4);
  611. /// Conditionally set the fields
  612. if ($result != $content) {
  613. $cnt->content = addslashes($result);
  614. }
  615. if ($result1 != $content1) {
  616. $cnt->content1 = addslashes($result1);
  617. }
  618. if ($result2 != $content2) {
  619. $cnt->content2 = addslashes($result2);
  620. }
  621. if ($result3 != $content3) {
  622. $cnt->content3 = addslashes($result3);
  623. }
  624. if ($result4 != $content4) {
  625. $cnt->content4 = addslashes($result4);
  626. }
  627. /// Update record with the changed fields
  628. $status = update_record("data_content",$cnt);
  629. if (debugging()) {
  630. if (!defined('RESTORE_SILENTLY')) {
  631. echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
  632. }
  633. }
  634. }
  635. /// Do some output
  636. if (($i+1) % 5 == 0) {
  637. if (!defined('RESTORE_SILENTLY')) {
  638. echo ".";
  639. if (($i+1) % 100 == 0) {
  640. echo "<br />";
  641. }
  642. }
  643. backup_flush(300);
  644. }
  645. }
  646. }
  647. return $status;
  648. }
  649. ?>