From 8cb3dab034ce6df05567be0af4e656eaaa47c949 Mon Sep 17 00:00:00 2001 From: Peter Burnett Date: Wed, 17 Jan 2024 15:54:09 +1000 Subject: [PATCH] Email task performance improvements and time limit --- classes/task/email_certificate_task.php | 11 +++++++++-- tests/email_certificate_task_test.php | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/classes/task/email_certificate_task.php b/classes/task/email_certificate_task.php index f364b815..17d4bd87 100644 --- a/classes/task/email_certificate_task.php +++ b/classes/task/email_certificate_task.php @@ -49,6 +49,7 @@ public function get_name() { public function execute() { global $DB; + $lastruntime = $DB->get_field('task_scheduled', 'lastruntime', ['classname' => "\\" . self::class]); // Get all the certificates that have requested someone get emailed. $emailotherslengthsql = $DB->sql_length('c.emailothers'); $sql = "SELECT c.*, ct.id as templateid, ct.name as templatename, ct.contextid, co.id as courseid, @@ -60,8 +61,14 @@ public function execute() { ON c.course = co.id WHERE (c.emailstudents = :emailstudents OR c.emailteachers = :emailteachers - OR $emailotherslengthsql >= 3)"; - if (!$customcerts = $DB->get_records_sql($sql, array('emailstudents' => 1, 'emailteachers' => 1))) { + OR $emailotherslengthsql >= 3) + AND :lastruntime <= ( + SELECT MAX(ula.timeaccess) + FROM {user_lastaccess} ula + WHERE ula.courseid = co.id + )"; + $params = ['emailstudents' => 1, 'emailteachers' => 1, "lastruntime" => $lastruntime]; + if (!$customcerts = $DB->get_records_sql($sql, $params)) { return; } diff --git a/tests/email_certificate_task_test.php b/tests/email_certificate_task_test.php index 6ddc4a2c..36eed810 100644 --- a/tests/email_certificate_task_test.php +++ b/tests/email_certificate_task_test.php @@ -54,6 +54,7 @@ public function setUp(): void { * @covers \mod_customcert\task\email_certificate_task */ public function test_email_certificates_no_elements() { + global $DB; // Create a course. $course = $this->getDataGenerator()->create_course(); @@ -66,6 +67,9 @@ public function test_email_certificates_no_elements() { // Enrol the user as a student. $this->getDataGenerator()->enrol_user($user1->id, $course->id); + // Register activity in the course to make it valid to be sent. + $DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]); + // Run the task. $sink = $this->redirectEmails(); $task = new email_certificate_task(); @@ -101,6 +105,9 @@ public function test_email_certificates_no_cap() { // Create a custom certificate. $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id, 'emailstudents' => 1]); + // Register activity in the course to make it valid to be sent. + $DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]); + // Create template object. $template = new stdClass(); $template->id = $customcert->templateid; @@ -155,6 +162,9 @@ public function test_email_certificates_students() { $customcert = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id, 'emailstudents' => 1)); + // Register activity in the course to make it valid to be sent. + $DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]); + // Create template object. $template = new stdClass(); $template->id = $customcert->templateid; @@ -241,6 +251,8 @@ public function test_email_certificates_teachers() { // Create a custom certificate. $customcert = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id, 'emailteachers' => 1)); + // Register activity in the course to make it valid to be sent. + $DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]); // Create template object. $template = new stdClass(); @@ -297,6 +309,9 @@ public function test_email_certificates_others() { $customcert = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id, 'emailothers' => 'testcustomcert@example.com, doo@dah')); + // Register activity in the course to make it valid to be sent. + $DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]); + // Create template object. $template = new stdClass(); $template->id = $customcert->templateid; @@ -350,6 +365,9 @@ public function test_email_certificates_students_not_visible() { // Create a custom certificate. $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id, 'emailstudents' => 1]); + // Register activity in the course to make it valid to be sent. + $DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]); + // Create template object. $template = new stdClass(); $template->id = $customcert->templateid; @@ -407,6 +425,9 @@ public function test_email_certificates_students_havent_met_required_time() { $customcert = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id, 'emailstudents' => 1, 'requiredtime' => '60')); + // Register activity in the course to make it valid to be sent. + $DB->insert_record('user_lastaccess', ['userid' => $user1->id, 'courseid' => $course->id, 'lastaccess' => time()]); + // Create template object. $template = new stdClass(); $template->id = $customcert->templateid;