public function restoreMergedBackup($dataItemId, $files, $session)
 {
     global $CFG;
     $page = new stdClass();
     //Move the merged backup file from the session folder to the course folder
     // Move the backups from the course directory into synch/sessions/sessionid/backups with the serverid appended the file name
     // Generate a new file name from the original backup file and the local server id
     //$newFileName = $this->createSessionBackupFileName($preferences);
     $sessionBackupFileName = $files['merged'];
     // Generate the path to the session backup folder
     $sessionBackupPath = $this->createSessionBackupPath($session);
     // Does the merge file exist
     if (!FileSystem::exists($sessionBackupPath . '/' . $sessionBackupFileName, 'f')) {
         return false;
     }
     // Move the session merge file to the course backup folder.
     if (1) {
         // fudge for testing in development
         $courseBackupFileName = FileSystem::removeFromFileName($sessionBackupFileName, $CFG->synch->merge_file_suffix);
         $courseBackupFileName = FileSystem::removeFromFileName($courseBackupFileName, synch_backup_controller::getUniqueCodeFromFileName($sessionBackupFileName));
         // Do we need to update the file name within the preferences. We will be passing
         // the file name and from then on it shouldn't be required.
         $courseId = SynchContentHierarchy::getIdFromDataItemId($dataItemId);
         $courseBackupPath = synch_Backup_controller::createBackupFilePath($courseId);
         // Create the folder structure if necessary
         FileSystem::createFoldersFromPath($courseBackupPath);
         $moved = FileSystem::copyFile($sessionBackupPath . '/' . $sessionBackupFileName, $courseBackupPath . '/' . $courseBackupFileName);
     } else {
         $moved = true;
     }
     if (!$moved) {
         return false;
     }
     $preferences = new Object();
     $preferences->course_id = $courseId;
     $preferences->backup_name = $courseBackupFileName;
     $preferences->backup_unique_code = synch_backup_controller::getUniqueCodeFromFileName($sessionBackupFileName);
     global $SynchServerController;
     // If there are changes from the remote Moodle restore to the local Moodle
     $remoteServerId = $SynchServerController->getRemoteServerId();
     if ($this->getSessionHasChangesByServerId($remoteServerId, $session)) {
         // Call the restoreCourse method of SynchBackupController
         $restored = synch_backup_controller::restoreCourse($preferences, $session);
     }
     return $restored;
 }
 public static function restoreCourse($preferences, $session)
 {
     global $Out;
     // the initial set of preferences should be enough to get the restore started.
     // once in progress the restore will obtain the preferences from the backup file
     // itself
     if (!isset($preferences)) {
         return null;
     }
     // Assumes the backup file is in the course data directory and the
     // preferences are in the backup file itself.
     global $CFG;
     // for large files uncomment following code
     //@ini_set("max_execution_time","3000");
     //raise_memory_limit("192M");
     $file = self::createBackupFilePath($preferences->course_id);
     $file .= '/' . $preferences->backup_name;
     //path to file
     //Check the file exists
     if (!is_file($file)) {
         return false;
     }
     //Check the file name ends with .zip
     if (!substr($file, -4) == ".zip") {
         return false;
     }
     //Now calculate the unique_code for this restore
     $backup_unique_code = $preferences->backup_unique_code;
     //Now check and create the backup dir (if it doesn't exist)
     $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) {
         $status = backup_delete_old_data();
     }
     $tempBackupPath = synch_backup_controller::createTempBackupPath($backup_unique_code);
     //Now copy the zip file to dataroot/temp/backup/backup_unique_code
     if ($status) {
         if (!($status = backup_copy_file($file, $tempBackupPath . "/" . basename($file)))) {
             // There has been a problem. Invalid name or bad perms
             return false;
         }
     }
     //Now unzip the file
     if ($status) {
         if (!($status = restore_unzip($tempBackupPath . "/" . basename($file)))) {
             // error: Invalid zip file
             return false;
         }
     }
     //Check for Blackboard backups and convert
     if ($status) {
         require_once "{$CFG->dirroot}/backup/bb/restore_bb.php";
         $status = blackboard_convert($tempBackupPath);
     }
     // backup file has now been unpacked. Retrieve the serialized preferences
     $preferencesPath = $tempBackupPath . '/' . self::getPreferencesFileName();
     $preferences = FileSystem::unSerializeFromFile($preferencesPath);
     // Now we have the preferences from the backup we need to tailor it to our current needs
     // should we be updating an existing item or creating one.
     $dataItemId = SynchContentHierarchy::generateDataItemId($preferences->course_id, synch_view_controller::$TYPE_ID_COURSE);
     global $SynchManager, $SynchServerController;
     $itemExists = $SynchManager->getSessionItemExistsByServerId($SynchServerController->getServerId(), $session);
     if (isset($itemExists) && is_array($itemExists) && in_array($dataItemId, $itemExists)) {
         $preferences->restoreto = 1;
         $preferences->deleting = 1;
     } else {
         $preferences->restoreto = 2;
     }
     //Now check for the moodle.xml file
     if ($status) {
         $xml_file = $tempBackupPath . "/moodle.xml";
         if (!($status = restore_check_moodle_file($xml_file))) {
             if (!is_file($xml_file)) {
                 //Error checking backup file. moodle.xml not found at root level of zip file
                 return false;
             } else {
                 //Error checking backup file. moodle.xml is incorrect or corrupted.
                 return false;
             }
         }
     }
     //unpack backup file
     //read contents
     //Reading info from file
     $info = restore_read_xml_info($xml_file);
     //Reading course_header from file
     $courseHeader = restore_read_xml_course_header($xml_file);
     //Save course header and info into php session
     if ($status) {
         //$SESSION->info = $info;
         //$SESSION->course_header = $course_header;
     }
     global $restore;
     $restore = $preferences;
     $message = null;
     $restoreSuccess = restore_execute($preferences, $info, $courseHeader, $message);
     return $restoreSuccess;
 }