From b5e58c183175b4bd798d0ffec7052443e033ad14 Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Mon, 1 Nov 2010 15:47:07 +0000 Subject: [PATCH] MDL-22164 backup - more cleanup of old 1.9 backup/restore code --- backup/backuplib.php | 3070 ----------------------------------------- backup/lib.php | 327 ----- backup/restorelib.php | 714 ---------- 3 files changed, 4111 deletions(-) diff --git a/backup/backuplib.php b/backup/backuplib.php index 3fa5f3dcdf5..3eaea31879c 100644 --- a/backup/backuplib.php +++ b/backup/backuplib.php @@ -1,773 +1,4 @@ id]->id = $enrolled_user->id; - } - } - } - - /// If we have backupable users - if ($backupable_users) { - /// Iterate over users putting their roles - foreach ($backupable_users as $backupable_user) { - $backupable_user->info = ""; - - /// Is needed user or enrolled user, mark it as needed - if (isset($needed_users[$backupable_user->id]) || isset($enrolled_users[$backupable_user->id])) { - $backupable_user->info .= "needed"; - } /// Yu: also needed because they can view course - /// might need another variable - - /// Now create the backup_id record - $backupids_rec->backup_code = $backup_unique_code; - $backupids_rec->table_name = "user"; - $backupids_rec->old_id = $backupable_user->id; - $backupids_rec->info = $backupable_user->info; - - /// TODO: Change this call inserting to a standard backup_putid() call - /// And read data acordingly with backup_getid() when needed. - /// TODO: Also analyse it the "needed" info is really needed for anything. Drop if not. - /// Insert the user to the backup_ids table. backup_user_info() will use that info - $status = $DB->insert_record('backup_ids', $backupids_rec, false); - $count_users++; - } - /// Do some output - backup_flush(30); - } - - /// Prepare Info - /// Gets the user data - $info[0][0] = get_string("users"); - $info[0][1] = $count_users; - - return $info; - } - - //Returns every needed user (participant) in a course - //It uses the xxxx_get_participants() function - //Also it search for users having messages and - //users having blogs - //WARNING: It returns only NEEDED users, not every - // every student and teacher in the course, so it - //must be merged with backup_get_enrrolled_users !! - function backup_get_needed_users ($courseid, $includemessages=false, $includeblogs=false) { - global $CFG, $DB; - - $result = false; - - $course_modules = $DB->get_records_sql("SELECT cm.id, m.name, cm.instance - FROM {modules} m, {course_modules} cm - WHERE m.id = cm.module and - cm.course = ?", array($courseid)); - - if ($course_modules) { - //Iterate over each module - foreach ($course_modules as $course_module) { - $modlib = "$CFG->dirroot/mod/$course_module->name/lib.php"; - $modgetparticipants = $course_module->name."_get_participants"; - if (file_exists($modlib)) { - include_once($modlib); - if (function_exists($modgetparticipants)) { - $module_participants = $modgetparticipants($course_module->instance); - //Add them to result - if ($module_participants) { - foreach ($module_participants as $module_participant) { - $result[$module_participant->id]->id = $module_participant->id; - } - } - } - } - } - } - - //Now, add message users if necessary - if ($includemessages) { - include_once("$CFG->dirroot/message/lib.php"); - //Get users - $messageusers = message_get_participants(); - //Add message users to results - if ($messageusers) { - foreach ($messageusers as $messageuser) { - //If id != 0 - if ($messageuser->id !=0) { - $result[$messageuser->id]->id = $messageuser->id; - } - } - } - } - - //Now, add blog users if necessary - if ($includeblogs) { - //Get users - $blogusers = $DB->get_records_sql("SELECT u.id, u.id FROM {user} u WHERE u.id IN (SELECT DISTINCT userid FROM {post})"); - - //Add blog users to results - if ($blogusers) { - foreach ($blogusers as $bloguser) { - //If id != 0 - if ($bloguser->id !=0) { - $result[$bloguser->id]->id = $bloguser->id; - } - } - } - } - - return $result; - - } - - //Returns every enrolled user (student and teacher) in a course - - function backup_get_enrolled_users ($courseid) { - global $CFG; - - return get_enrolled_users(get_context_instance(CONTEXT_COURSE, $courseid)); - } - - //Returns all users ids (every record in users table) - function backup_get_all_users() { - global $DB; - - return $DB->get_records('user', null, '', 'id, id'); - } - - //Calculate the number of log entries to backup - //Return an array of info (name,value) - function log_check_backup($course) { - global $CFG, $DB; - - //Now execute the count - $ids = $DB->count_records("log", array("course"=>$course)); - - //Gets the user data - $info[0][0] = get_string("logs"); - if ($ids) { - $info[0][1] = $ids; - } else { - $info[0][1] = 0; - } - - return $info; - } - - //Calculate the number of user files to backup - //Under $CFG->dataroot/users - //Return an array of info (name,value) - function user_files_check_backup($course,$backup_unique_code) { - global $CFG, $DB; - - $count = 0; - - $backup_users = $DB->get_recordset("backup_ids", - array('backup_code'=>$backup_unique_code, 'table_name'=>'user'), "", "id, old_id"); - - foreach ($backup_users as $user) { - //TODO: deleted old user pic processing - } - $backup_users->close(); - //Gets the user data - $info[0][0] = get_string('userswithfiles', 'moodle'); - $info[0][1] = $count; - - return $info; - } - - /** - * Calculate the number of course files to backup - * under $CFG->dataroot/$course, except $CFG->moddata, and backupdata - * and put them (their path) in backup_ids - * Return an array of info (name,value) - */ - function course_files_check_backup($course, $backup_unique_code) { - global $CFG, $DB; - - $rootdir = $CFG->dataroot."/$course"; - //Check if directory exists - if (is_dir($rootdir)) { - //Get files and directories without descend - $coursedirs = get_directory_list($rootdir,$CFG->moddata,false,true,true); - $backupdata_dir = "backupdata"; - foreach ($coursedirs as $dir) { - //Check it isn't backupdata_dir - if (strpos($dir,$backupdata_dir)!==0) { - //Insert them into backup_files - $file = new stdClass(); - $file->backup_code = $backup_unique_code; - $file->file_type = 'course'; - $file->path = $dir; - $status = $DB->insert_record("backup_files", $file); - } - //Do some output - backup_flush(30); - } - } - - //Now execute the select - $ids = $DB->get_records_sql("SELECT DISTINCT b.path, b.old_id - FROM {backup_files} b - WHERE backup_code = ? AND - file_type = 'course'", array($backup_unique_code)); - //Gets the user data - $info = array(); - $info[0] = array(); - $info[0][0] = get_string("files"); - if ($ids) { - $info[0][1] = count($ids); - } else { - $info[0][1] = 0; - } - - return $info; - } - - /** - * Calculate the number of site files to backup - * under $CFG->dataroot/SITEID - * Their path is already in backup_ids, put there by modules check_backup functions. - * Modules only put in paths of files that are used. - * - * Return an array of info (name,value) - */ - function site_files_check_backup($course, $backup_unique_code) { - global $CFG, $DB; - - //execute the select, records have been inserted by modules during their ****_check_backup_mods function. - $ids = $DB->get_records_sql("SELECT DISTINCT b.path - FROM {backup_files} b - WHERE backup_code = ? AND - file_type = 'site'", array($backup_unique_code)); - //Gets the user data - $info = array(); - $info[0] = array(); - $info[0][0] = get_string('files'); - if ($ids) { - $info[0][1] = count($ids); - } else { - $info[0][1] = 0; - } - - return $info; - } - - //Function to check and create the needed moddata dir to - //save all the mod backup files. We always name it moddata - //to be able to restore it, but in restore we check for - //$CFG->moddata !! - function check_and_create_moddata_dir($backup_unique_code) { - global $CFG; - - $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/moddata",true); - - return $status; - } - - //Function to check and create the "user_files" dir to - //save all the user files we need from "users" dir - function check_and_create_user_files_dir($backup_unique_code) { - global $CFG; - - $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/user_files",true); - - return $status; - } - - //Function to check and create the "group_files" dir to - //save all the user files we need from "groups" dir - function check_and_create_group_files_dir($backup_unique_code) { - global $CFG; - - $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/group_files",true); - - return $status; - } - - //Function to check and create the "course_files" dir to - //save all the course files we need from "CFG->datadir/course" dir - function check_and_create_course_files_dir($backup_unique_code) { - global $CFG; - - $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/course_files",true); - - return $status; - } - - //Function to check and create the "site_files" dir to - //save all the course files we need from "CFG->datadir/SITEID" dir - function check_and_create_site_files_dir($backup_unique_code) { - global $CFG; - - $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/site_files",true); - - return $status; - } - - //Function to create, open and write header of the xml file - function backup_open_xml($backup_unique_code) { - global $CFG; - - $status = true; - - //Open for writing - - $file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml"; - $backup_file = fopen($file,"w"); - //Writes the header - $status = fwrite ($backup_file,"\n"); - if ($status) { - $status = fwrite ($backup_file,start_tag("MOODLE_BACKUP",0,true)); - } - if ($status) { - return $backup_file; - } else { - return false; - } - } - - //Close the file - function backup_close_xml($backup_file) { - $status = fwrite ($backup_file,end_tag("MOODLE_BACKUP",0,true)); - return fclose($backup_file); - } - - //Return the xml start tag - function start_tag($tag,$level=0,$endline=false,$attributes=null) { - if ($endline) { - $endchar = "\n"; - } else { - $endchar = ""; - } - $attrstring = ''; - if (!empty($attributes) && is_array($attributes)) { - foreach ($attributes as $key => $value) { - $attrstring .= " ".xml_tag_safe_content($key)."=\"". - xml_tag_safe_content($value)."\""; - } - } - return str_repeat(" ",$level*2)."<".strtoupper($tag).$attrstring.">".$endchar; - } - - //Return the xml end tag - function end_tag($tag,$level=0,$endline=true) { - if ($endline) { - $endchar = "\n"; - } else { - $endchar = ""; - } - return str_repeat(" ",$level*2)."".$endchar; - } - - //Return the start tag, the contents and the end tag - function full_tag($tag,$level=0,$endline=true,$content,$attributes=null) { - global $CFG; - - //Here we encode absolute links - // MDL-10770 - if (is_null($content)) { - $content = '$@NULL@$'; - } else { - $content = backup_encode_absolute_links($content); - } - $st = start_tag($tag,$level,$endline,$attributes); - - $co = xml_tag_safe_content($content); - - $et = end_tag($tag,0,true); - - return $st.$co.$et; - } - - - function xml_tag_safe_content($content) { - global $CFG; - //If enabled, we strip all the control chars (\x0-\x1f) from the text but tabs (\x9), - //newlines (\xa) and returns (\xd). The delete control char (\x7f) is also included. - //because they are forbiden in XML 1.0 specs. The expression below seems to be - //UTF-8 safe too because it simply ignores the rest of characters. - $content = preg_replace("/[\x-\x8\xb-\xc\xe-\x1f\x7f]/is","",$content); - $content = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content)); - return $content; - } - - //Prints General info about the course - //name, moodle_version (internal and release), backup_version, date, info in file... - function backup_general_info ($bf,$preferences) { - global $CFG, $DB; - - fwrite ($bf,start_tag("INFO",1,true)); - - //The name of the backup - fwrite ($bf,full_tag("NAME",2,false,$preferences->backup_name)); - //The moodle_version - fwrite ($bf,full_tag("MOODLE_VERSION",2,false,$preferences->moodle_version)); - fwrite ($bf,full_tag("MOODLE_RELEASE",2,false,$preferences->moodle_release)); - //The backup_version - fwrite ($bf,full_tag("BACKUP_VERSION",2,false,$preferences->backup_version)); - fwrite ($bf,full_tag("BACKUP_RELEASE",2,false,$preferences->backup_release)); - //The date - fwrite ($bf,full_tag("DATE",2,false,$preferences->backup_unique_code)); - //The original site wwwroot - fwrite ($bf,full_tag("ORIGINAL_WWWROOT",2,false,$CFG->wwwroot)); - //The original site identifier. MD5 hashed for security. - fwrite ($bf,full_tag("ORIGINAL_SITE_IDENTIFIER_HASH",2,false,md5(get_site_identifier()))); - //Indicate if it includes external MNET users - $sql = "SELECT b.old_id - FROM {backup_ids} b - JOIN {user} u ON b.old_id=u.id - WHERE b.backup_code = ? - AND b.table_name = 'user' AND u.mnethostid != ?"; - if ($DB->record_exists_sql($sql, array($preferences->backup_unique_code, $CFG->mnet_localhost_id))) { - fwrite ($bf,full_tag("MNET_REMOTEUSERS",2,false,'true')); - } - //Te includes tag - fwrite ($bf,start_tag("DETAILS",2,true)); - //Now, go to mod element of preferences to print its status - foreach ($preferences->mods as $element) { - //Calculate info - $included = "false"; - $userinfo = "false"; - if ($element->backup) { - $included = "true"; - if ($element->userinfo) { - $userinfo = "true"; - } - } - //Prints the mod start - fwrite ($bf,start_tag("MOD",3,true)); - fwrite ($bf,full_tag("NAME",4,false,$element->name)); - fwrite ($bf,full_tag("INCLUDED",4,false,$included)); - fwrite ($bf,full_tag("USERINFO",4,false,$userinfo)); - - if (isset($preferences->mods[$element->name]->instances) - && is_array($preferences->mods[$element->name]->instances) - && count($preferences->mods[$element->name]->instances)) { - fwrite ($bf, start_tag("INSTANCES",4,true)); - foreach ($preferences->mods[$element->name]->instances as $id => $object) { - if (!empty($object->backup)) { - //Calculate info - $included = "false"; - $userinfo = "false"; - if ($object->backup) { - $included = "true"; - if ($object->userinfo) { - $userinfo = "true"; - } - } - fwrite ($bf, start_tag("INSTANCE",5,true)); - fwrite ($bf, full_tag("ID",5,false,$id)); - fwrite ($bf, full_tag("NAME",5,false,$object->name)); - fwrite ($bf, full_tag("INCLUDED",5,false,$included)) ; - fwrite ($bf, full_tag("USERINFO",5,false,$userinfo)); - fwrite ($bf, end_tag("INSTANCE",5,true)); - } - } - fwrite ($bf, end_tag("INSTANCES",4,true)); - } - - - //Print the end - fwrite ($bf,end_tag("MOD",3,true)); - } - //The metacourse in backup - if ($preferences->backup_metacourse == 1) { - fwrite ($bf,full_tag("METACOURSE",3,false,"true")); - } else { - fwrite ($bf,full_tag("METACOURSE",3,false,"false")); - } - //The user in backup - if ($preferences->backup_users == 1) { - fwrite ($bf,full_tag("USERS",3,false,"course")); - } else if ($preferences->backup_users == 0) { - fwrite ($bf,full_tag("USERS",3,false,"all")); - } else { - fwrite ($bf,full_tag("USERS",3,false,"none")); - } - //The logs in backup - if ($preferences->backup_logs == 1) { - fwrite ($bf,full_tag("LOGS",3,false,"true")); - } else { - fwrite ($bf,full_tag("LOGS",3,false,"false")); - } - //The user files - if ($preferences->backup_user_files == 1) { - fwrite ($bf,full_tag("USERFILES",3,false,"true")); - } else { - fwrite ($bf,full_tag("USERFILES",3,false,"false")); - } - //The course files - if ($preferences->backup_course_files == 1) { - fwrite ($bf,full_tag("COURSEFILES",3,false,"true")); - } else { - fwrite ($bf,full_tag("COURSEFILES",3,false,"false")); - } - //The site files - if ($preferences->backup_site_files == 1) { - fwrite ($bf,full_tag("SITEFILES",3,false,"true")); - } else { - fwrite ($bf,full_tag("SITEFILES",3,false,"false")); - } - //The gradebook histories - if (empty($CFG->disablegradehistory) && $preferences->backup_gradebook_history == 1) { - fwrite ($bf,full_tag("GRADEBOOKHISTORIES",3,false,"true")); - } else { - fwrite ($bf,full_tag("GRADEBOOKHISTORIES",3,false,"false")); - } - //The messages in backup - if ($preferences->backup_messages == 1 && $preferences->backup_course == SITEID) { - fwrite ($bf,full_tag("MESSAGES",3,false,"true")); - } else { - fwrite ($bf,full_tag("MESSAGES",3,false,"false")); - } - //The blogs in backup - if ($preferences->backup_blogs == 1 && $preferences->backup_course == SITEID) { - fwrite ($bf,full_tag("BLOGS",3,false,"true")); - } else { - fwrite ($bf,full_tag("BLOGS",3,false,"false")); - } - //The mode of writing the block data - fwrite ($bf,full_tag('BLOCKFORMAT',3,false,'instances')); - fwrite ($bf,end_tag("DETAILS",2,true)); - - $status = fwrite ($bf,end_tag("INFO",1,true)); - - ///Roles stuff goes in here - - fwrite ($bf, start_tag('ROLES', 1, true)); - $roles = backup_fetch_roles($preferences); - - $sitecontext = get_context_instance(CONTEXT_SYSTEM); - $coursecontext = get_context_instance(CONTEXT_COURSE, $preferences->backup_course); - - foreach ($roles as $role) { - fwrite ($bf,start_tag('ROLE',2,true)); - fwrite ($bf,full_tag('ID', 3, false, $role->id)); - fwrite ($bf,full_tag('NAME',3,false,$role->name)); - fwrite ($bf,full_tag('SHORTNAME',3,false,$role->shortname)); - /// Calculate $role name in course - $nameincourse = role_get_name($role, $coursecontext); - if ($nameincourse != $role->name) { - fwrite ($bf,full_tag('NAMEINCOURSE', 3, false, $nameincourse)); - } - /// List of context level where this role can be assigned. - fwrite ($bf,start_tag('CONTEXTLEVELS',3,true)); - $contextlevels = get_role_contextlevels($role->id); - foreach ($contextlevels as $cl) { - fwrite ($bf,full_tag('CONTEXTLEVEL', 4, false, $cl)); - } - fwrite ($bf,end_tag('CONTEXTLEVELS',3,true)); - /// find and write all default capabilities - fwrite ($bf,start_tag('CAPABILITIES',3,true)); - // pull out all default (site context) capabilities - if ($capabilities = role_context_capabilities($role->id, $sitecontext)) { - foreach ($capabilities as $capability=>$value) { - fwrite ($bf,start_tag('CAPABILITY',4,true)); - fwrite ($bf,full_tag('NAME', 5, false, $capability)); - fwrite ($bf,full_tag('PERMISSION', 5, false, $value)); - // use this to pull out the other info (timemodified and modifierid) - - $cap = $DB->get_record_sql("SELECT * - FROM {role_capabilities} - WHERE capability = ? - AND contextid = ? - AND roleid = ?", array($capability, $sitecontext->id, $role->id)); - fwrite ($bf, full_tag("TIMEMODIFIED", 5, false, $cap->timemodified)); - fwrite ($bf, full_tag("MODIFIERID", 5, false, $cap->modifierid)); - fwrite ($bf,end_tag('CAPABILITY',4,true)); - } - } - fwrite ($bf,end_tag('CAPABILITIES',3,true)); - fwrite ($bf,end_tag('ROLE',2,true)); - } - fwrite ($bf,end_tag('ROLES', 1, true)); - return $status; - } - - //Prints course's general info (table course) - function backup_course_start ($bf,$preferences) { - global $CFG, $DB; - - $status = true; - - //Course open tag - fwrite ($bf,start_tag("COURSE",1,true)); - //Header open tag - fwrite ($bf,start_tag("HEADER",2,true)); - - //Get info from course - $course = $DB->get_record("course", array("id"=>$preferences->backup_course)); - $context = get_context_instance(CONTEXT_COURSE, $course->id); - if ($course) { - //Prints course info - fwrite ($bf,full_tag("ID",3,false,$course->id)); - //Obtain the category - $category = $DB->get_record("course_categories", array("id"=>"$course->category")); - if ($category) { - //Prints category info - fwrite ($bf,start_tag("CATEGORY",3,true)); - fwrite ($bf,full_tag("ID",4,false,$course->category)); - fwrite ($bf,full_tag("NAME",4,false,$category->name)); - fwrite ($bf,end_tag("CATEGORY",3,true)); - } - //Continues with the course - fwrite ($bf,full_tag("PASSWORD",3,false,$course->password)); - fwrite ($bf,full_tag("FULLNAME",3,false,$course->fullname)); - fwrite ($bf,full_tag("SHORTNAME",3,false,$course->shortname)); - fwrite ($bf,full_tag("IDNUMBER",3,false,$course->idnumber)); - fwrite ($bf,full_tag("SUMMARY",3,false,$course->summary)); - fwrite ($bf,full_tag("FORMAT",3,false,$course->format)); - fwrite ($bf,full_tag("SHOWGRADES",3,false,$course->showgrades)); - fwrite ($bf,full_tag("NEWSITEMS",3,false,$course->newsitems)); - fwrite ($bf,full_tag("GUEST",3,false,$course->guest)); - fwrite ($bf,full_tag("STARTDATE",3,false,$course->startdate)); - fwrite ($bf,full_tag("NUMSECTIONS",3,false,$course->numsections)); - //fwrite ($bf,full_tag("SHOWRECENT",3,false,$course->showrecent)); INFO: This is out in 1.3 - fwrite ($bf,full_tag("MAXBYTES",3,false,$course->maxbytes)); - fwrite ($bf,full_tag("SHOWREPORTS",3,false,$course->showreports)); - fwrite ($bf,full_tag("GROUPMODE",3,false,$course->groupmode)); - fwrite ($bf,full_tag("GROUPMODEFORCE",3,false,$course->groupmodeforce)); - fwrite ($bf,full_tag("DEFAULTGROUPINGID",3,false,$course->defaultgroupingid)); - fwrite ($bf,full_tag("LANG",3,false,$course->lang)); - fwrite ($bf,full_tag("THEME",3,false,$course->theme)); - fwrite ($bf,full_tag("COST",3,false,$course->cost)); - fwrite ($bf,full_tag("CURRENCY",3,false,$course->currency)); - fwrite ($bf,full_tag("MARKER",3,false,$course->marker)); - fwrite ($bf,full_tag("VISIBLE",3,false,$course->visible)); - fwrite ($bf,full_tag("HIDDENSECTIONS",3,false,$course->hiddensections)); - fwrite ($bf,full_tag("TIMECREATED",3,false,$course->timecreated)); - fwrite ($bf,full_tag("TIMEMODIFIED",3,false,$course->timemodified)); - //If not selected, force metacourse to 0 - if (!$preferences->backup_metacourse) { - $status = fwrite ($bf,full_tag("METACOURSE",3,false,'0')); - //else, export the field as is in DB - } else { - $status = fwrite ($bf,full_tag("METACOURSE",3,false,$course->metacourse)); - } - fwrite ($bf,full_tag("EXPIRENOTIFY",3,false,$course->expirynotify)); - fwrite ($bf,full_tag("NOTIFYSTUDENTS",3,false,$course->notifystudents)); - fwrite ($bf,full_tag("EXPIRYTHRESHOLD",3,false,$course->expirythreshold)); - fwrite ($bf,full_tag("ENROLLABLE",3,false,$course->enrollable)); - fwrite ($bf,full_tag("ENROLSTARTDATE",3,false,$course->enrolstartdate)); - fwrite ($bf,full_tag("ENROLENDDATE",3,false,$course->enrolenddate)); - fwrite ($bf,full_tag("ENROLPERIOD",3,false,$course->enrolperiod)); - fwrite ($bf,full_tag("ENABLECOMPLETION",3,false,$course->enablecompletion)); - - // Write role assigns, overrides, etc. - write_per_context_data($bf, $preferences, $context, 3); - - // Print header end - fwrite ($bf,end_tag("HEADER",2,true)); - } else { - $status = false; - } - - return $status; - } - - //Prints course's end tag - function backup_course_end ($bf,$preferences) { - - //Course end tag - $status = fwrite ($bf,end_tag("COURSE",1,true)); - - return $status; - - } - - //Prints course's metacourse info (table course_meta) - function backup_course_metacourse ($bf,$preferences) { - global $CFG, $DB; - - $status = true; - - //Get info from meta - $parents = $DB->get_records_sql ("SELECT m.*, c.idnumber, c.shortname - FROM {course_meta} m, {course} c - WHERE m.child_course = ? AND - m.parent_course = c.id", array($preferences->backup_course)); - $childs = $DB->get_records_sql ("SELECT m.*, c.idnumber, c.shortname - FROM {course_meta} m, {course} c - WHERE m.parent_course = ? AND - m.child_course = c.id", array($preferences->backup_course)); - - if ($parents || $childs) { - //metacourse open tag - fwrite ($bf,start_tag("METACOURSE",2,true)); - if ($parents) { - fwrite($bf, start_tag("PARENTS",3,true)); - //Iterate over every parent - foreach ($parents as $parent) { - //Begin parent - fwrite ($bf,start_tag("PARENT",4,true)); - fwrite ($bf,full_tag("ID",5,false,$parent->parent_course)); - fwrite ($bf,full_tag("IDNUMBER",5,false,$parent->idnumber)); - fwrite ($bf,full_tag("SHORTNAME",5,false,$parent->shortname)); - //End parent - fwrite ($bf,end_tag("PARENT",4,true)); - } - fwrite ($bf,end_tag("PARENTS",3,true)); - } - if ($childs) { - fwrite($bf, start_tag("CHILDS",3,true)); - //Iterate over every child - foreach ($childs as $child) { - //Begin parent - fwrite ($bf,start_tag("CHILD",4,true)); - fwrite ($bf,full_tag("ID",5,false,$child->child_course)); - fwrite ($bf,full_tag("IDNUMBER",5,false,$child->idnumber)); - fwrite ($bf,full_tag("SHORTNAME",5,false,$child->shortname)); - //End parent - fwrite ($bf,end_tag("CHILD",4,true)); - } - fwrite ($bf,end_tag("CHILDS",3,true)); - } - //metacourse close tag - $status = fwrite ($bf,end_tag("METACOURSE",3,true)); - } - - return $status; - - } - //Prints course's messages info (tables message, message_read and message_contacts) function backup_messages ($bf,$preferences) { global $CFG, $DB; @@ -969,141 +200,6 @@ fwrite($bf, end_tag("BLOG",$level,true)); } - - - /** - * Prints course's blocks info (table block_instance) - */ - function backup_course_blocks ($bf,$preferences) { - global $CFG, $DB; - - $status = true; - - // Read all of the block table - $blocks = blocks_get_record(); - - $pages = array(); - $pages[] = page_create_object(PAGE_COURSE_VIEW, $preferences->backup_course); - - // get course structure - $course = $DB->get_record('course', array('id'=>$preferences->backup_course)); - $modinfo =& get_fast_modinfo($course); - - // backup blocks on mod pages. - foreach($preferences->mods as $module) { - if (!$module->backup) { - continue; - } - - if (empty($modinfo->instances[$module->name])) { - continue; - } - - $pagetypes = page_import_types('mod/'.$module->name.'/'); - if (empty($pagetypes)) { - continue; - } - - foreach($pagetypes as $pagetype) { - foreach($modinfo->instances[$module->name] as $cm) { - if (!empty($module->instances[$cm->instance]->backup)) { - $pages[] = page_create_object($pagetype, $cm->instance); - } - } - } - } - - //Blocks open tag - fwrite ($bf,start_tag('BLOCKS',2,true)); - - foreach($pages as $page) { - if ($instances = array()) { // TODO fix. - //Iterate over every block - foreach ($instances as $position) { - foreach ($position as $instance) { - - //If we somehow have a block with an invalid id, skip it - if(empty($blocks[$instance->blockid]->name)) { - continue; - } - $blockname = $blocks[$instance->blockid]->name; - - if (!$blockobj = block_instance($blockname, $instance)) { - // Invalid block - continue; - } - - // encode absolute links in block config - $instance->configdata = $blockobj->get_backup_encoded_config(); - - //Begin Block - fwrite ($bf,start_tag('BLOCK',3,true)); - fwrite ($bf,full_tag('ID', 4, false,$instance->id)); - fwrite ($bf,full_tag('NAME',4,false,$blockname)); - fwrite ($bf,full_tag('PAGEID',4,false,$instance->pageid)); - fwrite ($bf,full_tag('PAGETYPE',4,false,$instance->pagetype)); - fwrite ($bf,full_tag('POSITION',4,false,$instance->position)); - fwrite ($bf,full_tag('WEIGHT',4,false,$instance->weight)); - fwrite ($bf,full_tag('VISIBLE',4,false,$instance->visible)); - fwrite ($bf,full_tag('CONFIGDATA',4,false,$instance->configdata)); - // Write instance data if needed - if ($blockobj->backuprestore_instancedata_used()) { - fwrite ($bf,start_tag('INSTANCEDATA',4,true)); - $status = $blockobj->instance_backup($bf, $preferences); - fwrite ($bf,end_tag('INSTANCEDATA',4,true)); - } - - // Write role assigns, overrides, etc. - $context = get_context_instance(CONTEXT_BLOCK, $instance->id); - write_per_context_data($bf, $preferences, $context, 4); - - //End Block - fwrite ($bf,end_tag('BLOCK',3,true)); - } - } - } - } - - //Blocks close tag - $status = fwrite ($bf,end_tag('BLOCKS',2,true)); - - return $status; - - } - - //Prints course's sections info (table course_sections) - function backup_course_sections ($bf,$preferences) { - global $CFG, $DB; - - $status = true; - - - //Get info from sections - $section=false; - if ($sections = $DB->get_records("course_sections", array("course"=>$preferences->backup_course),"section")) { - //Section open tag - fwrite ($bf,start_tag("SECTIONS",2,true)); - //Iterate over every section (ordered by section) - foreach ($sections as $section) { - //Begin Section - fwrite ($bf,start_tag("SECTION",3,true)); - fwrite ($bf,full_tag("ID",4,false,$section->id)); - fwrite ($bf,full_tag("NUMBER",4,false,$section->section)); - fwrite ($bf,full_tag("SUMMARY",4,false,$section->summary)); - fwrite ($bf,full_tag("VISIBLE",4,false,$section->visible)); - //Now print the mods in section - backup_course_modules ($bf,$preferences,$section); - //End section - fwrite ($bf,end_tag("SECTION",3,true)); - } - //Section close tag - $status = fwrite ($bf,end_tag("SECTIONS",2,true)); - } - - return $status; - - } - //Prints course's format data (any data the format might want to save). function backup_format_data ($bf,$preferences) { global $CFG, $DB; @@ -1133,346 +229,6 @@ return fwrite ($bf,end_tag("FORMATDATA",2,true)); } - //Prints course's modules info (table course_modules) - //Only for selected mods in preferences - function backup_course_modules ($bf,$preferences,$section) { - global $CFG, $DB; - - $status = true; - - $first_record = true; - - $course=$DB->get_record('course',array('id'=>$preferences->backup_course)); - if(!$course) { - return false; - } - - //Now print the mods in section - //Extracts mod id from sequence - $tok = strtok($section->sequence,","); - while ($tok) { - //Get module's type - $moduletype = get_module_type ($preferences->backup_course,$tok); - //Check if we've selected to backup that type - if ($moduletype and $preferences->mods[$moduletype]->backup) { - $selected = true; - } else { - $selected = false; - } - - if ($selected) { - $context = get_context_instance(CONTEXT_MODULE, $tok); - //Gets course_module data from db - verify activity exists and is enabled! - $sql = "SELECT cm.* - FROM {course_modules} cm - JOIN {modules} m ON m.id = cm.module - JOIN {".$moduletype."} a ON a.id = cm.instance - WHERE m.visible = 1 AND cm.id = $tok"; - if (!$course_module = $DB->get_record_sql($sql)) { - // cm exists but activity instance missing - probably caused by double clicking - $tok = strtok(","); - continue; - } - - //If it's the first, pring MODS tag - if ($first_record) { - fwrite ($bf,start_tag("MODS",4,true)); - $first_record = false; - } - // if we're doing selected instances, check that too. - if (is_array($preferences->mods[$moduletype]->instances) - && count($preferences->mods[$moduletype]->instances) - && (!array_key_exists($course_module->instance,$preferences->mods[$moduletype]->instances) - || empty($preferences->mods[$moduletype]->instances[$course_module->instance]->backup))) { - $tok = strtok(","); - continue; - } - - // find all role values that has an override in this context - $roles = $DB->get_records('role_capabilities', array('contextid'=>$context->id)); - - //Print mod info from course_modules - fwrite ($bf,start_tag("MOD",5,true)); - //Save neccesary info to backup_ids - fwrite ($bf,full_tag("ID",6,false,$tok)); - fwrite ($bf,full_tag("TYPE",6,false,$moduletype)); - fwrite ($bf,full_tag("INSTANCE",6,false,$course_module->instance)); - fwrite ($bf,full_tag("ADDED",6,false,$course_module->added)); - fwrite ($bf,full_tag("SCORE",6,false,$course_module->score)); - fwrite ($bf,full_tag("INDENT",6,false,$course_module->indent)); - fwrite ($bf,full_tag("VISIBLE",6,false,$course_module->visible)); - fwrite ($bf,full_tag("GROUPMODE",6,false,$course_module->groupmode)); - fwrite ($bf,full_tag("GROUPINGID",6,false,$course_module->groupingid)); - fwrite ($bf,full_tag("GROUPMEMBERSONLY",6,false,$course_module->groupmembersonly)); - fwrite ($bf,full_tag("IDNUMBER",6,false,$course_module->idnumber)); - fwrite ($bf,full_tag("COMPLETION",6,false,$course_module->completion)); - fwrite ($bf,full_tag("COMPLETIONGRADEITEMNUMBER",6,false,$course_module->completiongradeitemnumber)); - fwrite ($bf,full_tag("COMPLETIONVIEW",6,false,$course_module->completionview)); - fwrite ($bf,full_tag("COMPLETIONEXPECTED",6,false,$course_module->completionexpected)); - fwrite ($bf,full_tag("AVAILABLEFROM",6,false,$course_module->availablefrom)); - fwrite ($bf,full_tag("AVAILABLEUNTIL",6,false,$course_module->availableuntil)); - fwrite ($bf,full_tag("SHOWAVAILABILITY",6,false,$course_module->showavailability)); - - // Write role assigns, overrides, etc. - write_per_context_data($bf, $preferences, $context, 6); - - // write completion data if enabled and user data enabled - require_once($CFG->libdir.'/completionlib.php'); - $completion=new completion_info($course); - if($completion->is_enabled($course_module) && - backup_userdata_selected($preferences,$moduletype,$course_module->instance)) { - fwrite ($bf,start_tag("COMPLETIONDATA",6,true)); - - // Get all completion records for this module and loop - $data=$DB->get_records('course_modules_completion',array('coursemoduleid'=>$course_module->id)); - $data=$data ? $data : array(); - foreach($data as $completion) { - // Write completion record - fwrite ($bf,start_tag("COMPLETION",7,true)); - fwrite ($bf,full_tag("USERID",8,false,$completion->userid)); - fwrite ($bf,full_tag("COMPLETIONSTATE",8,false,$completion->completionstate)); - fwrite ($bf,full_tag("VIEWED",8,false,$completion->viewed)); - fwrite ($bf,full_tag("TIMEMODIFIED",8,false,$completion->timemodified)); - fwrite ($bf,end_tag("COMPLETION",7,true)); - } - - fwrite ($bf,end_tag("COMPLETIONDATA",6,true)); - } - - // Write availability data if enabled - require_once($CFG->libdir.'/conditionlib.php'); - if(!empty($CFG->enableavailability)) { - fwrite ($bf,start_tag("AVAILABILITYDATA",6,true)); - // Get all availability restrictions for this activity - $data=$DB->get_records('course_modules_availability', - array('coursemoduleid'=>$course_module->id)); - $data=$data ? $data : array(); - foreach($data as $availability) { - // Write availability record - fwrite ($bf,start_tag("AVAILABILITY",7,true)); - fwrite ($bf,full_tag("SOURCECMID",8,false,$availability->sourcecmid)); - fwrite ($bf,full_tag("REQUIREDCOMPLETION",8,false,$availability->requiredcompletion)); - fwrite ($bf,full_tag("GRADEITEMID",8,false,$availability->gradeitemid)); - fwrite ($bf,full_tag("GRADEMIN",8,false,$availability->grademin)); - fwrite ($bf,full_tag("GRADEMAX",8,false,$availability->grademax)); - fwrite ($bf,end_tag("AVAILABILITY",7,true)); - } - fwrite ($bf,end_tag("AVAILABILITYDATA",6,true)); - } - - fwrite ($bf,end_tag("MOD",5,true)); - } - //check for next - $tok = strtok(","); - } - - //Si ha habido modulos, final de MODS - if (!$first_record) { - $status =fwrite ($bf,end_tag("MODS",4,true)); - } - - return $status; - } - - //Print users to xml - //Only users previously calculated in backup_ids will output - // - function backup_user_info ($bf,$preferences) { - global $CFG, $DB; - require_once ($CFG->dirroot.'/tag/lib.php'); - - $status = true; - - // Use a recordset to for the memory handling on to - // the DB and run faster - // Note the outer join with mnet_host: It shouldn't be neccesary - // but there are some sites having mnet_host records missing - // and that causes backup to fail (no users). Being a bit more - // flexible here (outer joing) we bypass the problem and doesn't - // cause more troubles. Eloy - MDL-16879 - $users = $DB->get_recordset_sql("SELECT b.old_id, b.table_name, b.info, - u.*, m.wwwroot - FROM {backup_ids} b - JOIN {user} u ON b.old_id=u.id - LEFT JOIN {mnet_host} m ON u.mnethostid=m.id - WHERE b.backup_code = ? AND - b.table_name = 'user'", array($preferences->backup_unique_code)); - - //If we have users to backup - if ($users && $users->valid()) { - //Begin Users tag - fwrite ($bf,start_tag("USERS",2,true)); - $counter = 0; - //With every user - foreach ($users as $user) { - //Begin User tag - fwrite ($bf,start_tag("USER",3,true)); - //Output all user data - fwrite ($bf,full_tag("ID",4,false,$user->id)); - fwrite ($bf,full_tag("AUTH",4,false,$user->auth)); - fwrite ($bf,full_tag("CONFIRMED",4,false,$user->confirmed)); - fwrite ($bf,full_tag("POLICYAGREED",4,false,$user->policyagreed)); - fwrite ($bf,full_tag("DELETED",4,false,$user->deleted)); - fwrite ($bf,full_tag("USERNAME",4,false,$user->username)); - // Prevent user passwords in backup files unless - // $CFG->includeuserpasswordsinbackup is defined. MDL-20838 - if (!empty($CFG->includeuserpasswordsinbackup)) { - fwrite ($bf,full_tag("PASSWORD",4,false,$user->password)); - } - fwrite ($bf,full_tag("IDNUMBER",4,false,$user->idnumber)); - fwrite ($bf,full_tag("FIRSTNAME",4,false,$user->firstname)); - fwrite ($bf,full_tag("LASTNAME",4,false,$user->lastname)); - fwrite ($bf,full_tag("EMAIL",4,false,$user->email)); - fwrite ($bf,full_tag("EMAILSTOP",4,false,$user->emailstop)); - fwrite ($bf,full_tag("ICQ",4,false,$user->icq)); - fwrite ($bf,full_tag("SKYPE",4,false,$user->skype)); - fwrite ($bf,full_tag("YAHOO",4,false,$user->yahoo)); - fwrite ($bf,full_tag("AIM",4,false,$user->aim)); - fwrite ($bf,full_tag("MSN",4,false,$user->msn)); - fwrite ($bf,full_tag("PHONE1",4,false,$user->phone1)); - fwrite ($bf,full_tag("PHONE2",4,false,$user->phone2)); - fwrite ($bf,full_tag("INSTITUTION",4,false,$user->institution)); - fwrite ($bf,full_tag("DEPARTMENT",4,false,$user->department)); - fwrite ($bf,full_tag("ADDRESS",4,false,$user->address)); - fwrite ($bf,full_tag("CITY",4,false,$user->city)); - fwrite ($bf,full_tag("COUNTRY",4,false,$user->country)); - fwrite ($bf,full_tag("LANG",4,false,$user->lang)); - fwrite ($bf,full_tag("THEME",4,false,$user->theme)); - fwrite ($bf,full_tag("TIMEZONE",4,false,$user->timezone)); - fwrite ($bf,full_tag("FIRSTACCESS",4,false,$user->firstaccess)); - fwrite ($bf,full_tag("LASTACCESS",4,false,$user->lastaccess)); - fwrite ($bf,full_tag("LASTLOGIN",4,false,$user->lastlogin)); - fwrite ($bf,full_tag("CURRENTLOGIN",4,false,$user->currentlogin)); - fwrite ($bf,full_tag("LASTIP",4,false,$user->lastip)); - fwrite ($bf,full_tag("PICTURE",4,false,$user->picture)); - fwrite ($bf,full_tag("URL",4,false,$user->url)); - fwrite ($bf,full_tag("DESCRIPTION",4,false,$user->description)); - fwrite ($bf,full_tag("MAILFORMAT",4,false,$user->mailformat)); - fwrite ($bf,full_tag("MAILDIGEST",4,false,$user->maildigest)); - fwrite ($bf,full_tag("MAILDISPLAY",4,false,$user->maildisplay)); - fwrite ($bf,full_tag("HTMLEDITOR",4,false,$user->htmleditor)); - fwrite ($bf,full_tag("AJAX",4,false,$user->ajax)); - fwrite ($bf,full_tag("AUTOSUBSCRIBE",4,false,$user->autosubscribe)); - fwrite ($bf,full_tag("TRACKFORUMS",4,false,$user->trackforums)); - if ($user->mnethostid != $CFG->mnet_localhost_id && !empty($user->wwwroot)) { - fwrite ($bf,full_tag("MNETHOSTURL",4,false,$user->wwwroot)); - } - fwrite ($bf,full_tag("TIMECREATED",4,false,$user->timecreated)); - fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$user->timemodified)); - - /// write assign/override code for context_userid - - $user->isneeded = strpos($user->info,"needed"); - - fwrite ($bf,start_tag("ROLES",4,true)); - if ($user->info != "needed" && $user->info!="") { - //PRINT ROLE INFO - $roles = explode(",", $user->info); - foreach ($roles as $role) { - if ($role!="" && $role!="needed") { - fwrite ($bf,start_tag("ROLE",5,true)); - //Print Role info - fwrite ($bf,full_tag("TYPE",6,false,$role)); - //Print ROLE end - fwrite ($bf,end_tag("ROLE",5,true)); - } - } - } - //Needed - if ($user->isneeded!==false) { - //Print ROLE start - fwrite ($bf,start_tag("ROLE",5,true)); - //Print Role info - fwrite ($bf,full_tag("TYPE",6,false,"needed")); - //Print ROLE end - fwrite ($bf,end_tag("ROLE",5,true)); - } - - //End ROLES tag - fwrite ($bf,end_tag("ROLES",4,true)); - - //Check if we have custom profile fields to backup - if ($cpfields = $DB->get_records_sql("SELECT uif.shortname, uif.datatype, uidata.data - FROM {user_info_field} uif, - {user_info_data} uidata - WHERE uif.id = uidata.fieldid - AND uidata.userid = ?", array($user->id))) { - //Start USER_CUSTOM_PROFILE_FIELDS tag - fwrite ($bf,start_tag("USER_CUSTOM_PROFILE_FIELDS",4,true)); - //Write custom profile fields - foreach ($cpfields as $cpfield) { - fwrite ($bf,start_tag("USER_CUSTOM_PROFILE_FIELD",5,true)); - fwrite ($bf,full_tag("FIELD_NAME",6,false,$cpfield->shortname)); - fwrite ($bf,full_tag("FIELD_TYPE",6,false,$cpfield->datatype)); - fwrite ($bf,full_tag("FIELD_DATA",6,false,$cpfield->data)); - fwrite ($bf,end_tag("USER_CUSTOM_PROFILE_FIELD",5,true)); - } - //End USER_CUSTOM_PROFILE_FIELDS tag - fwrite ($bf,end_tag("USER_CUSTOM_PROFILE_FIELDS",4,true)); - } - - //Check if we have user tags to backup - if (!empty($CFG->usetags)) { - if ($tags = tag_get_tags('user', $user->id)) { //This return them ordered by default - //Start USER_TAGS tag - fwrite ($bf,start_tag("USER_TAGS",4,true)); - //Write user tags fields - foreach ($tags as $tag) { - fwrite ($bf,start_tag("USER_TAG",5,true)); - fwrite ($bf,full_tag("NAME",6,false,$tag->name)); - fwrite ($bf,full_tag("RAWNAME",6,false,$tag->rawname)); - fwrite ($bf,end_tag("USER_TAG",5,true)); - } - //End USER_TAGS tag - fwrite ($bf,end_tag("USER_TAGS",4,true)); - } - } - - //Check if we have user_preferences to backup - if ($preferences_data = $DB->get_records("user_preferences", array("userid"=>$user->old_id))) { - //Start USER_PREFERENCES tag - fwrite ($bf,start_tag("USER_PREFERENCES",4,true)); - //Write each user_preference - foreach ($preferences_data as $user_preference) { - fwrite ($bf,start_tag("USER_PREFERENCE",5,true)); - fwrite ($bf,full_tag("NAME",6,false,$user_preference->name)); - fwrite ($bf,full_tag("VALUE",6,false,$user_preference->value)); - fwrite ($bf,end_tag("USER_PREFERENCE",5,true)); - } - //End USER_PREFERENCES tag - fwrite ($bf,end_tag("USER_PREFERENCES",4,true)); - } - - // Write role assigns, overrides, etc. - $context = get_context_instance(CONTEXT_USER, $user->old_id); - write_per_context_data($bf, $preferences, $context, 4); - - //End User tag - fwrite ($bf,end_tag("USER",3,true)); - //Do some output - $counter++; - if ($counter % 10 == 0) { - echo "."; - if ($counter % 200 == 0) { - echo "
"; - } - backup_flush(300); - } - } - //End Users tag - fwrite ($bf,end_tag("USERS",2,true)); - } else { - // There aren't any users. - $status = true; - } - - if ($users) { - $users->close(); - } - - return $status; - } - //Backup log info (time ordered) function backup_log_info($bf,$preferences) { global $CFG, $DB; @@ -1541,302 +297,6 @@ return $status; } - //Backup gradebook info - function backup_gradebook_info($bf, $preferences) { - global $CFG, $DB; - require_once($CFG->libdir.'/gradelib.php'); - - //first make sure items are properly sorted and everything is ok - grade_category::fetch_course_tree($preferences->backup_course, true); - grade_regrade_final_grades($preferences->backup_course); - - $status = true; - - // see if ALL grade items of type mod of this course are being backed up - // if not, we do not need to backup grade category and associated grade items/grades - $backupall = true; - - if ($grade_items = $DB->get_records_sql("SELECT * - FROM {grade_items} - WHERE courseid = ? - AND itemtype = 'mod'", array($preferences->backup_course))) { - foreach ($grade_items as $grade_item) { - // get module information - // if some activities not selected, we do not backup categories at all - if (!backup_mod_selected($preferences,$grade_item->itemmodule,$grade_item->iteminstance)) { - $backupall = false; - break; - } - } - unset($grade_items); //free memory - } - - //Gradebook header - fwrite ($bf,start_tag("GRADEBOOK",2,true)); - - $status = backup_gradebook_outcomes_info($bf, $preferences); - $status = backup_gradebook_grade_letters_info($bf,$preferences); - - // Now backup grade_item (inside grade_category) - if ($backupall) { - $status = backup_gradebook_category_info($bf,$preferences); - } - - $status = backup_gradebook_item_info($bf,$preferences, $backupall); - - // backup gradebook histories (only if grade history is enabled and selected) - if (empty($CFG->disablegradehistory) && $preferences->backup_gradebook_history) { - $status = backup_gradebook_outcomes_history($bf, $preferences); - $status = backup_gradebook_categories_history_info($bf, $preferences); - $status = backup_gradebook_items_history_info($bf, $preferences); - $status = backup_gradebook_grades_history_info($bf, $preferences); - } - - //Gradebook footer - $status = fwrite ($bf,end_tag("GRADEBOOK",2,true)); - return $status; - } - - function backup_gradebook_category_info($bf, $preferences) { - global $CFG, $DB; - $status = true; - - // get grade categories in proper order - specified in category grade items - $course_item = grade_item::fetch_course_item($preferences->backup_course); - $grade_categories = $DB->get_records_sql("SELECT gc.*, gi.sortorder - FROM {grade_categories} gc - JOIN {grade_items} gi ON (gi.iteminstance = gc.id) - WHERE gc.courseid = ? - AND (gi.itemtype='course' OR gi.itemtype='category') - ORDER BY gi.sortorder ASC", array($preferences->backup_course)); - - if ($grade_categories) { - //Begin grade_categories tag - fwrite ($bf,start_tag("GRADE_CATEGORIES",3,true)); - //Iterate for each category - foreach ($grade_categories as $grade_category) { - //Begin grade_category - fwrite ($bf,start_tag("GRADE_CATEGORY",4,true)); - //Output individual fields - fwrite ($bf,full_tag("ID",5,false,$grade_category->id)); - - // not keeping path and depth because they can be derived - fwrite ($bf,full_tag("PARENT",5,false,$grade_category->parent)); - fwrite ($bf,full_tag("FULLNAME",5,false,$grade_category->fullname)); - fwrite ($bf,full_tag("AGGREGATION",5,false,$grade_category->aggregation)); - fwrite ($bf,full_tag("KEEPHIGH",5,false,$grade_category->keephigh)); - fwrite ($bf,full_tag("DROPLOW",5,false,$grade_category->droplow)); - fwrite ($bf,full_tag("AGGREGATEONLYGRADED",5,false,$grade_category->aggregateonlygraded)); - fwrite ($bf,full_tag("AGGREGATEOUTCOMES",5,false,$grade_category->aggregateoutcomes)); - fwrite ($bf,full_tag("AGGREGATESUBCATS",5,false,$grade_category->aggregatesubcats)); - fwrite ($bf,full_tag("TIMECREATED",5,false,$grade_category->timecreated)); - fwrite ($bf,full_tag("TIMEMODIFIED",5,false,$grade_category->timemodified)); - - //End grade_category - fwrite ($bf,end_tag("GRADE_CATEGORY",4,true)); - } - //End grade_categories tag - $status = fwrite ($bf,end_tag("GRADE_CATEGORIES",3,true)); - } - - return $status; - } - - //Backup gradebook_item (called from backup_gradebook_info - function backup_gradebook_item_info($bf, $preferences, $backupall) { - global $CFG, $DB; - - $status = true; - // get all the grade_items, ordered by sort order since upon restoring, it is not always - // possible to use the same sort order. We could at least preserve the sortorder by restoring - // grade_items in the original sortorder - if ($grade_items = $DB->get_records_sql("SELECT * - FROM {grade_items} - WHERE courseid = ? - ORDER BY sortorder ASC", array($preferences->backup_course))) { - - //Begin grade_items tag - fwrite ($bf,start_tag("GRADE_ITEMS",3,true)); - //Iterate over each item - foreach ($grade_items as $item) { - // Instantiate a grade_item object, to access its methods - $grade_item = new grade_item($item, false); - - // do not restore if this grade_item is a mod, and - if ($grade_item->itemtype == 'mod') { - //MDL-12463 - exclude grade_items of instances not chosen for backup - if (empty($preferences->mods[$grade_item->itemmodule]->instances[$grade_item->iteminstance]->backup)) { - continue; - } - - } else if ($grade_item->itemtype == 'category') { - // if not all grade items are being backed up - // we ignore this type of grade_item and grades associated - if (!$backupall) { - continue; - } - } - - //Begin grade_item - fwrite ($bf,start_tag("GRADE_ITEM",4,true)); - //Output individual fields - - fwrite ($bf,full_tag("ID",5,false,$grade_item->id)); - fwrite ($bf,full_tag("CATEGORYID",5,false,$grade_item->categoryid)); - fwrite ($bf,full_tag("ITEMNAME",5,false,$grade_item->itemname)); - fwrite ($bf,full_tag("ITEMTYPE",5,false,$grade_item->itemtype)); - fwrite ($bf,full_tag("ITEMMODULE",5,false,$grade_item->itemmodule)); - fwrite ($bf,full_tag("ITEMINSTANCE",5,false,$grade_item->iteminstance)); - fwrite ($bf,full_tag("ITEMNUMBER",5,false,$grade_item->itemnumber)); - fwrite ($bf,full_tag("ITEMINFO",5,false,$grade_item->iteminfo)); - fwrite ($bf,full_tag("IDNUMBER",5,false,$grade_item->idnumber)); - // use [idnumber] in calculation instead of [#giXXX#] - fwrite ($bf,full_tag("CALCULATION",5,false,$grade_item->get_calculation())); - fwrite ($bf,full_tag("GRADETYPE",5,false,$grade_item->gradetype)); - fwrite ($bf,full_tag("GRADEMAX",5,false,$grade_item->grademax)); - fwrite ($bf,full_tag("GRADEMIN",5,false,$grade_item->grademin)); - fwrite ($bf,full_tag("SCALEID",5,false,$grade_item->scaleid)); - fwrite ($bf,full_tag("OUTCOMEID",5,false,$grade_item->outcomeid)); - fwrite ($bf,full_tag("GRADEPASS",5,false,$grade_item->gradepass)); - fwrite ($bf,full_tag("MULTFACTOR",5,false,$grade_item->multfactor)); - fwrite ($bf,full_tag("PLUSFACTOR",5,false,$grade_item->plusfactor)); - fwrite ($bf,full_tag("AGGREGATIONCOEF",5,false,$grade_item->aggregationcoef)); - fwrite ($bf,full_tag("DISPLAY",5,false,$grade_item->display)); - fwrite ($bf,full_tag("DECIMALS",5,false,$grade_item->decimals)); - fwrite ($bf,full_tag("HIDDEN",5,false,$grade_item->hidden)); - fwrite ($bf,full_tag("LOCKED",5,false,$grade_item->locked)); - fwrite ($bf,full_tag("LOCKTIME",5,false,$grade_item->locktime)); - fwrite ($bf,full_tag("NEEDSUPDATE",5,false,$grade_item->needsupdate)); - fwrite ($bf,full_tag("TIMECREATED",5,false,$grade_item->timecreated)); - fwrite ($bf,full_tag("TIMEMODIFIED",5,false,$grade_item->timemodified)); - - // back up the other stuff here - // mod grades should only be backed up if selected - if ($grade_item->itemtype == 'mod' && !backup_userdata_selected($preferences,$grade_item->itemmodule,$grade_item->iteminstance)) { - // do not write grades if a mod grade_item is being restored - // but userdata is not selected - } else { - $status = backup_gradebook_grades_info($bf,$preferences,$grade_item->id); - } - //End grade_item - fwrite ($bf,end_tag("GRADE_ITEM",4,true)); - } - //End grade_items tag - $status = fwrite ($bf,end_tag("GRADE_ITEMS",3,true)); - } - - return $status; - } - //Backup gradebook_item (called from backup_gradebook_info - - function backup_gradebook_grade_letters_info($bf, $preferences) { - global $CFG, $DB; - $status = true; - - $context = get_context_instance(CONTEXT_COURSE, $preferences->backup_course); - $grade_letters = $DB->get_records_sql("SELECT * - FROM {grade_letters} - WHERE contextid = ?", array($context->id)); - if ($grade_letters) { - //Begin grade_l tag - fwrite ($bf,start_tag("GRADE_LETTERS",3,true)); - //Iterate for each letter - foreach ($grade_letters as $grade_letter) { - //Begin grade_letter - fwrite ($bf,start_tag("GRADE_LETTER",4,true)); - //Output individual fields - fwrite ($bf,full_tag("ID",5,false,$grade_letter->id)); - fwrite ($bf,full_tag("LOWERBOUNDARY",5,false,$grade_letter->lowerboundary)); - fwrite ($bf,full_tag("LETTER",5,false,$grade_letter->letter)); - - //End grade_letter - fwrite ($bf,end_tag("GRADE_LETTER",4,true)); - } - //End grade_categories tag - $status = fwrite ($bf,end_tag("GRADE_LETTERS",3,true)); - } - - return $status; - } - - function backup_gradebook_outcomes_info($bf, $preferences) { - global $CFG, $DB; - - $status = true; - // only back up courses already in the grade_outcomes_courses table - $grade_outcomes = $DB->get_records_sql("SELECT go.* - FROM {grade_outcomes} go - JOIN {grade_outcomes_courses} goc ON (goc.outcomeid = go.id) - WHERE goc.courseid = ?", array($preferences->backup_course)); - - if (!empty($grade_outcomes)) { - //Begin grade_outcomes tag - fwrite ($bf,start_tag("GRADE_OUTCOMES",3,true)); - //Iterate for each outcome - foreach ($grade_outcomes as $grade_outcome) { - //Begin grade_outcome - fwrite ($bf,start_tag("GRADE_OUTCOME",4,true)); - //Output individual fields - - fwrite ($bf,full_tag("ID",5,false,$grade_outcome->id)); - fwrite ($bf,full_tag("COURSEID",5,false,$grade_outcome->courseid)); - fwrite ($bf,full_tag("SHORTNAME",5,false,$grade_outcome->shortname)); - fwrite ($bf,full_tag("FULLNAME",5,false,$grade_outcome->fullname)); - fwrite ($bf,full_tag("SCALEID",5,false,$grade_outcome->scaleid)); - fwrite ($bf,full_tag("DESCRIPTION",5,false,$grade_outcome->description)); - fwrite ($bf,full_tag("TIMECREATED",5,false,$grade_outcome->timecreated)); - fwrite ($bf,full_tag("TIMEMODIFIED",5,false,$grade_outcome->timemodified)); - fwrite ($bf,full_tag("USERMODIFIED",5,false,$grade_outcome->usermodified)); - - //End grade_outcome - fwrite ($bf,end_tag("GRADE_OUTCOME",4,true)); - } - //End grade_outcomes tag - $status = fwrite ($bf,end_tag("GRADE_OUTCOMES",3,true)); - } - return $status; - } - - function backup_gradebook_grades_info($bf,$preferences, $itemid) { - global $CFG, $DB; - - $status = true; - - // find all grades belonging to this item - if ($grades = $DB->get_records('grade_grades', array('itemid'=>$itemid))) { - fwrite ($bf,start_tag("GRADE_GRADES",5,true)); - foreach ($grades as $grade) { - /// Grades are only sent to backup if the user is one target user - if (backup_getid($preferences->backup_unique_code, 'user', $grade->userid)) { - fwrite ($bf,start_tag("GRADE",6,true)); - fwrite ($bf,full_tag("ID",7,false,$grade->id)); - fwrite ($bf,full_tag("USERID",7,false,$grade->userid)); - fwrite ($bf,full_tag("RAWGRADE",7,false,$grade->rawgrade)); - fwrite ($bf,full_tag("RAWGRADEMAX",7,false,$grade->rawgrademax)); - fwrite ($bf,full_tag("RAWGRADEMIN",7,false,$grade->rawgrademin)); - fwrite ($bf,full_tag("RAWSCALEID",7,false,$grade->rawscaleid)); - fwrite ($bf,full_tag("USERMODIFIED",7,false,$grade->usermodified)); - fwrite ($bf,full_tag("FINALGRADE",7,false,$grade->finalgrade)); - fwrite ($bf,full_tag("HIDDEN",7,false,$grade->hidden)); - fwrite ($bf,full_tag("LOCKED",7,false,$grade->locked)); - fwrite ($bf,full_tag("LOCKTIME",7,false,$grade->locktime)); - fwrite ($bf,full_tag("EXPORTED",7,false,$grade->exported)); - fwrite ($bf,full_tag("OVERRIDDEN",7,false,$grade->overridden)); - fwrite ($bf,full_tag("EXCLUDED",7,false,$grade->excluded)); - fwrite ($bf,full_tag("FEEDBACK",7,false,$grade->feedback)); - fwrite ($bf,full_tag("FEEDBACKFORMAT",7,false,$grade->feedbackformat)); - fwrite ($bf,full_tag("INFORMATION",7,false,$grade->information)); - fwrite ($bf,full_tag("INFORMATIONFORMAT",7,false,$grade->informationformat)); - fwrite ($bf,full_tag("TIMECREATED",7,false,$grade->timecreated)); - fwrite ($bf,full_tag("TIMEMODIFIED",7,false,$grade->timemodified)); - fwrite ($bf,end_tag("GRADE",6,true)); - } - } - $status = fwrite ($bf,end_tag("GRADE_GRADES",5,true)); - } - return $status; - } - function backup_gradebook_categories_history_info($bf, $preferences) { global $CFG, $DB; @@ -1990,55 +450,6 @@ return $status; } - //Backup scales info (common and course scales) - function backup_scales_info($bf,$preferences) { - global $CFG, $DB; - - $status = true; - - //Counter, points to current record - $counter = 0; - - //Get scales (common and course scales) - $scales = $DB->get_records_sql("SELECT id, courseid, userid, name, scale, description, timemodified - FROM {scale} - WHERE courseid = 0 OR courseid = ?", array($preferences->backup_course)); - - //Copy only used scales to $backupscales. They will be in backup (unused no). See Bug 1223. - $backupscales = array(); - if ($scales) { - foreach ($scales as $scale) { - if (course_scale_used($preferences->backup_course, $scale->id)) { - $backupscales[] = $scale; - } - } - } - - //Pring scales header - if ($backupscales) { - //Pring scales header - fwrite ($bf,start_tag("SCALES",2,true)); - //Iterate - foreach ($backupscales as $scale) { - //Begin scale tag - fwrite ($bf,start_tag("SCALE",3,true)); - //Output scale tag - fwrite ($bf,full_tag("ID",4,false,$scale->id)); - fwrite ($bf,full_tag("COURSEID",4,false,$scale->courseid)); - fwrite ($bf,full_tag("USERID",4,false,$scale->userid)); - fwrite ($bf,full_tag("NAME",4,false,$scale->name)); - fwrite ($bf,full_tag("SCALETEXT",4,false,$scale->scale)); - fwrite ($bf,full_tag("DESCRIPTION",4,false,$scale->description)); - fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$scale->timemodified)); - //End scale tag - fwrite ($bf,end_tag("SCALE",3,true)); - } - //End scales tag - $status = fwrite ($bf,end_tag("SCALES",2,true)); - } - return $status; - } - //Backup events info (course events) function backup_events_info($bf,$preferences) { global $CFG, $DB; @@ -2081,1484 +492,3 @@ } return $status; } - - //Backup groups info - function backup_groups_info($bf,$preferences) { - global $CFG, $DB; - - $status = true; - $status2 = true; - - //Get groups - $groups = $DB->get_records("groups", array("courseid"=>$preferences->backup_course)); - - //Pring groups header - if ($groups) { - //Pring groups header - fwrite ($bf,start_tag("GROUPS",2,true)); - //Iterate - foreach ($groups as $group) { - //Begin group tag - fwrite ($bf,start_tag("GROUP",3,true)); - //Output group contents - fwrite ($bf,full_tag("ID",4,false,$group->id)); - //fwrite ($bf,full_tag("COURSEID",4,false,$group->courseid)); - fwrite ($bf,full_tag("NAME",4,false,$group->name)); - fwrite ($bf,full_tag("DESCRIPTION",4,false,$group->description)); - fwrite ($bf,full_tag("ENROLMENTKEY",4,false,$group->enrolmentkey)); - fwrite ($bf,full_tag("PICTURE",4,false,$group->picture)); - fwrite ($bf,full_tag("HIDEPICTURE",4,false,$group->hidepicture)); - fwrite ($bf,full_tag("TIMECREATED",4,false,$group->timecreated)); - fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$group->timemodified)); - - //Now, backup groups_members, only if users are included - if ($preferences->backup_users != 2) { - $status2 = backup_groups_members_info($bf,$preferences,$group->id); - } - - //End group tag - fwrite ($bf,end_tag("GROUP",3,true)); - } - //End groups tag - $status = fwrite ($bf,end_tag("GROUPS",2,true)); - - //Now save group_files - if ($status && $status2) { - $status2 = backup_copy_group_files($preferences); - } - } - return ($status && $status2); - } - - //Backup groups_members info - function backup_groups_members_info($bf,$preferences,$groupid) { - global $CFG, $DB; - - $status = true; - - //Get groups_members that are being included in backup - $groups_members = $DB->get_records_sql("SELECT gm.* - FROM {groups_members} gm, {backup_ids} bi - WHERE gm.groupid = ? - AND bi.backup_code = ? - AND bi.table_name = 'user' - AND bi.old_id = gm.userid", array($groupid, $preferences->backup_unique_code)); - - //Pring groups_members header - if ($groups_members) { - //Pring groups_members header - fwrite ($bf,start_tag("MEMBERS",4,true)); - //Iterate - foreach ($groups_members as $group_member) { - //Begin group_member tag - fwrite ($bf,start_tag("MEMBER",5,true)); - //Output group_member contents - fwrite ($bf,full_tag("GROUPID",6,false,$group_member->groupid)); - fwrite ($bf,full_tag("USERID",6,false,$group_member->userid)); - fwrite ($bf,full_tag("TIMEADDED",6,false,$group_member->timeadded)); - //End group_member tag - fwrite ($bf,end_tag("MEMBER",5,true)); - } - //End groups_members tag - $status = fwrite ($bf,end_tag("MEMBERS",4,true)); - } - return $status; - } - - //Backup groupings info - function backup_groupings_info($bf,$preferences) { - global $CFG, $DB; - - $status = true; - - //Get groups - $groupings = $DB->get_records("groupings", array("courseid"=>$preferences->backup_course)); - - //Pring groups header - if ($groupings) { - //Pring groups header - fwrite ($bf,start_tag("GROUPINGS",2,true)); - //Iterate - foreach ($groupings as $grouping) { - //Begin group tag - fwrite ($bf,start_tag("GROUPING",3,true)); - //Output group contents - fwrite ($bf,full_tag("ID",4,false,$grouping->id)); - //fwrite ($bf,full_tag("COURSEID",4,false,$grouping->courseid)); - fwrite ($bf,full_tag("NAME",4,false,$grouping->name)); - fwrite ($bf,full_tag("DESCRIPTION",4,false,$grouping->description)); - fwrite ($bf,full_tag("CONFIGDATA",4,false,$grouping->configdata)); - fwrite ($bf,full_tag("TIMECREATED",4,false,$grouping->timecreated)); - fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$grouping->timemodified)); - - //End group tag - fwrite ($bf,end_tag("GROUPING",3,true)); - } - //End groups tag - $status = fwrite ($bf,end_tag("GROUPINGS",2,true)); - - //(Now save grouping_files) - } - return $status; - } - - //Backup groupings-groups info - function backup_groupings_groups_info($bf,$preferences) { - global $CFG, $DB; - - $status = true; - - //Get grouping_groups - $courseid = $preferences->backup_course; - $sql = "SELECT gg.* FROM {groupings} g, {groupings_groups} gg - WHERE g.courseid=? AND g.id=gg.groupingid"; - $grouping_groups = $DB->get_records_sql($sql, array($courseid)); - - //Pring grouping_groups header - if ($grouping_groups) { - //Pring grouping_groups header - fwrite ($bf,start_tag("GROUPINGSGROUPS",2,true)); - //Iterate - foreach ($grouping_groups as $members) { - //Begin grouping_group tag - fwrite ($bf,start_tag("GROUPINGGROUP",3,true)); - //Output group_member contents - fwrite ($bf,full_tag("ID",4,false,$members->id)); - fwrite ($bf,full_tag("GROUPINGID",4,false,$members->groupingid)); - fwrite ($bf,full_tag("GROUPID",4,false,$members->groupid)); - fwrite ($bf,full_tag("TIMEADDED",4,false,$members->timeadded)); - //End grouping_group tag - fwrite ($bf,end_tag("GROUPINGGROUP",3,true)); - } - //End grouping_groups tag - $status = fwrite ($bf,end_tag("GROUPINGSGROUPS",2,true)); - } - return $status; - } - - //Start the modules tag - function backup_modules_start ($bf,$preferences) { - - return fwrite ($bf,start_tag("MODULES",2,true)); - } - - //End the modules tag - function backup_modules_end ($bf,$preferences) { - - return fwrite ($bf,end_tag("MODULES",2,true)); - } - - //This function makes all the necesary calls to every mod - //to export itself and its files !!! - function backup_module($bf,$preferences,$module) { - global $CFG, $DB, $OUTPUT; - - $status = true; - $statusm = true; - - require_once($CFG->dirroot.'/mod/'.$module.'/backuplib.php'); - - if (isset($preferences->mods[$module]->instances) - && is_array($preferences->mods[$module]->instances)) { - $onemodbackup = $module.'_backup_one_mod'; - if (function_exists($onemodbackup)) { - foreach ($preferences->mods[$module]->instances as $instance => $object) { - if (!empty($object->backup)) { - $statusm = $onemodbackup($bf,$preferences,$instance); - if (!$statusm) { - $errorstr = 'backup of '.$module.'-'.$object->name.' failed.'; - add_to_backup_log(time(),$preferences->backup_course,$errorstr,'manual'); - if (!defined('BACKUP_SILENTLY')) { - echo $OUTPUT->notification($errorstr); - } - $status = false; - } - } - } - } else { - $status = false; - } - } else { // whole module. - //First, re-check if necessary functions exists - $modbackup = $module."_backup_mods"; - if (function_exists($modbackup)) { - //Call the function - $status = $modbackup($bf,$preferences); - } else { - //Something was wrong. Function should exist. - $status = false; - } - } - - return $status; - - } - - //This function encode things to make backup multi-site fully functional - //It does this conversions: - // - $CFG->wwwroot/file.php/courseid ------------------> $@FILEPHP@$ (slasharguments links) - // - $CFG->wwwroot/file.php?file=/courseid ------------> $@FILEPHP@$ (non-slasharguments links) - // - Every module/block/course_format xxxx_encode_content_links() is executed too - // - function backup_encode_absolute_links($content) { - global $CFG,$preferences, $DB; - - /// MDL-14072: Prevent NULLs, empties and numbers to be processed by the - /// heavy interlinking. Just a few cpu cycles saved. - if ($content === NULL) { - return NULL; - } else if ($content === '') { - return ''; - } else if (is_numeric($content)) { - return $content; - } - - //Use one static variable to cache all the require_once calls that, - //under PHP5 seems to increase load too much, and we are requiring - //them here thousands of times (one per content). MDL-8700. - //Once fixed by PHP, we'll delete this hack - - static $includedfiles; - if (!isset($includedfiles)) { - $includedfiles = array(); - } - - //Check if we support unicode modifiers in regular expressions. Cache it. - static $unicoderegexp; - if (!isset($unicoderegexp)) { - $unicoderegexp = @preg_match('/\pL/u', 'a'); // This will fail silenty, returning false, - } // if regexp libraries don't support unicode - - //Check if preferences is ok. If it isn't set, we are - //in a scheduled_backup to we are able to get a copy - //from CFG->backup_preferences - if (!isset($preferences)) { - $mypreferences = $CFG->backup_preferences; - } else { - //We are in manual backups so global preferences must exist!! - $mypreferences = $preferences; - } - //First, we check for every call to file.php inside the course - $search = array($CFG->wwwroot.'/file.php/'.$mypreferences->backup_course, - $CFG->wwwroot.'/file.php?file=/'.$mypreferences->backup_course, - $CFG->wwwroot.'/file.php?file=%2f'.$mypreferences->backup_course, - $CFG->wwwroot.'/file.php?file=%2F'.$mypreferences->backup_course); - - $replace = array('$@FILEPHP@$', '$@FILEPHP@$', '$@FILEPHP@$', '$@FILEPHP@$'); - - $result = str_replace($search,$replace,$content); - - // Now we look for any '$@FILEPHP@$' URLs, replacing: - // - slashes and %2F by $@SLASH@$ - // - &forcedownload=1 &forcedownload=1 and ?forcedownload=1 by $@FORCEDOWNLOAD@$ - // This way, backup contents will be neutral and independent of slasharguments configuration. MDL-18799 - // Based in $unicoderegexp, decide the regular expression to use - if ($unicoderegexp) { //We can use unicode modifiers - $search = '/(\$@FILEPHP@\$)((?:(?:\/|%2f|%2F))(?:(?:\([-;:@#&=\pL0-9\$~_.+!*\',]*?\))|[-;:@#&=\pL0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*)?(\?(?:(?:(?:\([-;:@#&=\pL0-9\$~_.+!*\',]*?\))|[-;:@#&=?\pL0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*))?(?mods as $name => $info) { - /// We only include the corresponding backuplib.php if it hasn't been included before - /// This will save some load under PHP5. MDL-8700. - /// Once fixed by PHP, we'll delete this hack - if (!in_array($name, $includedfiles)) { - include_once("$CFG->dirroot/mod/$name/backuplib.php"); - $includedfiles[] = $name; - } - //Check if the xxxx_encode_content_links exists - $function_name = $name."_encode_content_links"; - if (function_exists($function_name)) { - $result = $function_name($result,$mypreferences); - } - } - - // For the current course format call its encode_content_links method (if it exists) - static $format_function_name; - if (!isset($format_function_name)) { - $format_function_name = false; - if ($format = $DB->get_field('course','format', array('id'=>$mypreferences->backup_course))) { - if (file_exists("$CFG->dirroot/course/format/$format/backuplib.php")) { - include_once("$CFG->dirroot/course/format/$format/backuplib.php"); - $function_name = $format.'_encode_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) { - $result = $format_function_name($result, $mypreferences); - } - - // For each block, call its encode_content_links method. - // This encodes forexample links to blocks/something/viewphp?id=666 - // that are stored in other activities. - static $blockobjects = null; - if (!isset($blockobjects)) { - $blockobjects = array(); - if ($blocks = $DB->get_records('block', array('visible'=>1))) { - foreach ($blocks as $block) { - if ($blockobject = block_instance($block->name)) { - $blockobjects[] = $blockobject; - } - } - } - } - - foreach ($blockobjects as $blockobject) { - $result = $blockobject->encode_content_links($result,$mypreferences); - } - - if ($result != $content) { - debugging('

