11b7137b0cd584e4dc4c0753c8e88360c36b8e08
2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Attendance task - Send warnings.
20 * @package mod_attendance
21 * @copyright 2017 onwards Dan Marsden http://danmarsden.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace mod_attendance
\task
;
26 defined('MOODLE_INTERNAL') ||
die();
28 require_once($CFG->dirroot
. '/mod/attendance/locallib.php');
32 * @package mod_attendance
33 * @copyright 2017 onwards Dan Marsden http://danmarsden.com
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class notify
extends \core
\task\scheduled_task
{
38 * Returns localised general event name.
42 public function get_name() {
43 // Shown in admin screens.
44 return get_string('notifytask', 'mod_attendance');
50 public function execute() {
52 if (empty(get_config('attendance', 'enablewarnings'))) {
53 return; // Warnings not enabled.
55 $now = time(); // Store current time to use in queries so they all match nicely.
57 $orderby = 'ORDER BY cm.id, atl.studentid, n.warningpercent ASC';
59 // Get records for attendance sessions that have been updated since last time this task ran.
60 // Note: this returns all users for these sessions - even if the users attendance wasn't changed
61 // since last time we ran, before sending a notification we check to see if the users have
62 // updated attendance logs since last time they were notified.
63 $records = attendance_get_users_to_notify(array(), $orderby, true
);
64 $sentnotifications = array();
65 $thirdpartynotifications = array();
68 foreach ($records as $record) {
69 if (empty($sentnotifications[$record->userid
])) {
70 $sentnotifications[$record->userid
] = array();
73 if (!empty($record->emailuser
)) {
74 // Only send one warning to this user from each attendance in this run.
75 // Flag any higher percent notifications as sent.
76 if (empty($sentnotifications[$record->userid
]) ||
!in_array($record->aid
, $sentnotifications[$record->userid
])) {
78 // If has previously been sent a warning, check to see if this user has
79 // attendance updated since the last time the notification was sent.
80 if (!empty($record->timesent
)) {
82 FROM {attendance_log} l
83 JOIN {attendance_sessions} s ON s.id = l.sessionid
84 WHERE s.attendanceid = ? AND studentid = ? AND timetaken > ?";
85 if (!$DB->record_exists_sql($sql, array($record->aid
, $record->userid
, $record->timesent
))) {
86 continue; // Skip this record and move to the next user.
90 // Convert variables in emailcontent.
91 $record = attendance_template_variables($record);
92 $user = $DB->get_record('user', array('id' => $record->userid
));
93 $from = \core_user
::get_noreply_user();
95 $emailcontent = format_text($record->emailcontent
, $record->emailcontentformat
);
97 email_to_user($user, $from, $record->emailsubject
, $emailcontent, $emailcontent);
99 $sentnotifications[$record->userid
][] = $record->aid
;
103 // Only send one warning to this user from each attendance in this run. - flag any higher percent notifications as sent.
104 if (!empty($record->thirdpartyemails
)) {
105 $sendto = explode(',', $record->thirdpartyemails
);
106 $record->percent
= round($record->percent
* 100)."%";
107 $context = \context_module
::instance($record->cmid
);
108 foreach ($sendto as $senduser) {
109 if (empty($senduser)) {
110 // Probably an extra comma in the thirdpartyusers field.
113 // Check user is allowed to receive warningemails.
114 if (has_capability('mod/attendance:warningemails', $context, $senduser)) {
115 if (empty($thirdpartynotifications[$senduser])) {
116 $thirdpartynotifications[$senduser] = array();
118 if (!isset($thirdpartynotifications[$senduser][$record->aid
. '_' . $record->userid
])) {
119 $thirdpartynotifications[$senduser][$record->aid
. '_' . $record->userid
]
120 = get_string('thirdpartyemailtext', 'attendance', $record);
123 mtrace("user".$senduser. "does not have capablity in cm".$record->cmid
);
127 $notify = new \
stdClass();
128 $notify->userid
= $record->userid
;
129 $notify->notifyid
= $record->notifyid
;
130 $notify->timesent
= $now;
131 $DB->insert_record('attendance_warning_done', $notify);
133 if (!empty($numsentusers)) {
134 mtrace($numsentusers ." user emails sent");
136 if (!empty($thirdpartynotifications)) {
137 foreach ($thirdpartynotifications as $sendid => $notifications) {
138 $user = $DB->get_record('user', array('id' => $sendid));
139 $from = \core_user
::get_noreply_user();
141 $emailcontent = implode("\n", $notifications);
142 $emailcontent .= "\n\n".get_string('thirdpartyemailtextfooter', 'attendance');
143 $emailcontent = format_text($emailcontent);
144 $emailsubject = get_string('thirdpartyemailsubject', 'attendance');
146 email_to_user($user, $from, $emailsubject, $emailcontent, $emailcontent);
149 if (!empty($numsentthird)) {
150 mtrace($numsentthird ." thirdparty emails sent");