Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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   * Display user activity reports for a course (totals)
  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  require('../../config.php');
  26  require_once($CFG->dirroot.'/report/log/locallib.php');
  27  require_once($CFG->dirroot.'/lib/tablelib.php');
  28  
  29  $userid   = required_param('id', PARAM_INT);
  30  $courseid = required_param('course', PARAM_INT);
  31  $mode     = optional_param('mode', 'today', PARAM_ALPHA);
  32  $page     = optional_param('page', 0, PARAM_INT);
  33  $perpage  = optional_param('perpage', 100, PARAM_INT);
  34  $logreader   = optional_param('logreader', '', PARAM_COMPONENT); // Log reader which will be used for displaying logs.
  35  
  36  if ($mode !== 'today' and $mode !== 'all') {
  37      $mode = 'today';
  38  }
  39  
  40  $user = $DB->get_record('user', array('id' => $userid, 'deleted' => 0), '*', MUST_EXIST);
  41  $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  42  
  43  $coursecontext   = context_course::instance($course->id);
  44  $personalcontext = context_user::instance($user->id);
  45  
  46  if ($courseid == SITEID) {
  47      $PAGE->set_context($personalcontext);
  48  }
  49  
  50  if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
  51          and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
  52      //TODO: do not require parents to be enrolled in courses - this is a hack!
  53      require_login();
  54      $PAGE->set_course($course);
  55  } else {
  56      require_login($course);
  57  }
  58  
  59  list($all, $today) = report_log_can_access_user_report($user, $course);
  60  
  61  if (!$today && !$all) {
  62      throw new \moodle_exception('nocapability', 'report_log');
  63  }
  64  
  65  if ($mode === 'today') {
  66      if (!$today) {
  67          require_capability('report/log:viewtoday', $coursecontext);
  68      }
  69  } else {
  70      if (!$all) {
  71          require_capability('report/log:view', $coursecontext);
  72      }
  73  }
  74  
  75  $stractivityreport = get_string('activityreport');
  76  
  77  $PAGE->set_pagelayout('report');
  78  $PAGE->set_url('/report/log/user.php', array('id' => $user->id, 'course' => $course->id, 'mode' => $mode));
  79  $PAGE->navigation->extend_for_user($user);
  80  $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
  81  $PAGE->set_title("$course->shortname: $stractivityreport");
  82  $PAGE->navbar->add(get_string('alllogs'));
  83  
  84  // Create the appropriate breadcrumb.
  85  $navigationnode = array(
  86          'url' => new moodle_url('/report/log/user.php', array('id' => $user->id, 'course' => $course->id, 'mode' => $mode))
  87      );
  88  if ($mode === 'today') {
  89      $navigationnode['name'] = get_string('todaylogs');
  90  } else {
  91      $navigationnode['name'] = get_string('alllogs');
  92  }
  93  $PAGE->add_report_nodes($user->id, $navigationnode);
  94  
  95  if ($courseid == SITEID) {
  96      $PAGE->set_heading(fullname($user, has_capability('moodle/site:viewfullnames', $PAGE->context)));
  97  } else {
  98      $PAGE->set_heading($course->fullname);
  99  }
 100  
 101  // Trigger a user logs viewed event.
 102  $event = \report_log\event\user_report_viewed::create(array('context' => $coursecontext, 'relateduserid' => $userid,
 103          'other' => array('mode' => $mode)));
 104  $event->trigger();
 105  
 106  echo $OUTPUT->header();
 107  if ($courseid != SITEID) {
 108      $userheading = array(
 109              'heading' => fullname($user, has_capability('moodle/site:viewfullnames', $PAGE->context)),
 110              'user' => $user,
 111              'usercontext' => $personalcontext,
 112          );
 113      echo $OUTPUT->context_header($userheading, 2);
 114      if ($mode === 'today') {
 115          echo $OUTPUT->heading(get_string('todaylogs', 'moodle'), 2, 'main mt-4 mb-4');
 116      } else {
 117          echo $OUTPUT->heading(get_string('alllogs', 'moodle'), 2, 'main mt-4 mb-4');
 118      }
 119  }
 120  
 121  // Time to filter records from.
 122  if ($mode === 'today') {
 123      $timefrom = usergetmidnight(time());
 124  } else {
 125      $timefrom = 0;
 126  }
 127  
 128  $output = $PAGE->get_renderer('report_log');
 129  $reportlog = new report_log_renderable($logreader, $course, $user->id, 0, '', -1, -1, false, false, true, false, $PAGE->url,
 130          $timefrom, '', $page, $perpage, 'timecreated DESC');
 131  
 132  // Setup table if log reader is enabled.
 133  if (!empty($reportlog->selectedlogreader)) {
 134      $reportlog->setup_table();
 135      $reportlog->tablelog->is_downloadable(false);
 136  }
 137  
 138  echo $output->reader_selector($reportlog);
 139  
 140  // Print the graphic chart accordingly to the mode (all, today).
 141  echo '<div class="graph">';
 142  report_log_print_graph($course, $user, $mode, 0, $logreader);
 143  echo '</div>';
 144  
 145  echo $output->render($reportlog);
 146  echo $OUTPUT->footer();