From d29abf4f5e6ca78889037eccb2998b7a08d3373c Mon Sep 17 00:00:00 2001 From: "1213317725@qq.com" <1213317725@qq.com> Date: Sun, 25 May 2025 14:32:46 +0800 Subject: [PATCH] 1 --- admin/src/app/api/exam_questions.ts | 3 + .../components/exam-papers-edit.vue | 92 ++++++++++++++-- .../components/exam-questions-edit.vue | 103 ++++++++++++++---- .../controller/exam_papers/ExamPapers.php | 8 ++ .../exam_questions/ExamQuestions.php | 17 ++- .../app/adminapi/route/exam_questions.php | 5 +- .../admin/exam_papers/ExamPapersService.php | 4 +- .../exam_questions/ExamQuestionsService.php | 29 ++++- 8 files changed, 218 insertions(+), 43 deletions(-) diff --git a/admin/src/app/api/exam_questions.ts b/admin/src/app/api/exam_questions.ts index 9f039f71..d8fb2843 100644 --- a/admin/src/app/api/exam_questions.ts +++ b/admin/src/app/api/exam_questions.ts @@ -49,6 +49,9 @@ export function deleteExamQuestions(id: number) { return request.delete(`exam_questions/exam_questions/${id}`, { showErrorMessage: true, showSuccessMessage: true }) } +export function randomQuestionsList(params: Record) { + return request.post(`exam_questions/random_questions_list`, params, { showErrorMessage: true, showSuccessMessage: true }) +} // USER_CODE_END -- exam_questions diff --git a/admin/src/app/views/exam_papers/components/exam-papers-edit.vue b/admin/src/app/views/exam_papers/components/exam-papers-edit.vue index 484d20f0..c81a4136 100644 --- a/admin/src/app/views/exam_papers/components/exam-papers-edit.vue +++ b/admin/src/app/views/exam_papers/components/exam-papers-edit.vue @@ -9,7 +9,7 @@ - + @@ -18,9 +18,40 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 随机题库 + 重新随机题库 + + + +
@@ -53,7 +84,6 @@ -
@@ -64,9 +94,15 @@
+ + + + + + - @@ -100,7 +136,7 @@ import { t } from '@/lang' import type { FormInstance } from 'element-plus' import { addExamPapers, editExamPapers, getExamPapersInfo } from '@/app/api/exam_papers' - import { getExamQuestionsList } from '@/app/api/exam_questions' + import { getExamQuestionsList,randomQuestionsList } from '@/app/api/exam_questions' let showDialog = ref(false) const loading = ref(false) @@ -115,8 +151,26 @@ questions_ids: '', total_score: '', passing_score: '', + single_choice_count:0, + multiple_choice_count:0, + true_false_count:0, + every_score:0 } + + + const addOption = () => { + randomQuestionsList({ + single_choice_count: formData.single_choice_count, + multiple_choice_count: formData.multiple_choice_count, + true_false_count: formData.true_false_count + }).then(res => { + formData.questions_ids = res.data.questions_ids; + console.log(res.data.questions_ids); + }).catch(() => { + + }) + } // 字典数据 @@ -164,6 +218,9 @@ const handleSelectionChange = (val) => { selectedQuestions.value = val + + const ids = selectedQuestions.value.map(item => item.id).join(',') + formData.questions_ids = ids } @@ -171,7 +228,18 @@ const formData : Record = reactive({ ...initialFormData }) const formRef = ref() - + + + watch( + () => [formData.questions_ids, formData.every_score], + ([newIds, newScore]) => { + const count = newIds ? newIds.split(',').filter(id => id.trim() !== '').length : 0; + const score = Number(newScore) || 0; + formData.total_score = count * score; + }, + { immediate: true } + ); + // 表单验证规则 const formRules = computed(() => { return { @@ -196,10 +264,10 @@ if (valid) { loading.value = true - if (formData.selection_mode == 'manual') { - const ids = selectedQuestions.value.map(item => item.id).join(',') - formData.questions_ids = ids - } + // if (formData.selection_mode == 'manual') { + // const ids = selectedQuestions.value.map(item => item.id).join(',') + // formData.questions_ids = ids + // } let data = formData diff --git a/admin/src/app/views/exam_questions/components/exam-questions-edit.vue b/admin/src/app/views/exam_questions/components/exam-questions-edit.vue index b3886d66..2efcdf11 100644 --- a/admin/src/app/views/exam_questions/components/exam-questions-edit.vue +++ b/admin/src/app/views/exam_questions/components/exam-questions-edit.vue @@ -9,7 +9,7 @@ + :placeholder="t('questionTypePlaceholder')" @change="typeChenge(index)"> @@ -34,15 +34,17 @@ - +
- - + + + + + + @@ -57,26 +59,39 @@ - - - - - 删除 + + + 正确答案 + + + + + + 删除 - -
- + 添加选项 + + + + + + + @@ -111,29 +126,69 @@ question_type: '', question_content_type: '', question_content: '', - option_json: [] + option_json: [], + correct_answer:'' } - + const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + const addOption = () => { + + const index = formData.option_json.length; formData.option_json.push({ option_content_type: 'text', option_content: '', - correct_answer: false + correct_answer: false, + option:alphabet[index] || '' }) } const removeOption = (index) => { formData.option_json.splice(index, 1) + + formData.option_json.forEach((opt, i) => { + opt.option = alphabet[i]; + }) + + handleCorrectAnswerChange(); + } + + + + const typeChenge = () => { + formData.option_json.forEach((opt, i) => { + opt.correct_answer = false; + }) + + formData.correct_answer = ''; } + + const handleCorrectAnswerChange = (changedIndex) => { - if (formData.question_type === 'single_choice') { - formData.options.forEach((opt, i) => { - if (i !== changedIndex) opt.correct_answer = false - }) - } + const correct_answer = []; + formData.option_json.forEach((opt, i) => { + if (opt.correct_answer){ + correct_answer.push(opt.option); + } + }) + formData.correct_answer = correct_answer.join(","); + + } + + + const radioChange = (changedIndex) => { + formData.option_json.forEach((opt, i) => { + opt.correct_answer = false; + }) + + formData.option_json[changedIndex].correct_answer = true; + + formData.correct_answer = formData.option_json[changedIndex].option; + } + + const formData : Record = reactive({ ...initialFormData }) diff --git a/niucloud/app/adminapi/controller/exam_papers/ExamPapers.php b/niucloud/app/adminapi/controller/exam_papers/ExamPapers.php index 5897438a..49cac4c5 100644 --- a/niucloud/app/adminapi/controller/exam_papers/ExamPapers.php +++ b/niucloud/app/adminapi/controller/exam_papers/ExamPapers.php @@ -54,6 +54,10 @@ class ExamPapers extends BaseAdminController ["questions_ids",""], ["total_score",0.00], ["passing_score",0.00], + ["single_choice_count",0], + ["multiple_choice_count",0], + ["true_false_count",0], + ["every_score",0] ]); $this->validate($data, 'app\validate\exam_papers\ExamPapers.add'); @@ -73,6 +77,10 @@ class ExamPapers extends BaseAdminController ["questions_ids",""], ["total_score",0.00], ["passing_score",0.00], + ["single_choice_count",0], + ["multiple_choice_count",0], + ["true_false_count",0], + ["every_score",0] ]); $this->validate($data, 'app\validate\exam_papers\ExamPapers.edit'); diff --git a/niucloud/app/adminapi/controller/exam_questions/ExamQuestions.php b/niucloud/app/adminapi/controller/exam_questions/ExamQuestions.php index 02b21317..6c659725 100644 --- a/niucloud/app/adminapi/controller/exam_questions/ExamQuestions.php +++ b/niucloud/app/adminapi/controller/exam_questions/ExamQuestions.php @@ -54,7 +54,8 @@ class ExamQuestions extends BaseAdminController ["question_type",""], ["question_content_type",""], ["question_content",""], - ["option_json",[]] + ["option_json",[]], + ["correct_answer",""] ]); $this->validate($data, 'app\validate\exam_questions\ExamQuestions.add'); @@ -73,7 +74,8 @@ class ExamQuestions extends BaseAdminController ["question_type",""], ["question_content_type",""], ["question_content",""], - ["option_json",[]] + ["option_json",[]], + ["correct_answer",""] ]); $this->validate($data, 'app\validate\exam_questions\ExamQuestions.edit'); @@ -92,4 +94,15 @@ class ExamQuestions extends BaseAdminController } + public function random_questions_list(){ + $data = $this->request->params([ + ["single_choice_count",0], + ["multiple_choice_count",0], + ["true_false_count",0] + ]); + + return (new ExamQuestionsService())->random_questions_list($data); + } + + } diff --git a/niucloud/app/adminapi/route/exam_questions.php b/niucloud/app/adminapi/route/exam_questions.php index d39aa69b..f54840ba 100644 --- a/niucloud/app/adminapi/route/exam_questions.php +++ b/niucloud/app/adminapi/route/exam_questions.php @@ -29,7 +29,10 @@ Route::group('exam_questions', function () { Route::put('exam_questions/:id', 'exam_questions.ExamQuestions/edit'); //删除试题 Route::delete('exam_questions/:id', 'exam_questions.ExamQuestions/del'); - + + Route::post('random_questions_list', 'exam_questions.ExamQuestions/random_questions_list'); + + })->middleware([ AdminCheckToken::class, AdminCheckRole::class, diff --git a/niucloud/app/service/admin/exam_papers/ExamPapersService.php b/niucloud/app/service/admin/exam_papers/ExamPapersService.php index e90f581b..34fb22eb 100644 --- a/niucloud/app/service/admin/exam_papers/ExamPapersService.php +++ b/niucloud/app/service/admin/exam_papers/ExamPapersService.php @@ -36,7 +36,7 @@ class ExamPapersService extends BaseAdminService */ public function getPage(array $where = []) { - $field = 'id,title,selection_mode,questions_ids,total_score,passing_score,created_at,updated_at'; + $field = 'id,title,selection_mode,questions_ids,single_choice_count,multiple_choice_count,true_false_count,total_score,every_score,passing_score,created_at,updated_at'; $order = 'id desc'; $search_model = $this->model->withSearch(["selection_mode","created_at"], $where)->field($field)->order($order); @@ -51,7 +51,7 @@ class ExamPapersService extends BaseAdminService */ public function getInfo(int $id) { - $field = 'id,title,selection_mode,questions_ids,total_score,passing_score,created_at,updated_at'; + $field = 'id,title,selection_mode,questions_ids,single_choice_count,multiple_choice_count,true_false_count,every_score,total_score,passing_score,created_at,updated_at'; $info = $this->model->field($field)->where([['id', "=", $id]])->findOrEmpty()->toArray(); return $info; diff --git a/niucloud/app/service/admin/exam_questions/ExamQuestionsService.php b/niucloud/app/service/admin/exam_questions/ExamQuestionsService.php index 28a71908..a969bf51 100644 --- a/niucloud/app/service/admin/exam_questions/ExamQuestionsService.php +++ b/niucloud/app/service/admin/exam_questions/ExamQuestionsService.php @@ -36,7 +36,7 @@ class ExamQuestionsService extends BaseAdminService */ public function getPage(array $where = []) { - $field = 'id,title,question_type,question_content_type,question_content,option_json,created_at,updated_at'; + $field = 'id,title,question_type,question_content_type,correct_answer,question_content,option_json,created_at,updated_at'; $order = 'id desc'; $search_model = $this->model->withSearch(["title","question_type","created_at"], $where)->field($field)->order($order); @@ -51,7 +51,7 @@ class ExamQuestionsService extends BaseAdminService */ public function getInfo(int $id) { - $field = 'id,title,question_type,question_content_type,question_content,option_json,created_at,updated_at'; + $field = 'id,title,question_type,question_content_type,correct_answer,question_content,option_json,created_at,updated_at'; $info = $this->model->field($field)->where([['id', "=", $id]])->findOrEmpty()->toArray(); return $info; @@ -95,5 +95,30 @@ class ExamQuestionsService extends BaseAdminService } + public function random_questions_list(array $data){ + + $singleChoiceIds = $data['single_choice_count'] > 0 ? $this->model + ->where([['question_type', '=', 'single_choice']]) + ->orderRaw('RAND()') + ->limit($data['single_choice_count']) + ->column("id") : []; + + $multipleChoiceIds = $data['multiple_choice_count'] > 0 ? $this->model + ->where([['question_type', '=', 'multiple_choice']]) + ->orderRaw('RAND()') + ->limit($data['multiple_choice_count']) + ->column("id") : []; + + $trueFalseIds = $data['true_false_count'] > 0 ? $this->model + ->where([['question_type', '=', 'true_false']]) + ->orderRaw('RAND()') + ->limit($data['true_false_count']) + ->column("id") : []; + + $questions_ids = array_merge($singleChoiceIds, $multipleChoiceIds, $trueFalseIds); + + return success("随机成功", ['questions_ids' => implode(',', $questions_ids)]); + } + }