parent
acf258b3b0
commit
ded31e5320
@ -1,10 +0,0 @@ |
||||
<?php |
||||
|
||||
|
||||
namespace admin; |
||||
|
||||
//题目管理 |
||||
class Question |
||||
{ |
||||
|
||||
} |
@ -1,11 +0,0 @@ |
||||
<?php |
||||
|
||||
|
||||
namespace admin; |
||||
|
||||
//垃圾分类百科 |
||||
class WasteSorting |
||||
{ |
||||
|
||||
} |
||||
|
@ -0,0 +1,325 @@ |
||||
<?php |
||||
|
||||
|
||||
namespace admin; |
||||
|
||||
|
||||
use Answer; |
||||
use AnswerQuery; |
||||
use DbUtil; |
||||
use DoConn; |
||||
use Exception; |
||||
use mysqli; |
||||
use Question; |
||||
use QuestionQuery; |
||||
|
||||
require_once "../config.php"; |
||||
require_once __ROOT__ . "/head.php"; |
||||
require_once __ROOT__ . "/database/DbUtil.php"; |
||||
require_once __ROOT__ . "/database/Query.php"; |
||||
require_once __ROOT__ . "/Log.php"; |
||||
|
||||
getMenu("题目管理"); |
||||
|
||||
function reponse_with_click($message, $class, $script) |
||||
{ |
||||
echo <<<EOF |
||||
<script> |
||||
function doClick() { |
||||
$script |
||||
} |
||||
</script> |
||||
<div class="container py-5"> |
||||
<div class="alert $class alert-dismissible fade show col-5 m-auto text-center" role="alert"> |
||||
<strong>$message</strong> |
||||
<button type="button" class="close" data-dismiss="alert" aria-label="Close" onclick="doClick()"> |
||||
<span aria-hidden="true">×</span> |
||||
</button> |
||||
</div> |
||||
</div> |
||||
EOF; |
||||
} |
||||
|
||||
function response_with_href($message, $class, $href) |
||||
{ |
||||
reponse_with_click($message, $class, <<<EOF |
||||
location.href='$href' |
||||
EOF |
||||
); |
||||
} |
||||
|
||||
function error_res($err) |
||||
{ |
||||
response_with_href($err, "alert-danger", "/admin/Question.php"); |
||||
} |
||||
|
||||
function info_res($info) |
||||
{ |
||||
response_with_href($info, "alert-info", "/admin/Question.php"); |
||||
} |
||||
|
||||
function warn_res($warn) |
||||
{ |
||||
response_with_href($warn, "alert-warning", "/admin/Question.php"); |
||||
} |
||||
|
||||
function warn_res_with_click($warn, $click) |
||||
{ |
||||
reponse_with_click($warn, "alert-warning", $click); |
||||
} |
||||
|
||||
if (empty($_POST) && empty($_GET)) { |
||||
echo <<<EOF |
||||
<!DOCTYPE html> |
||||
<html lang="zh"> |
||||
<script> |
||||
$(function () { |
||||
$('[data-toggle="tooltip"]').tooltip(); |
||||
var validation = 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="text-center text-danger">tip:选中题目查看选项</div> |
||||
<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="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("问题不能为空!"); |
||||
} else if (empty($_POST["answer"])) { |
||||
error_res("答案内容不能为空!"); |
||||
} else if (!is_array($_POST["answer"])) { |
||||
error_res("非法答案参数"); |
||||
} else if (count($_POST["answer"]) != $answer_count) { |
||||
error_res("答案数量不等于4个!"); |
||||
} else if (count(array_unique($_POST["answer"])) != $answer_count) { |
||||
error_res("答案不能重复!"); |
||||
} else if (empty($_POST["right_answer"])) { |
||||
error_res("没有正确答案!"); |
||||
} 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("保存成功"); |
||||
} catch (Exception $e) { |
||||
error($e); |
||||
error_res("保存异常,请稍后再试或者联系管理员!"); |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
} else if (isset($_GET["action"]) && $_GET["action"] == "list") { |
||||
if (empty($_GET["question_id"])) { |
||||
error_res("没有指定问题id,无法查看详情信息!"); |
||||
} else if (!is_numeric($_GET["question_id"])) { |
||||
error_res("问题id不合法"); |
||||
} 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不合法"); |
||||
} 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("操作异常,请联系管理员"); |
||||
} |
||||
} |
||||
echo '</div><button class="btn btn-info btn-block col-6 m-auto" onclick="location.href=\'/admin/Question.php\'">返回</button></div>'; |
||||
|
||||
} |
||||
} |
||||
} else if (isset($_GET["action"]) && $_GET["action"] == "delete") { |
||||
if (empty($_GET["question_id"])) { |
||||
error_res("缺少问题id"); |
||||
} else if (!is_numeric($_GET["question_id"])) { |
||||
error_res("非法参数id"); |
||||
} 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("问题删除成功"); |
||||
} catch (Exception $e) { |
||||
error($e); |
||||
error_res("操作异常,请联系管理员"); |
||||
} |
||||
|
||||
} |
||||
} else { |
||||
\error("非法操作!"); |
||||
} |
||||
|
@ -0,0 +1,49 @@ |
||||
<?php |
||||
|
||||
require_once "../config.php"; |
||||
require_once __ROOT__ . "/api/JsonResponse.php"; |
||||
require_once __ROOT__ . "/Log.php"; |
||||
require_once __ROOT__ . "/database/Query.php"; |
||||
|
||||
//题库题目数上限 |
||||
$max_count = 10; |
||||
$q_result = new QuestionQuery(); |
||||
//随机抽取问题生成题库 |
||||
DbUtil::query("select * from question order by rand() limit 0," . $max_count, $q_result); |
||||
|
||||
if (empty($q_result->getQuestionArray())) { |
||||
json_res(json_encode(array(["status" => false, "msg" => "生成题库失败,题库为空,请联系管理员添加题目"]), JSON_UNESCAPED_UNICODE)); |
||||
return; |
||||
} else { |
||||
foreach ($q_result->getQuestionArray() as $querstion_item) { |
||||
if ($querstion_item instanceof Question) { |
||||
$answer_res = new AnswerQuery(); |
||||
DbUtil::query("select * from answer where question_id=? order by rand()", $answer_res, array($querstion_item->getQuestionId())); |
||||
if (empty($answer_res->getAnswerArray())) { |
||||
error("无法查询question_id=" . $querstion_item->getQuestionId() . "答案"); |
||||
json_res(json_encode(array(["status" => false, "msg" => "生成题库失败,请联系管理员"]), JSON_UNESCAPED_UNICODE)); |
||||
return; |
||||
} else { |
||||
$querstion_item->setAnswer($answer_res->getAnswerArray()); |
||||
foreach ($answer_res->getAnswerArray() as $answer_item) { |
||||
if ($answer_item instanceof Answer) { |
||||
if ($answer_item->getIsTrue() == "1") { |
||||
$querstion_item->setRightAnswer(array_search($answer_item, $answer_res->getAnswerArray())); |
||||
break; |
||||
} |
||||
} else { |
||||
error("实体类型不匹配" . var_dump($answer_res[0])); |
||||
json_res(json_encode(array(["status" => false, "msg" => "生成题库失败,请联系管理员"]), JSON_UNESCAPED_UNICODE)); |
||||
return; |
||||
} |
||||
} |
||||
} |
||||
} else { |
||||
error("实体类型不匹配" . var_dump($querstion_item)); |
||||
json_res(json_encode(array(["status" => false, "msg" => "生成题库失败,请联系管理员"]), JSON_UNESCAPED_UNICODE)); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
json_res(json_encode(array("status" => true, "msg" => "成功创建题库,包含" . count($q_result->getQuestionArray()) . "条题目", "result" => $q_result->getQuestionArray()), JSON_UNESCAPED_UNICODE)); |
||||
} |
@ -0,0 +1,66 @@ |
||||
<?php |
||||
|
||||
//答案类 |
||||
class Answer implements JsonSerializable |
||||
{ |
||||
private $answer_id; |
||||
private $question_id; |
||||
private $answer; |
||||
private $is_true; |
||||
|
||||
/** |
||||
* Answer constructor. |
||||
* @param $answer_id |
||||
* @param $question_id |
||||
* @param $answer |
||||
* @param $is_true |
||||
*/ |
||||
public function __construct($answer_id, $question_id, $answer, $is_true) |
||||
{ |
||||
$this->answer_id = $answer_id; |
||||
$this->question_id = $question_id; |
||||
$this->answer = $answer; |
||||
$this->is_true = $is_true; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getAnswerId() |
||||
{ |
||||
return $this->answer_id; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getQuestionId() |
||||
{ |
||||
return $this->question_id; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getAnswer() |
||||
{ |
||||
return $this->answer; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getIsTrue() |
||||
{ |
||||
return $this->is_true; |
||||
} |
||||
|
||||
public function jsonSerialize() |
||||
{ |
||||
$data = []; |
||||
foreach ($this as $key => $val) { |
||||
if ($val !== null) $data[$key] = $val; |
||||
} |
||||
return $data; |
||||
} |
||||
} |
@ -0,0 +1,38 @@ |
||||
<?php |
||||
|
||||
|
||||
//菜单管理 |
||||
class Menu |
||||
{ |
||||
private $name; |
||||
private $interface; |
||||
|
||||
/** |
||||
* Menu constructor. |
||||
* @param $name |
||||
* @param $interface |
||||
*/ |
||||
public function __construct($name, $interface) |
||||
{ |
||||
$this->name = $name; |
||||
$this->interface = $interface; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getName() |
||||
{ |
||||
return $this->name; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getInterface() |
||||
{ |
||||
return $this->interface; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,80 @@ |
||||
<?php |
||||
|
||||
//问题类 |
||||
class Question implements JsonSerializable |
||||
{ |
||||
private $question_id; |
||||
private $question_title; |
||||
private $answer = array(); |
||||
private $right_answer; |
||||
|
||||
/** |
||||
* Question constructor. |
||||
* @param $question_id |
||||
* @param $question_title |
||||
*/ |
||||
public function __construct($question_id, $question_title) |
||||
{ |
||||
$this->question_id = $question_id; |
||||
$this->question_title = $question_title; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getQuestionId() |
||||
{ |
||||
return $this->question_id; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getQuestionTitle() |
||||
{ |
||||
return $this->question_title; |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $question_id |
||||
*/ |
||||
public function setQuestionId($question_id): void |
||||
{ |
||||
$this->question_id = $question_id; |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $question_title |
||||
*/ |
||||
public function setQuestionTitle($question_title): void |
||||
{ |
||||
$this->question_title = $question_title; |
||||
} |
||||
|
||||
public function jsonSerialize() |
||||
{ |
||||
$data = []; |
||||
foreach ($this as $key => $val) { |
||||
if ($val !== null) $data[$key] = $val; |
||||
} |
||||
return $data; |
||||
} |
||||
|
||||
/** |
||||
* @param array $answer |
||||
*/ |
||||
public function setAnswer(array $answer): void |
||||
{ |
||||
$this->answer = $answer; |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $right_answer |
||||
*/ |
||||
public function setRightAnswer($right_answer): void |
||||
{ |
||||
$this->right_answer = $right_answer; |
||||
} |
||||
|
||||
|
||||
} |
File diff suppressed because one or more lines are too long
@ -1,23 +1,29 @@ |
||||
<?php |
||||
include "config.php"; |
||||
include __ROOT__ . "/database/DbUtil.php"; |
||||
require_once "config.php"; |
||||
require_once __ROOT__ . "/database/DbUtil.php"; |
||||
require_once __ROOT__ . "/entity/Question.php"; |
||||
require_once __ROOT__ . "/entity/Answer.php"; |
||||
|
||||
echo <<<EOF |
||||
<html> |
||||
<head> |
||||
<meta charset="utf-8"> |
||||
<title>菜鸟教程(runoob.com)</title> |
||||
</head> |
||||
<body> |
||||
//echo <<<EOF |
||||
//<html> |
||||
//<head> |
||||
//<meta charset="utf-8"> |
||||
//<title>菜鸟教程(runoob.com)</title> |
||||
//</head> |
||||
//<body> |
||||
// |
||||
//<form action="/api/BaiduImage.php" method="post" enctype="multipart/form-data"> |
||||
// <label for="file">文件名:</label> |
||||
// <input type="file" name="file" id="file"><br> |
||||
// <input type="submit" name="submit" value="提交"> |
||||
//</form> |
||||
// |
||||
//</body> |
||||
//</html> |
||||
//EOF; |
||||
|
||||
var_dump(array_search(new Answer(1, 2, 3, 4), array(new Answer(4, 5, 6, 7), new Answer(2, 2, 3, 4)))); |
||||
|
||||
<form action="/api/BaiduImage.php" method="post" enctype="multipart/form-data"> |
||||
<label for="file">文件名:</label> |
||||
<input type="file" name="file" id="file"><br> |
||||
<input type="submit" name="submit" value="提交"> |
||||
</form> |
||||
|
||||
</body> |
||||
</html> |
||||
EOF; |
||||
|
||||
|
||||
|
Loading…
Reference in new issue