Fixes #302 make sure user lang is used when sending mail.
[moodle-mod_attendance.git] / classes / task / notify.php
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 * Attendance task - Send warnings.
19 *
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
23 */
24
25 namespace mod_attendance\task;
26 defined('MOODLE_INTERNAL') || die();
27
28 require_once($CFG->dirroot . '/mod/attendance/locallib.php');
29 /**
30 * Task class
31 *
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
35 */
36 class notify extends \core\task\scheduled_task {
37 /**
38 * Returns localised general event name.
39 *
40 * @return string
41 */
42 public function get_name() {
43 // Shown in admin screens.
44 return get_string('notifytask', 'mod_attendance');
45 }
46
47 /**
48 * Execute the task.
49 */
50 public function execute() {
51 global $DB;
52 if (empty(get_config('attendance', 'enablewarnings'))) {
53 return; // Warnings not enabled.
54 }
55 $now = time(); // Store current time to use in queries so they all match nicely.
56
57 $orderby = 'ORDER BY cm.id, atl.studentid, n.warningpercent ASC';
58
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();
66 $numsentusers = 0;
67 $numsentthird = 0;
68 foreach ($records as $record) {
69 if (empty($sentnotifications[$record->userid])) {
70 $sentnotifications[$record->userid] = array();
71 }
72
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])) {
77
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)) {
81 $sql = "SELECT *
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.
87 }
88 }
89
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();
94 $oldforcelang = force_current_language($user->lang);
95
96 $emailcontent = format_text($record->emailcontent, $record->emailcontentformat);
97 $emailsubject = format_text($record->emailsubject);
98 email_to_user($user, $from, $emailsubject, $emailcontent, $emailcontent);
99
100 force_current_language($oldforcelang);
101 $sentnotifications[$record->userid][] = $record->aid;
102 $numsentusers++;
103 }
104 }
105 // Only send one warning to this user from each attendance in this run. - flag any higher percent notifications as sent.
106 if (!empty($record->thirdpartyemails)) {
107 $sendto = explode(',', $record->thirdpartyemails);
108 $record->percent = round($record->percent * 100)."%";
109 $context = \context_module::instance($record->cmid);
110 foreach ($sendto as $senduser) {
111 if (empty($senduser)) {
112 // Probably an extra comma in the thirdpartyusers field.
113 continue;
114 }
115 // Check user is allowed to receive warningemails.
116 if (has_capability('mod/attendance:warningemails', $context, $senduser)) {
117 if (empty($thirdpartynotifications[$senduser])) {
118 $thirdpartynotifications[$senduser] = array();
119 }
120 if (!isset($thirdpartynotifications[$senduser][$record->aid . '_' . $record->userid])) {
121 $thirdpartynotifications[$senduser][$record->aid . '_' . $record->userid]
122 = get_string('thirdpartyemailtext', 'attendance', $record);
123 }
124 } else {
125 mtrace("user".$senduser. "does not have capablity in cm".$record->cmid);
126 }
127 }
128 }
129 $notify = new \stdClass();
130 $notify->userid = $record->userid;
131 $notify->notifyid = $record->notifyid;
132 $notify->timesent = $now;
133 $DB->insert_record('attendance_warning_done', $notify);
134 }
135 if (!empty($numsentusers)) {
136 mtrace($numsentusers ." user emails sent");
137 }
138 if (!empty($thirdpartynotifications)) {
139 foreach ($thirdpartynotifications as $sendid => $notifications) {
140 $user = $DB->get_record('user', array('id' => $sendid));
141 $from = \core_user::get_noreply_user();
142 $oldforcelang = force_current_language($user->lang);
143
144 $emailcontent = implode("\n", $notifications);
145 $emailcontent .= "\n\n".get_string('thirdpartyemailtextfooter', 'attendance');
146 $emailcontent = format_text($emailcontent);
147 $emailsubject = get_string('thirdpartyemailsubject', 'attendance');
148
149 email_to_user($user, $from, $emailsubject, $emailcontent, $emailcontent);
150 force_current_language($oldforcelang);
151 $numsentthird++;
152 }
153 if (!empty($numsentthird)) {
154 mtrace($numsentthird ." thirdparty emails sent");
155 }
156 }
157 }
158 }