Added result tracking

This commit is contained in:
Daniel Mason 2017-01-02 11:12:27 +13:00
parent 0c6a5f3833
commit b8794f6dc5
8 changed files with 167 additions and 47 deletions

View File

@ -30,10 +30,35 @@ html, body, #container {
right: 0;
margin-right: 15px;
margin-top: 10px;
width: 250px;
width: 300px;
text-align:right;
}
#cover {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: #222222;
z-index: 100;
opacity: 0.9;
}
#login {
position: absolute;
top: 50%;
left: 50%;
-moz-border-radius: 15px;
border-radius: 15px;
height: 180px;
width: 250px;
background-color: #EEEEEE;
padding: 20px;
margin: -150px 0 0 -125px;
z-index: 101;
}
html {
color: #222;
font-size: 1em;

View File

@ -65,7 +65,7 @@ class DataItem {
}
public function getTime() {
$timeColumn = static::_getCreationTime();
$timeColumn = self::_getTime();
return $this->$timeColumn;
}
}

View File

@ -22,18 +22,34 @@ class Result extends DataItem {
return json_decode($this->resultdata_result, true);
}
public function setScore($score)
{
$this->resultdata_score = json_encode($score);
}
public function getScore()
{
return json_decode($this->resultdata_score, true);
}
public function setUser($userId)
{
$this->resultdata_user = $userId;
}
public function getUser()
{
return $this->resultdata_user;
}
public function save()
{
$db = new db();
$db->query("INSERT INTO result(result_time, resultdata_user, resultdata_result)
VALUES(:qTime, :qUser, :qContent)");
$db->query("INSERT INTO result(result_time, resultdata_user, resultdata_result, resultdata_score)
VALUES(:qTime, :qUser, :qContent, :qScore)");
$db->bind("qTime", time());
$db->bind("qUser", $this->resultdata_user);
$db->bind("qScore", $this->resultdata_score);
$db->bind("qContent", $this->resultdata_result);
return $db->execute();
}

View File

@ -16,9 +16,9 @@ class User extends DataItem {
public static function loginOrRegister($data)
{
if(isset($data['register'])) {
if($data['method'] == "register") {
self::register($data['username'], $data['password']);
} elseif(isset($data['login'])) {
} elseif ($data['method'] == "login") {
self::login($data['username'], $data['password']);
}
}
@ -97,11 +97,17 @@ class User extends DataItem {
return $this->userdata_username;
}
public function storeuser($dataArray)
public function storeResult($dataArray, $score)
{
$user = new user();
$user->setuser($dataArray);
$user->setUser($this->getId());
$user->save();
$result = new Result();
$result->setResult($dataArray);
$result->setUser($this->getId());
$result->setScore($score);
$result->save();
}
public function getResults()
{
return Result::getAllWhere("resultdata_user = ".$this->getId(), "ORDER BY result_time DESC");
}
}

View File

@ -27,4 +27,14 @@ CREATE TABLE IF NOT EXISTS `answer` (
`answerdata_correct` INT(1) DEFAULT 0,
PRIMARY KEY (`answer_id`),
UNIQUE KEY `answer_id_UNIQUE` (`answer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `result` (
`result_id` INT(11) NOT NULL AUTO_INCREMENT,
`result_time` INT(11) NOT NULL,
`resultdata_user` INT(11) NOT NULL,
`resultdata_result` MEDIUMTEXT DEFAULT NULL,
`resultdata_score` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (`result_id`),
UNIQUE KEY `result_id_UNIQUE` (`result_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

29
includes/fullDb.sql Executable file → Normal file
View File

@ -71,6 +71,33 @@ INSERT INTO `question` VALUES (1,1483169094,'1.1','The Amateur Service may be br
/*!40000 ALTER TABLE `question` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `result`
--
DROP TABLE IF EXISTS `result`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `result` (
`result_id` int(11) NOT NULL AUTO_INCREMENT,
`result_time` int(11) NOT NULL,
`resultdata_user` int(11) NOT NULL,
`resultdata_result` mediumtext,
`resultdata_score` varchar(255) DEFAULT NULL,
PRIMARY KEY (`result_id`),
UNIQUE KEY `result_id_UNIQUE` (`result_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `result`
--
LOCK TABLES `result` WRITE;
/*!40000 ALTER TABLE `result` DISABLE KEYS */;
/*!40000 ALTER TABLE `result` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `user`
--
@ -107,4 +134,4 @@ UNLOCK TABLES;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2016-12-31 20:28:19
-- Dump completed on 2017-01-02 10:44:10

View File

@ -1,6 +1,6 @@
<?php
require_once('includes/include.php');
if(isset($_REQUEST['login']) || isset($_REQUEST['register'])) {
if(isset($_REQUEST['method'])) {
User::loginOrRegister($_REQUEST);
}
if(isset($_REQUEST['logout'])) User::logout();
@ -10,35 +10,47 @@ if(isset($_SESSION['userId'])) {
}
$questions = [];
if(!isset($_POST['mark'])) {
if (isset($_REQUEST['viewresult']) && is_object($user)) {
$result = Result::getById($_REQUEST['viewresult']);
if(!is_object($result) || $result->getUser() != $user->getId()) {
header("Location: //".$_SERVER['HTTP_HOST']);
exit();
}
$res = $result->getResult();
foreach($res as $p=>$r) {
$_POST[$p] = $r;
}
$_POST['mark'] = 1;
} elseif (isset($_REQUEST['results']) && is_object($user)) {
$results = $user->getResults();
}
if (isset($_POST['mark'])) {
unset($_POST['mark']);
$score['total'] = $score['correct'] = $score['wrong'] = 0;
foreach($_POST as $i=>$q) {
$question = Question::getById($i);
$answer = Answer::getById($q);
if(!is_object($question) || !is_object($answer)) continue;
if($answer->isCorrect()) {
$score['correct']++;
} else {
$output .= "<span style='font-weight:bold'>".$question->getQuestion()."</span><br>";
$corAnswer = $question->getCorrectAnswer();
$output .= "Your Answer: ".$answer->getAnswer()."<br>Correct Answer: ".$corAnswer->getAnswer().'<br><br>';
$score['wrong']++;
}
$score['total']++;
}
if(is_object($user)) {
$user->storeResult($_POST, $score);
}
} else {
if(isset($_GET['questions'])) {
define('QUESTION_COUNT', intval($_GET['questions']));
} else {
define('QUESTION_COUNT', 60);
}
$questions = Question::getQuestions(QUESTION_COUNT);
$total = false;
} else {
unset($_POST['mark']);
$total = count($_POST);
$correct = $wrong = 0;
$output = "";
foreach($_POST as $i=>$q) {
$question = Question::getById($i);
$answer = Answer::getById($q);
if($answer->isCorrect()) {
$correct++;
} else {
$output .= "<span style='font-weight:bold'>".$question->getQuestion()."</span><br>";
$corAnswer = $question->getCorrectAnswer();
$output .= "Your Answer: ".$answer->getAnswer()."<br>Correct Answer: ".$corAnswer->getAnswer().'<br><br>';
$wrong++;
}
}
if(is_object($user)) {
$user->storeResult($_POST);
}
}
?><!doctype html>
<html class="no-js" lang="">
@ -61,16 +73,24 @@ if(!isset($_POST['mark'])) {
<body>
<div id="container">
<div id="user"><?php if(is_object($user)) {
echo "Welcome Back ".$_SESSION['username'].". <a href='index.php/logout=1'>Logout</a>";
echo "<a href='index.php?results=1'>Result History</a>. Welcome Back ".$_SESSION['username'].".<br><a href='index.php?logout=1'>Logout</a>";
} else {
echo "<div id='loginTrigger' onclick='showLoginBox()'>Login or Register</div>";
echo "<div id='loginTrigger'>Login or Register</div>";
} ?></div>
<div id="cover" style="display:none;"></div>
<div id="login" style="display:none;">
<h2 style="margin:0 0 5px 0;padding:0;">Login/Register</h2>
<form method="post">
<label>Username<input type="text" name="username"><br/></label>
<label>Password<input type="text" name="password"><br/></label>
<label>Password<input type="password" name="password"><br/></label><br/>
<input type="hidden" name="method" value="register">
<button type="submit" class="loginbutton" value="login">Login</button>
<button type="submit" class="loginbutton" value="register">Register</button>
<script type="text/javascript">
$(".loginbutton").on("click", function(){
$('input[name=method]').attr("value",$(this).attr("value"));
});
</script>
</form>
</div>
<div id="header"><h1>Unofficial NZART Practice Exam</h1></div>
@ -80,12 +100,21 @@ if(!isset($_POST['mark'])) {
<a href="/index.php?questions=60">60 Questions (Full Exam)</a> -
<a href="/index.php?questions=600">600 Questions (All Questions)</a>
<br/><br/>
<?php if($total) {
<?php if(isset($score)) {
?><?php
echo "<h3>Score ".(($correct/$total)*100)."% (".$correct."/".$total.")</h3>";
echo "<h3>Score ".(($score['correct']/$score['total'])*100)."% (".$score['correct']."/".$score['total'].")</h3>";
echo $output;
} else { ?>
<?=QUESTION_COUNT?> Questions<br><br>
} elseif (isset($results)) {
foreach ($results as $result) {
$score = $result->getScore();
echo date("Y-m-d", $result->getTime())." Score ".(($score['correct']/$score['total'])*100)."% (".$score['correct']."/".$score['total']."). ".
"<a href='index.php?viewresult=".$result->getId()."'>View Result</a><br/>";
}
} else {
if(!is_object($user)) { ?>
Please Login to track results.<br>
<?php } ?><span style="font-weight:bold">
<?=QUESTION_COUNT?> Questions</span><br><br>
<form action="/" method="POST">
<table>
<tbody>

View File

@ -1,4 +1,11 @@
function showLoginBox()
{
alert("login");
}
$(document).ready(function(){
$('#cover').on("click", function(){
$(this).hide();
$("#login").hide();
});
$("#loginTrigger").on("click", function(){
$("#cover").show();
$("#login").show();
});
});