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.
/report/log/ -> lib.php (source)

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