Add: requestTodayBestRecord, updateTodayBestRecord

This commit is contained in:
2018-06-05 16:32:57 +09:00
parent 5f53360871
commit feb95d461b
6 changed files with 219 additions and 35 deletions
@@ -0,0 +1,43 @@
<?php
header('Content-Type: application/json');
include "./../send_error_code.php";
$maestro_id = $_POST["MaestroID"];
$user_id = $_POST["UserID"];
$app_id = $_POST["AppID"];
include "./../setup/connect_db.php";
$replyJSON = array();
$replyJSON["BestRecord"] = get_best_record($maestro_id, $app_id, $user_id);
if($replyJSON.length === 0) {
send_error_message($replyJSON, "히스토리 기록 없음");
$db_conn->close();
exit;
}
echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE);
$db_conn->close();
function get_best_record($maestro_id, $app_id, $user_id) {
global $db_conn;
$query = "
SELECT BestRecord
FROM moty_best_record
WHERE MaestroID = ? AND AppID = ? AND UserID = ? AND DATE(RecordDateTime) = Date(NOW())
";
$stmt = $db_conn->prepare($query);
$stmt->bind_param("iii", $maestro_id, $app_id, $user_id);
$stmt->execute();
$stmt->bind_result($best_record);
$stmt->fetch();
$stmt->close();
return $best_record;
}
?>
@@ -1,32 +0,0 @@
<?php
header('Content-Type: application/json');
include "send_error_code.php";
$user_id = $_POST["UserID"];
$language = $_POST["language"];
$stageName = $_POST["stageName"];
$record = $_POST["Record"];
$stage_name = $language."_".$stageName;
// $user_id = $_GET["UserID"];
// $language = $_GET["language"];
// $stageName = $_GET["stageName"];
// $best_record = $_GET["BestRecord"];
// $typing_stage = $language."_".$stageName;
// echo $user_id."\n";
// echo $typingStage."\n";
include "connect_db.php";
$query = "
INSERT INTO afterschool_record (UserID, Record, RecordDateTime, StageName)
VALUES (?, ?, NOW(), ?);
";
$stmt = $db_conn->prepare($query);
$stmt->bind_param('ids', $user_id, $record, $stage_name);
$stmt->execute();
$db_conn->close();
?>
@@ -0,0 +1,82 @@
<?php
header('Content-Type: application/json');
include "./../send_error_code.php";
$maestro_id = $_POST["MaestroID"];
$user_id = $_POST["UserID"];
$app_id = $_POST["AppID"];
$best_record = $_POST["BestRecord"];
echo $best_record." / ";
include "./../setup/connect_db.php";
$prev_best_record_id = -1;
$prev_best_record = 0;
$prev_best_record_id = get_best_record($maestro_id, $app_id, $user_id);
// echo $prev_best_record_id." ".$prev_best_record;
if($prev_best_record_id == "") {
echo "insert";
insert_best_record($maestro_id, $app_id, $user_id, $best_record);
} else {
echo $best_record." ".$prev_best_record;
if($best_record > $prev_best_record) {
echo "update";
update_best_record($prev_best_record_id, $best_record);
}
}
$db_conn->close();
function get_best_record($maestro_id, $app_id, $user_id) {
global $db_conn;
global $prev_best_record_id, $prev_best_record;
$query = "
SELECT BestRecordID, BestRecord
FROM moty_best_record
WHERE MaestroID = ? AND AppID = ? AND UserID = ? AND DATE(RecordDateTime) = DATE(NOW())
";
$stmt = $db_conn->prepare($query);
$stmt->bind_param("iii", $maestro_id, $app_id, $user_id);
$stmt->execute();
$stmt->bind_result($prev_best_record_id, $prev_best_record);
$stmt->fetch();
$stmt->close();
$GLOBALS["prev_best_record_id"] = $prev_best_record_id;
$GLOBALS["prev_best_record"] = $prev_best_record;
return $prev_best_record_id;
}
function insert_best_record($maestro_id, $app_id, $user_id, $best_record) {
global $db_conn;
$query = "
INSERT INTO moty_best_record (MaestroID, UserID, AppID, BestRecord, RecordDateTime)
VALUES (?, ?, ?, ?, NOW());
";
$stmt = $db_conn->prepare($query);
$stmt->bind_param('iiii', $maestro_id, $user_id, $app_id, $best_record);
$stmt->execute();
}
function update_best_record($prev_best_record_id, $best_record) {
global $db_conn;
$query = "
UPDATE moty_best_record
SET BestRecord = ?, RecordDateTime = now()
WHERE BestRecordID = ? AND DATE(RecordDateTime) = DATE(NOW())
";
$stmt = $db_conn->prepare($query);
$stmt->bind_param('ii', $best_record, $prev_best_record_id);
$stmt->execute();
}
?>