@@ -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.
0 commit comments