Skip to content

Commit 31c296f

Browse files
committed
Email task performance improvements and time limit
1 parent 9315de6 commit 31c296f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

classes/task/email_certificate_task.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function get_name() {
4949
public function execute() {
5050
global $DB;
5151

52+
$lastruntime = $DB->get_field('task_scheduled', 'lastruntime', ['classname' => "\\" . self::class]);
5253
// Get all the certificates that have requested someone get emailed.
5354
$emailotherslengthsql = $DB->sql_length('c.emailothers');
5455
$sql = "SELECT c.*, ct.id as templateid, ct.name as templatename, ct.contextid, co.id as courseid,
@@ -60,8 +61,14 @@ public function execute() {
6061
ON c.course = co.id
6162
WHERE (c.emailstudents = :emailstudents
6263
OR c.emailteachers = :emailteachers
63-
OR $emailotherslengthsql >= 3)";
64-
if (!$customcerts = $DB->get_records_sql($sql, array('emailstudents' => 1, 'emailteachers' => 1))) {
64+
OR $emailotherslengthsql >= 3)
65+
AND :lastruntime <= (
66+
SELECT MAX(ula.timeaccess)
67+
FROM {user_lastaccess} ula
68+
WHERE ula.courseid = co.id
69+
)";
70+
71+
if (!$customcerts = $DB->get_records_sql($sql, ['emailstudents' => 1, 'emailteachers' => 1, "lastruntime" => $lastruntime])) {
6572
return;
6673
}
6774

tests/email_certificate_task_test.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function setUp(): void {
5454
* @covers \mod_customcert\task\email_certificate_task
5555
*/
5656
public function test_email_certificates_no_elements() {
57+
global $DB;
5758
// Create a course.
5859
$course = $this->getDataGenerator()->create_course();
5960

@@ -66,6 +67,9 @@ public function test_email_certificates_no_elements() {
6667
// Enrol the user as a student.
6768
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
6869

70+
// Register activity in the course to make it valid to be sent.
71+
$DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]);
72+
6973
// Run the task.
7074
$sink = $this->redirectEmails();
7175
$task = new email_certificate_task();
@@ -101,6 +105,9 @@ public function test_email_certificates_no_cap() {
101105
// Create a custom certificate.
102106
$customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id, 'emailstudents' => 1]);
103107

108+
// Register activity in the course to make it valid to be sent.
109+
$DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]);
110+
104111
// Create template object.
105112
$template = new stdClass();
106113
$template->id = $customcert->templateid;
@@ -155,6 +162,9 @@ public function test_email_certificates_students() {
155162
$customcert = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id,
156163
'emailstudents' => 1));
157164

165+
// Register activity in the course to make it valid to be sent.
166+
$DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]);
167+
158168
// Create template object.
159169
$template = new stdClass();
160170
$template->id = $customcert->templateid;
@@ -241,6 +251,8 @@ public function test_email_certificates_teachers() {
241251
// Create a custom certificate.
242252
$customcert = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id,
243253
'emailteachers' => 1));
254+
// Register activity in the course to make it valid to be sent.
255+
$DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]);
244256

245257
// Create template object.
246258
$template = new stdClass();
@@ -297,6 +309,9 @@ public function test_email_certificates_others() {
297309
$customcert = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id,
298310
'emailothers' => '[email protected], doo@dah'));
299311

312+
// Register activity in the course to make it valid to be sent.
313+
$DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]);
314+
300315
// Create template object.
301316
$template = new stdClass();
302317
$template->id = $customcert->templateid;
@@ -350,6 +365,9 @@ public function test_email_certificates_students_not_visible() {
350365
// Create a custom certificate.
351366
$customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id, 'emailstudents' => 1]);
352367

368+
// Register activity in the course to make it valid to be sent.
369+
$DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]);
370+
353371
// Create template object.
354372
$template = new stdClass();
355373
$template->id = $customcert->templateid;
@@ -407,6 +425,9 @@ public function test_email_certificates_students_havent_met_required_time() {
407425
$customcert = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id, 'emailstudents' => 1,
408426
'requiredtime' => '60'));
409427

428+
// Register activity in the course to make it valid to be sent.
429+
$DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]);
430+
410431
// Create template object.
411432
$template = new stdClass();
412433
$template->id = $customcert->templateid;

0 commit comments

Comments
 (0)