Created
June 21, 2014 07:07
-
-
Save dangtrinhnt/273c224e7108c9758bdf to your computer and use it in GitHub Desktop.
Mass import template course into all other courses of Moodle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/php | |
<?php | |
define('CLI_SCRIPT', true); | |
// Require both the backup and restore libs | |
require_once('../config.php'); | |
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); | |
require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php'); | |
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); | |
require_once($CFG->dirroot . '/backup/util/ui/import_extensions.php'); | |
require_once($CFG->dirroot . '/course/lib.php'); | |
if(isset($argv[1])){ | |
// The id of the course we are importing FROM (will only be set if past first stage | |
$importcourseid = $argv[1]; // optional_param => 'importid' | |
} else { | |
die("\nNo course to import from!\n"); | |
} | |
// The target method for the restore (adding or deleting) | |
$restoretarget = 1; //optional_param => 'target' | |
// Jun 20, 2014 - Trinh Nguyen | |
// get admin account | |
$admin = get_admin(); | |
if (!$admin) { | |
mtrace("Error: No admin account was found"); | |
die(); | |
} | |
// in my case, I only want the blocks and activities to be copied | |
$settings = array( | |
'activities' => 'backup_auto_activities', | |
'blocks' => 'backup_auto_blocks', | |
//~ 'users' => 'backup_auto_users', | |
//~ 'role_assignments' => 'backup_auto_role_assignments', | |
//~ 'filters' => 'backup_auto_filters', | |
//~ 'comments' => 'backup_auto_comments', | |
//~ 'completion_information' => 'backup_auto_userscompletion', | |
//~ 'logs' => 'backup_auto_logs', | |
//~ 'histories' => 'backup_auto_histories' | |
); | |
$config = get_config('backup'); | |
//~ print_r($config); | |
function backup_template() { | |
$bc = new backup_controller(backup::TYPE_1COURSE, $GLOBALS['importcourseid'], backup::FORMAT_MOODLE, | |
backup::INTERACTIVE_YES, backup::MODE_IMPORT, $GLOBALS['admin']->id); | |
$backupid = $bc->get_backupid(); | |
$bc->get_plan()->get_setting('users')->set_status(backup_setting::LOCKED_BY_CONFIG); | |
foreach ($GLOBALS['settings'] as $setting => $configsetting) { | |
if ($bc->get_plan()->setting_exists($setting)) { | |
$bc->get_plan()->get_setting($setting)->set_value($GLOBALS['config']->{$configsetting}); | |
} | |
} | |
// backing up | |
$bc->finish_ui(); | |
$bc->execute_plan(); | |
$bc->destroy(); | |
unset($bc); | |
return $backupid; | |
} | |
// clone the backup to the course | |
function restore_to_course($courseid, $backupid) { | |
// Check whether the backup directory still exists. If missing, something | |
// went really wrong in backup, throw error. Note that backup::MODE_IMPORT | |
// backups don't store resulting files ever | |
$tempdestination = $GLOBALS['CFG']->tempdir . '/backup/' . $backupid; | |
if (!file_exists($tempdestination) || !is_dir($tempdestination)) { | |
print_error('unknownbackupexporterror'); // shouldn't happen ever | |
} | |
$rc = new restore_controller($backupid, $courseid, backup::INTERACTIVE_YES, | |
backup::MODE_IMPORT, $GLOBALS['admin']->id, $GLOBALS['restoretarget']); | |
// Convert the backup if required.... it should NEVER happed | |
if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) { | |
$rc->convert(); | |
} | |
// Mark the UI finished. | |
$rc->finish_ui(); | |
// Execute prechecks | |
$rc->execute_precheck(); | |
if ($GLOBALS['restoretarget'] == backup::TARGET_CURRENT_DELETING || $GLOBALS['restoretarget'] == backup::TARGET_EXISTING_DELETING) { | |
restore_dbops::delete_course_content($courseid); | |
} | |
// Execute the restore. | |
$rc->execute_plan(); | |
$rc->destroy(); | |
unset($rc); | |
// Delete the temp directory now | |
fulldelete($tempdestination); | |
} | |
// restoring backup into course(s) | |
if (isset($argv[2])) { | |
echo "\n######### Applying blocks to course {$argv[2]}... ##########\n\n"; | |
$courseid = $argv[2]; | |
// I ran the template backup for every course to avoid losing the | |
// backup data in the next execution | |
$backupid = backup_template(); | |
restore_to_course($courseid, $backupid); | |
} else { | |
echo "\n######### Applying blocks to all courses... ##########\n\n"; | |
$courses = get_courses(); | |
if(count($courses) > 1) { | |
foreach ($courses as &$course) { | |
$courseid = $course->id; | |
if( ($courseid != $importcourseid) && ($courseid != 1) ) { | |
print_r("\n+ Restoring course {$courseid}\n\n"); | |
$backupid = backup_template(); | |
restore_to_course($courseid, $backupid); | |
} | |
} | |
} | |
} | |
exit(0); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment