From df058698b02c7ae1118b7d0c3b2db7d683de30e2 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 23 May 2018 14:01:55 +0800 Subject: [PATCH] Added helper function to issue certificates --- classes/certificate.php | 21 +++++++++++++++++++++ classes/task/email_certificate_task.php | 10 +--------- tests/email_certificate_task_test.php | 10 +--------- view.php | 8 +------- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/classes/certificate.php b/classes/certificate.php index 918c188..84afcbd 100644 --- a/classes/certificate.php +++ b/classes/certificate.php @@ -421,6 +421,27 @@ class certificate { return $DB->get_records_sql($sql, array('userid' => $userid), $limitfrom, $limitnum); } + /** + * Issues a certificate to a user. + * + * @param int $certificateid The ID of the certificate + * @param int $userid The ID of the user to issue the certificate to + * @return int The ID of the issue + */ + public static function issue_certificate($certificateid, $userid) { + global $DB; + + $issue = new \stdClass(); + $issue->userid = $userid; + $issue->customcertid = $certificateid; + $issue->code = self::generate_code(); + $issue->emailed = 0; + $issue->timecreated = time(); + + // Insert the record into the database. + return $DB->insert_record('customcert_issues', $issue); + } + /** * Generates a 10-digit code of random letters and numbers. * diff --git a/classes/task/email_certificate_task.php b/classes/task/email_certificate_task.php index 3a305a8..88fe7a6 100644 --- a/classes/task/email_certificate_task.php +++ b/classes/task/email_certificate_task.php @@ -130,15 +130,7 @@ class email_certificate_task extends \core\task\scheduled_task { array('userid' => $enroluser->id, 'customcertid' => $customcert->id)); if (empty($issueid)) { // Ok, issue them the certificate. - $customcertissue = new \stdClass(); - $customcertissue->customcertid = $customcert->id; - $customcertissue->userid = $enroluser->id; - $customcertissue->code = \mod_customcert\certificate::generate_code(); - $customcertissue->emailed = 0; - $customcertissue->timecreated = time(); - - // Insert the record into the database. - $issueid = $DB->insert_record('customcert_issues', $customcertissue); + $issueid = \mod_customcert\certificate::issue_certificate($customcert->id, $enroluser->id); } // Add them to the array so we email them. diff --git a/tests/email_certificate_task_test.php b/tests/email_certificate_task_test.php index 972baec..b05f7f1 100644 --- a/tests/email_certificate_task_test.php +++ b/tests/email_certificate_task_test.php @@ -71,15 +71,7 @@ class mod_customcert_task_email_certificate_task_testcase extends advanced_testc 'emailstudents' => 1)); // Ok, now issue this to one user. - $customcertissue = new stdClass(); - $customcertissue->customcertid = $customcert->id; - $customcertissue->userid = $user1->id; - $customcertissue->code = \mod_customcert\certificate::generate_code(); - $customcertissue->timecreated = time(); - $customcertissue->emailed = 0; - - // Insert the record into the database. - $DB->insert_record('customcert_issues', $customcertissue); + \mod_customcert\certificate::issue_certificate($customcert->id, $user1->id); // Confirm there is only one entry in this table. $this->assertEquals(1, $DB->count_records('customcert_issues')); diff --git a/view.php b/view.php index 5d09752..4a99839 100644 --- a/view.php +++ b/view.php @@ -121,13 +121,7 @@ if (empty($action)) { } else { // Output to pdf. // Create new customcert issue record if one does not already exist. if (!$DB->record_exists('customcert_issues', array('userid' => $USER->id, 'customcertid' => $customcert->id))) { - $customcertissue = new stdClass(); - $customcertissue->customcertid = $customcert->id; - $customcertissue->userid = $USER->id; - $customcertissue->code = \mod_customcert\certificate::generate_code(); - $customcertissue->timecreated = time(); - // Insert the record into the database. - $DB->insert_record('customcert_issues', $customcertissue); + \mod_customcert\certificate::issue_certificate($customcert->id, $USER->id); } // Set the custom certificate as viewed.