Certificate module - mail the certificate to the user

Certificate module - mail the certificate to the user

by Marion de Groot -
Number of replies: 3

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 wink

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);
}

Average of ratings: -
In reply to Marion de Groot

Re: Certificate module - mail the certificate to the user

by David Cogg -

Hi Marion,

Thank you very much for posting this, I will start working on it first of the week.

 

thanks again, much appreciated.

 

Dave

In reply to David Cogg

Re: Certificate module - mail the certificate to the user

by William Caban -
I'm using "certificate-1.6_withsecurity.zip" and I notice the lack of support for PostgreSQL so I like to submit the following patch:

In ./certificate/db/

Copy mysql.php as postgres7.php

Create a postgres7.sql file with the following information:

---- postgres7.sql ---
# This file contains a complete database schema for all the
# tables used by this module, written in SQL

CREATE TABLE prefix_certificate (
  id SERIAL PRIMARY KEY,
  course integer NOT NULL default '0',
  name varchar(255) NOT NULL default '',
  type integer  NOT NULL default '0',
  border_style varchar(30) NOT NULL default '0',
  border_color varchar(30) NOT NULL default '0',
  print_wmark varchar(30) NOT NULL default '0',
  date_fmt integer  NOT NULL default '0',
  print_number integer  NOT NULL default '0',
  print_grade integer  NOT NULL default '0',
  print_teacher integer  NOT NULL default '0',
  print_signature varchar(30) NOT NULL default '0',
  print_seal varchar(30) NOT NULL default '0'
);

CREATE TABLE prefix_certificate_viewed (
  id SERIAL PRIMARY KEY,
  courseid integer  NOT NULL default '0',
  userid integer  NOT NULL default '0',
  studentname varchar(40) NOT NULL default '',
  code varchar(30) NOT NULL default '',
  classname varchar(40) NOT NULL default '',
  cert_date integer  NOT NULL default '0',
  timemodified integer  NOT NULL default '0'
);
--- end of file ----