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.
   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   * Class represents a single subscription.
  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  namespace tool_monitor;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Class represents a single subscription instance (i.e with all the subscription info).
  31   *
  32   * @since      Moodle 2.8
  33   * @package    tool_monitor
  34   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class subscription {
  38      /**
  39       * @var \stdClass
  40       */
  41      protected $subscription;
  42  
  43      /**
  44       * Constructor.
  45       *
  46       * use {@link \tool_monitor\subscription_manager::get_subscription} to get an instance instead of directly calling this method.
  47       *
  48       * @param \stdClass $subscription
  49       */
  50      public function __construct($subscription) {
  51          $this->subscription = $subscription;
  52      }
  53  
  54      /**
  55       * Magic get method.
  56       *
  57       * @param string $prop property to get.
  58       * @return mixed
  59       * @throws \coding_exception
  60       */
  61      public function __get($prop) {
  62          if (isset($this->subscription->$prop)) {
  63              return $this->subscription->$prop;
  64          }
  65          throw new \coding_exception('Property "' . $prop . '" doesn\'t exist');
  66      }
  67  
  68      /**
  69       * Magic isset method.
  70       *
  71       * @param string $prop the property to get.
  72       * @return bool true if the property is set, false otherwise.
  73       */
  74      public function __isset($prop) {
  75          return property_exists($this->subscription, $prop);
  76      }
  77  
  78      /**
  79       * Get a human readable name for instances associated with this subscription.
  80       *
  81       * @return string
  82       * @throws \coding_exception
  83       */
  84      public function get_instance_name() {
  85          if ($this->plugin === 'core') {
  86              $string = get_string('allevents', 'tool_monitor');
  87          } else {
  88              if ($this->cmid == 0) {
  89                  $string = get_string('allmodules', 'tool_monitor');
  90              } else {
  91                  $cms = get_fast_modinfo($this->courseid);
  92                  $cms = $cms->get_cms();
  93                  if (isset($cms[$this->cmid])) {
  94                      $string = $cms[$this->cmid]->get_formatted_name(); // Instance name.
  95                  } else {
  96                      // Something is wrong, instance is not present anymore.
  97                      $string = get_string('invalidmodule', 'tool_monitor');
  98                  }
  99              }
 100          }
 101  
 102          return $string;
 103      }
 104  
 105      /**
 106       * Method to get event name.
 107       *
 108       * @return string
 109       * @throws \coding_exception
 110       */
 111      public function get_event_name() {
 112          $eventclass = $this->eventname;
 113          if (class_exists($eventclass)) {
 114              return $eventclass::get_name_with_info();
 115          }
 116          return get_string('eventnotfound', 'tool_monitor');
 117      }
 118  
 119      /**
 120       * Get filter description.
 121       *
 122       * @return string
 123       */
 124      public function get_filters_description() {
 125          $a = new \stdClass();
 126          $a->freq = $this->frequency;
 127          $mins = $this->timewindow / MINSECS; // Convert seconds to minutes.
 128          $a->mins = $mins;
 129          return get_string('freqdesc', 'tool_monitor', $a);
 130      }
 131  
 132      /**
 133       * Get properly formatted name of the rule associated.
 134       *
 135       * @param \context $context context where this name would be displayed.
 136       * @return string Formatted name of the rule.
 137       */
 138      public function get_name(\context $context) {
 139          return format_text($this->name, FORMAT_HTML, array('context' => $context));
 140      }
 141  
 142      /**
 143       * Get properly formatted description of the rule associated.
 144       *
 145       * @param \context $context context where this description would be displayed.
 146       * @return string Formatted description of the rule.
 147       */
 148      public function get_description(\context $context) {
 149          return format_text($this->description, $this->descriptionformat, array('context' => $context));
 150      }
 151  
 152      /**
 153       * Get name of the plugin associated with this rule
 154       *
 155       * @return string Plugin name.
 156       */
 157      public function get_plugin_name() {
 158          if ($this->plugin === 'core') {
 159              $string = get_string('core', 'tool_monitor');
 160          } else if (get_string_manager()->string_exists('pluginname', $this->plugin)) {
 161              $string = get_string('pluginname', $this->plugin);
 162          } else {
 163              $string = $this->plugin;
 164          }
 165          return $string;
 166      }
 167  
 168      /**
 169       * Get properly formatted name of the course associated.
 170       *
 171       * @param \context $context context where this name would be displayed.
 172       * @return string Formatted name of the rule.
 173       */
 174      public function get_course_name(\context $context) {
 175          $courseid = $this->courseid;
 176          if (empty($courseid)) {
 177              return get_string('site');
 178          } else {
 179              try {
 180                  $course = get_course($courseid);
 181                  return format_string($course->fullname, true, array('context' => $context));
 182              } catch (\dml_exception $e) {
 183                  return '-';
 184              }
 185          }
 186      }
 187  
 188      /**
 189       * Can the current user manage the rule associate with this subscription?
 190       *
 191       * @return bool true if the current user can manage this rule, else false.
 192       */
 193      public function can_manage_rule() {
 194          $courseid = $this->rulecourseid;
 195          $context = empty($courseid) ? \context_system::instance() : \context_course::instance($courseid);
 196          return has_capability('tool/monitor:managerules', $context);
 197      }
 198  }