Skip to content

Commit

Permalink
Variables Change
Browse files Browse the repository at this point in the history
  • Loading branch information
sciuridae0603 committed Jul 30, 2016
1 parent 8f11cb0 commit f8be5ee
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 61 deletions.
52 changes: 33 additions & 19 deletions answer.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
//POST DATA
$tguser = (INT)$_POST['user'];
$token = $_POST['token'];
$qid = (INT)$_POST['question_id'];
$correct = (INT)$_POST['correct'];
$tguser = (INT)$_POST['user'];//Telegram User ID
$token = $_POST['token'];
$QuestionID = (INT)$_POST['question_id'];
$correct = (INT)$_POST['correct'];

//Variables
$config = include('config.php');
Expand All @@ -12,60 +12,74 @@
$db_name = $config['db_name'];
$db_user = $config['db_user'];
$db_password = $config['db_password'];
$a = "1";
$PDONumber = "1";

//Check POST DATA

if ($QuestionID = null or $correct = null or $token = null or $tguser =null){
echo("Not correct POST Data")
die;
}

//Check token
if ($token != $hosttoken ){
echo("Auth Fail");
die("Auth Fail");
die;
}

//Database
//Database Connection
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name.';charset=utf8', $db_user, $db_password);

$qalist = $db->prepare("SELECT user, list, now, next FROM qalist WHERE user = :user");
//Read qalist table to get user's correct question
$qalist = $db->prepare("SELECT user, list, now, next FROM qalist WHERE user = :user");
$qalist->bindParam(':user',$tguser, PDO::PARAM_INT);
$qalist->execute();
$obj = $qalist->fetchObject();
$userquestion = $obj->now - 1 ;
$qa = JSON_Decode($obj->list);
$qnumber = $qa[$userquestion] + 0;

$Object = $qalist->fetchObject(); //get data
$UserCorrectQuestionNumber = $Object->now - 1 ;
$QuestionQueue = JSON_Decode($Object->list);
$qnumber = $QuestionQueue[$UserCorrectQuestionNumber] + 0;

//if no more question
if ($userquestion == $config[questions]){
die("No_more_question");
}
if ($qnumber != $qid){

//if database question id != user question id
if ($qnumber != $QuestionID){
die("question_id_not_correct");
}

if ($correct == 0){
$updateuser = $db->prepare("UPDATE user SET wronganswer = wronganswer + :1 WHERE user=:user");
$updateuser->bindParam(':1',$a, PDO::PARAM_INT);
$updateuser->bindParam(':1',$PDONumber, PDO::PARAM_INT);
$updateuser->bindParam(':user',$tguser, PDO::PARAM_INT);
$updateuser->execute();

$updatenow = $db->prepare("UPDATE qalist SET now = now + :1 WHERE user=:user");
$updatenow->bindParam(':1',$a, PDO::PARAM_INT);
$updatenow->bindParam(':1',$PDONumber, PDO::PARAM_INT);
$updatenow->bindParam(':user',$tguser, PDO::PARAM_INT);
$updatenow->execute();

$updatenext = $db->prepare("UPDATE qalist SET next = next + :1 WHERE user=:user");
$updatenext->bindParam(':1',$a, PDO::PARAM_INT);
$updatenext->bindParam(':1',$PDONumber, PDO::PARAM_INT);
$updatenext->bindParam(':user',$tguser, PDO::PARAM_INT);
$updatenext->execute();
}

if ($correct == 1){
$updateuser = $db->prepare("UPDATE user SET rightanswer = rightanswer + :1 WHERE user=:user");
$updateuser->bindParam(':1',$a, PDO::PARAM_INT);
$updateuser->bindParam(':1',$PDONumber, PDO::PARAM_INT);
$updateuser->bindParam(':user',$tguser, PDO::PARAM_INT);
$updateuser->execute();

$updatenow = $db->prepare("UPDATE qalist SET now = now + :1 WHERE user=:user");
$updatenow->bindParam(':1',$a, PDO::PARAM_INT);
$updatenow->bindParam(':1',$PDONumber, PDO::PARAM_INT);
$updatenow->bindParam(':user',$tguser, PDO::PARAM_INT);
$updatenow->execute();

$updatenext = $db->prepare("UPDATE qalist SET next = next + :1 WHERE user=:user");
$updatenext->bindParam(':1',$a, PDO::PARAM_INT);
$updatenext->bindParam(':1',$PDONumber, PDO::PARAM_INT);
$updatenext->bindParam(':user',$tguser, PDO::PARAM_INT);
$updatenext->execute();
}
Expand Down
1 change: 0 additions & 1 deletion db.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ function init() {
}

return init();

6 changes: 4 additions & 2 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//Database
$db = $config['db'];

//add table if not exist
//add table user for recode user's info (id,rightanswe,wronganswer)
$userTableCreate = "CREATE TABLE IF NOT EXISTS `user` (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user INT NOT NULL,
Expand All @@ -16,6 +16,7 @@

$db->exec($userTableCreate);

//create question for record data
$questionTableCreate = "CREATE TABLE IF NOT EXISTS `question` (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
question TEXT NOT NULL,
Expand All @@ -25,6 +26,7 @@

$db->exec($questionTableCreate);

//create qalist for recode user question queue list
$qalist = "CREATE TABLE IF NOT EXISTS `qalist` (
user INT NOT NULL,
list TEXT NOT NULL,
Expand All @@ -33,7 +35,7 @@
)";
$db->exec($qalist);

