MDL-53674 event monitor: Do not send HTML as plain text formatted emails
[moodle.git] / admin / tool / monitor / classes / notification_task.php
CommitLineData
1ebfdaec
AA
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)) {
a8ea7ccc 48 mtrace("Sending message to the user with id " . $message->userto->id . " for the subscription with id $id...");
1ebfdaec 49 message_send($message);
a8ea7ccc 50 mtrace("Sent.");
1ebfdaec
AA
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);
a8ea7ccc
AA
73 if (empty($user)) {
74 // User doesn't exist. Should never happen, nothing to do return.
75 return false;
76 }
1ebfdaec
AA
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);
db1d5b10 85 $htmlmessage = format_text($template, $subscription->templateformat, array('context' => $context));
1ebfdaec
AA
86 $msgdata = new \stdClass();
87 $msgdata->component = 'tool_monitor'; // Your component name.
88 $msgdata->name = 'notification'; // This is the message name from messages.php.
89 $msgdata->userfrom = \core_user::get_noreply_user();
90 $msgdata->userto = $user;
91 $msgdata->subject = $subscription->get_name($context);
db1d5b10
DM
92 $msgdata->fullmessage = html_to_text($htmlmessage);
93 $msgdata->fullmessageformat = FORMAT_PLAIN;
94 $msgdata->fullmessagehtml = $htmlmessage;
1ebfdaec
AA
95 $msgdata->smallmessage = '';
96 $msgdata->notification = 1; // This is only set to 0 for personal messages between users.
97
98 return $msgdata;
99 }
100
101 /**
102 * Replace place holders in the template with respective content.
103 *
104 * @param string $template Message template.
105 * @param subscription $subscription subscription instance
106 * @param \stdclass $eventobj Event data
107 * @param \context $context context object
108 *
109 * @return mixed final template string.
110 */
111 protected function replace_placeholders($template, subscription $subscription, $eventobj, $context) {
112 $template = str_replace('{link}', $eventobj->link, $template);
113 if ($eventobj->contextlevel == CONTEXT_MODULE && !empty($eventobj->contextinstanceid)
114 && (strpos($template, '{modulelink}') !== false)) {
115 $cm = get_fast_modinfo($eventobj->courseid)->get_cm($eventobj->contextinstanceid);
116 $modulelink = $cm->url;
117 $template = str_replace('{modulelink}', $modulelink, $template);
118 }
119 $template = str_replace('{rulename}', $subscription->get_name($context), $template);
120 $template = str_replace('{description}', $subscription->get_description($context), $template);
121 $template = str_replace('{eventname}', $subscription->get_event_name(), $template);
122
123 return $template;
124 }
125}