diff --git a/backup/moodle2/backup_final_task.class.php b/backup/moodle2/backup_final_task.class.php index ad5ac4aee17..2cd375416ef 100644 --- a/backup/moodle2/backup_final_task.class.php +++ b/backup/moodle2/backup_final_task.class.php @@ -157,6 +157,11 @@ class backup_final_task extends backup_task { $this->built = true; } + public function get_weight() { + // The final task takes ages, so give it 20 times the weight of a normal task. + return 20; + } + // Protected API starts here /** diff --git a/backup/util/plan/base_plan.class.php b/backup/util/plan/base_plan.class.php index 7b3cab16bc4..74790941b7b 100644 --- a/backup/util/plan/base_plan.class.php +++ b/backup/util/plan/base_plan.class.php @@ -158,10 +158,23 @@ abstract class base_plan implements checksumable, executable { if (!$this->built) { throw new base_plan_exception('base_plan_not_built'); } + + // Calculate the total weight of all tasks and start progress tracking. + $progress = $this->get_progress(); + $totalweight = 0; + foreach ($this->tasks as $task) { + $totalweight += $task->get_weight(); + } + $progress->start_progress($this->get_name(), $totalweight); + + // Build and execute all tasks. foreach ($this->tasks as $task) { $task->build(); $task->execute(); } + + // Finish progress tracking. + $progress->end_progress(); } /** diff --git a/backup/util/plan/base_task.class.php b/backup/util/plan/base_task.class.php index 523679c7a20..e167b89dd6c 100644 --- a/backup/util/plan/base_task.class.php +++ b/backup/util/plan/base_task.class.php @@ -67,6 +67,17 @@ abstract class base_task implements checksumable, executable, loggable { return $this->settings; } + /** + * Returns the weight of this task, an approximation of the amount of time + * it will take. By default this value is 1. It can be increased for longer + * tasks. + * + * @return int Weight + */ + public function get_weight() { + return 1; + } + public function get_setting($name) { // First look in task settings $result = null; @@ -159,6 +170,13 @@ abstract class base_task implements checksumable, executable, loggable { if ($this->executed) { throw new base_task_exception('base_task_already_executed', $this->name); } + + // Starts progress based on the weight of this task and number of steps. + $progress = $this->get_progress(); + $progress->start_progress($this->get_name(), count($this->steps), $this->get_weight()); + $done = 0; + + // Execute all steps. foreach ($this->steps as $step) { $result = $step->execute(); // If step returns array, it will be forwarded to plan @@ -166,11 +184,16 @@ abstract class base_task implements checksumable, executable, loggable { if (is_array($result) and !empty($result)) { $this->add_result($result); } + $done++; + $progress->progress($done); } // Mark as executed if any step has been executed if (!empty($this->steps)) { $this->executed = true; } + + // Finish progress for this task. + $progress->end_progress(); } /**