Saturday, March 17, 2012

You've got a Stamp! Sending a E-mail notification to a student each time a Stamp is awarded (in Moodle 1.9)

On my drive home from work yesterday afternoon, I had a brain-storm. I thought to myself: "Why can't the Stamp Collection module for Moodle 1.9.x, created by David Nudrak, have its own built-in E-mail notification system?". After all, wouldn't it be useful for students to receive an E-mail from the teacher each time a stamp was awarded?

(Photo credit: Mzelle Biscotte via under the 

Why not indeed? After all, Instructure's Canvas LMS which is built on Ruby-On-Rails has Moodle beaten on the student notification area. Canvas allows multiple channels of notification of events and activities that the teacher has added. Twitter, Facebook, SMS and email are among those channels. So, one thing that the Stamp Collection badly needs, in my humble opinion is E-mail notification.

So I then sat down before my trusty Dell laptop and Samsung LCD monitor on a Saturday weekend and got to work. Coding, testing and debugging PHP and MySQL query code is a ritual that I am only too familiar with.

8 hours(!) later I had managed to raise David Mudrak's Stamp Collection for Moodle 1.9.x to a higher level of functionality. I'm pleased to announce that I have added an E-mail notification feature to the module that will email the student every time a Stamp is awarded by the teacher. The work involved just one file: editstamps.php. I've tested it and it works. It wasn't easy. I had to work from scratch from the code below (which I forgot where I got it from). Coupled with a programmer's intuition of what code may work and what doesn't, I dug around and used Google too many times than I can remember.


 * This is a testmail.php that simulates standard Moodle send email procedure.

$CFG->smtphosts = '';
echo '<pre>' . "\n";

$mail =& get_mailer();
$mail->From     = $CFG->noreplyaddress;
$mail->AddAddress('', 'Frankie');
$mail->Subject = 'Hello world';
$mail->Body =  "\nThis is a test\n";

if ($mail->Send()) {
    echo 'Mail sent';
    echo '</pre>';
} else {
    echo('ERROR: '. $mail->ErrorInfo);
    echo '</pre>';


The code above is a stand-alone PHP code that sends emails to just my email address of My enhanced code is based on this short by this practical program. The biggest challenge (for me at least) was how to get the system to extract the email data from the MySQL Moodle database. The screens below are the result of my 8 hours of toil.

Here are some screenshots of the E-mail notification in action:

Once the "Add" button has been clicked, then the E-mail is sent, but not before a confirmation message appears on the screen.

In this example, I've used my own email address ( as the recepient's email. So let's hop over to my E-mail account, shall we?

My email account opens, you can see the message "Fantastic work on the PowerPoint Presentation on Cloud Computing! Keep it up!" sitting pretty right at the top:

This begs the question. What happens if the student is non-too-pleased to receive emails from the Teacher/Administrator? Can he or she do anything about it? This leads me to a word on privacy. Every Moodle user has an option of enabling or disabling his or her email address. As in the image below:

I've written the code so that if the user or student has disabled his or her email address, then the E-mail notification will not be sent. As I wanted to keep the Email message simple and free from clutter, I did not include typed instructions on how to do this. So the instructor, teacher or administrator should inform the student of his or her rights and how to disable the E-mail. This is important so that the student can maintain his privacy and be pleased that his or her Email inbox is free from intrusive E-mails. On the other hand, the student will miss on out the pleasant joys of receiving notice of awarded stamps.

This modification is important form of engaging the student in Moodle 1.9.x.  I believe that it is an important addition to the Stamp Collection and is that it is sorely needed by Teachers and Administrators. It's been a long time coming. Hats off to David Mudrak for creating an interesting and engaging pedagogical module that touches the psyche!

Lastly, in the true Spirit of Open Source, I'm making the code available on as per GNU GLP License reqirements where if I make a derivative work, I am releasing it under the same terms in which the original code existed. So you can install the code yourself on your Moodle production site if you know how to.

Here's the <<<code>>> to the editstamps.php for the Stamp Collection Module of Moodle 1.9, original code copyrighted by David Mudrak. Have fun and make it make a BIG difference to your institution, college or university!

Update on 18/3/2012 : 
The code in the link above does work but is highly inefficient! I do NOT recomment that you download the <<<code>>> since it involves much redundant code and slows down the Stamp awarding process. Download it to compare its inefficiency with the code found in the orange download badge below!

Update on 18/3/2012 : 
The code in the image link below works and is MUCH MORE EFFICIENT!
Credit and many thanks to Davo Smith for pointing out to me the simplicity and elegance of Moodle's Database functions in this post.

The editstamps.php file contains this code which is instrumental in making the E-mail notification feature possible:

//Davo Smith's code helped me here!
$user = get_record('user', 'id', $newstamp->userid);

$message = $newstamp->text;

$subject = $USER->firstname." ".$USER->lastname.
"'s just given you a NEW STAMP in ".$course->shortname;

//Send off the E-mail!
$success = email_to_user($user, $USER, $subject, $message);

if ($success == "1") {
   echo "E-mail successful sent to ".$user->firstname." ".$user->lastname."!";
} else if ($success == "emailstop") {
          echo "E-mail of ".$user->firstname." ".$user->lastname." has been DISABLED!"
          .'E-mail NOT sent';
       } else if (!$success){
                 echo "Error: E-mail unsuccessful.";
Is there all there is to it? Yup! That's the core code that makes the E-mailing possible!
So you think you can install the code yourself after downloading it? Be my guest!

On the other hand, if you are interested in hiring my services to implement the E-mail notification feature for you, please don't hesitate to contact me for a (very reasonable) quote for my amount of work spent on the modification.

Send Frankie an email
Frankie Kam
Melaka, Malaysia
Related posts:

  •  Stamp Collection module development and stamp icons from other Moodlers. Click here!
  •  Motivating students through multistamps in the Stamp Collection module (Moodle 1.9.15) Click here!

If you like this post or site
a small donation would be nice but would last only a day,
otherwise leaving a comment (or a compliment) below will last me a month!

1 comment:

  1. I almost hesitate to suggest:
    $subject = fullname($USER);



Ratings and Recommendations by outbrain