Skip to content

The loop in "email_certificate_task.php" that says it removes users already emailed appears redundant #608

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
JackAidley opened this issue Feb 28, 2024 · 3 comments

Comments

@JackAidley
Copy link

I've been looking at this task because it is taking a long time even when nothing is happening - hopefully that'll be resolved by #596 - but while looking I noticed that this loop:

// Remove all the users who have already been emailed.
foreach ($issuedusers as $key => $issueduser) {
    if ($issueduser->emailed) {
        unset($issuedusers[$key]);
    }
}

Appears to do nothing since, as far as I can tell, the emailed flag is only ever set in the code a few lines above where it is set to 0:

// Add them to the array so we email them.
$enroluser->issueid = $issueid;
$enroluser->emailed = 0;
$issuedusers[] = $enroluser;

And so can never have a truthy value. I'm unclear whether this is actually causing a bug or whether it's just redundant.

@mdjnelson
Copy link
Owner

I had to think about this one for a while to understand why I put that there. Turns out it is needed.

This is needed because we get all the issued users in $issuedusers = $DB->get_records_sql($sql, ['customcertid' => $customcert->id]);. It then gets all the enrolled users in the course, and if they can see the certificate adds them to that list. The loop you pointed out removes the ones that are already emailed that were retrieved from the DB call I listed. We can not filter by email in the get_records_sql call because we want all users. If we just got the people who werent emailed then it would keep emailing users who already have it because they are in the enrolled users call.

@JackAidley
Copy link
Author

Ah gotcha. Sorry I missed that on reading the code. One thought though, wouldn't it be better to screen out the issuedusers with emailed set to non-zero in the SQL query that returns them instead of fetching unwanted records and then screening them out? I don't suppose it's particularly performance critical either way.

@mdjnelson
Copy link
Owner

The problem with that is that the $issuedusers is used to check if the user has already been issued.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants