PageRenderTime 31ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/journal/restorelib.php

https://github.com/cgtaylor/moodle
PHP | 341 lines | 237 code | 38 blank | 66 comment | 49 complexity | 2caa13636e4aaf99ce510529c52d490e MD5 | raw file
  1. <?php //$Id: restorelib.php,v 1.1 2008/04/30 10:26:59 skodak Exp $
  2. //This php script contains all the stuff to backup/restore
  3. //journal mods
  4. //This is the "graphical" structure of the journal mod:
  5. //
  6. // journal
  7. // (CL,pk->id)
  8. // |
  9. // |
  10. // |
  11. // journal_entries
  12. // (UL,pk->id, fk->journal)
  13. //
  14. // Meaning: pk->primary key field of the table
  15. // fk->foreign key to link with parent
  16. // nt->nested field (recursive data)
  17. // CL->course level info
  18. // UL->user level info
  19. // files->table may have files)
  20. //
  21. //-----------------------------------------------------------
  22. //This function executes all the restore procedure about this mod
  23. function journal_restore_mods($mod,$restore) {
  24. global $CFG;
  25. $status = true;
  26. //Get record from backup_ids
  27. $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
  28. if ($data) {
  29. //Now get completed xmlized object
  30. $info = $data->info;
  31. //if necessary, write to restorelog and adjust date/time fields
  32. if ($restore->course_startdateoffset) {
  33. restore_log_date_changes('Journal', $restore, $info['MOD']['#'], array('TIMEMODIFIED'));
  34. }
  35. //traverse_xmlize($info); //Debug
  36. //print_object ($GLOBALS['traverse_array']); //Debug
  37. //$GLOBALS['traverse_array']=""; //Debug
  38. //Now, build the JOURNAL record structure
  39. $journal->course = $restore->course_id;
  40. $journal->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
  41. $journal->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
  42. $journal->introformat = backup_todb($info['MOD']['#']['INTROFORMAT']['0']['#']);
  43. $journal->days = backup_todb($info['MOD']['#']['DAYS']['0']['#']);
  44. $journal->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
  45. $journal->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
  46. //We have to recode the assessed field if it is <0 (scale)
  47. if ($journal->assessed < 0) {
  48. $scale = backup_getid($restore->backup_unique_code,"scale",abs($journal->assessed));
  49. if ($scale) {
  50. $journal->assessed = -($scale->new_id);
  51. }
  52. }
  53. //The structure is equal to the db, so insert the journal
  54. $newid = insert_record ("journal",$journal);
  55. //Do some output
  56. if (!defined('RESTORE_SILENTLY')) {
  57. echo "<li>".get_string("modulename","journal")." \"".format_string(stripslashes($journal->name),true)."\"</li>";
  58. }
  59. backup_flush(300);
  60. if ($newid) {
  61. //We have the newid, update backup_ids
  62. backup_putid($restore->backup_unique_code,$mod->modtype,
  63. $mod->id, $newid);
  64. //Now check if want to restore user data and do it.
  65. if (restore_userdata_selected($restore,'journal',$mod->id)) {
  66. //Restore journal_entries
  67. $status = journal_entries_restore_mods ($mod->id, $newid,$info,$restore);
  68. }
  69. } else {
  70. $status = false;
  71. }
  72. } else {
  73. $status = false;
  74. }
  75. return $status;
  76. }
  77. //This function restores the journal_entries
  78. function journal_entries_restore_mods($old_journal_id, $new_journal_id,$info,$restore) {
  79. global $CFG;
  80. $status = true;
  81. //Get the entries array
  82. $entries = $info['MOD']['#']['ENTRIES']['0']['#']['ENTRY'];
  83. //Iterate over entries
  84. for($i = 0; $i < sizeof($entries); $i++) {
  85. $entry_info = $entries[$i];
  86. //traverse_xmlize($entry_info); //Debug
  87. //print_object ($GLOBALS['traverse_array']); //Debug
  88. //$GLOBALS['traverse_array']=""; //Debug
  89. //We'll need this later!! $sub_info changed to $entry_info
  90. $oldid = backup_todb($entry_info['#']['ID']['0']['#']);
  91. $olduserid = backup_todb($entry_info['#']['USERID']['0']['#']);
  92. //Now, build the JOURNAL_ENTRIES record structure
  93. $entry->journal = $new_journal_id;
  94. $entry->userid = backup_todb($entry_info['#']['USERID']['0']['#']);
  95. $entry->modified = backup_todb($entry_info['#']['MODIFIED']['0']['#']);
  96. $entry->modified += $restore->course_startdateoffset;
  97. $entry->text = backup_todb($entry_info['#']['TEXT']['0']['#']);
  98. $entry->format = backup_todb($entry_info['#']['FORMAT']['0']['#']);
  99. $entry->rating = backup_todb($entry_info['#']['RATING']['0']['#']);
  100. if (isset($entry_info['#']['COMMENT']['0']['#'])) {
  101. $entry->entrycomment = backup_todb($entry_info['#']['COMMENT']['0']['#']);
  102. } else {
  103. $entry->entrycomment = backup_todb($entry_info['#']['ENTRYCOMMENT']['0']['#']);
  104. }
  105. $entry->teacher = backup_todb($entry_info['#']['TEACHER']['0']['#']);
  106. $entry->timemarked = backup_todb($entry_info['#']['TIMEMARKED']['0']['#']);
  107. $entry->timemarked += $restore->course_startdateoffset;
  108. $entry->mailed = backup_todb($entry_info['#']['MAILED']['0']['#']);
  109. //We have to recode the userid field
  110. $user = backup_getid($restore->backup_unique_code,"user",$entry->userid);
  111. if ($user) {
  112. $entry->userid = $user->new_id;
  113. }
  114. //We have to recode the teacher field
  115. $user = backup_getid($restore->backup_unique_code,"user",$entry->teacher);
  116. if ($user) {
  117. $entry->teacher = $user->new_id;
  118. }
  119. //The structure is equal to the db, so insert the journal_entry
  120. $newid = insert_record ("journal_entries",$entry);
  121. //Do some output
  122. if (($i+1) % 50 == 0) {
  123. if (!defined('RESTORE_SILENTLY')) {
  124. echo ".";
  125. if (($i+1) % 1000 == 0) {
  126. echo "<br />";
  127. }
  128. }
  129. backup_flush(300);
  130. }
  131. if ($newid) {
  132. //We have the newid, update backup_ids
  133. backup_putid($restore->backup_unique_code,"journal_entry",$oldid,
  134. $newid);
  135. } else {
  136. $status = false;
  137. }
  138. }
  139. return $status;
  140. }
  141. //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
  142. //some texts in the module
  143. function journal_restore_wiki2markdown ($restore) {
  144. global $CFG;
  145. $status = true;
  146. //Convert journal_entries->text
  147. if ($records = get_records_sql ("SELECT e.id, e.text, e.format
  148. FROM {$CFG->prefix}journal_entries e,
  149. {$CFG->prefix}journal j,
  150. {$CFG->prefix}backup_ids b
  151. WHERE j.id = e.journal AND
  152. j.course = $restore->course_id AND
  153. e.format = ".FORMAT_WIKI. " AND
  154. b.backup_code = $restore->backup_unique_code AND
  155. b.table_name = 'journal_entries' AND
  156. b.new_id = e.id")) {
  157. foreach ($records as $record) {
  158. //Rebuild wiki links
  159. $record->text = restore_decode_wiki_content($record->text, $restore);
  160. //Convert to Markdown
  161. $wtm = new WikiToMarkdown();
  162. $record->text = $wtm->convert($record->text, $restore->course_id);
  163. $record->format = FORMAT_MARKDOWN;
  164. $status = update_record('journal_entries', addslashes_object($record));
  165. //Do some output
  166. $i++;
  167. if (($i+1) % 1 == 0) {
  168. if (!defined('RESTORE_SILENTLY')) {
  169. echo ".";
  170. if (($i+1) % 20 == 0) {
  171. echo "<br />";
  172. }
  173. }
  174. backup_flush(300);
  175. }
  176. }
  177. }
  178. //Convert journal->intro
  179. if ($records = get_records_sql ("SELECT j.id, j.intro, j.introformat
  180. FROM {$CFG->prefix}journal j,
  181. {$CFG->prefix}backup_ids b
  182. WHERE j.course = $restore->course_id AND
  183. j.introformat = ".FORMAT_WIKI. " AND
  184. b.backup_code = $restore->backup_unique_code AND
  185. b.table_name = 'journal' AND
  186. b.new_id = j.id")) {
  187. foreach ($records as $record) {
  188. //Rebuild wiki links
  189. $record->intro = restore_decode_wiki_content($record->intro, $restore);
  190. //Convert to Markdown
  191. $wtm = new WikiToMarkdown();
  192. $record->intro = $wtm->convert($record->intro, $restore->course_id);
  193. $record->introformat = FORMAT_MARKDOWN;
  194. $status = update_record('journal', addslashes_object($record));
  195. //Do some output
  196. $i++;
  197. if (($i+1) % 1 == 0) {
  198. if (!defined('RESTORE_SILENTLY')) {
  199. echo ".";
  200. if (($i+1) % 20 == 0) {
  201. echo "<br />";
  202. }
  203. }
  204. backup_flush(300);
  205. }
  206. }
  207. }
  208. return $status;
  209. }
  210. //This function returns a log record with all the necessay transformations
  211. //done. It's used by restore_log_module() to restore modules log.
  212. function journal_restore_logs($restore,$log) {
  213. $status = false;
  214. //Depending of the action, we recode different things
  215. switch ($log->action) {
  216. case "add":
  217. if ($log->cmid) {
  218. //Get the new_id of the module (to recode the info field)
  219. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  220. if ($mod) {
  221. $log->url = "view.php?id=".$log->cmid;
  222. $log->info = $mod->new_id;
  223. $status = true;
  224. }
  225. }
  226. break;
  227. case "update":
  228. if ($log->cmid) {
  229. //Get the new_id of the module (to recode the info field)
  230. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  231. if ($mod) {
  232. $log->url = "view.php?id=".$log->cmid;
  233. $log->info = $mod->new_id;
  234. $status = true;
  235. }
  236. }
  237. break;
  238. case "view":
  239. if ($log->cmid) {
  240. //Get the new_id of the module (to recode the info field)
  241. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  242. if ($mod) {
  243. $log->url = "view.php?id=".$log->cmid;
  244. $log->info = $mod->new_id;
  245. $status = true;
  246. }
  247. }
  248. break;
  249. case "add entry":
  250. if ($log->cmid) {
  251. //Get the new_id of the module (to recode the info field)
  252. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  253. if ($mod) {
  254. $log->url = "view.php?id=".$log->cmid;
  255. $log->info = $mod->new_id;
  256. $status = true;
  257. }
  258. }
  259. break;
  260. case "update entry":
  261. if ($log->cmid) {
  262. //Get the new_id of the module (to recode the info field)
  263. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  264. if ($mod) {
  265. $log->url = "view.php?id=".$log->cmid;
  266. $log->info = $mod->new_id;
  267. $status = true;
  268. }
  269. }
  270. break;
  271. case "view responses":
  272. if ($log->cmid) {
  273. //Get the new_id of the module (to recode the info field)
  274. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  275. if ($mod) {
  276. $log->url = "report.php?id=".$log->cmid;
  277. $log->info = $mod->new_id;
  278. $status = true;
  279. }
  280. }
  281. break;
  282. case "update feedback":
  283. if ($log->cmid) {
  284. $log->url = "report.php?id=".$log->cmid;
  285. $status = true;
  286. }
  287. break;
  288. case "view all":
  289. $log->url = "index.php?id=".$log->course;
  290. $status = true;
  291. break;
  292. default:
  293. if (!defined('RESTORE_SILENTLY')) {
  294. echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
  295. }
  296. break;
  297. }
  298. if ($status) {
  299. $status = $log;
  300. }
  301. return $status;
  302. }
  303. ?>