Skip to content

Commit

Permalink
MDL-48533 report_backups: added ability to view backup logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Jan 8, 2015
1 parent 0b2e01d commit 1272101
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 11 deletions.
Binary file added pix/t/viewdetails.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions pix/t/viewdetails.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 88 additions & 11 deletions report/backups/index.php
Expand Up @@ -24,13 +24,95 @@
*/

require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir . '/adminlib.php');

// Required for backup::xxx constants.
require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php');
require_once($CFG->dirroot . '/backup/backup.class.php');

$courseid = optional_param('courseid', 0, PARAM_INT);
$page = optional_param('page', 0, PARAM_INT); // This represents which backup we are viewing.

// Required for constants in backup_cron_automated_helper
require_once($CFG->dirroot.'/backup/util/helper/backup_cron_helper.class.php');

admin_externalpage_setup('reportbackups', '', null, '', array('pagelayout'=>'report'));

$strftimedatetime = get_string('strftimerecent');
$strerror = get_string('error');
$strok = get_string('ok');
$strunfinished = get_string('unfinished');
$strskipped = get_string('skipped');
$strwarning = get_string('warning');
$strnotyetrun = get_string('backupnotyetrun');

if ($courseid) {
$course = $DB->get_record('course', array('id' => $courseid), 'id, fullname', MUST_EXIST);

// Get the automated backups that have been performed for this course.
$params = array('operation' => backup::OPERATION_BACKUP,
'type' => backup::TYPE_1COURSE,
'itemid' => $course->id,
'interactive' => backup::INTERACTIVE_NO);
if ($backups = $DB->get_records('backup_controllers', $params, 'timecreated DESC',
'id, backupid, status, timecreated', $page, 1)) {
// Get the backup we want to use.
$backup = reset($backups);

// Get the backup status.
if ($backup->status == backup::STATUS_FINISHED_OK) {
$status = $strok;
$statusclass = 'backup-ok'; // Green.
} else if ($backup->status == backup::STATUS_AWAITING || $backup->status == backup::STATUS_EXECUTING) {
$status = $strunfinished;
$statusclass = 'backup-unfinished'; // Red.
} else { // Else show error.
$status = $strerror;
$statusclass = 'backup-error'; // Red.
}

$table = new html_table();
$table->head = array('');
$table->data = array();
$statusrow = get_string('status') . ' - ' . html_writer::tag('span', $status, array('class' => $statusclass));
$table->data[] = array($statusrow);

// Get the individual logs for this backup.
if ($logs = $DB->get_records('backup_logs', array('backupid' => $backup->backupid), 'timecreated ASC',
'id, message, timecreated')) {
foreach ($logs as $log) {
$table->data[] = array(userdate($log->timecreated, get_string('strftimetime', 'report_backups')) .
' - ' . $log->message);
}
} else {
$table->data[] = array(get_string('nologsfound', 'report_backups'));
}
}

// Set the course name to display.
$coursename = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));

echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('backupofcourselogs', 'report_backups', $coursename));
if (isset($backup)) {
// We put this logic down here as we may be viewing a backup that was performed which there were no logs
// recorded for. We still want to display the pagination so the user can still navigate to other backups,
// and we also display a message so they are aware that the backup happened but there were no logs.
$baseurl = new moodle_url('/report/backups/index.php', array('courseid' => $courseid));
$numberofbackups = $DB->count_records('backup_controllers', $params);
$pagingbar = new paging_bar($numberofbackups, $page, 1, $baseurl);

echo $OUTPUT->render($pagingbar);
echo $OUTPUT->heading(get_string('logsofbackupexecutedon', 'report_backups', userdate($backup->timecreated)), 3);
echo html_writer::table($table);
echo $OUTPUT->render($pagingbar);
} else {
echo $OUTPUT->box(get_string('nobackupsfound', 'report_backups'), 'center');
}
echo $OUTPUT->footer();
exit();
}

$table = new html_table;
$table->head = array(
get_string("course"),
Expand All @@ -42,17 +124,9 @@
$table->attributes = array('class' => 'generaltable backup-report');
$table->data = array();

$strftimedatetime = get_string('strftimerecent');
$strerror = get_string('error');
$strok = get_string('ok');
$strunfinished = get_string('unfinished');
$strskipped = get_string('skipped');
$strwarning = get_string('warning');
$strnotyetrun = get_string('backupnotyetrun');

$select = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$join = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$sql = "SELECT bc.*, c.fullname $select
$sql = "SELECT bc.*, c.id as courseid, c.fullname $select
FROM {backup_courses} bc
JOIN {course} c ON c.id = bc.courseid
$join";
Expand Down Expand Up @@ -86,8 +160,11 @@
$status->attributes = array('class' => $statusclass);

// Create the row and add it to the table
$backuprowname = format_string($backuprow->fullname, true, array('context' => context_course::instance($backuprow->courseid)));
$backuplogsurl = new moodle_url('/report/backups/index.php', array('courseid' => $backuprow->courseid));
$backuplogsicon = new pix_icon('t/viewdetails', get_string('viewlogs', 'report_backups'));
$cells = array(
format_string($backuprow->fullname, true, array('context' => context_course::instance($backuprow->courseid))),
$backuprowname . ' ' . $OUTPUT->action_icon($backuplogsurl, $backuplogsicon),
userdate($backuprow->laststarttime, $strftimedatetime),
'-',
userdate($backuprow->lastendtime, $strftimedatetime),
Expand Down
6 changes: 6 additions & 0 deletions report/backups/lang/en/report_backups.php
Expand Up @@ -23,4 +23,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['backupofcourselogs'] = 'Backup logs of {$a}';
$string['logsofbackupexecutedon'] = 'Logs of the backup executed on {$a}';
$string['nobackupsfound'] = 'There were no backups found.';
$string['nologsfound'] = 'There were no logs found for this backup.';
$string['pluginname'] = 'Backups report';
$string['strftimetime'] = '%I:%M:%S %p';
$string['viewlogs'] = 'View logs';

0 comments on commit 1272101

Please sign in to comment.