mirror of
https://github.com/idanoo/nzart-exam-generator.git
synced 2024-12-04 14:23:10 +00:00
User login progress
This commit is contained in:
parent
4b01dc70ec
commit
0c6a5f3833
10
css/main.css
10
css/main.css
@ -24,6 +24,16 @@ html, body, #container {
|
||||
margin: 0 auto 0 auto;
|
||||
}
|
||||
|
||||
#user {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
right: 0;
|
||||
margin-right: 15px;
|
||||
margin-top: 10px;
|
||||
width: 250px;
|
||||
text-align:right;
|
||||
}
|
||||
|
||||
html {
|
||||
color: #222;
|
||||
font-size: 1em;
|
||||
|
@ -2,12 +2,6 @@
|
||||
|
||||
class DataItem {
|
||||
|
||||
protected $_db;
|
||||
|
||||
function __construct() {
|
||||
$this->_db = new db(); //Will optimise this to get existing conn at some point.
|
||||
}
|
||||
|
||||
public static function getById($id) {
|
||||
$db = new db();
|
||||
$db->query("SELECT * FROM `".static::_getType()."` WHERE ".static::_getType()."_id = :id");
|
||||
@ -22,15 +16,14 @@ class DataItem {
|
||||
return $obj;
|
||||
}
|
||||
|
||||
private function _getAllWhere($where = false, $orderBy = false, $join = false, $limit = false) {
|
||||
$Objarr = array();
|
||||
$typeName = static::_getType();
|
||||
$className = static::_getClass();
|
||||
$this->_db->query("SELECT * FROM `".$typeName."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy." ":" ").($limit?"LIMIT ".$limit:""));
|
||||
$results = $this->_db->resultset();
|
||||
public static function getAllWhere($where = false, $orderBy = false, $join = false, $limit = false) {
|
||||
$Objarr = [];
|
||||
$db = new db();
|
||||
$db->query("SELECT * FROM `".static::_getType()."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy." ":" ").($limit?"LIMIT ".$limit:""));
|
||||
$results = $db->resultset();
|
||||
if(!$results) return false;
|
||||
foreach ($results as $result) {
|
||||
$obj = new $className();
|
||||
$obj = new static();
|
||||
foreach ($result as $key=>$val) {
|
||||
$obj->$key = $val;
|
||||
}
|
||||
@ -39,30 +32,18 @@ class DataItem {
|
||||
return $Objarr;
|
||||
}
|
||||
|
||||
public static function getAllWhere($where = false, $orderBy = false, $join = false, $limit = false) {
|
||||
$me = new static();
|
||||
return $me->_getAllWhere($where, $orderBy, $join, $limit);
|
||||
}
|
||||
|
||||
private function _getWhere($where = false, $orderBy = false, $join = false, $limit = false) {
|
||||
$typeName = static::_getType();
|
||||
$className = static::_getClass();
|
||||
$this->_db = new db();
|
||||
$this->_db->query("SELECT * FROM `".$typeName."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy."":"").($limit?"LIMIT ".$limit:""));
|
||||
$result = $this->_db->single();
|
||||
public static function getWhere($where = false, $orderBy = false, $join = false, $limit = false) {
|
||||
$db = new db();
|
||||
$db->query("SELECT * FROM `".static::_getType()."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy."":"").($limit?"LIMIT ".$limit:""));
|
||||
$result = $db->single();
|
||||
if(!$result) return false;
|
||||
$obj = new $className();
|
||||
$obj = new static();
|
||||
foreach ($result as $key=>$val) {
|
||||
$obj->$key = $val;
|
||||
}
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public static function getWhere($where = false, $orderBy = false, $join = false, $limit = false) {
|
||||
$me = new static();
|
||||
return $me->_getWhere($where, $orderBy, $join, $limit);
|
||||
}
|
||||
|
||||
public function getId() {
|
||||
$typeName = static::_getType();
|
||||
$id = $typeName."_id";
|
||||
|
41
includes/classes/class.result.php
Normal file
41
includes/classes/class.result.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
class Result extends DataItem {
|
||||
|
||||
public static function _getClass()
|
||||
{
|
||||
return "Result";
|
||||
}
|
||||
|
||||
public static function _getType()
|
||||
{
|
||||
return "result";
|
||||
}
|
||||
|
||||
public function setResult($result)
|
||||
{
|
||||
$this->resultdata_result = json_encode($result);
|
||||
}
|
||||
|
||||
public function getResult()
|
||||
{
|
||||
return json_decode($this->resultdata_result, true);
|
||||
}
|
||||
|
||||
public function setUser($userId)
|
||||
{
|
||||
$this->resultdata_user = $userId;
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$db = new db();
|
||||
$db->query("INSERT INTO result(result_time, resultdata_user, resultdata_result)
|
||||
VALUES(:qTime, :qUser, :qContent)");
|
||||
$db->bind("qTime", time());
|
||||
$db->bind("qUser", $this->resultdata_user);
|
||||
$db->bind("qContent", $this->resultdata_result);
|
||||
return $db->execute();
|
||||
}
|
||||
|
||||
}
|
@ -14,25 +14,58 @@ class User extends DataItem {
|
||||
return "user";
|
||||
}
|
||||
|
||||
public static function loginOrRegister($data)
|
||||
{
|
||||
if(isset($data['register'])) {
|
||||
self::register($data['username'], $data['password']);
|
||||
} elseif(isset($data['login'])) {
|
||||
self::login($data['username'], $data['password']);
|
||||
}
|
||||
}
|
||||
|
||||
public static function register($userName, $password)
|
||||
{
|
||||
if(isset($userName) && isset($password)) {
|
||||
return self::_register($userName, $password);
|
||||
}
|
||||
return false; }
|
||||
|
||||
public static function login($userName = false, $password = false)
|
||||
{
|
||||
if(isset($_SESSION['userName']) && isset($_SESSION['userId'])) return true;
|
||||
|
||||
if(isset($userName) && isset($password)) {
|
||||
return self::_login($userName, $password, false);
|
||||
return self::_login($userName, $password);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static function _login($userName = false, $password = false, $token = false) {
|
||||
if($token && !$password) {
|
||||
//query token
|
||||
} else if ($userName && $password) {
|
||||
private static function _register($userName = false, $password = false)
|
||||
{
|
||||
if ($userName && $password) {
|
||||
$user = User::getWhere("userdata_username = '".$userName."'");
|
||||
if (!is_object($user)) {
|
||||
$hash = password_hash($password, PASSWORD_BCRYPT);
|
||||
$db = new db();
|
||||
$db->query("INSERT INTO user(user_time, userdata_username, userdata_password)
|
||||
VALUES(:qTime, :qUser, :qPassword)");
|
||||
$db->bind("qTime", time());
|
||||
$db->bind("qUser", $userName);
|
||||
$db->bind("qPassword", $hash);
|
||||
if($db->execute()) {
|
||||
$_SESSION['username'] = $userName;
|
||||
$_SESSION['userId'] = $db->lastInsertId();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static function _login($userName = false, $password = false)
|
||||
{
|
||||
if ($userName && $password) {
|
||||
$user = User::getWhere("userdata_username = '".$userName."'");
|
||||
if (is_object($user)) {
|
||||
if (password_verify($password, $user->_getHash())) {
|
||||
setcookie("userName", $user->getUserName(), COOKIE_EXPIRY);
|
||||
$_SESSION['username'] = $user->getUserName();
|
||||
$_SESSION['userId'] = $user->getId();
|
||||
return true;
|
||||
@ -42,17 +75,19 @@ class User extends DataItem {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function logout()
|
||||
{
|
||||
session_destroy();
|
||||
header("Location: //".$_SERVER['HTTP_HOST']);
|
||||
exit();
|
||||
}
|
||||
|
||||
public static function getUserFromSession()
|
||||
{
|
||||
return self::getWhere("user_id = '".$_SESSION['userId']."'");
|
||||
}
|
||||
|
||||
public static function register()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function _getHash()
|
||||
protected function _getHash()
|
||||
{
|
||||
return $this->userdata_password;
|
||||
}
|
||||
@ -61,4 +96,12 @@ class User extends DataItem {
|
||||
{
|
||||
return $this->userdata_username;
|
||||
}
|
||||
|
||||
public function storeuser($dataArray)
|
||||
{
|
||||
$user = new user();
|
||||
$user->setuser($dataArray);
|
||||
$user->setUser($this->getId());
|
||||
$user->save();
|
||||
}
|
||||
}
|
@ -1,8 +1,11 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
require_once('config.php'); //DB CREDENTIALS
|
||||
|
||||
require_once('classes/class.db.php');
|
||||
require_once('classes/class.dataitem.php');
|
||||
require_once('classes/class.user.php');
|
||||
require_once('classes/class.question.php');
|
||||
require_once('classes/class.answer.php');
|
||||
require_once('classes/class.answer.php');
|
||||
require_once('classes/class.result.php');
|
35
index.php
35
index.php
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
require_once('includes/include.php');
|
||||
if(isset($_REQUEST['login']) || isset($_REQUEST['register'])) {
|
||||
User::loginOrRegister($_REQUEST);
|
||||
}
|
||||
if(isset($_REQUEST['logout'])) User::logout();
|
||||
$loggedIn = $user = false;
|
||||
if(isset($_SESSION['userId'])) {
|
||||
$user = User::getById($_SESSION['userId']);
|
||||
}
|
||||
|
||||
$questions = [];
|
||||
if(!isset($_POST['mark'])) {
|
||||
if(isset($_GET['questions'])) {
|
||||
@ -26,6 +35,10 @@ if(!isset($_POST['mark'])) {
|
||||
$wrong++;
|
||||
}
|
||||
}
|
||||
if(is_object($user)) {
|
||||
$user->storeResult($_POST);
|
||||
}
|
||||
|
||||
}
|
||||
?><!doctype html>
|
||||
<html class="no-js" lang="">
|
||||
@ -40,9 +53,26 @@ if(!isset($_POST['mark'])) {
|
||||
|
||||
<link rel="stylesheet" href="css/normalize.css">
|
||||
<link rel="stylesheet" href="css/main.css">
|
||||
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
|
||||
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.12.0.min.js"><\/script>')</script>
|
||||
<script src="js/plugins.js"></script>
|
||||
<script src="js/main.js"></script>
|
||||
</head>
|
||||
<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>";
|
||||
} else {
|
||||
echo "<div id='loginTrigger' onclick='showLoginBox()'>Login or Register</div>";
|
||||
} ?></div>
|
||||
<div id="cover" style="display:none;"></div>
|
||||
<div id="login" style="display:none;">
|
||||
<form method="post">
|
||||
<label>Username<input type="text" name="username"><br/></label>
|
||||
<label>Password<input type="text" name="password"><br/></label>
|
||||
<button type="submit" class="loginbutton" value="login">Login</button>
|
||||
</form>
|
||||
</div>
|
||||
<div id="header"><h1>Unofficial NZART Practice Exam</h1></div>
|
||||
<div id="body" class="center">
|
||||
New Exam: <a href="/index.php?questions=10">10 Questions</a> -
|
||||
@ -93,10 +123,5 @@ if(!isset($_POST['mark'])) {
|
||||
Last updated 31-12-2016.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
|
||||
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.12.0.min.js"><\/script>')</script>
|
||||
<script src="js/plugins.js"></script>
|
||||
<script src="js/main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -0,0 +1,4 @@
|
||||
function showLoginBox()
|
||||
{
|
||||
alert("login");
|
||||
}
|
Loading…
Reference in New Issue
Block a user