source: moodle/trunk/fuentes/admin/tool/monitor/classes/notification_task.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.0 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 * This file defines an adhoc task to send notifications.
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 * Adhock class, used to send notifications to users.
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 notification_task extends \core\task\adhoc_task {
38
39    /**
40     * Send out messages.
41     */
42    public function execute() {
43        foreach ($this->get_custom_data() as $data) {
44            $eventobj = $data->event;
45            $subscriptionids = $data->subscriptionids;
46            foreach ($subscriptionids as $id) {
47                if ($message = $this->generate_message($id, $eventobj)) {
48                    mtrace("Sending message to the user with id " . $message->userto->id . " for the subscription with id $id...");
49                    message_send($message);
50                    mtrace("Sent.");
51                }
52            }
53        }
54    }
55
56    /**
57     * Generates the message object for a give subscription and event.
58     *
59     * @param int $subscriptionid Subscription instance
60     * @param \stdClass $eventobj Event data
61     *
62     * @return false|\stdClass message object
63     */
64    protected function generate_message($subscriptionid, \stdClass $eventobj) {
65
66        try {
67            $subscription = subscription_manager::get_subscription($subscriptionid);
68        } catch (\dml_exception $e) {
69            // Race condition, someone deleted the subscription.
70            return false;
71        }
72        $user = \core_user::get_user($subscription->userid);
73        if (empty($user)) {
74            // User doesn't exist. Should never happen, nothing to do return.
75            return false;
76        }
77        $context = \context_user::instance($user->id, IGNORE_MISSING);
78        if ($context === false) {
79            // User context doesn't exist. Should never happen, nothing to do return.
80            return false;
81        }
82
83        $template = $subscription->template;
84        $template = $this->replace_placeholders($template, $subscription, $eventobj, $context);
85        $msgdata = new \stdClass();
86        $msgdata->component         = 'tool_monitor'; // Your component name.
87        $msgdata->name              = 'notification'; // This is the message name from messages.php.
88        $msgdata->userfrom          = \core_user::get_noreply_user();
89        $msgdata->userto            = $user;
90        $msgdata->subject           = $subscription->get_name($context);
91        $msgdata->fullmessage       = format_text($template, $subscription->templateformat, array('context' => $context));
92        $msgdata->fullmessageformat = $subscription->templateformat;
93        $msgdata->fullmessagehtml   = format_text($template, $subscription->templateformat, array('context' => $context));
94        $msgdata->smallmessage      = '';
95        $msgdata->notification      = 1; // This is only set to 0 for personal messages between users.
96
97        return $msgdata;
98    }
99
100    /**
101     * Replace place holders in the template with respective content.
102     *
103     * @param string $template Message template.
104     * @param subscription $subscription subscription instance
105     * @param \stdclass $eventobj Event data
106     * @param \context $context context object
107     *
108     * @return mixed final template string.
109     */
110    protected function replace_placeholders($template, subscription $subscription, $eventobj, $context) {
111        $template = str_replace('{link}', $eventobj->link, $template);
112        if ($eventobj->contextlevel == CONTEXT_MODULE && !empty($eventobj->contextinstanceid)
113            && (strpos($template, '{modulelink}') !== false)) {
114            $cm = get_fast_modinfo($eventobj->courseid)->get_cm($eventobj->contextinstanceid);
115            $modulelink = $cm->url;
116            $template = str_replace('{modulelink}', $modulelink, $template);
117        }
118        $template = str_replace('{rulename}', $subscription->get_name($context), $template);
119        $template = str_replace('{description}', $subscription->get_description($context), $template);
120        $template = str_replace('{eventname}', $subscription->get_event_name(), $template);
121
122        return $template;
123    }
124}
Note: See TracBrowser for help on using the repository browser.