tce_pdf_results.php 8.92 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:
Nicola Asuni's avatar
Nicola Asuni 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');
nick's avatar
nick 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');
nick's avatar
nick committed
49
require_once('tce_functions_user_select.php');
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
50

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

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
140

Nicola Asuni's avatar
Nicola Asuni committed
141
142
143
$pubmode = false;

// get the data to print
tecnickcom's avatar
tecnickcom committed
144
$ts = F_getAllUsersTestStat($test_id, $group_id, $user_id, $startdate, $enddate, $full_order_field, $pubmode, $display_mode);
Nicola Asuni's avatar
Nicola Asuni committed
145
146
147

if (empty($ts['num_records'])) {
	return;
nick's avatar
nick committed
148
149
}

Nicola Asuni's avatar
Nicola Asuni committed
150
switch ($mode) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
151
152
153
154
155
156
157
	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
nick's avatar
nick committed
158
	case 4: // detailed report for all users
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
	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;
	}
}

// --- create pdf document

$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
174
175
176
$pdf = new TCPDFEX(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, $isunicode);

// Set backlink QR-Code
Nicola Asuni's avatar
Nicola Asuni committed
177
178
179
180
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);
nick's avatar
nick committed
181
}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
182
183
184
185
186
187

// set document information
$pdf->SetCreator('TCExam ver.'.K_TCEXAM_VERSION.'');
$pdf->SetAuthor(PDF_AUTHOR);
$pdf->SetTitle($doc_title);
$pdf->SetSubject($doc_description);
nick's avatar
nick committed
188
$pdf->SetKeywords('TCExam, '.$doc_title);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
189
190
191
192
193
194
195
196
197
198
199
200
201

$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
202
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
203
204
205
206
207
208

$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
209
210
211
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
212
213
}

Nicola Asuni's avatar
Nicola Asuni committed
214
215
216
217
218
219
220
221
222
223
224
$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
225
226
227
228
229
230
231
232
233
234
235
236
	$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
237
}
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
238

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

nick's avatar
nick committed
252
253
254
255
256
257
258
259
260
$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
261
// set PDF file name
tecnickcom's avatar
tecnickcom committed
262
263
264
265
266
267
268
269
270
271
$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';
nick's avatar
nick committed
272

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

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