Merge branch 'MDL-62397-master' of git://github.com/andrewnicols/moodle
[moodle.git] / report / backups / index.php
blob39dadc41fa4d4d740259d03e1b5b1a2ddfa798a3
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * A report to display the outcome of scheduled backups
20 * @package report
21 * @subpackage backups
22 * @copyright 2007 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../../config.php');
27 require_once($CFG->libdir . '/adminlib.php');
29 // Required for backup::xxx constants.
30 require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php');
31 require_once($CFG->dirroot . '/backup/backup.class.php');
33 $courseid = optional_param('courseid', 0, PARAM_INT);
34 $page = optional_param('page', 0, PARAM_INT); // This represents which backup we are viewing.
36 // Required for constants in backup_cron_automated_helper
37 require_once($CFG->dirroot.'/backup/util/helper/backup_cron_helper.class.php');
39 admin_externalpage_setup('reportbackups', '', null, '', array('pagelayout'=>'report'));
41 $strftimedatetime = get_string('strftimerecent');
42 $strerror = get_string('error');
43 $strok = get_string('ok');
44 $strunfinished = get_string('unfinished');
45 $strskipped = get_string('skipped');
46 $strwarning = get_string('warning');
47 $strnotyetrun = get_string('backupnotyetrun');
49 if ($courseid) {
50 $course = $DB->get_record('course', array('id' => $courseid), 'id, fullname', MUST_EXIST);
52 // Get the automated backups that have been performed for this course.
53 $params = array('operation' => backup::OPERATION_BACKUP,
54 'type' => backup::TYPE_1COURSE,
55 'itemid' => $course->id,
56 'interactive' => backup::INTERACTIVE_NO);
57 if ($backups = $DB->get_records('backup_controllers', $params, 'timecreated DESC',
58 'id, backupid, status, timecreated', $page, 1)) {
59 // Get the backup we want to use.
60 $backup = reset($backups);
62 // Get the backup status.
63 if ($backup->status == backup::STATUS_FINISHED_OK) {
64 $status = $strok;
65 $statusclass = 'backup-ok'; // Green.
66 } else if ($backup->status == backup::STATUS_AWAITING || $backup->status == backup::STATUS_EXECUTING) {
67 $status = $strunfinished;
68 $statusclass = 'backup-unfinished'; // Red.
69 } else { // Else show error.
70 $status = $strerror;
71 $statusclass = 'backup-error'; // Red.
74 $table = new html_table();
75 $table->head = array('');
76 $table->data = array();
77 $statusrow = get_string('status') . ' - ' . html_writer::tag('span', $status, array('class' => $statusclass));
78 $table->data[] = array($statusrow);
80 // Get the individual logs for this backup.
81 if ($logs = $DB->get_records('backup_logs', array('backupid' => $backup->backupid), 'timecreated ASC',
82 'id, message, timecreated')) {
83 foreach ($logs as $log) {
84 $table->data[] = array(userdate($log->timecreated, get_string('strftimetime', 'report_backups')) .
85 ' - ' . $log->message);
87 } else {
88 $table->data[] = array(get_string('nologsfound', 'report_backups'));
92 // Set the course name to display.
93 $coursename = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
95 echo $OUTPUT->header();
96 echo $OUTPUT->heading(get_string('backupofcourselogs', 'report_backups', $coursename));
97 if (isset($backup)) {
98 // We put this logic down here as we may be viewing a backup that was performed which there were no logs
99 // recorded for. We still want to display the pagination so the user can still navigate to other backups,
100 // and we also display a message so they are aware that the backup happened but there were no logs.
101 $baseurl = new moodle_url('/report/backups/index.php', array('courseid' => $courseid));
102 $numberofbackups = $DB->count_records('backup_controllers', $params);
103 $pagingbar = new paging_bar($numberofbackups, $page, 1, $baseurl);
105 echo $OUTPUT->render($pagingbar);
106 echo $OUTPUT->heading(get_string('logsofbackupexecutedon', 'report_backups', userdate($backup->timecreated)), 3);
107 echo html_writer::table($table);
108 echo $OUTPUT->render($pagingbar);
109 } else {
110 echo $OUTPUT->box(get_string('nobackupsfound', 'report_backups'), 'center');
112 echo $OUTPUT->footer();
113 exit();
116 $table = new html_table;
117 $table->head = array(
118 get_string("course"),
119 get_string("timetaken", "backup"),
120 get_string("status"),
121 get_string("backupnext")
123 $table->headspan = array(1, 3, 1, 1);
124 $table->attributes = array('class' => 'generaltable backup-report');
125 $table->data = array();
127 $select = ', ' . context_helper::get_preload_record_columns_sql('ctx');
128 $join = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
129 $sql = "SELECT bc.*, c.id as courseid, c.fullname $select
130 FROM {backup_courses} bc
131 JOIN {course} c ON c.id = bc.courseid
132 $join";
133 $rs = $DB->get_recordset_sql($sql, array('contextlevel' => CONTEXT_COURSE));
134 foreach ($rs as $backuprow) {
136 // Cache the course context
137 context_helper::preload_from_record($backuprow);
139 // Prepare a cell to display the status of the entry.
140 if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_OK) {
141 $status = $strok;
142 $statusclass = 'backup-ok'; // Green.
143 } else if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED) {
144 $status = $strunfinished;
145 $statusclass = 'backup-unfinished'; // Red.
146 } else if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_SKIPPED) {
147 $status = $strskipped;
148 $statusclass = 'backup-skipped'; // Green.
149 } else if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_WARNING) {
150 $status = $strwarning;
151 $statusclass = 'backup-warning'; // Orange.
152 } else if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_NOTYETRUN) {
153 $status = $strnotyetrun;
154 $statusclass = 'backup-notyetrun';
155 } else {
156 $status = $strerror;
157 $statusclass = 'backup-error'; // Red.
159 $status = new html_table_cell($status);
160 $status->attributes = array('class' => $statusclass);
162 // Create the row and add it to the table
163 $backuprowname = format_string($backuprow->fullname, true, array('context' => context_course::instance($backuprow->courseid)));
164 $backuplogsurl = new moodle_url('/report/backups/index.php', array('courseid' => $backuprow->courseid));
165 $backuplogsicon = new pix_icon('t/viewdetails', get_string('viewlogs', 'report_backups'));
166 $cells = array(
167 $backuprowname . ' ' . $OUTPUT->action_icon($backuplogsurl, $backuplogsicon),
168 userdate($backuprow->laststarttime, $strftimedatetime),
169 '-',
170 userdate($backuprow->lastendtime, $strftimedatetime),
171 $status,
172 userdate($backuprow->nextstarttime, $strftimedatetime)
174 $table->data[] = new html_table_row($cells);
176 $rs->close();
178 // Check if we have any results and if not add a no records notification
179 if (empty($table->data)) {
180 $cell = new html_table_cell($OUTPUT->notification(get_string('nologsfound')));
181 $cell->colspan = 6;
182 $table->data[] = new html_table_row(array($cell));
185 $automatedbackupsenabled = get_config('backup', 'backup_auto_active');
187 // Display the backup report
188 echo $OUTPUT->header();
189 echo $OUTPUT->heading(get_string("backuploglaststatus"));
190 echo $OUTPUT->box_start();
191 if (empty($automatedbackupsenabled)) {
192 // Automated backups aren't active, display a notification.
193 // Not we don't stop because of this as perhaps scheduled backups are being run
194 // automatically, or were enabled in the page.
195 echo $OUTPUT->notification(get_string('automatedbackupsinactive', 'backup'));
197 echo html_writer::table($table);
198 echo $OUTPUT->box_end();
199 echo $OUTPUT->footer();