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.
   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   * Activities due indicator.
  19   *
  20   * @package   core
  21   * @copyright 2019 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_course\analytics\indicator;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->dirroot . '/calendar/externallib.php');
  30  
  31  /**
  32   * Activities due indicator.
  33   *
  34   * @package   core
  35   * @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class activities_due extends \core_analytics\local\indicator\binary {
  39  
  40      /**
  41       * Returns the name.
  42       *
  43       * If there is a corresponding '_help' string this will be shown as well.
  44       *
  45       * @return \lang_string
  46       */
  47      public static function get_name() : \lang_string {
  48          return new \lang_string('indicator:activitiesdue');
  49      }
  50  
  51      /**
  52       * required_sample_data
  53       *
  54       * @return string[]
  55       */
  56      public static function required_sample_data() {
  57          return array('user');
  58      }
  59  
  60      /**
  61       * calculate_sample
  62       *
  63       * @param int $sampleid
  64       * @param string $sampleorigin
  65       * @param int $starttime
  66       * @param int $endtime
  67       * @return float
  68       */
  69      protected function calculate_sample($sampleid, $sampleorigin, $starttime = false, $endtime = false) {
  70  
  71          $user = $this->retrieve('user', $sampleid);
  72  
  73          $actionevents = \core_calendar_external::get_calendar_action_events_by_timesort($starttime, $endtime, 0, 1,
  74              true, $user->id);
  75  
  76          $useractionevents = [];
  77          if ($actionevents->events) {
  78  
  79              // We first need to check that at least one of the core_calendar_provide_event_action
  80              // callbacks has the $userid param.
  81              foreach ($actionevents->events as $event) {
  82                  $nparams = $this->get_provide_event_action_num_params($event->modulename);
  83                  if ($nparams > 2) {
  84                      // Just the basic info for the insight as we want a low memory usage.
  85                      $useractionevents[$event->id] = (object)[
  86                          'name' => $event->name,
  87                          'url' => $event->url,
  88                          'time' => $event->timesort,
  89                          'coursename' => $event->course->fullnamedisplay,
  90                          'icon' => $event->icon,
  91                      ];
  92                  }
  93              }
  94  
  95              if (!empty($useractionevents)) {
  96                  $this->add_shared_calculation_info($sampleid, $useractionevents);
  97                  return self::get_max_value();
  98              }
  99          }
 100  
 101          return self::get_min_value();
 102      }
 103  
 104      /**
 105       * Returns the number of params declared in core_calendar_provide_event_action's implementation.
 106       *
 107       * @param  string $modulename The module name
 108       * @return int
 109       */
 110      private function get_provide_event_action_num_params(string $modulename) {
 111          $functionname = 'mod_' . $modulename . '_core_calendar_provide_event_action';
 112          $reflection = new \ReflectionFunction($functionname);
 113          return $reflection->getNumberOfParameters();
 114      }
 115  }