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. * The report that displays the certificates the user has throughout the site.
* *
* @package mod_htmlcert * @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 * @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. * Class for the report that displays the certificates the user has throughout the site.
* *
* @package mod_htmlcert * @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 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class my_certificates_table extends \table_sql { class my_certificates_table extends \table_sql {
@ -148,7 +148,7 @@ class my_certificates_table extends \table_sql {
'certificateid' => $certificate->id, 'certificateid' => $certificate->id,
'downloadcert' => '1')); '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. * A scheduled task for emailing certificates.
* *
* @package mod_htmlcert * @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 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
namespace mod_htmlcert\task; 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'); $htmlrenderer = $PAGE->get_renderer('mod_htmlcert', 'email', 'htmlemail');
$textrenderer = $PAGE->get_renderer('mod_htmlcert', 'email', 'textemail'); $textrenderer = $PAGE->get_renderer('mod_htmlcert', 'email', 'textemail');
// Get the context. foreach ($customcerts as $customcert) {
$context = \context::instance_by_id($htmlcert->contextid);
// Set the $PAGE context - this ensure settings, such as language, are kept and don't default to the site settings. // Get the context.
$PAGE->set_context($context); $context = \context::instance_by_id($htmlcert->contextid);
// Get the person we are going to send this email on behalf of. // Set the $PAGE context - this ensure settings, such as language, are kept and don't default to the site settings.
$userfrom = \core_user::get_noreply_user(); $PAGE->set_context($context);
// Store teachers for later. // Get the person we are going to send this email on behalf of.
$teachers = get_enrolled_users($context, 'moodle/course:update'); $userfrom = \core_user::get_noreply_user();
$courseshortname = format_string($htmlcert->courseshortname, true, array('context' => $context)); // Store teachers for later.
$coursefullname = format_string($htmlcert->coursefullname, true, array('context' => $context)); $teachers = get_enrolled_users($context, 'moodle/course:update');
$certificatename = format_string($htmlcert->name, true, array('context' => $context));
// Used to create the email subject. $courseshortname = format_string($htmlcert->courseshortname, true, array('context' => $context));
$info = new \stdClass; $coursefullname = format_string($htmlcert->coursefullname, true, array('context' => $context));
$info->coursename = $courseshortname; // Added for BC, so users who have edited the string don't lose this value. $certificatename = format_string($htmlcert->name, true, array('context' => $context));
$info->courseshortname = $courseshortname;
$info->coursefullname = $coursefullname;
$info->certificatename = $certificatename;
// Get a list of all the issues. // Used to create the email subject.
$userfields = helper::get_all_user_name_fields('u'); $info = new \stdClass;
$sql = "SELECT u.id, u.username, $userfields, u.email, ci.id as issueid, ci.emailed $info->coursename = $courseshortname; // Added for BC, so users who have edited the string don't lose this value.
FROM {htmlcert_issues} ci $info->courseshortname = $courseshortname;
JOIN {user} u $info->coursefullname = $coursefullname;
ON ci.userid = u.id $info->certificatename = $certificatename;
WHERE ci.htmlcertid = :htmlcertid";
$issuedusers = $DB->get_records_sql($sql, array('htmlcertid' => $htmlcert->id));
// Now, get a list of users who can access the certificate but have not yet. // Get a list of all the issues.
$enrolledusers = get_enrolled_users(\context_course::instance($htmlcert->courseid), 'mod/htmlcert:view'); $userfields = helper::get_all_user_name_fields('u');
foreach ($enrolledusers as $enroluser) { $sql = "SELECT u.id, u.username, $userfields, u.email, ci.id as issueid, ci.emailed
// Check if the user has already been issued. FROM {htmlcert_issues} ci
if (in_array($enroluser->id, array_keys((array) $issuedusers))) { JOIN {user} u
continue; 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. // Now, get a list of users who can access the certificate but have not yet.
$cm = get_fast_modinfo($htmlcert->courseid, $enroluser->id)->instances['htmlcert'][$htmlcert->id]; $enrolledusers = get_enrolled_users(\context_course::instance($htmlcert->courseid), 'mod/htmlcert:view');
if (!$cm->uservisible) { foreach ($enrolledusers as $enroluser) {
continue; // Check if the user has already been issued.
} if (in_array($enroluser->id, array_keys((array) $issuedusers))) {
// 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)) {
continue; continue;
} }
}
// Ensure the cert hasn't already been issued, e.g via the UI (view.php) - a race condition. // Now check if the certificate is not visible to the current user.
$issueid = $DB->get_field('htmlcert_issues', 'id', $cm = get_fast_modinfo($htmlcert->courseid, $enroluser->id)->instances['htmlcert'][$htmlcert->id];
array('userid' => $enroluser->id, 'htmlcertid' => $htmlcert->id), IGNORE_MULTIPLE); if (!$cm->uservisible) {
if (empty($issueid)) { continue;
// 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);
} }
}
if (!empty($htmlcert->emailothers)) { // Don't want to email those with the capability to manage the certificate.
$others = explode(',', $htmlcert->emailothers); if (has_capability('mod/htmlcert:manage', $context, $enroluser->id)) {
foreach ($others as $email) { continue;
$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); // Only email those with the capability to receive the certificate.
$message = $textrenderer->render($renderable); if (!has_capability('mod/htmlcert:receiveissue', $context, $enroluser->id)) {
$messagehtml = $htmlrenderer->render($renderable); continue;
}
$emailuser = new \stdClass(); // Check that they have passed the required time.
$emailuser->id = -1; if (!empty($htmlcert->requiredtime)) {
$emailuser->email = $email; if (\mod_htmlcert\certificate::get_course_time($htmlcert->courseid,
email_to_user($emailuser, fullname($userfrom), html_entity_decode($subject), $message, $enroluser->id) < ($htmlcert->requiredtime * 60)) {
$messagehtml, $tempfile, $filename); 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. // Remove all the users who have already been emailed.
$DB->set_field('htmlcert_issues', 'emailed', 1, array('id' => $user->issueid)); 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. * Class represents a htmlcert template.
* *
* @package mod_htmlcert * @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 * @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. * Class represents a htmlcert template.
* *
* @package mod_htmlcert * @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 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class template { class template {
@ -118,7 +118,7 @@ class template {
$user = \core_user::get_user($userid); $user = \core_user::get_user($userid);
} }
profile_load_data($user); profile_load_data($user);
$htmlcert = $DB->get_record('htmlcert', ['templateid' => $this->id]); $htmlcert = $DB->get_record('htmlcert', ['templateid' => $this->id]);
@ -165,7 +165,7 @@ class template {
$html = str_replace("__CERTNUM__", $code, $html); $html = str_replace("__CERTNUM__", $code, $html);
$courseid = $htmlcert->course || $SITE->id; $courseid = $htmlcert->course ?: $SITE->id;
$course = get_course($courseid); $course = get_course($courseid);
$coursename = $course->fullname; $coursename = $course->fullname;
@ -176,7 +176,7 @@ class template {
$html = str_replace("__DATE__", userdate($date, '%B %d, %Y'), $html); $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->addPage($html);
$pdf->send(); $pdf->send();

View file

@ -18,7 +18,7 @@
* Handles viewing a htmlcert. * Handles viewing a htmlcert.
* *
* @package mod_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 * @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)); $link = new moodle_url('/mod/htmlcert/view.php', array('id' => $cm->id, 'downloadown' => true));
$downloadbutton = new single_button($link, $linkname, 'get', true); $downloadbutton = new single_button($link, $linkname, 'get', true);
$downloadbutton->class .= ' m-b-1'; // Seems a bit hackish, ahem. $downloadbutton->class .= ' m-b-1'; // Seems a bit hackish, ahem.
$downloadbutton->add_action(new \popup_action('click', $link));
$downloadbutton = $OUTPUT->render($downloadbutton); $downloadbutton = $OUTPUT->render($downloadbutton);
} }