修改添加题目功能
This commit is contained in:
@@ -92,21 +92,6 @@ class TkController extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//递归构建树形结构
|
||||
private function buildTree($item, $list)
|
||||
{
|
||||
$item['children'] = [];
|
||||
foreach ($list as $child) {
|
||||
if ($child['parent_id'] == $item['id']) {
|
||||
// 添加层级关系标识
|
||||
$child['isLeaf'] = ($child['level'] == max(array_column($list, 'level')));
|
||||
$item['children'][] = $this->buildTree($child, $list);
|
||||
}
|
||||
}
|
||||
return $item;
|
||||
}
|
||||
|
||||
//题目分类增加
|
||||
public function categoryadd()
|
||||
{
|
||||
@@ -162,7 +147,7 @@ class TkController extends BaseController
|
||||
|
||||
// 分类筛选
|
||||
if (!empty($params['category'])) {
|
||||
$cateInfo = TkSubjects::where('name', $params['category'])
|
||||
$cateInfo = TkQuestions::where('name', $params['category'])
|
||||
->where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->field('id')
|
||||
@@ -190,7 +175,7 @@ class TkController extends BaseController
|
||||
->select()
|
||||
->each(function ($item) {
|
||||
// 获取分类信息
|
||||
$cateInfo = TkSubjects::where('id', (int) $item['cate'])
|
||||
$cateInfo = TkQuestions::where('id', (int) $item['cate'])
|
||||
->field('name, icon')
|
||||
->find();
|
||||
if ($cateInfo) {
|
||||
@@ -210,44 +195,69 @@ class TkController extends BaseController
|
||||
'data' => $lists
|
||||
]);
|
||||
} else {
|
||||
$allCategories = TkSubjects::where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->field('id, name, cid, icon')
|
||||
->order('sort asc, id asc')
|
||||
->select()
|
||||
->toArray();
|
||||
// $allCategories = TkQuestions::where('delete_time', null)
|
||||
// ->where('status', 1)
|
||||
// ->field('id, name, cid, icon')
|
||||
// ->order('sort asc, id asc')
|
||||
// ->select()
|
||||
// ->toArray();
|
||||
|
||||
$categories = $this->buildParentChild($allCategories);
|
||||
// $categories = $this->buildParentChild($allCategories);
|
||||
|
||||
View::assign([
|
||||
'categories' => $categories
|
||||
]);
|
||||
// View::assign([
|
||||
// 'categories' => $categories
|
||||
// ]);
|
||||
return View::fetch();
|
||||
}
|
||||
}
|
||||
|
||||
// 构建父子结构
|
||||
private function buildParentChild($lists)
|
||||
//题目添加
|
||||
public function topicadd()
|
||||
{
|
||||
$tree = [];
|
||||
foreach ($lists as $item) {
|
||||
if ($item['cid'] == 0) {
|
||||
// 顶级分类
|
||||
$tree[] = $item;
|
||||
} else {
|
||||
// 子分类
|
||||
foreach ($tree as &$parent) {
|
||||
if ($parent['id'] == $item['cid']) {
|
||||
if (!isset($parent['children'])) {
|
||||
$parent['children'] = [];
|
||||
}
|
||||
$parent['children'][] = $item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($this->request->isPost()) {
|
||||
$params = $this->request->post();
|
||||
|
||||
// 数据验证
|
||||
$validate = new \think\Validate([
|
||||
'question_type' => 'require|number|between:1,6',
|
||||
'subject_id' => 'require|number',
|
||||
'knowledge_point_id' => 'number',
|
||||
'difficulty' => 'number|between:1,5',
|
||||
'content' => 'require',
|
||||
'analysis' => 'require',
|
||||
'answer' => 'require'
|
||||
]);
|
||||
|
||||
if (!$validate->check($params)) {
|
||||
return json(['code' => 1, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
// 处理数据
|
||||
$data = [
|
||||
'question_type' => (int)$params['question_type'],
|
||||
'subject_id' => (int)$params['subject_id'],
|
||||
'knowledge_point_id' => isset($params['knowledge_point_id']) ? (int)$params['knowledge_point_id'] : null,
|
||||
'difficulty' => isset($params['difficulty']) ? (int)$params['difficulty'] : 3,
|
||||
'content' => $params['content'],
|
||||
'analysis' => $params['analysis'],
|
||||
'answer' => $params['answer'],
|
||||
'created_by' => $this->getUserId(), // 假设有获取当前用户ID的方法
|
||||
'created_time' => date('Y-m-d H:i:s'),
|
||||
'updated_by' => $this->getUserId(),
|
||||
'updated_time' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
// 保存数据
|
||||
$result = \think\facade\Db::name('questions')->insert($data);
|
||||
|
||||
if ($result) {
|
||||
return json(['code' => 0, 'msg' => '添加成功']);
|
||||
} else {
|
||||
return json(['code' => 1, 'msg' => '添加失败']);
|
||||
}
|
||||
} else {
|
||||
return View::fetch();
|
||||
}
|
||||
return $tree;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user