'.s($content).'
changed to
'.s($result).'

'); - } - - return $result; - } - - /** - * Callback preg_replace function used by backup_encode_absolute_links() - * to process $@FILEPHP@$ URLs to get slasharguments independent URLs - */ - function backup_process_filephp_uses($matches) { - - // Replace slashes (plain and encoded) and forcedownload=1 parameter - $search = array('/', '%2f', '%2F', '?forcedownload=1', '&forcedownload=1', '&forcedownload=1'); - $replace = array('$@SLASH@$', '$@SLASH@$', '$@SLASH@$', '$@FORCEDOWNLOAD@$', '$@FORCEDOWNLOAD@$', '$@FORCEDOWNLOAD@$'); - - $result = $matches[1] . (isset($matches[2]) ? str_replace($search, $replace, $matches[2]) : '') . (isset($matches[3]) ? str_replace($search, $replace, $matches[3]) : ''); - - return $result; - } - - //This function copies all the needed files under the "user" directory to the "user_files" - //directory under temp/backup - function backup_copy_user_files ($preferences) { - global $CFG, $DB; - - $status = true; - - //First we check that "user_files" exists and create it if necessary - //in temp/backup/$backup_code dir - $status = check_and_create_user_files_dir($preferences->backup_unique_code); - //now get a list of users that we need for this backup. - $backup_users = $DB->get_recordset("backup_ids", - array('backup_code'=>$preferences->backup_unique_code, 'table_name'=>'user'), "", "id, old_id"); - foreach ($backup_users as $user) { - //If this user's directory exists, copy it - //TODO: rewrite to use new file storage - /* - $userdir = make_user_directory($user->old_id, true); - if (check_dir_exists($userdir)) { - //first remove dirroot so we can split out the folders. - $parts = explode('/', str_replace($CFG->dataroot.'/user/', '', $userdir)); - $status = true; - if (is_array($parts)) { - $group = $parts[0]; - $userid = $parts[1]; - - // Create group dir first - $status = check_dir_exists("$CFG->dataroot/temp/backup/$preferences->backup_unique_code/user_files/". $group, true); - } - - $status = $status && backup_copy_file($userdir, - "$CFG->dataroot/temp/backup/$preferences->backup_unique_code/user_files/$group/$user->old_id"); - } - //Do some output - backup_flush(30);*/ - } - $backup_users->close(); - - return $status; - } - - //This function copies all the needed files under the "groups" directory to the "group_files" - //directory under temp/backup - function backup_copy_group_files ($preferences) { - global $CFG, $DB; - - $status = true; - - //First we check if "group_files" exists and create it as necessary - //in temp/backup/$backup_code dir - $status = check_and_create_group_files_dir($preferences->backup_unique_code); - - //Now iterate over directories under "groups" to check if that user must be - //copied to backup - - $rootdir = $CFG->dataroot.'/groups'; - //Check if directory exists - if (is_dir($rootdir)) { - $list = list_directories ($rootdir); - if ($list) { - //Iterate - foreach ($list as $dir) { - //Look for dir like group in groups table - $data = $DB->get_record ('groups', array('courseid'=>$preferences->backup_course, 'id'=>$dir)); - //If exists, copy it - if ($data) { - $status = backup_copy_file($rootdir."/".$dir, - $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/group_files/".$dir); - } - } - } - } - return $status; - } - - //This function copies all the course files under the course directory (except the moddata - //directory to the "course_files" directory under temp/backup - function backup_copy_course_files ($preferences) { - global $CFG, $DB; - - $status = true; - - //First we check to "course_files" exists and create it as necessary - //in temp/backup/$backup_code dir - $status = check_and_create_course_files_dir($preferences->backup_unique_code); - - //Now iterate over files and directories except $CFG->moddata and backupdata to be - //copied to backup - - $rootdir = $CFG->dataroot."/".$preferences->backup_course; - - $name_moddata = $CFG->moddata; - $name_backupdata = "backupdata"; - //Check if directory exists - if (is_dir($rootdir)) { - $list = list_directories_and_files ($rootdir); - if ($list) { - //Iterate - foreach ($list as $dir) { - if ($dir !== $name_moddata and $dir !== $name_backupdata) { - $status = backup_copy_file($rootdir."/".$dir, - $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/course_files/".$dir); - } - } - } - } - return $status; - } - - /** - * This function copies all the site files under the site directory (except the moddata and backupdata - * directories to the "site_files" directory under temp/backup - */ - function backup_copy_site_files ($preferences) { - global $CFG, $DB; - - $status = true; - - if ($preferences->backup_course == SITEID){ - return $status; - } - - //First we check to "site_files" exists and create it as necessary - //in temp/backup/$backup_code dir - $status = $status && check_and_create_site_files_dir($preferences->backup_unique_code); - - $rootdir = $CFG->dataroot."/".SITEID; - - - - $files = $DB->get_records('backup_files', array('backup_code'=>$preferences->backup_unique_code, 'file_type'=>'site')); - if ($files) { - //Iterate - foreach ($files as $fileobj) { - //check for dir structure and create recursively - $file = $fileobj->path; - $status = $status && check_dir_exists(dirname($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/site_files/".$file), true, true); - $status = $status && backup_copy_file($rootdir."/".$file, - $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/site_files/".$file); - } - } - return $status; - } - //This function creates the zip file containing all the backup info - //moodle.xml, moddata, user_files, course_files. - //The zipped file is created in the backup directory and named with - //the "oficial" name of the backup - //It uses "pclzip" if available or system "zip" (unix only) - function backup_zip ($preferences) { - global $CFG, $DB; - - $status = true; - - //Base dir where everything happens - $basedir = cleardoubleslashes($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code); - //Backup zip file name - $name = $preferences->backup_name; - //List of files and directories - $filelist = list_directories_and_files ($basedir); - - //Convert them to full paths - $files = array(); - foreach ($filelist as $file) { - $files[] = "$basedir/$file"; - } - - $status = zip_files($files, "$basedir/$name"); - - //echo "
Status: ".$status; //Debug - return $status; - - } - - //This function copies the final zip to the course dir - function copy_zip_to_course_dir ($preferences) { - global $CFG, $DB; - - $status = true; - - //Define zip location (from) - $from_zip_file = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/".$preferences->backup_name; - - //Initialise $to_zip_file - $to_zip_file=""; - - //If $preferences->backup_destination isn't empty, then copy to custom directory - if (!empty($preferences->backup_destination)) { - $to_zip_file = $preferences->backup_destination."/".$preferences->backup_name; - - //Copy zip file - if ($status) { - $status = backup_copy_file ($from_zip_file,$to_zip_file); - } - } else { - //Define zip destination (course dir) - $context = get_context_instance(CONTEXT_COURSE, $preferences->backup_course); - $fs = get_file_storage(); - $file_record = array('contextid'=>$context->id, 'component'=>'backup', 'filearea'=>'course', - 'itemid'=>0, 'filepath'=>'/', 'filename'=>$preferences->backup_name, - 'timecreated'=>time(), 'timemodified'=>time()); - $fs->create_file_from_pathname($file_record, $from_zip_file); - } - return $status; - } - - /** - * compatibility function - * with new granular backup - * we need to know - */ - function backup_userdata_selected($preferences,$modname,$modid) { - return ((empty($preferences->mods[$modname]->instances) - && !empty($preferences->mods[$modname]->userinfo)) - || (is_array($preferences->mods[$modname]->instances) - && array_key_exists($modid,$preferences->mods[$modname]->instances) - && !empty($preferences->mods[$modname]->instances[$modid]->userinfo))); - } - - - function backup_mod_selected($preferences,$modname,$modid) { - return ((empty($preferences->mods[$modname]->instances) - && !empty($preferences->mods[$modname]->backup)) - || (is_array($preferences->mods[$modname]->instances) - && array_key_exists($modid,$preferences->mods[$modname]->instances) - && !empty($preferences->mods[$modname]->instances[$modid]->backup))); - } - - /** - * Checks for the required files/functions to backup every mod - * And check if there is data about it - */ - function backup_fetch_prefs_from_request(&$preferences,&$count,$course) { - global $CFG,$SESSION, $DB; - - // check to see if it's in the session already - if (!empty($SESSION->backupprefs) && array_key_exists($course->id,$SESSION->backupprefs) && !empty($SESSION->backupprefs[$course->id])) { - $sprefs = $SESSION->backupprefs[$course->id]; - $preferences = $sprefs; - // refetch backup_name just in case. - $bn = optional_param('backup_name','',PARAM_FILE); - if (!empty($bn)) { - $preferences->backup_name = $bn; - } - $count = 1; - return true; - } - - if ($allmods = $DB->get_records("modules") ) { - foreach ($allmods as $mod) { - $modname = $mod->name; - $modfile = "$CFG->dirroot/mod/$modname/backuplib.php"; - $modbackup = $modname."_backup_mods"; - $modbackupone = $modname."_backup_one_mod"; - $modcheckbackup = $modname."_check_backup_mods"; - if (!file_exists($modfile)) { - continue; - } - include_once($modfile); - if (!function_exists($modbackup) || !function_exists($modcheckbackup)) { - continue; - } - $var = "exists_".$modname; - $preferences->$var = true; - $count++; - // check that there are instances and we can back them up individually - if (!$DB->count_records('course_modules', array('course'=>$course->id,'module'=>$mod->id)) || !function_exists($modbackupone)) { - continue; - } - $var = 'exists_one_'.$modname; - $preferences->$var = true; - $varname = $modname.'_instances'; - $preferences->$varname = get_all_instances_in_course($modname, $course, NULL, true); - foreach ($preferences->$varname as $instance) { - $preferences->mods[$modname]->instances[$instance->id]->name = $instance->name; - $var = 'backup_'.$modname.'_instance_'.$instance->id; - $$var = optional_param($var,0, PARAM_CLEAN); - $preferences->$var = $$var; - $preferences->mods[$modname]->instances[$instance->id]->backup = $$var; - $var = 'backup_user_info_'.$modname.'_instance_'.$instance->id; - $$var = optional_param($var,0, PARAM_CLEAN); - $preferences->$var = $$var; - $preferences->mods[$modname]->instances[$instance->id]->userinfo = $$var; - $var = 'backup_'.$modname.'_instances'; - $preferences->$var = 1; // we need this later to determine what to display in modcheckbackup. - } - - //Check data - //Check module info - $preferences->mods[$modname]->name = $modname; - - $var = "backup_".$modname; - $$var = optional_param( $var,0, PARAM_CLEAN); - $preferences->$var = $$var; - $preferences->mods[$modname]->backup = $$var; - - //Check include user info - $var = "backup_user_info_".$modname; - $$var = optional_param( $var,0, PARAM_CLEAN); - $preferences->$var = $$var; - $preferences->mods[$modname]->userinfo = $$var; - - } - } - - //Check other parameters - $preferences->backup_metacourse = optional_param('backup_metacourse',1,PARAM_INT); - $preferences->backup_users = optional_param('backup_users',1,PARAM_INT); - $preferences->backup_logs = optional_param('backup_logs',0,PARAM_INT); - $preferences->backup_user_files = optional_param('backup_user_files',1,PARAM_INT); - $preferences->backup_course_files = optional_param('backup_course_files',1,PARAM_INT); - $preferences->backup_gradebook_history = optional_param('backup_gradebook_history', 1, PARAM_INT); - $preferences->backup_site_files = optional_param('backup_site_files',1,PARAM_INT); - $preferences->backup_messages = optional_param('backup_messages',1,PARAM_INT); - $preferences->backup_blogs = optional_param('backup_blogs',1,PARAM_INT); - $preferences->backup_course = $course->id; - $preferences->backup_name = required_param('backup_name', PARAM_FILE); - $preferences->backup_unique_code = required_param('backup_unique_code', PARAM_INT); - - $roles = get_all_roles(); - $preferences->backuproleassignments = array(); - foreach ($roles as $role) { - if (optional_param('backupassignments_' . $role->shortname, 0, PARAM_INT)) { - $preferences->backuproleassignments[$role->id] = $role; - } - } - - // put it (back) in the session - $SESSION->backupprefs[$course->id] = $preferences; - } - - /** - * Finds all related roles used in course, mod and blocks context - * @param object $preferences - * @param object $course - * @return array of role objects - */ - function backup_fetch_roles($preferences) { - global $CFG, $DB; - - $contexts = array(); - $roles = array(); - - /// adding course context - $coursecontext = get_context_instance(CONTEXT_COURSE, $preferences->backup_course); - $contexts[$coursecontext->id] = $coursecontext; - - /// adding mod contexts - $mods = $preferences->mods; - foreach ($mods as $modname => $mod) { - $instances = $mod->instances; - foreach ($instances as $id => $instance) { - // if this type of mod is to be backed up - if ($instance->backup) { - $cm = get_coursemodule_from_instance($modname, $id); - $context = get_context_instance(CONTEXT_MODULE, $cm->id); - // put context in array keys - $contexts[$context->id] = $context; - } - } - } - - // add all roles assigned at user context - if ($preferences->backup_users) { - if ($users = $DB->get_records_sql("SELECT u.old_id, u.table_name,u.info - FROM {backup_ids} u - WHERE u.backup_code = ? AND - u.table_name = 'user'", array($preferences->backup_unique_code))) { - foreach ($users as $user) { - $context = get_context_instance(CONTEXT_USER, $user->old_id); - $contexts[$context->id] = $context; - } - } - - } - - // add all roles assigned at block context - if ($courseblocks = $DB->get_records_sql("SELECT * FROM {block_instances} WHERE contextid = ?", - array($coursecontext->id))) { - - foreach ($courseblocks as $courseblock) { - - $context = get_context_instance(CONTEXT_BLOCK, $courseblock->oldid); - $contexts[$context->id] = $context; - } - } - - // foreach context, call get_roles_on_exact_context + get_roles_with_override_on_context() and insert into array - foreach ($contexts as $context) { - if ($proles = get_roles_on_exact_context($context)) { // Look for roles assignments - foreach ($proles as $prole) { - $roles[$prole->id] = $prole; - } - } - if ($proles = get_roles_with_override_on_context($context)) { // Look for roles overrides - foreach ($proles as $prole) { - $roles[$prole->id] = $prole; - } - } - } - - return $roles; - } - - /** - * Writes out all of the data that should appear for every context. - */ - function write_per_context_data($bf, $preferences, $context, $startlevel) { - write_role_overrides_xml($bf, $context, $startlevel); - write_role_assignments_xml($bf, $preferences, $context, $startlevel); - write_local_filter_settings($bf, $preferences, $context, $startlevel); - } - - /** - * function to print xml for overrides - */ - function write_role_overrides_xml($bf, $context, $startlevel) { - fwrite ($bf, start_tag("ROLES_OVERRIDES", $startlevel, true)); - if ($roles = get_roles_with_override_on_context($context)) { - foreach ($roles as $role) { - fwrite ($bf, start_tag("ROLE", $startlevel+1, true)); - fwrite ($bf, full_tag("ID", $startlevel+2, false, $role->id)); - fwrite ($bf, full_tag("NAME", $startlevel+2, false, $role->name)); - fwrite ($bf, full_tag("SHORTNAME", $startlevel+2, false, $role->shortname)); - fwrite ($bf, start_tag("CAPABILITIES", $startlevel+2, true)); - if ($capabilities = get_capabilities_from_role_on_context($role, $context)) { - foreach ($capabilities as $capability) { - fwrite ($bf, start_tag("CAPABILITY", $startlevel+3, true)); - fwrite ($bf, full_tag("NAME", $startlevel+4, false, $capability->capability)); - fwrite ($bf, full_tag("PERMISSION", $startlevel+4, false, $capability->permission)); - fwrite ($bf, full_tag("TIMEMODIFIED", $startlevel+4, false, $capability->timemodified)); - if (!isset($capability->modifierid)) { - $capability->modifierid = 0; - } - fwrite ($bf, full_tag("MODIFIERID", $startlevel+4, false, $capability->modifierid)); - fwrite ($bf, end_tag("CAPABILITY", $startlevel+3, true)); - } - } - fwrite ($bf, end_tag("CAPABILITIES", $startlevel+2, true)); - fwrite ($bf, end_tag("ROLE", $startlevel+1, true)); - } - } - fwrite ($bf, end_tag("ROLES_OVERRIDES", $startlevel, true)); - } - - /** - * function to print xml for assignment - */ - function write_role_assignments_xml($bf, $preferences, $context, $startlevel) { - /// write role_assign code here - fwrite ($bf, start_tag("ROLES_ASSIGNMENTS", $startlevel, true)); - - if ($roles = get_roles_with_assignment_on_context($context)) { - foreach ($roles as $role) { - /// Skip non-selected roles - if (!isset($preferences->backuproleassignments[$role->id])) { - continue; - } - fwrite ($bf, start_tag("ROLE", $startlevel+1, true)); - fwrite ($bf, full_tag("ID", $startlevel+2, false, $role->id)); - fwrite ($bf, full_tag("NAME", $startlevel+2, false, $role->name)); - fwrite ($bf, full_tag("SHORTNAME", $startlevel+2, false, $role->shortname)); - fwrite ($bf, start_tag("ASSIGNMENTS", $startlevel+2, true)); - if ($assignments = get_users_from_role_on_context($role, $context)) { - foreach ($assignments as $assignment) { - /// Role assignments are only sent to backup if the user is one target user - if (backup_getid($preferences->backup_unique_code, 'user', $assignment->userid)) { - fwrite ($bf, start_tag("ASSIGNMENT", $startlevel+3, true)); - fwrite ($bf, full_tag("USERID", $startlevel+4, false, $assignment->userid)); - fwrite ($bf, full_tag("TIMESTART", $startlevel+4, false, $assignment->timestart)); - fwrite ($bf, full_tag("TIMEEND", $startlevel+4, false, $assignment->timeend)); - fwrite ($bf, full_tag("TIMEMODIFIED", $startlevel+4, false, $assignment->timemodified)); - if (!isset($assignment->modifierid)) { - $assignment->modifierid = 0; - } - fwrite ($bf, full_tag("MODIFIERID", $startlevel+4, false, $assignment->modifierid)); - fwrite ($bf, full_tag("ENROL", $startlevel+4, false, $assignment->enrol)); - fwrite ($bf, full_tag("SORTORDER", $startlevel+4, false, $assignment->sortorder)); - fwrite ($bf, end_tag("ASSIGNMENT", $startlevel+3, true)); - } - } - } - fwrite ($bf, end_tag("ASSIGNMENTS", $startlevel+2, true)); - fwrite ($bf, end_tag("ROLE", $startlevel+1, true)); - } - } - fwrite ($bf, end_tag("ROLES_ASSIGNMENTS", $startlevel, true)); - } - - /** - * Write any local filter settings for this context to the backup file. - * They comprise On/off filter_active.active overrides, and any filter_config - * records for this contextid. - */ - function write_local_filter_settings($bf, $preferences, $context, $startlevel) { - if (!filter_context_may_have_filter_settings($context)) { - return; - } - list($actives, $configs) = filter_get_all_local_settings($context->id); - - fwrite($bf, start_tag("FILTERACTIVES", $startlevel, true)); - foreach ($actives as $active) { - fwrite($bf, start_tag("FILTERACTIVE", $startlevel + 1, true)); - fwrite($bf, full_tag("FILTER", $startlevel + 2, false, $active->filter)); - fwrite($bf, full_tag("ACTIVE", $startlevel + 2, false, $active->active)); - fwrite($bf, end_tag("FILTERACTIVE", $startlevel + 1, true)); - } - fwrite($bf, end_tag("FILTERACTIVES", $startlevel, true)); - - fwrite($bf, start_tag("FILTERCONFIGS", $startlevel, true)); - foreach ($configs as $config) { - fwrite($bf, start_tag("FILTERCONFIG", $startlevel + 1, true)); - fwrite($bf, full_tag("FILTER", $startlevel + 2, false, $config->filter)); - fwrite($bf, full_tag("NAME", $startlevel + 2, false, $config->name)); - fwrite($bf, full_tag("VALUE", $startlevel + 2, false, $config->value)); - fwrite($bf, end_tag("FILTERCONFIG", $startlevel + 1, true)); - } - fwrite($bf, end_tag("FILTERCONFIGS", $startlevel, true)); - } - - function backup_execute(&$preferences, &$errorstr) { - global $CFG, $DB, $OUTPUT; - $status = true; - - //Check for temp and backup and backup_unique_code directory - //Create them as needed - if (!defined('BACKUP_SILENTLY')) { - echo "
  • ".get_string("creatingtemporarystructures").'
  • '; - } - - $status = check_and_create_backup_dir($preferences->backup_unique_code); - //Empty dir - if ($status) { - $status = clear_backup_dir($preferences->backup_unique_code); - } - - //Delete old_entries from backup tables - if (!defined('BACKUP_SILENTLY')) { - echo "
  • ".get_string("deletingolddata").'
  • '; - } - $status = backup_delete_old_data(); - if (!$status) { - $errorstr = "An error occurred deleting old backup data"; - add_to_backup_log(time(),$preferences->backup_course,$errorstr,'manual'); - if (!defined('BACKUP_SILENTLY')) { - echo $OUTPUT->notification($errorstr); - } - } - - //Create the moodle.xml file - if ($status) { - if (!defined('BACKUP_SILENTLY')) { - echo "
  • ".get_string("creatingxmlfile"); - //Begin a new list to xml contents - echo "
  • "; - } - } - - //Now, if selected, copy user files - if ($status) { - if ($preferences->backup_user_files) { - if (!defined('BACKUP_SILENTLY')) { - echo "
  • ".get_string("copyinguserfiles").'
  • '; - } - if (!$status = backup_copy_user_files ($preferences)) { - $errorstr = "An error occurred while copying user files"; - add_to_backup_log(time(),$preferences->backup_course,$errorstr,'manual'); - if (!defined('BACKUP_SILENTLY')) { - echo $OUTPUT->notification($errorstr); - } else { - return false; - } - } - } - } - - //Now, if selected, copy course files - if ($status) { - if ($preferences->backup_course_files) { - if (!defined('BACKUP_SILENTLY')) { - echo "
  • ".get_string("copyingcoursefiles").'
  • '; - } - if (!$status = backup_copy_course_files ($preferences)) { - $errorstr = "An error occurred while copying course files"; - add_to_backup_log(time(),$preferences->backup_course,$errorstr,'manual'); - if (!defined('BACKUP_SILENTLY')) { - echo $OUTPUT->notification($errorstr); - } else { - return false; - } - } - } - } - //Now, if selected, copy site files - if ($status) { - if ($preferences->backup_site_files) { - if (!defined('BACKUP_SILENTLY')) { - echo "
  • ".get_string("copyingsitefiles").'
  • '; - } - if (!$status = backup_copy_site_files ($preferences)) { - $errorstr = "An error occurred while copying site files"; - add_to_backup_log(time(),$preferences->backup_course,$errorstr,'manual'); - if (!defined('BACKUP_SILENTLY')) { - echo $OUTPUT->notification($errorstr); - } else { - return false; - } - } - } - } - //Now, zip all the backup directory contents - if ($status) { - if (!defined('BACKUP_SILENTLY')) { - echo "
  • ".get_string("zippingbackup").'
  • '; - } - if (!$status = backup_zip ($preferences)) { - $errorstr = "An error occurred while zipping the backup"; - add_to_backup_log(time(),$preferences->backup_course,$errorstr,'manual'); - if (!defined('BACKUP_SILENTLY')) { - echo $OUTPUT->notification($errorstr); - } else { - return false; - } - } - } - - //Now, copy the zip file to course directory - if ($status) { - if (!defined('BACKUP_SILENTLY')) { - echo "
  • ".get_string("copyingzipfile").'
  • '; - } - if (!$status = copy_zip_to_course_dir ($preferences)) { - $errorstr = "An error occurred while copying the zip file to the course directory"; - add_to_backup_log(time(),$preferences->backup_course,$errorstr,'manual'); - if (!defined('BACKUP_SILENTLY')) { - echo $OUTPUT->notification($errorstr); - } else { - return false; - } - } - } - - //Now, clean temporary data (db and filesystem) - if ($status) { - if (!defined('BACKUP_SILENTLY')) { - echo "
  • ".get_string("cleaningtempdata").'
  • '; - } - if (!$status = clean_temp_data ($preferences)) { - $errorstr = "An error occurred while cleaning up temporary data"; - add_to_backup_log(time(),$preferences->backup_course,$errorstr,'manual'); - if (!defined('BACKUP_SILENTLY')) { - echo $OUTPUT->notification($errorstr); - } else { - return false; - } - } - } - - return $status; - } - - /** - * This function calculates the "backup" part of the file name - * from lang files. Used both in manual and scheduled backups - * - * @param object $course course object - * @return string "backup" part of the filename - */ - function backup_get_backup_string($course) { - - /// Calculate the backup word - /// Take off some characters in the filename !! - $takeoff = array(" ", ":", "/", "\\", "|"); - $backup_word = str_replace($takeoff,"_",moodle_strtolower(get_string("backupfilename"))); - /// If non-translated, use "backup" - if (substr($backup_word,0,1) == "[") { - $backup_word= "backup"; - } - - return $backup_word; - } - - /** - * This function generates the default zipfile name for a backup - * based on the course shortname - * - * @param object $course course object - * @return string filename (excluding path information) - */ - function backup_get_zipfile_name($course) { - - //Calculate the backup word - $backup_word = backup_get_backup_string($course); - - //Calculate the date format string - $backup_date_format = str_replace(" ","_",get_string("backupnameformat", 'langconfig')); - //If non-translated, use "%Y%m%d-%H%M" - if (substr($backup_date_format,0,1) == "[") { - $backup_date_format = "%%Y%%m%%d-%%H%%M"; - } - - //Calculate the shortname - $backup_shortname = clean_filename($course->shortname); - if (empty($backup_shortname) or $backup_shortname == '_' ) { - $backup_shortname = $course->id; - } - - //Calculate the final backup filename - //The backup word - $backup_name = $backup_word."-"; - //The shortname - $backup_name .= moodle_strtolower($backup_shortname)."-"; - //The date format - $backup_name .= userdate(time(),$backup_date_format,99,false); - //The extension - $backup_name .= ".zip"; - //And finally, clean everything - $backup_name = clean_filename($backup_name); - - return $backup_name; - } - - /** - * This function generates the common file substring for a course - * used to keep n copies by the scheduled backup - * - * @param object $course course object - * @return string common part of filename in backups of this course - */ - function backup_get_keep_name($course) { - - //Calculate the backup word - $backup_word = backup_get_backup_string($course); - - //Calculate the shortname - $backup_shortname = clean_filename($course->shortname); - if (empty($backup_shortname) or $backup_shortname == '_' ) { - $backup_shortname = $course->id; - } - - $keep_name = $backup_word . "-" . moodle_strtolower($backup_shortname)."-"; - $keep_name = clean_filename($keep_name); - - return $keep_name; - } - /** - * This function adds on the standard items to the preferences - * Like moodle version and backup version - * - * @param object $preferences existing preferences object. - * (passed by reference) - */ - function backup_add_static_preferences(&$preferences) { - global $CFG; - $preferences->moodle_version = $CFG->version; - $preferences->moodle_release = $CFG->release; - $preferences->backup_version = $CFG->backup_version; - $preferences->backup_release = $CFG->backup_release; - } diff --git a/backup/lib.php b/backup/lib.php index 885c7caeb51..865430b6946 100644 --- a/backup/lib.php +++ b/backup/lib.php @@ -488,333 +488,6 @@ return ($status && $status2); } - /** this function will restore an entire backup.zip into the specified course - * using standard moodle backup/restore functions, but silently. - * @param string $pathtofile the absolute path to the backup file. - * @param int $destinationcourse the course id to restore to. - * @param boolean $emptyfirst whether to delete all coursedata first. - * @param boolean $userdata whether to include any userdata that may be in the backup file. - * @param array $preferences optional, 0 will be used. Can contain: - * metacourse - * logs - * course_files - * messages - */ - function import_backup_file_silently($pathtofile,$destinationcourse,$emptyfirst=false,$userdata=false, $preferences=array()) { - global $CFG,$SESSION,$USER, $DB; // is there such a thing on cron? I guess so.. - - if (!defined('RESTORE_SILENTLY')) { - define('RESTORE_SILENTLY',true); // don't output all the stuff to us. - } - - $debuginfo = 'import_backup_file_silently: '; - $cleanupafter = false; - $errorstr = ''; // passed by reference to restore_precheck to get errors from. - - $course = null; - if ($destinationcourse && !$course = $DB->get_record('course', array('id'=>$destinationcourse))) { - mtrace($debuginfo.'Course with id $destinationcourse was not a valid course!'); - return false; - } - - // first check we have a valid file. - if (!file_exists($pathtofile) || !is_readable($pathtofile)) { - mtrace($debuginfo.'File '.$pathtofile.' either didn\'t exist or wasn\'t readable'); - return false; - } - - // now make sure it's a zip file - require_once($CFG->dirroot.'/lib/filelib.php'); - $filename = substr($pathtofile,strrpos($pathtofile,'/')+1); - $mimetype = mimeinfo("type", $filename); - if ($mimetype != 'application/zip') { - mtrace($debuginfo.'File '.$pathtofile.' was of wrong mimetype ('.$mimetype.')' ); - return false; - } - - // restore_precheck wants this within dataroot, so lets put it there if it's not already.. - if (strstr($pathtofile,$CFG->dataroot) === false) { - // first try and actually move it.. - if (!check_dir_exists($CFG->dataroot.'/temp/backup/',true)) { - mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! '); - return false; - } - if (!copy($pathtofile,$CFG->dataroot.'/temp/backup/'.$filename)) { - mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! '); - return false; - } else { - $pathtofile = 'temp/backup/'.$filename; - $cleanupafter = true; - } - } else { - // it is within dataroot, so take it off the path for restore_precheck. - $pathtofile = substr($pathtofile,strlen($CFG->dataroot.'/')); - } - - if (!backup_required_functions()) { - mtrace($debuginfo.'Required function check failed (see backup_required_functions)'); - return false; - } - @ini_set("max_execution_time","3000"); - raise_memory_limit(MEMORY_EXTRA); - - if (!$backup_unique_code = restore_precheck($destinationcourse,$pathtofile,$errorstr,true)) { - mtrace($debuginfo.'Failed restore_precheck (error was '.$errorstr.')'); - return false; - } - - global $restore; // ick - $restore = new StdClass; - // copy back over the stuff that gets set in restore_precheck - $restore->course_header = $SESSION->course_header; - $restore->info = $SESSION->info; - - $xmlfile = "$CFG->dataroot/temp/backup/$backup_unique_code/moodle.xml"; - $info = restore_read_xml_roles($xmlfile); - $restore->rolesmapping = array(); - if (isset($info->roles) && is_array($info->roles)) { - foreach ($info->roles as $id => $info) { - if ($newroleid = $DB->get_field('role', 'id', array('shortname' => $info->shortname))) { - $restore->rolesmapping[$id] = $newroleid; - } - } - } - - // add on some extra stuff we need... - $restore->metacourse = (isset($preferences['restore_metacourse']) ? $preferences['restore_metacourse'] : 0); - $restore->course_id = $destinationcourse; - if ($destinationcourse) { - $restore->restoreto = RESTORETO_CURRENT_ADDING; - $restore->course_startdateoffset = $course->startdate - $restore->course_header->course_startdate; - } else { - $restore->restoreto = RESTORETO_NEW_COURSE; - $restore->restore_restorecatto = 0; // let this be handled by the headers - $restore->course_startdateoffset = 0; - - } - - $restore->users = $userdata; - $restore->user_files = $userdata; - $restore->deleting = $emptyfirst; - - $restore->groups = (isset($preferences['restore_groups']) ? $preferences['restore_groups'] : RESTORE_GROUPS_NONE); - $restore->logs = (isset($preferences['restore_logs']) ? $preferences['restore_logs'] : 0); - $restore->messages = (isset($preferences['restore_messages']) ? $preferences['restore_messages'] : 0); - $restore->blogs = (isset($preferences['restore_blogs']) ? $preferences['restore_blogs'] : 0); - $restore->course_files = (isset($preferences['restore_course_files']) ? $preferences['restore_course_files'] : 0); - $restore->site_files = (isset($preferences['restore_site_files']) ? $preferences['restore_site_files'] : 0); - - $restore->backup_version = $restore->info->backup_backup_version; - $restore->original_wwwroot = $restore->info->original_wwwroot; - - // now copy what we have over to the session - // this needs to happen before restore_setup_for_check - // which for some reason reads the session - $SESSION->restore =& $restore; - // rename the things that are called differently - $SESSION->restore->restore_course_files = $restore->course_files; - $SESSION->restore->restore_site_files = $restore->site_files; - $SESSION->restore->backup_version = $restore->info->backup_backup_version; - - restore_setup_for_check($restore, $backup_unique_code); - - // maybe we need users (defaults to 2 (none) in restore_setup_for_check) - // so set this again here - if (!empty($userdata)) { - $restore->users = 1; - } - - // we also need modules... - if ($allmods = $DB->get_records("modules")) { - foreach ($allmods as $mod) { - $modname = $mod->name; - //Now check that we have that module info in the backup file - if (isset($restore->info->mods[$modname]) && $restore->info->mods[$modname]->backup == "true") { - $restore->mods[$modname]->restore = true; - $restore->mods[$modname]->userinfo = $userdata; - } - else { - // avoid warnings - $restore->mods[$modname]->restore = false; - $restore->mods[$modname]->userinfo = false; - } - } - } - if (!$status = restore_execute($restore,$restore->info,$restore->course_header,$errorstr)) { - mtrace($debuginfo.'Failed restore_execute (error was '.$errorstr.')'); - return false; - } - // now get out the new courseid and return that - if ($restore->restoreto = RESTORETO_NEW_COURSE) { - if (!empty($SESSION->restore->course_id)) { - return $SESSION->restore->course_id; - } - return false; - } - return true; - } - - /** - * Function to backup an entire course silently and create a zipfile. - * - * @param int $courseid the id of the course - * @param array $prefs see {@link backup_generate_preferences_artificially} - */ - function backup_course_silently($courseid, $prefs, &$errorstring) { - global $CFG, $preferences, $DB; // global preferences here because something else wants it :( - if (!defined('BACKUP_SILENTLY')) { - define('BACKUP_SILENTLY', 1); - } - if (!$course = $DB->get_record('course', array('id'=>$courseid))) { - debugging("Couldn't find course with id $courseid in backup_course_silently"); - return false; - } - $preferences = backup_generate_preferences_artificially($course, $prefs); - $preferences->destination = array_key_exists('destination', $prefs) ? $prefs['destination'] : 0; - if (backup_execute($preferences, $errorstring)) { - return $CFG->dataroot . '/' . $course->id . '/backupdata/' . $preferences->backup_name; - } - else { - return false; - } - } - - /** - * Function to generate the $preferences variable that - * backup uses. This will back up all modules and instances in a course. - * - * @param object $course course object - * @param array $prefs can contain: - backup_metacourse - backup_users - backup_logs - backup_user_files - backup_course_files - backup_site_files - backup_messages - userdata - * and if not provided, they will not be included. - */ - - function backup_generate_preferences_artificially($course, $prefs) { - global $CFG, $DB; - $preferences = new StdClass; - $preferences->backup_unique_code = time(); - $preferences->backup_users = (isset($prefs['backup_users']) ? $prefs['backup_users'] : 0); - $preferences->backup_name = backup_get_zipfile_name($course, $preferences->backup_unique_code); - $preferences->mods = array(); - $count = 0; - - if ($allmods = $DB->get_records("modules") ) { - foreach ($allmods as $mod) { - $modname = $mod->name; - $modfile = "$CFG->dirroot/mod/$modname/backuplib.php"; - $modbackup = $modname."_backup_mods"; - $modbackupone = $modname."_backup_one_mod"; - $modcheckbackup = $modname."_check_backup_mods"; - if (!file_exists($modfile)) { - continue; - } - include_once($modfile); - if (!function_exists($modbackup) || !function_exists($modcheckbackup)) { - continue; - } - $var = "exists_".$modname; - $preferences->$var = true; - $count++; - // check that there are instances and we can back them up individually - if (!$DB->count_records('course_modules', array('course'=>$course->id), array('module'=>$mod->id)) || !function_exists($modbackupone)) { - continue; - } - $var = 'exists_one_'.$modname; - $preferences->$var = true; - $varname = $modname.'_instances'; - $preferences->$varname = get_all_instances_in_course($modname, $course, NULL, true); - foreach ($preferences->$varname as $instance) { - $preferences->mods[$modname]->instances[$instance->id]->name = $instance->name; - $var = 'backup_'.$modname.'_instance_'.$instance->id; - $preferences->$var = true; - $preferences->mods[$modname]->instances[$instance->id]->backup = true; - $var = 'backup_user_info_'.$modname.'_instance_'.$instance->id; - $preferences->$var = (!array_key_exists('userdata', $prefs) || $prefs['userdata']); - $preferences->mods[$modname]->instances[$instance->id]->userinfo = $preferences->$var; - $var = 'backup_'.$modname.'_instances'; - $preferences->$var = 1; // we need this later to determine what to display in modcheckbackup. - } - - //Check data - //Check module info - $preferences->mods[$modname]->name = $modname; - - $var = "backup_".$modname; - $preferences->$var = true; - $preferences->mods[$modname]->backup = true; - - //Check include user info - $var = "backup_user_info_".$modname; - $preferences->$var = (!array_key_exists('userdata', $prefs) || $prefs['userdata']); - $preferences->mods[$modname]->userinfo = $preferences->$var; - - //Call the check function to show more info - $modcheckbackup = $modname."_check_backup_mods"; - $var = $modname.'_instances'; - $instancestopass = array(); - if (!empty($preferences->$var) && is_array($preferences->$var) && count($preferences->$var)) { - $table->data = array(); - $countinstances = 0; - foreach ($preferences->$var as $instance) { - $var1 = 'backup_'.$modname.'_instance_'.$instance->id; - $var2 = 'backup_user_info_'.$modname.'_instance_'.$instance->id; - if (!empty($preferences->$var1)) { - $obj = new StdClass; - $obj->name = $instance->name; - $obj->userdata = $preferences->$var2; - $obj->id = $instance->id; - $instancestopass[$instance->id]= $obj; - $countinstances++; - - } - } - } - $modcheckbackup($course->id,$preferences->$var,$preferences->backup_unique_code,$instancestopass); - } - } - - //Check other parameters - $preferences->backup_metacourse = (isset($prefs['backup_metacourse']) ? $prefs['backup_metacourse'] : 0); - $preferences->backup_logs = (isset($prefs['backup_logs']) ? $prefs['backup_logs'] : 0); - $preferences->backup_user_files = (isset($prefs['backup_user_files']) ? $prefs['backup_user_files'] : 0); - $preferences->backup_course_files = (isset($prefs['backup_course_files']) ? $prefs['backup_course_files'] : 0); - $preferences->backup_site_files = (isset($prefs['backup_site_files']) ? $prefs['backup_site_files'] : 0); - $preferences->backup_messages = (isset($prefs['backup_messages']) ? $prefs['backup_messages'] : 0); - $preferences->backup_gradebook_history = (isset($prefs['backup_gradebook_history']) ? $prefs['backup_gradebook_history'] : 0); - $preferences->backup_blogs = (isset($prefs['backup_blogs']) ? $prefs['backup_blogs'] : 0); - $preferences->backup_course = $course->id; - - //Check users - user_check_backup($course->id,$preferences->backup_unique_code,$preferences->backup_users,$preferences->backup_messages, $preferences->backup_blogs); - - //Check logs - log_check_backup($course->id); - - //Check user files - user_files_check_backup($course->id,$preferences->backup_unique_code); - - //Check course files - course_files_check_backup($course->id,$preferences->backup_unique_code); - - //Check site files - site_files_check_backup($course->id,$preferences->backup_unique_code); - - //Role assignments - $roles = $DB->get_records('role', null, 'sortorder'); - foreach ($roles as $role) { - $preferences->backuproleassignments[$role->id] = $role; - } - - backup_add_static_preferences($preferences); - return $preferences; - } function add_to_backup_log($starttime,$courseid,$message, $backuptype) { global $DB; $log = new stdClass(); diff --git a/backup/restorelib.php b/backup/restorelib.php index afd7c82d540..1df204a4c32 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -414,34 +414,6 @@ return $status; } - //This function creates all the categories and questions - //from xml - function restore_create_questions($restore,$xml_file) { - global $CFG; - - $status = true; - //Check it exists - if (!file_exists($xml_file)) { - $status = false; - } - //Get info from xml - if ($status) { - //info will contain the old_id of every category - //in backup_ids->info will be the real info (serialized) - $info = restore_read_xml_questions($restore,$xml_file); - } - //Now, if we have anything in info, we have to restore that - //categories/questions - if ($info) { - if ($info !== true) { - $status = $status && restore_question_categories($info, $restore); - } - } else { - $status = false; - } - return $status; - } - //This function creates all the course events function restore_create_events($restore,$xml_file) { global $DB; @@ -960,460 +932,11 @@ return $status; } - /** - * @param string $errorstr passed by reference, if silent is true, - * errorstr will be populated and this function will return false rather than calling print_error() or notify() - * @param boolean $noredirect (optional) if this is passed, this function will not print continue, or - * redirect to the next step in the restore process, instead will return $backup_unique_code - */ - function restore_precheck($id,$file,&$errorstr,$noredirect=false) { - - global $CFG, $SESSION, $OUTPUT; - - //Prepend dataroot to variable to have the absolute path - $file = $CFG->dataroot."/".$file; - - if (!defined('RESTORE_SILENTLY')) { - //Start the main table - echo ""; - echo ""; - echo "
    "; - - //Start the mail ul - echo "
      "; - } - - //Check the file exists - if (!is_file($file)) { - if (!defined('RESTORE_SILENTLY')) { - print_error('nofile'); - } else { - $errorstr = "File not exists ($file)"; - return false; - } - } - - //Check the file name ends with .zip - if (!substr($file,-4) == ".zip") { - if (!defined('RESTORE_SILENTLY')) { - print_error('incorrectext'); - } else { - $errorstr = get_string('incorrectext', 'error'); - return false; - } - } - - //Now calculate the unique_code for this restore - $backup_unique_code = time(); - - //Now check and create the backup dir (if it doesn't exist) - if (!defined('RESTORE_SILENTLY')) { - echo "
    • ".get_string("creatingtemporarystructures").'
    • '; - } - $status = check_and_create_backup_dir($backup_unique_code); - //Empty dir - if ($status) { - $status = clear_backup_dir($backup_unique_code); - } - - //Now delete old data and directories under dataroot/temp/backup - if ($status) { - if (!defined('RESTORE_SILENTLY')) { - echo "
    • ".get_string("deletingolddata").'
    • '; - } - if (!backup_delete_old_data()) {; - $errorstr = "An error occurred deleting old data"; - add_to_backup_log(time(),$preferences->backup_course,$errorstr,'restoreprecheck'); - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification($errorstr); - } - } - } - - //Now copy he zip file to dataroot/temp/backup/backup_unique_code - if ($status) { - if (!defined('RESTORE_SILENTLY')) { - echo "
    • ".get_string("copyingzipfile").'
    • '; - } - if (! $status = backup_copy_file($file,$CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Error copying backup file. Invalid name or bad perms."); - } else { - $errorstr = "Error copying backup file. Invalid name or bad perms"; - return false; - } - } - } - - //Now unzip the file - if ($status) { - if (!defined('RESTORE_SILENTLY')) { - echo "
    • ".get_string("unzippingbackup").'
    • '; - } - if (! $status = restore_unzip ($CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Error unzipping backup file. Invalid zip file."); - } else { - $errorstr = "Error unzipping backup file. Invalid zip file."; - return false; - } - } - } - - // If experimental option is enabled (enableimsccimport) - // check for Common Cartridge packages and convert to Moodle format - if ($status && isset($CFG->enableimsccimport) && $CFG->enableimsccimport == 1) { - require_once($CFG->dirroot. '/backup/cc/restore_cc.php'); - if (!defined('RESTORE_SILENTLY')) { - echo "
    • ".get_string('checkingforimscc', 'imscc').'
    • '; - } - $status = cc_convert($CFG->dataroot. DIRECTORY_SEPARATOR .'temp'. DIRECTORY_SEPARATOR . 'backup'. DIRECTORY_SEPARATOR . $backup_unique_code); - } - - //Check for Blackboard backups and convert - if ($status){ - require_once("$CFG->dirroot/backup/bb/restore_bb.php"); - if (!defined('RESTORE_SILENTLY')) { - echo "
    • ".get_string("checkingforbbexport").'
    • '; - } - $status = blackboard_convert($CFG->dataroot."/temp/backup/".$backup_unique_code); - } - - //Now check for the moodle.xml file - if ($status) { - $xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml"; - if (!defined('RESTORE_SILENTLY')) { - echo "
    • ".get_string("checkingbackup").'
    • '; - } - if (! $status = restore_check_moodle_file ($xml_file)) { - if (!is_file($xml_file)) { - $errorstr = 'Error checking backup file. moodle.xml not found at root level of zip file.'; - } else { - $errorstr = 'Error checking backup file. moodle.xml is incorrect or corrupted.'; - } - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification($errorstr); - } else { - return false; - } - } - } - - $info = ""; - $course_header = ""; - - //Now read the info tag (all) - if ($status) { - if (!defined('RESTORE_SILENTLY')) { - echo "
    • ".get_string("readinginfofrombackup").'
    • '; - } - //Reading info from file - $info = restore_read_xml_info ($xml_file); - //Reading course_header from file - $course_header = restore_read_xml_course_header ($xml_file); - - if(!is_object($course_header)){ - // ensure we fail if there is no course header - $course_header = false; - } - } - - if (!defined('RESTORE_SILENTLY')) { - //End the main ul - echo "
    \n"; - - //End the main table - echo "
    "; - } - - //We compare Moodle's versions - if ($status && $CFG->version < $info->backup_moodle_version) { - $message = new stdClass(); - $message->serverversion = $CFG->version; - $message->serverrelease = $CFG->release; - $message->backupversion = $info->backup_moodle_version; - $message->backuprelease = $info->backup_moodle_release; - echo $OUTPUT->box(get_string('noticenewerbackup','',$message), "noticebox"); - - } - - //Now we print in other table, the backup and the course it contains info - if ($info and $course_header and $status) { - //First, the course info - if (!defined('RESTORE_SILENTLY')) { - $status = restore_print_course_header($course_header); - } - //Now, the backup info - if ($status) { - if (!defined('RESTORE_SILENTLY')) { - $status = restore_print_info($info); - } - } - } - - //Save course header and info into php session - if ($status) { - $SESSION->info = $info; - $SESSION->course_header = $course_header; - } - - //Finally, a little form to continue - //with some hidden fields - if ($status) { - if (!defined('RESTORE_SILENTLY')) { - echo "
    "; - $hidden["backup_unique_code"] = $backup_unique_code; - $hidden["launch"] = "form"; - $hidden["file"] = $file; - $hidden["id"] = $id; - echo $OUTPUT->single_button(new moodle_url("restore.php", $hidden), get_string("continue")); - echo "
    "; - } - else { - if (empty($noredirect)) { - echo $OUTPUT->continue_button(new moodle_url('/backup/restore.php', array('backup_unique_code'=>$backup_unique_code, 'launch'=>'form', 'file'=>$file, 'id'=>$id, 'sesskey'=>sesskey()))); - echo $OUTPUT->footer(); - die; - - } else { - return $backup_unique_code; - } - } - } - - if (!$status) { - if (!defined('RESTORE_SILENTLY')) { - print_error('error'); - } else { - $errorstr = "An error has occured"; // helpful! :P - return false; - } - } - return true; - } - function restore_execute(&$restore,$info,$course_header,&$errorstr) { global $CFG, $USER, $DB, $OUTPUT; $status = true; - - //If we've selected to restore into new course - //create it (course) - //Saving conversion id variables into backup_tables - if ($restore->restoreto == RESTORETO_NEW_COURSE) { - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '.get_string('creatingnewcourse') . '
  • '; - } - $oldidnumber = $course_header->course_idnumber; - if (!$status = restore_create_new_course($restore,$course_header)) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Error while creating the new empty course."); - } else { - $errorstr = "Error while creating the new empty course."; - return false; - } - } - - //Print course fullname and shortname and category - if ($status) { - if (!defined('RESTORE_SILENTLY')) { - echo ""; - //Put the destination course_id - } - $restore->course_id = $course_header->course_id; - } - - if ($status = restore_open_html($restore,$course_header)){ - if (!defined('RESTORE_SILENTLY')) { - echo "
  • Creating the Restorelog.html in the course backup folder
  • "; - } - } - - } else { - $course = $DB->get_record("course", array("id"=>$restore->course_id)); - if ($course) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("usingexistingcourse"); - echo "
  • "; - } - //If we have selected to restore deleting, we do it now. - if ($restore->deleting) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("deletingolddata").'
  • '; - } - $status = remove_course_contents($restore->course_id,false) and - delete_dir_contents($CFG->dataroot."/".$restore->course_id,"backupdata"); - if ($status) { - //Now , this situation is equivalent to the "restore to new course" one (we - //have a course record and nothing more), so define it as "to new course" - $restore->restoreto = RESTORETO_NEW_COURSE; - } else { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("An error occurred while deleting some of the course contents."); - } else { - $errrostr = "An error occurred while deleting some of the course contents."; - return false; - } - } - } - } else { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Error opening existing course."); - $status = false; - } else { - $errorstr = "Error opening existing course."; - return false; - } - } - } - - //Now create groupings as needed - if ($status and ($restore->groups == RESTORE_GROUPINGS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("creatinggroupings"); - } - if (!$status = restore_create_groupings($restore,$xml_file)) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Could not restore groupings!"); - } else { - $errorstr = "Could not restore groupings!"; - return false; - } - } - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '; - } - } - - //Now create groupingsgroups as needed - if ($status and $restore->groups == RESTORE_GROUPS_GROUPINGS) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("creatinggroupingsgroups"); - } - if (!$status = restore_create_groupings_groups($restore,$xml_file)) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Could not restore groups in groupings!"); - } else { - $errorstr = "Could not restore groups in groupings!"; - return false; - } - } - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '; - } - } - - - //Now create the course_sections and their associated course_modules - //we have to do this after groups and groupings are restored, because we need the new groupings id - if ($status) { - //Into new course - if ($restore->restoreto == RESTORETO_NEW_COURSE) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("creatingsections"); - } - if (!$status = restore_create_sections($restore,$xml_file)) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Error creating sections in the existing course."); - } else { - $errorstr = "Error creating sections in the existing course."; - return false; - } - } - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '; - } - //Into existing course - } else if ($restore->restoreto != RESTORETO_NEW_COURSE) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("checkingsections"); - } - if (!$status = restore_create_sections($restore,$xml_file)) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Error creating sections in the existing course."); - } else { - $errorstr = "Error creating sections in the existing course."; - return false; - } - } - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '; - } - //Error - } else { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Neither a new course or an existing one was specified."); - $status = false; - } else { - $errorstr = "Neither a new course or an existing one was specified."; - return false; - } - } - } - - //Now create categories and questions as needed - if ($status) { - include_once("$CFG->dirroot/question/restorelib.php"); - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("creatingcategoriesandquestions"); - echo "
  • "; - } - } - - - //Now create course files as needed - if ($status and ($restore->course_files)) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("copyingcoursefiles"); - } - if (!$status = restore_course_files($restore)) { - if (empty($status)) { - echo $OUTPUT->notification("Could not restore course files!"); - } else { - $errorstr = "Could not restore course files!"; - return false; - } - } - //If all is ok (and we have a counter) - if ($status and ($status !== true)) { - //Inform about user dirs created from backup - if (!defined('RESTORE_SILENTLY')) { - echo ""; - } - } - if (!defined('RESTORE_SILENTLY')) { - echo "
  • "; - } - } - //Now create events as needed if ($status) { if (!defined('RESTORE_SILENTLY')) { @@ -1432,48 +955,6 @@ } } - //Now create course modules as needed - if ($status) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("creatingcoursemodules"); - } - if (!$status = restore_create_modules($restore,$xml_file)) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Could not restore modules!"); - } else { - $errorstr = "Could not restore modules!"; - return false; - } - } - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '; - } - } - - //Bring back the course blocks -- do it AFTER the modules!!! - if ($status) { - //If we are deleting and bringing into a course or making a new course, same situation - if ($restore->restoreto == RESTORETO_CURRENT_DELETING || - $restore->restoreto == RESTORETO_EXISTING_DELETING || - $restore->restoreto == RESTORETO_NEW_COURSE) { - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '.get_string('creatingblocks'); - } - $course_header->blockinfo = !empty($course_header->blockinfo) ? $course_header->blockinfo : NULL; - if (!$status = restore_create_blocks($restore, $info->backup_block_format, $course_header->blockinfo, $xml_file)) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification('Error while creating the course blocks'); - } else { - $errorstr = "Error while creating the course blocks"; - return false; - } - } - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '; - } - } - } - if ($status) { //If we are deleting and bringing into a course or making a new course, same situation if ($restore->restoreto == RESTORETO_CURRENT_DELETING || @@ -1515,26 +996,6 @@ } } - //Now, if all is OK, adjust the instance field in course_modules !! - //this also calculates the final modinfo information so, after this, - //code needing it can be used (like role_assignments. MDL-13740) - if ($status) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("checkinginstances"); - } - if (!$status = restore_check_instances($restore)) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Could not adjust instances in course_modules!"); - } else { - $errorstr = "Could not adjust instances in course_modules!"; - return false; - } - } - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '; - } - } - //Now, if all is OK, adjust activity events if ($status) { if (!defined('RESTORE_SILENTLY')) { @@ -1552,178 +1013,3 @@ echo ''; } } - - //Now, if all is OK, adjust inter-activity links - if ($status) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("decodinginternallinks"); - } - if (!$status = restore_decode_content_links($restore)) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Could not decode content links!"); - } else { - $errorstr = "Could not decode content links!"; - return false; - } - } - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '; - } - } - - //Now create gradebook as needed -- AFTER modules and blocks!!! - if ($status) { - if ($restore->backup_version > 2007090500) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("creatinggradebook"); - } - if (!$status = restore_create_gradebook($restore,$xml_file)) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Could not restore gradebook!"); - } else { - $errorstr = "Could not restore gradebook!"; - return false; - } - } - - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '; - } - - } else { - // for moodle versions before 1.9, those grades need to be converted to use the new gradebook - // this code needs to execute *after* the course_modules are sorted out - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("migratinggrades"); - } - - /// force full refresh of grading data before migration == crete all items first - if (!$status = restore_migrate_old_gradebook($restore,$xml_file)) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Could not migrate gradebook!"); - } else { - $errorstr = "Could not migrade gradebook!"; - return false; - } - } - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '; - } - } - /// force full refresh of grading data after all items are created - grade_force_full_regrading($restore->course_id); - grade_grab_course_grades($restore->course_id); - } - - /******************************************************************************* - ************* Restore of Roles and Capabilities happens here ****************** - *******************************************************************************/ - // try to restore roles even when restore is going to fail - teachers might have - // at least some role assigned - this is not correct though - $status = restore_create_roles($restore, $xml_file) && $status; - $status = restore_roles_and_filter_settings($restore, $xml_file) && $status; - - //Now if all is OK, update: - // - course modinfo field - // - categories table - // - add user as teacher - if ($status) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("checkingcourse"); - } - //categories table - $course = $DB->get_record("course", array("id"=>$restore->course_id)); - fix_course_sortorder(); - // Check if the user has course update capability in the newly restored course - // there is no need to load his capabilities again, because restore_roles_and_filter_settings - // would have loaded it anyway, if there is any assignments. - // fix for MDL-6831 - $newcontext = get_context_instance(CONTEXT_COURSE, $restore->course_id); - if (!has_capability('moodle/course:manageactivities', $newcontext)) { - // fix for MDL-9065, use the new config setting if exists - if ($CFG->creatornewroleid) { - role_assign($CFG->creatornewroleid, $USER->id, 0, $newcontext->id); - } else { - if ($legacyteachers = get_archetype_roles('editingteacher')) { - if ($legacyteacher = array_shift($legacyteachers)) { - role_assign($legacyteacher->id, $USER->id, 0, $newcontext->id); - } - } else { - echo $OUTPUT->notification('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.'); - } - } - } - // Availability system, if used, needs to find IDs for grade items - $rs=$DB->get_recordset_sql(" -SELECT - cma.id,cma.gradeitemid -FROM - {course_modules) cm - INNER JOIN {course_modules_availability} cma on cm.id=cma.coursemoduleid -WHERE - cma.gradeitemid IS NOT NULL - AND cm.course=? -",array($restore->course_id)); - foreach($rs as $rec) { - $newgradeid=backup_getid($restore->backup_unique_code, - 'grade_items',$rec->gradeitemid); - if($newgradeid) { - $newdata=(object)array( - 'id'=>$rec->id, - 'gradeitemid'=>$newgradeid->new_id); - $DB->update_record('course_modules_availability',$newdata); - } else { - if (!defined('RESTORE_SILENTLY')) { - echo "

    Can't find new ID for grade item $data->gradeitemid, ignoring availability condition.

    "; - } - continue; - } - } - $rs->close(); - - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '; - } - } - - //Cleanup temps (files and db) - if ($status) { - if (!defined('RESTORE_SILENTLY')) { - echo "
  • ".get_string("cleaningtempdata"); - } - if (!$status = clean_temp_data ($restore)) { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Could not clean up temporary data from files and database"); - } else { - $errorstr = "Could not clean up temporary data from files and database"; - return false; - } - } - if (!defined('RESTORE_SILENTLY')) { - echo '
  • '; - } - } - - // this is not a critical check - the result can be ignored - if (restore_close_html($restore)){ - if (!defined('RESTORE_SILENTLY')) { - echo '
  • Closing the Restorelog.html file.
  • '; - } - } - else { - if (!defined('RESTORE_SILENTLY')) { - echo $OUTPUT->notification("Could not close the restorelog.html file"); - } - } - - if (!defined('RESTORE_SILENTLY')) { - //End the main ul - echo ""; - - //End the main table - echo ""; - echo ""; - } - - return $status; - }