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 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   * This file contains functions used by the log reports
  19   *
  20   * This file is also required by /admin/reports/stats/index.php.
  21   *
  22   * @package    report
  23   * @subpackage stats
  24   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die;
  29  
  30  /**
  31   * This function extends the navigation with the report items
  32   *
  33   * @param navigation_node $navigation The navigation node to extend
  34   * @param stdClass $course The course to object for the report
  35   * @param stdClass $context The context of the course
  36   */
  37  function report_stats_extend_navigation_course($navigation, $course, $context) {
  38      global $CFG;
  39      if (empty($CFG->enablestats)) {
  40          return;
  41      }
  42      if (has_capability('report/stats:view', $context)) {
  43          $url = new moodle_url('/report/stats/index.php', array('course'=>$course->id));
  44          $navigation->add(get_string('pluginname', 'report_stats'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', ''));
  45      }
  46  }
  47  
  48  /**
  49   * This function extends the course navigation with the report items
  50   *
  51   * @param navigation_node $navigation The navigation node to extend
  52   * @param stdClass $user
  53   * @param stdClass $course The course to object for the report
  54   */
  55  function report_stats_extend_navigation_user($navigation, $user, $course) {
  56      global $CFG;
  57      if (empty($CFG->enablestats)) {
  58          return;
  59      }
  60      if (report_stats_can_access_user_report($user, $course)) {
  61          $url = new moodle_url('/report/stats/user.php', array('id'=>$user->id, 'course'=>$course->id));
  62          $navigation->add(get_string('stats'), $url);
  63      }
  64  }
  65  
  66  /**
  67   * Is current user allowed to access this report
  68   *
  69   * @private defined in lib.php for performance reasons
  70   *
  71   * @param stdClass $user
  72   * @param stdClass $course
  73   * @return bool
  74   */
  75  function report_stats_can_access_user_report($user, $course) {
  76      global $USER;
  77  
  78      $coursecontext = context_course::instance($course->id);
  79      $personalcontext = context_user::instance($user->id);
  80  
  81      if ($user->id == $USER->id) {
  82          if ($course->showreports and (is_viewing($coursecontext, $USER) or is_enrolled($coursecontext, $USER))) {
  83              return true;
  84          }
  85      } else if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)) {
  86          if ($course->showreports and (is_viewing($coursecontext, $user) or is_enrolled($coursecontext, $user))) {
  87              return true;
  88          }
  89      }
  90  
  91      // Check if $USER shares group with $user (in case separated groups are enabled and 'moodle/site:accessallgroups' is disabled).
  92      if (!groups_user_groups_visible($course, $user->id)) {
  93          return false;
  94      }
  95  
  96      if (has_capability('report/stats:view', $coursecontext)) {
  97          return true;
  98      }
  99  
 100      return false;
 101  }
 102  
 103  /**
 104   * Return a list of page types
 105   * @param string $pagetype current page type
 106   * @param stdClass $parentcontext Block's parent context
 107   * @param stdClass $currentcontext Current context of block
 108   * @return array
 109   */
 110  function report_stats_page_type_list($pagetype, $parentcontext, $currentcontext) {
 111      $array = array(
 112          '*'                  => get_string('page-x', 'pagetype'),
 113          'report-*'           => get_string('page-report-x', 'pagetype'),
 114          'report-stats-*'     => get_string('page-report-stats-x',  'report_stats'),
 115          'report-stats-index' => get_string('page-report-stats-index',  'report_stats'),
 116          'report-stats-user'  => get_string('page-report-stats-user',  'report_stats')
 117      );
 118      return $array;
 119  }
 120  
 121  /**
 122   * Callback to verify if the given instance of store is supported by this report or not.
 123   *
 124   * @param string $instance store instance.
 125   *
 126   * @return bool returns true if the store is supported by the report, false otherwise.
 127   */
 128  function report_stats_supports_logstore($instance) {
 129      if ($instance instanceof \core\log\sql_internal_table_reader || $instance instanceof \logstore_legacy\log\store) {
 130          return true;
 131      }
 132      return false;
 133  }
 134  
 135  /**
 136   * Add nodes to myprofile page.
 137   *
 138   * @param \core_user\output\myprofile\tree $tree Tree object
 139   * @param stdClass $user user object
 140   * @param bool $iscurrentuser
 141   * @param stdClass $course Course object
 142   * @return bool
 143   */
 144  function report_stats_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
 145      global $CFG;
 146      if (empty($CFG->enablestats)) {
 147          return false;
 148      }
 149      if (empty($course)) {
 150          // We want to display these reports under the site context.
 151          $course = get_fast_modinfo(SITEID)->get_course();
 152      }
 153      if (report_stats_can_access_user_report($user, $course)) {
 154          $url = new moodle_url('/report/stats/user.php', array('id' => $user->id, 'course' => $course->id));
 155          $node = new core_user\output\myprofile\node('reports', 'stats', get_string('stats'), null, $url);
 156          $tree->add_node($node);
 157      }
 158  }