Merge branch 'MDL-32148-master' of git://github.com/StudiUM/moodle
authorSam Hemelryk <sam@moodle.com>
Tue, 16 Oct 2012 03:16:33 +0000 (11:16 +0800)
committerSam Hemelryk <sam@moodle.com>
Tue, 16 Oct 2012 03:16:33 +0000 (11:16 +0800)
admin/settings/courses.php
backup/util/helper/backup_cron_helper.class.php
lang/en/backup.php

index 25614ab..363bfb1 100644 (file)
@@ -169,6 +169,25 @@ if ($hassiteconfig
         500 => '500');
     $temp->add(new admin_setting_configselect('backup/backup_auto_keep', new lang_string('keep'), new lang_string('backupkeephelp'), 1, $keepoptoins));
     $temp->add(new admin_setting_configcheckbox('backup/backup_shortname', new lang_string('backup_shortname', 'admin'), new lang_string('backup_shortnamehelp', 'admin'), 0));
+    $temp->add(new admin_setting_configcheckbox('backup/backup_auto_skip_hidden', new lang_string('skiphidden', 'backup'), new lang_string('skiphiddenhelp', 'backup'), 1));
+    $temp->add(new admin_setting_configselect('backup/backup_auto_skip_modif_days', new lang_string('skipmodifdays', 'backup'), new lang_string('skipmodifdayshelp', 'backup'), 30, array(
+        0 => new lang_string('never'),
+        1 => new lang_string('numdays', '', 1),
+        2 => new lang_string('numdays', '', 2),
+        3 => new lang_string('numdays', '', 3),
+        5 => new lang_string('numdays', '', 5),
+        7 => new lang_string('numdays', '', 7),
+        10 => new lang_string('numdays', '', 10),
+        14 => new lang_string('numdays', '', 14),
+        20 => new lang_string('numdays', '', 20),
+        30 => new lang_string('numdays', '', 30),
+        60 => new lang_string('numdays', '', 60),
+        90 => new lang_string('numdays', '', 90),
+        120 => new lang_string('numdays', '', 120),
+        180 => new lang_string('numdays', '', 180),
+        365 => new lang_string('numdays', '', 365)
+    )));
+    $temp->add(new admin_setting_configcheckbox('backup/backup_auto_skip_modif_prev', new lang_string('skipmodifprev', 'backup'), new lang_string('skipmodifprevhelp', 'backup'), 0));
 
     // Automated defaults section.
     $temp->add(new admin_setting_heading('automatedsettings', new lang_string('automatedsettings','backup'), ''));
