-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
138 lines (121 loc) · 5.5 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
/**
* This plugin sets the nextstarttime value of the backup_courses table high or low, depending on the settings.
*
*/
class CategoryBackup {
private $active = null; // True: Selective backup is actived in settings.
private $categories = null;
private $courses = null; // Courses in selected categories.
private $FAR_FUTURE = 2147483000; // 01/19/2038 @ 3:03am (UTC).
function __construct() {
global $CFG;
// Get config data.
$this->active = $CFG->local_categorybackup_active;
$this->categories = explode(',', $CFG->local_categorybackup_categories);
}
public function cron() {
if ($this->active) {
echo "\nSelective backup is actived in settings. Activate backups for the following courses: \n\n";
$this->collectCoursesInCategories();
$this->deactivateBackup();
} else {
echo "\nSelective backup is NOT actived in settings. Activate backups for all courses. \n\n";
$this->activateBackup();
}
}
/**
* Sets the FAR_FUTURE date for the next run to zero
* (Create backup in the next run)
*/
private function activateBackup() {
global $DB;
// If this plugin set the nextstarttime in the past, make it be backed up in the next automated backup.
$sql = "UPDATE {backup_courses} SET nextstarttime=0 WHERE nextstarttime=" . $this->FAR_FUTURE;
$DB->execute($sql);
}
/**
* Collects recursivly all courses in the selected categories
*
* (Returns the courses to backup in class variable)
*/
private function collectCoursesInCategories() {
$courses = array();
foreach ($this->categories as $id) {
$courses_tmp = \core_course_category::get($id, IGNORE_MISSING, true)->get_courses(array('recursive' => true));
$courses = $courses + $courses_tmp;
}
$this->courses = $courses;
}
/**
* Deactivates the backup for all courses that are not in one of the selected categories
*
* Deactivation means: Setting the next date for backup to one in the far future
*/
private function deactivateBackup() {
global $CFG, $DB;
require_once $CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php';
$rs = $DB->get_recordset('course');
$now = time();
echo "ID \t | \t Course fullname \n"; // Table header.
echo "-------------------------- \n";
foreach ($rs as $course) {
// Should a backup be created?
$course_to_backup = isset($this->courses[$course->id]) && $course->id != 1;
// Get past schedule record for course.
$backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id));
if (!$course_to_backup) {
// The course is not in one of the selected categories.
if (!$backupcourse) {
// Create new record for this course.
$backupcourse = new stdClass;
$backupcourse->courseid = $course->id;
$DB->insert_record('backup_courses', $backupcourse);
$backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id));
}
// Set date and update.
$backupcourse->laststatus = 1; // OK.
$backupcourse->laststarttime = $now;
$backupcourse->lastendtime = $now;
$backupcourse->nextstarttime = $this->FAR_FUTURE;
$DB->update_record('backup_courses', $backupcourse);
} else {
// The course should be backed up the next time the automated backups are run.
echo $course->id . "\t | \t " . $course->fullname . "\n";
if (!$backupcourse) {
// Create new record for this course.
$backupcourse = new stdClass;
$backupcourse->courseid = $course->id;
$backupcourse->laststatus = 5; // Not yet run.
$DB->insert_record('backup_courses', $backupcourse);
$backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id));
}
if ($backupcourse->nextstarttime == $this->FAR_FUTURE) {
$backupcourse->nextstarttime = 0;
$backupcourse->laststarttime = 0;
$backupcourse->lastendtime = 0;
$backupcourse->laststatus = 5; // Not yet run.
}
$DB->update_record('backup_courses', $backupcourse);
}
}
echo "\n";
$rs->close();
}
}