tce_pdf_results.php 8.95 KB
Newer Older
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
1
2
3
4
<?php
//============================================================+
// File name   : tce_pdf_results.php
// Begin       : 2004-06-10
tecnickcom's avatar
tecnickcom committed
5
// Last Update : 2014-01-27
nick's avatar
nick committed
6
7
//
// Description : Create PDF document to display test results
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
8
//               summary for all users.
nick's avatar
nick committed
9
//
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
10
11
12
13
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
Nick's avatar
Nick committed
14
//               Tecnick.com LTD
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
15
16
17
//               www.tecnick.com
//               info@tecnick.com
//
nick's avatar
nick committed
18
// License:
tecnickcom's avatar
tecnickcom committed
19
//    Copyright (C) 2004-2013 Nicola Asuni - Tecnick.com LTD
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
20
21
22
23
//    See LICENSE.TXT file for more information.
//============================================================+

/**
nick's avatar
nick committed
24
 * @file
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
25
26
27
28
 * Create PDF document to display users' tests results.
 * @package com.tecnick.tcexam.admin
 * @author Nicola Asuni
 * @since 2004-06-11
Nicola Asuni's avatar
Nicola Asuni committed
29
30
31
32
33
34
35
 * @param $_REQUEST['mode'] (int) document mode: 1=all users results, 3=detailed report for single user; 4=detailed report for all users; 5=detailed report for all users with only TEXT questions.
 * @param $_REQUEST['test_id'] (int) test ID
 * @param $_REQUEST['user_id'] (int) user ID
 * @param $_REQUEST['group_id'] (int) group ID
 * @param $_REQUEST['testuser_id'] (int) test user ID
 * @param $_REQUEST['order_field'] (string) ORDER BY portion of SQL selection query
 * @param $_REQUEST['orderdir'] (int) Ordering direction.
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
36
37
38
39
40
41
 */

/**
 */

require_once('../config/tce_config.php');
Nicola Asuni's avatar
Nicola Asuni committed
42
require_once('../../shared/code/tce_authorization.php');
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
43
44
45
46
require_once('../../shared/code/tce_functions_tcecode.php');
require_once('../../shared/code/tce_functions_test.php');
require_once('../../shared/code/tce_functions_test_stats.php');
require_once('../../shared/config/tce_pdf.php');
nick's avatar
nick committed
47
require_once('../../shared/code/tcpdfex.php');
Nicola Asuni's avatar
Nicola Asuni committed
48
require_once('../../shared/code/tce_functions_statistics.php');
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
49

Nicola Asuni's avatar
Nicola Asuni committed
50
$user_id = intval($_SESSION['session_user_id']);
tecnickcom's avatar
tecnickcom committed
51

Nicola Asuni's avatar
Nicola Asuni committed
52
53
54
55
56
if (isset($_REQUEST['mode']) AND ($_REQUEST['mode'] > 0)) {
	$mode = intval($_REQUEST['mode']);
} else {
	$mode = 0;
}
tecnickcom's avatar
tecnickcom committed
57
$onlytext = ($mode == 5);
tecnickcom's avatar
tecnickcom committed
58
if (isset($_REQUEST['email']) AND ($_REQUEST['email'] != getPasswordHash(date('Y').$testuser_id.K_RANDOM_SECURITY.$test_id.date('m').$user_id))) {
Nicola Asuni's avatar
Nicola Asuni committed
59
	F_print_error('ERROR', $l['m_authorization_denied']);
nick's avatar
nick committed
60
61
	exit;
}
Nicola Asuni's avatar
Nicola Asuni committed
62
63
64
65
66
67
68
69
70
$filter = 'sel=1';
if (isset($_REQUEST['test_id']) AND ($_REQUEST['test_id'] > 0)) {
	$test_id = intval($_REQUEST['test_id']);
	if (!isset($_REQUEST['email'])) {
		if (!F_isAuthorizedUser(K_TABLE_TESTS, 'test_id', $test_id, 'test_user_id')) {
			exit;
		}
	}
	$filter .= '&amp;test_id='.$test_id.'';
nick's avatar
nick committed
71
} else {
Nicola Asuni's avatar
Nicola Asuni committed
72
	$test_id = 0;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
73
}
Nicola Asuni's avatar
Nicola Asuni committed
74
75
76
77
78
if (isset($_REQUEST['group_id']) AND ($_REQUEST['group_id'] > 0)) {
	$group_id = intval($_REQUEST['group_id']);
	$filter .= '&amp;group_id='.$group_id.'';
} else {
	$group_id = 0;
nick's avatar
nick committed
79
}
Nicola Asuni's avatar
Nicola Asuni committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
if (isset($_REQUEST['testuser_id']) AND ($_REQUEST['testuser_id'] > 1)) {
	$testuser_id = intval($_REQUEST['testuser_id']);
	$filter .= '&amp;testuser_id='.$testuser_id.'';
} else {
	$testuser_id = 0;
}
if (isset($_REQUEST['startdate'])) {
	$startdate = $_REQUEST['startdate'];
	$startdate_time = strtotime($startdate);
	$startdate = date(K_TIMESTAMP_FORMAT, $startdate_time);
	$filter .= '&amp;startdate='.urlencode($startdate);
} else {
	$startdate = '';
}
if (isset($_REQUEST['enddate'])) {
	$enddate = $_REQUEST['enddate'];
	$enddate_time = strtotime($enddate);
	$enddate = date(K_TIMESTAMP_FORMAT, $enddate_time);
	$filter .= '&amp;enddate='.urlencode($enddate).'';
} else {
	$enddate = '';
}
tecnickcom's avatar
tecnickcom committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

