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.
   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   * Read actions indicator.
  19   *
  20   * @package   core
  21   * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\analytics\indicator;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Read actions indicator.
  31   *
  32   * @package   core
  33   * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class read_actions extends \core_analytics\local\indicator\linear {
  37  
  38      /**
  39       * Returns the name.
  40       *
  41       * If there is a corresponding '_help' string this will be shown as well.
  42       *
  43       * @return \lang_string
  44       */
  45      public static function get_name() : \lang_string {
  46          return new \lang_string('indicator:readactions');
  47      }
  48  
  49      /**
  50       * required_sample_data
  51       *
  52       * @return string[]
  53       */
  54      public static function required_sample_data() {
  55          // User is not required, calculate_sample can handle its absence.
  56          return array('context');
  57      }
  58  
  59      /**
  60       * calculate_sample
  61       *
  62       * @param int $sampleid
  63       * @param string $sampleorigin
  64       * @param int $starttime
  65       * @param int $endtime
  66       * @return float
  67       */
  68      protected function calculate_sample($sampleid, $sampleorigin, $starttime = false, $endtime = false) {
  69  
  70          if (!$starttime || !$endtime) {
  71              return null;
  72          }
  73  
  74          $select = '';
  75          $params = array();
  76  
  77          if ($user = $this->retrieve('user', $sampleid)) {
  78              $select .= "userid = :userid AND ";
  79              $params = $params + array('userid' => $user->id);
  80          }
  81  
  82          if (!$logstore = \core_analytics\manager::get_analytics_logstore()) {
  83              throw new \coding_exception('No available log stores');
  84          }
  85  
  86          // Filter by context to use the logstore_standard_log db table index.
  87          $context = $this->retrieve('context', $sampleid);
  88          $select .= "contextlevel = :contextlevel AND contextinstanceid = :contextinstanceid AND " .
  89              "crud = 'r' AND timecreated > :starttime AND timecreated <= :endtime";
  90          $params = $params + array('contextlevel' => $context->contextlevel,
  91              'contextinstanceid' => $context->instanceid, 'starttime' => $starttime, 'endtime' => $endtime);
  92          $nrecords = $logstore->get_events_select_count($select, $params);
  93          // We define a list of ranges to fit $nrecords into it
  94          // # Done absolutely nothing
  95          // # Not much really, just accessing the course once a week
  96          // # More than just accessing the course, some interaction
  97          // # Significant contribution, will depend on the course anyway.
  98  
  99          // We need to adapt the limits to the time range duration.
 100          $nweeks = $this->get_time_range_weeks_number($starttime, $endtime);
 101  
 102          // Careful with this, depends on the course.
 103          $limit = $nweeks * 3 * 10;
 104          $ranges = array(
 105              array('eq', 0),
 106              // 1 course access per week (3 records are easily generated).
 107              array('le', $nweeks * 3),
 108              // 3 course accesses per week doing some stuff.
 109              array('le', $limit),
 110              array('gt', $limit)
 111          );
 112          return $this->classify_value($nrecords, $ranges);
 113      }
 114  }