source-function-F_xml_export_question_stats

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_question_stats.php
  4: // Begin       : 2010-05-10
  5: // Last Update : 2013-09-05
  6: //
  7: // Description : Functions to export question stats using XML
  8: //               or JSON format.
  9: //
 10: // Author: Nicola Asuni
 11: //
 12: // (c) Copyright:
 13: //               Nicola Asuni
 14: //               Tecnick.com LTD
 15: //               Manor Coach House, Church Hill
 16: //               Aldershot, Hants, GU12 4RQ
 17: //               UK
 18: //               www.tecnick.com
 19: //               info@tecnick.com
 20: //
 21: // License:
 22: //    Copyright (C) 2004-2013  Nicola Asuni - Tecnick.com LTD
 23: //    See LICENSE.TXT file for more information.
 24: //============================================================+
 25: 
 26: /**
 27:  * @file
 28:  * Export question stats in XML or JSON format.
 29:  * @package com.tecnick.tcexam.admin
 30:  * @author Nicola Asuni
 31:  * @since 2010-05-10
 32:  */
 33: 
 34: /**
 35:  */
 36: 
 37:  // check user's authorization
 38: require_once('../config/tce_config.php');
 39: $pagelevel = K_AUTH_ADMIN_RESULTS;
 40: require_once('../../shared/code/tce_authorization.php');
 41: require_once('../../shared/code/tce_functions_test.php');
 42: require_once('../../shared/code/tce_functions_test_stats.php');
 43: require_once('../code/tce_functions_statistics.php');
 44: require_once('../code/tce_functions_auth_sql.php');
 45: 
 46: if (isset($_REQUEST['testid']) and ($_REQUEST['testid'] > 0)) {
 47:     $test_id = intval($_REQUEST['testid']);
 48:     // check user's authorization
 49:     if (!F_isAuthorizedUser(K_TABLE_TESTS, 'test_id', $test_id, 'test_user_id')) {
 50:         F_print_error('ERROR', $l['m_authorization_denied']);
 51:         exit;
 52:     }
 53: 
 54:     $output_format = isset($_REQUEST['format']) ? strtoupper($_REQUEST['format']) : 'XML';
 55:     $out_filename = 'tcexam_questions_'.$test_id.'_'.date('YmdHis');
 56:     $xml = F_xml_export_question_stats($test_id);
 57: 
 58:     switch ($output_format) {
 59:         case 'JSON': {
 60:             header('Content-Description: JSON File Transfer');
 61:             header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
 62:             header('Pragma: public');
 63:             header('Expires: Thu, 04 Jan 1973 00:00:00 GMT'); // Date in the past
 64:             header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
 65:             // force download dialog
 66:             header('Content-Type: application/force-download');
 67:             header('Content-Type: application/octet-stream', false);
 68:             header('Content-Type: application/download', false);
 69:             header('Content-Type: application/json', false);
 70:             // use the Content-Disposition header to supply a recommended filename
 71:             header('Content-Disposition: attachment; filename='.$out_filename.'.json;');
 72:             header('Content-Transfer-Encoding: binary');
 73:             $xmlobj = new SimpleXMLElement($xml);
 74:             echo json_encode($xmlobj);
 75:             break;
 76:         }
 77:         case 'XML':
 78:         default: {
 79:             header('Content-Description: XML File Transfer');
 80:             header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
 81:             header('Pragma: public');
 82:             header('Expires: Thu, 04 Jan 1973 00:00:00 GMT'); // Date in the past
 83:             header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
 84:             // force download dialog
 85:             header('Content-Type: application/force-download');
 86:             header('Content-Type: application/octet-stream', false);
 87:             header('Content-Type: application/download', false);
 88:             header('Content-Type: application/xml', false);
 89:             // use the Content-Disposition header to supply a recommended filename
 90:             header('Content-Disposition: attachment; filename='.$out_filename.'.xml;');
 91:             header('Content-Transfer-Encoding: binary');
 92:             echo $xml;
 93:             break;
 94:         }
 95:     }
 96: } else {
 97:     exit;
 98: }
 99: 
100: /**
101:  * Export all question statistics of the selected test to XML.
102:  * @author Nicola Asuni
103:  * @since 2010-05-10
104:  * @param $test_id (int) test ID
105:  * @return XML data
106:  */
107: function F_xml_export_question_stats($test_id)
108: {
109:     global $l, $db;
110:     require_once('../config/tce_config.php');
111:     require_once('../../shared/code/tce_authorization.php');
112:     require_once('../code/tce_functions_auth_sql.php');
113: 
114:     $boolean = array('false', 'true');
115:     $type = array('single', 'multiple', 'text', 'ordering');
116: 
117:     $xml = ''; // XML data to be returned
118: 
119:     $xml .= '<'.'?xml version="1.0" encoding="UTF-8" ?'.'>'.K_NEWLINE;
120:     $xml .= '<tcexamquestionstats version="'.K_TCEXAM_VERSION.'">'.K_NEWLINE;
121:     $xml .=  K_TAB.'<header';
122:     $xml .= ' lang="'.K_USER_LANG.'"';
123:     $xml .= ' date="'.date(K_TIMESTAMP_FORMAT).'">'.K_NEWLINE;
124:     $xml .= K_TAB.'</header>'.K_NEWLINE;
125:     $xml .=  K_TAB.'<body>'.K_NEWLINE;
126: 
127:     // get test data
128:     $testdata = F_getTestData($test_id);
129: 
130:     // get total number of questions for the selected test
131:     $num_questions = F_count_rows(K_TABLE_TESTS_LOGS.', '.K_TABLE_TEST_USER, 'WHERE testlog_testuser_id=testuser_id AND testuser_test_id='.$test_id.'');
132: 
133:     // output questions stats
134:     $sqlr = 'SELECT
135:             question_id,
136:             COUNT(question_id) AS recurrence,
137:             AVG(testlog_score) AS average_score,
138:             AVG(testlog_change_time - testlog_display_time) AS average_time,
139:             min(question_difficulty) AS question_difficulty
140:         FROM '.K_TABLE_TESTS_LOGS.', '.K_TABLE_TEST_USER.', '.K_TABLE_QUESTIONS.'
141:         WHERE testlog_testuser_id=testuser_id
142:             AND testlog_question_id=question_id
143:             AND testuser_test_id='.$test_id.'
144:         GROUP BY question_id
145:         ORDER BY recurrence DESC,average_score DESC';
146:     if ($rr = F_db_query($sqlr, $db)) {
147:         while ($mr = F_db_fetch_array($rr)) {
148:             $xml .= K_TAB.K_TAB.'<question>'.K_NEWLINE;
149: 
150:             // get the question max score
151:             $question_max_score = $testdata['test_score_right'] * $mr['question_difficulty'];
152:             $qsttestdata = F_getQuestionTestStat($test_id, $mr['question_id']);
153: 
154:             $xml .= K_TAB.K_TAB.K_TAB.'<id>'.$mr['question_id'].'</id>'.K_NEWLINE;
155:             $question_description = '';
156:             $sqlrq = 'SELECT question_description FROM '.K_TABLE_QUESTIONS.' WHERE question_id='.$mr['question_id'].'';
157:             if ($rrq = F_db_query($sqlrq, $db)) {
158:                 if ($mrq = F_db_fetch_array($rrq)) {
159:                     $question_description = $mrq['question_description'];
160:                 }
161:             } else {
162:                 F_display_db_error();
163:             }
164:             $xml .= K_TAB.K_TAB.K_TAB.'<description>'.F_text_to_xml($question_description).'</description>'.K_NEWLINE;
165:             $xml .= K_TAB.K_TAB.K_TAB.'<recurrence>'.$mr['recurrence'].'</recurrence>'.K_NEWLINE;
166:             $xml .= K_TAB.K_TAB.K_TAB.'<recurrence_percent>'.F_formatXMLPercentage($mr['recurrence'] / $num_questions).'</recurrence_percent>'.K_NEWLINE;
167:             $xml .= K_TAB.K_TAB.K_TAB.'<points>'.number_format($mr['average_score'], 3, '.', '').'</points>'.K_NEWLINE;
168:             $xml .= K_TAB.K_TAB.K_TAB.'<points_percent>'.F_formatXMLPercentage($mr['average_score'] / $question_max_score).'</points_percent>'.K_NEWLINE;
169:             if (stripos($mr['average_time'], ':') !== false) {
170:                 // PostgreSQL returns formatted time, while MySQL returns the number of seconds
171:                 $mr['average_time'] = strtotime($mr['average_time']);
172:             }
173:             $xml .= K_TAB.K_TAB.K_TAB.'<time>'.date('i:s', $mr['average_time']).'</time>'.K_NEWLINE;
174:             $xml .= K_TAB.K_TAB.K_TAB.'<correct>'.$qsttestdata['right'].'</correct>'.K_NEWLINE;
175:             $xml .= K_TAB.K_TAB.K_TAB.'<correct_percent>'.F_formatXMLPercentage($qsttestdata['right'] / $qsttestdata['num']).'</correct_percent>'.K_NEWLINE;
176:             $xml .= K_TAB.K_TAB.K_TAB.'<wrong>'.$qsttestdata['wrong'].'</wrong>'.K_NEWLINE;
177:             $xml .= K_TAB.K_TAB.K_TAB.'<wrong_percent>'.F_formatXMLPercentage($qsttestdata['wrong'] / $qsttestdata['num']).'</wrong_percent>'.K_NEWLINE;
178:             $xml .= K_TAB.K_TAB.K_TAB.'<unanswered>'.$qsttestdata['unanswered'].'</unanswered>'.K_NEWLINE;
179:             $xml .= K_TAB.K_TAB.K_TAB.'<unanswered_percent>'.F_formatXMLPercentage($qsttestdata['unanswered'] / $qsttestdata['num']).'</unanswered_percent>'.K_NEWLINE;
180:             $xml .= K_TAB.K_TAB.K_TAB.'<undisplayed>'.$qsttestdata['undisplayed'].'</undisplayed>'.K_NEWLINE;
181:             $xml .= K_TAB.K_TAB.K_TAB.'<undisplayed_percent>'.F_formatXMLPercentage($qsttestdata['undisplayed'] / $qsttestdata['num']).'</undisplayed_percent>'.K_NEWLINE;
182:             $xml .= K_TAB.K_TAB.K_TAB.'<unrated>'.$qsttestdata['unrated'].'</unrated>'.K_NEWLINE;
183:             $xml .= K_TAB.K_TAB.K_TAB.'<unrated_percent>'.F_formatXMLPercentage($qsttestdata['unrated'] / $qsttestdata['num']).'</unrated_percent>'.K_NEWLINE;
184: 
185:             // answers statistics
186: 
187:             $sqla = 'SELECT *
188:                 FROM '.K_TABLE_ANSWERS.'
189:                 WHERE answer_question_id='.$mr['question_id'].'
190:                 ORDER BY answer_id';
191:             if ($ra = F_db_query($sqla, $db)) {
192:                 while ($ma = F_db_fetch_array($ra)) {
193:                     $xml .= K_TAB.K_TAB.K_TAB.'<answer>'.K_NEWLINE;
194:                     $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<id>'.$ma['answer_id'].'</id>'.K_NEWLINE;
195:                     $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<description>'.F_text_to_xml($ma['answer_description']).'</description>'.K_NEWLINE;
196: 
197:                     $num_all_answers = F_count_rows(K_TABLE_TEST_USER.', '.K_TABLE_TESTS_LOGS.', '.K_TABLE_ANSWERS.', '.K_TABLE_LOG_ANSWER.' WHERE logansw_answer_id=answer_id AND logansw_testlog_id=testlog_id AND testlog_testuser_id=testuser_id AND testuser_test_id='.$test_id.' AND testlog_question_id='.$mr['question_id'].'');
198:                     $num_answers = F_count_rows(K_TABLE_TEST_USER.', '.K_TABLE_TESTS_LOGS.', '.K_TABLE_ANSWERS.', '.K_TABLE_LOG_ANSWER.' WHERE answer_id='.$ma['answer_id'].' AND logansw_answer_id=answer_id AND logansw_testlog_id=testlog_id AND testlog_testuser_id=testuser_id AND testuser_test_id='.$test_id.' AND testlog_question_id='.$mr['question_id'].'');
199:                     $right_answers = F_count_rows(K_TABLE_TEST_USER.', '.K_TABLE_TESTS_LOGS.', '.K_TABLE_ANSWERS.', '.K_TABLE_LOG_ANSWER.' WHERE answer_id='.$ma['answer_id'].' AND logansw_answer_id=answer_id AND logansw_testlog_id=testlog_id AND testlog_testuser_id=testuser_id AND testuser_test_id='.$test_id.' AND testlog_question_id='.$mr['question_id'].' AND ((answer_isright=\'0\' AND logansw_selected=0) OR (answer_isright=\'1\' AND logansw_selected=1) OR (answer_position IS NOT NULL AND logansw_position IS NOT NULL AND answer_position=logansw_position))');
200:                     $wrong_answers = F_count_rows(K_TABLE_TEST_USER.', '.K_TABLE_TESTS_LOGS.', '.K_TABLE_ANSWERS.', '.K_TABLE_LOG_ANSWER.' WHERE answer_id='.$ma['answer_id'].' AND logansw_answer_id=answer_id AND logansw_testlog_id=testlog_id AND testlog_testuser_id=testuser_id AND testuser_test_id='.$test_id.' AND testlog_question_id='.$mr['question_id'].' AND ((answer_isright=\'0\' AND logansw_selected=1) OR (answer_isright=\'1\' AND logansw_selected=0) OR (answer_position IS NOT NULL AND answer_position!=logansw_position))');
201:                     $unanswered = F_count_rows(K_TABLE_TEST_USER.', '.K_TABLE_TESTS_LOGS.', '.K_TABLE_ANSWERS.', '.K_TABLE_LOG_ANSWER.' WHERE answer_id='.$ma['answer_id'].' AND logansw_answer_id=answer_id AND logansw_testlog_id=testlog_id AND testlog_testuser_id=testuser_id AND testuser_test_id='.$test_id.' AND testlog_question_id='.$mr['question_id'].' AND logansw_selected=-1');
202: 
203:                     $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<recurrence>'.$num_answers.'</recurrence>'.K_NEWLINE;
204:                     $perc = 0;
205:                     if ($num_all_answers > 0) {
206:                         $perc = ($num_answers / $num_all_answers);
207:                     }
208:                     $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<recurrence_percent>'.F_formatXMLPercentage($perc).'</recurrence_percent>'.K_NEWLINE;
209:                     $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<correct>'.$right_answers.'</correct>'.K_NEWLINE;
210:                     $perc = 0;
211:                     if ($num_answers > 0) {
212:                         $perc = ($right_answers / $num_answers);
213:                     }
214:                     $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<correct_percent>'.F_formatXMLPercentage($perc).'</correct_percent>'.K_NEWLINE;
215:                     $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<wrong>'.$wrong_answers.'</wrong>'.K_NEWLINE;
216:                     $perc = 0;
217:                     if ($num_answers > 0) {
218:                         $perc = round($wrong_answers / $num_answers);
219:                     }
220:                     $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<wrong_percent>'.F_formatXMLPercentage($perc).'</wrong_percent>'.K_NEWLINE;
221:                     $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<unanswered>'.$unanswered.'</unanswered>'.K_NEWLINE;
222:                     $perc = 0;
223:                     if ($num_answers > 0) {
224:                         $perc = round($unanswered / $num_answers);
225:                     }
226:                     $xml .= K_TAB.K_TAB.K_TAB.K_TAB.'<unanswered_percent>'.F_formatXMLPercentage($perc).'</unanswered_percent>'.K_NEWLINE;
227:                     $xml .= K_TAB.K_TAB.K_TAB.'</answer>'.K_NEWLINE;
228:                 }
229:             } else {
230:                 F_display_db_error();
231:             }
232:             $xml .= K_TAB.K_TAB.'</question>'.K_NEWLINE;
233:         }
234:     } else {
235:         F_display_db_error();
236:     }
237: 
238:     $xml .= K_TAB.'</body>'.K_NEWLINE;
239:     $xml .= '</tcexamquestionstats>'.K_NEWLINE;
240: 
241:     return $xml;
242: }
243: 
244: //============================================================+
245: // END OF FILE
246: //============================================================+
247: 
 

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