Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

9467 lines (8485 sloc) 482.625 kB
<?php //$Id$
//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;
}
/**
* Given one user object (from backup file), perform all the neccesary
* checks is order to decide how that user will be handled on restore.
*
* Note the function requires $user->mnethostid to be already calculated
* so it's caller responsibility to set it
*
* This function is used both by @restore_precheck_users() and
* @restore_create_users() to get consistent results in both places
*
* It returns:
* - one user object (from DB), if match has been found and user will be remapped
* - boolean true if the user needs to be created
* - boolean false if some conflict happened and the user cannot be handled
*
* Each test is responsible for returning its results and interrupt
* execution. At the end, boolean true (user needs to be created) will be
* returned if no test has interrupted that.
*
* Here it's the logic applied, keep it updated:
*
* If restoring users from same site backup:
* 1A - Normal check: If match by id and username and mnethost => ok, return target user
* 1B - Handle users deleted in DB and "alive" in backup file:
* If match by id and mnethost and user is deleted in DB and
* (match by username LIKE 'backup_email.%' or by non empty email = md5(username)) => ok, return target user
* 1C - Handle users deleted in backup file and "alive" in DB:
* If match by id and mnethost and user is deleted in backup file
* and match by email = email_without_time(backup_email) => ok, return target user
* 1D - Conflict: If match by username and mnethost and doesn't match by id => conflict, return false
* 1E - None of the above, return true => User needs to be created
*
* if restoring from another site backup (cannot match by id here, replace it by email/firstaccess combination):
* 2A - Normal check: If match by username and mnethost and (email or non-zero firstaccess) => ok, return target user
* 2B - Handle users deleted in DB and "alive" in backup file:
* 2B1 - If match by mnethost and user is deleted in DB and not empty email = md5(username) and
* (username LIKE 'backup_email.%' or non-zero firstaccess) => ok, return target user
* 2B2 - If match by mnethost and user is deleted in DB and
* username LIKE 'backup_email.%' and non-zero firstaccess) => ok, return target user
* (to cover situations were md5(username) wasn't implemented on delete we requiere both)
* 2C - Handle users deleted in backup file and "alive" in DB:
* If match mnethost and user is deleted in backup file
* and by email = email_without_time(backup_email) and non-zero firstaccess=> ok, return target user
* 2D - Conflict: If match by username and mnethost and not by (email or non-zero firstaccess) => conflict, return false
* 2E - None of the above, return true => User needs to be created
*
* Note: for DB deleted users email is stored in username field, hence we
* are looking there for emails. See delete_user()
* Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
* hence we are looking there for usernames if not empty. See delete_user()
*/
function restore_check_user($restore, $user) {
global $CFG;
// Verify mnethostid is set, return error if not
// it's parent responsibility to define that before
// arriving here
if (empty($user->mnethostid)) {
debugging("restore_check_user() wrong use, mnethostid not set for user $user->username", DEBUG_DEVELOPER);
return false;
}
// Handle checks from same site backups
if (backup_is_same_site($restore) && empty($CFG->forcedifferentsitecheckingusersonrestore)) {
// 1A - If match by id and username and mnethost => ok, return target user
if ($rec = get_record('user', 'id', $user->id, 'username', addslashes($user->username), 'mnethostid', $user->mnethostid)) {
return $rec; // Matching user found, return it
}
// 1B - Handle users deleted in DB and "alive" in backup file
// Note: for DB deleted users email is stored in username field, hence we
// are looking there for emails. See delete_user()
// Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
// hence we are looking there for usernames if not empty. See delete_user()
// If match by id and mnethost and user is deleted in DB and
// match by username LIKE 'backup_email.%' or by non empty email = md5(username) => ok, return target user
if ($rec = get_record_sql("SELECT *
FROM {$CFG->prefix}user u
WHERE id = $user->id
AND mnethostid = $user->mnethostid
AND deleted = 1
AND (
username LIKE '".addslashes($user->email).".%'
OR (
".sql_isnotempty('user', 'email', false, false)."
AND email = '".md5($user->username)."'
)
)")) {
return $rec; // Matching user, deleted in DB found, return it
}
// 1C - Handle users deleted in backup file and "alive" in DB
// If match by id and mnethost and user is deleted in backup file
// and match by email = email_without_time(backup_email) => ok, return target user
if ($user->deleted) {
// Note: for DB deleted users email is stored in username field, hence we
// are looking there for emails. See delete_user()
// Trim time() from email
$trimemail = preg_replace('/(.*?)\.[0-9]+.?$/', '\\1', $user->username);
if ($rec = get_record_sql("SELECT *
FROM {$CFG->prefix}user u
WHERE id = $user->id
AND mnethostid = $user->mnethostid
AND email = '".addslashes($trimemail)."'")) {
return $rec; // Matching user, deleted in backup file found, return it
}
}
// 1D - If match by username and mnethost and doesn't match by id => conflict, return false
if ($rec = get_record('user', 'username', addslashes($user->username), 'mnethostid', $user->mnethostid)) {
if ($user->id != $rec->id) {
return false; // Conflict, username already exists and belongs to another id
}
}
// Handle checks from different site backups
} else {
// 2A - If match by username and mnethost and
// (email or non-zero firstaccess) => ok, return target user
if ($rec = get_record_sql("SELECT *
FROM {$CFG->prefix}user u
WHERE username = '".addslashes($user->username)."'
AND mnethostid = $user->mnethostid
AND (
email = '".addslashes($user->email)."'
OR (
firstaccess != 0
AND firstaccess = $user->firstaccess
)
)")) {
return $rec; // Matching user found, return it
}
// 2B - Handle users deleted in DB and "alive" in backup file
// Note: for DB deleted users email is stored in username field, hence we
// are looking there for emails. See delete_user()
// Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
// hence we are looking there for usernames if not empty. See delete_user()
// 2B1 - If match by mnethost and user is deleted in DB and not empty email = md5(username) and
// (by username LIKE 'backup_email.%' or non-zero firstaccess) => ok, return target user
if ($rec = get_record_sql("SELECT *
FROM {$CFG->prefix}user u
WHERE mnethostid = $user->mnethostid
AND deleted = 1
AND ".sql_isnotempty('user', 'email', false, false)."
AND email = '".md5($user->username)."'
AND (
username LIKE '".addslashes($user->email).".%'
OR (
firstaccess != 0
AND firstaccess = $user->firstaccess
)
)")) {
return $rec; // Matching user found, return it
}
// 2B2 - If match by mnethost and user is deleted in DB and
// username LIKE 'backup_email.%' and non-zero firstaccess) => ok, return target user
// (this covers situations where md5(username) wasn't being stored so we require both
// the email & non-zero firstaccess to match)
if ($rec = get_record_sql("SELECT *
FROM {$CFG->prefix}user u
WHERE mnethostid = $user->mnethostid
AND deleted = 1
AND username LIKE '".addslashes($user->email).".%'
AND firstaccess != 0
AND firstaccess = $user->firstaccess")) {
return $rec; // Matching user found, return it
}
// 2C - Handle users deleted in backup file and "alive" in DB
// If match mnethost and user is deleted in backup file
// and match by email = email_without_time(backup_email) and non-zero firstaccess=> ok, return target user
if ($user->deleted) {
// Note: for DB deleted users email is stored in username field, hence we
// are looking there for emails. See delete_user()
// Trim time() from email
$trimemail = preg_replace('/(.*?)\.[0-9]+.?$/', '\\1', $user->username);
if ($rec = get_record_sql("SELECT *
FROM {$CFG->prefix}user u
WHERE mnethostid = $user->mnethostid
AND email = '".addslashes($trimemail)."'
AND firstaccess != 0
AND firstaccess = $user->firstaccess")) {
return $rec; // Matching user, deleted in backup file found, return it
}
}
// 2D - If match by username and mnethost and not by (email or non-zero firstaccess) => conflict, return false
if ($rec = get_record_sql("SELECT *
FROM {$CFG->prefix}user u
WHERE username = '".addslashes($user->username)."'
AND mnethostid = $user->mnethostid
AND NOT (
email = '".addslashes($user->email)."'
OR (
firstaccess != 0
AND firstaccess = $user->firstaccess
)
)")) {
return false; // Conflict, username/mnethostid already exist and belong to another user (by email/firstaccess)
}
}
// Arrived here, return true as the user will need to be created and no
// conflicts have been found in the logic above. This covers:
// 1E - else => user needs to be created, return true
// 2E - else => user needs to be created, return true
return true;
}
/**
* For all the users being restored, check if they are going to cause problems
* before executing the restore process itself, detecting situations like:
* - conflicts preventing restore to continue - provided by @restore_check_user()
* - prevent creation of users if not allowed - check some global settings/caps
*/
function restore_precheck_users($xml_file, $restore, &$problems) {
global $CFG;
$status = true; // Init $status
// We aren't restoring users, nothing to check, allow continue
if ($restore->users == 2) {
return true;
}
// Get array of users from xml file and load them in backup_ids table
if (!$info = restore_read_xml_users($restore,$xml_file)) {
return true; // No users, nothing to check, allow continue
}
// We are going to map mnethostid, so load all the available ones
$mnethosts = get_records('mnet_host', '', '', 'wwwroot', 'wwwroot, id');
// Calculate the context we are going to use for capability checking
if (!empty($restore->course_id)) { // Know the target (existing) course, check capabilities there
$context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
} else if (!empty($restore->restore_restorecatto)) { // Know the category, check capabilities there
$context = get_context_instance(CONTEXT_COURSECAT, $restore->restore_restorecatto);
} else { // Last resort, check capabilities at system level
$context = get_context_instance(CONTEXT_SYSTEM);
}
// Calculate if we have perms to create users, by checking:
// to 'moodle/restore:createuser' and 'moodle/restore:userinfo'
// and also observe $CFG->disableusercreationonrestore
$cancreateuser = false;
if (has_capability('moodle/restore:createuser', $context) and
has_capability('moodle/restore:userinfo', $context) and
empty($CFG->disableusercreationonrestore)) { // Can create users
$cancreateuser = true;
}
// Iterate over all users, checking if they are likely to cause problems on restore
$counter = 0;
foreach ($info->users as $userid) {
$rec = backup_getid($restore->backup_unique_code, 'user', $userid);
$user = $rec->info;
// Find the correct mnethostid for user before performing any further check
if (empty($user->mnethosturl) || $user->mnethosturl === $CFG->wwwroot) {
$user->mnethostid = $CFG->mnet_localhost_id;
} else {
// fast url-to-id lookups
if (isset($mnethosts[$user->mnethosturl])) {
$user->mnethostid = $mnethosts[$user->mnethosturl]->id;
} else {
$user->mnethostid = $CFG->mnet_localhost_id;
}
}
// Calculate the best way to handle this user from backup file
$usercheck = restore_check_user($restore, $user);
if (is_object($usercheck)) { // No problem, we have found one user in DB to be mapped to
// Annotate it, for later process by restore_create_users(). Set new_id to mapping user->id
backup_putid($restore->backup_unique_code, 'user', $userid, $usercheck->id, $user);
} else if ($usercheck === false) { // Found conflict, report it as problem
$problems[] = get_string('restoreuserconflict', '', $user->username);
$status = false;
} else if ($usercheck === true) { // User needs to be created, check if we are able
if ($cancreateuser) { // Can create user, annotate it, for later process by restore_create_users(). Set new_id to 0
backup_putid($restore->backup_unique_code, 'user', $userid, 0, $user);
} else { // Cannot create user, report it as problem
$problems[] = get_string('restorecannotcreateuser', '', $user->username);
$status = false;
}
} else { // Shouldn't arrive here ever, something is for sure wrong in restore_check_user()
if (!defined('RESTORE_SILENTLY')) {
notify('Unexpected error pre-checking user ' . s($user->username) . ' from backup file');
return false;
}
}
// Do some output
$counter++;
if ($counter % 10 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if ($counter % 200 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
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, $SESSION;
$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;
$SESSION->restore->course_id = $newid;
return $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))) {
//Get the module id from modules
$module = get_record("modules","name",$mod->type);
if ($module) {
$course_module = new object();
$course_module->course = $restore->course_id;
$course_module->module = $module->id;
$course_module->section = $newid;
$course_module->added = $mod->added;
$course_module->score = $mod->score;
$course_module->indent = $mod->indent;
$course_module->visible = $mod->visible;
$course_module->groupmode = $mod->groupmode;
if ($mod->groupingid and $grouping = restore_grouping_getid($restore, $mod->groupingid)) {
$course_module->groupingid = $grouping->new_id;
} else {
$course_module->groupingid = 0;
}
$course_module->groupmembersonly = $mod->groupmembersonly;
$course_module->instance = 0;
//NOTE: The instance (new) is calculated and updated in db in the
// final step of the restore. We don't know it yet.
//print_object($course_module); //Debug
//Save it to db
if ($mod->idnumber) {
if (grade_verify_idnumber($mod->idnumber, $restore->course_id)) {
$course_module->idnumber = $mod->idnumber;
}
}
$newidmod = insert_record("course_modules", addslashes_recursive($course_module));
if ($newidmod) {
//save old and new module id
//In the info field, we save the original instance of the module
//to use it later
backup_putid ($restore->backup_unique_code,"course_modules",
$keym,$newidmod,$mod->instance);
$restore->mods[$mod->type]->instances[$mod->instance]->restored_as_course_module = $newidmod;
} else {
$status = false;
}
//Now, calculate the sequence field
if ($status) {
if ($sequence) {
$sequence .= ",".$newidmod;
} else {
$sequence = $newidmod;
}
}
} else {
$status = false;
}
}
}
}
}
}
//If all is OK, update sequence field in course_sections
if ($status) {
if (isset($sequence)) {
$update_rec = new object();
$update_rec->id = $newid;
$update_rec->sequence = $sequence;
$status = update_record("course_sections",$update_rec);
}
}
}
} else {
$status = false;
}
return $status;
}
//Called to set up any course-format specific data that may be in the file
function restore_set_format_data($restore,$xml_file) {
global $CFG,$db;
$status = true;
//Check it exists
if (!file_exists($xml_file)) {
return false;
}
//Load data from XML to info
if(!($info = restore_read_xml_formatdata($xml_file))) {
return false;
}
//Process format data if there is any
if (isset($info->format_data)) {
if(!$format=get_field('course','format','id',$restore->course_id)) {
return false;
}
// If there was any data then it must have a restore method
$file=$CFG->dirroot."/course/format/$format/restorelib.php";
if(!file_exists($file)) {
return false;
}
require_once($file);
$function=$format.'_restore_format_data';
if(!function_exists($function)) {
return false;
}
return $function($restore,$info->format_data);
}
// If we got here then there's no data, but that's cool
return true;
}
//This function creates all the metacourse data from xml, notifying
//about each incidence
function restore_create_metacourse($restore,$xml_file) {
global $CFG,$db;
$status = true;
//Check it exists
if (!file_exists($xml_file)) {
$status = false;
}
//Get info from xml
if ($status) {
//Load data from XML to info
$info = restore_read_xml_metacourse($xml_file);
}
//Process info about metacourse
if ($status and $info) {
//Process child records
if (!empty($info->childs)) {
foreach ($info->childs as $child) {
$dbcourse = false;
$dbmetacourse = false;
//Check if child course exists in destination server
//(by id in the same server or by idnumber and shortname in other server)
if (backup_is_same_site($restore)) {
//Same server, lets see by id
$dbcourse = get_record('course','id',$child->id);
} else {
//Different server, lets see by idnumber and shortname, and only ONE record
$dbcount = count_records('course','idnumber',$child->idnumber,'shortname',$child->shortname);
if ($dbcount == 1) {
$dbcourse = get_record('course','idnumber',$child->idnumber,'shortname',$child->shortname);
}
}
//If child course has been found, insert data
if ($dbcourse) {
$dbmetacourse->child_course = $dbcourse->id;
$dbmetacourse->parent_course = $restore->course_id;
$status = insert_record ('course_meta',$dbmetacourse);
} else {
//Child course not found, notice!
if (!defined('RESTORE_SILENTLY')) {
echo '<ul><li>'.get_string ('childcoursenotfound').' ('.$child->id.'/'.$child->idnumber.'/'.$child->shortname.')</li></ul>';
}
}
}
//Now, recreate student enrolments...
sync_metacourse($restore->course_id);
}
//Process parent records
if (!empty($info->parents)) {
foreach ($info->parents as $parent) {
$dbcourse = false;
$dbmetacourse = false;
//Check if parent course exists in destination server
//(by id in the same server or by idnumber and shortname in other server)
if (backup_is_same_site($restore)) {
//Same server, lets see by id
$dbcourse = get_record('course','id',$parent->id);
} else {
//Different server, lets see by idnumber and shortname, and only ONE record
$dbcount = count_records('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
if ($dbcount == 1) {
$dbcourse = get_record('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
}
}
//If parent course has been found, insert data if it is a metacourse
if ($dbcourse) {
if ($dbcourse->metacourse) {
$dbmetacourse->parent_course = $dbcourse->id;
$dbmetacourse->child_course = $restore->course_id;
$status = insert_record ('course_meta',$dbmetacourse);
//Now, recreate student enrolments in parent course
sync_metacourse($dbcourse->id);
} else {
//Parent course isn't metacourse, notice!
if (!defined('RESTORE_SILENTLY')) {
echo '<ul><li>'.get_string ('parentcoursenotmetacourse').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
}
}
} else {
//Parent course not found, notice!
if (!defined('RESTORE_SILENTLY')) {
echo '<ul><li>'.get_string ('parentcoursenotfound').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
}
}
}
}
}
return $status;
}
/**
* This function migrades all the pre 1.9 gradebook data from xml
*/
function restore_migrate_old_gradebook($restore,$xml_file) {
global $CFG;
$status = true;
//Check it exists
if (!file_exists($xml_file)) {
return false;
}
// Get info from xml
// info will contain the number of record to process
$info = restore_read_xml_gradebook($restore, $xml_file);
// If we have info, then process
if (empty($info)) {
return $status;
}
// make sure top course category exists
$course_category = grade_category::fetch_course_category($restore->course_id);
$course_category->load_grade_item();
// we need to know if all grade items that were backed up are being restored
// if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
// i.e. the aggregated grades of that category
$restoreall = true; // set to false if any grade_item is not selected/restored
$importing = !empty($SESSION->restore->importing); // there should not be a way to import old backups, but anyway ;-)
if ($importing) {
$restoreall = false;
} else {
$prev_grade_items = grade_item::fetch_all(array('courseid'=>$restore->course_id));
$prev_grade_cats = grade_category::fetch_all(array('courseid'=>$restore->course_id));
// if any categories already present, skip restore of categories from backup
if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) {
$restoreall = false;
}
unset($prev_grade_items);
unset($prev_grade_cats);
}
// force creation of all grade_items - the course_modules already exist
grade_force_full_regrading($restore->course_id);
grade_grab_course_grades($restore->course_id);
// Start ul
if (!defined('RESTORE_SILENTLY')) {
echo '<ul>';
}
/// Process letters
$context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
// respect current grade letters if defined
if ($status and $restoreall and !record_exists('grade_letters', 'contextid', $context->id)) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('gradeletters','grades').'</li>';
}
// Fetch recordset_size records in each iteration
$recs = get_records_select("backup_ids","table_name = 'grade_letter' AND backup_code = $restore->backup_unique_code",
"",
"old_id");
if ($recs) {
foreach ($recs as $rec) {
// Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_letter',$rec->old_id);
if ($data) {
$info = $data->info;
$dbrec = new object();
$dbrec->contextid = $context->id;
$dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['GRADE_LOW']['0']['#']);
$dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']);
insert_record('grade_letters', $dbrec);
}
}
}
}
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('categories','grades').'</li>';
}
//Fetch recordset_size records in each iteration
$recs = get_records_select("backup_ids","table_name = 'grade_category' AND backup_code = $restore->backup_unique_code",
"old_id",
"old_id");
$cat_count = count($recs);
if ($recs) {
foreach ($recs as $rec) {
//Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_category',$rec->old_id);
if ($data) {
//Now get completed xmlized object
$info = $data->info;
if ($restoreall) {
if ($cat_count == 1) {
$course_category->fullname = backup_todb($info['GRADE_CATEGORY']['#']['NAME']['0']['#'], false);
$course_category->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROP_X_LOWEST']['0']['#'], false);
$course_category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
$course_category->aggregateonlygraded = 0;
$course_category->update('restore');
$grade_category = $course_category;
} else {
$grade_category = new grade_category();
$grade_category->courseid = $restore->course_id;
$grade_category->fullname = backup_todb($info['GRADE_CATEGORY']['#']['NAME']['0']['#'], false);
$grade_category->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROP_X_LOWEST']['0']['#'], false);
$grade_category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
$grade_category->aggregateonlygraded = 0;
$grade_category->insert('restore');
$grade_category->load_grade_item(); // force cretion of grade_item
}
} else {
$grade_category = null;
}
/// now, restore grade_items
$items = array();
if (!empty($info['GRADE_CATEGORY']['#']['GRADE_ITEMS']['0']['#']['GRADE_ITEM'])) {
//Iterate over items
foreach ($info['GRADE_CATEGORY']['#']['GRADE_ITEMS']['0']['#']['GRADE_ITEM'] as $ite_info) {
$modname = backup_todb($ite_info['#']['MODULE_NAME']['0']['#'], false);
$olditeminstance = backup_todb($ite_info['#']['CMINSTANCE']['0']['#'], false);
if (!$mod = backup_getid($restore->backup_unique_code,$modname, $olditeminstance)) {
continue; // not restored
}
$iteminstance = $mod->new_id;
if (!$cm = get_coursemodule_from_instance($modname, $iteminstance, $restore->course_id)) {
continue; // does not exist
}
if (!$grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$cm->modname, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course, 'itemnumber'=>0))) {
continue; // no item yet??
}
if ($grade_category) {
$grade_item->sortorder = backup_todb($ite_info['#']['SORT_ORDER']['0']['#'], false);
$grade_item->set_parent($grade_category->id);
}
if ($importing
or ($grade_item->itemtype == 'mod' and !restore_userdata_selected($restore, $grade_item->itemmodule, $olditeminstance))) {
// module instance not selected when restored using granular
// skip this item
continue;
}
//Now process grade excludes
if (empty($ite_info['#']['GRADE_EXCEPTIONS'])) {
continue;
}
foreach($ite_info['#']['GRADE_EXCEPTIONS']['0']['#']['GRADE_EXCEPTION'] as $exc_info) {
if ($u = backup_getid($restore->backup_unique_code,"user",backup_todb($exc_info['#']['USERID']['0']['#']))) {
$userid = $u->new_id;
$grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$userid));
$grade_grade->excluded = 1;
if ($grade_grade->id) {
$grade_grade->update('restore');
} else {
$grade_grade->insert('restore');
}
}
}
}
}
}
}
}
if (!defined('RESTORE_SILENTLY')) {
//End ul
echo '</ul>';
}
return $status;
}
/**
* This function creates all the gradebook data from xml
*/
function restore_create_gradebook($restore,$xml_file) {
global $CFG;
$status = true;
//Check it exists
if (!file_exists($xml_file)) {
return false;
}
// Get info from xml
// info will contain the number of record to process
$info = restore_read_xml_gradebook($restore, $xml_file);
// If we have info, then process
if (empty($info)) {
return $status;
}
if (empty($CFG->disablegradehistory) and isset($info->gradebook_histories) and $info->gradebook_histories == "true") {
$restore_histories = true;
} else {
$restore_histories = false;
}
// make sure top course category exists
$course_category = grade_category::fetch_course_category($restore->course_id);
$course_category->load_grade_item();
// we need to know if all grade items that were backed up are being restored
// if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
// i.e. the aggregated grades of that category
$restoreall = true; // set to false if any grade_item is not selected/restored or already exist
$importing = !empty($SESSION->restore->importing);
if ($importing) {
$restoreall = false;
} else {
$prev_grade_items = grade_item::fetch_all(array('courseid'=>$restore->course_id));
$prev_grade_cats = grade_category::fetch_all(array('courseid'=>$restore->course_id));
// if any categories already present, skip restore of categories from backup - course item or category already exist
if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) {
$restoreall = false;
}
unset($prev_grade_items);
unset($prev_grade_cats);
if ($restoreall) {
if ($recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = $restore->backup_unique_code", "", "old_id")) {
foreach ($recs as $rec) {
if ($data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id)) {
$info = $data->info;
// do not restore if this grade_item is a mod, and
$itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
if ($itemtype == 'mod') {
$olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
$itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
if (empty($restore->mods[$itemmodule]->granular)) {
continue;
} else if (!empty($restore->mods[$itemmodule]->instances[$olditeminstance]->restore)) {
continue;
}
// at least one activity should not be restored - do not restore categories and manual items at all
$restoreall = false;
break;
}
}
}
}
}
}
// Start ul
if (!defined('RESTORE_SILENTLY')) {
echo '<ul>';
}
// array of restored categories - speedup ;-)
$cached_categories = array();
$outcomes = array();
/// Process letters
$context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
// respect current grade letters if defined
if ($status and $restoreall and !record_exists('grade_letters', 'contextid', $context->id)) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('gradeletters','grades').'</li>';
}
// Fetch recordset_size records in each iteration
$recs = get_records_select("backup_ids","table_name = 'grade_letters' AND backup_code = $restore->backup_unique_code",
"",
"old_id");
if ($recs) {
foreach ($recs as $rec) {
// Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_letters',$rec->old_id);
if ($data) {
$info = $data->info;
$dbrec = new object();
$dbrec->contextid = $context->id;
$dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['LOWERBOUNDARY']['0']['#']);
$dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']);
insert_record('grade_letters', $dbrec);
}
}
}
}
/// Preprocess outcomes - do not store them yet!
if ($status and !$importing and $restoreall) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('gradeoutcomes','grades').'</li>';
}
$recs = get_records_select("backup_ids","table_name = 'grade_outcomes' AND backup_code = '$restore->backup_unique_code'",
"",
"old_id");
if ($recs) {
foreach ($recs as $rec) {
//Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_outcomes',$rec->old_id);
if ($data) {
$info = $data->info;
//first find out if outcome already exists
$shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#']);
if ($candidates = get_records_sql("SELECT *
FROM {$CFG->prefix}grade_outcomes
WHERE (courseid IS NULL OR courseid = $restore->course_id)
AND shortname = '$shortname'
ORDER BY courseid ASC, id ASC")) {
$grade_outcome = reset($candidates);
$outcomes[$rec->old_id] = $grade_outcome;
continue;
}
$dbrec = new object();
if (has_capability('moodle/grade:manageoutcomes', get_context_instance(CONTEXT_SYSTEM))) {
$oldoutcome = backup_todb($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#']);
if (empty($oldoutcome)) {
//site wide
$dbrec->courseid = null;
} else {
//course only
$dbrec->courseid = $restore->course_id;
}
} else {
// no permission to add site outcomes
$dbrec->courseid = $restore->course_id;
}
//Get the fields
$dbrec->shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#'], false);
$dbrec->fullname = backup_todb($info['GRADE_OUTCOME']['#']['FULLNAME']['0']['#'], false);
$dbrec->scaleid = backup_todb($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#'], false);
$dbrec->description = backup_todb($info['GRADE_OUTCOME']['#']['DESCRIPTION']['0']['#'], false);
$dbrec->timecreated = backup_todb($info['GRADE_OUTCOME']['#']['TIMECREATED']['0']['#'], false);
$dbrec->timemodified = backup_todb($info['GRADE_OUTCOME']['#']['TIMEMODIFIED']['0']['#'], false);
$dbrec->usermodified = backup_todb($info['GRADE_OUTCOME']['#']['USERMODIFIED']['0']['#'], false);
//Need to recode the scaleid
if ($scale = backup_getid($restore->backup_unique_code, 'scale', $dbrec->scaleid)) {
$dbrec->scaleid = $scale->new_id;
}
//Need to recode the usermodified
if ($modifier = backup_getid($restore->backup_unique_code, 'user', $dbrec->usermodified)) {
$dbrec->usermodified = $modifier->new_id;
}
$grade_outcome = new grade_outcome($dbrec, false);
$outcomes[$rec->old_id] = $grade_outcome;
}
}
}
}
/// Process grade items and grades
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('gradeitems','grades').'</li>';
}
$counter = 0;
//Fetch recordset_size records in each iteration
$recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'",
"id", // restore in the backup order
"old_id");
if ($recs) {
foreach ($recs as $rec) {
//Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id);
if ($data) {
$info = $data->info;
// first find out if category or normal item
$itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
if ($itemtype == 'course' or $itemtype == 'category') {
if (!$restoreall or $importing) {
continue;
}
$oldcat = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false);
if (!$cdata = backup_getid($restore->backup_unique_code,'grade_categories',$oldcat)) {
continue;
}
$cinfo = $cdata->info;
unset($cdata);
if ($itemtype == 'course') {
$course_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false);
$course_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false);
$course_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false);
$course_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false);
$course_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false);
$course_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false);
$course_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false);
$course_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false);
$course_category->update('restore');
$status = backup_putid($restore->backup_unique_code,'grade_categories',$oldcat,$course_category->id) && $status;
$cached_categories[$oldcat] = $course_category;
$grade_item = $course_category->get_grade_item();
} else {
$oldparent = backup_todb($cinfo['GRADE_CATEGORY']['#']['PARENT']['0']['#'], false);
if (empty($cached_categories[$oldparent])) {
debugging('parent not found '.$oldparent);
continue; // parent not found, sorry
}
$grade_category = new grade_category();
$grade_category->courseid = $restore->course_id;
$grade_category->parent = $cached_categories[$oldparent]->id;
$grade_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false);
$grade_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false);
$grade_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false);
$grade_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false);
$grade_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false);
$grade_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false);
$grade_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false);
$grade_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false);
$grade_category->insert('restore');
$status = backup_putid($restore->backup_unique_code,'grade_categories',$oldcat,$grade_category->id) && $status;
$cached_categories[$oldcat] = $grade_category;
$grade_item = $grade_category->get_grade_item(); // creates grade_item too
}
unset($cinfo);
$idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
if (grade_verify_idnumber($idnumber, $restore->course_id)) {
$grade_item->idnumber = $idnumber;
}
$grade_item->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
$grade_item->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false);
$grade_item->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false);
$grade_item->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false);
$grade_item->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false);
$grade_item->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false);
$grade_item->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false);
$grade_item->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false);
$grade_item->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false);
$grade_item->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false);
$grade_item->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false);
$grade_item->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false);
$grade_item->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false);
$grade_item->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false);
$grade_item->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false);
$grade_item->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false);
if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) {
$scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false));
$grade_item->scaleid = $scale->new_id;
}
if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false)) {
$outcome = backup_getid($restore->backup_unique_code,"grade_outcomes",backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false));
$grade_item->outcomeid = $outcome->new_id;
}
$grade_item->update('restore');
$status = backup_putid($restore->backup_unique_code,"grade_items", $rec->old_id, $grade_item->id) && $status;
} else {
if ($itemtype != 'mod' and (!$restoreall or $importing)) {
// not extra gradebook stuff if restoring individual activities or something already there
continue;
}
$dbrec = new object();
$dbrec->courseid = $restore->course_id;
$dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
$dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false);
if ($itemtype == 'mod') {
// iteminstance should point to new mod
$olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false);
$mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $olditeminstance);
$dbrec->iteminstance = $mod->new_id;
if (!$cm = get_coursemodule_from_instance($dbrec->itemmodule, $mod->new_id)) {
// item not restored - no item
continue;
}
// keep in sync with activity idnumber
$dbrec->idnumber = $cm->idnumber;
} else {
$idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
if (grade_verify_idnumber($idnumber, $restore->course_id)) {
//make sure the new idnumber is unique
$dbrec->idnumber = $idnumber;
}
}
$dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
$dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
$dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false);
$dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#'], false);
$dbrec->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false);
$dbrec->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false);
$dbrec->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false);
$dbrec->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false);
$dbrec->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false);
$dbrec->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false);
$dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false);
$dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false);
$dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false);
$dbrec->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false);
$dbrec->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false);
$dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false);
$dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false);
$dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false);
$dbrec->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false);
if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) {
$scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false));
$dbrec->scaleid = $scale->new_id;
}
if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'])) {
$oldoutcome = backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']);
if (empty($outcomes[$oldoutcome])) {
continue; // error!
}
if (empty($outcomes[$oldoutcome]->id)) {
$outcomes[$oldoutcome]->insert('restore');
$outcomes[$oldoutcome]->use_in($restore->course_id);
backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $outcomes[$oldoutcome]->id);
}
$dbrec->outcomeid = $outcomes[$oldoutcome]->id;
}
$grade_item = new grade_item($dbrec, false);
$grade_item->insert('restore');
if ($restoreall) {
// set original parent if restored
$oldcat = $info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'];
if (!empty($cached_categories[$oldcat])) {
$grade_item->set_parent($cached_categories[$oldcat]->id);
}
}
$status = backup_putid($restore->backup_unique_code,"grade_items", $rec->old_id, $grade_item->id) && $status;
}
// no need to restore grades if user data is not selected or importing activities
if ($importing
or ($grade_item->itemtype == 'mod' and !restore_userdata_selected($restore, $grade_item->itemmodule, $olditeminstance))) {
// module instance not selected when restored using granular
// skip this item
continue;
}
/// now, restore grade_grades
if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
//Iterate over items
foreach ($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'] as $g_info) {
$grade = new grade_grade();
$grade->itemid = $grade_item->id;
$olduser = backup_todb($g_info['#']['USERID']['0']['#'], false);
$user = backup_getid($restore->backup_unique_code,"user",$olduser);
$grade->userid = $user->new_id;
$grade->rawgrade = backup_todb($g_info['#']['RAWGRADE']['0']['#'], false);
$grade->rawgrademax = backup_todb($g_info['#']['RAWGRADEMAX']['0']['#'], false);
$grade->rawgrademin = backup_todb($g_info['#']['RAWGRADEMIN']['0']['#'], false);
// need to find scaleid
if (backup_todb($g_info['#']['RAWSCALEID']['0']['#'])) {
$scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($g_info['#']['RAWSCALEID']['0']['#'], false));
$grade->rawscaleid = $scale->new_id;
}
if (backup_todb($g_info['#']['USERMODIFIED']['0']['#'])) {
if ($modifier = backup_getid($restore->backup_unique_code,"user", backup_todb($g_info['#']['USERMODIFIED']['0']['#'], false))) {
$grade->usermodified = $modifier->new_id;
}
}
$grade->finalgrade = backup_todb($g_info['#']['FINALGRADE']['0']['#'], false);
$grade->hidden = backup_todb($g_info['#']['HIDDEN']['0']['#'], false);
$grade->locked = backup_todb($g_info['#']['LOCKED']['0']['#'], false);
$grade->locktime = backup_todb($g_info['#']['LOCKTIME']['0']['#'], false);
$grade->exported = backup_todb($g_info['#']['EXPORTED']['0']['#'], false);
$grade->overridden = backup_todb($g_info['#']['OVERRIDDEN']['0']['#'], false);
$grade->excluded = backup_todb($g_info['#']['EXCLUDED']['0']['#'], false);
$grade->feedback = backup_todb($g_info['#']['FEEDBACK']['0']['#'], false);
$grade->feedbackformat = backup_todb($g_info['#']['FEEDBACKFORMAT']['0']['#'], false);
$grade->information = backup_todb($g_info['#']['INFORMATION']['0']['#'], false);
$grade->informationformat = backup_todb($g_info['#']['INFORMATIONFORMAT']['0']['#'], false);
$grade->timecreated = backup_todb($g_info['#']['TIMECREATED']['0']['#'], false);
$grade->timemodified = backup_todb($g_info['#']['TIMEMODIFIED']['0']['#'], false);
$grade->insert('restore');
backup_putid($restore->backup_unique_code,"grade_grades", backup_todb($g_info['#']['ID']['0']['#']), $grade->id);
$counter++;
if ($counter % 20 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if ($counter % 400 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
}
}
}
}
/// add outcomes that are not used when doing full restore
if ($status and $restoreall) {
foreach ($outcomes as $oldoutcome=>$grade_outcome) {
if (empty($grade_outcome->id)) {
$grade_outcome->insert('restore');
$grade_outcome->use_in($restore->course_id);
backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $grade_outcome->id);
}
}
}
if ($status and !$importing and $restore_histories) {
/// following code is very inefficient
$gchcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories_history');
$gghcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_grades_history');
$gihcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_items_history');
$gohcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes_history');
// Number of records to get in every chunk
$recordset_size = 2;
// process histories
if ($gchcount && $status) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('gradecategoryhistory','grades').'</li>';
}
$counter = 0;
while ($counter < $gchcount) {
//Fetch recordset_size records in each iteration
$recs = get_records_select("backup_ids","table_name = 'grade_categories_history' AND backup_code = '$restore->backup_unique_code'",
"old_id",
"old_id",
$counter,
$recordset_size);
if ($recs) {
foreach ($recs as $rec) {
//Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_categories_history',$rec->old_id);
if ($data) {
//Now get completed xmlized object
$info = $data->info;
//traverse_xmlize($info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
$oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['OLDID']['0']['#']));
if (empty($oldobj->new_id)) {
// if the old object is not being restored, can't restoring its history
$counter++;
continue;
}
$dbrec->oldid = $oldobj->new_id;
$dbrec->action = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['ACTION']['0']['#']);
$dbrec->source = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['SOURCE']['0']['#']);
$dbrec->timemodified = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
// loggeduser might not be restored, e.g. admin
if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
$dbrec->loggeduser = $oldobj->new_id;
}
// this item might not have a parent at all, do not skip it if no parent is specified
if (backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])) {
$oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#']));
if (empty($oldobj->new_id)) {
// if the parent category not restored
$counter++;
continue;
}
}
$dbrec->parent = $oldobj->new_id;
$dbrec->depth = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DEPTH']['0']['#']);
// path needs to be rebuilt
if ($path = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PATH']['0']['#'])) {
// to preserve the path and make it work, we need to replace the categories one by one
// we first get the list of categories in current path
if ($paths = explode("/", $path)) {
$newpath = '';
foreach ($paths as $catid) {
if ($catid) {
// find the new corresponding path
$oldpath = backup_getid($restore->backup_unique_code,"grade_categories", $catid);
$newpath .= "/$oldpath->new_id";
}
}
$dbrec->path = $newpath;
}
}
$dbrec->fullname = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['FULLNAME']['0']['#']);
$dbrec->aggregation = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGRETGATION']['0']['#']);
$dbrec->keephigh = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['KEEPHIGH']['0']['#']);
$dbrec->droplow = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DROPLOW']['0']['#']);
$dbrec->aggregateonlygraded = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEONLYGRADED']['0']['#']);
$dbrec->aggregateoutcomes = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEOUTCOMES']['0']['#']);
$dbrec->aggregatesubcats = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATESUBCATS']['0']['#']);
$dbrec->courseid = $restore->course_id;
insert_record('grade_categories_history', $dbrec);
unset($dbrec);
}
//Increment counters
$counter++;
//Do some output
if ($counter % 1 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if ($counter % 20 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
}
}
// process histories
if ($gghcount && $status) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('gradegradeshistory','grades').'</li>';
}
$counter = 0;
while ($counter < $gghcount) {
//Fetch recordset_size records in each iteration
$recs = get_records_select("backup_ids","table_name = 'grade_grades_history' AND backup_code = '$restore->backup_unique_code'",
"old_id",
"old_id",
$counter,
$recordset_size);
if ($recs) {
foreach ($recs as $rec) {
//Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code,'grade_grades_history',$rec->old_id);
if ($data) {
//Now get completed xmlized object
$info = $data->info;
//traverse_xmlize($info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
$oldobj = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($info['GRADE_GRADES_HISTORY']['#']['OLDID']['0']['#']));
if (empty($oldobj->new_id)) {
// if the old object is not being restored, can't restoring its history
$counter++;
continue;
}
$dbrec->oldid = $oldobj->new_id;
$dbrec->action = backup_todb($info['GRADE_GRADES_HISTORY']['#']['ACTION']['0']['#']);
$dbrec->source = backup_todb($info['GRADE_GRADES_HISTORY']['#']['SOURCE']['0']['#']);
$dbrec->timemodified = backup_todb($info['GRADE_GRADES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
$dbrec->loggeduser = $oldobj->new_id;
}
$oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_GRADES_HISTORY']['#']['ITEMID']['0']['#']));
$dbrec->itemid = $oldobj->new_id;
if (empty($dbrec->itemid)) {
$counter++;
continue; // grade item not being restored
}
$oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERID']['0']['#']));
$dbrec->userid = $oldobj->new_id;
$dbrec->rawgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADE']['0']['#']);
$dbrec->rawgrademax = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMAX']['0']['#']);
$dbrec->rawgrademin = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMIN']['0']['#']);
if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
$dbrec->usermodified = $oldobj->new_id;
}
if (backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#'])) {
$scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#']));
$dbrec->rawscaleid = $scale->new_id;
}
$dbrec->finalgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['FINALGRADE']['0']['#']);
$dbrec->hidden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['HIDDEN']['0']['#']);
$dbrec->locked = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKED']['0']['#']);
$dbrec->locktime = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKTIME']['0']['#']);
$dbrec->exported = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXPORTED']['0']['#']);
$dbrec->overridden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['OVERRIDDEN']['0']['#']);
$dbrec->excluded = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXCLUDED']['0']['#']);
$dbrec->feedback = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACK']['0']['#']);
$dbrec->feedbackformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACKFORMAT']['0']['#']);
$dbrec->information = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATION']['0']['#']);
$dbrec->informationformat = backup_todb($info['GRADE_TEXT_