index e3ada4f..c9179cb 100644 (file)
@@ -69,6 +69,7 @@ abstract class backup_cron_automated_helper {
         $status = true;
         $emailpending = false;
         $now = time();
+        $config = get_config('backup');
 
         mtrace("Checking automated backup status",'...');
         $state = backup_cron_automated_helper::get_automated_backup_state($rundirective);
@@ -125,40 +126,69 @@ abstract class backup_cron_automated_helper {
                     $backupcourse = $DB->get_record('backup_courses', array('courseid'=>$course->id));
                 }
 
-                // Skip courses that do not yet need backup
+                // The last backup is considered as successful when OK or SKIPPED.
+                $lastbackupwassuccessful =  ($backupcourse->laststatus == self::BACKUP_STATUS_SKIPPED ||
+                                            $backupcourse->laststatus == self::BACKUP_STATUS_OK) && (
+                                            $backupcourse->laststarttime > 0 && $backupcourse->lastendtime > 0);
+
+                // Skip courses that do not yet need backup.
                 $skipped = !(($backupcourse->nextstarttime > 0 && $backupcourse->nextstarttime < $now) || $rundirective == self::RUN_IMMEDIATELY);
-                if ($skipped && $backupcourse->nextstarttime != $nextstarttime) {
-                    $backupcourse->nextstarttime = $nextstarttime;
-                    $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED;
-                    $DB->update_record('backup_courses', $backupcourse);
-                    mtrace('Backup of \'' . $course->fullname . '\' is scheduled on ' . $showtime);
+                $skippedmessage = 'Does not require backup';
+
+                // If config backup_auto_skip_hidden is set to true, skip courses that are not visible.
+                if (!$skipped && $config->backup_auto_skip_hidden) {
+                    $skipped = ($config->backup_auto_skip_hidden && !$course->visible);
+                    $skippedmessage = 'Not visible';
                 }
 
-                // Skip backup of unavailable courses that have remained unmodified in a month
-                if (!$skipped && empty($course->visible) && ($now - $course->timemodified) > 31*24*60*60) {  //Hidden + settings were unmodified last month
-                    //Check log if there were any modifications to the course content
-                    $sqlwhere = "course=:courseid AND time>:time AND ". $DB->sql_like('action', ':action', false, true, true);
-                    $params = array('courseid' => $course->id, 'time' => $now-31*24*60*60, 'action' => '%view%');
+                // If config backup_auto_skip_modif_days is set to true, skip courses
+                // that have not been modified since the number of days defined.
+                if (!$skipped && $lastbackupwassuccessful && $config->backup_auto_skip_modif_days) {
+                    $sqlwhere = "course=:courseid AND time>:time AND ".$DB->sql_like('action', ':action', false, true, true);
+                    $timenotmodifsincedays = $now - ($config->backup_auto_skip_modif_days * DAYSECS);
+                    // Check log if there were any modifications to the course content.
+                    $params = array('courseid' => $course->id,
+                                    'time' => $timenotmodifsincedays,
+                                    'action' => '%view%');
                     $logexists = $DB->record_exists_select('log', $sqlwhere, $params);
-                    if (!$logexists) {
-                        $backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED;
-                        $backupcourse->nextstarttime = $nextstarttime;
-                        $DB->update_record('backup_courses', $backupcourse);
-                        mtrace('Skipping unchanged course '.$course->fullname);
-                        $skipped = true;
-                    }
+
+                    $skipped = ($course->timemodified <= $timenotmodifsincedays && !$logexists);
+                    $skippedmessage = 'Not modified in the past '.$config->backup_auto_skip_modif_days.' days';
+                }
+
+                // If config backup_auto_skip_modif_prev is set to true, skip courses
+                // that have not been modified since previous backup.
+                if (!$skipped && $lastbackupwassuccessful && $config->backup_auto_skip_modif_prev) {
+                    // Check log if there were any modifications to the course content.
+                    $params = array('courseid' => $course->id,
+                                    'time' => $backupcourse->laststarttime,
+                                    'action' => '%view%');
+                    $logexists = $DB->record_exists_select('log', $sqlwhere, $params);
+
+                    $skipped = ($course->timemodified <= $backupcourse->laststarttime && !$logexists);
+                    $skippedmessage = 'Not modified since previous backup';
                 }
 
-                //Now we backup every non-skipped course
-                if (!$skipped) {
+                // Skip courses not needed for backup.
+                if ($skipped) {
+                    // Output the next execution time when it has been updated.
+                    if ($backupcourse->nextstarttime != $nextstarttime) {
+                        mtrace('Backup of \'' . $course->fullname . '\' is scheduled on ' . $showtime);
+                    }
+                    $backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED;
+                    $backupcourse->nextstarttime = $nextstarttime;
+                    $DB->update_record('backup_courses', $backupcourse);
+                    mtrace('Skipping '.$course->fullname.' ('.$skippedmessage.')');
+                } else {
+                    // Backup every non-skipped courses.
                     mtrace('Backing up '.$course->fullname.'...');
 
-                    //We have to send a email because we have included at least one backup
+                    // We have to send an email because we have included at least one backup.
                     $emailpending = true;
 
-                    //Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error)
-                    if ($backupcourse->laststatus != 2) {
-                        //Set laststarttime
+                    // Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error).
+                    if ($backupcourse->laststatus != self::BACKUP_STATUS_UNFINISHED) {
+                        // Set laststarttime.
                         $starttime = time();
 
                         $backupcourse->laststarttime = time();
index 23216e2..222b032 100644 (file)
@@ -239,5 +239,11 @@ $string['setting_course_shortname'] = 'Course short name';
 $string['setting_course_startdate'] = 'Course startdate';
 $string['setting_keep_roles_and_enrolments'] = 'Keep current roles and enrolments';
 $string['setting_keep_groups_and_groupings'] = 'Keep current groups and groupings';
+$string['skiphidden'] = 'Skip hidden courses';
+$string['skiphiddenhelp'] = 'Choose whether or not to skip hidden courses';
+$string['skipmodifdays'] = 'Skip courses not modified since';
+$string['skipmodifdayshelp'] = 'Choose to skip courses that have not been modified since a number of days';
+$string['skipmodifprev'] = 'Skip courses not modified since previous backup';
+$string['skipmodifprevhelp'] = 'Choose whether or not to skip courses that have not been modified since previous backup';
 $string['totalcategorysearchresults'] = 'Total categories: {$a}';
 $string['totalcoursesearchresults'] = 'Total courses: {$a}';