Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 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   * Displays different views of the logs.
  19   *
  20   * @package    report_log
  21   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use core\report_helper;
  26  
  27  require('../../config.php');
  28  require_once($CFG->dirroot.'/course/lib.php');
  29  require_once($CFG->dirroot.'/report/log/locallib.php');
  30  require_once($CFG->libdir.'/adminlib.php');
  31  require_once($CFG->dirroot.'/lib/tablelib.php');
  32  
  33  $id          = optional_param('id', 0, PARAM_INT);// Course ID.
  34  $group       = optional_param('group', 0, PARAM_INT); // Group to display.
  35  $user        = optional_param('user', 0, PARAM_INT); // User to display.
  36  $date        = optional_param('date', 0, PARAM_INT); // Date to display.
  37  $modid       = optional_param('modid', 0, PARAM_ALPHANUMEXT); // Module id or 'site_errors'.
  38  $modaction   = optional_param('modaction', '', PARAM_ALPHAEXT); // An action as recorded in the logs.
  39  $page        = optional_param('page', '0', PARAM_INT);     // Which page to show.
  40  $perpage     = optional_param('perpage', '100', PARAM_INT); // How many per page.
  41  $showcourses = optional_param('showcourses', false, PARAM_BOOL); // Whether to show courses if we're over our limit.
  42  $showusers   = optional_param('showusers', false, PARAM_BOOL); // Whether to show users if we're over our limit.
  43  $chooselog   = optional_param('chooselog', false, PARAM_BOOL);
  44  $logformat   = optional_param('download', '', PARAM_ALPHA);
  45  $logreader      = optional_param('logreader', '', PARAM_COMPONENT); // Reader which will be used for displaying logs.
  46  $edulevel    = optional_param('edulevel', -1, PARAM_INT); // Educational level.
  47  $origin      = optional_param('origin', '', PARAM_TEXT); // Event origin.
  48  
  49  $params = array();
  50  if (!empty($id)) {
  51      $params['id'] = $id;
  52  } else {
  53      $id = $SITE->id;
  54  }
  55  if ($group !== 0) {
  56      $params['group'] = $group;
  57  }
  58  if ($user !== 0) {
  59      $params['user'] = $user;
  60  }
  61  if ($date !== 0) {
  62      $params['date'] = $date;
  63  }
  64  if ($modid !== 0) {
  65      $params['modid'] = $modid;
  66  }
  67  if ($modaction !== '') {
  68      $params['modaction'] = $modaction;
  69  }
  70  if ($page !== '0') {
  71      $params['page'] = $page;
  72  }
  73  if ($perpage !== '100') {
  74      $params['perpage'] = $perpage;
  75  }
  76  if ($showcourses) {
  77      $params['showcourses'] = $showcourses;
  78  }
  79  if ($showusers) {
  80      $params['showusers'] = $showusers;
  81  }
  82  if ($chooselog) {
  83      $params['chooselog'] = $chooselog;
  84  }
  85  if ($logformat !== '') {
  86      $params['download'] = $logformat;
  87  }
  88  if ($logreader !== '') {
  89      $params['logreader'] = $logreader;
  90  }
  91  if (($edulevel != -1)) {
  92      $params['edulevel'] = $edulevel;
  93  }
  94  if ($origin !== '') {
  95      $params['origin'] = $origin;
  96  }
  97  // Legacy store hack, as edulevel is not supported.
  98  if ($logreader == 'logstore_legacy') {
  99      $params['edulevel'] = -1;
 100      $edulevel = -1;
 101  }
 102  $url = new moodle_url("/report/log/index.php", $params);
 103  
 104  $PAGE->set_url('/report/log/index.php', array('id' => $id));
 105  $PAGE->set_pagelayout('report');
 106  
 107  // Get course details.
 108  if ($id != $SITE->id) {
 109      $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
 110      require_login($course);
 111      $context = context_course::instance($course->id);
 112  } else {
 113      $course = $SITE;
 114      require_login();
 115      $context = context_system::instance();
 116      $PAGE->set_context($context);
 117  }
 118  
 119  require_capability('report/log:view', $context);
 120  
 121  // When user choose to view logs then only trigger event.
 122  if ($chooselog) {
 123      // Trigger a report viewed event.
 124      $event = \report_log\event\report_viewed::create(array('context' => $context, 'relateduserid' => $user,
 125              'other' => array('groupid' => $group, 'date' => $date, 'modid' => $modid, 'modaction' => $modaction,
 126              'logformat' => $logformat)));
 127      $event->trigger();
 128  }
 129  
 130  if (!empty($page)) {
 131      $strlogs = get_string('logs'). ": ". get_string('page', 'report_log', $page + 1);
 132  } else {
 133      $strlogs = get_string('logs');
 134  }
 135  $stradministration = get_string('administration');
 136  $strreports = get_string('reports');
 137  
 138  // Before we close session, make sure we have editing information in session.
 139  $adminediting = optional_param('adminedit', -1, PARAM_BOOL);
 140  if ($PAGE->user_allowed_editing() && $adminediting != -1) {
 141      $USER->editing = $adminediting;
 142  }
 143  
 144  if ($course->id == $SITE->id) {
 145      admin_externalpage_setup('reportlog', '', null, '', array('pagelayout' => 'report'));
 146      $PAGE->set_title($strlogs);
 147      $PAGE->set_primary_active_tab('siteadminnode');
 148  } else {
 149      $PAGE->set_title($course->shortname .': '. $strlogs);
 150      $PAGE->set_heading($course->fullname);
 151  }
 152  
 153  $reportlog = new report_log_renderable($logreader, $course, $user, $modid, $modaction, $group, $edulevel, $showcourses, $showusers,
 154          $chooselog, true, $url, $date, $logformat, $page, $perpage, 'timecreated DESC', $origin);
 155  $readers = $reportlog->get_readers();
 156  $output = $PAGE->get_renderer('report_log');
 157  
 158  if (empty($readers)) {
 159      echo $output->header();
 160      echo $output->heading(get_string('nologreaderenabled', 'report_log'));
 161  } else {
 162      if (!empty($chooselog)) {
 163          // Delay creation of table, till called by user with filter.
 164          $reportlog->setup_table();
 165  
 166          if (empty($logformat)) {
 167              echo $output->header();
 168              // Print selector dropdown.
 169              $pluginname = get_string('pluginname', 'report_log');
 170              report_helper::print_report_selector($pluginname);
 171              $userinfo = get_string('allparticipants');
 172              $dateinfo = get_string('alldays');
 173  
 174              if ($user) {
 175                  $u = $DB->get_record('user', array('id' => $user, 'deleted' => 0), '*', MUST_EXIST);
 176                  $userinfo = fullname($u, has_capability('moodle/site:viewfullnames', $context));
 177              }
 178              if ($date) {
 179                  $dateinfo = userdate($date, get_string('strftimedaydate'));
 180              }
 181              if (!empty($course) && ($course->id != SITEID)) {
 182                  $PAGE->navbar->add("$userinfo, $dateinfo");
 183              }
 184              echo $output->render($reportlog);
 185          } else {
 186              \core\session\manager::write_close();
 187              $reportlog->download();
 188              exit();
 189          }
 190      } else {
 191          echo $output->header();
 192          // Print selector dropdown.
 193          $pluginname = get_string('pluginname', 'report_log');
 194          report_helper::print_report_selector($pluginname);
 195          echo $output->heading(get_string('chooselogs') .':', 3);
 196          echo $output->render($reportlog);
 197      }
 198  }
 199  
 200  echo $output->footer();