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]

   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      throw new \moodle_exception('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      throw new \moodle_exception('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      throw new \moodle_exception('nostatstodisplay', '',
 123          $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
 124  }
 125  
 126  // use the earliest.
 127  $timekeys = array_keys($timeoptions);
 128  $time = array_pop($timekeys);
 129  
 130  $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
 131  $params = $param->params;
 132  $param->table = 'user_'.$param->table;
 133  
 134  // Build the conditions and parameters.
 135  $wheres = [
 136      "userid = :userid",
 137      "timeend >= :timeend",
 138      "stattype = :stattype",
 139  ];
 140  $params['userid'] = $user->id;
 141  $params['timeend'] = $param->timeafter;
 142  $params['stattype'] = $param->stattype;
 143  // Add condition for course ID when specified.
 144  if ($course->id != SITEID) {
 145      $wheres[] = "courseid = :courseid";
 146      $params['courseid'] = $course->id;
 147  }
 148  // Combine the conditions.
 149  $wheresql = implode(" AND ", $wheres);
 150  
 151  // Build the query.
 152  $sql = "
 153      SELECT {$param->fields}
 154        FROM {stats_{$param->table}}
 155       WHERE {$wheresql}
 156      {$param->extras}
 157    ORDER BY timeend DESC";
 158  
 159  // Fetch the stats data.
 160  $stats = $DB->get_records_sql($sql, $params);
 161  
 162  if (empty($stats)) {
 163      throw new \moodle_exception('nostatstodisplay', '',
 164          $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
 165  }
 166  
 167  report_stats_print_chart($course->id, STATS_REPORT_USER_VIEW, $time, STATS_MODE_DETAILED, $user->id);
 168  
 169  // What the heck is this about?   -- MD
 170  $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
 171  
 172  $table = new html_table();
 173  $table->align = array('left','center','center','center');
 174  $param->table = str_replace('user_','',$param->table);
 175  switch ($param->table) {
 176      case 'daily'  : $period = get_string('day'); break;
 177      case 'weekly' : $period = get_string('week'); break;
 178      case 'monthly': $period = get_string('month', 'form'); break;
 179      default : $period = '';
 180  }
 181  $table->head = array(get_string('periodending','moodle',$period),$param->line1,$param->line2,$param->line3);
 182  foreach ($stats as $stat) {
 183      if (!empty($stat->zerofixed)) {  // Don't know why this is necessary, see stats_fix_zeros above - MD
 184          continue;
 185      }
 186      $a = array(userdate($stat->timeend - DAYSECS, get_string('strftimedate'), $CFG->timezone), $stat->line1);
 187      $a[] = $stat->line2;
 188      $a[] = $stat->line3;
 189      $table->data[] = $a;
 190  }
 191  echo html_writer::table($table);
 192  
 193  
 194  echo $OUTPUT->footer();