sQuiz/engine/engine.php
2019-08-08 16:43:21 +03:00

101 lines
3.6 KiB
PHP

<?php
/**
* /engine/engine.php
* @version 1.2
* @desc PHP server side engine to serve commonly accessable tests, collect submitted results
* @author Fándly Gergő Zoltán (gergo@systemtest.tk, systemtest.tk)
* @copy 2017 Fándly Gergő Zoltán
* License:
sQuiz for creating small jQuery based quizs in an implementable way
Copyright (C) 2017 Fándly Gergő Zoltán
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
//config
$sql_user="squiz";
$sql_passwd="squizuserforgood";
$sql_db="squiz";
$quiz_tag=0;
$db=new PDO("mysql:host=localhost;dbname=".$sql_db.";charset=utf8", $sql_user, $sql_passwd);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_GET['list'])){
if($quiz_tag!=0){
$sql=$db->prepare("SELECT id, name, description FROM tests WHERE allowed=1 and tag=:tag");
$sql->execute(array(":tag"=>$quiz_tag));
}
else{
$sql=$db->prepare("SELECT id, name, description FROM tests WHERE allowed=1");
$sql->execute();
}
$obj=array();
while($row=$sql->fetch(PDO::FETCH_ASSOC)){
array_push($obj, array("name"=>$row['name'], "description"=>$row['description'], "file"=>"//".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?getQuiz=".$row['id']));
}
header("Content-type: text/json; charset=UTF-8");
echo json_encode($obj);
die();
}
if(isset($_GET['getQuiz'])){
$sql=$db->prepare("SELECT quiz FROM tests WHERE id=:id");
$sql->execute(array(":id"=>$_GET['getQuiz']));
$res=$sql->fetch(PDO::FETCH_ASSOC);
header("Content-type: text/json; charset=UTF-8");
echo $res['quiz'];
die();
}
if(isset($_POST['submitResult']) && isset($_POST['submitResult_submitter'])){
$sql=$db->prepare("INSERT INTO results (submitter, answered) VALUES (:submitter, :answered)");
$sql->execute(array(":submitter"=>$_POST['submitResult_submitter'], ":answered"=>$_POST['submitResult']));
$id=$db->lastInsertId();
echo $id;
die();
}
if(isset($_GET['getResult'])){
$sql=$db->prepare("SELECT COUNT(id) AS count, submitter, time, answered FROM results WHERE id=:id");
$sql->execute(array(":id"=>$_GET['getResult']));
$res=$sql->fetch(PDO::FETCH_ASSOC);
if($res['count']<1){
echo "not found";
}
else{
$obj=array("submitter"=>$res['submitter'], "time"=>$res['time'], "answered"=>$res['answered']);
header("Content-type: text/json; charset=UTF-8");
echo json_encode($obj);
die();
}
}
if(isset($_POST['submitQuiz']) && isset($_POST['submitQuiz_tag']) && isset($_POST['submitQuiz_name']) && isset($_POST['submitQuiz_description'])){
$sql=$db->prepare("INSERT INTO tests (name, description, quiz, tag) VALUES (:name, :desc, :quiz, :tag)");
$sql->execute(array(":name"=>$_POST['submitQuiz_name'], ":desc"=>$_POST['submitQuiz_description'], ":quiz"=>$_POST['submitQuiz'], ":tag"=>$_POST['submitQuiz_tag']));
$id=$db->lastInsertId();
$link="https://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?getQuiz=".$id;
echo $link;
die();
}