Skip to content

Commit

Permalink
Optimise email certificate task. (mdjnelson#531)
Browse files Browse the repository at this point in the history
By reducing database reads/writes and introducing
configurable settings for task efficiency.
  • Loading branch information
mohamedmohamedatia committed Aug 4, 2024
1 parent 8f77308 commit 4950be3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 40 deletions.
17 changes: 9 additions & 8 deletions classes/task/email_certificate_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function execute() {
$certificateexecutionperiod = (int)get_config('customcert', 'certificateexecutionperiod');

// Get the last processed batch and total certificates to process.
$taskprogress = $DB->get_record('customcert_task_progress', ['taskname' => 'email_certificate_task']);
$taskprogress = $DB->get_record('customcert_email_task_prgrs', ['taskname' => 'email_certificate_task']);
$lastprocessed = $taskprogress->last_processed;

// Get all the certificates that have requested someone get emailed.
Expand Down Expand Up @@ -101,7 +101,7 @@ public function execute() {

// Store the total count of certificates in the database.
$totalcertificatestoprocess = count($customcerts);
$DB->set_field('customcert_task_progress', 'total_certificate_to_process', $totalcertificatestoprocess, [
$DB->set_field('customcert_email_task_prgrs', 'total_certificate_to_process', $totalcertificatestoprocess, [
'taskname' => 'email_certificate_task',
]);

Expand Down Expand Up @@ -174,11 +174,9 @@ public function execute() {

// 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($context, 'mod/customcert:receiveissue',
'u.id, username, firstname, lastname, email, firstnamephonetic, lastnamephonetic, middlename, alternatename');
$userswithissue = get_users_by_capability($context, 'mod/customcert:receiveissue');
// Get users with mod/customcert:view capability.
$userswithview = get_users_by_capability($context, 'mod/customcert:view',
'u.id, username, firstname, lastname, email, firstnamephonetic, lastnamephonetic, middlename, alternatename');
$userswithview = get_users_by_capability($context, 'mod/customcert:view');
// Users with both mod/customcert:view and mod/customcert:receiveissue cabapilities.
$userswithissueview = array_intersect_key($userswithissue, $userswithview);

Expand Down Expand Up @@ -313,14 +311,17 @@ public function execute() {
// Set the field so that it is emailed.
$issueids[] = $user->issueid;
}

if (!empty($issueids)) {
$DB->set_field_select('customcert_issues', 'emailed', 1, 'id IN (' . implode(',', $issueids) . ')');
list($sql, $params) = $DB->get_in_or_equal($issueids, SQL_PARAMS_NAMED, 'id');
$DB->set_field_select('customcert_issues', 'emailed', 1, 'id ' . $sql, $params);
}
}

// Update the last processed position, if run in batches.
if ($certificatesperrun > 0) {
$newlastprocessed = $lastprocessed + count($certificates);
$DB->set_field('customcert_task_progress', 'last_processed', $newlastprocessed, [
$DB->set_field('customcert_email_task_prgrs', 'last_processed', $newlastprocessed, [
'taskname' => 'email_certificate_task',
]);
}
Expand Down
11 changes: 3 additions & 8 deletions db/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Customcert module upgrade code.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @copyright 2024 Mohamed Atia <matia12[@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

Expand All @@ -35,18 +35,13 @@
function xmldb_customcert_install() {
global $DB;

$dbman = $DB->get_manager();

// Add a default row to the customcert_task_progress table.
// Add a default row to the customcert_email_task_prgrs table.
$defaultdata = new stdClass();
$defaultdata->taskname = 'email_certificate_task';
$defaultdata->last_processed = 0;
$defaultdata->total_certificate_to_process = 0;

// Write close to ensure the transaction is committed.
\core\session\manager::write_close();

// Insert the default data into the table.
$DB->insert_record('customcert_task_progress', $defaultdata);
$DB->insert_record('customcert_email_task_prgrs', $defaultdata);
return true;
}
2 changes: 1 addition & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<KEY NAME="page" TYPE="foreign" FIELDS="pageid" REFTABLE="customcert_pages" REFFIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="customcert_task_progress" COMMENT="to track email task progress">
<TABLE NAME="customcert_email_task_prgrs" COMMENT="to track email task progress">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="taskname" TYPE="char" LENGTH="255" NOTNULL="true" DEFAULT="email_certificate_task" SEQUENCE="false"/>
Expand Down
24 changes: 11 additions & 13 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,38 +235,36 @@ function xmldb_customcert_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2023042405, 'customcert');
}

if ($oldversion < 2023042408) {
if ($oldversion < 2023042409) {

// Define table customcert_task_progress to be created.
$table = new xmldb_table('customcert_task_progress');
// Define table customcert_email_task_prgrs to be created.
$table = new xmldb_table('customcert_email_task_prgrs');

// Adding fields to table customcert_task_progress.
// Adding fields to table customcert_email_task_prgrs.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('taskname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'email_certificate_task');
$table->add_field('last_processed', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, '0');
$table->add_field('total_certificate_to_process', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, '0');

// Adding keys to table customcert_task_progress.
// Adding keys to table customcert_email_task_prgrs.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('usermodified', XMLDB_KEY_FOREIGN, ['usermodified'], 'user', ['id']);

// Conditionally launch create table for customcert_task_progress.
// Conditionally launch create table for customcert_email_task_prgrs.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
// Add a default row to the customcert_task_progress table.

// Add a default row to the customcert_email_task_prgrs table.
$defaultdata = new stdClass();
$defaultdata->taskname = 'email_certificate_task';
$defaultdata->last_processed = 0;
$defaultdata->total_certificate_to_process = 0;

// Write close to ensure the transaction is committed.
\core\session\manager::write_close();

// Insert the default data into the table.
$DB->insert_record('customcert_task_progress', $defaultdata);
$DB->insert_record('customcert_email_task_prgrs', $defaultdata);
}

// Customcert savepoint reached.
upgrade_mod_savepoint(true, 2023042408, 'customcert');
upgrade_mod_savepoint(true, 2023042409, 'customcert');
}
return true;
}
17 changes: 8 additions & 9 deletions lang/en/customcert.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
$string['awardedto'] = 'Awarded to';
$string['cannotverifyallcertificates'] = 'You do not have the permission to verify all certificates on the site.';
$string['certificate'] = 'Certificate';
$string['certificateexecutionperiod'] = 'Certificate execution period';
$string['certificateexecutionperiod_desc'] = 'Specify the period for which certificates should be executed based on their end date. Set to 0 to execute all certificates, regardless of their age.';
$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['code'] = 'Code';
$string['copy'] = 'Copy';
$string['coursetimereq'] = 'Required minutes in course';
Expand Down Expand Up @@ -122,6 +126,8 @@
$string['gradeoutcome'] = 'Outcome';
$string['height'] = 'Height';
$string['height_help'] = 'This is the height of the certificate PDF in mm. For reference an A4 piece of paper is 297mm high and a letter is 279mm high.';
$string['includeinnotvisiblecourses'] = 'Include certificates in hidden courses';
$string['includeinnotvisiblecourses_desc'] = 'Check this box to include certificates in courses that are not visible to the user.';
$string['invalidcode'] = 'Invalid code supplied.';
$string['invalidcolour'] = 'Invalid colour chosen, please enter a valid HTML colour name, or a six-digit, or three-digit hexadecimal colour.';
$string['invalidelementwidthorheightnotnumber'] = 'Please enter a valid number.';
Expand Down Expand Up @@ -192,6 +198,8 @@
$string['saveandcontinue'] = 'Save and continue';
$string['savechangespreview'] = 'Save changes and preview';
$string['savetemplate'] = 'Save template';
$string['scheduledtaskconfigdesc'] = 'Configure the settings for the scheduled task that processes certificates.';
$string['scheduledtaskconfigheading'] = 'Scheduled task configuration';
$string['search:activity'] = 'Custom certificate - activity information';
$string['setprotection'] = 'Set protection';
$string['setprotection_help'] = 'Choose the actions you wish to prevent users from performing on this certificate.';
Expand Down Expand Up @@ -230,12 +238,3 @@

// Acess API.
$string['customcert:managelanguages'] = 'Manage language on edit form';

$string['certificatesperrun'] = 'Certificates per run';
$string['certificatesperrun_desc'] = 'Enter the number of certificates to process per scheduled task run <b>where 0 means it will process all certificates</b>.';
$string['includeinnotvisiblecourses'] = 'Include certificates in hidden courses';
$string['includeinnotvisiblecourses_desc'] = 'Check this box to include certificates in courses that are not visible to the user.';
$string['certificateexecutionperiod'] = 'Certificate execution period';
$string['certificateexecutionperiod_desc'] = 'Specify the period for which certificates should be executed based on their end date. <b>Set to 0 to execute all certificates, regardless of their age.</b>';
$string['scheduledtaskconfigheading'] = 'Scheduled task configuration';
$string['scheduledtaskconfigdesc'] = 'Configure the settings for the scheduled task that processes certificates.';
1 change: 0 additions & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
new moodle_url('/mod/customcert/download_all_certificates.php'), ''));
}


$settings->add(new admin_setting_heading('scheduledtaskconfig',
get_string('scheduledtaskconfigheading', 'customcert'),
get_string('scheduledtaskconfigdesc', 'customcert')));
Expand Down

0 comments on commit 4950be3

Please sign in to comment.