/** * Sends the student their issued certificate from moddata as an email * attachment. * * @param stdClass $user * @param stdClass $course * @param stdClass $certificate * @param stdClass $certrecord * @param stdClass $context */ function certificate_email_students($user, $course, $certificate, $certrecord, $context) { global $DB, $USER; if ($certrecord->mailed > 0) { return; } // Get teachers if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC', '', '', '', '', false, true)) { $users = sort_by_roleassignment_authority($users, $context); $teacher = array_shift($users); } // If we haven't found a teacher yet, look for a non-editing teacher in this course. if (empty($teacher) && ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC', '', '', '', '', false, true))) { $users = sort_by_roleassignment_authority($users, $context); $teacher = array_shift($users); } $info = new stdClass(); $info->username = fullname($user); $info->certificate = format_string($certificate->name, true); $info->course = format_string($course->fullname, true); $from = fullname($teacher); $subject = $info->course . ': ' . $info->certificate; $message = get_string('emailstudenttext', 'certificate', $info) . "\n"; // Make the HTML version more XHTML happy (&) $messagehtml = text_to_html(get_string('emailstudenttext', 'certificate', $info)); $user->mailformat = 0; // Always send HTML version as well $filename = clean_filename($certificate->name . '.pdf'); // Get hashed pathname $fs = get_file_storage(); $component = 'mod_certificate'; $filearea = 'issue'; $filepath = '/'; $files = $fs->get_area_files($context->id, $component, $filearea, $certrecord->id); foreach ($files as $f) { $filepathname = $f->get_contenthash(); } $attachment = 'filedir/' . certificate_path_from_hash($filepathname) . '/' . $filepathname; $attachname = $filename; $DB->set_field('certificate_issues', 'mailed', '1', array('certificateid' => $certificate->id, 'userid' => $user->id)); return email_to_user($user, $from, $subject, $message, $messagehtml, $attachment, $attachname); }
/** * Sends the student their issued certificate from moddata as an email * attachment. * * @param stdClass $course * @param stdClass $certificate * @param stdClass $certrecord * @param stdClass $context */ function certificate_email_student($course, $certificate, $certrecord, $context) { global $DB, $USER; // Get teachers if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC', '', '', '', '', false, true)) { $users = sort_by_roleassignment_authority($users, $context); $teacher = array_shift($users); } // If we haven't found a teacher yet, look for a non-editing teacher in this course. if (empty($teacher) && ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC', '', '', '', '', false, true))) { $users = sort_by_roleassignment_authority($users, $context); $teacher = array_shift($users); } // Ok, no teachers, use administrator name if (empty($teacher)) { $teacher = fullname(get_admin()); } $info = new stdClass(); $info->username = fullname($USER); $info->certificate = format_string($certificate->name, true); $info->course = format_string($course->fullname, true); $from = fullname($teacher); $subject = $info->course . ': ' . $info->certificate; $message = get_string('emailstudenttext', 'certificate', $info) . "\n"; // Make the HTML version more XHTML happy (&) $messagehtml = text_to_html(get_string('emailstudenttext', 'certificate', $info)); // Remove full-stop at the end if it exists, to avoid "..pdf" being created and being filtered by clean_filename $certname = rtrim($certificate->name, '.'); $filename = clean_filename("{$certname}.pdf"); // Get hashed pathname $fs = get_file_storage(); $component = 'mod_certificate'; $filearea = 'issue'; $filepath = '/'; $files = $fs->get_area_files($context->id, $component, $filearea, $certrecord->id); foreach ($files as $f) { $filepathname = $f->get_contenthash(); } $attachment = 'filedir/' . certificate_path_from_hash($filepathname) . '/' . $filepathname; $attachname = $filename; return email_to_user($USER, $from, $subject, $message, $messagehtml, $attachment, $attachname); }