source-function-F_xml_export_user_results

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Interfaces

Exceptions

Functions

  1: <?php
  2: //============================================================+
  3: // File name   : tce_xml_user_results.php
  4: // Begin       : 2008-12-26
  5: // Last Update : 2013-09-05
  6: //
  7: // Description : Export all user's results in XML or JSON format.
  8: //
  9: // Author: Nicola Asuni
 10: //
 11: // (c) Copyright:
 12: //               Nicola Asuni
 13: //               Tecnick.com LTD
 14: //               Manor Coach House, Church Hill
 15: //               Aldershot, Hants, GU12 4RQ
 16: //               UK
 17: //               www.tecnick.com
 18: //               info@tecnick.com
 19: //
 20: // License:
 21: //    Copyright (C) 2004-2013  Nicola Asuni - Tecnick.com LTD
 22: //    See LICENSE.TXT file for more information.
 23: //============================================================+
 24: 
 25: /**
 26:  * @file
 27:  * Export all user's results in XML or JSON format.
 28:  * @package com.tecnick.tcexam.admin
 29:  * @author Nicola Asuni
 30:  * @since 2008-12-26
 31:  * @param $_REQUEST['user_id'] (int) user ID
 32:  * @param $_REQUEST['startdate'] (int) start date
 33:  * @param $_REQUEST['enddate'] (int) end date
 34:  * @param $_REQUEST['orderfield'] (string) ORDER BY portion of SQL selection query
 35:  */
 36: 
 37: /**
 38:  */
 39: 
 40: require_once('../config/tce_config.php');
 41: require_once('../../shared/code/tce_authorization.php');
 42: require_once('../../shared/code/tce_functions_tcecode.php');
 43: require_once('../../shared/code/tce_functions_test.php');
 44: require_once('../../shared/code/tce_functions_test_stats.php');
 45: require_once('../code/tce_functions_statistics.php');
 46: require_once('tce_functions_user_select.php');
 47: 
 48: if (isset($_REQUEST['user_id']) and ($_REQUEST['user_id'] > 0)) {
 49:     $user_id = intval($_REQUEST['user_id']);
 50:     if (!F_isAuthorizedEditorForUser($user_id)) {
 51:         exit;
 52:     }
 53: } else {
 54:     exit;
 55: }
 56: if (isset($_REQUEST['startdate']) and ($_REQUEST['startdate'] > 0)) {
 57:     $startdate = urldecode($_REQUEST['startdate']);
 58:     $startdate_time = strtotime($startdate);
 59:     $startdate = date(K_TIMESTAMP_FORMAT, $startdate_time);
 60: } else {
 61:     $startdate = date('Y').'-01-01 00:00:00';
 62: }
 63: if (isset($_REQUEST['enddate']) and ($_REQUEST['enddate'] > 0)) {
 64:     $enddate = urldecode($_REQUEST['enddate']);
 65:     $enddate_time = strtotime($enddate);
 66:     $enddate = date(K_TIMESTAMP_FORMAT, $enddate_time);
 67: } else {
 68:     $enddate = date('Y').'-01-01 00:00:00';
 69: }
 70: if (isset($_REQUEST['order_field']) and !empty($_REQUEST['order_field']) and (in_array($_REQUEST['order_field'], array('testuser_creation_time', 'total_score')))) {
 71:     $order_field = $_REQUEST['order_field'];
 72: } else {
 73:     $order_field = 'testuser_creation_time';
 74: }
 75: 
 76: $output_format = isset($_REQUEST['format']) ? strtoupper($_REQUEST['format']) : 'XML';
 77: $out_filename = 'tcexam_user_results_'.$user_id.'_'.date('YmdHis');
 78: $xml = F_xml_export_user_results($user_id, $startdate, $enddate, $order_field);
 79: 
 80: switch ($output_format) {
 81:     case 'JSON': {
 82:         header('Content-Description: JSON File Transfer');
 83:         header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
 84:         header('Pragma: public');
 85:         header('Expires: Thu, 04 Jan 1973 00:00:00 GMT'); // Date in the past
 86:         header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
 87:         // force download dialog
 88:         header('Content-Type: application/force-download');
 89:         header('Content-Type: application/octet-stream', false);
 90:         header('Content-Type: application/download', false);
 91:         header('Content-Type: application/json', false);
 92:         // use the Content-Disposition header to supply a recommended filename
 93:         header('Content-Disposition: attachment; filename='.$out_filename.'.json;');
 94:         header('Content-Transfer-Encoding: binary');
 95:         $xmlobj = new SimpleXMLElement($xml);
 96:         echo json_encode($xmlobj);
 97:         break;
 98:     }
 99:     case 'XML':
100:     default: {
101:         header('Content-Description: XML File Transfer');
102:         header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
103:         header('Pragma: public');
104:         header('Expires: Thu, 04 Jan 1973 00:00:00 GMT'); // Date in the past
105:         header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
106:         // force download dialog
107:         header('Content-Type: application/force-download');
108:         header('Content-Type: application/octet-stream', false);
109:         header('Content-Type: application/download', false);
110:         header('Content-Type: application/xml', false);
111:         // use the Content-Disposition header to supply a recommended filename
112:         header('Content-Disposition: attachment; filename='.$out_filename.'.xml;');
113:         header('Content-Transfer-Encoding: binary');
114:         echo $xml;
115:         break;
116:     }
117: }
118: 
119: /**
120:  * Export user results in XML format.
121:  * @param $user_id (int) user ID - if greater than zero, filter stats for the specified user.
122:  * @param $startdate (string) start date ID - if greater than zero, filter stats for the specified starting date
123:  * @param $enddate (string) end date ID - if greater than zero, filter stats for the specified ending date
124:  * @param $order_field (string) Ordering fields for SQL query.
125:  * @author Nicola Asuni
126:  * @return XML data
127:  */
128: function F_xml_export_user_results($user_id, $startdate, $enddate, $order_field)
129: {
130:     global $l, $db;
131:     require_once('../config/tce_config.php');
132:     
133:     // define symbols for answers list
134:     $qtype = array('S', 'M', 'T', 'O'); // question types
135:     $type = array('single', 'multiple', 'text', 'ordering');
136:     $boolean = array('false', 'true');
137: 
138:     $xml = ''; // XML data to be returned
139: 
140:     $xml .= '<'.'?xml version="1.0" encoding="UTF-8" ?'.'>'.K_NEWLINE;
141:     $xml .= '<tcexamuserresults version="'.K_TCEXAM_VERSION.'">'.K_NEWLINE;
142:     $xml .=  K_TAB.'<header';
143:     $xml .= ' lang="'.K_USER_LANG.'"';
144:     $xml .= ' date="'.date(K_TIMESTAMP_FORMAT).'">'.K_NEWLINE;
145:     $xml .= K_TAB.K_TAB.'<user_id>'.$user_id.'</user_id>'.K_NEWLINE;
146:     $sql = 'SELECT user_name, user_lastname, user_firstname FROM '.K_TABLE_USERS.' WHERE user_id='.$user_id.'';
147:     if ($r = F_db_query($sql, $db)) {
148:         if ($m = F_db_fetch_array($r)) {
149:             $xml .= K_TAB.K_TAB.'<user_name>'.$m['user_name'].'</user_name>'.K_NEWLINE;
150:             $xml .= K_TAB.K_TAB.'<user_lastname>'.$m['user_lastname'].'</user_lastname>'.K_NEWLINE;
151:             $xml .= K_TAB.K_TAB.'<user_firstname>'.$m['user_firstname'].'</user_firstname>'.K_NEWLINE;
152:         }
153:     } else {
154:         F_display_db_error();
155:     }
156:     $xml .= K_TAB.K_TAB.'<date_from>'.$startdate.'</date_from>'.K_NEWLINE;
157:     $xml .= K_TAB.K_TAB.'<date_to>'.$enddate.'</date_to>'.K_NEWLINE;
158:     $xml .= K_TAB.'</header>'.K_NEWLINE;
159:     $xml .=  K_TAB.'<body>'.K_NEWLINE;
160: 
161:     $statsdata = array();
162:     $statsdata['score'] = array();
163:     $statsdata['right'] = array();
164:     $statsdata['wrong'] = array();
165:     $statsdata['unanswered'] = array();
166:     $statsdata['undisplayed'] = array();
167:     $statsdata['unrated'] = array();
168: 
169:     $sql = 'SELECT
170:             testuser_id,
171:             test_id,
172:             test_name,
173:             testuser_creation_time,
174:             testuser_status,
175:             SUM(testlog_score) AS total_score,
176:             MAX(testlog_change_time) AS testuser_end_time
177:         FROM '.K_TABLE_TESTS_LOGS.', '.K_TABLE_TEST_USER.', '.K_TABLE_TESTS.'
178:         WHERE testuser_status>0
179:             AND testuser_creation_time>=\''.F_escape_sql($db, $startdate).'\'
180:             AND testuser_creation_time<=\''.F_escape_sql($db, $enddate).'\'
181:             AND testuser_user_id='.$user_id.'
182:             AND testlog_testuser_id=testuser_id
183:             AND testuser_test_id=test_id';
184:     if ($_SESSION['session_user_level'] < K_AUTH_ADMINISTRATOR) {
185:         $sql .= ' AND test_user_id IN ('.F_getAuthorizedUsers($_SESSION['session_user_id']).')';
186:     }
187:     $sql .= ' GROUP BY testuser_id, test_id, test_name, testuser_creation_time, testuser_status ORDER BY '.F_escape_sql($db, $order_field).'';
188:     if ($r = F_db_query($sql, $db)) {
189:         $passed = 0;
190:         while ($m = F_db_fetch_array($r)) {
191:             $testuser_id = $m['testuser_id'];
192:             $usrtestdata = F_getUserTestStat($m['test_id'], $user_id);
193:             $halfscore = ($usrtestdata['max_score'] / 2);
194:             $xml .= K_TAB.K_TAB.'<test id=\''.$m['test_id'].'\'>'.K_NEWLINE;
195:             $xml .= K_TAB.K_TAB.K_TAB.'<start_time>'.$m['testuser_creation_time'].'</start_time>'.K_NEWLINE;
196:             $xml .= K_TAB.K_TAB.K_TAB.'<end_time>'.$m['testuser_end_time'].'</end_time>'.K_NEWLINE;
197:             $time_diff = strtotime($m['testuser_end_time']) - strtotime($m['testuser_creation_time']); //sec
198:             $time_diff = gmdate('H:i:s', $time_diff);
199:             $xml .= K_TAB.K_TAB.K_TAB.'<time>'.$time_diff.'</time>'.K_NEWLINE;
200:             $xml .= K_TAB.K_TAB.K_TAB.'<name>'.F_text_to_xml($m['test_name']).'</name>'.K_NEWLINE;
201:             if ($usrtestdata['score_threshold'] > 0) {
202:                 if ($usrtestdata['score'] >= $usrtestdata['score_threshold']) {
203:                     $xml .= K_TAB.K_TAB.K_TAB.'<passed>true</passed>'.K_NEWLINE;
204:                     $passed++;
205:                 } else {
206:                     $xml .= K_TAB.K_TAB.K_TAB.'<passed>false</passed>'.K_NEWLINE;
207:                 }
208:             } elseif ($usrtestdata['score'] > $halfscore) {
209:                 $passed++;
210:             }
211:             $xml .= K_TAB.K_TAB.K_TAB.'<score>'.round($m['total_score'], 3).'</score>'.K_NEWLINE;
212:             $xml .= K_TAB.K_TAB.K_TAB.'<score_percent>'.round(100 * $usrtestdata['score'] / $usrtestdata['max_score']).'</score_percent>'.K_NEWLINE;
213:             $xml .= K_TAB.K_TAB.K_TAB.'<right>'.$usrtestdata['right'].'</right>'.K_NEWLINE;
214:             $xml .= K_TAB.K_TAB.K_TAB.'<right_percent>'.round(100 * $usrtestdata['right'] / $usrtestdata['all']).'</right_percent>'.K_NEWLINE;
215:             $xml .= K_TAB.K_TAB.K_TAB.'<wrong>'.$usrtestdata['wrong'].'</wrong>'.K_NEWLINE;
216:             $xml .= K_TAB.K_TAB.K_TAB.'<wrong_percent>'.round(100 * $usrtestdata['wrong'] / $usrtestdata['all']).'</wrong_percent>'.K_NEWLINE;
217:             $xml .= K_TAB.K_TAB.K_TAB.'<unanswered>'.$usrtestdata['unanswered'].'</unanswered>'.K_NEWLINE;
218:             $xml .= K_TAB.K_TAB.K_TAB.'<unanswered_percent>'.round(100 * $usrtestdata['unanswered'] / $usrtestdata['all']).'</unanswered_percent>'.K_NEWLINE;
219:             $xml .= K_TAB.K_TAB.K_TAB.'<undisplayed>'.$usrtestdata['undisplayed'].'</undisplayed>'.K_NEWLINE;
220:             $xml .= K_TAB.K_TAB.K_TAB.'<undisplayed_percent>'.round(100 * $usrtestdata['undisplayed'] / $usrtestdata['all']).'</undisplayed_percent>'.K_NEWLINE;
221:             if ($m['testuser_status'] == 4) {
222:                 $status = $l['w_locked'];
223:             } else {
224:                 $status = $l['w_unlocked'];
225:             }
226:             $xml .= K_TAB.K_TAB.K_TAB.'<status>'.$status.'</status>'.K_NEWLINE;
227:             $xml .= K_TAB.K_TAB.K_TAB.'<comment>'.F_text_to_xml($usrtestdata['comment']).'</comment>'.K_NEWLINE;
228:             $xml .= K_TAB.K_TAB.'</test>'.K_NEWLINE;
229: 
230:             // collects data for descriptive statistics
231:             $statsdata['score'][] = $m['total_score'] / $usrtestdata['max_score'];
232:             $statsdata['right'][] = $usrtestdata['right'] / $usrtestdata['all'];
233:             $statsdata['wrong'][] = $usrtestdata['wrong'] / $usrtestdata['all'];
234:             $statsdata['unanswered'][] = $usrtestdata['unanswered'] / $usrtestdata['all'];
235:             $statsdata['undisplayed'][] = $usrtestdata['undisplayed'] / $usrtestdata['all'];
236:             $statsdata['unrated'][] = $usrtestdata['unrated'] / $usrtestdata['all'];
237:         }
238:     } else {
239:         F_display_db_error();
240:     }
241: 
242:     // calculate statistics
243:     $stats = F_getArrayStatistics($statsdata);
244:     $excludestat = array('sum', 'variance');
245:     $calcpercent = array('mean', 'median', 'mode', 'minimum', 'maximum', 'range', 'standard_deviation');
246: 
247:     $xml .= K_TAB.K_TAB.'<teststatistics>'.K_NEWLINE;
248:     $xml .= K_TAB.K_TAB.K_TAB.'<passed>'.$passed.'</passed>'.K_NEWLINE;
249:     $passed_perc = 0;
250:     if ($itemcount > 0) {
251:         $passed_perc = ($passed / $stats['number']['score']);
252:     }
253:     $xml .= K_TAB.K_TAB.K_TAB.'<passed_percent>'.round(100 * $passed_perc).'</passed_percent>'.K_NEWLINE;
254:     foreach ($stats as $row => $columns) {
255:         if (!in_array($row, $excludestat)) {
256:             $xml .= K_TAB.K_TAB.K_TAB.'<'.$row.'>'.K_NEWLINE;
257:             $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<score>'.round($columns['score'], 3).'</score>'.K_NEWLINE;
258:             $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<right>'.round($columns['right'], 3).'</right>'.K_NEWLINE;
259:             $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<wrong>'.round($columns['wrong'], 3).'</wrong>'.K_NEWLINE;
260:             $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<unanswered>'.round($columns['unanswered'], 3).'</unanswered>'.K_NEWLINE;
261:             $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<undisplayed>'.round($columns['undisplayed'], 3).'</undisplayed>'.K_NEWLINE;
262:             $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<unrated>'.round($columns['unrated'], 3).'</unrated>'.K_NEWLINE;
263:             if (in_array($row, $calcpercent)) {
264:                 $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<score_percent>'.round(100 * ($columns['score'] / $usrtestdata['max_score'])).'</score_percent>'.K_NEWLINE;
265:                 $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<right_percent>'.round(100 * ($columns['right'] / $usrtestdata['all'])).'</right_percent>'.K_NEWLINE;
266:                 $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<wrong_percent>'.round(100 * ($columns['wrong'] / $usrtestdata['all'])).'</wrong_percent>'.K_NEWLINE;
267:                 $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<unanswered_percent>'.round(100 * ($columns['unanswered'] / $usrtestdata['all'])).'</unanswered_percent>'.K_NEWLINE;
268:                 $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<undisplayed_percent>'.round(100 * ($columns['undisplayed'] / $usrtestdata['all'])).'</undisplayed_percent>'.K_NEWLINE;
269:                 $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<unrated_percent>'.round(100 * ($columns['unrated'] / $usrtestdata['all'])).'</unrated_percent>'.K_NEWLINE;
270:             }
271:             $xml .= K_TAB.K_TAB.K_TAB.'</'.$row.'>'.K_NEWLINE;
272:         }
273:     }
274:     $xml .= K_TAB.K_TAB.'</teststatistics>'.K_NEWLINE;
275: 
276:     $xml .= K_TAB.'</body>'.K_NEWLINE;
277:     $xml .= '</tcexamuserresults>'.K_NEWLINE;
278:     
279:     return $xml;
280: }
281: 
282: //============================================================+
283: // END OF FILE
284: //============================================================+
285: 
 

© 2004-2018 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy