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/api/QuestionAPI.php

58 lines
2.9 KiB

<?php
require_once $_SERVER["DOCUMENT_ROOT"] . "/config.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/api/JsonResponse.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/Log.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/Query.php";
//题库题目数上限
$max_count = 10;
$q_result = new QueryQuestion();
//随机抽取问题生成题库
try {
DbUtil::query("select * from question order by rand() limit 0," . $max_count, $q_result);
} catch (Exception $e) {
json_res(json_encode(array("status" => false, "msg" => "生成题库出现严重异常"), JSON_UNESCAPED_UNICODE));
return;
}
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 QueryAnswer();
try {
DbUtil::query("select * from answer where question_id=? order by rand()", $answer_res, array($querstion_item->getQuestionId()));
} catch (Exception $e) {
json_res(json_encode(array("status" => false, "msg" => "生成题库出现严重异常,请联系管理员"), JSON_UNESCAPED_UNICODE));
}
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));
}