MDL-64783 analytics: Send notifications using the receiver's language
[moodle.git] / analytics / classes / insights_generator.php
CommitLineData
bb7dccdb
DM
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 * Insights generator.
19 *
20 * @package core_analytics
21 * @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_analytics;
26
27defined('MOODLE_INTERNAL') || die();
28
29require_once($CFG->dirroot . '/lib/messagelib.php');
30
31/**
32 * Insights generator.
33 *
34 * @package core_analytics
35 * @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class insights_generator {
39
40 /**
41 * @var int
42 */
43 private $modelid;
44
45 /**
46 * @var \core_analytics\local\target\base
47 */
48 private $target;
49
50 /**
51 * @var int[]
52 */
53 private $contextcourseids;
54
55 /**
56 * Constructor.
57 *
58 * @param int $modelid
59 * @param \core_analytics\local\target\base $target
60 */
61 public function __construct(int $modelid, \core_analytics\local\target\base $target) {
62 $this->modelid = $modelid;
63 $this->target = $target;
64 }
65
66 /**
67 * Generates insight notifications.
68 *
982fef46
DM
69 * @param array $samplecontexts The contexts these predictions belong to
70 * @param \core_analytics\prediction[] $predictions The prediction records
bb7dccdb
DM
71 * @return null
72 */
73 public function generate($samplecontexts, $predictions) {
93663fa1 74 global $OUTPUT;
bb7dccdb
DM
75
76 $analyserclass = $this->target->get_analyser_class();
77
21bf5ae5
DM
78 // We will need to restore it later.
79 $actuallanguage = current_language();
80
bb7dccdb 81 if ($analyserclass::one_sample_per_analysable()) {
21bf5ae5
DM
82
83 // Iterate through the predictions and the users in each prediction (likely to be just one).
bb7dccdb
DM
84 foreach ($predictions as $prediction) {
85
86 $context = $samplecontexts[$prediction->get_prediction_data()->contextid];
21bf5ae5
DM
87
88 $users = $this->target->get_insights_users($context);
89 foreach ($users as $user) {
90
91 $this->set_notification_language($user);
92 list($insighturl, $fullmessage, $fullmessagehtml) = $this->prediction_info($prediction);
93 $this->notification($context, $user, $insighturl, $fullmessage, $fullmessagehtml);
94 }
bb7dccdb
DM
95 }
96
97 } else {
21bf5ae5
DM
98
99 // Iterate through the context and the users in each context.
bb7dccdb
DM
100 foreach ($samplecontexts as $context) {
101
21bf5ae5
DM
102 $users = $this->target->get_insights_users($context);
103 foreach ($users as $user) {
93663fa1 104
21bf5ae5
DM
105 $this->set_notification_language($user);
106
107 $insighturl = $this->target->get_insight_context_url($this->modelid, $context);
108
109 $fullmessage = get_string('insightinfomessage', 'analytics', $insighturl->out(false));
110 $fullmessagehtml = $OUTPUT->render_from_template('core_analytics/insight_info_message',
111 ['url' => $insighturl->out(false)]
112 );
113
114 $this->notification($context, $user, $insighturl, $fullmessage, $fullmessagehtml);
115 }
bb7dccdb
DM
116 }
117 }
21bf5ae5
DM
118
119 force_current_language($actuallanguage);
bb7dccdb
DM
120 }
121
122 /**
123 * Generates a insight notification for the user.
124 *
125 * @param \context $context
21bf5ae5 126 * @param \stdClass $user
bb7dccdb
DM
127 * @param \moodle_url $insighturl The insight URL
128 * @param string $fullmessage
129 * @param string $fullmessagehtml
130 * @return null
131 */
21bf5ae5 132 private function notification(\context $context, \stdClass $user, \moodle_url $insighturl, string $fullmessage, string $fullmessagehtml) {
bb7dccdb 133
21bf5ae5
DM
134 $message = new \core\message\message();
135 $message->component = 'moodle';
136 $message->name = 'insights';
bb7dccdb 137
21bf5ae5
DM
138 $message->userfrom = \core_user::get_noreply_user();
139 $message->userto = $user;
bb7dccdb 140
21bf5ae5 141 $message->subject = $this->target->get_insight_subject($this->modelid, $context);
bb7dccdb 142
21bf5ae5
DM
143 // Same than the subject.
144 $message->contexturlname = $message->subject;
145 $message->courseid = $this->get_context_courseid($context);
bb7dccdb 146
21bf5ae5
DM
147 $message->fullmessage = $fullmessage;
148 $message->fullmessageformat = FORMAT_PLAIN;
149 $message->fullmessagehtml = $fullmessagehtml;
150 $message->smallmessage = $fullmessage;
151 $message->contexturl = $insighturl->out(false);
bb7dccdb 152
21bf5ae5 153 message_send($message);
bb7dccdb
DM
154 }
155
156 /**
157 * Returns the course context of the provided context reading an internal cache first.
158 *
159 * @param \context $context
160 * @return int
161 */
162 private function get_context_courseid(\context $context) {
163
164 if (empty($this->contextcourseids[$context->id])) {
165
166 $coursecontext = $context->get_course_context(false);
167 if (!$coursecontext) {
168 // Default to the frontpage course context.
169 $coursecontext = \context_course::instance(SITEID);
170 }
171 $this->contextcourseids[$context->id] = $coursecontext->instanceid;
172 }
173
174 return $this->contextcourseids[$context->id];
175 }
176
177 /**
178 * Extracts info from the prediction for display purposes.
179 *
180 * @param \core_analytics\prediction $prediction
181 * @return array Three items array with formats [\moodle_url, string, string]
182 */
183 private function prediction_info(\core_analytics\prediction $prediction) {
93663fa1 184 global $OUTPUT;
bb7dccdb 185
20d7f527 186 $predictionactions = $this->target->prediction_actions($prediction, true, true);
bb7dccdb 187
93663fa1
DM
188 // For FORMAT_PLAIN.
189 $fullmessageplaintext = '';
190
191 // For FORMAT_HTML.
192 $messageactions = [];
bb7dccdb
DM
193 $insighturl = null;
194 foreach ($predictionactions as $action) {
195 $actionurl = $action->get_url();
196 if (!$actionurl->get_param('forwardurl')) {
197
198 $actiondoneurl = new \moodle_url('/report/insights/done.php');
199 // Set the forward url to the 'done' script.
200 $actionurl->param('forwardurl', $actiondoneurl->out(false));
201 }
202
bb7dccdb
DM
203 if (empty($insighturl)) {
204 // We use the primary action url as insight url so we log that the user followed the provided link.
205 $insighturl = $action->get_url();
bb7dccdb 206 }
93663fa1
DM
207 $actiondata = (object)['url' => $action->get_url()->out(false), 'text' => $action->get_text()];
208 $fullmessageplaintext .= get_string('insightinfomessageaction', 'analytics', $actiondata) . PHP_EOL;
209 $messageactions[] = $actiondata;
bb7dccdb
DM
210 }
211
982fef46
DM
212 $fullmessagehtml = $OUTPUT->render_from_template('core_analytics/insight_info_message_prediction',
213 ['actions' => $messageactions]);
93663fa1 214 return [$insighturl, $fullmessageplaintext, $fullmessagehtml];
bb7dccdb 215 }
21bf5ae5
DM
216
217 /**
218 * Sets the session language to the language used by the notification receiver.
219 *
220 * @param \stdClass $user The user who will receive the message
221 * @return null
222 */
223 private function set_notification_language($user) {
224 global $CFG;
225
226 // Copied from current_language().
227 if (!empty($user->lang)) {
228 $lang = $user->lang;
229 } else if (isset($CFG->lang)) {
230 $lang = $CFG->lang;
231 } else {
232 $lang = 'en';
233 }
234 force_current_language($lang);
235 }
20d7f527 236}