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 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [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   * Display user activity reports for a course (totals)
  19   *
  20   * @package    report
  21   * @subpackage stats
  22   * @copyright  1999 onwards Martin Dougiamas  http://dougiamas.com
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require('../../config.php');
  27  require_once($CFG->dirroot.'/report/stats/locallib.php');
  28  
  29  $userid   = required_param('id', PARAM_INT);
  30  $courseid = required_param('course', PARAM_INT);
  31  
  32  $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
  33  $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
  34  
  35  $coursecontext   = context_course::instance($course->id);
  36  $personalcontext = context_user::instance($user->id);
  37  
  38  $pageheading = $course->fullname;
  39  $userfullname = fullname($user);
  40  if ($courseid == SITEID) {
  41      $PAGE->set_context($personalcontext);
  42      $pageheading = $userfullname;
  43  }
  44  
  45  if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
  46          and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
  47      //TODO: do not require parents to be enrolled in courses - this is a hack!
  48      require_login();
  49      $PAGE->set_course($course);
  50  } else {
  51      require_login($course);
  52  }
  53  
  54  if (!report_stats_can_access_user_report($user, $course)) {
  55      // this should never happen
  56      print_error('nocapability', 'report_stats');
  57  }
  58  
  59  $stractivityreport = get_string('activityreport');
  60  
  61  $PAGE->set_pagelayout('report');
  62  $PAGE->set_url('/report/stats/user.php', array('id'=>$user->id, 'course'=>$course->id));
  63  $PAGE->navigation->extend_for_user($user);
  64  $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
  65  // Breadcrumb stuff.
  66  $navigationnode = array(
  67          'name' => get_string('stats'),
  68          'url' => new moodle_url('/report/stats/user.php', array('id' => $user->id, 'course' => $course->id))
  69      );
  70  $PAGE->add_report_nodes($user->id, $navigationnode);
  71  
  72  $PAGE->set_title("$course->shortname: $stractivityreport");
  73  $PAGE->set_heading($pageheading);
  74  echo $OUTPUT->header();
  75  if ($courseid != SITEID) {
  76      $backurl = new moodle_url('/user/view.php', ['id' => $userid, 'course' => $courseid]);
  77      echo $OUTPUT->single_button($backurl, get_string('back'), 'get', ['class' => 'mb-3']);
  78  
  79      echo $OUTPUT->context_header(
  80              array(
  81              'heading' => $userfullname,
  82              'user' => $user,
  83              'usercontext' => $personalcontext
  84          ), 2);
  85      echo $OUTPUT->heading(get_string('statistics', 'moodle'), 2, 'main mt-4 mb-4');
  86  }
  87  
  88  // Trigger a user report viewed event.
  89  $event = \report_stats\event\user_report_viewed::create(array('context' => $coursecontext, 'relateduserid' => $user->id));
  90  $event->trigger();
  91  
  92  if (empty($CFG->enablestats)) {
  93      print_error('statsdisable', 'error');
  94  }
  95  
  96  $statsstatus = stats_check_uptodate($course->id);
  97  if ($statsstatus !== NULL) {
  98      echo $OUTPUT->notification($statsstatus);
  99  }
 100  
 101  $earliestday   = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_daily}');
 102  $earliestweek  = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_weekly}');
 103  $earliestmonth = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_monthly}');
 104  
 105  if (empty($earliestday)) {
 106      $earliestday = time();
 107  }
 108  if (empty($earliestweek)) {
 109      $earliestweek = time();
 110  }
 111  if (empty($earliestmonth)) {
 112      $earliestmonth = time();
 113  }
 114  
 115  $now = stats_get_base_daily();
 116  $lastweekend = stats_get_base_weekly();
 117  $lastmonthend = stats_get_base_monthly();
 118  
 119  $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
 120  
 121  if (empty($timeoptions)) {
 122      print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
 123  }
 124  
 125  // use the earliest.
 126  $timekeys = array_keys($timeoptions);
 127  $time = array_pop($timekeys);
 128  
 129  $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
 130  $params = $param->params;
 131  $param->table = 'user_'.$param->table;
 132  
 133  // Build the conditions and parameters.
 134  $wheres = [
 135      "userid = :userid",
 136      "timeend >= :timeend",
 137      "stattype = :stattype",
 138  ];
 139  $params['userid'] = $user->id;
 140  $params['timeend'] = $param->timeafter;
 141  $params['stattype'] = $param->stattype;
 142  // Add condition for course ID when specified.
 143  if ($course->id != SITEID) {
 144      $wheres[] = "courseid = :courseid";
 145      $params['courseid'] = $course->id;
 146  }
 147  // Combine the conditions.
 148  $wheresql = implode(" AND ", $wheres);
 149  
 150  // Build the query.
 151  $sql = "
 152      SELECT {$param->fields}
 153        FROM {stats_{$param->table}}
 154       WHERE {$wheresql}
 155      {$param->extras}
 156    ORDER BY timeend DESC";
 157  
 158  // Fetch the stats data.
 159  $stats = $DB->get_records_sql($sql, $params);
 160  
 161  if (empty($stats)) {
 162      print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
 163  }
 164  
 165  report_stats_print_chart($course->id, STATS_REPORT_USER_VIEW, $time, STATS_MODE_DETAILED, $user->id);
 166  
 167  // What the heck is this about?   -- MD
 168  $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
 169  
 170  $table = new html_table();
 171  $table->align = array('left','center','center','center');
 172  $param->table = str_replace('user_','',$param->table);
 173  switch ($param->table) {
 174      case 'daily'  : $period = get_string('day'); break;
 175      case 'weekly' : $period = get_string('week'); break;
 176      case 'monthly': $period = get_string('month', 'form'); break;
 177      default : $period = '';
 178  }
 179  $table->head = array(get_string('periodending','moodle',$period),$param->line1,$param->line2,$param->line3);
 180  foreach ($stats as $stat) {
 181      if (!empty($stat->zerofixed)) {  // Don't know why this is necessary, see stats_fix_zeros above - MD
 182          continue;
 183      }
 184      $a = array(userdate($stat->timeend - DAYSECS, get_string('strftimedate'), $CFG->timezone), $stat->line1);
 185      $a[] = $stat->line2;
 186      $a[] = $stat->line3;
 187      $table->data[] = $a;
 188  }
 189  echo html_writer::table($table);
 190  
 191  
 192  echo $OUTPUT->footer();