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