1 |
efrain |
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 |
namespace core_course\task;
|
|
|
18 |
|
|
|
19 |
use core\task\adhoc_task;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Class handling course content updates notifications.
|
|
|
23 |
*
|
|
|
24 |
* @package core_course
|
|
|
25 |
* @copyright 2021 Juan Leyva <juan@moodle.com>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
class content_notification_task extends adhoc_task {
|
|
|
29 |
|
|
|
30 |
// Use the logging trait for better logging.
|
|
|
31 |
use \core\task\logging_trait;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Run the main task.
|
|
|
35 |
*/
|
|
|
36 |
public function execute() {
|
|
|
37 |
global $CFG, $OUTPUT;
|
|
|
38 |
require_once($CFG->libdir . '/enrollib.php');
|
|
|
39 |
|
|
|
40 |
$data = $this->get_custom_data();
|
|
|
41 |
|
|
|
42 |
$course = get_course($data->courseid);
|
|
|
43 |
$modinfo = get_fast_modinfo($course);
|
|
|
44 |
$cm = $modinfo->cms[$data->cmid];
|
|
|
45 |
$isupdate = !empty($data->update);
|
|
|
46 |
|
|
|
47 |
if (!$course->visible || !$cm->visible) {
|
|
|
48 |
// The course or module is hidden. We don't check if the user can see hidden courses, does not make sense here.
|
|
|
49 |
// Permissions may have changed since it was queued.
|
|
|
50 |
return;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
// Get only active users.
|
|
|
54 |
$coursecontext = \context_course::instance($course->id);
|
|
|
55 |
$users = get_enrolled_users($coursecontext, '', 0, 'u.*', null, 0, 0, true);
|
|
|
56 |
if (empty($users)) {
|
|
|
57 |
return;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
$userfrom = \core_user::get_user($data->userfrom);
|
|
|
61 |
|
|
|
62 |
// Now send the messages
|
|
|
63 |
$countusers = count($users);
|
|
|
64 |
$sentcount = $errorcount = 0;
|
|
|
65 |
$this->log_start("Sending course content update notifications to {$countusers} potential users
|
|
|
66 |
from user with id {$userfrom->id}.");
|
|
|
67 |
foreach ($users as $user) {
|
|
|
68 |
|
|
|
69 |
\core\cron::setup_user($user, $course);
|
|
|
70 |
|
|
|
71 |
// Ensure that the activity is available/visible to the user.
|
|
|
72 |
$cm = get_fast_modinfo($course)->cms[$cm->id];
|
|
|
73 |
if (!\core_availability\info_module::is_user_visible($cm, $user->id, false)) {
|
|
|
74 |
$this->log("Ignoring user {$user->id} (no permissions to see the module)", 1);
|
|
|
75 |
continue;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
// Get module names in the user's language.
|
|
|
79 |
$modnames = get_module_types_names();
|
|
|
80 |
$a = [
|
|
|
81 |
'coursename' => format_string(get_course_display_name_for_list($course), true, ['context' => $coursecontext]),
|
|
|
82 |
'courselink' => (new \moodle_url('/course/view.php', ['id' => $course->id]))->out(false),
|
|
|
83 |
'modulename' => $cm->get_formatted_name(),
|
|
|
84 |
'moduletypename' => $modnames[$cm->modname],
|
|
|
85 |
'link' => (new \moodle_url('/mod/' . $cm->modname . '/view.php', ['id' => $cm->id]))->out(false),
|
|
|
86 |
'notificationpreferenceslink' =>
|
|
|
87 |
(new \moodle_url('/message/notificationpreferences.php', ['userid' => $user->id]))->out(false),
|
|
|
88 |
];
|
|
|
89 |
|
|
|
90 |
if ($isupdate) {
|
|
|
91 |
$messagesubject = get_string('coursecontentnotifupdate', 'course', $a);
|
|
|
92 |
$messagebody = get_string('coursecontentnotifupdatebody', 'course', $a);
|
|
|
93 |
} else {
|
|
|
94 |
$messagesubject = get_string('coursecontentnotifnew', 'course', $a);
|
|
|
95 |
$messagebody = get_string('coursecontentnotifnewbody', 'course', $a);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
// Send notification.
|
|
|
99 |
$eventdata = new \core\message\message();
|
|
|
100 |
$eventdata->courseid = $course->id;
|
|
|
101 |
$eventdata->component = 'moodle';
|
|
|
102 |
$eventdata->name = 'coursecontentupdated';
|
|
|
103 |
$eventdata->userfrom = $userfrom;
|
|
|
104 |
$eventdata->userto = $user;
|
|
|
105 |
$eventdata->subject = $messagesubject;
|
|
|
106 |
$eventdata->fullmessageformat = FORMAT_HTML;
|
|
|
107 |
$eventdata->fullmessagehtml = $messagebody;
|
|
|
108 |
$eventdata->smallmessage = strip_tags($eventdata->fullmessagehtml);
|
|
|
109 |
$eventdata->contexturl = (new \moodle_url('/mod/' . $cm->modname . '/view.php', ['id' => $cm->id]))->out(false);
|
|
|
110 |
$eventdata->contexturlname = $cm->get_formatted_name();
|
|
|
111 |
$eventdata->notification = 1;
|
|
|
112 |
|
|
|
113 |
// Add notification custom data.
|
|
|
114 |
$eventcustomdata = ['notificationiconurl' => $cm->get_icon_url()->out(false)];
|
|
|
115 |
if ($courseimage = \core_course\external\course_summary_exporter::get_course_image($course)) {
|
|
|
116 |
$eventcustomdata['notificationpictureurl'] = $courseimage;
|
|
|
117 |
}
|
|
|
118 |
$eventdata->customdata = $eventcustomdata;
|
|
|
119 |
|
|
|
120 |
$activitydates = \core\activity_dates::get_dates_for_module($cm, $user->id);
|
|
|
121 |
if (!empty($activitydates)) {
|
|
|
122 |
$data = (new \core_course\output\activity_dates($activitydates))->export_for_template($OUTPUT);
|
|
|
123 |
foreach ($data->activitydates as $date) {
|
|
|
124 |
$eventdata->fullmessagehtml .= \html_writer::div($date['label'] . ' ' . $date['datestring']);
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
$eventdata->fullmessage = html_to_text($eventdata->fullmessagehtml);
|
|
|
128 |
|
|
|
129 |
if (message_send($eventdata)) {
|
|
|
130 |
$this->log("Notification sent to user with id {$user->id}", 1);
|
|
|
131 |
$sentcount++;
|
|
|
132 |
} else {
|
|
|
133 |
$this->log("Failed to send notification to user with id {$user->id}", 1);
|
|
|
134 |
$errorcount++;
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
$this->log_finish("Sent {$sentcount} notifications with {$errorcount} failures");
|
|
|
139 |
}
|
|
|
140 |
}
|