2017-02-02 09:17:41 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A scheduled task for emailing certificates.
|
|
|
|
*
|
|
|
|
* @package mod_customcert
|
|
|
|
* @copyright 2017 Mark Nelson <markn@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
namespace mod_customcert\task;
|
|
|
|
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A scheduled task for emailing certificates.
|
|
|
|
*
|
|
|
|
* @package mod_customcert
|
|
|
|
* @copyright 2017 Mark Nelson <markn@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
class email_certificate_task extends \core\task\scheduled_task {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a descriptive name for this task (shown to admins).
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_name() {
|
|
|
|
return get_string('taskemailcertificate', 'customcert');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute.
|
|
|
|
*/
|
|
|
|
public function execute() {
|
|
|
|
global $DB, $PAGE;
|
|
|
|
|
|
|
|
// Get all the certificates that have requested someone get emailed.
|
|
|
|
$sql = "SELECT c.*, ct.id as templateid, ct.name as templatename, ct.contextid, co.id as courseid,
|
2017-11-11 07:11:55 +00:00
|
|
|
co.fullname as coursefullname, co.shortname as courseshortname
|
2017-02-02 09:17:41 +00:00
|
|
|
FROM {customcert} c
|
|
|
|
JOIN {customcert_templates} ct
|
|
|
|
ON c.templateid = ct.id
|
|
|
|
JOIN {course} co
|
|
|
|
ON c.course = co.id
|
|
|
|
WHERE (c.emailstudents = :emailstudents
|
|
|
|
OR c.emailteachers = :emailteachers
|
|
|
|
OR c.emailothers != '')";
|
|
|
|
if ($customcerts = $DB->get_records_sql($sql, array('emailstudents' => 1, 'emailteachers' => 1))) {
|
|
|
|
// The renderers used for sending emails.
|
|
|
|
$htmlrenderer = $PAGE->get_renderer('mod_customcert', 'email', 'htmlemail');
|
|
|
|
$textrenderer = $PAGE->get_renderer('mod_customcert', 'email', 'textemail');
|
|
|
|
foreach ($customcerts as $customcert) {
|
|
|
|
// Get the context.
|
|
|
|
$context = \context::instance_by_id($customcert->contextid);
|
|
|
|
|
|
|
|
// Get the person we are going to send this email on behalf of.
|
|
|
|
// Look through the teachers.
|
|
|
|
if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
|
|
|
|
'', '', '', '', false, true)) {
|
|
|
|
$teachers = sort_by_roleassignment_authority($teachers, $context);
|
|
|
|
$userfrom = reset($teachers);
|
|
|
|
} else { // Ok, no teachers, use administrator name.
|
|
|
|
$userfrom = get_admin();
|
|
|
|
}
|
|
|
|
|
2017-11-11 07:11:55 +00:00
|
|
|
$courseshortname = format_string($customcert->courseshortname, true, array('context' => $context));
|
|
|
|
$coursefullname = format_string($customcert->coursefullname, true, array('context' => $context));
|
2017-02-02 09:17:41 +00:00
|
|
|
$certificatename = format_string($customcert->name, true, array('context' => $context));
|
|
|
|
|
|
|
|
// Used to create the email subject.
|
|
|
|
$info = new \stdClass;
|
2017-11-11 07:11:55 +00:00
|
|
|
$info->coursename = $courseshortname; // Added for BC, so users who have edited the string don't lose this value.
|
|
|
|
$info->courseshortname = $courseshortname;
|
|
|
|
$info->coursefullname = $coursefullname;
|
2017-02-02 09:17:41 +00:00
|
|
|
$info->certificatename = $certificatename;
|
|
|
|
|
2017-05-21 02:16:45 +00:00
|
|
|
// Get a list of all the issues.
|
2017-02-02 09:17:41 +00:00
|
|
|
$userfields = get_all_user_name_fields(true, 'u');
|
2017-05-21 02:16:45 +00:00
|
|
|
$sql = "SELECT u.id, u.username, $userfields, u.email, ci.id as issueid, ci.emailed
|
2017-02-02 09:17:41 +00:00
|
|
|
FROM {customcert_issues} ci
|
|
|
|
JOIN {user} u
|
|
|
|
ON ci.userid = u.id
|
2017-05-21 02:16:45 +00:00
|
|
|
WHERE ci.customcertid = :customcertid";
|
|
|
|
$issuedusers = $DB->get_records_sql($sql, array('customcertid' => $customcert->id));
|
2017-02-02 09:17:41 +00:00
|
|
|
|
|
|
|
// Now, get a list of users who can access the certificate but have not yet.
|
|
|
|
$enrolledusers = get_enrolled_users(\context_course::instance($customcert->courseid), 'mod/customcert:view');
|
|
|
|
foreach ($enrolledusers as $enroluser) {
|
|
|
|
// Check if the user has already been issued.
|
|
|
|
if (in_array($enroluser->id, array_keys((array) $issuedusers))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check if the certificate is not visible to the current user.
|
|
|
|
$cm = get_fast_modinfo($customcert->courseid, $enroluser->id)->instances['customcert'][$customcert->id];
|
|
|
|
if (!$cm->uservisible) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't want to email those with the capability to manage the certificate.
|
|
|
|
if (has_capability('mod/customcert:manage', $context, $enroluser->id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that they have passed the required time.
|
2017-04-27 07:18:11 +00:00
|
|
|
if (!empty($customcert->requiredtime)) {
|
|
|
|
if (\mod_customcert\certificate::get_course_time($customcert->courseid,
|
|
|
|
$enroluser->id) < ($customcert->requiredtime * 60)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-02 09:17:41 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 02:30:50 +00:00
|
|
|
// Ensure the cert hasn't already been issued, e.g via the UI (view.php) - a race condition.
|
|
|
|
$issueid = $DB->get_field('customcert_issues', 'id',
|
|
|
|
array('userid' => $enroluser->id, 'customcertid' => $customcert->id));
|
|
|
|
if (empty($issueid)) {
|
|
|
|
// Ok, issue them the certificate.
|
2018-05-23 06:01:55 +00:00
|
|
|
$issueid = \mod_customcert\certificate::issue_certificate($customcert->id, $enroluser->id);
|
2018-03-27 02:30:50 +00:00
|
|
|
}
|
2017-02-02 09:17:41 +00:00
|
|
|
|
|
|
|
// Add them to the array so we email them.
|
|
|
|
$enroluser->issueid = $issueid;
|
2017-05-21 02:16:45 +00:00
|
|
|
$enroluser->emailed = 0;
|
2017-02-02 09:17:41 +00:00
|
|
|
$issuedusers[] = $enroluser;
|
|
|
|
}
|
|
|
|
|
2017-05-21 02:16:45 +00:00
|
|
|
// Remove all the users who have already been emailed.
|
|
|
|
foreach ($issuedusers as $key => $issueduser) {
|
|
|
|
if ($issueduser->emailed) {
|
|
|
|
unset($issuedusers[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 09:17:41 +00:00
|
|
|
// Now, email the people we need to.
|
|
|
|
if ($issuedusers) {
|
|
|
|
foreach ($issuedusers as $user) {
|
|
|
|
$userfullname = fullname($user);
|
|
|
|
|
|
|
|
// Create a directory to store the PDF we will be sending.
|
|
|
|
$tempdir = make_temp_directory('certificate/attachment');
|
|
|
|
if (!$tempdir) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, get the PDF.
|
|
|
|
$template = new \stdClass();
|
|
|
|
$template->id = $customcert->templateid;
|
|
|
|
$template->name = $customcert->templatename;
|
|
|
|
$template->contextid = $customcert->contextid;
|
|
|
|
$template = new \mod_customcert\template($template);
|
|
|
|
$filecontents = $template->generate_pdf(false, $user->id, true);
|
|
|
|
|
|
|
|
// Set the name of the file we are going to send.
|
2017-11-11 07:11:55 +00:00
|
|
|
$filename = $courseshortname . '_' . $certificatename;
|
2017-02-02 09:17:41 +00:00
|
|
|
$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 ($customcert->emailstudents) {
|
2017-11-11 07:11:55 +00:00
|
|
|
$renderable = new \mod_customcert\output\email_certificate(true, $userfullname, $courseshortname,
|
|
|
|
$coursefullname, $certificatename, $customcert->contextid);
|
2017-02-02 09:17:41 +00:00
|
|
|
|
|
|
|
$subject = get_string('emailstudentsubject', 'customcert', $info);
|
|
|
|
$message = $textrenderer->render($renderable);
|
|
|
|
$messagehtml = $htmlrenderer->render($renderable);
|
|
|
|
email_to_user($user, fullname($userfrom), $subject, $message, $messagehtml, $tempfile, $filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($customcert->emailteachers) {
|
2017-11-11 07:11:55 +00:00
|
|
|
$renderable = new \mod_customcert\output\email_certificate(false, $userfullname, $courseshortname,
|
|
|
|
$coursefullname, $certificatename, $customcert->contextid);
|
2017-02-02 09:17:41 +00:00
|
|
|
|
|
|
|
$subject = get_string('emailnonstudentsubject', 'customcert', $info);
|
|
|
|
$message = $textrenderer->render($renderable);
|
|
|
|
$messagehtml = $htmlrenderer->render($renderable);
|
|
|
|
foreach ($teachers as $teacher) {
|
|
|
|
email_to_user($teacher, fullname($userfrom), $subject, $message, $messagehtml, $tempfile,
|
|
|
|
$filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($customcert->emailothers)) {
|
|
|
|
$others = explode(',', $customcert->emailothers);
|
|
|
|
foreach ($others as $email) {
|
|
|
|
$email = trim($email);
|
|
|
|
if (validate_email($email)) {
|
2017-11-11 07:11:55 +00:00
|
|
|
$renderable = new \mod_customcert\output\email_certificate(false, $userfullname,
|
|
|
|
$courseshortname, $coursefullname, $certificatename, $customcert->contextid);
|
2017-02-02 09:17:41 +00:00
|
|
|
|
|
|
|
$subject = get_string('emailnonstudentsubject', 'customcert', $info);
|
|
|
|
$message = $textrenderer->render($renderable);
|
|
|
|
$messagehtml = $htmlrenderer->render($renderable);
|
|
|
|
|
|
|
|
$emailuser = new \stdClass();
|
|
|
|
$emailuser->id = -1;
|
|
|
|
$emailuser->email = $email;
|
|
|
|
email_to_user($emailuser, fullname($userfrom), $subject, $message, $messagehtml, $tempfile,
|
|
|
|
$filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the field so that it is emailed.
|
|
|
|
$DB->set_field('customcert_issues', 'emailed', 1, array('id' => $user->issueid));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|