Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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      echo $OUTPUT->context_header(
  77              array(
  78              'heading' => $userfullname,
  79              'user' => $user,
  80              'usercontext' => $personalcontext
  81          ), 2);
  82  }
  83  
  84  // Trigger a user report viewed event.
  85  $event = \report_stats\event\user_report_viewed::create(array('context' => $coursecontext, 'relateduserid' => $user->id));
  86  $event->trigger();
  87  
  88  if (empty($CFG->enablestats)) {
  89      print_error('statsdisable', 'error');
  90  }
  91  
  92  $statsstatus = stats_check_uptodate($course->id);
  93  if ($statsstatus !== NULL) {
  94      echo $OUTPUT->notification($statsstatus);
  95  }
  96  
  97  $earliestday   = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_daily}');
  98  $earliestweek  = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_weekly}');
  99  $earliestmonth = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_monthly}');
 100  
 101  if (empty($earliestday)) {
 102      $earliestday = time();
 103  }
 104  if (empty($earliestweek)) {
 105      $earliestweek = time();
 106  }
 107  if (empty($earliestmonth)) {
 108      $earliestmonth = time();
 109  }
 110  
 111  $now = stats_get_base_daily();
 112  $lastweekend = stats_get_base_weekly();
 113  $lastmonthend = stats_get_base_monthly();
 114  
 115  $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
 116  
 117  if (empty($timeoptions)) {
 118      print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
 119  }
 120  
 121  // use the earliest.
 122  $timekeys = array_keys($timeoptions);
 123  $time = array_pop($timekeys);
 124  
 125  $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
 126  $params = $param->params;
 127  $param->table = 'user_'.$param->table;
 128  
 129  // Build the conditions and parameters.
 130  $wheres = [
 131      "userid = :userid",
 132      "timeend >= :timeend",
 133      "stattype = :stattype",
 134  ];
 135  $params['userid'] = $user->id;
 136  $params['timeend'] = $param->timeafter;
 137  $params['stattype'] = $param->stattype;
 138  // Add condition for course ID when specified.
 139  if ($course->id != SITEID) {
 140      $wheres[] = "courseid = :courseid";
 141      $params['courseid'] = $course->id;
 142  }
 143  // Combine the conditions.
 144  $wheresql = implode(" AND ", $wheres);
 145  
 146  // Build the query.
 147  $sql = "
 148      SELECT {$param->fields}
 149        FROM {stats_{$param->table}}
 150       WHERE {$wheresql}
 151      {$param->extras}
 152    ORDER BY timeend DESC";
 153  
 154  // Fetch the stats data.
 155  $stats = $DB->get_records_sql($sql, $params);
 156  
 157  if (empty($stats)) {
 158      print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
 159  }
 160  
 161  report_stats_print_chart($course->id, STATS_REPORT_USER_VIEW, $time, STATS_MODE_DETAILED, $user->id);
 162  
 163  // What the heck is this about?   -- MD
 164  $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
 165  
 166  $table = new html_table();
 167  $table->align = array('left','center','center','center');
 168  $param->table = str_replace('user_','',$param->table);
 169  switch ($param->table) {
 170      case 'daily'  : $period = get_string('day'); break;
 171      case 'weekly' : $period = get_string('week'); break;
 172      case 'monthly': $period = get_string('month', 'form'); break;
 173      default : $period = '';
 174  }
 175  $table->head = array(get_string('periodending','moodle',$period),$param->line1,$param->line2,$param->line3);
 176  foreach ($stats as $stat) {
 177      if (!empty($stat->zerofixed)) {  // Don't know why this is necessary, see stats_fix_zeros above - MD
 178          continue;
 179      }
 180      $a = array(userdate($stat->timeend - DAYSECS, get_string('strftimedate'), $CFG->timezone), $stat->line1);
 181      $a[] = $stat->line2;
 182      $a[] = $stat->line3;
 183      $table->data[] = $a;
 184  }
 185  echo html_writer::table($table);
 186  
 187  
 188  echo $OUTPUT->footer();