tce_functions_email_reports.php 8.59 KB
Newer Older
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
1
2
3
4
<?php
//============================================================+
// File name   : tce_functions_email_reports.php
// Begin       : 2005-02-24
tecnickcom's avatar
tecnickcom committed
5
// Last Update : 2014-01-27
nick's avatar
nick committed
6
//
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
7
8
9
10
11
12
// Description : Sends email test reports to users.
//
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
Nick's avatar
Nick committed
13
//               Tecnick.com LTD
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
14
15
16
//               www.tecnick.com
//               info@tecnick.com
//
nick's avatar
nick committed
17
// License:
tecnickcom's avatar
tecnickcom committed
18
//    Copyright (C) 2004-2014 Nicola Asuni - Tecnick.com LTD
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
19
20
21
22
//    See LICENSE.TXT file for more information.
//============================================================+

/**
nick's avatar
nick committed
23
 * @file
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
24
25
26
27
28
29
30
31
32
33
 * Functions to send email reports to users.
 * @package com.tecnick.tcexam.admin
 * @author Nicola Asuni
 * @since 2005-02-24
 */

/**
 * Sends email test reports to users.
 * @author Nicola Asuni
 * @since 2005-02-24
nick's avatar
nick committed
34
35
 * @param $test_id (int) TEST ID
 * @param $user_id (int) USER ID (0 means all users)
Nicola Asuni's avatar
Nicola Asuni committed
36
 * @param $testuser_id (int) test-user ID - if greater than zero, filter stats for the specified test-user.
nick's avatar
nick committed
37
 * @param $group_id (int) GROUP ID (0 means all groups)
Nicola Asuni's avatar
Nicola Asuni committed
38
39
 * @param $startdate (int) start date ID - if greater than zero, filter stats for the specified starting date
 * @param $enddate (int) end date ID - if greater than zero, filter stats for the specified ending date
nick's avatar
nick committed
40
 * @param $mode (int) type of report to send: 0=detailed report; 1=summary report (without questions)
tecnickcom's avatar
tecnickcom committed
41
42
 * @param $display_mode display (int) mode: 0 = disabled; 1 = minimum; 2 = module; 3 = subject; 4 = question; 5 = answer.
 * @param $show_graph (boolean) If true display the score graph.
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
43
 */
tecnickcom's avatar
tecnickcom committed
44
function F_send_report_emails($test_id, $user_id=0, $testuser_id=0, $group_id=0, $startdate=0, $enddate=0, $mode=0, $display_mode=1, $show_graph=false) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
45
46
	global $l, $db;
	require_once('../config/tce_config.php');
Nick's avatar
9.0.037    
Nick committed
47
48
	require_once('../../shared/code/tce_functions_test.php');
	require_once('../../shared/code/tce_functions_test_stats.php');
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
49
	require_once('../../shared/code/tce_class_mailer.php');
nick's avatar
nick committed
50
	require_once('tce_functions_user_select.php');
nick's avatar
nick committed
51

Nick's avatar
9.0.037    
Nick committed
52
	$mode = intval($mode);
Nicola Asuni's avatar
Nicola Asuni committed
53
54
55
56
57
58
59
	if ($test_id > 0) {
		$test_id = intval($test_id);
		if (!F_isAuthorizedUser(K_TABLE_TESTS, 'test_id', $test_id, 'test_user_id')) {
			return;
		}
	} else {
		$test_id = 0;
nick's avatar
nick committed
60
	}
Nicola Asuni's avatar
Nicola Asuni committed
61
62
63
64
	if ($user_id > 0) {
		$user_id = intval($user_id);
	} else {
		$user_id = 0;
nick's avatar
nick committed
65
	}
Nicola Asuni's avatar
Nicola Asuni committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	if ($testuser_id > 0) {
		$testuser_id = intval($testuser_id);
	} else {
		$testuser_id = 0;
	}
	if ($group_id > 0) {
		$group_id = intval($group_id);
	} else {
		$group_id = 0;
	}
	if (!empty($startdate)) {
		$startdate_time = strtotime($startdate);
		$startdate = date(K_TIMESTAMP_FORMAT, $startdate_time);
	} else {
		$startdate = '';
	}
	if (!empty($enddate)) {
		$enddate_time = strtotime($enddate);
		$enddate = date(K_TIMESTAMP_FORMAT, $enddate_time);
	} else {
		$enddate = '';
nick's avatar
nick committed
87
88
	}

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
89
90
	// Instantiate C_mailer class
	$mail = new C_mailer;
