Added helper function to issue certificates

This commit is contained in:
Mark Nelson 2018-05-23 14:01:55 +08:00
parent 06cf83ffe9
commit df058698b0
4 changed files with 24 additions and 25 deletions

View file

@ -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.
*

View file

@ -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.

View file

@ -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'));

View file

@ -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.