Sending the certificate to the user seems to be on many peoples' wishlist, so I'll share my code. I've tried to do things the 'moodle-way' as much as I could I've heavily customized my certificate module, so I hope I've destillated from my files all the right codelines that are used for sending the certificate to the user. If not I'll hear it from you all soon enough, I guess
In view.php:
before the switch($type)
type these lines:
// the location and name of the saved certificate
//you can replace my ' cert_" . $course->shortname . ".pdf" ' with whatever you would like to call your certificate
$filenaam = $CFG->dataroot . "/" . $course->id . "/moddata/certificate/cert_" . $course->shortname . ".pdf";
Then at the end of view.php, add/replace these lines:
$pdf->Output($filenaam, 'F');//save as file
$pdf->Output('certificate.pdf', 'I');//show in browser
certificate_send_email($USER);// send email to user, with certificate as attachment
In lib.php:
In the function certificate_add_instance($certificate) {
add these lines before the line "return insert_record("certificate", $certificate);":
$uploaddir = $course->id.'/moddata/certificate/';
make_upload_directory($uploaddir);
And at the end of lib.php, add this function:
function certificate_send_email($USER) {
global $course, $CFG;
// $from is the sender of the message, I use the admin user
$from = get_admin();
// subject of the message
// this is my example subject, replace it with your own subject;
$subject = "Your certificate for \"".$course->fullname."\"";
// use the $message variable when creating a text only message
// this is my example message, replace it with your own message; \n is to go to the next line
$message ="Dear ".$USER->firstname.",\n\nThank you for attending our course \"".$course->fullname."\".\nAttachted to this message you'll find your certificate of completion. \n\nRegards,\n\nThe course moderator\n\nMarion de Groot";
// use the $messagehtml when creating a html message
$messagehtml="";
// $attachment is the location of the file relative your moodledata dir.
//The certificate is saved as moodledata/$course->id . "/moddata/certificate/cert_" . $course->shortname . ".pdf, so:
$attachment= $course->id . "/moddata/certificate/cert_" . $course->shortname . ".pdf";
// $attachname is the name of the certificate file that should be send, in my case :
$attachname= "cert_" . $course->shortname . ".pdf";
//this line calls the function moodle uses to send email messages, like for the forum
return email_to_user($USER, $from, $subject, $message, $messagehtml, $attachment, $attachname);
}