Initial Commit

This commit is contained in:
Daniel Mason 2016-12-30 18:16:13 +13:00
commit 391177596e
41 changed files with 1233 additions and 0 deletions

View file

@ -0,0 +1,31 @@
<?php
/**
* Created by PhpStorm.
* User: daniel
* Date: 12/30/16
* Time: 2:09 PM
*/
class Answer extends DataItem
{
public static function _getClass()
{
return "Answer";
}
public static function _getType()
{
return "answer";
}
public function getAnswer()
{
return $this->answerdata_content;
}
public function isCorrect()
{
return $this->answerdata_correct;
}
}

View file

@ -0,0 +1,82 @@
<?php
class DataItem {
protected $_db;
function __construct() {
$this->_db = new db(); //Will optimise this to get existing conn at some point.
}
public function getById($id) {
$this->_db->query("SELECT * FROM `".static::_getType()."` WHERE ".static::_getType()."_id = :id");
$this->_db->bind(":id", $id);
return $this->_db->getObject();
}
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();
if(!$results) return false;
foreach ($results as $result) {
$obj = new $className();
foreach ($result as $key=>$val) {
$obj->$key = $val;
}
$Objarr[] = $obj;
}
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();
if(!$result) return false;
$obj = new $className();
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";
return $this->$id;
}
public static function create($PDOobj)
{
$className = static::_getClass();
$obj = new $className();
foreach ($PDOobj as $key=>$val) {
$obj->$key = $val;
}
return $obj;
}
protected function _getTime() {
return static::_getType()."_time";
}
public function getTime() {
$timeColumn = static::_getCreationTime();
return $this->$timeColumn;
}
}

96
includes/classes/class.db.php Executable file
View file

@ -0,0 +1,96 @@
<?php
class db {
private $host = DB_HOSTNAME;
private $user = DB_USERNAME;
private $pass = DB_PASSWORD;
private $dbname = DB_DATABASE;
private $stmt;
private $dbh;
public function __construct(){
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
} catch (PDOException $e) {
error_log($e->getMessage());
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultSet(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function getObject() {
$this->execute();
return $this->stmt->fetchObject();
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->dbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
public function kill(){
$this->dbh = null;
}
}

View file

@ -0,0 +1,40 @@
<?php
/**
* Created by PhpStorm.
* User: daniel
* Date: 12/30/16
* Time: 2:09 PM
*/
class Question extends DataItem {
public static function _getClass()
{
return "Question";
}
public static function _getType()
{
return "question";
}
public static function getQuestions($count = false)
{
$questions = static::getAllWhere(false, false, false, $count);
foreach ($questions as $q) {
$q->answers = $q->getAnswers();
shuffle($q->answers);
}
return $questions;
}
public function getQuestion()
{
return $this->questiondata_content;
}
public function getAnswers()
{
return Answer::getAllWhere("answerdata_question = ".$this->getId());
}
}

64
includes/classes/class.user.php Executable file
View file

@ -0,0 +1,64 @@
<?php
class User extends DataItem {
public $user_id;
public $userdata_username;
public $userdata_password;
public static function _getClass() {
return "User";
}
public static function _getType() {
return "user";
}
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 false;
}
private static function _login($userName = false, $password = false, $token = false) {
if($token && !$password) {
//query token
} else 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;
}
}
}
return false;
}
public static function getUserFromSession()
{
return self::getWhere("user_id = '".$_SESSION['userId']."'");
}
public static function register()
{
}
public function _getHash()
{
return $this->userdata_password;
}
public function getUserName()
{
return $this->userdata_username;
}
}