Skip to content

Issue #531: Email task performance improvements and time limit #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions classes/task/email_certificate_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/email_certificate_task_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -297,6 +309,9 @@ public function test_email_certificates_others() {
$customcert = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id,
'emailothers' => '[email protected], 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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down