Skip to content

Commit

Permalink
DEF-2908: Refactoring task to issuetask and adhoc email tak, setting …
Browse files Browse the repository at this point in the history
…to improve performance

DEF-2908: Improving code a little bit

DEF-2908: Fixing pipelines and adding adhoc task test

DEF-2908: Fixing pipeline error

DEF-2908: Fixing unnecesary changes

DEF-2908: Fixing test
  • Loading branch information
ojnadjarm committed Aug 23, 2024
1 parent 94cfd4b commit 1be0571
Show file tree
Hide file tree
Showing 7 changed files with 435 additions and 258 deletions.
340 changes: 93 additions & 247 deletions classes/task/email_certificate_task.php

Large diffs are not rendered by default.

191 changes: 191 additions & 0 deletions classes/task/issue_certificates_task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* A scheduled task for issuing certificates that have requested someone get emailed.
*
* @package mod_customcert
* @copyright 2024 Oscar Nadjar <oscar.nadjar@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert\task;

use mod_customcert\helper;

/**
* A scheduled task for issuing certificates that have requested someone get emailed.
*
* @package mod_customcert
* @copyright 2024 Oscar Nadjar <oscar.nadjar@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class issue_certificates_task extends \core\task\scheduled_task {

/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('issuecertificate', 'customcert');
}

/**
* Execute.
*/
public function execute() {
global $DB;

// Get the certificatesperrun, includeinnotvisiblecourses, and certificateexecutionperiod configurations.
$certificatesperrun = (int)get_config('customcert', 'certificatesperrun');
$includeinnotvisiblecourses = (bool)get_config('customcert', 'includeinnotvisiblecourses');
$certificateexecutionperiod = (int)get_config('customcert', 'certificateexecutionperiod');
$offset = (int)get_config('customcert', 'certificate_offset');

// We are going to issue 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,
co.fullname as coursefullname, co.shortname as courseshortname
FROM {customcert} c
JOIN {customcert_templates} ct ON c.templateid = ct.id
JOIN {course} co ON c.course = co.id
JOIN {course_categories} cat ON co.category = cat.id
LEFT JOIN {customcert_issues} ci ON c.id = ci.customcertid";

// Add conditions to exclude certificates from hidden courses.
$sql .= " WHERE (c.emailstudents = :emailstudents
OR c.emailteachers = :emailteachers
OR $emailotherslengthsql >= 3)";

$params = ['emailstudents' => 1, 'emailteachers' => 1];

// Check the includeinnotvisiblecourses configuration.
if (!$includeinnotvisiblecourses) {
// Exclude certificates from hidden courses.
$sql .= " AND co.visible = 1 AND cat.visible = 1";
}
// Add condition based on certificate execution period.
if ($certificateexecutionperiod > 0) {
// Include courses with no end date or end date greater than the specified period.
$sql .= " AND (co.enddate > :enddate OR (co.enddate = 0 AND ci.timecreated > :enddate2))";
$params['enddate'] = time() - $certificateexecutionperiod;
$params['enddate2'] = $params['enddate'];
}

$sql .= " GROUP BY c.id, ct.id, ct.name, ct.contextid, co.id, co.fullname, co.shortname";

// Execute the SQL query.
$customcerts = $DB->get_records_sql($sql, $params, $offset, $certificatesperrun);

// When we get to the end of the list, reset the offset.
set_config('certificate_offset', !empty($customcerts) ? $offset + $certificatesperrun : 0, 'customcert');
if (empty($customcerts)) {
return;
}