if (isset($_REQUEST['display_mode'])) {
	$display_mode = max(0, min(5, intval($_REQUEST['display_mode'])));
	$filter .= '&amp;display_mode='.$display_mode;
} else {
	$display_mode = 0;
}

if (isset($_REQUEST['show_graph'])) {
	$show_graph = intval($_REQUEST['show_graph']);
	$filter .= '&amp;show_graph='.$show_graph;
	if ($show_graph AND ($display_mode == 0)) {
		$display_mode = 1;
	}
} else {
	$show_graph = 0;
}

Nicola Asuni's avatar
Nicola Asuni committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
if (isset($_REQUEST['order_field']) AND !empty($_REQUEST['order_field']) AND (in_array($_REQUEST['order_field'], array('testuser_creation_time', 'testuser_end_time', 'user_name', 'user_lastname', 'user_firstname', 'total_score', 'testuser_test_id')))) {
	$order_field = $_REQUEST['order_field'];
} else {
	$order_field = 'total_score, user_lastname, user_firstname';
}
$filter .= '&amp;order_field='.urlencode($order_field).'';
if (!isset($_REQUEST['orderdir']) OR empty($_REQUEST['orderdir'])) {
	$orderdir = 0;
	$nextorderdir = 1;
	$full_order_field = $order_field;
} else {
	$orderdir = 1;
	$nextorderdir = 0;
	$full_order_field = $order_field.' DESC';
}
$filter .= '&amp;orderdir='.$orderdir.'';
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
136

Nicola Asuni's avatar
Nicola Asuni committed
137
$pubmode = true;
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
138

Nicola Asuni's avatar
Nicola Asuni committed
139
// get the data to print
tecnickcom's avatar
tecnickcom committed
140
$ts = F_getAllUsersTestStat($test_id, $group_id, $user_id, $startdate, $enddate, $full_order_field, $pubmode, $display_mode);
Nicola Asuni's avatar
Nicola Asuni committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

if (empty($ts['num_records'])) {
	return;
}

switch ($mode) {
	case 1: {
		// all users results
		$doc_title = unhtmlentities($l['t_result_all_users']);
		$doc_description = F_compact_string(unhtmlentities($l['hp_result_alluser']));
		break;
	}
	case 3: // detailed report for specific user
	case 4: // detailed report for all users
	case 5: { // detailed report for all users with only open questions
		$doc_title = unhtmlentities($l['t_result_user']);
		$doc_description = F_compact_string(unhtmlentities($l['hp_result_user']));
		break;
	}
	default: {
		echo $l['m_authorization_denied'];
		exit;
	}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
164
165
}

Nicola Asuni's avatar
Nicola Asuni committed
166
167
// --- create pdf document

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
168
169
$isunicode = (strcasecmp($l['a_meta_charset'], 'UTF-8') == 0);
//create new PDF document (document units are set by default to millimeters)
nick's avatar
nick committed
170
171
$pdf = new TCPDFEX(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, $isunicode);

Nicola Asuni's avatar
Nicola Asuni committed
172
173
174
175
176
177
178
// Set backlink QR-Code
if ($pubmode) {
	$pdf->setTCExamBackLink(K_PATH_URL.'public/code/tce_test_allresults.php?'.$filter);
} else  {
	$pdf->setTCExamBackLink(K_PATH_URL.'admin/code/tce_show_result_allusers.php?'.$filter);
}

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// set document information
$pdf->SetCreator('TCExam ver.'.K_TCEXAM_VERSION.'');
$pdf->SetAuthor(PDF_AUTHOR);
$pdf->SetTitle($doc_title);
$pdf->SetSubject($doc_description);
$pdf->SetKeywords('TCExam, '.$doc_title);

$pdf->setHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

$pdf->setHeaderMargin(PDF_MARGIN_HEADER);
$pdf->setFooterMargin(PDF_MARGIN_FOOTER);

//set image scale factor
nick's avatar
nick committed
198
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
199
200
201
202
203
204

$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

$pdf->setLanguageArray($l); //set language items

