Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * A report to display the outcome of scheduled backups
  19   *
  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
  24   */
  25  
  26  require_once('../../config.php');
  27  require_once($CFG->libdir . '/adminlib.php');
  28  
  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');
  32  
  33  $courseid = optional_param('courseid', 0, PARAM_INT);
  34  $page = optional_param('page', 0, PARAM_INT); // This represents which backup we are viewing.
  35  
  36  // Required for constants in backup_cron_automated_helper
  37  require_once($CFG->dirroot.'/backup/util/helper/backup_cron_helper.class.php');
  38  
  39  admin_externalpage_setup('reportbackups', '', null, '', array('pagelayout'=>'report'));
  40  
  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');
  48  $strqueued = get_string('queued');
  49  
  50  if ($courseid) {
  51      $course = $DB->get_record('course', array('id' => $courseid), 'id, fullname', MUST_EXIST);
  52  
  53      // Get the automated backups that have been performed for this course.
  54      $params = array('operation' => backup::OPERATION_BACKUP,
  55                      'type' => backup::TYPE_1COURSE,
  56                      'itemid' => $course->id,
  57                      'interactive' => backup::INTERACTIVE_NO);
  58      if ($backups = $DB->get_records('backup_controllers', $params, 'timecreated DESC',
  59          'id, backupid, status, timecreated', $page, 1)) {
  60          // Get the backup we want to use.
  61          $backup = reset($backups);
  62  
  63          // Get the backup status.
  64          if ($backup->status == backup::STATUS_FINISHED_OK) {
  65              $status = $strok;
  66              $statusclass = 'table-success'; // Green.
  67          } else if ($backup->status == backup::STATUS_AWAITING || $backup->status == backup::STATUS_EXECUTING) {
  68              $status = $strunfinished;
  69              $statusclass = 'table-danger'; // Red.
  70          } else { // Else show error.
  71              $status = $strerror;
  72              $statusclass = 'table-danger'; // Red.
  73          }
  74  
  75          $table = new html_table();
  76          $table->head = array('');
  77          $table->data = array();
  78          $statusrow = get_string('status') . ' - ' . html_writer::tag('span', $status, array('class' => $statusclass));
  79          $table->data[] = array($statusrow);
  80  
  81          // Get the individual logs for this backup.
  82          if ($logs = $DB->get_records('backup_logs', array('backupid' => $backup->backupid), 'timecreated ASC',
  83              'id, message, timecreated')) {
  84              foreach ($logs as $log) {
  85                  $table->data[] = array(userdate($log->timecreated, get_string('strftimetime', 'report_backups')) .
  86                      ' - ' . $log->message);
  87              }
  88          } else {
  89              $table->data[] = array(get_string('nologsfound', 'report_backups'));
  90          }
  91      }
  92  
  93      // Set the course name to display.
  94      $coursename = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
  95  
  96      echo $OUTPUT->header();
  97      echo $OUTPUT->heading(get_string('backupofcourselogs', 'report_backups', $coursename));
  98      if (isset($backup)) {
  99          // We put this logic down here as we may be viewing a backup that was performed which there were no logs
 100          // recorded for. We still want to display the pagination so the user can still navigate to other backups,
 101          // and we also display a message so they are aware that the backup happened but there were no logs.
 102          $baseurl = new moodle_url('/report/backups/index.php', array('courseid' => $courseid));
 103          $numberofbackups = $DB->count_records('backup_controllers', $params);
 104          $pagingbar = new paging_bar($numberofbackups, $page, 1, $baseurl);
 105  
 106          echo $OUTPUT->render($pagingbar);
 107          echo $OUTPUT->heading(get_string('logsofbackupexecutedon', 'report_backups', userdate($backup->timecreated)), 3);
 108          echo html_writer::table($table);
 109          echo $OUTPUT->render($pagingbar);
 110      } else {
 111          echo $OUTPUT->box(get_string('nobackupsfound', 'report_backups'), 'center');
 112      }
 113      echo $OUTPUT->footer();
 114      exit();
 115  }
 116  
 117  $table = new html_table;
 118  $table->head = array(
 119      get_string("course"),
 120      get_string("timetaken", "backup"),
 121      get_string("status"),
 122      get_string("backupnext")
 123  );
 124  $table->headspan = array(1, 3, 1, 1);
 125  $table->attributes = array('class' => 'generaltable backup-report');
 126  $table->data = array();
 127  
 128  $select = ', ' . context_helper::get_preload_record_columns_sql('ctx');
 129  $join = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
 130  $sql = "SELECT bc.*, c.id as courseid, c.fullname $select
 131            FROM {backup_courses} bc
 132            JOIN {course} c ON c.id = bc.courseid
 133                 $join";
 134  $rs = $DB->get_recordset_sql($sql, array('contextlevel' => CONTEXT_COURSE));
 135  foreach ($rs as $backuprow) {
 136  
 137      // Cache the course context
 138      context_helper::preload_from_record($backuprow);
 139  
 140      // Prepare a cell to display the status of the entry.
 141      if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_OK) {
 142          $status = $strok;
 143          $statusclass = 'table-success'; // Green.
 144      } else if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED) {
 145          $status = $strunfinished;
 146          $statusclass = 'table-danger'; // Red.
 147      } else if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_SKIPPED) {
 148          $status = $strskipped;
 149          $statusclass = 'table-success'; // Green.
 150      } else if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_WARNING) {
 151          $status = $strwarning;
 152          $statusclass = 'table-warning'; // Orange.
 153      } else if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_NOTYETRUN) {
 154          $status = $strnotyetrun;
 155          $statusclass = 'table-success';
 156      } else if ($backuprow->laststatus == backup_cron_automated_helper::BACKUP_STATUS_QUEUED) {
 157          $status = $strqueued;
 158          $statusclass = 'table-success';
 159      } else {
 160          $status = $strerror;
 161          $statusclass = 'table-danger'; // Red.
 162      }
 163      $status = new html_table_cell($status);
 164      $status->attributes = array('class' => $statusclass);
 165  
 166      // Create the row and add it to the table
 167      $backuprowname = format_string($backuprow->fullname, true, array('context' => context_course::instance($backuprow->courseid)));
 168      $backuplogsurl = new moodle_url('/report/backups/index.php', array('courseid' => $backuprow->courseid));
 169      $backuplogsicon = new pix_icon('t/viewdetails', get_string('viewlogs', 'report_backups'));
 170      $cells = array(
 171          $backuprowname . ' ' . $OUTPUT->action_icon($backuplogsurl, $backuplogsicon),
 172          userdate($backuprow->laststarttime, $strftimedatetime),
 173          '-',
 174          userdate($backuprow->lastendtime, $strftimedatetime),
 175          $status,
 176          userdate($backuprow->nextstarttime, $strftimedatetime)
 177      );
 178      $table->data[] = new html_table_row($cells);
 179  }
 180  $rs->close();
 181  
 182  // Check if we have any results and if not add a no records notification
 183  if (empty($table->data)) {
 184      $cell = new html_table_cell($OUTPUT->notification(get_string('nologsfound')));
 185      $cell->colspan = 6;
 186      $table->data[] = new html_table_row(array($cell));
 187  }
 188  
 189  $automatedbackupsenabled = get_config('backup', 'backup_auto_active');
 190  
 191  // Display the backup report
 192  echo $OUTPUT->header();
 193  echo $OUTPUT->heading(get_string("backuploglaststatus"));
 194  echo $OUTPUT->box_start();
 195  if (empty($automatedbackupsenabled)) {
 196      // Automated backups aren't active, display a notification.
 197      // Not we don't stop because of this as perhaps scheduled backups are being run
 198      // automatically, or were enabled in the page.
 199      echo $OUTPUT->notification(get_string('automatedbackupsinactive', 'backup'));
 200  }
 201  echo html_writer::table($table);
 202  echo $OUTPUT->box_end();
 203  echo $OUTPUT->footer();