Browse files

MDL-27790 Backup: temperory course will be deleted in case of excepti…

…on and cancellation of restore process
  • Loading branch information...
1 parent 735de1c commit 416128379fa0829262894af4c21aee3e8985d38b Rajesh Taneja committed Sep 22, 2011
Showing with 45 additions and 10 deletions.
  1. +6 −1 backup/restore.php
  2. +38 −8 backup/util/ui/restore_ui.class.php
  3. +1 −1 backup/util/ui/restore_ui_stage.class.php
View
7 backup/restore.php
@@ -42,7 +42,12 @@
$outcome = $restore->process();
if (!$restore->is_independent()) {
if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage()) {
- $restore->execute();
+ try {
+ $restore->execute();
+ } catch(Exception $e) {
+ $restore->cleanup();
+ throw new moodle_exception((string)$e);
+ }
} else {
$restore->save_controller();
}
View
46 backup/util/ui/restore_ui.class.php
@@ -146,6 +146,39 @@ public function execute() {
$this->stage = new restore_ui_stage_complete($this, $this->stage->get_params(), $this->controller->get_results());
return true;
}
+
+ /**
+ * Delete course which is created by restore process
+ */
+ public function cleanup() {
+ $courseid = $this->controller->get_courseid();
+ if ($this->is_temporary_course_created($courseid)) {
+ delete_course($courseid, false);
+ }
+ }
+
+ /**
+ * Checks if the course is not restored fully and current controller has created it.
+ * @param int $courseid id of the course which needs to be checked
+ * @return bool
+ */
+ protected function is_temporary_course_created($courseid) {
+ global $DB;
+ //Check if current controller instance has created new course.
+ if ($this->controller->get_target() == backup::TARGET_NEW_COURSE) {
+ $results = $DB->record_exists_sql("SELECT bc.itemid
+ FROM {backup_controllers} bc, {course} c
+ WHERE bc.operation = 'restore'
+ AND bc.type = 'course'
+ AND bc.itemid = c.id
+ AND bc.itemid = ?",
+ array($courseid)
+ );
+ return $results;
+ }
+ return false;
+ }
+
/**
* Returns true if enforce_dependencies changed any settings
* @return bool
@@ -191,15 +224,12 @@ public function enforce_changed_dependencies() {
/**
* Cancels the current restore and redirects the user back to the relevant place
*/
- public function cancel_restore() {
- global $PAGE;
- // Determine the approriate URL to redirect the user to
- if ($PAGE->context->contextlevel == CONTEXT_MODULE && $PAGE->cm !== null) {
- $relevanturl = new moodle_url('/mod/'.$PAGE->cm->modname.'/view.php', array('id'=>$PAGE->cm->id));
- } else {
- $relevanturl = new moodle_url('/course/view.php', array('id'=>$PAGE->course->id));
+ public function cancel_process() {
+ //Delete temporary restore course if exists.
+ if ($this->controller->get_target() == backup::TARGET_NEW_COURSE) {
+ $this->cleanup();
}
- redirect($relevanturl);
+ parent::cancel_process();
}
/**
* Gets an array of progress bar items that can be displayed through the restore renderer.
View
2 backup/util/ui/restore_ui_stage.class.php
@@ -333,7 +333,7 @@ public function process(base_moodleform $form=null) {
$form = $this->initialise_stage_form();
if ($form->is_cancelled()) {
- $this->ui->cancel_restore();
+ $this->ui->cancel_process();
}
$data = $form->get_data();

0 comments on commit 4161283

Please sign in to comment.