MDL-45758 tool_monitor: Create a subscription instance class
authorAnkit Agarwal <ankit@moodle.com>
Thu, 17 Jul 2014 09:37:26 +0000 (17:37 +0800)
committerAnkit Agarwal <ankit@moodle.com>
Wed, 15 Oct 2014 02:11:04 +0000 (07:41 +0530)
Original issue - MDL-46241

admin/tool/monitor/classes/subscription.php [new file with mode: 0644]
admin/tool/monitor/lang/en/tool_monitor.php

diff --git a/admin/tool/monitor/classes/subscription.php b/admin/tool/monitor/classes/subscription.php
new file mode 100644 (file)
index 0000000..4355e8c
--- /dev/null
@@ -0,0 +1,96 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Class represents a single subscription.
+ *
+ * @package    tool_monitor
+ * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace tool_monitor;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Class represents a single subscription instance (i.e with all the subscription info).
+ *
+ * @since      Moodle 2.8
+ * @package    tool_monitor
+ * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class subscription {
+    use helper_trait;
+    /**
+     * @var \stdClass
+     */
+    protected $subscription;
+
+    /**
+     * Constructor.
+     *
+     * use {@link \tool_monitor\subscription_manager::get_subscription} to get an instance instead of directly calling this method.
+     *
+     * @param \stdClass $subscription
+     */
+    public function __construct($subscription) {
+        $this->subscription = $subscription;
+    }
+
+    /**
+     * Magic get method.
+     *
+     * @param string $prop property to get.
+     *
+     * @return mixed
+     * @throws \coding_exception
+     */
+    public function __get($prop) {
+        if (property_exists($this->subscription, $prop)) {
+            return $this->subscription->$prop;
+        }
+        throw new \coding_exception('Property "' . $prop . '" doesn\'t exist');
+    }
+
+    /**
+     * Get a human readable name for instances associated with this subscription.
+     *
+     * @return string
+     * @throws \coding_exception
+     */
+    public function get_instance_name() {
+        if ($this->plugin === 'core') {
+            $string = get_string('allevents', 'tool_monitor');
+        } else {
+            if ($this->cmid == 0) {
+                $string = get_string('allmodules', 'tool_monitor');
+            } else {
+                $cms = get_fast_modinfo($this->courseid);
+                $cms = $cms->get_cms();
+                if (isset($cms[$this->cmid])) {
+                    $string = $cms[$this->cmid]->get_formatted_name(); // Instance name.
+                } else {
+                    // Something is wrong, instance is not present anymore.
+                    $string = get_string('invalidmodule', 'tool_monitor');
+                }
+            }
+        }
+
+        return $string;
+    }
+}
index 8355cd0..f9ff73e 100644 (file)
@@ -36,7 +36,7 @@ $string['defaultmessagetpl'] = 'Rule "{rulename}" has happened. You can find fur
 $string['eventnotfound'] = 'Event not found';
 $string['errorincorrectevent'] = 'Please select an event related to the selected plugin';
 $string['frequency'] = 'Frequency';
-$string['freqdesc'] = '{$a->freq} times in {$a->mins} minutes';
+$string['invalidmodule'] = 'Invalid module';
 $string['manage'] = 'Manage';
 $string['managesubscriptions'] = 'Manage subscriptions';
 $string['managerules'] = 'Manage rules';