/moodle/backup/restorelib.php
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
- <?php //$Id: restorelib.php,v 1.283.2.79 2009/10/06 01:22:15 stronk7 Exp $
- //Functions used in restore
- require_once($CFG->libdir.'/gradelib.php');
- /**
- * Group backup/restore constants, 0.
- */
- define('RESTORE_GROUPS_NONE', 0);
- /**
- * Group backup/restore constants, 1.
- */
- define('RESTORE_GROUPS_ONLY', 1);
- /**
- * Group backup/restore constants, 2.
- */
- define('RESTORE_GROUPINGS_ONLY', 2);
- /**
- * Group backup/restore constants, course/all.
- */
- define('RESTORE_GROUPS_GROUPINGS', 3);
- //This function unzips a zip file in the same directory that it is
- //It automatically uses pclzip or command line unzip
- function restore_unzip ($file) {
- return unzip_file($file, '', false);
- }
- //This function checks if moodle.xml seems to be a valid xml file
- //(exists, has an xml header and a course main tag
- function restore_check_moodle_file ($file) {
- $status = true;
- //Check if it exists
- if ($status = is_file($file)) {
- //Open it and read the first 200 bytes (chars)
- $handle = fopen ($file, "r");
- $first_chars = fread($handle,200);
- $status = fclose ($handle);
- //Chek if it has the requires strings
- if ($status) {
- $status = strpos($first_chars,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
- if ($status !== false) {
- $status = strpos($first_chars,"<MOODLE_BACKUP>");
- }
- }
- }
- return $status;
- }
- //This function iterates over all modules in backup file, searching for a
- //MODNAME_refresh_events() to execute. Perhaps it should ve moved to central Moodle...
- function restore_refresh_events($restore) {
- global $CFG;
- $status = true;
- //Take all modules in backup
- $modules = $restore->mods;
- //Iterate
- foreach($modules as $name => $module) {
- //Only if the module is being restored
- if (isset($module->restore) && $module->restore == 1) {
- //Include module library
- include_once("$CFG->dirroot/mod/$name/lib.php");
- //If module_refresh_events exists
- $function_name = $name."_refresh_events";
- if (function_exists($function_name)) {
- $status = $function_name($restore->course_id);
- }
- }
- }
- return $status;
- }
- //This function makes all the necessary calls to xxxx_decode_content_links_caller()
- //function in each module/block/course format..., passing them the desired contents to be decoded
- //from backup format to destination site/course in order to mantain inter-activities
- //working in the backup/restore process
- function restore_decode_content_links($restore) {
- global $CFG;
- $status = true;
- if (!defined('RESTORE_SILENTLY')) {
- echo "<ul>";
- }
- // Recode links in the course summary.
- if (!defined('RESTORE_SILENTLY')) {
- echo '<li>' . get_string('from') . ' ' . get_string('course');
- }
- $course = get_record('course', 'id', $restore->course_id, '', '', '', '', 'id,summary');
- $coursesummary = backup_todb($course->summary,false); // Exception: Process FILEPHP (not available when restored) MDL-18222
- $coursesummary = restore_decode_content_links_worker($coursesummary, $restore);
- if ($coursesummary != $course->summary) {
- $course->summary = addslashes($coursesummary);
- if (!update_record('course', $course)) {
- $status = false;
- }
- }
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- // Recode links in section summaries.
- $sections = get_records('course_sections', 'course', $restore->course_id, 'id', 'id,summary');
- if ($sections) {
- if (!defined('RESTORE_SILENTLY')) {
- echo '<li>' . get_string('from') . ' ' . get_string('sections');
- }
- foreach ($sections as $section) {
- $sectionsummary = restore_decode_content_links_worker($section->summary, $restore);
- if ($sectionsummary != $section->summary) {
- $section->summary = addslashes($sectionsummary);
- if (!update_record('course_sections', $section)) {
- $status = false;
- }
- }
- }
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- }
- // Restore links in modules.
- foreach ($restore->mods as $name => $info) {
- //If the module is being restored
- if (isset($info->restore) && $info->restore == 1) {
- //Check if the xxxx_decode_content_links_caller exists
- include_once("$CFG->dirroot/mod/$name/restorelib.php");
- $function_name = $name."_decode_content_links_caller";
- if (function_exists($function_name)) {
- if (!defined('RESTORE_SILENTLY')) {
- echo "<li>".get_string ("from")." ".get_string("modulenameplural",$name);
- }
- $status = $function_name($restore) && $status;
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- }
- }
- }
- // For the course format call its decode_content_links method (if it exists)
- $format = get_field('course', 'format', 'id', $restore->course_id);
- if (file_exists("$CFG->dirroot/course/format/$format/restorelib.php")) {
- include_once("$CFG->dirroot/course/format/$format/restorelib.php");
- $function_name = $format.'_decode_format_content_links_caller';
- if (function_exists($function_name)) {
- if (!defined('RESTORE_SILENTLY')) {
- echo "<li>".get_string ("from")." ".get_string("format").' '.$format;
- }
- $status = $function_name($restore);
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- }
- }
- // Process all html text also in blocks too
- if (!defined('RESTORE_SILENTLY')) {
- echo '<li>'.get_string ('from').' '.get_string('blocks');
- }
- if ($blocks = get_records('block', 'visible', 1)) {
- foreach ($blocks as $block) {
- if ($blockobject = block_instance($block->name)) {
- $blockobject->decode_content_links_caller($restore);
- }
- }
- }
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- // Restore links in questions.
- require_once("$CFG->dirroot/question/restorelib.php");
- if (!defined('RESTORE_SILENTLY')) {
- echo '<li>' . get_string('from') . ' ' . get_string('questions', 'quiz');
- }
- $status = question_decode_content_links_caller($restore) && $status;
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- if (!defined('RESTORE_SILENTLY')) {
- echo "</ul>";
- }
- return $status;
- }
- //This function is called from all xxxx_decode_content_links_caller(),
- //its task is to ask all modules (maybe other linkable objects) to restore
- //links to them.
- function restore_decode_content_links_worker($content,$restore) {
- global $CFG;
- foreach($restore->mods as $name => $info) {
- $function_name = $name."_decode_content_links";
- if (function_exists($function_name)) {
- $content = $function_name($content,$restore);
- }
- }
- // For the current format, call decode_format_content_links if it exists
- static $format_function_name;
- if (!isset($format_function_name)) {
- $format_function_name = false;
- if ($format = get_field('course', 'format', 'id', $restore->course_id)) {
- if (file_exists("$CFG->dirroot/course/format/$format/restorelib.php")) {
- include_once("$CFG->dirroot/course/format/$format/restorelib.php");
- $function_name = $format.'_decode_format_content_links';
- if (function_exists($function_name)) {
- $format_function_name = $function_name;
- }
- }
- }
- }
- // If the above worked - then we have a function to call
- if ($format_function_name) {
- $content = $format_function_name($content, $restore);
- }
- // For each block, call its encode_content_links method
- static $blockobjects = null;
- if (!isset($blockobjects)) {
- $blockobjects = array();
- if ($blocks = get_records('block', 'visible', 1)) {
- foreach ($blocks as $block) {
- if ($blockobject = block_instance($block->name)) {
- $blockobjects[] = $blockobject;
- }
- }
- }
- }
-
- foreach ($blockobjects as $blockobject) {
- $content = $blockobject->decode_content_links($content,$restore);
- }
- return $content;
- }
- //This function converts all the wiki texts in the restored course
- //to the Markdown format. Used only for backup files prior 2005041100.
- //It calls every module xxxx_convert_wiki2markdown function
- function restore_convert_wiki2markdown($restore) {
- $status = true;
- if (!defined('RESTORE_SILENTLY')) {
- echo "<ul>";
- }
- foreach ($restore->mods as $name => $info) {
- //If the module is being restored
- if ($info->restore == 1) {
- //Check if the xxxx_restore_wiki2markdown exists
- $function_name = $name."_restore_wiki2markdown";
- if (function_exists($function_name)) {
- $status = $function_name($restore);
- if (!defined('RESTORE_SILENTLY')) {
- echo "<li>".get_string("modulenameplural",$name);
- echo '</li>';
- }
- }
- }
- }
- if (!defined('RESTORE_SILENTLY')) {
- echo "</ul>";
- }
- return $status;
- }
- //This function receives a wiki text in the restore process and
- //return it with every link to modules " modulename:moduleid"
- //converted if possible. See the space before modulename!!
- function restore_decode_wiki_content($content,$restore) {
- global $CFG;
- $result = $content;
- $searchstring='/ ([a-zA-Z]+):([0-9]+)\(([^)]+)\)/';
- //We look for it
- preg_match_all($searchstring,$content,$foundset);
- //If found, then we are going to look for its new id (in backup tables)
- if ($foundset[0]) {
- //print_object($foundset); //Debug
- //Iterate over foundset[2]. They are the old_ids
- foreach($foundset[2] as $old_id) {
- //We get the needed variables here (course id)
- $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
- //Personalize the searchstring
- $searchstring='/ ([a-zA-Z]+):'.$old_id.'\(([^)]+)\)/';
- //If it is a link to this course, update the link to its new location
- if($rec->new_id) {
- //Now replace it
- $result= preg_replace($searchstring,' $1:'.$rec->new_id.'($2)',$result);
- } else {
- //It's a foreign link so redirect it to its original URL
- $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/$1/view.php?id='.$old_id.'($2)',$result);
- }
- }
- }
- return $result;
- }
- //This function read the xml file and store it data from the info zone in an object
- function restore_read_xml_info ($xml_file) {
- //We call the main read_xml function, with todo = INFO
- $info = restore_read_xml ($xml_file,"INFO",false);
- return $info;
- }
- //This function read the xml file and store it data from the course header zone in an object
- function restore_read_xml_course_header ($xml_file) {
- //We call the main read_xml function, with todo = COURSE_HEADER
- $info = restore_read_xml ($xml_file,"COURSE_HEADER",false);
- return $info;
- }
- //This function read the xml file and store its data from the blocks in a object
- function restore_read_xml_blocks ($restore, $xml_file) {
- //We call the main read_xml function, with todo = BLOCKS
- $info = restore_read_xml ($xml_file,'BLOCKS',$restore);
- return $info;
- }
- //This function read the xml file and store its data from the sections in a object
- function restore_read_xml_sections ($xml_file) {
- //We call the main read_xml function, with todo = SECTIONS
- $info = restore_read_xml ($xml_file,"SECTIONS",false);
- return $info;
- }
- //This function read the xml file and store its data from the course format in an object
- function restore_read_xml_formatdata ($xml_file) {
- //We call the main read_xml function, with todo = FORMATDATA
- $info = restore_read_xml ($xml_file,'FORMATDATA',false);
- return $info;
- }
- //This function read the xml file and store its data from the metacourse in a object
- function restore_read_xml_metacourse ($xml_file) {
- //We call the main read_xml function, with todo = METACOURSE
- $info = restore_read_xml ($xml_file,"METACOURSE",false);
- return $info;
- }
- //This function read the xml file and store its data from the gradebook in a object
- function restore_read_xml_gradebook ($restore, $xml_file) {
- //We call the main read_xml function, with todo = GRADEBOOK
- $info = restore_read_xml ($xml_file,"GRADEBOOK",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the users in
- //backup_ids->info db (and user's id in $info)
- function restore_read_xml_users ($restore,$xml_file) {
- //We call the main read_xml function, with todo = USERS
- $info = restore_read_xml ($xml_file,"USERS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the messages in
- //backup_ids->message backup_ids->message_read and backup_ids->contact and db (and their counters in info)
- function restore_read_xml_messages ($restore,$xml_file) {
- //We call the main read_xml function, with todo = MESSAGES
- $info = restore_read_xml ($xml_file,"MESSAGES",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the blogs in
- //backup_ids->blog and backup_ids->blog_tag and db (and their counters in info)
- function restore_read_xml_blogs ($restore,$xml_file) {
- //We call the main read_xml function, with todo = BLOGS
- $info = restore_read_xml ($xml_file,"BLOGS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the questions in
- //backup_ids->info db (and category's id in $info)
- function restore_read_xml_questions ($restore,$xml_file) {
- //We call the main read_xml function, with todo = QUESTIONS
- $info = restore_read_xml ($xml_file,"QUESTIONS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the scales in
- //backup_ids->info db (and scale's id in $info)
- function restore_read_xml_scales ($restore,$xml_file) {
- //We call the main read_xml function, with todo = SCALES
- $info = restore_read_xml ($xml_file,"SCALES",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the groups in
- //backup_ids->info db (and group's id in $info)
- function restore_read_xml_groups ($restore,$xml_file) {
- //We call the main read_xml function, with todo = GROUPS
- $info = restore_read_xml ($xml_file,"GROUPS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the groupings in
- //backup_ids->info db (and grouping's id in $info)
- function restore_read_xml_groupings ($restore,$xml_file) {
- //We call the main read_xml function, with todo = GROUPINGS
- $info = restore_read_xml ($xml_file,"GROUPINGS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the groupings in
- //backup_ids->info db (and grouping's id in $info)
- function restore_read_xml_groupings_groups ($restore,$xml_file) {
- //We call the main read_xml function, with todo = GROUPINGS
- $info = restore_read_xml ($xml_file,"GROUPINGSGROUPS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the events (course) in
- //backup_ids->info db (and event's id in $info)
- function restore_read_xml_events ($restore,$xml_file) {
- //We call the main read_xml function, with todo = EVENTS
- $info = restore_read_xml ($xml_file,"EVENTS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the modules in
- //backup_ids->info
- function restore_read_xml_modules ($restore,$xml_file) {
- //We call the main read_xml function, with todo = MODULES
- $info = restore_read_xml ($xml_file,"MODULES",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the logs in
- //backup_ids->info
- function restore_read_xml_logs ($restore,$xml_file) {
- //We call the main read_xml function, with todo = LOGS
- $info = restore_read_xml ($xml_file,"LOGS",$restore);
- return $info;
- }
- function restore_read_xml_roles ($xml_file) {
- //We call the main read_xml function, with todo = ROLES
- $info = restore_read_xml ($xml_file,"ROLES",false);
- return $info;
- }
- //This function prints the contents from the info parammeter passed
- function restore_print_info ($info) {
- global $CFG;
- $status = true;
- if ($info) {
- $table = new object();
- //This is tha align to every ingo table
- $table->align = array ("right","left");
- //This is the nowrap clause
- $table->wrap = array ("","nowrap");
- //The width
- $table->width = "70%";
- //Put interesting info in table
- //The backup original name
- $tab[0][0] = "<b>".get_string("backuporiginalname").":</b>";
- $tab[0][1] = $info->backup_name;
- //The moodle version
- $tab[1][0] = "<b>".get_string("moodleversion").":</b>";
- $tab[1][1] = $info->backup_moodle_release." (".$info->backup_moodle_version.")";
- //The backup version
- $tab[2][0] = "<b>".get_string("backupversion").":</b>";
- $tab[2][1] = $info->backup_backup_release." (".$info->backup_backup_version.")";
- //The backup date
- $tab[3][0] = "<b>".get_string("backupdate").":</b>";
- $tab[3][1] = userdate($info->backup_date);
- //Is this the same Moodle install?
- if (!empty($info->original_siteidentifier)) {
- $tab[4][0] = "<b>".get_string("backupfromthissite").":</b>";
- if (backup_is_same_site($info)) {
- $tab[4][1] = get_string('yes');
- } else {
- $tab[4][1] = get_string('no');
- }
- }
- //Print title
- print_heading(get_string("backup").":");
- $table->data = $tab;
- //Print backup general info
- print_table($table);
- if ($info->backup_backup_version <= 2005070500) {
- notify(get_string('backupnonisowarning')); // Message informing that this backup may not work!
- }
- //Now backup contents in another table
- $tab = array();
- //First mods info
- $mods = $info->mods;
- $elem = 0;
- foreach ($mods as $key => $mod) {
- $tab[$elem][0] = "<b>".get_string("modulenameplural",$key).":</b>";
- if ($mod->backup == "false") {
- $tab[$elem][1] = get_string("notincluded");
- } else {
- if ($mod->userinfo == "true") {
- $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
- } else {
- $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
- }
- if (isset($mod->instances) && is_array($mod->instances) && count($mod->instances)) {
- foreach ($mod->instances as $instance) {
- if ($instance->backup) {
- $elem++;
- $tab[$elem][0] = $instance->name;
- if ($instance->userinfo == 'true') {
- $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
- } else {
- $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
- }
- }
- }
- }
- }
- $elem++;
- }
- //Metacourse info
- $tab[$elem][0] = "<b>".get_string("metacourse").":</b>";
- if ($info->backup_metacourse == "true") {
- $tab[$elem][1] = get_string("yes");
- } else {
- $tab[$elem][1] = get_string("no");
- }
- $elem++;
- //Users info
- $tab[$elem][0] = "<b>".get_string("users").":</b>";
- $tab[$elem][1] = get_string($info->backup_users);
- $elem++;
- //Logs info
- $tab[$elem][0] = "<b>".get_string("logs").":</b>";
- if ($info->backup_logs == "true") {
- $tab[$elem][1] = get_string("yes");
- } else {
- $tab[$elem][1] = get_string("no");
- }
- $elem++;
- //User Files info
- $tab[$elem][0] = "<b>".get_string("userfiles").":</b>";
- if ($info->backup_user_files == "true") {
- $tab[$elem][1] = get_string("yes");
- } else {
- $tab[$elem][1] = get_string("no");
- }
- $elem++;
- //Course Files info
- $tab[$elem][0] = "<b>".get_string("coursefiles").":</b>";
- if ($info->backup_course_files == "true") {
- $tab[$elem][1] = get_string("yes");
- } else {
- $tab[$elem][1] = get_string("no");
- }
- $elem++;
- //site Files info
- $tab[$elem][0] = "<b>".get_string("sitefiles").":</b>";
- if (isset($info->backup_site_files) && $info->backup_site_files == "true") {
- $tab[$elem][1] = get_string("yes");
- } else {
- $tab[$elem][1] = get_string("no");
- }
- $elem++;
- //gradebook history info
- $tab[$elem][0] = "<b>".get_string('gradebookhistories', 'grades').":</b>";
- if (isset($info->gradebook_histories) && $info->gradebook_histories == "true") {
- $tab[$elem][1] = get_string("yes");
- } else {
- $tab[$elem][1] = get_string("no");
- }
- $elem++;
- //Messages info (only showed if present)
- if ($info->backup_messages == 'true') {
- $tab[$elem][0] = "<b>".get_string('messages','message').":</b>";
- $tab[$elem][1] = get_string('yes');
- $elem++;
- } else {
- //Do nothing
- }
- $elem++;
- //Blogs info (only showed if present)
- if (isset($info->backup_blogs) && $info->backup_blogs == 'true') {
- $tab[$elem][0] = "<b>".get_string('blogs','blog').":</b>";
- $tab[$elem][1] = get_string('yes');
- $elem++;
- } else {
- //Do nothing
- }
- $table->data = $tab;
- //Print title
- print_heading(get_string("backupdetails").":");
- //Print backup general info
- print_table($table);
- } else {
- $status = false;
- }
- return $status;
- }
- //This function prints the contents from the course_header parammeter passed
- function restore_print_course_header ($course_header) {
- $status = true;
- if ($course_header) {
- $table = new object();
- //This is tha align to every ingo table
- $table->align = array ("right","left");
- //The width
- $table->width = "70%";
- //Put interesting course header in table
- //The course name
- $tab[0][0] = "<b>".get_string("name").":</b>";
- $tab[0][1] = $course_header->course_fullname." (".$course_header->course_shortname.")";
- //The course summary
- $tab[1][0] = "<b>".get_string("summary").":</b>";
- $tab[1][1] = $course_header->course_summary;
- $table->data = $tab;
- //Print title
- print_heading(get_string("course").":");
- //Print backup course header info
- print_table($table);
- } else {
- $status = false;
- }
- return $status;
- }
- //This function create a new course record.
- //When finished, course_header contains the id of the new course
- function restore_create_new_course($restore,&$course_header) {
- global $CFG;
- $status = true;
- $fullname = $course_header->course_fullname;
- $shortname = $course_header->course_shortname;
- $currentfullname = "";
- $currentshortname = "";
- $counter = 0;
- //Iteratere while the name exists
- do {
- if ($counter) {
- $suffixfull = " ".get_string("copyasnoun")." ".$counter;
- $suffixshort = "_".$counter;
- } else {
- $suffixfull = "";
- $suffixshort = "";
- }
- $currentfullname = $fullname.$suffixfull;
- // Limit the size of shortname - database column accepts <= 100 chars
- $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort;
- $coursefull = get_record("course","fullname",addslashes($currentfullname));
- $courseshort = get_record("course","shortname",addslashes($currentshortname));
- $counter++;
- } while ($coursefull || $courseshort);
- //New name = currentname
- $course_header->course_fullname = $currentfullname;
- $course_header->course_shortname = $currentshortname;
- // first try to get it from restore
- if ($restore->restore_restorecatto) {
- $category = get_record('course_categories', 'id', $restore->restore_restorecatto);
- }
- // else we try to get it from the xml file
- //Now calculate the category
- if (empty($category)) {
- $category = get_record("course_categories","id",$course_header->category->id,
- "name",addslashes($course_header->category->name));
- }
- //If no exists, try by name only
- if (!$category) {
- $category = get_record("course_categories","name",addslashes($course_header->category->name));
- }
- //If no exists, get category id 1
- if (!$category) {
- $category = get_record("course_categories","id","1");
- }
- //If category 1 doesn'exists, lets create the course category (get it from backup file)
- if (!$category) {
- $ins_category = new object();
- $ins_category->name = addslashes($course_header->category->name);
- $ins_category->parent = 0;
- $ins_category->sortorder = 0;
- $ins_category->coursecount = 0;
- $ins_category->visible = 0; //To avoid interferences with the rest of the site
- $ins_category->timemodified = time();
- $newid = insert_record("course_categories",$ins_category);
- $category->id = $newid;
- $category->name = $course_header->category->name;
- }
- //If exists, put new category id
- if ($category) {
- $course_header->category->id = $category->id;
- $course_header->category->name = $category->name;
- //Error, cannot locate category
- } else {
- $course_header->category->id = 0;
- $course_header->category->name = get_string("unknowncategory");
- $status = false;
- }
- //Create the course_object
- if ($status) {
- $course = new object();
- $course->category = addslashes($course_header->category->id);
- $course->password = addslashes($course_header->course_password);
- $course->fullname = addslashes($course_header->course_fullname);
- $course->shortname = addslashes($course_header->course_shortname);
- $course->idnumber = addslashes($course_header->course_idnumber);
- $course->idnumber = ''; //addslashes($course_header->course_idnumber); // we don't want this at all.
- $course->summary = addslashes($course_header->course_summary);
- $course->format = addslashes($course_header->course_format);
- $course->showgrades = addslashes($course_header->course_showgrades);
- $course->newsitems = addslashes($course_header->course_newsitems);
- $course->teacher = addslashes($course_header->course_teacher);
- $course->teachers = addslashes($course_header->course_teachers);
- $course->student = addslashes($course_header->course_student);
- $course->students = addslashes($course_header->course_students);
- $course->guest = addslashes($course_header->course_guest);
- $course->startdate = addslashes($course_header->course_startdate);
- $course->startdate += $restore->course_startdateoffset;
- $course->numsections = addslashes($course_header->course_numsections);
- //$course->showrecent = addslashes($course_header->course_showrecent); INFO: This is out in 1.3
- $course->maxbytes = addslashes($course_header->course_maxbytes);
- $course->showreports = addslashes($course_header->course_showreports);
- if (isset($course_header->course_groupmode)) {
- $course->groupmode = addslashes($course_header->course_groupmode);
- }
- if (isset($course_header->course_groupmodeforce)) {
- $course->groupmodeforce = addslashes($course_header->course_groupmodeforce);
- }
- if (isset($course_header->course_defaultgroupingid)) {
- //keep the original now - convert after groupings restored
- $course->defaultgroupingid = addslashes($course_header->course_defaultgroupingid);
- }
- $course->lang = addslashes($course_header->course_lang);
- $course->theme = addslashes($course_header->course_theme);
- $course->cost = addslashes($course_header->course_cost);
- $course->currency = isset($course_header->course_currency)?addslashes($course_header->course_currency):'';
- $course->marker = addslashes($course_header->course_marker);
- $course->visible = addslashes($course_header->course_visible);
- $course->hiddensections = addslashes($course_header->course_hiddensections);
- $course->timecreated = addslashes($course_header->course_timecreated);
- $course->timemodified = addslashes($course_header->course_timemodified);
- $course->metacourse = addslashes($course_header->course_metacourse);
- $course->expirynotify = isset($course_header->course_expirynotify) ? addslashes($course_header->course_expirynotify):0;
- $course->notifystudents = isset($course_header->course_notifystudents) ? addslashes($course_header->course_notifystudents) : 0;
- $course->expirythreshold = isset($course_header->course_expirythreshold) ? addslashes($course_header->course_expirythreshold) : 0;
- $course->enrollable = isset($course_header->course_enrollable) ? addslashes($course_header->course_enrollable) : 1;
- $course->enrolstartdate = isset($course_header->course_enrolstartdate) ? addslashes($course_header->course_enrolstartdate) : 0;
- if ($course->enrolstartdate) { //Roll course dates
- $course->enrolstartdate += $restore->course_startdateoffset;
- }
- $course->enrolenddate = isset($course_header->course_enrolenddate) ? addslashes($course_header->course_enrolenddate) : 0;
- if ($course->enrolenddate) { //Roll course dates
- $course->enrolenddate += $restore->course_startdateoffset;
- }
- $course->enrolperiod = addslashes($course_header->course_enrolperiod);
- //Calculate sortorder field
- $sortmax = get_record_sql('SELECT MAX(sortorder) AS max
- FROM ' . $CFG->prefix . 'course
- WHERE category=' . $course->category);
- if (!empty($sortmax->max)) {
- $course->sortorder = $sortmax->max + 1;
- unset($sortmax);
- } else {
- $course->sortorder = 100;
- }
- //Now, recode some languages (Moodle 1.5)
- if ($course->lang == 'ma_nt') {
- $course->lang = 'mi_nt';
- }
- //Disable course->metacourse if avoided in restore config
- if (!$restore->metacourse) {
- $course->metacourse = 0;
- }
- //Check if the theme exists in destination server
- $themes = get_list_of_themes();
- if (!in_array($course->theme, $themes)) {
- $course->theme = '';
- }
- //Now insert the record
- $newid = insert_record("course",$course);
- if ($newid) {
- //save old and new course id
- backup_putid ($restore->backup_unique_code,"course",$course_header->course_id,$newid);
- //Replace old course_id in course_header
- $course_header->course_id = $newid;
- } else {
- $status = false;
- }
- }
- return $status;
- }
- //This function creates all the block stuff when restoring courses
- //It calls selectively to restore_create_block_instances() for 1.5
- //and above backups. Upwards compatible with old blocks.
- function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_file) {
- global $CFG;
- $status = true;
- blocks_delete_all_on_page(PAGE_COURSE_VIEW, $restore->course_id);
- if (empty($backup_block_format)) { // This is a backup from Moodle < 1.5
- if (empty($blockinfo)) {
- // Looks like it's from Moodle < 1.3. Let's give the course default blocks...
- $newpage = page_create_object(PAGE_COURSE_VIEW, $restore->course_id);
- blocks_repopulate_page($newpage);
- } else {
- // We just have a blockinfo field, this is a legacy 1.4 or 1.3 backup
- $blockrecords = get_records_select('block', '', '', 'name, id');
- $temp_blocks_l = array();
- $temp_blocks_r = array();
- @list($temp_blocks_l, $temp_blocks_r) = explode(':', $blockinfo);
- $temp_blocks = array(BLOCK_POS_LEFT => explode(',', $temp_blocks_l), BLOCK_POS_RIGHT => explode(',', $temp_blocks_r));
- foreach($temp_blocks as $blockposition => $blocks) {
- $blockweight = 0;
- foreach($blocks as $blockname) {
- if(!isset($blockrecords[$blockname])) {
- // We don't know anything about this block!
- continue;
- }
- $blockinstance = new stdClass;
- // Remove any - prefix before doing the name-to-id mapping
- if(substr($blockname, 0, 1) == '-') {
- $blockname = substr($blockname, 1);
- $blockinstance->visible = 0;
- } else {
- $blockinstance->visible = 1;
- }
- $blockinstance->blockid = $blockrecords[$blockname]->id;
- $blockinstance->pageid = $restore->course_id;
- $blockinstance->pagetype = PAGE_COURSE_VIEW;
- $blockinstance->position = $blockposition;
- $blockinstance->weight = $blockweight;
- if(!$status = insert_record('block_instance', $blockinstance)) {
- $status = false;
- }
- ++$blockweight;
- }
- }
- }
- } else if($backup_block_format == 'instances') {
- $status = restore_create_block_instances($restore,$xml_file);
- }
- return $status;
- }
- //This function creates all the block_instances from xml when restoring in a
- //new course
- function restore_create_block_instances($restore,$xml_file) {
- global $CFG;
- $status = true;
- //Check it exists
- if (!file_exists($xml_file)) {
- $status = false;
- }
- //Get info from xml
- if ($status) {
- $info = restore_read_xml_blocks($restore,$xml_file);
- }
- if(empty($info->instances)) {
- return $status;
- }
- // First of all, iterate over the blocks to see which distinct pages we have
- // in our hands and arrange the blocks accordingly.
- $pageinstances = array();
- foreach($info->instances as $instance) {
- //pagetype and pageid black magic, we have to handle the case of blocks for the
- //course, blocks from other pages in that course etc etc etc.
- if($instance->pagetype == PAGE_COURSE_VIEW) {
- // This one's easy...
- $instance->pageid = $restore->course_id;
- } else if (!empty($CFG->showblocksonmodpages)) {
- $parts = explode('-', $instance->pagetype);
- if($parts[0] == 'mod') {
- if(!$restore->mods[$parts[1]]->restore) {
- continue;
- }
- $getid = backup_getid($restore->backup_unique_code, $parts[1], $instance->pageid);
- if (empty($getid->new_id)) {
- // Failed, perhaps the module was not included in the restore MDL-13554
- continue;
- }
- $instance->pageid = $getid->new_id;
- }
- else {
- // Not invented here ;-)
- continue;
- }
- } else {
- // do not restore activity blocks if disabled
- continue;
- }
- if(!isset($pageinstances[$instance->pagetype])) {
- $pageinstances[$instance->pagetype] = array();
- }
- if(!isset($pageinstances[$instance->pagetype][$instance->pageid])) {
- $pageinstances[$instance->pagetype][$instance->pageid] = array();
- }
- $pageinstances[$instance->pagetype][$instance->pageid][] = $instance;
- }
- $blocks = get_records_select('block', 'visible = 1', '', 'name, id, multiple');
- // For each type of page we have restored
- foreach($pageinstances as $thistypeinstances) {
- // For each page id of that type
- foreach($thistypeinstances as $thisidinstances) {
- $addedblocks = array();
- $maxweights = array();
- // For each block instance in that page
- foreach($thisidinstances as $instance) {
- if(!isset($blocks[$instance->name])) {
- //We are trying to restore a block we don't have...
- continue;
- }
- //If we have already added this block once and multiples aren't allowed, disregard it
- if(!$blocks[$instance->name]->multiple && isset($addedblocks[$instance->name])) {
- continue;
- }
- //If its the first block we add to a new position, start weight counter equal to 0.
- if(empty($maxweights[$instance->position])) {
- $maxweights[$instance->position] = 0;
- }
- //If the instance weight is greater than the weight counter (we skipped some earlier
- //blocks most probably), bring it back in line.
- if($instance->weight > $maxweights[$instance->position]) {
- $instance->weight = $maxweights[$instance->position];
- }
- //Add this instance
- $instance->blockid = $blocks[$instance->name]->id;
- // This will only be set if we come from 1.7 and above backups
- // Also, must do this before insert (insert_record unsets id)
- if (!empty($instance->id)) {
- $oldid = $instance->id;
- } else {
- $oldid = 0;
- }
- if ($instance->id = insert_record('block_instance', $instance)) {
- // Create block instance
- if (!$blockobj = block_instance($instance->name, $instance)) {
- $status = false;
- break;
- }
- // Run the block restore if needed
- if ($blockobj->backuprestore_instancedata_used()) {
- // Get restore information
- $data = backup_getid($restore->backup_unique_code,'block_instance',$oldid);
- $data->new_id = $instance->id; // For completeness
- if (!$blockobj->instance_restore($restore, $data)) {
- $status = false;
- break;
- }
- }
- // Save oldid after block restore process because info will be over-written with blank string
- if ($oldid) {
- backup_putid ($restore->backup_unique_code,"block_instance",$oldid,$instance->id);
- }
- } else {
- $status = false;
- break;
- }
- //Get an object for the block and tell it it's been restored so it can update dates
- //etc. if necessary
- if ($blockobj = block_instance($instance->name,$instance)) {
- $blockobj->after_restore($restore);
- }
- //Now we can increment the weight counter
- ++$maxweights[$instance->position];
- //Keep track of block types we have already added
- $addedblocks[$instance->name] = true;
- }
- }
- }
- return $status;
- }
- //This function creates all the course_sections and course_modules from xml
- //when restoring in a new course or simply checks sections and create records
- //in backup_ids when restoring in a existing course
- function restore_create_sections(&$restore, $xml_file) {
- global $CFG,$db;
- $status = true;
- //Check it exists
- if (!file_exists($xml_file)) {
- $status = false;
- }
- //Get info from xml
- if ($status) {
- $info = restore_read_xml_sections($xml_file);
- }
- //Put the info in the DB, recoding ids and saving the in backup tables
- $sequence = "";
- if ($info) {
- //For each, section, save it to db
- foreach ($info->sections as $key => $sect) {
- $sequence = "";
- $section = new object();
- $section->course = $restore->course_id;
- $section->section = $sect->number;
- $section->summary = backup_todb($sect->summary);
- $section->visible = $sect->visible;
- $section->sequence = "";
- //Now calculate the section's newid
- $newid = 0;
- if ($restore->restoreto == RESTORETO_NEW_COURSE) {
- //Save it to db (only if restoring to new course)
- $newid = insert_record("course_sections",$section);
- } else {
- //Get section id when restoring in existing course
- $rec = get_record("course_sections","course",$restore->course_id,
- "section",$section->section);
- //If section exists, has empty summary and backup has some summary, use it. MDL-8848
- if ($rec && empty($rec->summary) && !empty($section->summary)) {
- $rec->summary = $section->summary;
- update_record("course_sections", $rec);
- }
- //If that section doesn't exist, get section 0 (every mod will be
- //asigned there
- if(!$rec) {
- $rec = get_record("course_sections","course",$restore->course_id,
- "section","0");
- }
- //New check. If section 0 doesn't exist, insert it here !!
- //Teorically this never should happen but, in practice, some users
- //have reported this issue.
- if(!$rec) {
- $zero_sec = new object();
- $zero_sec->course = $restore->course_id;
- $zero_sec->section = 0;
- $zero_sec->summary = "";
- $zero_sec->sequence = "";
- $newid = insert_record("course_sections",$zero_sec);
- $rec->id = $newid;
- $rec->sequence = "";
- }
- $newid = $rec->id;
- $sequence = $rec->sequence;
- }
- if ($newid) {
- //save old and new section id
- backup_putid ($restore->backup_unique_code,"course_sections",$key,$newid);
- } else {
- $status = false;
- }
- //If all is OK, go with associated mods
- if ($status) {
- //If we have mods in the section
- if (!empty($sect->mods)) {
- //For each mod inside section
- foreach ($sect->mods as $keym => $mod) {
- // Yu: This part is called repeatedly for every instance,
- // so it is necessary to set the granular flag and check isset()
- // when the first instance of this type of mod is processed.
- //if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
- if (!isset($restore->mods[$mod->type]->granular)) {
- if (isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
- // This defines whether we want to restore specific
- // instances of the modules (granular restore), or
- // whether we don't care and just want to restore
- // all module instances (non-granular).
- $restore->mods[$mod->type]->granular = true;
- } else {
- $restore->mods[$mod->type]->granular = false;
- }
- }
- //Check if we've to restore this module (and instance)
- if (!empty($restore->mods[$mod->type]->restore)) {
- if (empty($restore->mods[$mod->type]->granular) // we don't care about per instance
- || (array_key_exists($mod->instance,$restore->mods[$mod->type]->instances)
- && !empty($restore->mods[$mod->type]->instances[$mod->instance]->restore))) {
- …
Large files files are truncated, but you can click here to view the full file