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]

   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 outline
  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  use core\report_helper;
  27  
  28  require('../../config.php');
  29  require_once($CFG->dirroot.'/report/outline/locallib.php');
  30  
  31  $id = required_param('id',PARAM_INT);       // course id
  32  $startdate = optional_param('startdate', null, PARAM_INT);
  33  $enddate = optional_param('enddate', null, PARAM_INT);
  34  
  35  $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
  36  
  37  $pageparams = array('id' => $id);
  38  if ($startdate) {
  39      $pageparams['startdate'] = $startdate;
  40  }
  41  if ($enddate) {
  42      $pageparams['enddate'] = $enddate;
  43  }
  44  
  45  $PAGE->set_url('/report/outline/index.php', $pageparams);
  46  $PAGE->set_pagelayout('report');
  47  
  48  require_login($course);
  49  $context = context_course::instance($course->id);
  50  require_capability('report/outline:view', $context);
  51  
  52  // Handle form to filter access logs by date.
  53  $filterform = new \report_outline\filter_form();
  54  $filterform->set_data(['id' => $course->id, 'filterstartdate' => $startdate, 'filterenddate' => $enddate]);
  55  if ($filterform->is_cancelled()) {
  56      $redir = $PAGE->url;
  57      $redir->remove_params(['startdate', 'enddate']);
  58      redirect($redir);
  59  }
  60  if ($filter = $filterform->get_data()) {
  61      $redir = $PAGE->url;
  62      if ($filter->filterstartdate) {
  63          $redir->param('startdate', $filter->filterstartdate);
  64      }
  65      if ($filter->filterenddate) {
  66          $redir->param('enddate', $filter->filterenddate);
  67      }
  68      redirect($redir);
  69  }
  70  
  71  // Trigger an activity report viewed event.
  72  $event = \report_outline\event\activity_report_viewed::create(array('context' => $context));
  73  $event->trigger();
  74  
  75  $showlastaccess = true;
  76  $hiddenfields = explode(',', $CFG->hiddenuserfields);
  77  
  78  if (array_search('lastaccess', $hiddenfields) !== false and !has_capability('moodle/user:viewhiddendetails', $context)) {
  79      $showlastaccess = false;
  80  }
  81  
  82  $stractivityreport = get_string('pluginname', 'report_outline');
  83  $stractivity       = get_string('activity');
  84  $strlast           = get_string('lastaccess');
  85  $strreports        = get_string('reports');
  86  $strviews          = get_string('views');
  87  $strrelatedblogentries = get_string('relatedblogentries', 'blog');
  88  
  89  $PAGE->set_title($course->shortname .': '. $stractivityreport);
  90  $PAGE->set_heading($course->fullname);
  91  echo $OUTPUT->header();
  92  
  93  // Print selector drop down.
  94  $pluginname = get_string('pluginname', 'report_outline');
  95  report_helper::print_report_selector($pluginname);
  96  
  97  list($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable) = report_outline_get_common_log_variables();
  98  
  99  // If no legacy and no internal log then don't proceed.
 100  if (!$uselegacyreader && !$useinternalreader) {
 101      echo $OUTPUT->box_start('generalbox', 'notice');
 102      echo $OUTPUT->notification(get_string('nologreaderenabled', 'report_outline'));
 103      echo $OUTPUT->box_end();
 104      echo $OUTPUT->footer();
 105      die();
 106  }
 107  
 108  // We want to display the time we are beginning to get logs from in the heading.
 109  // If we are using the legacy reader check the minimum time in that log table.
 110  if ($uselegacyreader) {
 111      $minlog = $DB->get_field_sql('SELECT min(time) FROM {log}');
 112  }
 113  
 114  // If we are using the internal reader check the minimum time in that table.
 115  if ($useinternalreader) {
 116      // If new log table has older data then don't use the minimum time obtained from the legacy table.
 117      if (empty($minlog) || ($minloginternalreader <= $minlog)) {
 118          $minlog = $minloginternalreader;
 119      }
 120  }
 121  
 122  $filterform->display();
 123  
 124  echo $OUTPUT->container(get_string('computedfromlogs', 'admin', userdate($minlog)), 'loginfo');
 125  
 126  $outlinetable = new html_table();
 127  $outlinetable->attributes['class'] = 'generaltable boxaligncenter';
 128  $outlinetable->cellpadding = 5;
 129  $outlinetable->id = 'outlinetable';
 130  $outlinetable->head = array($stractivity, $strviews);
 131  
 132  if (!empty($CFG->enableblogs) && $CFG->useblogassociations) {
 133      $outlinetable->head[] = $strrelatedblogentries;
 134  }
 135  
 136  if ($showlastaccess) {
 137      $outlinetable->head[] = $strlast;
 138  }
 139  
 140  $modinfo = get_fast_modinfo($course);
 141  
 142  // If using legacy log then get users from old table.
 143  if ($uselegacyreader) {
 144      // If we are going to use the internal (not legacy) log table, we should only get records
 145      // from the legacy table that exist before we started adding logs to the new table.
 146      $params = array('courseid' => $course->id, 'action' => 'view%', 'visible' => 1);
 147      $limittime = '';
 148      if (!empty($minloginternalreader)) {
 149          $limittime = ' AND time < :timeto ';
 150          $params['timeto'] = $minloginternalreader;
 151      }
 152      if ($startdate) {
 153          $limittime .= ' AND time >= :startdate ';
 154          $params['startdate'] = $startdate;
 155      }
 156      if ($enddate) {
 157          $limittime .= ' AND time < :enddate ';
 158          $params['enddate'] = $enddate;
 159      }
 160      // Check if we need to show the last access.
 161      $sqllasttime = '';
 162      if ($showlastaccess) {
 163          $sqllasttime = ", MAX(time) AS lasttime";
 164      }
 165      $logactionlike = $DB->sql_like('l.action', ':action');
 166      $sql = "SELECT cm.id, COUNT('x') AS numviews, COUNT(DISTINCT userid) AS distinctusers $sqllasttime
 167                FROM {course_modules} cm
 168                JOIN {modules} m
 169                  ON m.id = cm.module
 170                JOIN {log} l
 171                  ON l.cmid = cm.id
 172               WHERE cm.course = :courseid
 173                 AND $logactionlike
 174                 AND m.visible = :visible $limittime
 175            GROUP BY cm.id";
 176      $views = $DB->get_records_sql($sql, $params);
 177  }
 178  
 179  // Get record from sql_internal_table_reader and merge with records obtained from legacy log (if needed).
 180  if ($useinternalreader) {
 181      // Check if we need to show the last access.
 182      $sqllasttime = '';
 183      if ($showlastaccess) {
 184          $sqllasttime = ", MAX(timecreated) AS lasttime";
 185      }
 186      $params = array('courseid' => $course->id, 'contextmodule' => CONTEXT_MODULE);
 187      $limittime = '';
 188      if ($startdate) {
 189          $limittime .= ' AND timecreated >= :startdate ';
 190          $params['startdate'] = $startdate;
 191      }
 192      if ($enddate) {
 193          $limittime .= ' AND timecreated < :enddate ';
 194          $params['enddate'] = $enddate;
 195      }
 196      $sql = "SELECT contextinstanceid as cmid, COUNT('x') AS numviews, COUNT(DISTINCT userid) AS distinctusers $sqllasttime
 197                FROM {" . $logtable . "} l
 198               WHERE courseid = :courseid
 199                 AND anonymous = 0
 200                 AND crud = 'r'
 201                 AND contextlevel = :contextmodule
 202                 $limittime
 203            GROUP BY contextinstanceid";
 204      $v = $DB->get_records_sql($sql, $params);
 205  
 206      if (empty($views)) {
 207          $views = $v;
 208      } else {
 209          // Merge two view arrays.
 210          foreach ($v as $key => $value) {
 211              if (isset($views[$key]) && !empty($views[$key]->numviews)) {
 212                  $views[$key]->numviews += $value->numviews;
 213                  if ($value->lasttime > $views[$key]->lasttime) {
 214                      $views[$key]->lasttime = $value->lasttime;
 215                  }
 216              } else {
 217                  $views[$key] = $value;
 218              }
 219          }
 220      }
 221  }
 222  
 223  $prevsecctionnum = 0;
 224  foreach ($modinfo->sections as $sectionnum=>$section) {
 225      foreach ($section as $cmid) {
 226          $cm = $modinfo->cms[$cmid];
 227          if (!$cm->has_view()) {
 228              continue;
 229          }
 230          if (!$cm->uservisible) {
 231              continue;
 232          }
 233          if ($prevsecctionnum != $sectionnum) {
 234              $sectionrow = new html_table_row();
 235              $sectionrow->attributes['class'] = 'section';
 236              $sectioncell = new html_table_cell();
 237              $sectioncell->colspan = count($outlinetable->head);
 238  
 239              $sectiontitle = get_section_name($course, $sectionnum);
 240  
 241              $sectioncell->text = $OUTPUT->heading($sectiontitle, 3);
 242              $sectionrow->cells[] = $sectioncell;
 243              $outlinetable->data[] = $sectionrow;
 244  
 245              $prevsecctionnum = $sectionnum;
 246          }
 247  
 248          $dimmed = $cm->visible ? '' : 'class="dimmed"';
 249          $modulename = get_string('modulename', $cm->modname);
 250  
 251          $reportrow = new html_table_row();
 252          $activitycell = new html_table_cell();
 253          $activitycell->attributes['class'] = 'activity';
 254  
 255          $activityicon = $OUTPUT->pix_icon('monologo', $modulename, $cm->modname, array('class'=>'icon'));
 256  
 257          $attributes = array();
 258          if (!$cm->visible) {
 259              $attributes['class'] = 'dimmed';
 260          }
 261  
 262          $activitycell->text = $activityicon . html_writer::link("$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id", format_string($cm->name), $attributes);
 263  
 264          $reportrow->cells[] = $activitycell;
 265  
 266          $numviewscell = new html_table_cell();
 267          $numviewscell->attributes['class'] = 'numviews';
 268  
 269          if (!empty($views[$cm->id]->numviews)) {
 270              $numviewscell->text = get_string('numviews', 'report_outline', $views[$cm->id]);
 271          } else {
 272              $numviewscell->text = '-';
 273          }
 274  
 275          $reportrow->cells[] = $numviewscell;
 276  
 277          if (!empty($CFG->enableblogs) && $CFG->useblogassociations) {
 278              require_once($CFG->dirroot.'/blog/lib.php');
 279              $blogcell = new html_table_cell();
 280              $blogcell->attributes['class'] = 'blog';
 281              if ($blogcount = blog_get_associated_count($course->id, $cm->id)) {
 282                  $blogurl = new moodle_url('/blog/index.php', array('modid' => $cm->id));
 283                  $blogcell->text = html_writer::link($blogurl, $blogcount);
 284              } else {
 285                  $blogcell->text = '-';
 286              }
 287              $reportrow->cells[] = $blogcell;
 288          }
 289  
 290          if ($showlastaccess) {
 291              $lastaccesscell = new html_table_cell();
 292              $lastaccesscell->attributes['class'] = 'lastaccess';
 293  
 294              if (isset($views[$cm->id]->lasttime)) {
 295                  $timeago = format_time(time() - $views[$cm->id]->lasttime);
 296                  $lastaccesscell->text = userdate($views[$cm->id]->lasttime)." ($timeago)";
 297              }
 298              $reportrow->cells[] = $lastaccesscell;
 299          }
 300          $outlinetable->data[] = $reportrow;
 301      }
 302  }
 303  echo html_writer::table($outlinetable);
 304  
 305  echo $OUTPUT->footer();
 306  
 307  
 308