Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
/report/log/ -> lib.php (source)

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400]

   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              ->set_show_in_secondary_navigation(false);
 131      }
 132  }
 133  
 134  /**
 135   * Return a list of page types
 136   *
 137   * @param string $pagetype current page type
 138   * @param stdClass $parentcontext Block's parent context
 139   * @param stdClass $currentcontext Current context of block
 140   * @return array a list of page types
 141   */
 142  function report_log_page_type_list($pagetype, $parentcontext, $currentcontext) {
 143      $array = array(
 144          '*'                => get_string('page-x', 'pagetype'),
 145          'report-*'         => get_string('page-report-x', 'pagetype'),
 146          'report-log-*'     => get_string('page-report-log-x',  'report_log'),
 147          'report-log-index' => get_string('page-report-log-index',  'report_log'),
 148          'report-log-user'  => get_string('page-report-log-user',  'report_log')
 149      );
 150      return $array;
 151  }
 152  
 153  /**
 154   * Add nodes to myprofile page.
 155   *
 156   * @param \core_user\output\myprofile\tree $tree Tree object
 157   * @param stdClass $user user object
 158   * @param bool $iscurrentuser
 159   * @param stdClass $course Course object
 160   *
 161   * @return bool
 162   */
 163  function report_log_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
 164      if (empty($course)) {
 165          // We want to display these reports under the site context.
 166          $course = get_fast_modinfo(SITEID)->get_course();
 167      }
 168      list($all, $today) = report_log_can_access_user_report($user, $course);
 169      if ($today) {
 170          // Today's log.
 171          $url = new moodle_url('/report/log/user.php',
 172              array('id' => $user->id, 'course' => $course->id, 'mode' => 'today'));
 173          $node = new core_user\output\myprofile\node('reports', 'todayslogs', get_string('todaylogs'), null, $url);
 174          $tree->add_node($node);
 175      }
 176  
 177      if ($all) {
 178          // All logs.
 179          $url = new moodle_url('/report/log/user.php',
 180              array('id' => $user->id, 'course' => $course->id, 'mode' => 'all'));
 181          $node = new core_user\output\myprofile\node('reports', 'alllogs', get_string('alllogs'), null, $url);
 182          $tree->add_node($node);
 183      }
 184      return true;
 185  }