Reduce code nesting level in task (#237)

Make the code easier to read and understand
This commit is contained in:
mwithheld 2018-10-05 14:31:37 -07:00 committed by Mark Nelson
parent 810bc2d3fd
commit 3e2e9c9c8b

View file

@ -61,169 +61,173 @@ class email_certificate_task extends \core\task\scheduled_task {
WHERE (c.emailstudents = :emailstudents WHERE (c.emailstudents = :emailstudents
OR c.emailteachers = :emailteachers OR c.emailteachers = :emailteachers
OR $emailotherslengthsql >= 3)"; OR $emailotherslengthsql >= 3)";
if ($customcerts = $DB->get_records_sql($sql, array('emailstudents' => 1, 'emailteachers' => 1))) { if (!$customcerts = $DB->get_records_sql($sql, array('emailstudents' => 1, 'emailteachers' => 1))) {
// The renderers used for sending emails. return false;
$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. // The renderers used for sending emails.
// Look through the teachers. $htmlrenderer = $PAGE->get_renderer('mod_customcert', 'email', 'htmlemail');
if ($teachers = get_enrolled_users($context, 'moodle/course:update')) { $textrenderer = $PAGE->get_renderer('mod_customcert', 'email', 'textemail');
$teachers = sort_by_roleassignment_authority($teachers, $context); foreach ($customcerts as $customcert) {
$userfrom = reset($teachers); // Get the context.
} else { // Ok, no teachers, use administrator name. $context = \context::instance_by_id($customcert->contextid);
$userfrom = get_admin();
// Get the person we are going to send this email on behalf of.
// Look through the teachers.
if ($teachers = get_enrolled_users($context, 'moodle/course:update')) {
$teachers = sort_by_roleassignment_authority($teachers, $context);
$userfrom = reset($teachers);
} else { // Ok, no teachers, use administrator name.
$userfrom = get_admin();
}
$courseshortname = format_string($customcert->courseshortname, true, array('context' => $context));
$coursefullname = format_string($customcert->coursefullname, true, array('context' => $context));
$certificatename = format_string($customcert->name, true, array('context' => $context));
// 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;
// Get a list of all the issues.
$userfields = get_all_user_name_fields(true, 'u');
$sql = "SELECT u.id, u.username, $userfields, u.email, ci.id as issueid, ci.emailed
FROM {customcert_issues} ci
JOIN {user} u
ON ci.userid = u.id
WHERE ci.customcertid = :customcertid";
$issuedusers = $DB->get_records_sql($sql, array('customcertid' => $customcert->id));
// 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;
} }
$courseshortname = format_string($customcert->courseshortname, true, array('context' => $context)); // Now check if the certificate is not visible to the current user.
$coursefullname = format_string($customcert->coursefullname, true, array('context' => $context)); $cm = get_fast_modinfo($customcert->courseid, $enroluser->id)->instances['customcert'][$customcert->id];
$certificatename = format_string($customcert->name, true, array('context' => $context)); if (!$cm->uservisible) {
continue;
// 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;
// Get a list of all the issues.
$userfields = get_all_user_name_fields(true, 'u');
$sql = "SELECT u.id, u.username, $userfields, u.email, ci.id as issueid, ci.emailed
FROM {customcert_issues} ci
JOIN {user} u
ON ci.userid = u.id
WHERE ci.customcertid = :customcertid";
$issuedusers = $DB->get_records_sql($sql, array('customcertid' => $customcert->id));
// 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.
if (!empty($customcert->requiredtime)) {
if (\mod_customcert\certificate::get_course_time($customcert->courseid,
$enroluser->id) < ($customcert->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('customcert_issues', 'id',
array('userid' => $enroluser->id, 'customcertid' => $customcert->id));
if (empty($issueid)) {
// Ok, issue them the certificate.
$issueid = \mod_customcert\certificate::issue_certificate($customcert->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. // Don't want to email those with the capability to manage the certificate.
foreach ($issuedusers as $key => $issueduser) { if (has_capability('mod/customcert:manage', $context, $enroluser->id)) {
if ($issueduser->emailed) { continue;
unset($issuedusers[$key]); }
// Check that they have passed the required time.
if (!empty($customcert->requiredtime)) {
if (\mod_customcert\certificate::get_course_time($customcert->courseid,
$enroluser->id) < ($customcert->requiredtime * 60)) {
continue;
} }
} }
// Now, email the people we need to. // Ensure the cert hasn't already been issued, e.g via the UI (view.php) - a race condition.
if ($issuedusers) { $issueid = $DB->get_field('customcert_issues', 'id',
foreach ($issuedusers as $user) { array('userid' => $enroluser->id, 'customcertid' => $customcert->id));
$userfullname = fullname($user); if (empty($issueid)) {
// Ok, issue them the certificate.
$issueid = \mod_customcert\certificate::issue_certificate($customcert->id, $enroluser->id);
}
// Create a directory to store the PDF we will be sending. // Add them to the array so we email them.
$tempdir = make_temp_directory('certificate/attachment'); $enroluser->issueid = $issueid;
if (!$tempdir) { $enroluser->emailed = 0;
return false; $issuedusers[] = $enroluser;
} }
// Now, get the PDF. // Remove all the users who have already been emailed.
$template = new \stdClass(); foreach ($issuedusers as $key => $issueduser) {
$template->id = $customcert->templateid; if ($issueduser->emailed) {
$template->name = $customcert->templatename; unset($issuedusers[$key]);
$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. // Now, email the people we need to.
$filename = $courseshortname . '_' . $certificatename; if (!$issuedusers) {
$filename = \core_text::entities_to_utf8($filename); continue;
$filename = strip_tags($filename); }
$filename = rtrim($filename, '.');
$filename = str_replace('&', '_', $filename) . '.pdf';
// Create the file we will be sending. foreach ($issuedusers as $user) {
$tempfile = $tempdir . '/' . md5(microtime() . $user->id) . '.pdf'; $userfullname = fullname($user);
file_put_contents($tempfile, $filecontents);
if ($customcert->emailstudents) { // Create a directory to store the PDF we will be sending.
$renderable = new \mod_customcert\output\email_certificate(true, $userfullname, $courseshortname, $tempdir = make_temp_directory('certificate/attachment');
$coursefullname, $certificatename, $customcert->contextid); if (!$tempdir) {
return false;
}
$subject = get_string('emailstudentsubject', 'customcert', $info); // Now, get the PDF.
$message = $textrenderer->render($renderable); $template = new \stdClass();
$messagehtml = $htmlrenderer->render($renderable); $template->id = $customcert->templateid;
email_to_user($user, fullname($userfrom), $subject, $message, $messagehtml, $tempfile, $filename); $template->name = $customcert->templatename;
} $template->contextid = $customcert->contextid;
$template = new \mod_customcert\template($template);
$filecontents = $template->generate_pdf(false, $user->id, true);
if ($customcert->emailteachers) { // Set the name of the file we are going to send.
$renderable = new \mod_customcert\output\email_certificate(false, $userfullname, $courseshortname, $filename = $courseshortname . '_' . $certificatename;
$coursefullname, $certificatename, $customcert->contextid); $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) {
$renderable = new \mod_customcert\output\email_certificate(true, $userfullname, $courseshortname,
$coursefullname, $certificatename, $customcert->contextid);
$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) {
$renderable = new \mod_customcert\output\email_certificate(false, $userfullname, $courseshortname,
$coursefullname, $certificatename, $customcert->contextid);
$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)) {
$renderable = new \mod_customcert\output\email_certificate(false, $userfullname,
$courseshortname, $coursefullname, $certificatename, $customcert->contextid);
$subject = get_string('emailnonstudentsubject', 'customcert', $info); $subject = get_string('emailnonstudentsubject', 'customcert', $info);
$message = $textrenderer->render($renderable); $message = $textrenderer->render($renderable);
$messagehtml = $htmlrenderer->render($renderable); $messagehtml = $htmlrenderer->render($renderable);
foreach ($teachers as $teacher) {
email_to_user($teacher, fullname($userfrom), $subject, $message, $messagehtml, $tempfile, $emailuser = new \stdClass();
$filename); $emailuser->id = -1;
} $emailuser->email = $email;
email_to_user($emailuser, 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)) {
$renderable = new \mod_customcert\output\email_certificate(false, $userfullname,
$courseshortname, $coursefullname, $certificatename, $customcert->contextid);
$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));
} }
} }
// Set the field so that it is emailed.
$DB->set_field('customcert_issues', 'emailed', 1, array('id' => $user->issueid));
} }
} }
} }