//setup uuid
//setup uuid for get data
$setupuuid="ALTER TABLE user ADD UNIQUE (user);
ALTER TABLE question ADD UNIQUE (id);
ALTER TABLE qalist ADD UNIQUE (user);";
Expand Down
53 changes: 29 additions & 24 deletions question.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
<?php
//POST DATA
$tguser = (INT)$_POST['user'];
$token = $_POST['token'];
$tguser = (INT)$_POST['user']; //Telegram User ID
$token = $_POST['token'];

//Variables
$config = include('config.php');
$hosttoken = $config['token'];
$db_host = $config['db_host'];
$db_name = $config['db_name'];
$db_user = $config['db_user'];
$db_password = $config['db_password'];
$config = include('config.php');
$hosttoken = $config['token'];
$db_host = $config['db_host'];
$db_name = $config['db_name'];
$db_user = $config['db_user'];
$db_password = $config['db_password'];

if ($token = null or $tguser =null){
echo("Not correct POST Data")
die;
}

//Check Token
if (!$token = $hosttoken){
echo("Auth Fail");
die("Auth Fail");
}

//Database
//Database Connection
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name.';charset=utf8', $db_user, $db_password);

//get data
$qalist = $db->prepare("SELECT user, list, now, next FROM qalist WHERE user = :user");
$qalist->bindParam(':user',$tguser, PDO::PARAM_INT);
$qalist->execute();
$obj = $qalist->fetchObject();
//get user question queue data
$QuesrionQueueList = $db->prepare("SELECT user, list, now, next FROM qalist WHERE user = :user");
$QuesrionQueueList->bindParam(':user',$tguser, PDO::PARAM_INT);
$QuesrionQueueList->execute();
$Object = $QuesrionQueueList->fetchObject();

$userquestion = $obj->now;
$qa = JSON_Decode($obj->list);
$qnumber = $qa[$userquestion] + 0;
$UserQuestionID = $Object->now;
$UserQuestionQueue = JSON_Decode($Object->list);
$QuestionID = $UserQuestionQueue[$UserQuestionID] + 0;

$qalist1 = $db->prepare("SELECT id, question, author, answers FROM question WHERE id = :id");
$qalist1->bindParam(':id',$qnumber, PDO::PARAM_INT);
$qalist1->execute();
$obj2 = $qalist1->fetchObject();
$tempArr = JSON_Decode($obj2->answers);
$obj2->answers = $tempArr;
echo JSON_Encode($obj2);
$QuestionList = $db->prepare("SELECT id, question, author, answers FROM question WHERE id = :id");
$QuestionList->bindParam(':id',$QuestionID, PDO::PARAM_INT);
$QuestionList->execute();
$Object2 = $QuestionList->fetchObject();
$TempArray = JSON_Decode($Object2->answers);
$Object2->answers = $TempArray;
echo JSON_Encode($Object2);
?>
22 changes: 14 additions & 8 deletions start.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@
}

//POST DATA
$tguser = intval($_POST["user"]);
$tguser = intval($_POST["user"]); //Telegram User ID
$token = $_POST["token"];

//Variables
$config = require_once __DIR__ . '/db.php';
$hosttoken = $config['token'];
$question = $config['questions'];
$a = '0' + '0';
$b = '1' + '0';
$c = '1' + '1';
$config = require_once __DIR__ . '/db.php';
$hosttoken = $config['token'];
$question = $config['questions'];
$PDONumber0 = (INT)"0";
$PDONumber1 = (INT)"1";
$PDONumber2 = (INT)"2";

//database
$db = $config['db'];

//Check POST Data
if ($token = null or $tguser =null){
echo("Not correct POST Data")
die;
}

//check token
if (!$token = $hosttoken) {
echo("Auth Fail");
Expand All @@ -39,9 +45,9 @@
shuffle($qaarray);
$encodearray=JSON_Encode($qaarray);
$userqalist = $db->prepare("INSERT IGNORE INTO `qalist` (user,list,now,next) VALUES (:user,:array,:c,:n)");

$userqalist->bindParam(':user', $tguser, PDO::PARAM_INT);
$userqalist->bindParam(':array', $encodearray, PDO::PARAM_STR);
$userqalist->bindParam(':c',$b, PDO::PARAM_INT);
$userqalist->bindParam(':n',$c, PDO::PARAM_INT);
$userqalist->execute();
?>
21 changes: 14 additions & 7 deletions user_info.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
//POST DATA
$token = $_POST['token'];
$tguser = (INT)$_POST['user'];
$tguser = (INT)$_POST['user']; //Telegram User ID

//Variables
$config = include('config.php');
Expand All @@ -14,16 +14,23 @@
//Database
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name.';charset=utf8', $db_user, $db_password);

//Check POST Data
if ($token = null or $tguser =null){
echo("Not correct POST Data")
die;
}

//check token
if($token != $hosttoken){
echo("Auth Fail");
die("Auth Fail");
die;
}

$userinfo = $db->prepare("SELECT rightanswer, wronganswer FROM user WHERE user = :user");
$userinfo->bindParam(':user',$tguser, PDO::PARAM_INT);
$userinfo->execute();
$obj2 = $userinfo->fetchObject();
echo JSON_Encode($obj2);
//Get User info
$UserInfo = $db->prepare("SELECT rightanswer, wronganswer FROM user WHERE user = :user");
$UserInfo->bindParam(':user',$tguser, PDO::PARAM_INT);
$UserInfo->execute();
$Object = $UserInfo->fetchObject();
echo JSON_Encode($Object);

?>

0 comments on commit f8be5ee

Please sign in to comment.