subscription.php 6.1 KB

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