[Xerte] Re: certificate printing by student if quiz passed
Paul Swanson
Paul.Swanson at harlandfs.com
Fri Feb 4 21:11:10 GMT 2011
I pass information from Xerte to a PHP script that, after saving information to a MySQL database, uses an FPDF library to create a PDF certificate that the user downloads and can print or save. In Xerte, I have a script icon that runs after the quiz is taken. This runs whether or not the user passes.
// url of PHP script
URLString = 'http://myDomain/pathToScript/db_queries.php';
// pass values to external script
saveVars = new LoadVars ();
saveVars.action = 'saveData';
saveVars.sid = student.id;
saveVars.totCorrect = totCorrect;
saveVars.quesCount = quesCount;
saveVars.testTries = student.testTries;
saveVars.testStartDate = student.testStartDate;
saveVars.score = score;
saveVars.pass = scorePath; // scorePath will be 0 if score < 80, otherwise it will be 1
saveVars.complete = scorePath; // only set complete to 1 if student passed
saveVars.institution = student.institution;
saveVars.email = student.email;
saveVars.name = student.fname + ' ' + student.lname;
saveVars.product = productName;
saveVars.courseName = courseTitle;
// send to db
testScore.sendAndLoadVars(URLString, saveVars);
Then, my PHP script has (among other things):
// pass -- boolean indicating whether student passed
if (isset ($_POST['pass']) && is_numeric ($_POST['pass'])) {
$pass = (int) $_POST['pass'];
// if $pass == 1 set active to 0 (false) so user would have to re-register to take again
if ($pass == 1) {
$active = 0;
// need to send email with certificate
// must have courseName, student name, institution, completion date and email
if (isset ($_POST['courseName'], $_POST['name'], $_POST['institution'], $_POST['email'])) {
$completion_date = date ('m/d/Y');
$url = 'http://' . $_SERVER['HTTP_HOST'] . '/wbt/certificate_link.php?';
$certCourseTitle = ($_POST['product'] == 'SBA Lending Solution' ? 'SBA Lending Solution' : $_POST['courseName']);
$qString = "courseName=$certCourseTitle&name={$_POST['name']}&company={$_POST['institution']}&completionDate=$completion_date";
// encrypt $qString
$rc4 = new rc4crypt;
$encString = $rc4->endecrypt('someCipherSalt',$qString);
$to = "{$_POST['email']}";
$from = "Education at example.com\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from\r\n";
$subject = "Certificate of Completion for {$_POST['product']} {$_POST['courseName']}";
$body = "<body style=\"font-family:arial;font-size:12pt;\">
<p>Congratulations on passing the {$_POST['product']}<sup>®</sup> {$_POST['courseName']}!</p>
<p>A Certificate of Completion for you is waiting at <a href=\"http://{$_SERVER['HTTP_HOST']}/wbt/certificate_link.php?data=$encString\">this link</a>. Recertification is required after one year.</p>
<p>When you open the link, you will be asked whether to open or save
the file. If you select Open, be sure to save the file after viewing
or printing.</p>
</body>";
$mail_success = mail ($to, $subject, $body, $headers, "-fEducation at example.com");
The encryption code is something I copied from the FPDF library. The script that creates the PDF file after the user clicks the encrypted link in their email:
<?php # certificate_link.php
/*
This script creates a pdf certificate of completion.
*/
// initialize variables
$course = FALSE;
$name = FALSE;
$company = FALSE;
$date = FALSE;
// class to encrypt/decrypt strings
class rc4crypt {
function endecrypt ($pwd, $data, $case='') {
if ($case == 'de') {
$data = urldecode($data);
}
$key[] = "";
$box[] = "";
$temp_swap = "";
$pwd_length = 0;
$pwd_length = strlen($pwd);
for ($i = 0; $i <= 255; $i++) {
$key[$i] = ord(substr($pwd, ($i % $pwd_length), 1));
$box[$i] = $i;
}
$x = 0;
for ($i = 0; $i <= 255; $i++) {
$x = ($x + $box[$i] + $key[$i]) % 256;
$temp_swap = $box[$i];
$box[$i] = $box[$x];
$box[$x] = $temp_swap;
}
$temp = "";
$k = "";
$cipherby = "";
$cipher = "";
$a = 0;
$j = 0;
for ($i = 0; $i < strlen($data); $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$temp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $temp;
$k = $box[(($box[$a] + $box[$j]) % 256)];
$cipherby = ord(substr($data, $i, 1)) ^ $k;
$cipher .= chr($cipherby);
}
if ($case == 'de') {
$cipher = urldecode(urlencode($cipher));
} else {
$cipher = urlencode($cipher);
}
return $cipher;
}
} // end of class rc4crypt
// require the fpdf script
require_once $_SERVER['DOCUMENT_ROOT'] . '/fpdf/fpdf.php';
// require subwrite script to allow superscript/subscripts
require_once $_SERVER['DOCUMENT_ROOT'] . '/fpdf/subwrite.php';
// require fpdf_protection script to add security to the document
require_once $_SERVER['DOCUMENT_ROOT'] . '/fpdf/tools/fpdf_protection.php';
// validate required data
if (isset ($_GET['data'])) {
// decrypt data
$rc4 = new rc4crypt;
$deString = $rc4->endecrypt('someCipherSalt', $_GET['data'], 'de');
$qString = $deString;
// parse string into parts
parse_str ($qString);
// convert date
$date = date ('F jS, Y', strtotime ($completionDate));
} // end of $_GET['data'] conditional
// create pdf if required fields present
if ($course && $name && $company && $date) {
$pdf = new FPDF_Protection();
$pdf->SetProtection(array('print'),'','somePassword'); // restrict to view and print, no user password, owner password needed to edit doc
$pdf->SetMargins(72,96,72);
$pdf->SetLineWidth(20);
$pdf->SetDrawColor(0, 128, 0);
$pdf->SetFillColor(197, 255, 197);
$pdf->AddPage();
$pdf->Rect(0, 0, 790, 610, 'DF');
$pdf->Ln();
$pdf->SetFont('Times', 'BI');
$pdf->SetFontSize(36);
$pdf->Cell(0, 48, 'Certificate of Completion', 0, 1, 'C');
$pdf->Ln();
$pdf->SetFontSize(24);
$pdf->Cell(0, 24, "$name", 0, 1, 'C');
$pdf->SetFont('', 'I');
$pdf->SetFontSize(18);
$pdf->Cell(0, 18, "of", 0, 1, 'C');
$pdf->SetFont('', 'BI');
$pdf->SetFontSize(24);
$pdf->Cell(0, 24, "$company", 0, 1, 'C');
$pdf->Ln();
$pdf->SetFont('', 'I');
$pdf->SetFontSize(20);
$pdf->Cell(0, 24, "has achieved certification in", 0, 1, 'C');
$pdf->Ln();
$pdf->SetFont('', 'BI');
$pdf->SetFontSize(24);
$pdf->Cell(36, 24, '', 0, 0, 'L');
// modify course name if $course == 'SBA Lending Solution'
if ($course == 'SBA Lending Solution Exam') {
$pdf->Cell(120, 24, '', 0, 0, 'L');
$pdf->Cell(225, 24, 'SBA Lending Solution', 0, 0, 'L');
$pdf->subWrite(24, '®', '', 12, 9);
$pdf->Cell(0, 24, ' Exam', 0, 1, 'L');
} else {
$pdf->Cell(36, 24, '', 0, 0, 'L');
$pdf->Cell(92, 24, 'LaserPro', 0, 0, 'L');
$pdf->subWrite(24, '®', '', 12, 9);
$pdf->Cell(0, 24, $course, 0, 1, 'L');
}
$pdf->Ln();
$pdf->SetFont('', 'I');
$pdf->SetFontSize(18);
$pdf->Cell(0, 18, "on", 0, 1, 'C');
$pdf->SetFontSize(24);
$pdf->Cell(0, 24, "$date", 0, 1, 'C');
$pdf->Image('hfs_hc_background.png', 100, 465, 207, 32, 'png');
$pdf->Image('cert_sig.png', 400, 460, 285, 80, 'png');
// create file if $certDest and $certFile exist, otherwise display in browser
if ($certDest && $certFile) {
$pdf->Output($certDest . $certFile . str_replace (' ', '_', $course) . '.pdf', "F");
} else {
//$pdf->Output();
$pdf->Output('LaserPro_' . str_replace (' ', '_', $course) . '.pdf', 'D');
}
} else {
echo "<p>Required data missing, no file created.</p>\n\n";
}
?>
FPDF can be found here: http://www.fpdf.org/
Of course, all this requires that your course is on the web, your web server has PHP installed and configured and you've installed the FPDF library.
Cheers,
Paul
From: xerte-bounces at lists.nottingham.ac.uk [mailto:xerte-bounces at lists.nottingham.ac.uk] On Behalf Of KnowledgeWare
Sent: Friday, February 04, 2011 12:03 PM
To: 'Xerte discussion list'
Subject: [Xerte] Re: certificate printing by student if quiz passed
Wow it's great to see the volume of this list expanding..but hard to keep up!
I'm wondering if anyone has built a page to print a student certificate or has suggestions around how to do this. Ideally, if the student passes the quiz I'd like to allow them to print a certificate and/or email it to themselves from xerte (not LMS). Anyone doing this now?
TIA
RonM2
This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.
This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system: you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nottingham.ac.uk/pipermail/xerte/attachments/20110204/6a146ba2/attachment-0001.html>
More information about the Xerte
mailing list