Current status

This commit is contained in:
Kumi 2022-09-07 13:59:29 +00:00
parent df7499249e
commit d406c4ae16
Signed by: kumi
GPG key ID: ECBCC9082395383F
4 changed files with 166 additions and 161 deletions

View file

@ -18,7 +18,7 @@
* The report that displays the certificates the user has throughout the site.
*
* @package mod_htmlcert
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2022 Kumi Systems e.U. <office@kumi.systems>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -34,7 +34,7 @@ require_once($CFG->libdir . '/tablelib.php');
* Class for the report that displays the certificates the user has throughout the site.
*
* @package mod_htmlcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2022 Kumi Systems e.U. <office@kumi.systems>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class my_certificates_table extends \table_sql {
@ -148,7 +148,7 @@ class my_certificates_table extends \table_sql {
'certificateid' => $certificate->id,
'downloadcert' => '1'));
return $OUTPUT->action_link($link, '', null, null, $icon);
return $OUTPUT->action_link($link, '', new \popup_action('click', $link), null, $icon);
}
/**

View file

@ -18,7 +18,7 @@
* A scheduled task for emailing certificates.
*
* @package mod_htmlcert
* @copyright 2017 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
* @copyright 2017 Mark Nelson <markn@moodle.com>, 2021 Kumi Systems e.U. <office@kumi.systems>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_htmlcert\task;
@ -71,176 +71,180 @@ class email_certificate_task extends \core\task\scheduled_task {
$htmlrenderer = $PAGE->get_renderer('mod_htmlcert', 'email', 'htmlemail');
$textrenderer = $PAGE->get_renderer('mod_htmlcert', 'email', 'textemail');
// Get the context.
$context = \context::instance_by_id($htmlcert->contextid);
foreach ($customcerts as $customcert) {
// Set the $PAGE context - this ensure settings, such as language, are kept and don't default to the site settings.
$PAGE->set_context($context);
// Get the context.
$context = \context::instance_by_id($htmlcert->contextid);
// Get the person we are going to send this email on behalf of.
$userfrom = \core_user::get_noreply_user();
// Set the $PAGE context - this ensure settings, such as language, are kept and don't default to the site settings.
$PAGE->set_context($context);
// Store teachers for later.
$teachers = get_enrolled_users($context, 'moodle/course:update');
// Get the person we are going to send this email on behalf of.
$userfrom = \core_user::get_noreply_user();
$courseshortname = format_string($htmlcert->courseshortname, true, array('context' => $context));
$coursefullname = format_string($htmlcert->coursefullname, true, array('context' => $context));
$certificatename = format_string($htmlcert->name, true, array('context' => $context));
// Store teachers for later.
$teachers = get_enrolled_users($context, 'moodle/course:update');
// Used to create the email subject.
$info = new \stdClass;
$info->coursename = $courseshortname; // Added for BC, so users who have edited the string don't lose this value.
$info->courseshortname = $courseshortname;
$info->coursefullname = $coursefullname;
$info->certificatename = $certificatename;
$courseshortname = format_string($htmlcert->courseshortname, true, array('context' => $context));
$coursefullname = format_string($htmlcert->coursefullname, true, array('context' => $context));
$certificatename = format_string($htmlcert->name, true, array('context' => $context));
// Get a list of all the issues.
$userfields = helper::get_all_user_name_fields('u');
$sql = "SELECT u.id, u.username, $userfields, u.email, ci.id as issueid, ci.emailed
FROM {htmlcert_issues} ci
JOIN {user} u
ON ci.userid = u.id
WHERE ci.htmlcertid = :htmlcertid";
$issuedusers = $DB->get_records_sql($sql, array('htmlcertid' => $htmlcert->id));
// Used to create the email subject.
$info = new \stdClass;
$info->coursename = $courseshortname; // Added for BC, so users who have edited the string don't lose this value.
$info->courseshortname = $courseshortname;
$info->coursefullname = $coursefullname;
$info->certificatename = $certificatename;
// Now, get a list of users who can access the certificate but have not yet.
$enrolledusers = get_enrolled_users(\context_course::instance($htmlcert->courseid), 'mod/htmlcert:view');
foreach ($enrolledusers as $enroluser) {
// Check if the user has already been issued.
if (in_array($enroluser->id, array_keys((array) $issuedusers))) {
continue;
}
// Get a list of all the issues.
$userfields = helper::get_all_user_name_fields('u');
$sql = "SELECT u.id, u.username, $userfields, u.email, ci.id as issueid, ci.emailed
FROM {htmlcert_issues} ci
JOIN {user} u
ON ci.userid = u.id
WHERE ci.htmlcertid = :htmlcertid";
$issuedusers = $DB->get_records_sql($sql, array('htmlcertid' => $htmlcert->id));
// Now check if the certificate is not visible to the current user.
$cm = get_fast_modinfo($htmlcert->courseid, $enroluser->id)->instances['htmlcert'][$htmlcert->id];
if (!$cm->uservisible) {
continue;
}
// Don't want to email those with the capability to manage the certificate.
if (has_capability('mod/htmlcert:manage', $context, $enroluser->id)) {
continue;
}
// Only email those with the capability to receive the certificate.
if (!has_capability('mod/htmlcert:receiveissue', $context, $enroluser->id)) {
continue;
}
// Check that they have passed the required time.
if (!empty($htmlcert->requiredtime)) {
if (\mod_htmlcert\certificate::get_course_time($htmlcert->courseid,
$enroluser->id) < ($htmlcert->requiredtime * 60)) {
// Now, get a list of users who can access the certificate but have not yet.
$enrolledusers = get_enrolled_users(\context_course::instance($htmlcert->courseid), 'mod/htmlcert:view');
foreach ($enrolledusers as $enroluser) {
// Check if the user has already been issued.
if (in_array($enroluser->id, array_keys((array) $issuedusers))) {
continue;
}
}
// Ensure the cert hasn't already been issued, e.g via the UI (view.php) - a race condition.
$issueid = $DB->get_field('htmlcert_issues', 'id',
array('userid' => $enroluser->id, 'htmlcertid' => $htmlcert->id), IGNORE_MULTIPLE);
if (empty($issueid)) {
// Ok, issue them the certificate.
$issueid = \mod_htmlcert\certificate::issue_certificate($htmlcert->id, $enroluser->id);
}
// Add them to the array so we email them.
$enroluser->issueid = $issueid;
$enroluser->emailed = 0;
$issuedusers[] = $enroluser;
}
// Remove all the users who have already been emailed.
foreach ($issuedusers as $key => $issueduser) {
if ($issueduser->emailed) {
unset($issuedusers[$key]);
}
}
// If there are no users to email we can return early.
if (!$issuedusers) {
return;
}
// Create a directory to store the PDF we will be sending.
$tempdir = make_temp_directory('certificate/attachment');
if (!$tempdir) {
return;
}
// Now, email the people we need to.
foreach ($issuedusers as $user) {
// Set up the user.
cron_setup_user($user);
$userfullname = fullname($user);
$info->userfullname = $userfullname;
// Now, get the PDF.
$template = new \stdClass();
$template->id = $htmlcert->templateid;
$template->name = $htmlcert->templatename;
$template->contextid = $htmlcert->contextid;
$template = new \mod_htmlcert\template($template);
$filecontents = $template->generate_pdf(false, $user->id, true);
// Set the name of the file we are going to send.
$filename = $courseshortname . '_' . $certificatename;
$filename = \core_text::entities_to_utf8($filename);
$filename = strip_tags($filename);
$filename = rtrim($filename, '.');
$filename = str_replace('&', '_', $filename) . '.pdf';
// Create the file we will be sending.
$tempfile = $tempdir . '/' . md5(microtime() . $user->id) . '.pdf';
file_put_contents($tempfile, $filecontents);
if ($htmlcert->emailstudents) {
$renderable = new \mod_htmlcert\output\email_certificate(true, $userfullname, $courseshortname,
$coursefullname, $certificatename, $htmlcert->contextid);
$subject = get_string('emailstudentsubject', 'htmlcert', $info);
$message = $textrenderer->render($renderable);
$messagehtml = $htmlrenderer->render($renderable);
email_to_user($user, fullname($userfrom), html_entity_decode($subject), $message, $messagehtml,
$tempfile, $filename);
}
if ($htmlcert->emailteachers) {
$renderable = new \mod_htmlcert\output\email_certificate(false, $userfullname, $courseshortname,
$coursefullname, $certificatename, $htmlcert->contextid);
$subject = get_string('emailnonstudentsubject', 'htmlcert', $info);
$message = $textrenderer->render($renderable);
$messagehtml = $htmlrenderer->render($renderable);
foreach ($teachers as $teacher) {
email_to_user($teacher, fullname($userfrom), html_entity_decode($subject), $message, $messagehtml,
$tempfile, $filename);
// Now check if the certificate is not visible to the current user.
$cm = get_fast_modinfo($htmlcert->courseid, $enroluser->id)->instances['htmlcert'][$htmlcert->id];
if (!$cm->uservisible) {
continue;
}
}
if (!empty($htmlcert->emailothers)) {
$others = explode(',', $htmlcert->emailothers);
foreach ($others as $email) {
$email = trim($email);
if (validate_email($email)) {
$renderable = new \mod_htmlcert\output\email_certificate(false, $userfullname,
$courseshortname, $coursefullname, $certificatename, $htmlcert->contextid);
// Don't want to email those with the capability to manage the certificate.
if (has_capability('mod/htmlcert:manage', $context, $enroluser->id)) {
continue;
}
$subject = get_string('emailnonstudentsubject', 'htmlcert', $info);
$message = $textrenderer->render($renderable);
$messagehtml = $htmlrenderer->render($renderable);
// Only email those with the capability to receive the certificate.
if (!has_capability('mod/htmlcert:receiveissue', $context, $enroluser->id)) {
continue;
}
$emailuser = new \stdClass();
$emailuser->id = -1;
$emailuser->email = $email;
email_to_user($emailuser, fullname($userfrom), html_entity_decode($subject), $message,
$messagehtml, $tempfile, $filename);
// Check that they have passed the required time.
if (!empty($htmlcert->requiredtime)) {
if (\mod_htmlcert\certificate::get_course_time($htmlcert->courseid,
$enroluser->id) < ($htmlcert->requiredtime * 60)) {
continue;
}
}
// Ensure the cert hasn't already been issued, e.g via the UI (view.php) - a race condition.
$issueid = $DB->get_field('htmlcert_issues', 'id',
array('userid' => $enroluser->id, 'htmlcertid' => $htmlcert->id), IGNORE_MULTIPLE);
if (empty($issueid)) {
// Ok, issue them the certificate.
$issueid = \mod_htmlcert\certificate::issue_certificate($htmlcert->id, $enroluser->id);
}
// Add them to the array so we email them.
$enroluser->issueid = $issueid;
$enroluser->emailed = 0;
$issuedusers[] = $enroluser;
}
// Set the field so that it is emailed.
$DB->set_field('htmlcert_issues', 'emailed', 1, array('id' => $user->issueid));
// Remove all the users who have already been emailed.
foreach ($issuedusers as $key => $issueduser) {
if ($issueduser->emailed) {
unset($issuedusers[$key]);
}
}
// If there are no users to email we can return early.
if (!$issuedusers) {
continue;
}
// Create a directory to store the PDF we will be sending.
$tempdir = make_temp_directory('certificate/attachment');
if (!$tempdir) {
return;
}
// Now, email the people we need to.
foreach ($issuedusers as $user) {
// Set up the user.
cron_setup_user($user);
$userfullname = fullname($user);
$info->userfullname = $userfullname;
// Now, get the PDF.
$template = new \stdClass();
$template->id = $htmlcert->templateid;
$template->name = $htmlcert->templatename;
$template->contextid = $htmlcert->contextid;
$template = new \mod_htmlcert\template($template);
$filecontents = $template->generate_pdf(false, $user->id, true);
// Set the name of the file we are going to send.
$filename = $courseshortname . '_' . $certificatename;
$filename = \core_text::entities_to_utf8($filename);
$filename = strip_tags($filename);
$filename = rtrim($filename, '.');
$filename = str_replace('&', '_', $filename) . '.pdf';
// Create the file we will be sending.
$tempfile = $tempdir . '/' . md5(microtime() . $user->id) . '.pdf';
file_put_contents($tempfile, $filecontents);
if ($htmlcert->emailstudents) {
$renderable = new \mod_htmlcert\output\email_certificate(true, $userfullname, $courseshortname,
$coursefullname, $certificatename, $htmlcert->contextid);
$subject = get_string('emailstudentsubject', 'htmlcert', $info);
$message = $textrenderer->render($renderable);
$messagehtml = $htmlrenderer->render($renderable);
email_to_user($user, fullname($userfrom), html_entity_decode($subject), $message, $messagehtml,
$tempfile, $filename);
}
if ($htmlcert->emailteachers) {
$renderable = new \mod_htmlcert\output\email_certificate(false, $userfullname, $courseshortname,
$coursefullname, $certificatename, $htmlcert->contextid);
$subject = get_string('emailnonstudentsubject', 'htmlcert', $info);
$message = $textrenderer->render($renderable);
$messagehtml = $htmlrenderer->render($renderable);
foreach ($teachers as $teacher) {
email_to_user($teacher, fullname($userfrom), html_entity_decode($subject), $message, $messagehtml,
$tempfile, $filename);
}
}
if (!empty($htmlcert->emailothers)) {
$others = explode(',', $htmlcert->emailothers);
foreach ($others as $email) {
$email = trim($email);
if (validate_email($email)) {
$renderable = new \mod_htmlcert\output\email_certificate(false, $userfullname,
$courseshortname, $coursefullname, $certificatename, $htmlcert->contextid);
$subject = get_string('emailnonstudentsubject', 'htmlcert', $info);
$message = $textrenderer->render($renderable);
$messagehtml = $htmlrenderer->render($renderable);
$emailuser = new \stdClass();
$emailuser->id = -1;
$emailuser->email = $email;
email_to_user($emailuser, fullname($userfrom), html_entity_decode($subject), $message,
$messagehtml, $tempfile, $filename);
}
}
}
// Set the field so that it is emailed.
$DB->set_field('htmlcert_issues', 'emailed', 1, array('id' => $user->issueid));
}
}
}
}

View file

@ -18,7 +18,7 @@
* Class represents a htmlcert template.
*
* @package mod_htmlcert
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021-2022 Kumi Systems e.U. <office@kumi.systems>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -35,7 +35,7 @@ defined('MOODLE_INTERNAL') || die();
* Class represents a htmlcert template.
*
* @package mod_htmlcert
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021-2022 Kumi Systems e.U. <office@kumi.systems>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class template {
@ -118,7 +118,7 @@ class template {
$user = \core_user::get_user($userid);
}
profile_load_data($user);
profile_load_data($user);
$htmlcert = $DB->get_record('htmlcert', ['templateid' => $this->id]);
@ -165,7 +165,7 @@ class template {
$html = str_replace("__CERTNUM__", $code, $html);
$courseid = $htmlcert->course || $SITE->id;
$courseid = $htmlcert->course ?: $SITE->id;
$course = get_course($courseid);
$coursename = $course->fullname;
@ -176,7 +176,7 @@ class template {
$html = str_replace("__DATE__", userdate($date, '%B %d, %Y'), $html);
$html = str_replace("__PIN__", $user->profile_field_pin, $html);
$html = str_replace("__PIN__", $user->username, $html);
$pdf->addPage($html);
$pdf->send();

View file

@ -18,7 +18,7 @@
* Handles viewing a htmlcert.
*
* @package mod_htmlcert
* @copyright 2013 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
* @copyright 2013 Mark Nelson <markn@moodle.com>, 2021-2022 Kumi Systems e.U. <office@kumi.systems>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -142,6 +142,7 @@ if (!$downloadown && !$downloadissue) {
$link = new moodle_url('/mod/htmlcert/view.php', array('id' => $cm->id, 'downloadown' => true));
$downloadbutton = new single_button($link, $linkname, 'get', true);
$downloadbutton->class .= ' m-b-1'; // Seems a bit hackish, ahem.
$downloadbutton->add_action(new \popup_action('click', $link));
$downloadbutton = $OUTPUT->render($downloadbutton);
}