PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/complete/questions.php

https://bitbucket.org/hv15/crushquiz
PHP | 167 lines | 141 code | 8 blank | 18 comment | 10 complexity | 68b300ef831dc007848f5ca65c295048 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. // Fusiontables Details
  3. $tableid = 3185297;
  4. $username = "crushquiz";
  5. $password = "heriotwatt";
  6. //include the files from the PHP FT client library
  7. require('lib/fusion-tables-client-php/clientlogin.php');
  8. require('lib/fusion-tables-client-php/sql.php');
  9. // Log into fusiontables, get client object
  10. $ftclient = new FTClientLogin(ClientLogin::getAuthToken($username, $password));
  11. function getQuestionCount() {
  12. global $ftclient, $tableid;
  13. // Returns number of highest QUI in fusiontables
  14. return substr(trim($ftclient->query("SELECT QID FROM $tableid ORDER BY QID DESC LIMIT 1")),3);
  15. }
  16. function getCurrentQuestion() {
  17. if(@simplexml_load_file("data/currentQuestion.xml")==FALSE) {
  18. setCurrentQuestion(1);
  19. }
  20. $questionArray = (array) simplexml_load_file("data/currentQuestion.xml");
  21. return $questionArray;
  22. }
  23. function setRandQuestion() {
  24. // Advances to a new random question, out of all available questions in database (regardless of category etc)
  25. setCurrentQuestion(rand(0,getQuestionCount()));
  26. }
  27. function setCurrentQuestion($qid) {
  28. if(file_exists("data/setQlock")) return;
  29. touch("data/setQlock");
  30. // Sanitize requested question ID
  31. if($qid>=getQuestionCount()) $qid=1;
  32. //echo "SET CURRENT QUESTION: $qid<br /><br />";
  33. global $ftclient, $tableid;
  34. // Get specific QID question data
  35. $selectquery = SQLBuilder::select($tableid,null, "QID = '$qid'");
  36. //echo $selectquery;
  37. $table_data = $ftclient->selectQueryMultiAssoc($selectquery);
  38. //echo "<textarea cols=80 rows=20>".print_r($table_data,1)."</textarea>";
  39. // Get contents of first row of data since we only requested one row from the fusiontable anyway
  40. $questionData = $table_data[0];
  41. // Add start time to question data to enable checking expiry time
  42. $questionData["startTime"]=time();
  43. $questionData["endTime"]=time()+$questionData["time"];
  44. // Form XML to store
  45. $questionXML = new SimpleXMLElement('<currentQuestion/>');
  46. // Function call to convert array to xml
  47. array_to_xml($questionData,$questionXML);
  48. // Pretty-print XML output
  49. $questionDOM = dom_import_simplexml($questionXML)->ownerDocument;
  50. $questionDOM->formatOutput = true;
  51. // Move old currentQuestion
  52. unlink("/home/macsquiz/public_html/data/previousQuestion.xml");
  53. rename("/home/macsquiz/public_html/data/currentQuestion.xml","/home/macsquiz/public_html/data/previousQuestion.xml");
  54. // Clear out old responses for this question
  55. $responsesArray = (array) simplexml_load_file("data/responses.xml");
  56. foreach($responsesArray as $QID=>$QIDusers) {
  57. $responsesArray[$QID]=(array)$QIDusers;
  58. foreach($QIDusers as $username=>$QIDuserArray) {
  59. $responsesArray[$QID][$username]=(array)$QIDuserArray;
  60. }
  61. }
  62. if(isset($responsesArray["QID$qid"])) {
  63. unset($responsesArray["QID$qid"]);
  64. }
  65. $responsesXML = new SimpleXMLElement('<responses/>');
  66. array_to_xml($responsesArray,$responsesXML);
  67. $responsesDOM = dom_import_simplexml($responsesXML)->ownerDocument;
  68. $responsesDOM->formatOutput = true;
  69. file_put_contents("data/responses.xml",$responsesDOM->saveXML());
  70. // Write formatted XML to file
  71. file_put_contents("data/currentQuestion.xml",$questionDOM->saveXML());
  72. unlink("data/setQlock");
  73. }
  74. function array_to_xml($data, &$xml_data) {
  75. foreach($data as $key => $value) {
  76. if(is_array($value)) {
  77. if(!is_numeric($key)){
  78. $subnode = $xml_data->addChild("$key");
  79. array_to_xml($value, $subnode);
  80. } else{
  81. array_to_xml($value, $xml_data);
  82. }
  83. } else {
  84. $xml_data->addChild("$key","$value");
  85. }
  86. }
  87. }
  88. if($_POST['action']=='currentQuestion') {
  89. $q = getCurrentQuestion();
  90. if($q['endTime']<=time()) {
  91. setCurrentQuestion($q['QID']+1);
  92. $q = getCurrentQuestion();
  93. }
  94. $timeleft=$q['endTime']-time();
  95. echo <<<EOF
  96. <div class="well">
  97. <h2>Q{$q["QID"]}: {$q['question']}</h2>
  98. <p class="size20">Time Remaining: {$timeleft}</p>
  99. </div>
  100. <div class="well">
  101. <h2 class="btn btn-info h2button">A</h2> <h2 class="question">{$q['answerA']}</h2> <br />
  102. <h2 class="btn btn-success h2button">B</h2> <h2 class="question">{$q['answerB']}</h2> <br />
  103. <h2 class="btn btn-warning h2button">C</h2> <h2 class="question">{$q['answerC']}</h2> <br />
  104. <h2 class="btn btn-danger h2button">D</h2> <h2 class="question">{$q['answerD']}</h2> <br />
  105. </div>
  106. EOF;
  107. return;
  108. }
  109. ?>
  110. <!DOCTYPE html>
  111. <html>
  112. <head>
  113. <title>MACS Quiz</title>
  114. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  115. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  116. <link href="/css/bootstrap.min.css" rel="stylesheet">
  117. <link href="/css/bootstrap-responsive.min.css" rel="stylesheet">
  118. <link href="/css/custom.css" rel="stylesheet">
  119. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  120. </head>
  121. <body>
  122. <div class="container-fluid">
  123. <div class="row-fluid">
  124. <div class="span3">
  125. <!--Sidebar content-->
  126. <div class="well pagination-centered">
  127. <img src="/img/macsquiz.png" alt="logo" /><br />
  128. <br />
  129. <a href='http://m.macsquiz.co.uk' onClick='window.open($(this).attr("href"), "MACS Quiz Mobile", "width=320,height=500,scrollbars=no"); return false;'>
  130. <img src='https://chart.googleapis.com/chart?chs=300x300&amp;cht=qr&amp;chl=http://m.macsquiz.co.uk&amp;chld=Q|0' alt='QR code'/>
  131. </a>
  132. <h1>Scan QR to play!</h1>
  133. </div>
  134. </div>
  135. <div class="span9">
  136. <!--Body content-->
  137. <div class="well">
  138. <h1 class="bottom20">Welcome to the non-stop quiz for MACS students and staff.</h1>
  139. <p class="size20">To play, scan the QR code on the left with your mobile phone.</p>
  140. <p class="size20">The current question is always displayed below. Good luck!</p>
  141. </div>
  142. <div id='currentQuestion'></div>
  143. </div>
  144. </div>
  145. </div>
  146. <script type='text/javascript'>
  147. $(function() {
  148. $.post('questions.php', { action: 'currentQuestion' }, function(data) {
  149. $('#currentQuestion').html(data);
  150. });
  151. var refreshId = setInterval(function() {
  152. $.post('questions.php', { action: 'currentQuestion' }, function(data) {
  153. $('#currentQuestion').html(data);
  154. });
  155. }, 1000);
  156. });
  157. </script>
  158. </body>
  159. </html>