PageRenderTime 56ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 3ms

/moodle/backup/restorelib.php

https://bitbucket.org/geek745/moodle-db2
PHP | 9133 lines | 6341 code | 942 blank | 1850 comment | 1739 complexity | 32db839c51da903d88e3e021cedaa498 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0

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

  1. <?php //$Id: restorelib.php,v 1.283.2.79 2009/10/06 01:22:15 stronk7 Exp $
  2. //Functions used in restore
  3. require_once($CFG->libdir.'/gradelib.php');
  4. /**
  5. * Group backup/restore constants, 0.
  6. */
  7. define('RESTORE_GROUPS_NONE', 0);
  8. /**
  9. * Group backup/restore constants, 1.
  10. */
  11. define('RESTORE_GROUPS_ONLY', 1);
  12. /**
  13. * Group backup/restore constants, 2.
  14. */
  15. define('RESTORE_GROUPINGS_ONLY', 2);
  16. /**
  17. * Group backup/restore constants, course/all.
  18. */
  19. define('RESTORE_GROUPS_GROUPINGS', 3);
  20. //This function unzips a zip file in the same directory that it is
  21. //It automatically uses pclzip or command line unzip
  22. function restore_unzip ($file) {
  23. return unzip_file($file, '', false);
  24. }
  25. //This function checks if moodle.xml seems to be a valid xml file
  26. //(exists, has an xml header and a course main tag
  27. function restore_check_moodle_file ($file) {
  28. $status = true;
  29. //Check if it exists
  30. if ($status = is_file($file)) {
  31. //Open it and read the first 200 bytes (chars)
  32. $handle = fopen ($file, "r");
  33. $first_chars = fread($handle,200);
  34. $status = fclose ($handle);
  35. //Chek if it has the requires strings
  36. if ($status) {
  37. $status = strpos($first_chars,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  38. if ($status !== false) {
  39. $status = strpos($first_chars,"<MOODLE_BACKUP>");
  40. }
  41. }
  42. }
  43. return $status;
  44. }
  45. //This function iterates over all modules in backup file, searching for a
  46. //MODNAME_refresh_events() to execute. Perhaps it should ve moved to central Moodle...
  47. function restore_refresh_events($restore) {
  48. global $CFG;
  49. $status = true;
  50. //Take all modules in backup
  51. $modules = $restore->mods;
  52. //Iterate
  53. foreach($modules as $name => $module) {
  54. //Only if the module is being restored
  55. if (isset($module->restore) && $module->restore == 1) {
  56. //Include module library
  57. include_once("$CFG->dirroot/mod/$name/lib.php");
  58. //If module_refresh_events exists
  59. $function_name = $name."_refresh_events";
  60. if (function_exists($function_name)) {
  61. $status = $function_name($restore->course_id);
  62. }
  63. }
  64. }
  65. return $status;
  66. }
  67. //This function makes all the necessary calls to xxxx_decode_content_links_caller()
  68. //function in each module/block/course format..., passing them the desired contents to be decoded
  69. //from backup format to destination site/course in order to mantain inter-activities
  70. //working in the backup/restore process
  71. function restore_decode_content_links($restore) {
  72. global $CFG;
  73. $status = true;
  74. if (!defined('RESTORE_SILENTLY')) {
  75. echo "<ul>";
  76. }
  77. // Recode links in the course summary.
  78. if (!defined('RESTORE_SILENTLY')) {
  79. echo '<li>' . get_string('from') . ' ' . get_string('course');
  80. }
  81. $course = get_record('course', 'id', $restore->course_id, '', '', '', '', 'id,summary');
  82. $coursesummary = backup_todb($course->summary,false); // Exception: Process FILEPHP (not available when restored) MDL-18222
  83. $coursesummary = restore_decode_content_links_worker($coursesummary, $restore);
  84. if ($coursesummary != $course->summary) {
  85. $course->summary = addslashes($coursesummary);
  86. if (!update_record('course', $course)) {
  87. $status = false;
  88. }
  89. }
  90. if (!defined('RESTORE_SILENTLY')) {
  91. echo '</li>';
  92. }
  93. // Recode links in section summaries.
  94. $sections = get_records('course_sections', 'course', $restore->course_id, 'id', 'id,summary');
  95. if ($sections) {
  96. if (!defined('RESTORE_SILENTLY')) {
  97. echo '<li>' . get_string('from') . ' ' . get_string('sections');
  98. }
  99. foreach ($sections as $section) {
  100. $sectionsummary = restore_decode_content_links_worker($section->summary, $restore);
  101. if ($sectionsummary != $section->summary) {
  102. $section->summary = addslashes($sectionsummary);
  103. if (!update_record('course_sections', $section)) {
  104. $status = false;
  105. }
  106. }
  107. }
  108. if (!defined('RESTORE_SILENTLY')) {
  109. echo '</li>';
  110. }
  111. }
  112. // Restore links in modules.
  113. foreach ($restore->mods as $name => $info) {
  114. //If the module is being restored
  115. if (isset($info->restore) && $info->restore == 1) {
  116. //Check if the xxxx_decode_content_links_caller exists
  117. include_once("$CFG->dirroot/mod/$name/restorelib.php");
  118. $function_name = $name."_decode_content_links_caller";
  119. if (function_exists($function_name)) {
  120. if (!defined('RESTORE_SILENTLY')) {
  121. echo "<li>".get_string ("from")." ".get_string("modulenameplural",$name);
  122. }
  123. $status = $function_name($restore) && $status;
  124. if (!defined('RESTORE_SILENTLY')) {
  125. echo '</li>';
  126. }
  127. }
  128. }
  129. }
  130. // For the course format call its decode_content_links method (if it exists)
  131. $format = get_field('course', 'format', 'id', $restore->course_id);
  132. if (file_exists("$CFG->dirroot/course/format/$format/restorelib.php")) {
  133. include_once("$CFG->dirroot/course/format/$format/restorelib.php");
  134. $function_name = $format.'_decode_format_content_links_caller';
  135. if (function_exists($function_name)) {
  136. if (!defined('RESTORE_SILENTLY')) {
  137. echo "<li>".get_string ("from")." ".get_string("format").' '.$format;
  138. }
  139. $status = $function_name($restore);
  140. if (!defined('RESTORE_SILENTLY')) {
  141. echo '</li>';
  142. }
  143. }
  144. }
  145. // Process all html text also in blocks too
  146. if (!defined('RESTORE_SILENTLY')) {
  147. echo '<li>'.get_string ('from').' '.get_string('blocks');
  148. }
  149. if ($blocks = get_records('block', 'visible', 1)) {
  150. foreach ($blocks as $block) {
  151. if ($blockobject = block_instance($block->name)) {
  152. $blockobject->decode_content_links_caller($restore);
  153. }
  154. }
  155. }
  156. if (!defined('RESTORE_SILENTLY')) {
  157. echo '</li>';
  158. }
  159. // Restore links in questions.
  160. require_once("$CFG->dirroot/question/restorelib.php");
  161. if (!defined('RESTORE_SILENTLY')) {
  162. echo '<li>' . get_string('from') . ' ' . get_string('questions', 'quiz');
  163. }
  164. $status = question_decode_content_links_caller($restore) && $status;
  165. if (!defined('RESTORE_SILENTLY')) {
  166. echo '</li>';
  167. }
  168. if (!defined('RESTORE_SILENTLY')) {
  169. echo "</ul>";
  170. }
  171. return $status;
  172. }
  173. //This function is called from all xxxx_decode_content_links_caller(),
  174. //its task is to ask all modules (maybe other linkable objects) to restore
  175. //links to them.
  176. function restore_decode_content_links_worker($content,$restore) {
  177. global $CFG;
  178. foreach($restore->mods as $name => $info) {
  179. $function_name = $name."_decode_content_links";
  180. if (function_exists($function_name)) {
  181. $content = $function_name($content,$restore);
  182. }
  183. }
  184. // For the current format, call decode_format_content_links if it exists
  185. static $format_function_name;
  186. if (!isset($format_function_name)) {
  187. $format_function_name = false;
  188. if ($format = get_field('course', 'format', 'id', $restore->course_id)) {
  189. if (file_exists("$CFG->dirroot/course/format/$format/restorelib.php")) {
  190. include_once("$CFG->dirroot/course/format/$format/restorelib.php");
  191. $function_name = $format.'_decode_format_content_links';
  192. if (function_exists($function_name)) {
  193. $format_function_name = $function_name;
  194. }
  195. }
  196. }
  197. }
  198. // If the above worked - then we have a function to call
  199. if ($format_function_name) {
  200. $content = $format_function_name($content, $restore);
  201. }
  202. // For each block, call its encode_content_links method
  203. static $blockobjects = null;
  204. if (!isset($blockobjects)) {
  205. $blockobjects = array();
  206. if ($blocks = get_records('block', 'visible', 1)) {
  207. foreach ($blocks as $block) {
  208. if ($blockobject = block_instance($block->name)) {
  209. $blockobjects[] = $blockobject;
  210. }
  211. }
  212. }
  213. }
  214. foreach ($blockobjects as $blockobject) {
  215. $content = $blockobject->decode_content_links($content,$restore);
  216. }
  217. return $content;
  218. }
  219. //This function converts all the wiki texts in the restored course
  220. //to the Markdown format. Used only for backup files prior 2005041100.
  221. //It calls every module xxxx_convert_wiki2markdown function
  222. function restore_convert_wiki2markdown($restore) {
  223. $status = true;
  224. if (!defined('RESTORE_SILENTLY')) {
  225. echo "<ul>";
  226. }
  227. foreach ($restore->mods as $name => $info) {
  228. //If the module is being restored
  229. if ($info->restore == 1) {
  230. //Check if the xxxx_restore_wiki2markdown exists
  231. $function_name = $name."_restore_wiki2markdown";
  232. if (function_exists($function_name)) {
  233. $status = $function_name($restore);
  234. if (!defined('RESTORE_SILENTLY')) {
  235. echo "<li>".get_string("modulenameplural",$name);
  236. echo '</li>';
  237. }
  238. }
  239. }
  240. }
  241. if (!defined('RESTORE_SILENTLY')) {
  242. echo "</ul>";
  243. }
  244. return $status;
  245. }
  246. //This function receives a wiki text in the restore process and
  247. //return it with every link to modules " modulename:moduleid"
  248. //converted if possible. See the space before modulename!!
  249. function restore_decode_wiki_content($content,$restore) {
  250. global $CFG;
  251. $result = $content;
  252. $searchstring='/ ([a-zA-Z]+):([0-9]+)\(([^)]+)\)/';
  253. //We look for it
  254. preg_match_all($searchstring,$content,$foundset);
  255. //If found, then we are going to look for its new id (in backup tables)
  256. if ($foundset[0]) {
  257. //print_object($foundset); //Debug
  258. //Iterate over foundset[2]. They are the old_ids
  259. foreach($foundset[2] as $old_id) {
  260. //We get the needed variables here (course id)
  261. $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
  262. //Personalize the searchstring
  263. $searchstring='/ ([a-zA-Z]+):'.$old_id.'\(([^)]+)\)/';
  264. //If it is a link to this course, update the link to its new location
  265. if($rec->new_id) {
  266. //Now replace it
  267. $result= preg_replace($searchstring,' $1:'.$rec->new_id.'($2)',$result);
  268. } else {
  269. //It's a foreign link so redirect it to its original URL
  270. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/$1/view.php?id='.$old_id.'($2)',$result);
  271. }
  272. }
  273. }
  274. return $result;
  275. }
  276. //This function read the xml file and store it data from the info zone in an object
  277. function restore_read_xml_info ($xml_file) {
  278. //We call the main read_xml function, with todo = INFO
  279. $info = restore_read_xml ($xml_file,"INFO",false);
  280. return $info;
  281. }
  282. //This function read the xml file and store it data from the course header zone in an object
  283. function restore_read_xml_course_header ($xml_file) {
  284. //We call the main read_xml function, with todo = COURSE_HEADER
  285. $info = restore_read_xml ($xml_file,"COURSE_HEADER",false);
  286. return $info;
  287. }
  288. //This function read the xml file and store its data from the blocks in a object
  289. function restore_read_xml_blocks ($restore, $xml_file) {
  290. //We call the main read_xml function, with todo = BLOCKS
  291. $info = restore_read_xml ($xml_file,'BLOCKS',$restore);
  292. return $info;
  293. }
  294. //This function read the xml file and store its data from the sections in a object
  295. function restore_read_xml_sections ($xml_file) {
  296. //We call the main read_xml function, with todo = SECTIONS
  297. $info = restore_read_xml ($xml_file,"SECTIONS",false);
  298. return $info;
  299. }
  300. //This function read the xml file and store its data from the course format in an object
  301. function restore_read_xml_formatdata ($xml_file) {
  302. //We call the main read_xml function, with todo = FORMATDATA
  303. $info = restore_read_xml ($xml_file,'FORMATDATA',false);
  304. return $info;
  305. }
  306. //This function read the xml file and store its data from the metacourse in a object
  307. function restore_read_xml_metacourse ($xml_file) {
  308. //We call the main read_xml function, with todo = METACOURSE
  309. $info = restore_read_xml ($xml_file,"METACOURSE",false);
  310. return $info;
  311. }
  312. //This function read the xml file and store its data from the gradebook in a object
  313. function restore_read_xml_gradebook ($restore, $xml_file) {
  314. //We call the main read_xml function, with todo = GRADEBOOK
  315. $info = restore_read_xml ($xml_file,"GRADEBOOK",$restore);
  316. return $info;
  317. }
  318. //This function read the xml file and store its data from the users in
  319. //backup_ids->info db (and user's id in $info)
  320. function restore_read_xml_users ($restore,$xml_file) {
  321. //We call the main read_xml function, with todo = USERS
  322. $info = restore_read_xml ($xml_file,"USERS",$restore);
  323. return $info;
  324. }
  325. //This function read the xml file and store its data from the messages in
  326. //backup_ids->message backup_ids->message_read and backup_ids->contact and db (and their counters in info)
  327. function restore_read_xml_messages ($restore,$xml_file) {
  328. //We call the main read_xml function, with todo = MESSAGES
  329. $info = restore_read_xml ($xml_file,"MESSAGES",$restore);
  330. return $info;
  331. }
  332. //This function read the xml file and store its data from the blogs in
  333. //backup_ids->blog and backup_ids->blog_tag and db (and their counters in info)
  334. function restore_read_xml_blogs ($restore,$xml_file) {
  335. //We call the main read_xml function, with todo = BLOGS
  336. $info = restore_read_xml ($xml_file,"BLOGS",$restore);
  337. return $info;
  338. }
  339. //This function read the xml file and store its data from the questions in
  340. //backup_ids->info db (and category's id in $info)
  341. function restore_read_xml_questions ($restore,$xml_file) {
  342. //We call the main read_xml function, with todo = QUESTIONS
  343. $info = restore_read_xml ($xml_file,"QUESTIONS",$restore);
  344. return $info;
  345. }
  346. //This function read the xml file and store its data from the scales in
  347. //backup_ids->info db (and scale's id in $info)
  348. function restore_read_xml_scales ($restore,$xml_file) {
  349. //We call the main read_xml function, with todo = SCALES
  350. $info = restore_read_xml ($xml_file,"SCALES",$restore);
  351. return $info;
  352. }
  353. //This function read the xml file and store its data from the groups in
  354. //backup_ids->info db (and group's id in $info)
  355. function restore_read_xml_groups ($restore,$xml_file) {
  356. //We call the main read_xml function, with todo = GROUPS
  357. $info = restore_read_xml ($xml_file,"GROUPS",$restore);
  358. return $info;
  359. }
  360. //This function read the xml file and store its data from the groupings in
  361. //backup_ids->info db (and grouping's id in $info)
  362. function restore_read_xml_groupings ($restore,$xml_file) {
  363. //We call the main read_xml function, with todo = GROUPINGS
  364. $info = restore_read_xml ($xml_file,"GROUPINGS",$restore);
  365. return $info;
  366. }
  367. //This function read the xml file and store its data from the groupings in
  368. //backup_ids->info db (and grouping's id in $info)
  369. function restore_read_xml_groupings_groups ($restore,$xml_file) {
  370. //We call the main read_xml function, with todo = GROUPINGS
  371. $info = restore_read_xml ($xml_file,"GROUPINGSGROUPS",$restore);
  372. return $info;
  373. }
  374. //This function read the xml file and store its data from the events (course) in
  375. //backup_ids->info db (and event's id in $info)
  376. function restore_read_xml_events ($restore,$xml_file) {
  377. //We call the main read_xml function, with todo = EVENTS
  378. $info = restore_read_xml ($xml_file,"EVENTS",$restore);
  379. return $info;
  380. }
  381. //This function read the xml file and store its data from the modules in
  382. //backup_ids->info
  383. function restore_read_xml_modules ($restore,$xml_file) {
  384. //We call the main read_xml function, with todo = MODULES
  385. $info = restore_read_xml ($xml_file,"MODULES",$restore);
  386. return $info;
  387. }
  388. //This function read the xml file and store its data from the logs in
  389. //backup_ids->info
  390. function restore_read_xml_logs ($restore,$xml_file) {
  391. //We call the main read_xml function, with todo = LOGS
  392. $info = restore_read_xml ($xml_file,"LOGS",$restore);
  393. return $info;
  394. }
  395. function restore_read_xml_roles ($xml_file) {
  396. //We call the main read_xml function, with todo = ROLES
  397. $info = restore_read_xml ($xml_file,"ROLES",false);
  398. return $info;
  399. }
  400. //This function prints the contents from the info parammeter passed
  401. function restore_print_info ($info) {
  402. global $CFG;
  403. $status = true;
  404. if ($info) {
  405. $table = new object();
  406. //This is tha align to every ingo table
  407. $table->align = array ("right","left");
  408. //This is the nowrap clause
  409. $table->wrap = array ("","nowrap");
  410. //The width
  411. $table->width = "70%";
  412. //Put interesting info in table
  413. //The backup original name
  414. $tab[0][0] = "<b>".get_string("backuporiginalname").":</b>";
  415. $tab[0][1] = $info->backup_name;
  416. //The moodle version
  417. $tab[1][0] = "<b>".get_string("moodleversion").":</b>";
  418. $tab[1][1] = $info->backup_moodle_release." (".$info->backup_moodle_version.")";
  419. //The backup version
  420. $tab[2][0] = "<b>".get_string("backupversion").":</b>";
  421. $tab[2][1] = $info->backup_backup_release." (".$info->backup_backup_version.")";
  422. //The backup date
  423. $tab[3][0] = "<b>".get_string("backupdate").":</b>";
  424. $tab[3][1] = userdate($info->backup_date);
  425. //Is this the same Moodle install?
  426. if (!empty($info->original_siteidentifier)) {
  427. $tab[4][0] = "<b>".get_string("backupfromthissite").":</b>";
  428. if (backup_is_same_site($info)) {
  429. $tab[4][1] = get_string('yes');
  430. } else {
  431. $tab[4][1] = get_string('no');
  432. }
  433. }
  434. //Print title
  435. print_heading(get_string("backup").":");
  436. $table->data = $tab;
  437. //Print backup general info
  438. print_table($table);
  439. if ($info->backup_backup_version <= 2005070500) {
  440. notify(get_string('backupnonisowarning')); // Message informing that this backup may not work!
  441. }
  442. //Now backup contents in another table
  443. $tab = array();
  444. //First mods info
  445. $mods = $info->mods;
  446. $elem = 0;
  447. foreach ($mods as $key => $mod) {
  448. $tab[$elem][0] = "<b>".get_string("modulenameplural",$key).":</b>";
  449. if ($mod->backup == "false") {
  450. $tab[$elem][1] = get_string("notincluded");
  451. } else {
  452. if ($mod->userinfo == "true") {
  453. $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
  454. } else {
  455. $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
  456. }
  457. if (isset($mod->instances) && is_array($mod->instances) && count($mod->instances)) {
  458. foreach ($mod->instances as $instance) {
  459. if ($instance->backup) {
  460. $elem++;
  461. $tab[$elem][0] = $instance->name;
  462. if ($instance->userinfo == 'true') {
  463. $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
  464. } else {
  465. $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
  466. }
  467. }
  468. }
  469. }
  470. }
  471. $elem++;
  472. }
  473. //Metacourse info
  474. $tab[$elem][0] = "<b>".get_string("metacourse").":</b>";
  475. if ($info->backup_metacourse == "true") {
  476. $tab[$elem][1] = get_string("yes");
  477. } else {
  478. $tab[$elem][1] = get_string("no");
  479. }
  480. $elem++;
  481. //Users info
  482. $tab[$elem][0] = "<b>".get_string("users").":</b>";
  483. $tab[$elem][1] = get_string($info->backup_users);
  484. $elem++;
  485. //Logs info
  486. $tab[$elem][0] = "<b>".get_string("logs").":</b>";
  487. if ($info->backup_logs == "true") {
  488. $tab[$elem][1] = get_string("yes");
  489. } else {
  490. $tab[$elem][1] = get_string("no");
  491. }
  492. $elem++;
  493. //User Files info
  494. $tab[$elem][0] = "<b>".get_string("userfiles").":</b>";
  495. if ($info->backup_user_files == "true") {
  496. $tab[$elem][1] = get_string("yes");
  497. } else {
  498. $tab[$elem][1] = get_string("no");
  499. }
  500. $elem++;
  501. //Course Files info
  502. $tab[$elem][0] = "<b>".get_string("coursefiles").":</b>";
  503. if ($info->backup_course_files == "true") {
  504. $tab[$elem][1] = get_string("yes");
  505. } else {
  506. $tab[$elem][1] = get_string("no");
  507. }
  508. $elem++;
  509. //site Files info
  510. $tab[$elem][0] = "<b>".get_string("sitefiles").":</b>";
  511. if (isset($info->backup_site_files) && $info->backup_site_files == "true") {
  512. $tab[$elem][1] = get_string("yes");
  513. } else {
  514. $tab[$elem][1] = get_string("no");
  515. }
  516. $elem++;
  517. //gradebook history info
  518. $tab[$elem][0] = "<b>".get_string('gradebookhistories', 'grades').":</b>";
  519. if (isset($info->gradebook_histories) && $info->gradebook_histories == "true") {
  520. $tab[$elem][1] = get_string("yes");
  521. } else {
  522. $tab[$elem][1] = get_string("no");
  523. }
  524. $elem++;
  525. //Messages info (only showed if present)
  526. if ($info->backup_messages == 'true') {
  527. $tab[$elem][0] = "<b>".get_string('messages','message').":</b>";
  528. $tab[$elem][1] = get_string('yes');
  529. $elem++;
  530. } else {
  531. //Do nothing
  532. }
  533. $elem++;
  534. //Blogs info (only showed if present)
  535. if (isset($info->backup_blogs) && $info->backup_blogs == 'true') {
  536. $tab[$elem][0] = "<b>".get_string('blogs','blog').":</b>";
  537. $tab[$elem][1] = get_string('yes');
  538. $elem++;
  539. } else {
  540. //Do nothing
  541. }
  542. $table->data = $tab;
  543. //Print title
  544. print_heading(get_string("backupdetails").":");
  545. //Print backup general info
  546. print_table($table);
  547. } else {
  548. $status = false;
  549. }
  550. return $status;
  551. }
  552. //This function prints the contents from the course_header parammeter passed
  553. function restore_print_course_header ($course_header) {
  554. $status = true;
  555. if ($course_header) {
  556. $table = new object();
  557. //This is tha align to every ingo table
  558. $table->align = array ("right","left");
  559. //The width
  560. $table->width = "70%";
  561. //Put interesting course header in table
  562. //The course name
  563. $tab[0][0] = "<b>".get_string("name").":</b>";
  564. $tab[0][1] = $course_header->course_fullname." (".$course_header->course_shortname.")";
  565. //The course summary
  566. $tab[1][0] = "<b>".get_string("summary").":</b>";
  567. $tab[1][1] = $course_header->course_summary;
  568. $table->data = $tab;
  569. //Print title
  570. print_heading(get_string("course").":");
  571. //Print backup course header info
  572. print_table($table);
  573. } else {
  574. $status = false;
  575. }
  576. return $status;
  577. }
  578. //This function create a new course record.
  579. //When finished, course_header contains the id of the new course
  580. function restore_create_new_course($restore,&$course_header) {
  581. global $CFG;
  582. $status = true;
  583. $fullname = $course_header->course_fullname;
  584. $shortname = $course_header->course_shortname;
  585. $currentfullname = "";
  586. $currentshortname = "";
  587. $counter = 0;
  588. //Iteratere while the name exists
  589. do {
  590. if ($counter) {
  591. $suffixfull = " ".get_string("copyasnoun")." ".$counter;
  592. $suffixshort = "_".$counter;
  593. } else {
  594. $suffixfull = "";
  595. $suffixshort = "";
  596. }
  597. $currentfullname = $fullname.$suffixfull;
  598. // Limit the size of shortname - database column accepts <= 100 chars
  599. $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort;
  600. $coursefull = get_record("course","fullname",addslashes($currentfullname));
  601. $courseshort = get_record("course","shortname",addslashes($currentshortname));
  602. $counter++;
  603. } while ($coursefull || $courseshort);
  604. //New name = currentname
  605. $course_header->course_fullname = $currentfullname;
  606. $course_header->course_shortname = $currentshortname;
  607. // first try to get it from restore
  608. if ($restore->restore_restorecatto) {
  609. $category = get_record('course_categories', 'id', $restore->restore_restorecatto);
  610. }
  611. // else we try to get it from the xml file
  612. //Now calculate the category
  613. if (empty($category)) {
  614. $category = get_record("course_categories","id",$course_header->category->id,
  615. "name",addslashes($course_header->category->name));
  616. }
  617. //If no exists, try by name only
  618. if (!$category) {
  619. $category = get_record("course_categories","name",addslashes($course_header->category->name));
  620. }
  621. //If no exists, get category id 1
  622. if (!$category) {
  623. $category = get_record("course_categories","id","1");
  624. }
  625. //If category 1 doesn'exists, lets create the course category (get it from backup file)
  626. if (!$category) {
  627. $ins_category = new object();
  628. $ins_category->name = addslashes($course_header->category->name);
  629. $ins_category->parent = 0;
  630. $ins_category->sortorder = 0;
  631. $ins_category->coursecount = 0;
  632. $ins_category->visible = 0; //To avoid interferences with the rest of the site
  633. $ins_category->timemodified = time();
  634. $newid = insert_record("course_categories",$ins_category);
  635. $category->id = $newid;
  636. $category->name = $course_header->category->name;
  637. }
  638. //If exists, put new category id
  639. if ($category) {
  640. $course_header->category->id = $category->id;
  641. $course_header->category->name = $category->name;
  642. //Error, cannot locate category
  643. } else {
  644. $course_header->category->id = 0;
  645. $course_header->category->name = get_string("unknowncategory");
  646. $status = false;
  647. }
  648. //Create the course_object
  649. if ($status) {
  650. $course = new object();
  651. $course->category = addslashes($course_header->category->id);
  652. $course->password = addslashes($course_header->course_password);
  653. $course->fullname = addslashes($course_header->course_fullname);
  654. $course->shortname = addslashes($course_header->course_shortname);
  655. $course->idnumber = addslashes($course_header->course_idnumber);
  656. $course->idnumber = ''; //addslashes($course_header->course_idnumber); // we don't want this at all.
  657. $course->summary = addslashes($course_header->course_summary);
  658. $course->format = addslashes($course_header->course_format);
  659. $course->showgrades = addslashes($course_header->course_showgrades);
  660. $course->newsitems = addslashes($course_header->course_newsitems);
  661. $course->teacher = addslashes($course_header->course_teacher);
  662. $course->teachers = addslashes($course_header->course_teachers);
  663. $course->student = addslashes($course_header->course_student);
  664. $course->students = addslashes($course_header->course_students);
  665. $course->guest = addslashes($course_header->course_guest);
  666. $course->startdate = addslashes($course_header->course_startdate);
  667. $course->startdate += $restore->course_startdateoffset;
  668. $course->numsections = addslashes($course_header->course_numsections);
  669. //$course->showrecent = addslashes($course_header->course_showrecent); INFO: This is out in 1.3
  670. $course->maxbytes = addslashes($course_header->course_maxbytes);
  671. $course->showreports = addslashes($course_header->course_showreports);
  672. if (isset($course_header->course_groupmode)) {
  673. $course->groupmode = addslashes($course_header->course_groupmode);
  674. }
  675. if (isset($course_header->course_groupmodeforce)) {
  676. $course->groupmodeforce = addslashes($course_header->course_groupmodeforce);
  677. }
  678. if (isset($course_header->course_defaultgroupingid)) {
  679. //keep the original now - convert after groupings restored
  680. $course->defaultgroupingid = addslashes($course_header->course_defaultgroupingid);
  681. }
  682. $course->lang = addslashes($course_header->course_lang);
  683. $course->theme = addslashes($course_header->course_theme);
  684. $course->cost = addslashes($course_header->course_cost);
  685. $course->currency = isset($course_header->course_currency)?addslashes($course_header->course_currency):'';
  686. $course->marker = addslashes($course_header->course_marker);
  687. $course->visible = addslashes($course_header->course_visible);
  688. $course->hiddensections = addslashes($course_header->course_hiddensections);
  689. $course->timecreated = addslashes($course_header->course_timecreated);
  690. $course->timemodified = addslashes($course_header->course_timemodified);
  691. $course->metacourse = addslashes($course_header->course_metacourse);
  692. $course->expirynotify = isset($course_header->course_expirynotify) ? addslashes($course_header->course_expirynotify):0;
  693. $course->notifystudents = isset($course_header->course_notifystudents) ? addslashes($course_header->course_notifystudents) : 0;
  694. $course->expirythreshold = isset($course_header->course_expirythreshold) ? addslashes($course_header->course_expirythreshold) : 0;
  695. $course->enrollable = isset($course_header->course_enrollable) ? addslashes($course_header->course_enrollable) : 1;
  696. $course->enrolstartdate = isset($course_header->course_enrolstartdate) ? addslashes($course_header->course_enrolstartdate) : 0;
  697. if ($course->enrolstartdate) { //Roll course dates
  698. $course->enrolstartdate += $restore->course_startdateoffset;
  699. }
  700. $course->enrolenddate = isset($course_header->course_enrolenddate) ? addslashes($course_header->course_enrolenddate) : 0;
  701. if ($course->enrolenddate) { //Roll course dates
  702. $course->enrolenddate += $restore->course_startdateoffset;
  703. }
  704. $course->enrolperiod = addslashes($course_header->course_enrolperiod);
  705. //Calculate sortorder field
  706. $sortmax = get_record_sql('SELECT MAX(sortorder) AS max
  707. FROM ' . $CFG->prefix . 'course
  708. WHERE category=' . $course->category);
  709. if (!empty($sortmax->max)) {
  710. $course->sortorder = $sortmax->max + 1;
  711. unset($sortmax);
  712. } else {
  713. $course->sortorder = 100;
  714. }
  715. //Now, recode some languages (Moodle 1.5)
  716. if ($course->lang == 'ma_nt') {
  717. $course->lang = 'mi_nt';
  718. }
  719. //Disable course->metacourse if avoided in restore config
  720. if (!$restore->metacourse) {
  721. $course->metacourse = 0;
  722. }
  723. //Check if the theme exists in destination server
  724. $themes = get_list_of_themes();
  725. if (!in_array($course->theme, $themes)) {
  726. $course->theme = '';
  727. }
  728. //Now insert the record
  729. $newid = insert_record("course",$course);
  730. if ($newid) {
  731. //save old and new course id
  732. backup_putid ($restore->backup_unique_code,"course",$course_header->course_id,$newid);
  733. //Replace old course_id in course_header
  734. $course_header->course_id = $newid;
  735. } else {
  736. $status = false;
  737. }
  738. }
  739. return $status;
  740. }
  741. //This function creates all the block stuff when restoring courses
  742. //It calls selectively to restore_create_block_instances() for 1.5
  743. //and above backups. Upwards compatible with old blocks.
  744. function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_file) {
  745. global $CFG;
  746. $status = true;
  747. blocks_delete_all_on_page(PAGE_COURSE_VIEW, $restore->course_id);
  748. if (empty($backup_block_format)) { // This is a backup from Moodle < 1.5
  749. if (empty($blockinfo)) {
  750. // Looks like it's from Moodle < 1.3. Let's give the course default blocks...
  751. $newpage = page_create_object(PAGE_COURSE_VIEW, $restore->course_id);
  752. blocks_repopulate_page($newpage);
  753. } else {
  754. // We just have a blockinfo field, this is a legacy 1.4 or 1.3 backup
  755. $blockrecords = get_records_select('block', '', '', 'name, id');
  756. $temp_blocks_l = array();
  757. $temp_blocks_r = array();
  758. @list($temp_blocks_l, $temp_blocks_r) = explode(':', $blockinfo);
  759. $temp_blocks = array(BLOCK_POS_LEFT => explode(',', $temp_blocks_l), BLOCK_POS_RIGHT => explode(',', $temp_blocks_r));
  760. foreach($temp_blocks as $blockposition => $blocks) {
  761. $blockweight = 0;
  762. foreach($blocks as $blockname) {
  763. if(!isset($blockrecords[$blockname])) {
  764. // We don't know anything about this block!
  765. continue;
  766. }
  767. $blockinstance = new stdClass;
  768. // Remove any - prefix before doing the name-to-id mapping
  769. if(substr($blockname, 0, 1) == '-') {
  770. $blockname = substr($blockname, 1);
  771. $blockinstance->visible = 0;
  772. } else {
  773. $blockinstance->visible = 1;
  774. }
  775. $blockinstance->blockid = $blockrecords[$blockname]->id;
  776. $blockinstance->pageid = $restore->course_id;
  777. $blockinstance->pagetype = PAGE_COURSE_VIEW;
  778. $blockinstance->position = $blockposition;
  779. $blockinstance->weight = $blockweight;
  780. if(!$status = insert_record('block_instance', $blockinstance)) {
  781. $status = false;
  782. }
  783. ++$blockweight;
  784. }
  785. }
  786. }
  787. } else if($backup_block_format == 'instances') {
  788. $status = restore_create_block_instances($restore,$xml_file);
  789. }
  790. return $status;
  791. }
  792. //This function creates all the block_instances from xml when restoring in a
  793. //new course
  794. function restore_create_block_instances($restore,$xml_file) {
  795. global $CFG;
  796. $status = true;
  797. //Check it exists
  798. if (!file_exists($xml_file)) {
  799. $status = false;
  800. }
  801. //Get info from xml
  802. if ($status) {
  803. $info = restore_read_xml_blocks($restore,$xml_file);
  804. }
  805. if(empty($info->instances)) {
  806. return $status;
  807. }
  808. // First of all, iterate over the blocks to see which distinct pages we have
  809. // in our hands and arrange the blocks accordingly.
  810. $pageinstances = array();
  811. foreach($info->instances as $instance) {
  812. //pagetype and pageid black magic, we have to handle the case of blocks for the
  813. //course, blocks from other pages in that course etc etc etc.
  814. if($instance->pagetype == PAGE_COURSE_VIEW) {
  815. // This one's easy...
  816. $instance->pageid = $restore->course_id;
  817. } else if (!empty($CFG->showblocksonmodpages)) {
  818. $parts = explode('-', $instance->pagetype);
  819. if($parts[0] == 'mod') {
  820. if(!$restore->mods[$parts[1]]->restore) {
  821. continue;
  822. }
  823. $getid = backup_getid($restore->backup_unique_code, $parts[1], $instance->pageid);
  824. if (empty($getid->new_id)) {
  825. // Failed, perhaps the module was not included in the restore MDL-13554
  826. continue;
  827. }
  828. $instance->pageid = $getid->new_id;
  829. }
  830. else {
  831. // Not invented here ;-)
  832. continue;
  833. }
  834. } else {
  835. // do not restore activity blocks if disabled
  836. continue;
  837. }
  838. if(!isset($pageinstances[$instance->pagetype])) {
  839. $pageinstances[$instance->pagetype] = array();
  840. }
  841. if(!isset($pageinstances[$instance->pagetype][$instance->pageid])) {
  842. $pageinstances[$instance->pagetype][$instance->pageid] = array();
  843. }
  844. $pageinstances[$instance->pagetype][$instance->pageid][] = $instance;
  845. }
  846. $blocks = get_records_select('block', 'visible = 1', '', 'name, id, multiple');
  847. // For each type of page we have restored
  848. foreach($pageinstances as $thistypeinstances) {
  849. // For each page id of that type
  850. foreach($thistypeinstances as $thisidinstances) {
  851. $addedblocks = array();
  852. $maxweights = array();
  853. // For each block instance in that page
  854. foreach($thisidinstances as $instance) {
  855. if(!isset($blocks[$instance->name])) {
  856. //We are trying to restore a block we don't have...
  857. continue;
  858. }
  859. //If we have already added this block once and multiples aren't allowed, disregard it
  860. if(!$blocks[$instance->name]->multiple && isset($addedblocks[$instance->name])) {
  861. continue;
  862. }
  863. //If its the first block we add to a new position, start weight counter equal to 0.
  864. if(empty($maxweights[$instance->position])) {
  865. $maxweights[$instance->position] = 0;
  866. }
  867. //If the instance weight is greater than the weight counter (we skipped some earlier
  868. //blocks most probably), bring it back in line.
  869. if($instance->weight > $maxweights[$instance->position]) {
  870. $instance->weight = $maxweights[$instance->position];
  871. }
  872. //Add this instance
  873. $instance->blockid = $blocks[$instance->name]->id;
  874. // This will only be set if we come from 1.7 and above backups
  875. // Also, must do this before insert (insert_record unsets id)
  876. if (!empty($instance->id)) {
  877. $oldid = $instance->id;
  878. } else {
  879. $oldid = 0;
  880. }
  881. if ($instance->id = insert_record('block_instance', $instance)) {
  882. // Create block instance
  883. if (!$blockobj = block_instance($instance->name, $instance)) {
  884. $status = false;
  885. break;
  886. }
  887. // Run the block restore if needed
  888. if ($blockobj->backuprestore_instancedata_used()) {
  889. // Get restore information
  890. $data = backup_getid($restore->backup_unique_code,'block_instance',$oldid);
  891. $data->new_id = $instance->id; // For completeness
  892. if (!$blockobj->instance_restore($restore, $data)) {
  893. $status = false;
  894. break;
  895. }
  896. }
  897. // Save oldid after block restore process because info will be over-written with blank string
  898. if ($oldid) {
  899. backup_putid ($restore->backup_unique_code,"block_instance",$oldid,$instance->id);
  900. }
  901. } else {
  902. $status = false;
  903. break;
  904. }
  905. //Get an object for the block and tell it it's been restored so it can update dates
  906. //etc. if necessary
  907. if ($blockobj = block_instance($instance->name,$instance)) {
  908. $blockobj->after_restore($restore);
  909. }
  910. //Now we can increment the weight counter
  911. ++$maxweights[$instance->position];
  912. //Keep track of block types we have already added
  913. $addedblocks[$instance->name] = true;
  914. }
  915. }
  916. }
  917. return $status;
  918. }
  919. //This function creates all the course_sections and course_modules from xml
  920. //when restoring in a new course or simply checks sections and create records
  921. //in backup_ids when restoring in a existing course
  922. function restore_create_sections(&$restore, $xml_file) {
  923. global $CFG,$db;
  924. $status = true;
  925. //Check it exists
  926. if (!file_exists($xml_file)) {
  927. $status = false;
  928. }
  929. //Get info from xml
  930. if ($status) {
  931. $info = restore_read_xml_sections($xml_file);
  932. }
  933. //Put the info in the DB, recoding ids and saving the in backup tables
  934. $sequence = "";
  935. if ($info) {
  936. //For each, section, save it to db
  937. foreach ($info->sections as $key => $sect) {
  938. $sequence = "";
  939. $section = new object();
  940. $section->course = $restore->course_id;
  941. $section->section = $sect->number;
  942. $section->summary = backup_todb($sect->summary);
  943. $section->visible = $sect->visible;
  944. $section->sequence = "";
  945. //Now calculate the section's newid
  946. $newid = 0;
  947. if ($restore->restoreto == RESTORETO_NEW_COURSE) {
  948. //Save it to db (only if restoring to new course)
  949. $newid = insert_record("course_sections",$section);
  950. } else {
  951. //Get section id when restoring in existing course
  952. $rec = get_record("course_sections","course",$restore->course_id,
  953. "section",$section->section);
  954. //If section exists, has empty summary and backup has some summary, use it. MDL-8848
  955. if ($rec && empty($rec->summary) && !empty($section->summary)) {
  956. $rec->summary = $section->summary;
  957. update_record("course_sections", $rec);
  958. }
  959. //If that section doesn't exist, get section 0 (every mod will be
  960. //asigned there
  961. if(!$rec) {
  962. $rec = get_record("course_sections","course",$restore->course_id,
  963. "section","0");
  964. }
  965. //New check. If section 0 doesn't exist, insert it here !!
  966. //Teorically this never should happen but, in practice, some users
  967. //have reported this issue.
  968. if(!$rec) {
  969. $zero_sec = new object();
  970. $zero_sec->course = $restore->course_id;
  971. $zero_sec->section = 0;
  972. $zero_sec->summary = "";
  973. $zero_sec->sequence = "";
  974. $newid = insert_record("course_sections",$zero_sec);
  975. $rec->id = $newid;
  976. $rec->sequence = "";
  977. }
  978. $newid = $rec->id;
  979. $sequence = $rec->sequence;
  980. }
  981. if ($newid) {
  982. //save old and new section id
  983. backup_putid ($restore->backup_unique_code,"course_sections",$key,$newid);
  984. } else {
  985. $status = false;
  986. }
  987. //If all is OK, go with associated mods
  988. if ($status) {
  989. //If we have mods in the section
  990. if (!empty($sect->mods)) {
  991. //For each mod inside section
  992. foreach ($sect->mods as $keym => $mod) {
  993. // Yu: This part is called repeatedly for every instance,
  994. // so it is necessary to set the granular flag and check isset()
  995. // when the first instance of this type of mod is processed.
  996. //if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
  997. if (!isset($restore->mods[$mod->type]->granular)) {
  998. if (isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
  999. // This defines whether we want to restore specific
  1000. // instances of the modules (granular restore), or
  1001. // whether we don't care and just want to restore
  1002. // all module instances (non-granular).
  1003. $restore->mods[$mod->type]->granular = true;
  1004. } else {
  1005. $restore->mods[$mod->type]->granular = false;
  1006. }
  1007. }
  1008. //Check if we've to restore this module (and instance)
  1009. if (!empty($restore->mods[$mod->type]->restore)) {
  1010. if (empty($restore->mods[$mod->type]->granular) // we don't care about per instance
  1011. || (array_key_exists($mod->instance,$restore->mods[$mod->type]->instances)
  1012. && !empty($restore->mods[$mod->type]->instances[$mod->instance]->restore))) {

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