Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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