source: moodle/trunk/fuentes/admin/tool/monitor/classes/subscription.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 5.8 KB
Line 
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
25namespace tool_monitor;
26
27defined('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 */
37class 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     *
59     * @return mixed
60     * @throws \coding_exception
61     */
62    public function __get($prop) {
63        if (property_exists($this->subscription, $prop)) {
64            return $this->subscription->$prop;
65        }
66        throw new \coding_exception('Property "' . $prop . '" doesn\'t exist');
67    }
68
69    /**
70     * Get a human readable name for instances associated with this subscription.
71     *
72     * @return string
73     * @throws \coding_exception
74     */
75    public function get_instance_name() {
76        if ($this->plugin === 'core') {
77            $string = get_string('allevents', 'tool_monitor');
78        } else {
79            if ($this->cmid == 0) {
80                $string = get_string('allmodules', 'tool_monitor');
81            } else {
82                $cms = get_fast_modinfo($this->courseid);
83                $cms = $cms->get_cms();
84                if (isset($cms[$this->cmid])) {
85                    $string = $cms[$this->cmid]->get_formatted_name(); // Instance name.
86                } else {
87                    // Something is wrong, instance is not present anymore.
88                    $string = get_string('invalidmodule', 'tool_monitor');
89                }
90            }
91        }
92
93        return $string;
94    }
95
96    /**
97     * Method to get event name.
98     *
99     * @return string
100     * @throws \coding_exception
101     */
102    public function get_event_name() {
103        $eventclass = $this->eventname;
104        if (class_exists($eventclass)) {
105            return $eventclass::get_name();
106        }
107        return get_string('eventnotfound', 'tool_monitor');
108    }
109
110    /**
111     * Get filter description.
112     *
113     * @return string
114     */
115    public function get_filters_description() {
116        $a = new \stdClass();
117        $a->freq = $this->frequency;
118        $mins = $this->timewindow / MINSECS; // Convert seconds to minutes.
119        $a->mins = $mins;
120        return get_string('freqdesc', 'tool_monitor', $a);
121    }
122
123    /**
124     * Get properly formatted name of the rule associated.
125     *
126     * @param \context $context context where this name would be displayed.
127     * @return string Formatted name of the rule.
128     */
129    public function get_name(\context $context) {
130        return format_text($this->name, FORMAT_HTML, array('context' => $context));
131    }
132
133    /**
134     * Get properly formatted description of the rule associated.
135     *
136     * @param \context $context context where this description would be displayed.
137     * @return string Formatted description of the rule.
138     */
139    public function get_description(\context $context) {
140        return format_text($this->description, $this->descriptionformat, array('context' => $context));
141    }
142
143    /**
144     * Get name of the plugin associated with this rule
145     *
146     * @return string Plugin name.
147     */
148    public function get_plugin_name() {
149        if ($this->plugin === 'core') {
150            $string = get_string('core', 'tool_monitor');
151        } else if (get_string_manager()->string_exists('pluginname', $this->plugin)) {
152            $string = get_string('pluginname', $this->plugin);
153        } else {
154            $string = $this->plugin;
155        }
156        return $string;
157    }
158
159    /**
160     * Get properly formatted name of the course associated.
161     *
162     * @param \context $context context where this name would be displayed.
163     * @return string Formatted name of the rule.
164     */
165    public function get_course_name(\context $context) {
166        $courseid = $this->courseid;
167        if (empty($courseid)) {
168            return get_string('site');
169        } else {
170            $course = get_course($courseid);
171            return format_string($course->fullname, true, array('context' => $context));
172        }
173    }
174
175    /**
176     * Can the current user manage the rule associate with this subscription?
177     *
178     * @return bool true if the current user can manage this rule, else false.
179     */
180    public function can_manage_rule() {
181        $courseid = $this->rulecourseid;
182        $context = empty($courseid) ? \context_system::instance() : \context_course::instance($courseid);
183        return has_capability('tool/monitor:managerules', $context);
184    }
185}
Note: See TracBrowser for help on using the repository browser.