nicolaasuni's avatar
9.0.023    
nicolaasuni committed
205
206
207
if (defined('K_DIGSIG_ENABLE') AND K_DIGSIG_ENABLE) {
	// set document signature
	$pdf->setSignature(K_DIGSIG_CERTIFICATE, K_DIGSIG_PRIVATE_KEY, K_DIGSIG_PASSWORD, K_DIGSIG_EXTRA_CERTS, K_DIGSIG_CERT_TYPE, array('Name'=>K_DIGSIG_NAME, 'Location'=>K_DIGSIG_LOCATION, 'Reason'=>K_DIGSIG_REASON, 'ContactInfo'=>K_DIGSIG_CONTACT));
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
208
209
}

Nicola Asuni's avatar
Nicola Asuni committed
210
211
212
213
214
215
216
217
218
219
220
$pdf->SetFillColor(204, 204, 204);
$pdf->SetLineWidth(0.1);
$pdf->SetDrawColor(0, 0, 0);

if ($mode != 3) {
	$pdf->AddPage();
	// print document name (title)
	$pdf->SetFont(PDF_FONT_NAME_DATA, 'B', PDF_FONT_SIZE_DATA * K_TITLE_MAGNIFICATION);
	$pdf->Cell(0, 0, $doc_title, 1, 1, 'C', 1);
	$pdf->Ln(5);
	// print test stats table
tecnickcom's avatar
tecnickcom committed
221
222
223
224
225
226
227
228
229
230
231
	$pdf->printTestResultStat($ts, $pubmode, $display_mode);
	if ($show_graph) {
		// display graph
		$pdf->Ln(5);
		$pdf->printSVGStatsGraph($ts['svgpoints']);
	}
	if ($display_mode > 1) {
		// print question
		$pdf->Bookmark($l['w_statistics']);
		$pdf->printQuestionStats($ts['qstats'], $display_mode);
	}
nick's avatar
nick committed
232
}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
233

Nicola Asuni's avatar
Nicola Asuni committed
234
235
236
237
238
239
240
if ($mode > 2) {
	// print testuser details
	if ($testuser_id == 0) {
		foreach ($ts['testuser'] as $tstusr) {
			if ((!$pubmode) OR F_getBoolean($tstusr['test']['test_report_to_users'])) {
				$pdf->AddPage();
				$pdf->printTestUserInfo($tstusr, $onlytext, $pubmode);
nick's avatar
nick committed
241
242
			}
		}
Nicola Asuni's avatar
Nicola Asuni committed
243
244
245
246
	} else {
		if ((!$pubmode) OR F_getBoolean($ts['testuser']['\''.$testuser_id.'\'']['test']['test_report_to_users'])) {
			$pdf->AddPage();
			$pdf->printTestUserInfo($ts['testuser']['\''.$testuser_id.'\''], $onlytext, $pubmode);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
247
248
249
250
		}
	}
}

nick's avatar
nick committed
251
252
253
254
255
256
257
258
$pdf->lastpage(true);
$pdf->SetAutoPageBreak(false);
$pdf->SetFont('helvetica', '', 5);
$pdf->SetTextColor(0,127,255);
$msg = "\x50\x6f\x77\x65\x72\x65\x64\x20\x62\x79\x20\x54\x43\x45\x78\x61\x6d\x20\x28\x77\x77\x77\x2e\x74\x63\x65\x78\x61\x6d\x2e\x6f\x72\x67\x29";
$lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x65\x78\x61\x6d\x2e\x6f\x72\x67";
$pdf->SetXY(15, $pdf->getPageHeight(), true);
$pdf->Cell(0, 0, $msg, 0, 0, 'R', 0, $lnk, 0, false, 'B', 'B');
nick's avatar
nick committed
259

Nicola Asuni's avatar
Nicola Asuni committed
260
// set PDF file name
tecnickcom's avatar
tecnickcom committed
261
262
263
264
265
266
267
268
269
270
$pdf_filename = 'tcexam_report';
$pdf_filename .= empty($startdate) ? '' : '_'.date('YmdHis', $startdate_time);
$pdf_filename .= empty($enddate) ? '' : '_'.date('YmdHis', $enddate_time);
$pdf_filename .= '_'.$mode;
$pdf_filename .= '_'.$display_mode;
$pdf_filename .= '_'.$test_id;
$pdf_filename .= '_'.$group_id;
$pdf_filename .= '_'.$user_id;
$pdf_filename .= '_'.$testuser_id;
$pdf_filename .= '.pdf';
Nicola Asuni's avatar
Nicola Asuni committed
271

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
272
// Send PDF output
Nicola Asuni's avatar
Nicola Asuni committed
273
$pdf->Output($pdf_filename, 'D');
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
274
275

//============================================================+
nick's avatar
nick committed
276
// END OF FILE
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
277
//============================================================+