From f2533aae092b6962a93070beff44c50b1bb1b9a2 Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Fri, 28 Oct 2011 14:55:01 -0500 Subject: Restore option to keep users when deleting current course. --- backup/controller/restore_controller.class.php | 12 ++++++- backup/restore.php | 2 +- backup/util/dbops/restore_dbops.class.php | 4 +- backup/util/ui/renderer.php | 47 +++++++++++++++++++++++- backup/util/ui/restore_ui.class.php | 4 ++- backup/util/ui/restore_ui_stage.class.php | 6 +++ lang/en/backup.php | 1 + lib/moodlelib.php | 12 ++++-- 8 files changed, 78 insertions(+), 10 deletions(-) diff --git a/backup/controller/restore_controller.class.php b/backup/controller/restore_controller.class.php index f523df9..4fa1778 100644 --- a/backup/controller/restore_controller.class.php +++ b/backup/controller/restore_controller.class.php @@ -44,6 +44,7 @@ class restore_controller extends backup implements loggable { protected $userid; // user id executing the restore protected $operation; // Type of operation (backup/restore) protected $target; // Restoring to new/existing/current_adding/_deleting + protected $unenrolusers; // Whether or not to unenrol users when deleting a current course protected $samesite; // Are we restoring to the same site where the backup was generated protected $status; // Current status of the controller (created, planned, configured...) @@ -68,13 +69,14 @@ class restore_controller extends backup implements loggable { * @param int $userid * @param int $target backup::TARGET_[ NEW_COURSE | CURRENT_ADDING | CURRENT_DELETING | EXISTING_ADDING | EXISTING_DELETING ] */ - public function __construct($tempdir, $courseid, $interactive, $mode, $userid, $target){ + public function __construct($tempdir, $courseid, $interactive, $mode, $userid, $target, $unenrolusers = 1) { $this->tempdir = $tempdir; $this->courseid = $courseid; $this->interactive = $interactive; $this->mode = $mode; $this->userid = $userid; $this->target = $target; + $this->unenrolusers = $unenrolusers; // Apply some defaults $this->type = ''; @@ -263,6 +265,14 @@ class restore_controller extends backup implements loggable { return $this->target; } + public function get_unenrolusers() { + return $this->unenrolusers; + } + + public function to_unenrolusers() { + return !empty($this->unenrolusers); + } + public function is_samesite() { return $this->samesite; } diff --git a/backup/restore.php b/backup/restore.php index ed72ab8..bd0471b 100644 --- a/backup/restore.php +++ b/backup/restore.php @@ -26,7 +26,7 @@ if ($stage & restore_ui::STAGE_CONFIRM + restore_ui::STAGE_DESTINATION) { $restore = restore_ui::engage_independent_stage($stage/2, $contextid); if ($restore->process()) { $rc = new restore_controller($restore->get_filepath(), $restore->get_course_id(), backup::INTERACTIVE_YES, - backup::MODE_GENERAL, $USER->id, $restore->get_target()); + backup::MODE_GENERAL, $USER->id, $restore->get_target(), $restore->get_unenrolusers()); } } if ($rc) { diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index 9f6f2f6..15b15d5 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -1356,8 +1356,8 @@ abstract class restore_dbops { * @param int $courseid * @return bool True for success */ - public static function delete_course_content($courseid) { - return remove_course_contents($courseid, false); + public static function delete_course_content($courseid, $unenrolusers = true) { + return remove_course_contents($courseid, false, $unenrolusers); } } diff --git a/backup/util/ui/renderer.php b/backup/util/ui/renderer.php index b96bad2..fcaf0d1 100644 --- a/backup/util/ui/renderer.php +++ b/backup/util/ui/renderer.php @@ -223,8 +223,10 @@ class core_backup_renderer extends plugin_renderer_base { $hasrestoreoption = false; + $categoryaccess = (!empty($categories) && ($categories->get_count() > 0 || $categories->get_search())); + $html = html_writer::start_tag('div', array('class'=>'backup-course-selector backup-restore')); - if ($wholecourse && !empty($categories) && ($categories->get_count() > 0 || $categories->get_search())) { + if ($wholecourse && $categoryaccess) { // New course $hasrestoreoption = true; $html .= $form; @@ -246,6 +248,49 @@ class core_backup_renderer extends plugin_renderer_base { $html .= $this->output->heading(get_string('restoretocurrentcourse', 'backup'), 2, array('class'=>'header')); $html .= $this->backup_detail_input(get_string('restoretocurrentcourseadding', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_ADDING, array('checked'=>'checked')); $html .= $this->backup_detail_input(get_string('restoretocurrentcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_CURRENT_DELETING); + if ($categoryaccess) { + $javascript = ' + function findInput(name, value) { + var inputs = document.getElementsByTagName("input"); + + for (var i=0; i <= inputs.length; i++) { + if (inputs[i].name == name && inputs[i].value == value) { + return inputs[i]; + } + } + return null; + } + + function withInput(name, value, fun) { + var input = findInput(name, value); + if (input) { + fun(input); + } + } + + function disableUnenrol(disable) { + var unenrolUsers = findInput("unenrol_users", 1); + + return function() { + unenrolUsers.disabled = disable; + }; + } + + function inputWillDisable(disable) { + return function(input) { + input.onchange = disableUnenrol(disable); + }; + } + + withInput("target", 1, inputWillDisable(true)); + withInput("target", 0, inputWillDisable(false)); + '; + $attrs = array('checked' => 'checked', 'disabled' => 'disabled'); + $html .= $this->backup_detail_input(get_string('restoreunenrolusers', 'backup'), 'checkbox', 'unenrol_users', 1, $attrs); + $html .= html_writer::start_tag('script', array('type' => 'text/javascript')); + $html .= $javascript; + $html .= html_writer::end_tag('script'); + } $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue')))); $html .= html_writer::end_tag('div'); $html .= html_writer::end_tag('form'); diff --git a/backup/util/ui/restore_ui.class.php b/backup/util/ui/restore_ui.class.php index 220cc23..d4a72eb 100644 --- a/backup/util/ui/restore_ui.class.php +++ b/backup/util/ui/restore_ui.class.php @@ -139,7 +139,9 @@ class restore_ui extends base_ui { throw new restore_ui_exception('restoreuifinalisedbeforeexecute'); } if ($this->controller->get_target() == backup::TARGET_CURRENT_DELETING || $this->controller->get_target() == backup::TARGET_EXISTING_DELETING) { - restore_dbops::delete_course_content($this->controller->get_courseid()); + $to_unenrol = $this->controller->to_unenrolusers(); + $courseid = $this->controller->get_courseid(); + restore_dbops::delete_course_content($courseid, $to_unenrol); } $this->controller->execute_plan(); $this->progress = self::PROGRESS_EXECUTED; diff --git a/backup/util/ui/restore_ui_stage.class.php b/backup/util/ui/restore_ui_stage.class.php index a50f259..7e0e525 100644 --- a/backup/util/ui/restore_ui_stage.class.php +++ b/backup/util/ui/restore_ui_stage.class.php @@ -221,6 +221,7 @@ class restore_ui_stage_destination extends restore_ui_independent_stage { protected $filepath = null; protected $courseid = null; protected $target = backup::TARGET_NEW_COURSE; + protected $unenrolusers = 1; protected $coursesearch = null; protected $categorysearch = null; public function __construct($contextid) { @@ -243,6 +244,8 @@ class restore_ui_stage_destination extends restore_ui_independent_stage { return false; } $this->target = optional_param('target', backup::TARGET_NEW_COURSE, PARAM_INT); + $default_unenrol = $this->target == backup::TARGET_NEW_COURSE ? 1 : 0; + $this->unenrolusers = optional_param('unenrol_users', $default_unenrol, PARAM_INT); $targetid = optional_param('targetid', null, PARAM_INT); if (!is_null($this->target) && !is_null($targetid) && confirm_sesskey()) { if ($this->target == backup::TARGET_NEW_COURSE) { @@ -312,6 +315,9 @@ class restore_ui_stage_destination extends restore_ui_independent_stage { public function get_target() { return $this->target; } + public function get_unenrolusers() { + return $this->unenrolusers; + } } /** * This stage is the settings stage. diff --git a/lang/en/backup.php b/lang/en/backup.php index 595c8a1..bf6e55d 100644 --- a/lang/en/backup.php +++ b/lang/en/backup.php @@ -191,6 +191,7 @@ $string['restoretonewcourse'] = 'Restore as a new course'; $string['restoringcourse'] = 'Course restoration in progress'; $string['restoringcourseshortname'] = 'restoring'; $string['restorerolemappings'] = 'Restore role mappings'; +$string['restoreunenrolusers'] = 'Unenrol users if deleting the contents'; $string['rootsettings'] = 'Backup settings'; $string['rootsettingusers'] = 'Include enrolled users'; $string['rootsettinganonymize'] = 'Anonymize user information'; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 8723805..b5ca2fc 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3986,7 +3986,7 @@ function delete_course($courseorid, $showfeedback = true) { * method returns false, some of the removals will probably have succeeded, and others * failed, but you have no way of knowing which. */ -function remove_course_contents($courseid, $showfeedback = true) { +function remove_course_contents($courseid, $showfeedback = true, $unenrolusers = true) { global $CFG, $DB, $OUTPUT; require_once($CFG->libdir.'/completionlib.php'); require_once($CFG->libdir.'/questionlib.php'); @@ -4005,8 +4005,10 @@ function remove_course_contents($courseid, $showfeedback = true) { $cc->clear_criteria(); // remove roles and enrolments - role_unassign_all(array('contextid'=>$context->id), true); - enrol_course_delete($course); + if ($unenrolusers) { + role_unassign_all(array('contextid'=>$context->id), true); + enrol_course_delete($course); + } // Clean up course formats (iterate through all formats in the even the course format was ever changed) $formats = get_plugin_list('format'); @@ -4131,7 +4133,9 @@ function remove_course_contents($courseid, $showfeedback = true) { // Delete all remaining stuff linked to context, // such as remaining roles, files, comments, etc. // Keep the context record for now. - delete_context(CONTEXT_COURSE, $course->id, false); + if ($unenrolusers) { + delete_context(CONTEXT_COURSE, $course->id, false); + } //trigger events $course->context = $context; // you can not access context in cron event later after course is deleted -- 1.7.1