nick's avatar
nick committed
91

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
92
93
94
95
96
97
98
99
100
101
102
103
104
	//Load default values
	$mail->language = $l;
	$mail->Priority = $emailcfg['Priority'];
	$mail->ContentType = $emailcfg['ContentType'];
	$mail->Encoding = $emailcfg['Encoding'];
	$mail->WordWrap = $emailcfg['WordWrap'];
	$mail->Mailer = $emailcfg['Mailer'];
	$mail->Sendmail = $emailcfg['Sendmail'];
	$mail->UseMSMailHeaders = $emailcfg['UseMSMailHeaders'];
	$mail->Host = $emailcfg['Host'];
	$mail->Port = $emailcfg['Port'];
	$mail->Helo = $emailcfg['Helo'];
	$mail->SMTPAuth = $emailcfg['SMTPAuth'];
Nick's avatar
9.0.034    
Nick committed
105
	$mail->SMTPSecure = $emailcfg['SMTPSecure'];
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
106
107
108
109
110
111
112
113
114
115
116
117
	$mail->Username = $emailcfg['Username'];
	$mail->Password = $emailcfg['Password'];
	$mail->Timeout = $emailcfg['Timeout'];
	$mail->SMTPDebug = $emailcfg['SMTPDebug'];
	$mail->PluginDir = $emailcfg['PluginDir'];
	$mail->Sender = $emailcfg['Sender'];
	$mail->From = $emailcfg['From'];
	$mail->FromName = $emailcfg['FromName'];
	if ($emailcfg['Reply']) {
		$mail->AddReplyTo($emailcfg['Reply'], $emailcfg['ReplyName']);
	}
	$mail->CharSet = $l['a_meta_charset'];
tecnickcom's avatar
tecnickcom committed
118
	if (!$mail->CharSet) {
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
119
120
121
122
		$mail->CharSet = $emailcfg['CharSet'];
	}
	$mail->Subject = $l['t_result_user'];
	$mail->IsHTML(TRUE); // Set message type to HTML.
nick's avatar
nick committed
123

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
124
	$email_num = 0; // count emails;
Nicola Asuni's avatar
Nicola Asuni committed
125
126
	
	// get all data
tecnickcom's avatar
tecnickcom committed
127
	$data = F_getAllUsersTestStat($test_id, $group_id, $user_id, $startdate, $enddate, 'total_score', false, $display_mode);
