Add: writing exam - save and update record (incompleted)

This commit is contained in:
2019-07-15 09:50:08 +09:00
parent 5d1090e652
commit cfb843f91c
7 changed files with 447 additions and 10 deletions
+52
View File
@@ -0,0 +1,52 @@
<?php
header('Content-Type: application/json');
// get parameters from client
$command = $_POST["command"];
include "./../lib/json_builder.php";
include "./../lib/connect_db.php";
$jsonBuilder = new JsonBuilder();
$dbConnector = new DBConnector();
$isConnected = $dbConnector->connectDB();
if (!$isConnected) {
$jsonBuilder->setErrorMessage("DB connection : failed");
$jsonBuilder->sendResultFail();
exit;
}
const MAESTRO_ID = 3; // 삼화초
const PLAYER_ID = 35; // 박지상
if ($command === "deleteWritingRecordAll") {
include "./db_method_container.php";
include "./typing_exam_collection.php";
$typingExam = new TypingExamCollection($dbConnector->getMysqli());
$typingExam->deleteTypingExamRecordAll(MAESTRO_ID, PLAYER_ID);
$typingExam->deleteTypingExamHighestRecordAll(MAESTRO_ID, PLAYER_ID);
$jsonBuilder->setData("deleteWritingRecordAll", "succeeded");
} elseif ($command === "deleteTypingExamRecordAll") {
include "./db_method_container.php";
include "./typing_exam_collection.php";
$typingExam = new TypingExamCollection($dbConnector->getMysqli());
$typingExam->deleteTypingExamRecordAll(MAESTRO_ID, PLAYER_ID);
$jsonBuilder->setData("deleteTypingExamRecordAll", "succeeded");
} elseif ($command === "deleteTypingExamHighestRecordAll") {
include "./db_method_container.php";
include "./typing_exam_collection.php";
$typingExam = new TypingExamCollection($dbConnector->getMysqli());
$typingExam->deleteTypingExamHighestRecordAll(MAESTRO_ID, PLAYER_ID);
$jsonBuilder->setData("deleteTypingExamHighestRecordAll", "succeeded");
}
$jsonBuilder->sendResultSuccess();
?>
+133
View File
@@ -0,0 +1,133 @@
<?php
class TypingExamCollection extends DBMethodContainer
{
// begin - for TDD
public function deleteTypingExamRecordAll($maestroID, $playerID) {
$query = "
DELETE
FROM typing_exam_record
WHERE MaestroID = ? AND PlayerID = ?";
$stmt = $this->mysqli->prepare($query);
$stmt->bind_param('ii', $maestroID, $playerID);
$stmt->execute();
$stmt->fetch();
$stmt->close();
}
public function deleteTypingExamHighestRecordAll($maestroID, $playerID) {
$query = "
DELETE
FROM typing_exam_highest_record
WHERE MaestroID = ? AND PlayerID = ?";
$stmt = $this->mysqli->prepare($query);
$stmt->bind_param('ii', $maestroID, $playerID);
$stmt->execute();
$stmt->fetch();
$stmt->close();
}
public function addPrevHourRecord($maestroID, $playerID, $writingID, $record)
{
$query = "
INSERT INTO typing_exam_record (MaestroID, PlayerID, WritingID, Record, RecordDateTime)
VALUES (?, ?, ?, ?, DATE_SUB(NOW(), INTERVAL 1 HOUR))";
$stmt = $this->mysqli->prepare($query);
$stmt->bind_param("iiid", $maestroID, $playerID, $writingID, $record);
$stmt->execute();
$stmt->close();
}
// end - for TDD
public function addRecord($maestroID, $playerID, $writingID, $record)
{
$query = "
INSERT INTO typing_exam_record (MaestroID, PlayerID, WritingID, Record, RecordDateTime)
VALUES (?, ?, ?, ?, NOW())";
$stmt = $this->mysqli->prepare($query);
$stmt->bind_param("iiid", $maestroID, $playerID, $writingID, $record);
$stmt->execute();
$stmt->close();
}
public function deleteRecord($typingExamRecordID)
{
$query = "
DELETE
FROM typing_exam_record
WHERE TypingExamRecordID = ?
";
$stmt = $this->mysqli->prepare($query);
$stmt->bind_param('i', $typingExamRecordID);
$stmt->execute();
$stmt->close();
}
public function addHighestRecord($maestroID, $playerID, $writingID, $record)
{
$query = "
INSERT INTO typing_exam_highest_record (MaestroID, PlayerID, WritingID, HighestRecord, RecordDateTime)
VALUES (?, ?, ?, ?, NOW())";
$stmt = $this->mysqli->prepare($query);
$stmt->bind_param("iiid", $maestroID, $playerID, $writingID, $record);
$stmt->execute();
$stmt->close();
}
public function updateHighestRecord($typingExamHighestRecordID, $record)
{
$query = "
UPDATE typing_exam_highest_record
SET HighestRecord = ?, RecordDateTime = NOW()
WHERE TypingExamHighestRecordID = ?";
$stmt = $this->mysqli->prepare($query);
$stmt->bind_param("id", $typingExamHighestRecordID, $record);
$stmt->execute();
$stmt->close();
}
public function getThisHourRecord($maestroID, $playerID)
{
$query = "
SELECT TypingExamRecordID, Record
FROM typing_exam_record
WHERE MaestroID = ? AND PlayerID = ? AND DATE(RecordDateTime) = DATE(NOW()) AND HOUR(RecordDateTime) = HOUR(NOW())";
$stmt = $this->mysqli->prepare($query);
$stmt->bind_param("ii", $maestroID, $playerID);
$stmt->execute();
$stmt->bind_result($typingExamRecordID, $record);
$stmt->fetch();
$result = array();
$result["typingExamRecordID"] = $typingExamRecordID;
$result["record"] = $record;
$stmt->close();
return $result;
}
public function getHighestRecord($maestroID, $playerID)
{
$query = "
SELECT WritingID, HighestRecord
FROM typing_exam_highest_record
WHERE MaestroID = ? AND PlayerID = ?";
$stmt = $this->mysqli->prepare($query);
$stmt->bind_param("ii", $maestroID, $playerID);
$stmt->execute();
$stmt->bind_result($writingID, $highestRecord);
$resultArray = array();
while($stmt->fetch()) {
$rowArray = array();
$rowArray["writingID"] = $writingID;
$rowArray["highestRecord"] = $highestRecord;
array_push($resultArray, $rowArray);
}
$stmt->close();
return $resultArray;
}
}
?>
@@ -0,0 +1,36 @@
<?php
header('Content-Type: application/json');
$maestroID = $_POST["maestroID"];
$playerID = $_POST["playerID"];
include "./../lib/json_builder.php";
include "./../lib/connect_db.php";
$jsonBuilder = new JsonBuilder();
$dbConnector = new DBConnector();
$isConnected = $dbConnector->connectDB();
if (!$isConnected) {
$jsonBuilder->setErrorMessage("DB connection : failed");
$jsonBuilder->sendResultFail();
exit;
}
include "./../db/db_method_container.php";
include "./../db/typing_exam_collection.php";
$typingExam = new TypingExamCollection($dbConnector->getMysqli());
$highestRecordArray = $typingExam->getHighestRecord();
$jsonBuilder->setData("highestRecordArray", $highestRecordArray);
if ($highestRecordArray === null) {
$jsonBuilder->sendResultFail();
exit;
}
$jsonBuilder->sendResultSuccess();
?>
@@ -0,0 +1,62 @@
<?php
header('Content-Type: application/json');
$maestroID = $_POST["maestroID"];
$playerID = $_POST["playerID"];
$writingID = $_POST["writingID"];
$record = $_POST["record"];
$isPrevHourFlag = $_POST["isPrevHourFlag"];
include "./../lib/json_builder.php";
include "./../lib/connect_db.php";
$jsonBuilder = new JsonBuilder();
$dbConnector = new DBConnector();
$isConnected = $dbConnector->connectDB();
if (!$isConnected) {
$jsonBuilder->setErrorMessage("DB connection : failed");
$jsonBuilder->sendResultFail();
exit;
}
include "./../db/db_method_container.php";
include "./../db/typing_exam_collection.php";
$typingExam = new TypingExamCollection($dbConnector->getMysqli());
if (isset($isPrevHourFlag)) {
$typingExam->addPrevHourRecord($maestroID, $playerID, $writingID, $record);
$jsonBuilder->setData("prevHourRecord", "added");
$jsonBuilder->sendResultSuccess();
exit;
}
$thisHourRecordData = $typingExam->getThisHourRecord($maestroID, $playerID);
if ($thisHourRecordData === null) {
$typingExam->addRecord($maestroID, $playerID, $writingID, $record);
$jsonBuilder->setData("record", "added");
} else {
if ($record > $thisHourRecordData["record"]) {
$typingExam->deleteRecord($thisHourRecordData["ID"]);
$typingExam->addRecord($maestroID, $playerID, $writingID, $record);
$jsonBuilder->setData("record", "updated");
}
}
$highestRecordData = $typingExam->getHighestRecord($maestroID, $playerID);
$jsonBuilder->setData("highestRecordData", $highestRecordData);
if (count($highestRecordData) === 0) {
$typingExam->addHighestRecord($maestroID, $playerID, $writingID, $record);
$jsonBuilder->setData("highestRecord", "added");
} else {
if ($record > $highestRecordData["record"]) {
$typingExam->updateHighestRecord($highestRecordData["ID"], $record);
$jsonBuilder->setData("highestRecord", "updated");
}
}
$jsonBuilder->sendResultSuccess();
?>