foreach ($customcerts as $customcert) {

// Check if the certificate is hidden, quit early.
$cm = get_course_and_cm_from_instance($customcert->id, 'customcert', $customcert->course)[1];
if (!$cm->visible) {
continue;
}

// Do not process an empty certificate.
$sql = "SELECT ce.*
FROM {customcert_elements} ce
JOIN {customcert_pages} cp ON cp.id = ce.pageid
JOIN {customcert_templates} ct ON ct.id = cp.templateid
WHERE ct.contextid = :contextid";
if (!$DB->record_exists_sql($sql, ['contextid' => $customcert->contextid])) {
continue;
}

// Get the context.
$context = \context::instance_by_id($customcert->contextid);

// Get a list of all the issues.
$sql = "SELECT u.id
FROM {customcert_issues} ci
JOIN {user} u
ON ci.userid = u.id
WHERE ci.customcertid = :customcertid
AND ci.emailed = 1";
$issuedusers = $DB->get_records_sql($sql, ['customcertid' => $customcert->id]);

// Now, get a list of users who can Manage the certificate.
$userswithmanage = get_users_by_capability($context, 'mod/customcert:manage', 'u.id');

// Get the context of the Custom Certificate module.
$cmcontext = \context_module::instance($cm->id);

// Now, get a list of users who can view and issue the certificate but have not yet.
// Get users with the mod/customcert:receiveissue capability in the Custom Certificate module context.
$userswithissue = get_users_by_capability($cmcontext, 'mod/customcert:receiveissue');
// Get users with mod/customcert:view capability.
$userswithview = get_users_by_capability($cmcontext, 'mod/customcert:view');
// Users with both mod/customcert:view and mod/customcert:receiveissue cabapilities.
$userswithissueview = array_intersect_key($userswithissue, $userswithview);

foreach ($userswithissueview as $enroluser) {
// Check if the user has already been issued and emailed.
if (in_array($enroluser->id, array_keys((array)$issuedusers))) {
continue;
}

// Don't want to issue to teachers.
if (in_array($enroluser->id, array_keys((array)$userswithmanage))) {
continue;
}

// Now check if the certificate is not visible to the current user.
$cm = get_fast_modinfo($customcert->courseid, $enroluser->id)->instances['customcert'][$customcert->id];
if (!$cm->uservisible) {
continue;
}

// Check that they have passed the required time.
if (!empty($customcert->requiredtime)) {
if (\mod_customcert\certificate::get_course_time($customcert->courseid,
$enroluser->id) < ($customcert->requiredtime * 60)) {
continue;
}
}

// Ensure the cert hasn't already been issued, e.g via the UI (view.php) - a race condition.
$issue = $DB->get_record('customcert_issues',
['userid' => $enroluser->id, 'customcertid' => $customcert->id], 'id, emailed');

// Ok, issue them the certificate.
$issueid = empty($issue) ?
\mod_customcert\certificate::issue_certificate($customcert->id, $enroluser->id) : $issue->id;

// Validate issueid and one last check for emailed.
if (!empty($issueid) && empty($issue->emailed)) {
// We create a new adhoc task to send the email.
$task = new \mod_customcert\task\email_certificate_task();
$task->set_custom_data(['issueid' => $issueid, 'customcertid' => $customcert->id]);
$useadhoc = get_config('customcert', 'useadhoc');
if ($useadhoc) {
\core\task\manager::queue_adhoc_task($task);
} else {
$task->execute();
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion db/tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

$tasks = [
[
'classname' => 'mod_customcert\task\email_certificate_task',
'classname' => 'mod_customcert\task\issue_certificates_task',
'blocking' => 0,
'minute' => '*',
'hour' => '*',
Expand Down
18 changes: 18 additions & 0 deletions lang/en/customcert.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,21 @@
$string['verifycertificatedesc'] = 'This link will take you to a new screen where you will be able to verify certificates on the site';
$string['width'] = 'Width';
$string['width_help'] = 'This is the width of the certificate PDF in mm. For reference an A4 piece of paper is 210mm wide and a letter is 216mm wide.';

$string['userlanguage'] = 'Use user preferences';
$string['languageoptions'] = 'Force Certificate Language';
$string['userlanguage_help'] = 'You can force the language of the certificate to override the user\'s language preferences.';

// Scheduled task configuration performance settings.
$string['certificateexecutionperiod'] = 'Ignore inactive certificates older than';
$string['certificateexecutionperiod_desc'] = 'If not 0, the task will not process certificates whose course has been inactive or the last issue is older than the configured time. This may help to improve the performance of the scheduled task.';
$string['certificatesperrun'] = 'Certificates per run';
$string['certificatesperrun_desc'] = 'Enter the number of certificates to process per scheduled task run where 0 means it will process all certificates.';
$string['customcert:managelanguages'] = 'Manage language on edit form';
$string['includeinnotvisiblecourses'] = 'Include certificates in hidden courses';
$string['includeinnotvisiblecourses_desc'] = 'Certificates from hidden courses are not proccesed by default. If you want to include them, enable this setting.';
$string['scheduledtaskconfigheading'] = 'Scheduled task configuration';
$string['scheduledtaskconfigdesc'] = 'Configure the settings for the scheduled task that processes certificates.';
$string['issuecertificate'] = 'Issue certificates task';
$string['useadhoc'] = 'Use Email Certificate adhoc task';
$string['useadhoc_desc'] = 'If enabled, the email will be processed on an adhoc task created per issue. If disabled, the email will be processed by the scheduled task. This may help to improve the performance of the scheduled task.';
4 changes: 4 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
get_string('includeinnotvisiblecourses', 'customcert'),
get_string('includeinnotvisiblecourses_desc', 'customcert'), 0));

$settings->add(new admin_setting_configcheckbox('customcert/useadhoc',
get_string('useadhoc', 'customcert'),
get_string('useadhoc_desc', 'customcert'), 0));

$settings->add(new admin_setting_configduration('customcert/certificateexecutionperiod',
get_string('certificateexecutionperiod', 'customcert'),
get_string('certificateexecutionperiod_desc', 'customcert'), 365 * DAYSECS));
Expand Down
Loading

0 comments on commit 1be0571

Please sign in to comment.