Nicola Asuni's avatar
Nicola Asuni committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

	foreach ($data['testuser'] as $tu) {
		if (strlen($tu['user_email']) > 3) {

			// set HTML header
			$mail->Body = $emailcfg['MsgHeader'];
			// compose alternate TEXT message
			$mail->AltBody = ''.$l['t_result_user'].' ['.$tu['testuser_creation_time'].']'.K_NEWLINE;
			$mail->AltBody .= $l['w_test'].': '.$tu['test']['test_name'].K_NEWLINE;

			$passmsg = '';
			if ($tu['test']['test_score_threshold'] > 0) {
				$mail->AltBody .= $l['w_test_score_threshold'].': '.$tu['test']['test_score_threshold'];
				if ($tu['total_score'] >= $tu['test']['test_score_threshold']) {
					$passmsg = ' - '.$l['w_passed'];
				} else {
					$passmsg = ' - '.$l['w_not_passed'];
Nick's avatar
9.0.037    
Nick committed
145
				}
Nicola Asuni's avatar
Nicola Asuni committed
146
147
				$mail->AltBody .= K_NEWLINE;
			}
nick's avatar
nick committed
148

tecnickcom's avatar
tecnickcom committed
149
150
151
152
153
154
155
			$mail->AltBody .= $l['w_score'].': '.F_formatFloat($tu['total_score']).' '.F_formatPercentage($tu['total_score_perc'], false).$passmsg.K_NEWLINE;
			if ($display_mode > 0) {	
				$mail->AltBody .= $l['w_answers_right'].': '.$tu['right'].'&nbsp;'.F_formatPercentage($tu['right_perc'], false).K_NEWLINE;
				$mail->AltBody .= $l['w_answers_wrong'].': '.$tu['wrong'].'&nbsp;'.F_formatPercentage($tu['wrong_perc'], false).K_NEWLINE;
				$mail->AltBody .= $l['w_questions_unanswered'].': '.$tu['unanswered'].'&nbsp;'.F_formatPercentage($tu['unanswered_perc'], false).K_NEWLINE;
				$mail->AltBody .= $l['w_questions_undisplayed'].': '.$tu['undisplayed'].'&nbsp;'.F_formatPercentage($tu['undisplayed_perc'], false).K_NEWLINE;
			}
Nicola Asuni's avatar
Nicola Asuni committed
156
157

			if ($mode == 0) {
tecnickcom's avatar
tecnickcom committed
158
				$pdfkey = getPasswordHash(date('Y').$tu['id'].K_RANDOM_SECURITY.$tu['test']['test_id'].date('m').$tu['user_id']);
Nicola Asuni's avatar
Nicola Asuni committed
159
				// create PDF doc
tecnickcom's avatar
tecnickcom committed
160
				$mode = 3;
tecnickcom's avatar
tecnickcom committed
161
				$pdf_content = file_get_contents(K_PATH_HOST.K_PATH_TCEXAM.'admin/code/tce_pdf_results.php?mode='.$mode.'&diplay_mode='.$display_mode.'&show_graph='.$show_graph.'&test_id='.$tu['test']['test_id'].'&user_id='.$tu['user_id'].'&testuser_id='.$tu['id'].'&email='.$pdfkey);
tecnickcom's avatar
tecnickcom committed
162
163
164
165
166
167
168
169
170
171
172
				// set PDF document file name
				$doc_name = 'tcexam_report';
				$doc_name .= '_'.$mode;
				$doc_name .= '_0';
				$doc_name .= '_'.$tu['test']['test_id'];
				$doc_name .= '_0';
				$doc_name .= '_'.$tu['user_id'];
				$doc_name .= '_'.$tu['id'];
				$doc_name .= '.pdf';

				// attach document
Nicola Asuni's avatar
Nicola Asuni committed
173
174
175
				$mail->AddStringAttachment($pdf_content, $doc_name, $emailcfg['AttachmentsEncoding'], 'application/octet-stream');
				$mail->AltBody .= K_NEWLINE.$l['w_attachment'].': '.$doc_name.K_NEWLINE;
			}
nick's avatar
nick committed
176

Nicola Asuni's avatar
Nicola Asuni committed
177
178
			// convert alternate text to HTML
			$mail->Body .= str_replace(K_NEWLINE, '<br />'.K_NEWLINE, $mail->AltBody);
nick's avatar
nick committed
179

Nicola Asuni's avatar
Nicola Asuni committed
180
181
			// add HTML footer
			$mail->Body .= $emailcfg['MsgFooter'];
nick's avatar
nick committed
182

Nicola Asuni's avatar
Nicola Asuni committed
183
184
185
186
187
188
189
190
			//--- Elaborate user Templates ---
			$mail->Body = str_replace('#CHARSET#', $l['a_meta_charset'], $mail->Body);
			$mail->Body = str_replace('#LANG#', $l['a_meta_language'], $mail->Body);
			$mail->Body = str_replace('#LANGDIR#', $l['a_meta_dir'], $mail->Body);
			$mail->Body = str_replace('#EMAIL#', $tu['user_email'], $mail->Body);
			$mail->Body = str_replace('#USERNAME#', htmlspecialchars($tu['user_name'], ENT_NOQUOTES, $l['a_meta_charset']), $mail->Body);
			$mail->Body = str_replace('#USERFIRSTNAME#', htmlspecialchars($tu['user_firstname'], ENT_NOQUOTES, $l['a_meta_charset']), $mail->Body);
			$mail->Body = str_replace('#USERLASTNAME#', htmlspecialchars($tu['user_lastname'], ENT_NOQUOTES, $l['a_meta_charset']), $mail->Body);
nick's avatar
nick committed
191

Nicola Asuni's avatar
Nicola Asuni committed
192
193
			// add a "To" address
			$mail->AddAddress($tu['user_email'], $tu['user_name']);
nick's avatar
nick committed
194

Nicola Asuni's avatar
Nicola Asuni committed
195
196
			$email_num++;
			$progresslog = ''.$email_num.'. '.$tu['user_email'].' ['.$tu['user_name'].']'; //output user data
nick's avatar
nick committed
197

tecnickcom's avatar
tecnickcom committed
198
199
			if (!$mail->Send()) { //send email to user
				$progresslog .= ' ['.$l['t_error'].']'; //display error message
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
200
			}
Nicola Asuni's avatar
Nicola Asuni committed
201
202
203
204
205

			$mail->ClearAddresses(); // Clear all addresses for next loop
			$mail->ClearAttachments(); // Clears all previously set filesystem, string, and binary attachments
		} else {
			$progresslog = '['.$l['t_error'].'] '.$tu['user_name'].': '.$l['m_unknown_email'].''; //output user data
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
206
		}
Nicola Asuni's avatar
Nicola Asuni committed
207
208
		echo $progresslog.'<br />'.K_NEWLINE; //output processed emails
		flush(); // force browser output
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
209
	}
nick's avatar
nick committed
210

nicolaasuni's avatar
9.0.026    
nicolaasuni committed
211
212
213
214
215
216
217
218
219
 	$mail->ClearAddresses(); // Clear all addresses for next loop
	$mail->ClearCustomHeaders(); // Clears all custom headers
	$mail->ClearAllRecipients(); // Clears all recipients assigned in the TO, CC and BCC
 	$mail->ClearAttachments(); // Clears all previously set filesystem, string, and binary attachments
	$mail->ClearReplyTos(); // Clears all recipients assigned in the ReplyTo array
	return;
}

//============================================================+
nick's avatar
nick committed
220
// END OF FILE
nicolaasuni's avatar
9.0.026    
nicolaasuni committed
221
//============================================================+