a5029f21aef142c93879348a2335456f434550fb
[atutor.git] / mods / certify / download_certificate.php.bak
1 <?php
2 define('AT_INCLUDE_PATH', '../../include/');
3
4 require (AT_INCLUDE_PATH.'vitals.inc.php');
5 require 'certify_functions.php';
6
7 // authenticate(AT_PRIV_CERTIFY); // TODO: Find correct privileges
8
9 if (isset($_GET['certify_id']))
10         $certify_id     = $addslashes($_GET['certify_id']);
11
12 $filebase = AT_CONTENT_DIR .'certify/cert_'.$_SESSION['member_id'].'_'.$certify_id.'.';
13 $templatefile = AT_CONTENT_DIR .'certify/template_'.$certify_id.'.pdf';
14 if (file_exists($templatefile)) {
15         $template = $templatefile;
16 } else {
17         $template = dirname(realpath('test.pdf')).'/mods/certify/test.pdf';
18 }
19
20
21 if (!file_exists($filebase.'pdf')) {
22         // Fetch cached scores
23         
24         $sql =  '
25         
26                 SELECT
27                         '.TABLE_PREFIX.'courses.title AS coursetitle,
28                         '.TABLE_PREFIX.'members.first_name,
29                         '.TABLE_PREFIX.'members.second_name,
30                         '.TABLE_PREFIX.'members.last_name,
31                         '.TABLE_PREFIX.'members.email,
32                         '.TABLE_PREFIX.'certify.title AS certifytitle
33         
34                 FROM '.TABLE_PREFIX.'members
35                 INNER JOIN '.TABLE_PREFIX.'certify ON '.TABLE_PREFIX.'certify.certify_id = '.$certify_id.'
36                 INNER JOIN '.TABLE_PREFIX.'courses ON '.TABLE_PREFIX.'certify.course_id = '.TABLE_PREFIX.'courses.course_id
37         
38                 WHERE '.TABLE_PREFIX.'members.member_id = '.$_SESSION['member_id'].' 
39         ';
40
41         //echo $sql;
42         //exit();
43
44         $result = mysql_query($sql, $db) or die(mysql_error() . $sql);
45         
46         if ( !$row = mysql_fetch_assoc($result)) { // Probably a hack attempt, so aborting should be sufficient
47                 echo "Oh no you don't!";
48                 exit();
49         }
50
51         if ( getCertificateProgress($_SESSION['member_id'], $certify_id)<100 ) { // Probably a hack attempt, so aborting should be sufficient
52                 echo "Oh no you don't!";
53                 exit();
54         }
55
56         // Generate FDF
57
58
59         $params = array(
60         //      'course_name'   => iconv("UTF-8", "ISO-8859-1//IGNORE", $row['coursetitle']),
61                 'full_name'             => iconv("UTF-8", "ISO-8859-1//IGNORE", implode(' ',array($row['first_name'],$row['second_name'],$row['last_name']))),
62                 'email'                 => $row['email'],
63                 'test_name'             => iconv("UTF-8", "ISO-8859-1//IGNORE", $row['certifytitle']),
64         //      'score'                 => iconv("UTF-8", "ISO-8859-1//IGNORE", 'Bestått'),
65                 'issued_date'   => iconv("UTF-8", "ISO-8859-1//IGNORE", date('F j, Y'))
66         );
67         
68         $fdfparams = '';
69         foreach ($params as $key => $value) {
70                 $fdfparams .= '<</T('.$key.')/V('.$value.')>>';
71         }
72         
73         //$filename = tempnam('', 'atutor_certify');
74         $filename = $filebase.'fdf';
75
76         $handle = fopen($filename,'wb');
77         fwrite($handle,"%FDF-1.2
78 %\xE2\xE3\xCF\xD3
79 1 0 obj
80 << 
81 /FDF << /Fields [ ".$fdfparams."] 
82 /F (http://www.helsekompetanse.no/test.pdf) /ID [ <".md5(time()).">
83 ] >> 
84 >> 
85 endobj
86 trailer
87 <<
88 /Root 1 0 R 
89
90 >>
91 %%EOF");
92         fclose($handle);
93         
94         // Flatten with PDF
95
96         $output = array();
97         $return_var = 0;
98         
99 //      $exec = '/opt/local/bin/pdftk '.$template.' fill_form '.$filename.' output '.$filebase.'pdf flatten';
100         $exec = '/usr/bin/pdftk '.$template.' fill_form '.$filename.' output '.$filebase.'pdf flatten';
101         exec($exec, $output, $return_var);
102
103         //unlink($filename);
104 }
105
106 if (file_exists($filebase.'pdf')) {
107
108   // Send PDF
109   header('Content-Description: File Transfer');
110   header('Content-Type: application/pdf');
111   header('Content-Disposition: attachment; filename="'.basename($filebase.'pdf').'"'); // TODO: Fix better filename
112   header('Content-Transfer-Encoding: binary');
113   header('Expires: 0');
114   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
115   header('Pragma: public');
116   header('Content-Length: ' . filesize($filebase.'pdf'));
117   //ob_clean();
118   //flush();
119   readfile($filebase.'pdf');
120
121 } else {
122   echo "PDFTK failed - not installed or wrong path?";
123   exit;
124 }
125 ?>