Adding Email Functionality to Quizzin (Plugin for WordPress)

This post is well overdue.

Quite a few months ago I had the pleasure of developing a web site for a textbook. One of the desired features was the ability to take quizzes and receive instant feedback on these quizzes. We were working with WordPress, so I thought finding a plugin to do this would be quite simple. I was mistaken.

After trying out several quiz/survey plugins, Quizzin by Binny VA (http://projects.binnyva.com/languages/php/quizzin-wordpress-plugin-quiz-creation-plugin/) was by far the closest to what we were looking for. We could deploy quizzes and include feedback for correct and incorrect responses. Perfect. Except for one small thing, there was no way to add the option to email the student and/or instructor with the results. If you have ever taken a quiz on a textbook’s web site, you will understand how important this feature is.

I contacted the developer of Quizzin and requested that he add the feature. He was kind enough to respond quickly letting me know it should be pretty easy to accomplish. Bless his heart. That’s when I took a crash course in PHP mail. I started examining every line of code in the plugin and trying all sorts of combinations, coming frustratingly close. After exhausting my wife’s patience, I contacted the developer again. I sent him all the code I had worked out and asked for assistance. His response? Move the code up two lines. Genius! He changed my WordPress life in that moment and steered me toward a working quiz tool.

So, if you are one of those desperate developers attempting to accomplish this same thing, I recommend you install Quizzin (I used version 1.01.4) and make these modifications (or something that fits your needs):

  • Open the “show_quiz.php” file within the Quizzin plugin
  • Locate this code…
    <?php if($answer_display == 2) { ?>
    <input type=”button” id=”show-answer” value=”<?php e(“Show Answer”) ?>”  /><br />
    <?php } else { ?>
    <input type=”button” id=”next-question” value=”<?php e(“Next”) ?> &gt;”  /><br />
    <?php } ?>
  • Create a form with code like this on the very next line after the <?php }?> tag listed in my previous bullet point…
    <strong>Before clicking “Show Results”</strong>: Please enter your instructor’s email address, your email address, and your name. Results from this quiz will be sent to all email addresses entered below once you click “Show Results.”.<br /><br />

    <table border=”0″>
    <tbody>

    <tr>

    <td valign=”top” width=”125″> Instructor’s Email: </td>
    <td valign=”top” width=”*”> <input type=”text” name=”instr_email” size=”18″ /></td>
    <td width=”300″></td>

    </tr>

    <td valign=”top”>Your Email: </td>
    <td valign=”top”><input type=”text” name=”stu_email” size=”18″ /></td>
    <td>(separate email addresses with commas to add multiple recipients)</td>

    </tr>

    <td valign=”top”>Your Name: </td>
    <td valign=”top”><input type=”text” name=”stu_name” size=”18″ /></td>
    <td></td>

    </tr>

    </tbody>
    </table>

    <br />

    <input type=”submit” name=”action” id=”action-button” value=”<?php e(“Show Results”) ?>”  />
    <input type=”hidden” name=”quiz_id” value=”<?php echo  $quiz_id ?>” />
    </form>

  • In the same file, locate this code…
    // Show the results

    print str_replace($replace_these, $with_these, stripslashes($quiz_details->final_screen));
    if($answer_display == 1) print ‘<hr />’ . $result;

  • Insert something like the following code on the very next line after the “if($answer_display == 1) print ‘<hr />’ . $result;” line in my previous bullet point, but remember to customize it for your use…
    //Send an email with results

    function make_safe($variable) { $variable = mysql_real_escape_string(trim($variable)); return $variable; }

    $to = make_safe($_POST[‘instr_email’] . “,” . $_POST[‘stu_email’]);
    $stu_name = make_safe($_POST[‘stu_name’]);
    $headers  = ‘MIME-Version: 1.0’ . “\r\n”;
    $headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”;
    $headers .= ‘From: My Web Site <admin@mywebsite.com>’ . “\r\n”;
    $headers .= ‘Reply-To: admin@mywebsite.com’ . “\r\n” .
    $subject = ‘Quiz Results for ‘ . $stu_name . ‘, ‘ . $quiz_details->name;
    $email_results = ‘<font color=”#000000″><br />Please do not respond to this email<br /><font size=”5″>Quiz: ‘ . $quiz_details->name . ‘<br />Student Name: ‘ . $stu_name . ‘<br />Score: ‘ . $score . ‘ of ‘ . $total . ‘<br /></font>’ . $result . ‘</font>’;
    mail($to,$subject,$email_results,$headers);

  • Save your changes and you should be all set (unless I am forgetting something since I did this some time ago now)
  • Make a back-up of the Quizzin plugin since any updates will overwrite these custom changes.

There were a few styling changes I made so it would be more obvious in an email whether a question was answered correctly or not. I changed the if(strpos($class,’answer correct-answer’)) $result line to if(strpos($class,’answer correct-answer’)) $result .= “<font color=’green’> (Your selected answer is correct)</font>”; and if(strpos($class,’ user-answer’)) if(!strpos($class,’ correct-answer’)) $result to if(strpos($class,’ user-answer’)) if(!strpos($class,’ correct-answer’)) $result .= “<font color=’red’> (Your selected answer is incorrect)</font>”;.

Though CSS is generally the way you want to go with styling, remember that your CSS file will not be present in the email received by the student and/or instructor. This is why I used the font tags. Good luck and may some poor soul have an easier time of it than I did.

Advertisements

3 thoughts on “Adding Email Functionality to Quizzin (Plugin for WordPress)

  1. rob says:

    hello,
    I have been struggling with the same issue and I feel that Quizzin is a great plugin.
    I have tried to add the code you suggest and it keeps coming up with errors. such as:
    Parse error: syntax error, unexpected ‘:’ in /home/wp-content/plugins/quizzin/show_quiz.php on line 80

    any help on this would be greatly appreciated
    thanks

  2. deb says:

    This is absolutely brilliant! Thanks for sharing this. I was able to get this functioning quite quickly. The only thing I couldn’t figure out was the styling changes. I don’t see that code to change.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s