You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
wastesortingserver/admin/Question.php

281 lines
12 KiB

<?php
namespace admin;
use Answer;
use AnswerQuery;
use DbUtil;
use DoConn;
use Exception;
use mysqli;
use Question;
use QuestionQuery;
require_once $_SERVER["DOCUMENT_ROOT"] . "/config.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/head.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/DbUtil.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/Query.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/Log.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/admin/Alert.php";
getMenu("题目管理");
if (empty($_REQUEST)) {
echo <<<EOF
<!DOCTYPE html>
<html lang="zh">
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip();
Array.prototype.filter.call($("form.needs-validation"), function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
$(":radio[name=answer_radio]").click(function() {
$("input[name=right_answer]").val($(this).val())
})
})
function doDel(id) {
if(confirm("确认删除此问题?")){
location.href="/admin/Question.php?action=delete&question_id="+id
}
this.event.preventDefault()
}
</script>
<body>
<ul class="nav nav-pills nav-justified">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#list" role="tab">题目列表</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#add" role="tab">添加题目</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#" onclick="location.href='/'" role="tab">返回首页</a>
</li>
</ul>
<div class="container py-5">
<div class="tab-content" id="v-pills-tabContent">
<div class="tab-pane fade show active" id="list" role="tabpanel" aria-labelledby="v-pills-home-tab">
<div class="text-center text-danger">tip:选中题目查看选项</div>
<div class="list-group d-flex justify-content-center align-items-center w-100">
EOF;
$query = new QuestionQuery();
DbUtil::query("select * from question", $query);
if (empty($query->getQuestionArray())) {
warn_res_with_click("题库暂无题目,需要添加题目", <<<EOF
$('a[href="#add"]').click()
EOF
);
} else {
foreach ($query->getQuestionArray() as $item) {
if ($item instanceof Question) {
$id = $item->getQuestionId();
$title = $item->getQuestionTitle();
echo <<<EOF
<a class="list-group-item list-group-item-action d-flex align-items-center col-6 m-auto justify-content-between" href="/admin/Question.php?action=list&question_id=$id">
$title?
<span class="badge badge-danger badge-pill" data-toggle="tooltip" data-placement="right" title="点击删除" onclick="doDel($id)">X</span>
</a>
EOF;
}
}
}
echo <<<EOF
</div>
</div>
<div id="add" class="tab-pane fade">
<div class="d-flex justify-content-center align-items-center w-100">
<form class="col-6 needs-validation" novalidate method="post" action="Question.php">
<input type="hidden" name="right_answer">
<input type="hidden" name="action" value="add">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">问题</span>
</div>
<input type="text" class="form-control" placeholder="问题" name="question" required>
<div class="input-group-prepend">
<span class="input-group-text">?</span>
</div>
<div class="invalid-feedback">
问题内容不为空
</div>
</div>
<div>
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="答案" name="answer[]" required>
<div class="custom-control custom-radio input-group-prepend m-auto" data-toggle="tooltip" data-placement="right" title="选中为正确答案">
<input type="radio" id="customRadio1" name="answer_radio" value="1" class="custom-control-input" required>
<label class="custom-control-label ml-3" for="customRadio1"></label>
<div class="invalid-feedback ml-3">
请选中一个正确答案
</div>
</div>
</div>
</div>
<div>
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="答案" name="answer[]" required>
<div class="invalid-feedback">
答案内容不为空
</div>
<div class="custom-control custom-radio input-group-prepend m-auto" data-toggle="tooltip" data-placement="right" title="选中为正确答案">
<input type="radio" id="customRadio2" name="answer_radio" value="2" class="custom-control-input" required>
<label class="custom-control-label ml-3" for="customRadio2"></label><div class="invalid-feedback ml-3">
请选中一个正确答案
</div>
</div>
</div>
</div>
<div>
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="答案" name="answer[]" required>
<div class="invalid-feedback">
答案内容不为空
</div>
<div class="custom-control custom-radio input-group-prepend m-auto" data-toggle="tooltip" data-placement="right" title="选中为正确答案">
<input type="radio" id="customRadio3" name="answer_radio" value="3" class="custom-control-input" required>
<label class="custom-control-label ml-3" for="customRadio3"></label>
<div class="invalid-feedback ml-3">
请选中一个正确答案
</div>
</div>
</div>
</div>
<div>
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="答案" name="answer[]" required>
<div class="invalid-feedback">
答案内容不为空
</div>
<div class="custom-control custom-radio input-group-prepend m-auto" data-toggle="tooltip" data-placement="right" title="选中为正确答案">
<input type="radio" id="customRadio4" name="answer_radio" value="4" class="custom-control-input" required>
<label class="custom-control-label ml-3" for="customRadio4"></label>
<div class="invalid-feedback ml-3">
请选中一个正确答案
</div>
</div>
</div>
</div>
<button class="btn btn-block btn-info">提交</button>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
EOF;
} else if (isset($_POST["action"]) && $_POST["action"] == "add") {
// 答案数
$answer_count = 4;
if (empty($_POST["question"])) {
error_res("问题不能为空!", "/admin/Question.php");
} else if (empty($_POST["answer"])) {
error_res("答案内容不能为空!", "/admin/Question.php");
} else if (!is_array($_POST["answer"])) {
error_res("非法答案参数", "/admin/Question.php");
} else if (count($_POST["answer"]) != $answer_count) {
error_res("答案数量不等于4个!", "/admin/Question.php");
} else if (count(array_unique($_POST["answer"])) != $answer_count) {
error_res("答案不能重复!", "/admin/Question.php");
} else if (empty($_POST["right_answer"])) {
error_res("没有正确答案!", "/admin/Question.php");
} else {
try {
DbUtil::insert_with_param("insert into question (question_title) values (?)", "s", new class() implements DoConn
{
public function before_close(mysqli $conn)
{
$id = mysqli_insert_id($conn);
foreach ($_POST["answer"] as $answer) {
DbUtil::insert("insert into answer (question_id, answer, is_true) values (?,?,?)", "isi", $id, $answer, array_search($answer, $_POST["answer"]) + 1 == (int)($_POST["right_answer"]));
}
}
}, $_POST["question"]);
info_res("保存成功", "/admin/Question.php");
} catch (Exception $e) {
error($e);
error_res("保存异常,请稍后再试或者联系管理员!", "/admin/Question.php");
}
}
} else if (isset($_GET["action"]) && $_GET["action"] == "list") {
if (empty($_GET["question_id"])) {
error_res("没有指定问题id,无法查看详情信息!", "/admin/Question.php");
} else if (!is_numeric($_GET["question_id"])) {
error_res("问题id不合法", "/admin/Question.php");
} else {
$query_result = new AnswerQuery();
DbUtil::query("select * from answer where question_id=?", $query_result, array($_GET["question_id"]));
if (empty($query_result->getAnswerArray())) {
error_res("问题id不合法", "/admin/Question.php");
} else {
echo '<div class="container py-5"><div class="col-6 m-auto">';
foreach ($query_result->getAnswerArray() as $item) {
if ($item instanceof Answer) {
$answer = $item->getAnswer();
$is_true = $item->getIsTrue() == 1 ? "checked" : "";
echo <<<EOF
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="答案" value="$answer" readonly>
<div class="custom-control custom-radio input-group-prepend m-auto" data-toggle="tooltip" data-placement="right" title="选中为正确答案">
<input type="radio" id="customRadio1" class="custom-control-input" $is_true disabled>
<label class="custom-control-label ml-3" for="customRadio1"></label>
</div>
</div>
EOF;
} else {
error("类型不匹配");
error_res("操作异常,请联系管理员", "/admin/Question.php");
}
}
echo '</div><a class="btn btn-info btn-block col-6 m-auto" href="/admin/Question.php">返回</a></div>';
}
}
} else if (isset($_GET["action"]) && $_GET["action"] == "delete") {
if (empty($_GET["question_id"])) {
error_res("缺少问题id", "/admin/Question.php");
} else if (!is_numeric($_GET["question_id"])) {
error_res("非法参数id", "/admin/Question.php");
} else {
try {
DbUtil::delete("DELETE q,a FROM question as q,answer as a WHERE q.question_id=a.question_id and q.question_id=?", "i", (int)$_GET["question_id"]);
info_res("问题删除成功", "/admin/Question.php");
} catch (Exception $e) {
error($e);
error_res("操作异常,请联系管理员", "/admin/Question.php");
}
}
} else {
error("非法操作!");
\error_res("非法操作!", "/admin/Question.php");
}