From b8794f6dc5db3fc524dde119c50bbbfc94de2065 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Mon, 2 Jan 2017 11:12:27 +1300 Subject: [PATCH] Added result tracking --- css/main.css | 27 ++++++++- includes/classes/class.dataitem.php | 2 +- includes/classes/class.result.php | 20 ++++++- includes/classes/class.user.php | 20 ++++--- includes/createDb.sql | 10 ++++ includes/fullDb.sql | 29 ++++++++- index.php | 91 +++++++++++++++++++---------- js/main.js | 15 +++-- 8 files changed, 167 insertions(+), 47 deletions(-) mode change 100755 => 100644 includes/fullDb.sql diff --git a/css/main.css b/css/main.css index 024e91f..6d774b8 100755 --- a/css/main.css +++ b/css/main.css @@ -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; diff --git a/includes/classes/class.dataitem.php b/includes/classes/class.dataitem.php index e120989..3e5beb9 100755 --- a/includes/classes/class.dataitem.php +++ b/includes/classes/class.dataitem.php @@ -65,7 +65,7 @@ class DataItem { } public function getTime() { - $timeColumn = static::_getCreationTime(); + $timeColumn = self::_getTime(); return $this->$timeColumn; } } \ No newline at end of file diff --git a/includes/classes/class.result.php b/includes/classes/class.result.php index 46a0a7a..2b6a755 100644 --- a/includes/classes/class.result.php +++ b/includes/classes/class.result.php @@ -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(); } diff --git a/includes/classes/class.user.php b/includes/classes/class.user.php index 5bf1f91..9ef4191 100755 --- a/includes/classes/class.user.php +++ b/includes/classes/class.user.php @@ -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"); } } \ No newline at end of file diff --git a/includes/createDb.sql b/includes/createDb.sql index 7c16e3f..ba88ccb 100755 --- a/includes/createDb.sql +++ b/includes/createDb.sql @@ -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; \ No newline at end of file diff --git a/includes/fullDb.sql b/includes/fullDb.sql old mode 100755 new mode 100644 index 4df8a6e..b5097b6 --- a/includes/fullDb.sql +++ b/includes/fullDb.sql @@ -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 diff --git a/index.php b/index.php index e8456c4..ee1acf6 100755 --- a/index.php +++ b/index.php @@ -1,6 +1,6 @@ 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 .= "".$question->getQuestion()."
"; + $corAnswer = $question->getCorrectAnswer(); + $output .= "Your Answer: ".$answer->getAnswer()."
Correct Answer: ".$corAnswer->getAnswer().'

'; + $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 .= "".$question->getQuestion()."
"; - $corAnswer = $question->getCorrectAnswer(); - $output .= "Your Answer: ".$answer->getAnswer()."
Correct Answer: ".$corAnswer->getAnswer().'

'; - $wrong++; - } - } - if(is_object($user)) { - $user->storeResult($_POST); - } - } ?> @@ -61,16 +73,24 @@ if(!isset($_POST['mark'])) {
Logout"; + echo "Result History. Welcome Back ".$_SESSION['username'].".
Logout"; } else { - echo "
Login or Register
"; + echo "
Login or Register
"; } ?>
@@ -80,12 +100,21 @@ if(!isset($_POST['mark'])) { 60 Questions (Full Exam) - 600 Questions (All Questions)

- Score ".(($correct/$total)*100)."% (".$correct."/".$total.")"; + echo "

Score ".(($score['correct']/$score['total'])*100)."% (".$score['correct']."/".$score['total'].")

"; echo $output; - } else { ?> - Questions

+ } 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']."). ". + "View Result
"; + } + } else { + if(!is_object($user)) { ?> + Please Login to track results.
+ + Questions

diff --git a/js/main.js b/js/main.js index ef7f732..2bcb9ee 100755 --- a/js/main.js +++ b/js/main.js @@ -1,4 +1,11 @@ -function showLoginBox() -{ - alert("login"); -} \ No newline at end of file +$(document).ready(function(){ + $('#cover').on("click", function(){ + $(this).hide(); + $("#login").hide(); + }); + + $("#loginTrigger").on("click", function(){ + $("#cover").show(); + $("#login").show(); + }); +}); \ No newline at end of file