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