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.

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

   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 page lists public api for tool_monitor plugin.
  19   *
  20   * @package    tool_monitor
  21   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die;
  26  
  27  /**
  28   * This function extends the navigation with the tool items
  29   *
  30   * @param navigation_node $navigation The navigation node to extend
  31   * @param stdClass        $course     The course to object for the tool
  32   * @param context         $context    The context of the course
  33   */
  34  function tool_monitor_extend_navigation_course($navigation, $course, $context) {
  35      if (has_capability('tool/monitor:managerules', $context) && get_config('tool_monitor', 'enablemonitor')) {
  36          $url = new moodle_url('/admin/tool/monitor/managerules.php', array('courseid' => $course->id));
  37          $settingsnode = navigation_node::create(get_string('managerules', 'tool_monitor'), $url, navigation_node::TYPE_SETTING,
  38                  null, null, new pix_icon('i/settings', ''));
  39          $reportnode = $navigation->get('coursereports');
  40  
  41          if (isset($settingsnode) && !empty($reportnode)) {
  42              $reportnode->add_node($settingsnode);
  43          }
  44      }
  45  }
  46  
  47  /**
  48   * This function extends the navigation with the tool items
  49   *
  50   * @param navigation_node $navigation The navigation node to extend
  51   * @param stdClass        $course     The course to object for the tool
  52   * @param context         $context    The context of the course
  53   */
  54  function tool_monitor_extend_navigation_frontpage($navigation, $course, $context) {
  55  
  56      if (has_capability('tool/monitor:managerules', $context)) {
  57          $url = new moodle_url('/admin/tool/monitor/managerules.php', array('courseid' => $course->id));
  58          $settingsnode = navigation_node::create(get_string('managerules', 'tool_monitor'), $url, navigation_node::TYPE_SETTING,
  59                  null, null, new pix_icon('i/settings', ''));
  60          $reportnode = $navigation->get('coursereports');
  61  
  62          if (isset($settingsnode) && !empty($reportnode)) {
  63              $reportnode->add_node($settingsnode);
  64          }
  65      }
  66  }
  67  
  68  /**
  69   * This function extends the navigation with the tool items for user settings node.
  70   *
  71   * @param navigation_node $navigation  The navigation node to extend
  72   * @param stdClass        $user        The user object
  73   * @param context         $usercontext The context of the user
  74   * @param stdClass        $course      The course to object for the tool
  75   * @param context         $coursecontext     The context of the course
  76   */
  77  function tool_monitor_extend_navigation_user_settings($navigation, $user, $usercontext, $course, $coursecontext) {
  78      global $USER, $PAGE;
  79  
  80      // Don't bother doing needless calculations unless we are on the relevant pages.
  81      $onpreferencepage = $PAGE->url->compare(new moodle_url('/user/preferences.php'), URL_MATCH_BASE);
  82      $onmonitorpage = $PAGE->url->compare(new moodle_url('/admin/tool/monitor/index.php'), URL_MATCH_BASE);
  83      if (!$onpreferencepage && !$onmonitorpage) {
  84          return null;
  85      }
  86  
  87      // Don't show the setting if the event monitor isn't turned on. No access to other peoples subscriptions.
  88      if (get_config('tool_monitor', 'enablemonitor') && $USER->id == $user->id) {
  89          // Now let's check to see if the user has any courses / site rules that they can subscribe to.
  90          // We skip doing a check here if we are on the event monitor page as the check is done internally on that page.
  91          if ($onmonitorpage || tool_monitor_can_subscribe()) {
  92              $url = new moodle_url('/admin/tool/monitor/index.php');
  93              $subsnode = navigation_node::create(get_string('managesubscriptions', 'tool_monitor'), $url,
  94                      navigation_node::TYPE_SETTING, null, 'monitor', new pix_icon('i/settings', ''));
  95  
  96              if (isset($subsnode) && !empty($navigation)) {
  97                  $navigation->add_node($subsnode);
  98              }
  99          }
 100      }
 101  }
 102  
 103  /**
 104   * Check if the user has the capacity to subscribe to an event monitor anywhere.
 105   *
 106   * @return bool True if a capability in a course is found. False otherwise.
 107   */
 108  function tool_monitor_can_subscribe() {
 109      if (has_capability('tool/monitor:subscribe', context_system::instance())) {
 110          return true;
 111      }
 112      $courses = get_user_capability_course('tool/monitor:subscribe', null, true, '', '', 1);
 113      return empty($courses) ? false : true;
 114  }
 115  
 116  /**
 117   * Get a list of courses and also include 'Site' for site wide rules.
 118   *
 119   * @return array|bool Returns an array of courses or false if the user has no permission to subscribe to rules.
 120   */
 121  function tool_monitor_get_user_courses() {
 122      // Get the course sorting according to the admin settings.
 123      $sort = enrol_get_courses_sortingsql();
 124  
 125      $options = array();
 126      if (has_capability('tool/monitor:subscribe', context_system::instance())) {
 127          $options[0] = get_string('site');
 128      }
 129  
 130      $fieldlist = array_merge(
 131              [
 132                  'fullname',
 133                  'visible',
 134              ],
 135              array_values(context_helper::get_preload_record_columns('c'))
 136          );
 137  
 138      $fields = implode(', ', $fieldlist);
 139      if ($courses = get_user_capability_course('tool/monitor:subscribe', null, true, $fields, $sort)) {
 140          foreach ($courses as $course) {
 141              context_helper::preload_from_record($course);
 142              $coursectx = context_course::instance($course->id);
 143              if ($course->visible || has_capability('moodle/course:viewhiddencourses', $coursectx)) {
 144                  $options[$course->id] = format_string($course->fullname, true, array('context' => $coursectx));
 145              }
 146          }
 147      }
 148      // If there are no courses and there is no site permission then return false.
 149      if (count($options) < 1) {
 150          return false;
 151      } else {
 152          return $options;
 153      }
 154  }