Skip to content

Commit 05e521c

Browse files
author
Frederic Massart
committed
MDL-28346 Backup: Added new status 'warning' for backup result
1 parent 569f1ad commit 05e521c

File tree

4 files changed

+55
-15
lines changed

4 files changed

+55
-15
lines changed

backup/util/helper/backup_cron_helper.class.php

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ abstract class backup_cron_automated_helper {
4646
const BACKUP_STATUS_UNFINISHED = 2;
4747
/** Course automated backup was skipped */
4848
const BACKUP_STATUS_SKIPPED = 3;
49+
/** Course automated backup had warnings */
50+
const BACKUP_STATUS_WARNING = 4;
4951

5052
/** Run if required by the schedule set in config. Default. **/
5153
const RUN_ON_SCHEDULE = 0;
@@ -139,7 +141,7 @@ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDUL
139141
$params = array('courseid' => $course->id, 'time' => $now-31*24*60*60, 'action' => '%view%');
140142
$logexists = $DB->record_exists_select('log', $sqlwhere, $params);
141143
if (!$logexists) {
142-
$backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED;
144+
$backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED;
143145
$backupcourse->nextstarttime = $nextstarttime;
144146
$DB->update_record('backup_courses', $backupcourse);
145147
mtrace('Skipping unchanged course '.$course->fullname);
@@ -160,7 +162,7 @@ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDUL
160162
$starttime = time();
161163

162164
$backupcourse->laststarttime = time();
163-
$backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED;
165+
$backupcourse->laststatus = self::BACKUP_STATUS_UNFINISHED;
164166
$DB->update_record('backup_courses', $backupcourse);
165167

166168
$backupcourse->laststatus = backup_cron_automated_helper::launch_automated_backup($course, $backupcourse->laststarttime, $admin->id);
@@ -169,7 +171,7 @@ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDUL
169171

170172
$DB->update_record('backup_courses', $backupcourse);
171173

172-
if ($backupcourse->laststatus) {
174+
if ($backupcourse->laststatus === self::BACKUP_STATUS_OK) {
173175
// Clean up any excess course backups now that we have
174176
// taken a successful backup.
175177
$removedcount = backup_cron_automated_helper::remove_excess_backups($course);
@@ -188,17 +190,18 @@ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDUL
188190
$message = "";
189191

190192
$count = backup_cron_automated_helper::get_backup_status_array();
191-
$haserrors = ($count[backup_cron_automated_helper::BACKUP_STATUS_ERROR] != 0 || $count[backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED] != 0);
193+
$haserrors = ($count[self::BACKUP_STATUS_ERROR] != 0 || $count[self::BACKUP_STATUS_UNFINISHED] != 0);
192194

193195
//Build the message text
194196
//Summary
195197
$message .= get_string('summary')."\n";
196198
$message .= "==================================================\n";
197199
$message .= " ".get_string('courses').": ".array_sum($count)."\n";
198-
$message .= " ".get_string('ok').": ".$count[backup_cron_automated_helper::BACKUP_STATUS_OK]."\n";
199-
$message .= " ".get_string('skipped').": ".$count[backup_cron_automated_helper::BACKUP_STATUS_SKIPPED]."\n";
200-
$message .= " ".get_string('error').": ".$count[backup_cron_automated_helper::BACKUP_STATUS_ERROR]."\n";
201-
$message .= " ".get_string('unfinished').": ".$count[backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED]."\n\n";
200+
$message .= " ".get_string('ok').": ".$count[self::BACKUP_STATUS_OK]."\n";
201+
$message .= " ".get_string('skipped').": ".$count[self::BACKUP_STATUS_SKIPPED]."\n";
202+
$message .= " ".get_string('error').": ".$count[self::BACKUP_STATUS_ERROR]."\n";
203+
$message .= " ".get_string('unfinished').": ".$count[self::BACKUP_STATUS_UNFINISHED]."\n";
204+
$message .= " ".get_string('warning').": ".$count[self::BACKUP_STATUS_WARNING]."\n\n";
202205

203206
//Reference
204207
if ($haserrors) {
@@ -261,6 +264,7 @@ public static function get_backup_status_array() {
261264
self::BACKUP_STATUS_OK => 0,
262265
self::BACKUP_STATUS_UNFINISHED => 0,
263266
self::BACKUP_STATUS_SKIPPED => 0,
267+
self::BACKUP_STATUS_WARNING => 0
264268
);
265269

266270
$statuses = $DB->get_records_sql('SELECT DISTINCT bc.laststatus, COUNT(bc.courseid) AS statuscount FROM {backup_courses} bc GROUP BY bc.laststatus');
@@ -334,7 +338,7 @@ public static function calculate_next_automated_backup($timezone, $now) {
334338
*/
335339
public static function launch_automated_backup($course, $starttime, $userid) {
336340

337-
$outcome = true;
341+
$outcome = self::BACKUP_STATUS_OK;
338342
$config = get_config('backup');
339343
$bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_AUTOMATED, $userid);
340344

@@ -369,6 +373,7 @@ public static function launch_automated_backup($course, $starttime, $userid) {
369373

370374
$bc->execute_plan();
371375
$results = $bc->get_results();
376+
$outcome = self::outcome_from_results($results);
372377
$file = $results['backup_destination']; // may be empty if file already moved to target location
373378
$dir = $config->backup_auto_destination;
374379
$storage = (int)$config->backup_auto_storage;
@@ -377,8 +382,10 @@ public static function launch_automated_backup($course, $starttime, $userid) {
377382
}
378383
if ($file && !empty($dir) && $storage !== 0) {
379384
$filename = backup_plan_dbops::get_default_backup_filename($format, $type, $course->id, $users, $anonymised, !$config->backup_shortname);
380-
$outcome = $file->copy_content_to($dir.'/'.$filename);
381-
if ($outcome && $storage === 1) {
385+
if (!$file->copy_content_to($dir.'/'.$filename)) {
386+
$outcome = self::BACKUP_STATUS_ERROR;
387+
}
388+
if ($outcome != self::BACKUP_STATUS_ERROR && $storage === 1) {
382389
$file->delete();
383390
}
384391
}
@@ -387,7 +394,7 @@ public static function launch_automated_backup($course, $starttime, $userid) {
387394
$bc->log('backup_auto_failed_on_course', backup::LOG_ERROR, $course->shortname); // Log error header.
388395
$bc->log('Exception: ' . $e->errorcode, backup::LOG_ERROR, $e->a, 1); // Log original exception problem.
389396
$bc->log('Debug: ' . $e->debuginfo, backup::LOG_DEBUG, null, 1); // Log original debug information.
390-
$outcome = false;
397+
$outcome = self::BACKUP_STATUS_ERROR;
391398
}
392399

393400
$bc->destroy();
@@ -396,6 +403,30 @@ public static function launch_automated_backup($course, $starttime, $userid) {
396403
return $outcome;
397404
}
398405

406+
/**
407+
* Returns the backup outcome by analysing its results.
408+
*
409+
* @param array $results returned by a backup
410+
* @return int {@link self::BACKUP_STATUS_OK} and other constants
411+
*/
412+
public static function outcome_from_results($results) {
413+
$outcome = self::BACKUP_STATUS_OK;
414+
foreach ($results as $code => $value) {
415+
// Each possible error and warning code has to be specified in this switch
416+
// which basically analyses the results to return the correct backup status.
417+
switch ($code) {
418+
case 'missing_files_in_pool':
419+
$outcome = self::BACKUP_STATUS_WARNING;
420+
break;
421+
}
422+
// If we found the highest error level, we exit the loop.
423+
if ($outcome == self::BACKUP_STATUS_ERROR) {
424+
break;
425+
}
426+
}
427+
return $outcome;
428+
}
429+
399430
/**
400431
* Removes deleted courses fromn the backup_courses table so that we don't
401432
* waste time backing them up.

lang/en/moodle.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,7 @@
18021802
$string['virusplaceholder'] = 'This file that has been uploaded was found to contain a virus and has been moved or deleted and the user notified.';
18031803
$string['visible'] = 'Visible';
18041804
$string['visibletostudents'] = 'Visible to {$a}';
1805+
$string['warning'] = 'Warning';
18051806
$string['warningdeleteresource'] = 'Warning: {$a} is referred in a resource. Would you like to update the resource?';
18061807
$string['webpage'] = 'Web page';
18071808
$string['week'] = 'Week';

report/backups/index.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
require_once($CFG->libdir.'/adminlib.php');
2828
require_once($CFG->dirroot.'/backup/lib.php');
2929

30+
// Required for constants in backup_cron_automated_helper
31+
require_once($CFG->dirroot.'/backup/util/helper/backup_cron_helper.class.php');
32+
3033
admin_externalpage_setup('reportbackups', '', null, '', array('pagelayout'=>'report'));
3134

3235
$table = new html_table;
@@ -45,6 +48,7 @@
4548
$strok = get_string("ok");
4649
$strunfinished = get_string("unfinished");
4750
$strskipped = get_string("skipped");
51+
$strwarning = get_string("warning");
4852

4953
list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
5054
$sql = "SELECT bc.*, c.fullname $select
@@ -58,15 +62,18 @@
5862
context_instance_preload($backuprow);
5963

6064
// Prepare a cell to display the status of the entry
61-
if ($backuprow->laststatus == 1) {
65+
if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_OK) {
6266
$status = $strok;
6367
$statusclass = 'backup-ok'; // Green
64-
} else if ($backuprow->laststatus == 2) {
68+
} else if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED) {
6569
$status = $strunfinished;
6670
$statusclass = 'backup-unfinished'; // Red
67-
} else if ($backuprow->laststatus == 3) {
71+
} else if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_SKIPPED) {
6872
$status = $strskipped;
6973
$statusclass = 'backup-skipped'; // Green
74+
} else if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_WARNING) {
75+
$status = $strwarning;
76+
$statusclass = 'backup-warning'; // Orange
7077
} else {
7178
$status = $strerror;
7279
$statusclass = 'backup-error'; // Red

theme/base/style/admin.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#page-admin-report-backups-index .backup-unfinished {color: #f00000;}
4343
#page-admin-report-backups-index .backup-skipped,
4444
#page-admin-report-backups-index .backup-ok {color: #006400;}
45+
#page-admin-report-backups-index .backup-warning {color: #ff9900;}
4546

4647
#page-admin-qbehaviours .disabled {color: gray;}
4748
#page-admin-qbehaviours th {white-space: normal;}

0 commit comments

Comments
 (0)