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 |
/**
|
|
|
18 |
* This file manages the public functions of this module
|
|
|
19 |
*
|
|
|
20 |
* @package mod_custommailing
|
|
|
21 |
* @author jeanfrancois@cblue.be,olivier@cblue.be
|
|
|
22 |
* @copyright 2021 CBlue SPRL
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
use core\notification;
|
|
|
27 |
use mod_custommailing\Mailing;
|
|
|
28 |
use mod_custommailing\MailingLog;
|
|
|
29 |
|
|
|
30 |
define('MAILING_MODE_NONE', 0);
|
|
|
31 |
define('MAILING_MODE_FIRSTLAUNCH', 1);
|
|
|
32 |
define('MAILING_MODE_REGISTRATION', 2);
|
|
|
33 |
define('MAILING_MODE_COMPLETE', 3);
|
|
|
34 |
define('MAILING_MODE_DAYSFROMINSCRIPTIONDATE', 4);
|
|
|
35 |
define('MAILING_MODE_DAYSFROMLASTCONNECTION', 5);
|
|
|
36 |
define('MAILING_MODE_DAYSFROMFIRSTLAUNCH', 6);
|
|
|
37 |
define('MAILING_MODE_DAYSFROMLASTLAUNCH', 7);
|
|
|
38 |
define('MAILING_MODE_SEND_CERTIFICATE', 8);
|
|
|
39 |
|
|
|
40 |
define('MAILING_STATUS_DISABLED', 0);
|
|
|
41 |
define('MAILING_STATUS_ENABLED', 1);
|
|
|
42 |
|
|
|
43 |
define('MAILING_LOG_IDLE', 0);
|
|
|
44 |
define('MAILING_LOG_PROCESSING', 1);
|
|
|
45 |
define('MAILING_LOG_SENT', 2);
|
|
|
46 |
define('MAILING_LOG_FAILED', 3);
|
|
|
47 |
|
|
|
48 |
define('MAILING_SOURCE_MODULE', 1);
|
|
|
49 |
define('MAILING_SOURCE_COURSE', 2);
|
|
|
50 |
define('MAILING_SOURCE_CERT', 3);
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* @param $custommailing
|
|
|
54 |
* @return bool|int
|
|
|
55 |
* @throws coding_exception
|
|
|
56 |
* @throws dml_exception
|
|
|
57 |
*/
|
|
|
58 |
function custommailing_add_instance($custommailing) {
|
|
|
59 |
global $CFG, $DB;
|
|
|
60 |
|
|
|
61 |
$custommailing->timecreated = time();
|
|
|
62 |
$custommailing->timemodified = time();
|
|
|
63 |
|
|
|
64 |
// Check if course has completion enabled, and enable it if not (and user has permission to do so)
|
|
|
65 |
$course = $DB->get_record('course', ['id' => $custommailing->course]);
|
|
|
66 |
if (empty($course->enablecompletion)) {
|
|
|
67 |
if (empty($CFG->enablecompletion)) {
|
|
|
68 |
// Completion tracking is disabled in Moodle
|
|
|
69 |
notification::error(get_string('coursecompletionnotenabled', 'custommailing'));
|
|
|
70 |
} else {
|
|
|
71 |
// Completion tracking is enabled in Moodle
|
|
|
72 |
if (has_capability('moodle/course:update', context_course::instance($course->id))) {
|
|
|
73 |
$data = ['id' => $course->id, 'enablecompletion' => '1'];
|
|
|
74 |
$DB->update_record('course', $data);
|
|
|
75 |
rebuild_course_cache($course->id);
|
|
|
76 |
notification::warning(get_string('coursecompletionenabled', 'custommailing'));
|
|
|
77 |
} else {
|
|
|
78 |
notification::error(get_string('coursecompletionnotenabled', 'custommailing'));
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
return $DB->insert_record('custommailing', $custommailing);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* @param $custommailing
|
|
|
89 |
* @return bool
|
|
|
90 |
* @throws dml_exception
|
|
|
91 |
*/
|
|
|
92 |
function custommailing_update_instance($custommailing) {
|
|
|
93 |
global $DB;
|
|
|
94 |
|
|
|
95 |
$custommailing->timemodified = time();
|
|
|
96 |
$custommailing->id = $custommailing->instance;
|
|
|
97 |
|
|
|
98 |
return $DB->update_record('custommailing', $custommailing);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* @param $id
|
|
|
103 |
* @return bool
|
|
|
104 |
* @throws dml_exception
|
|
|
105 |
*/
|
|
|
106 |
function custommailing_delete_instance($id) {
|
|
|
107 |
global $DB;
|
|
|
108 |
|
|
|
109 |
if (!$custommailing = $DB->get_record('custommailing', ['id' => $id])) {
|
|
|
110 |
return false;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
$result = true;
|
|
|
114 |
|
|
|
115 |
// Delete any dependent mailing here.
|
|
|
116 |
Mailing::deleteAll($custommailing->id);
|
|
|
117 |
|
|
|
118 |
if (!$DB->delete_records('custommailing', ['id' => $custommailing->id])) {
|
|
|
119 |
$result = false;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
return $result;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* @param $feature
|
|
|
127 |
* @return bool|null
|
|
|
128 |
*/
|
|
|
129 |
function custommailing_supports($feature) {
|
|
|
130 |
switch($feature) {
|
|
|
131 |
case FEATURE_GRADE_HAS_GRADE:
|
|
|
132 |
return false;
|
|
|
133 |
case FEATURE_GRADE_OUTCOMES:
|
|
|
134 |
return false;
|
|
|
135 |
case FEATURE_ADVANCED_GRADING:
|
|
|
136 |
return false;
|
|
|
137 |
case FEATURE_CONTROLS_GRADE_VISIBILITY:
|
|
|
138 |
return false;
|
|
|
139 |
case FEATURE_COMPLETION_TRACKS_VIEWS:
|
|
|
140 |
return false;
|
|
|
141 |
case FEATURE_COMPLETION_HAS_RULES:
|
|
|
142 |
return false;
|
|
|
143 |
case FEATURE_NO_VIEW_LINK:
|
|
|
144 |
return false;
|
|
|
145 |
case FEATURE_IDNUMBER:
|
|
|
146 |
return true;
|
|
|
147 |
case FEATURE_GROUPS:
|
|
|
148 |
return false;
|
|
|
149 |
case FEATURE_GROUPINGS:
|
|
|
150 |
return false;
|
|
|
151 |
case FEATURE_MOD_INTRO:
|
|
|
152 |
return false;
|
|
|
153 |
case FEATURE_MODEDIT_DEFAULT_COMPLETION:
|
|
|
154 |
return false;
|
|
|
155 |
case FEATURE_COMMENT:
|
|
|
156 |
return false;
|
|
|
157 |
case FEATURE_RATE:
|
|
|
158 |
return false;
|
|
|
159 |
case FEATURE_BACKUP_MOODLE2:
|
|
|
160 |
return true;
|
|
|
161 |
case FEATURE_SHOW_DESCRIPTION:
|
|
|
162 |
return false;
|
|
|
163 |
case FEATURE_USES_QUESTIONS:
|
|
|
164 |
return false;
|
|
|
165 |
default:
|
|
|
166 |
return false;
|
|
|
167 |
}
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
/**
|
|
|
171 |
* @param mixed $only [false for all OR modname (scorm, quiz, etc...)]
|
|
|
172 |
* @return array
|
|
|
173 |
* @throws moodle_exception
|
|
|
174 |
*/
|
|
|
175 |
function custommailing_get_activities ($only= false) {
|
|
|
176 |
global $COURSE, $PAGE;
|
|
|
177 |
$course_module_context = $PAGE->context;
|
|
|
178 |
|
|
|
179 |
$activities = [];
|
|
|
180 |
foreach ($modinfo = get_fast_modinfo($COURSE)->get_cms() as $cm) {
|
|
|
181 |
if ($cm->id != $course_module_context->instanceid) {
|
|
|
182 |
if (!$only || $cm->modname == $only) {
|
|
|
183 |
$activities[(int) $cm->id] = format_string($cm->name);
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
return $activities;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* @param int $courseid
|
|
|
193 |
* @return array
|
|
|
194 |
* @throws dml_exception
|
|
|
195 |
*/
|
|
|
196 |
function custommailing_getcustomcertsfromcourse($courseid)
|
|
|
197 |
{
|
|
|
198 |
global $DB;
|
|
|
199 |
|
|
|
200 |
$certs = [];
|
|
|
201 |
$result = $DB->get_records('customcert', ['course' => $courseid]);
|
|
|
202 |
foreach ($result as $cert) {
|
|
|
203 |
$certs[(int) $cert->id] = format_string($cert->name);
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
return $certs;
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* @param int $customcertmoduleid
|
|
|
211 |
* @return false|mixed|stdClass
|
|
|
212 |
* @throws dml_exception
|
|
|
213 |
*/
|
|
|
214 |
function custommailing_getCmFromCustomcertInstance($customcertmoduleid)
|
|
|
215 |
{
|
|
|
216 |
global $DB;
|
|
|
217 |
|
|
|
218 |
$module = $DB->get_record('modules', ['name' => 'customcert']);
|
|
|
219 |
|
|
|
220 |
if (!empty($module->id)) {
|
|
|
221 |
return $DB->get_record('course_modules', ['instance' => $customcertmoduleid, 'module' => $module->id]);
|
|
|
222 |
} else {
|
|
|
223 |
return false;
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* @param int $customcertid
|
|
|
229 |
* @return false|mixed|stdClass
|
|
|
230 |
* @throws dml_exception
|
|
|
231 |
*/
|
|
|
232 |
function custommailing_getCustomcert($customcertid)
|
|
|
233 |
{
|
|
|
234 |
global $DB;
|
|
|
235 |
|
|
|
236 |
return $DB->get_record('customcert', ['id' => $customcertid]);
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* @throws coding_exception
|
|
|
241 |
* @throws dml_exception
|
|
|
242 |
* @throws moodle_exception
|
|
|
243 |
*/
|
|
|
244 |
function custommailing_logs_generate()
|
|
|
245 |
{
|
|
|
246 |
global $DB;
|
|
|
247 |
|
|
|
248 |
$mailings = Mailing::getAllToSend();
|
|
|
249 |
foreach ($mailings as $mailing) {
|
|
|
250 |
$sql = custommailing_getsql($mailing);
|
|
|
251 |
if (!empty($sql['sql']) && !empty($sql['params'])) {
|
|
|
252 |
$users = $DB->get_records_sql($sql['sql'], $sql['params']);
|
|
|
253 |
if (is_array($users)) {
|
|
|
254 |
foreach ($users as $user) {
|
|
|
255 |
if (validate_email($user->email) && !$DB->get_record('custommailing_logs', ['custommailingmailingid' => $mailing->id, 'emailtouserid' => $user->id])) {
|
|
|
256 |
$record = new stdClass();
|
|
|
257 |
$record->custommailingmailingid = (int) $mailing->id;
|
|
|
258 |
$record->emailtouserid = (int) $user->id;
|
|
|
259 |
$record->emailstatus = MAILING_LOG_PROCESSING;
|
|
|
260 |
$record->timecreated = time();
|
|
|
261 |
MailingLog::create($record);
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
}
|
|
|
266 |
}
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
/**
|
|
|
270 |
* @param object $mailing
|
|
|
271 |
* @return false|string
|
|
|
272 |
* @throws coding_exception
|
|
|
273 |
* @throws dml_exception
|
|
|
274 |
* @throws moodle_exception
|
|
|
275 |
*/
|
|
|
276 |
function custommailing_getsql($mailing)
|
|
|
277 |
{
|
|
|
278 |
$sql = false;
|
|
|
279 |
$params = [];
|
|
|
280 |
|
|
|
281 |
$config = get_config('custommailing');
|
|
|
282 |
if (!empty($config->debugmode)) {
|
|
|
283 |
$delay_range = 'MINUTE';
|
|
|
284 |
} else {
|
|
|
285 |
$delay_range = 'DAY';
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
// target module completion
|
|
|
289 |
if (!empty($mailing->targetmoduleid)) {
|
|
|
290 |
if (!empty($mailing->targetmodulestatus)) {
|
|
|
291 |
$sql_where = " AND (cmc.completionstate IN (0,3) OR cmc.completionstate IS NULL)";
|
|
|
292 |
} else {
|
|
|
293 |
$sql_where = " AND cmc.completionstate IN (1,2)";
|
|
|
294 |
}
|
|
|
295 |
} else {
|
|
|
296 |
$sql_where = '';
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
// mailing modes
|
|
|
300 |
if ($mailing->mailingmode == MAILING_MODE_FIRSTLAUNCH && !empty($mailing->targetmoduleid)) {
|
|
|
301 |
$sql = "SELECT DISTINCT u.*
|
|
|
302 |
FROM {user} u
|
|
|
303 |
JOIN {logstore_standard_log} lsl ON lsl.userid = u.id AND lsl.contextlevel = 70 AND lsl.contextinstanceid = :targetmoduleid AND lsl.action = 'viewed'
|
|
|
304 |
";
|
|
|
305 |
$params['targetmoduleid'] = $mailing->targetmoduleid;
|
|
|
306 |
} elseif ($mailing->mailingmode == MAILING_MODE_REGISTRATION && !empty($mailing->courseid)) {
|
|
|
307 |
// retroactive mode
|
|
|
308 |
if (!$mailing->retroactive) {
|
|
|
309 |
$sql_retro = " AND ue.timecreated >= :timecreated AND (ue.timestart = 0 OR ue.timestart >= :timestart) AND (ue.timeend = 0 OR ue.timeend >= :timeend)";
|
|
|
310 |
$params['timecreated'] = $mailing->timecreated;
|
|
|
311 |
$params['timestart'] = $mailing->timecreated;
|
|
|
312 |
$params['timeend'] = $mailing->timecreated;
|
|
|
313 |
} else {
|
|
|
314 |
$sql_retro = '';
|
|
|
315 |
}
|
|
|
316 |
$sql = "SELECT DISTINCT u.*
|
|
|
317 |
FROM {user} u
|
|
|
318 |
JOIN {user_enrolments} ue ON ue.userid = u.id
|
|
|
319 |
JOIN {enrol} e ON e.id = ue.enrolid
|
|
|
320 |
WHERE e.courseid = :courseid $sql_retro
|
|
|
321 |
";
|
|
|
322 |
$params['courseid'] = $mailing->courseid;
|
|
|
323 |
} elseif ($mailing->mailingmode == MAILING_MODE_COMPLETE && !empty($mailing->targetmoduleid)) {
|
|
|
324 |
$sql = "SELECT DISTINCT u.*
|
|
|
325 |
FROM {user} u
|
|
|
326 |
JOIN {course_modules_completion} cmc ON cmc.userid = u.id AND cmc.coursemoduleid = :targetmoduleid
|
|
|
327 |
";
|
|
|
328 |
$params['targetmoduleid'] = $mailing->targetmoduleid;
|
|
|
329 |
} elseif ($mailing->mailingmode == MAILING_MODE_DAYSFROMINSCRIPTIONDATE && !empty($mailing->mailingdelay)) {
|
|
|
330 |
// retroactive mode
|
|
|
331 |
if (!$mailing->retroactive) {
|
|
|
332 |
$sql_retro = " AND ue.timecreated >= :timecreated AND (ue.timestart = 0 OR ue.timestart >= :timestart) AND (ue.timeend = 0 OR ue.timeend >= :timeend)";
|
|
|
333 |
$params['timecreated'] = $mailing->timecreated;
|
|
|
334 |
$params['timestart'] = $mailing->timecreated;
|
|
|
335 |
$params['timeend'] = $mailing->timecreated;
|
|
|
336 |
} else {
|
|
|
337 |
$sql_retro = '';
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
$start = new \DateTime();
|
|
|
341 |
$interval_duration = "P" . $mailing->mailingdelay . "D";
|
|
|
342 |
if ($delay_range == 'MINUTE') {
|
|
|
343 |
$interval_duration = "PT" . $mailing->mailingdelay . "M";
|
|
|
344 |
}
|
|
|
345 |
$start->sub(new DateInterval($interval_duration));
|
|
|
346 |
|
|
|
347 |
$sql = "SELECT DISTINCT u.*
|
|
|
348 |
FROM {user} u
|
|
|
349 |
JOIN {user_enrolments} ue ON ue.userid = u.id
|
|
|
350 |
JOIN {enrol} e ON e.id = ue.enrolid
|
|
|
351 |
WHERE e.courseid = :courseid AND ue.timestart < " . $start->getTimestamp() . " $sql_retro
|
|
|
352 |
";
|
|
|
353 |
$params['courseid'] = $mailing->courseid;
|
|
|
354 |
} elseif ($mailing->mailingmode == MAILING_MODE_DAYSFROMLASTCONNECTION && !empty($mailing->courseid)) {
|
|
|
355 |
// retroactive mode
|
|
|
356 |
if (!$mailing->retroactive) {
|
|
|
357 |
$sql_retro = " AND lsl.timecreated >= :timecreated1";
|
|
|
358 |
$params['timecreated1'] = $mailing->timecreated;
|
|
|
359 |
} else {
|
|
|
360 |
$sql_retro = '';
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
$start = new \DateTime();
|
|
|
364 |
$interval_duration = "P" . $mailing->mailingdelay . "D";
|
|
|
365 |
if ($delay_range == 'MINUTE') {
|
|
|
366 |
$interval_duration = "PT" . $mailing->mailingdelay . "M";
|
|
|
367 |
}
|
|
|
368 |
$start->sub(new DateInterval($interval_duration));
|
|
|
369 |
|
|
|
370 |
$sql = "SELECT DISTINCT u.*
|
|
|
371 |
FROM {user} u
|
|
|
372 |
JOIN {course} c ON c.id = :courseid
|
|
|
373 |
JOIN {logstore_standard_log} lsl ON lsl.userid = u.id AND lsl.contextlevel = 50 AND lsl.action = 'viewed' AND lsl.courseid = c.id
|
|
|
374 |
WHERE lsl.timecreated < :timecreated2 $sql_retro
|
|
|
375 |
";
|
|
|
376 |
$params['courseid'] = $mailing->courseid;
|
|
|
377 |
$params['timecreated2'] = $start->getTimestamp();
|
|
|
378 |
} elseif ($mailing->mailingmode == MAILING_MODE_DAYSFROMFIRSTLAUNCH && !empty($mailing->targetmoduleid) && !empty($mailing->mailingdelay)) {
|
|
|
379 |
// retroactive mode
|
|
|
380 |
$join = "JOIN {user_enrolments} ue ON ue.userid = u.id
|
|
|
381 |
JOIN {enrol} e ON e.id = ue.enrolid
|
|
|
382 |
JOIN {course} c ON c.id = e.courseid";
|
|
|
383 |
$sql_where .= " AND c.id = :courseid ";
|
|
|
384 |
$params['courseid'] = $mailing->courseid;
|
|
|
385 |
if (!$mailing->retroactive) {
|
|
|
386 |
$sql_where .= " AND ue.timecreated >= :timecreated1 AND (ue.timestart = 0 OR ue.timestart >= :timestart) AND (ue.timeend = 0 OR ue.timeend >= :timeend)";
|
|
|
387 |
$params['timecreated1'] = $mailing->timecreated;
|
|
|
388 |
$params['timestart'] = $mailing->timecreated;
|
|
|
389 |
$params['timeend'] = $mailing->timecreated;
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
$start = new \DateTime();
|
|
|
393 |
$interval_duration = "P" . $mailing->mailingdelay . "D";
|
|
|
394 |
if ($delay_range == 'MINUTE') {
|
|
|
395 |
$interval_duration = "PT" . $mailing->mailingdelay . "M";
|
|
|
396 |
}
|
|
|
397 |
$start->sub(new DateInterval($interval_duration));
|
|
|
398 |
|
|
|
399 |
//ToDo : other modules than scorm
|
|
|
400 |
if (empty($mailing->targetmodulestatus)) {
|
|
|
401 |
$sql = "SELECT DISTINCT u.*
|
|
|
402 |
FROM {user} u
|
|
|
403 |
$join
|
|
|
404 |
JOIN {logstore_standard_log} lsl ON lsl.userid = u.id AND lsl.contextlevel = 70 AND lsl.contextinstanceid = :targetmoduleid AND lsl.action = 'launched' AND lsl.target = 'sco'
|
|
|
405 |
LEFT JOIN {course_modules_completion} cmc ON cmc.userid = u.id AND cmc.coursemoduleid = :coursemoduleid
|
|
|
406 |
WHERE lsl.timecreated < :timecreated $sql_where
|
|
|
407 |
";
|
|
|
408 |
} else {
|
|
|
409 |
$sql = "SELECT DISTINCT u.*
|
|
|
410 |
FROM {user} u
|
|
|
411 |
$join
|
|
|
412 |
LEFT JOIN {logstore_standard_log} lsl ON lsl.userid = u.id AND lsl.contextlevel = 70 AND lsl.contextinstanceid = :targetmoduleid AND lsl.action = 'launched' AND lsl.target = 'sco'
|
|
|
413 |
LEFT JOIN {course_modules_completion} cmc ON cmc.userid = u.id AND cmc.coursemoduleid = :coursemoduleid
|
|
|
414 |
WHERE (lsl.timecreated < :timecreated OR lsl.timecreated IS NULL) $sql_where
|
|
|
415 |
";
|
|
|
416 |
}
|
|
|
417 |
$params['targetmoduleid'] = $mailing->targetmoduleid;
|
|
|
418 |
$params['coursemoduleid'] = $mailing->targetmoduleid;
|
|
|
419 |
$params['timecreated'] = $start->getTimestamp();
|
|
|
420 |
|
|
|
421 |
} elseif ($mailing->mailingmode == MAILING_MODE_DAYSFROMLASTLAUNCH && !empty($mailing->targetmoduleid) && !empty($mailing->mailingdelay)) {
|
|
|
422 |
// retroactive mode
|
|
|
423 |
if (!$mailing->retroactive) {
|
|
|
424 |
$join_retro = " JOIN {user_enrolments} ue ON ue.userid = u.id
|
|
|
425 |
JOIN {enrol} e ON e.id = ue.enrolid
|
|
|
426 |
JOIN {course} c ON c.id = e.courseid ";
|
|
|
427 |
$sql_where .= " c.id = :courseid AND ue.timecreated >= :timecreated AND (ue.timestart = 0 OR ue.timestart >= :timestart) AND (ue.timeend = 0 OR ue.timeend >= :timeend)";
|
|
|
428 |
$params['courseid'] = $mailing->courseid;
|
|
|
429 |
$params['timecreated'] = $mailing->timecreated;
|
|
|
430 |
$params['timestart'] = $mailing->timecreated;
|
|
|
431 |
$params['timeend'] = $mailing->timecreated;
|
|
|
432 |
} else {
|
|
|
433 |
$join_retro = '';
|
|
|
434 |
}
|
|
|
435 |
|
|
|
436 |
$start = new \DateTime();
|
|
|
437 |
$interval_duration = "P" . $mailing->mailingdelay . "D";
|
|
|
438 |
if ($delay_range == 'MINUTE') {
|
|
|
439 |
$interval_duration = "PT" . $mailing->mailingdelay . "M";
|
|
|
440 |
}
|
|
|
441 |
$start->sub(new DateInterval($interval_duration));
|
|
|
442 |
|
|
|
443 |
//ToDo : other modules than scorm
|
|
|
444 |
$sql = "SELECT DISTINCT u.*
|
|
|
445 |
FROM {user} u
|
|
|
446 |
$join_retro
|
|
|
447 |
JOIN {logstore_standard_log} lsl ON lsl.userid = u.id AND lsl.contextlevel = 70 AND lsl.contextinstanceid = :contextinstanceid AND lsl.action = 'launched' AND lsl.target = 'sco'
|
|
|
448 |
LEFT JOIN {course_modules_completion} cmc ON cmc.userid = u.id AND cmc.coursemoduleid = :coursemoduleid
|
|
|
449 |
WHERE lsl.timecreated < :timecreated $sql_where
|
|
|
450 |
";
|
|
|
451 |
$params['contextinstanceid'] = $mailing->targetmoduleid;
|
|
|
452 |
$params['coursemoduleid'] = $mailing->targetmoduleid;
|
|
|
453 |
$params['timecreated'] = $start->getTimestamp();
|
|
|
454 |
|
|
|
455 |
} elseif ($mailing->mailingmode == MAILING_MODE_SEND_CERTIFICATE && !empty($mailing->customcertmoduleid)) {
|
|
|
456 |
custommailing_certifications($mailing->customcertmoduleid, $mailing->courseid);
|
|
|
457 |
// retroactive mode
|
|
|
458 |
if (!$mailing->retroactive) {
|
|
|
459 |
$join_retro = " JOIN {user_enrolments} ue ON ue.userid = u.id
|
|
|
460 |
JOIN {enrol} e ON e.id = ue.enrolid
|
|
|
461 |
JOIN {course} c ON c.id = e.courseid ";
|
|
|
462 |
$sql_where = " WHERE c.id = :courseid AND ue.timecreated >= :timecreated1 AND (ue.timestart = 0 OR ue.timestart >= :timestart) AND (ue.timeend = 0 OR ue.timeend >= :timeend)";
|
|
|
463 |
$params['courseid'] = $mailing->courseid;
|
|
|
464 |
$params['timecreated1'] = $mailing->timecreated;
|
|
|
465 |
$params['timestart'] = $mailing->timecreated;
|
|
|
466 |
$params['timeend'] = $mailing->timecreated;
|
|
|
467 |
} else {
|
|
|
468 |
$join_retro = '';
|
|
|
469 |
$sql_where = '';
|
|
|
470 |
}
|
|
|
471 |
$sql = "SELECT DISTINCT u.*
|
|
|
472 |
FROM {user} u
|
|
|
473 |
$join_retro
|
|
|
474 |
JOIN {customcert_issues} ci ON ci.userid = u.id AND ci.customcertid = $mailing->customcertmoduleid
|
|
|
475 |
$sql_where
|
|
|
476 |
";
|
|
|
477 |
}
|
|
|
478 |
|
|
|
479 |
$return = [
|
|
|
480 |
'sql' => $sql,
|
|
|
481 |
'params' => $params
|
|
|
482 |
];
|
|
|
483 |
return $return;
|
|
|
484 |
}
|
|
|
485 |
|
|
|
486 |
/**
|
|
|
487 |
* Process custommailing_logs MAILING_LOG_SENT records
|
|
|
488 |
* Send email to each user
|
|
|
489 |
*
|
|
|
490 |
* @throws dml_exception
|
|
|
491 |
*/
|
|
|
492 |
function custommailing_crontask() {
|
|
|
493 |
|
|
|
494 |
global $DB;
|
|
|
495 |
|
|
|
496 |
custommailing_logs_generate();
|
|
|
497 |
|
|
|
498 |
$ids_to_update = [];
|
|
|
499 |
|
|
|
500 |
$sql = "SELECT u.*, u.id as userid, rm.mailingsubject, rm.mailingcontent, rl.id as logid, rm.customcertmoduleid
|
|
|
501 |
FROM {user} u
|
|
|
502 |
JOIN {custommailing_logs} rl ON rl.emailtouserid = u.id
|
|
|
503 |
JOIN {custommailing_mailing} rm ON rm.id = rl.custommailingmailingid
|
|
|
504 |
WHERE rl.emailstatus < :mailing_log_sent";
|
|
|
505 |
$logs = $DB->get_recordset_sql($sql, ['mailing_log_sent' => MAILING_LOG_SENT]);
|
|
|
506 |
|
|
|
507 |
foreach ($logs as $log) {
|
|
|
508 |
|
|
|
509 |
if (!empty($log->customcertmoduleid)) {
|
|
|
510 |
$attachment = custommailing_getcertificate($log->userid, $log->customcertmoduleid);
|
|
|
511 |
} else {
|
|
|
512 |
$attachment = new stdClass();
|
|
|
513 |
$attachment->file = '';
|
|
|
514 |
$attachment->filename = '';
|
|
|
515 |
}
|
|
|
516 |
$log->mailingcontent = str_replace(['%firstname%', '%lastname%'], [$log->firstname, $log->lastname], $log->mailingcontent);
|
|
|
517 |
if (email_to_user($log, core_user::get_support_user(), $log->mailingsubject, strip_tags($log->mailingcontent), $log->mailingcontent, $attachment->file, $attachment->filename)) {
|
|
|
518 |
$ids_to_update[] = $log->logid;
|
|
|
519 |
}
|
|
|
520 |
}
|
|
|
521 |
$logs->close();
|
|
|
522 |
|
|
|
523 |
// Set emailstatus to MAILING_LOG_SENT on each sended email
|
|
|
524 |
if (is_array($ids_to_update) && count($ids_to_update)) {
|
|
|
525 |
list($insql, $sqlparams) = $DB->get_in_or_equal($ids_to_update, SQL_PARAMS_NAMED);
|
|
|
526 |
$sqlparams['mailing_log_sent'] = MAILING_LOG_SENT;
|
|
|
527 |
$DB->execute("UPDATE {custommailing_logs} SET emailstatus = :mailing_log_sent WHERE id $insql", $sqlparams);
|
|
|
528 |
}
|
|
|
529 |
|
|
|
530 |
}
|
|
|
531 |
|
|
|
532 |
/**
|
|
|
533 |
* get certificate (REQUIRE mod/customcert)
|
|
|
534 |
*
|
|
|
535 |
* @param int $userid
|
|
|
536 |
* @param int $customcertid
|
|
|
537 |
* @return stdClass
|
|
|
538 |
* @throws dml_exception
|
|
|
539 |
*/
|
|
|
540 |
function custommailing_getcertificate($userid, $customcertid) {
|
|
|
541 |
|
|
|
542 |
global $DB;
|
|
|
543 |
|
|
|
544 |
$sql = "SELECT c.*, ct.id as templateid, ct.name as templatename, ct.contextid, co.id as courseid,
|
|
|
545 |
co.fullname as coursefullname, co.shortname as courseshortname
|
|
|
546 |
FROM {customcert} c
|
|
|
547 |
JOIN {customcert_templates} ct ON c.templateid = ct.id
|
|
|
548 |
JOIN {course} co ON c.course = co.id
|
|
|
549 |
JOIN {customcert_issues} ci ON ci.customcertid = c.id
|
|
|
550 |
WHERE ci.userid = :userid AND c.id = :certid";
|
|
|
551 |
$customcert = $DB->get_record_sql($sql, ['userid' => $userid, 'certid' => $customcertid]);
|
|
|
552 |
|
|
|
553 |
$template = new \stdClass();
|
|
|
554 |
$template->id = $customcert->templateid;
|
|
|
555 |
$template->name = $customcert->templatename;
|
|
|
556 |
$template->contextid = $customcert->contextid;
|
|
|
557 |
$template = new \mod_customcert\template($template);
|
|
|
558 |
$filecontents = $template->generate_pdf(false, $userid, true);
|
|
|
559 |
|
|
|
560 |
// Set the name of the file we are going to send.
|
|
|
561 |
$filename = $customcert->courseshortname . '_' . $customcert->name;
|
|
|
562 |
$filename = \core_text::entities_to_utf8($filename);
|
|
|
563 |
$filename = strip_tags($filename);
|
|
|
564 |
$filename = rtrim($filename, '.');
|
|
|
565 |
$filename = str_replace('&', '_', $filename) . '.pdf';
|
|
|
566 |
|
|
|
567 |
// Create the file we will be sending.
|
|
|
568 |
$tempdir = make_temp_directory('certificate/attachment');
|
|
|
569 |
$tempfile = $tempdir . '/' . md5(microtime() . $userid) . '.pdf';
|
|
|
570 |
file_put_contents($tempfile, $filecontents);
|
|
|
571 |
|
|
|
572 |
$attach = new stdClass();
|
|
|
573 |
$attach->file = $tempfile;
|
|
|
574 |
$attach->filename = $filename;
|
|
|
575 |
|
|
|
576 |
return $attach;
|
|
|
577 |
}
|
|
|
578 |
|
|
|
579 |
/**
|
|
|
580 |
* @param int $customcertid
|
|
|
581 |
* @param int $courseid
|
|
|
582 |
* @throws coding_exception
|
|
|
583 |
* @throws dml_exception
|
|
|
584 |
* @throws moodle_exception
|
|
|
585 |
*/
|
|
|
586 |
function custommailing_certifications($customcertid, $courseid)
|
|
|
587 |
{
|
|
|
588 |
global $DB;
|
|
|
589 |
|
|
|
590 |
$sql = "SELECT u.*
|
|
|
591 |
FROM {user} u
|
|
|
592 |
JOIN {course} c ON c.id = :courseid
|
|
|
593 |
JOIN {enrol} e ON e.courseid = c.id
|
|
|
594 |
JOIN {user_enrolments} ue ON ue.userid = u.id AND ue.enrolid = e.id";
|
|
|
595 |
|
|
|
596 |
$users = $DB->get_records_sql($sql, ['courseid' => $courseid]);
|
|
|
597 |
foreach ($users as $user) {
|
|
|
598 |
custommailing_certification($user->id, $customcertid, $courseid);
|
|
|
599 |
}
|
|
|
600 |
}
|
|
|
601 |
|
|
|
602 |
/**
|
|
|
603 |
* @param int $userid
|
|
|
604 |
* @param int $customcertid
|
|
|
605 |
* @param int $courseid
|
|
|
606 |
* @throws coding_exception
|
|
|
607 |
* @throws dml_exception
|
|
|
608 |
* @throws moodle_exception
|
|
|
609 |
*/
|
|
|
610 |
function custommailing_certification($userid, $customcertid, $courseid)
|
|
|
611 |
{
|
|
|
612 |
global $DB;
|
|
|
613 |
|
|
|
614 |
$sql = "SELECT cm.*, m.name, md.name AS modname
|
|
|
615 |
FROM {course_modules} cm
|
|
|
616 |
JOIN {modules} md ON md.id = cm.module
|
|
|
617 |
JOIN {customcert} m ON m.id = cm.instance
|
|
|
618 |
WHERE cm.instance = :cminstance AND md.name = :modulename AND cm.course = :courseid";
|
|
|
619 |
|
|
|
620 |
$cm = $DB->get_record_sql($sql, ['cminstance' => $customcertid, 'modulename' => 'customcert', 'courseid' => $courseid]);
|
|
|
621 |
// $cm = get_coursemodule_from_id('customcert', $cmid, $courseid, false, MUST_EXIST);
|
|
|
622 |
$modinfo = get_fast_modinfo($courseid);
|
|
|
623 |
$cminfo = $modinfo->get_cm($cm->id);
|
|
|
624 |
$ainfomod = new \core_availability\info_module($cminfo);
|
|
|
625 |
|
|
|
626 |
if ($ainfomod->is_available($availabilityinfo, false, $userid)) {
|
|
|
627 |
$customcertissue = new stdClass();
|
|
|
628 |
$customcertissue->customcertid = $customcertid;
|
|
|
629 |
$customcertissue->userid = $userid;
|
|
|
630 |
$customcertissue->code = \mod_customcert\certificate::generate_code();
|
|
|
631 |
$customcertissue->timecreated = time();
|
|
|
632 |
|
|
|
633 |
if (!$DB->record_exists('customcert_issues', ['userid' => $userid, 'customcertid' => $customcertid])) {
|
|
|
634 |
$DB->insert_record('customcert_issues', $customcertissue);
|
|
|
635 |
}
|
|
|
636 |
}
|
|
|
637 |
|
|
|
638 |
}
|