TestPaper.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\merchant\controller\questions;
  12. use app\merchant\controller\AuthController;
  13. use service\JsonService as Json;
  14. use service\UploadService as Upload;
  15. use think\Request;
  16. use think\Url;
  17. use service\FormBuilder as Form;
  18. use app\merchant\model\questions\TestPaper as TestPaperModel;
  19. use app\admin\model\questions\TestPaperCategory;
  20. use app\merchant\model\questions\QuestionsCategpry;
  21. use app\merchant\model\questions\Questions;
  22. use app\merchant\model\questions\TestPaperQuestions;
  23. use app\merchant\model\questions\TestPaperScoreGrade;
  24. use app\merchant\model\questions\Certificate;
  25. use app\merchant\model\questions\CertificateRelated;
  26. use app\merchant\model\questions\ExaminationRecord;
  27. use app\merchant\model\special\Special;
  28. use app\merchant\model\user\User;
  29. /**
  30. * 试卷
  31. * Class TestPaper
  32. */
  33. class TestPaper extends AuthController
  34. {
  35. /**
  36. * 试卷列表
  37. */
  38. public function index($type = 1)
  39. {
  40. $this->assign(['type' => $type, 'category' => TestPaperCategory::taskCategoryAll(2, $type)]);
  41. return $this->fetch();
  42. }
  43. /**
  44. * 获取试卷列表
  45. */
  46. public function getTestPaperExercisesList($type = 1)
  47. {
  48. $where = parent::getMore([
  49. ['page', 1],
  50. ['limit', 20],
  51. ['pid', 0],
  52. ['is_show', ''],
  53. ['title', '']
  54. ]);
  55. $where['type'] = $type;
  56. $where['mer_id'] = $this->merchantId;
  57. return Json::successlayui(TestPaperModel::testPaperExercisesList($where));
  58. }
  59. /**
  60. * 用户答题记录
  61. */
  62. public function answerNotes($type = 1, $test_id = 0, $uid = 0)
  63. {
  64. $this->assign(['type' => $type, 'test_id' => $test_id, 'uid' => $uid, 'testPaper' => TestPaperModel::testPaperList($type, $this->merchantId)]);
  65. return $this->fetch('record');
  66. }
  67. /**
  68. * 获取试卷列表
  69. */
  70. public function getExaminationRecords($type = 1, $testId = 0, $uid = 0)
  71. {
  72. $where = parent::getMore([
  73. ['page', 1],
  74. ['limit', 20],
  75. ['test_id', 0],
  76. ['title', ''],
  77. ['excel', 0]
  78. ]);
  79. $where['type'] = $type;
  80. if ($testId) $where['test_id'] = $testId;
  81. if ($uid) $where['uid'] = $uid;
  82. $where['mer_id'] = $this->merchantId;
  83. return Json::successlayui(ExaminationRecord::getExaminationRecord($where));
  84. }
  85. /**
  86. * 用户答题
  87. */
  88. public function answers($record_id = 0, $test_id = 0, $type = 1, $uid = 0)
  89. {
  90. $dat = TestPaperModel::where('id', $test_id)->field('single_sort,many_sort,judge_sort')->find();
  91. $this->assign(['record_id' => $record_id, 'test_id' => $test_id, 'type' => $type, 'uid' => $uid, 'single_sort' => $dat['single_sort'], 'many_sort' => $dat['many_sort'], 'judge_sort' => $dat['judge_sort']]);
  92. return $this->fetch();
  93. }
  94. /**答题信息
  95. * @param $uid
  96. * @return void
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. * @throws \think\exception\DbException
  100. */
  101. public function getUserInformation($uid)
  102. {
  103. if (!$uid) return Json::fail('参数错误');
  104. $data = User::where(['uid' => $uid])->field('nickname,name,uid,avatar')->find();
  105. return Json::successful($data);
  106. }
  107. /**成绩
  108. * @param int $record_id
  109. * @param int $test_id
  110. * @param int $uid
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. * @throws \think\exception\DbException
  114. */
  115. public function getUserAchievement($record_id = 0, $test_id = 0, $uid = 0)
  116. {
  117. if (!$record_id || !$test_id || !$uid) return Json::fail('参数错误');
  118. $dat = TestPaperModel::where('id', $test_id)->field('title,item_number,total_score')->find();
  119. $record = ExaminationRecord::where(['id' => $record_id, 'test_id' => $test_id, 'uid' => $uid, 'type' => 2])->find();
  120. $data['title'] = $dat['title'];
  121. $data['item_number'] = $dat['item_number'];
  122. $data['total_score'] = $dat['total_score'];
  123. $data['yes_questions'] = $record['yes_questions'];
  124. $data['score'] = $record['score'];
  125. $data['start_time'] = date('Y-m-d H:i', $record['start_time']);
  126. return Json::successful($data);
  127. }
  128. /**试卷中的试题答题情况
  129. * @param int $id
  130. * @param int $type
  131. */
  132. public function getTestPaperAnswers($test_id = 0, $record_id = 0, $question_type = 1)
  133. {
  134. if (!$test_id || !$record_id) return Json::fail('参数错误');
  135. return Json::successful(TestPaperQuestions::getExaminationRecordAnswers($test_id, $record_id, $question_type));
  136. }
  137. /**
  138. * 快速编辑
  139. * @param string $field 字段名
  140. * @param int $id 修改的主键
  141. * @param string value 修改后的值
  142. * @return json
  143. */
  144. public function set_value($field = '', $id = '', $value = '', $test = 0)
  145. {
  146. if (!$field || !$id || $value == '') Json::fail('缺少参数3');
  147. if ($field == 'sort' && bcsub($value, 0, 0) < 0) return Json::fail('排序不能为负数');
  148. if ($test) {
  149. $model_type = 'paper';
  150. } else {
  151. $model_type = 'test';
  152. }
  153. $res = parent::getDataModification($model_type, $id, $field, $value);
  154. if ($res)
  155. return Json::successful('保存成功');
  156. else
  157. return Json::fail('保存失败');
  158. }
  159. /**关联试题 手动组题
  160. * @param int $id
  161. */
  162. public function questions($question_type = 0, $id = 1)
  163. {
  164. $this->assign(['id' => $id, 'question_type' => $question_type, 'cateList' => QuestionsCategpry::taskCategoryAll(2, $this->merchantId)]);
  165. return $this->fetch('questions');
  166. }
  167. /**
  168. * 获取题库列表
  169. */
  170. public function getTestQuestionsList($question_type = '', $id = 0)
  171. {
  172. $where = parent::getMore([
  173. ['page', 1],
  174. ['limit', 20],
  175. ['pid', 0],
  176. ['title', '']
  177. ]);
  178. $where['type'] = $question_type;
  179. $where['mer_id'] = $this->merchantId;
  180. $arrays = [];
  181. if ($id) {
  182. $arrays = TestPaperQuestions::where(['test_id' => $id])->column('questions_id');
  183. }
  184. $list = Questions::questionsList($where, $arrays);
  185. return Json::successlayui($list);
  186. }
  187. /**试题分类
  188. * @param int $id
  189. * @param int $type
  190. */
  191. public function cate_questions()
  192. {
  193. $list = QuestionsCategpry::taskCategoryAll(2, $this->merchantId);
  194. return Json::successful($list);
  195. }
  196. /**
  197. * 查看试卷
  198. */
  199. public function test_paper($id = 0, $type = 1)
  200. {
  201. $this->assign(['id' => $id, 'type' => $type]);
  202. return $this->fetch();
  203. }
  204. /**试卷中的试题
  205. * @param int $id
  206. * @param int $type
  207. */
  208. public function getTestPaperList($id = 0, $type = 1)
  209. {
  210. $where = parent::getMore([
  211. ['page', 1],
  212. ['limit', 20],
  213. ]);
  214. return Json::successlayui(TestPaperQuestions::getTestPaperList($where, $id, $type));
  215. }
  216. /**添加/编辑
  217. * @param int $id
  218. * @return mixed
  219. */
  220. public function add($id = 0, $type = 1)
  221. {
  222. $test = $id > 0 ? TestPaperModel::get($id) : [];
  223. $grade = [];
  224. if ($test && $id) {
  225. $single_tmp_list = TestPaperQuestions::gettestPaperQuestions($id, 1);
  226. $many_tmp_list = TestPaperQuestions::gettestPaperQuestions($id, 2);
  227. $judge_tmp_list = TestPaperQuestions::gettestPaperQuestions($id, 3);
  228. if ($type == 2) $grade = TestPaperScoreGrade::testPaperScoreGradeList($id);
  229. } else {
  230. $single_tmp_list = [];
  231. $many_tmp_list = [];
  232. $judge_tmp_list = [];
  233. }
  234. $this->assign([
  235. 'id' => $id,
  236. 'type' => $type,
  237. 'test' => json_encode($test),
  238. 'grade' => json_encode($grade),
  239. 'single_tmp_list' => json_encode($single_tmp_list),
  240. 'many_tmp_list' => json_encode($many_tmp_list),
  241. 'judge_tmp_list' => json_encode($judge_tmp_list)
  242. ]);
  243. return $this->fetch();
  244. }
  245. /**
  246. * 获取试题分类
  247. */
  248. public function add_cate_list($type = 1)
  249. {
  250. $category = TestPaperCategory::taskCategoryAll(2, $type);
  251. return Json::successful($category);
  252. }
  253. /**添加/编辑试题
  254. * @param int $id
  255. */
  256. public function save_add($id = 0, $type = 1)
  257. {
  258. $data = parent::postMore([
  259. ['title', ''],
  260. ['image', ''],
  261. ['tid', 0],
  262. ['is_show', 1],
  263. ['item_number', 0],
  264. ['total_score', 0],
  265. ['single_number', 0],
  266. ['single_score', 0],
  267. ['many_number', 0],
  268. ['many_score', 0],
  269. ['judge_number', 0],
  270. ['judge_score', 0],
  271. ['single_sort', 0],
  272. ['many_sort', 0],
  273. ['judge_sort', 0],
  274. ['txamination_time', 0],
  275. ['fake_sales', 0],
  276. ['difficulty', 1],
  277. ['pay_type', 0],
  278. ['money', 0],
  279. ['member_pay_type', 0],
  280. ['member_money', 0],
  281. ['is_score', 0],
  282. ['is_group', 1],
  283. ['cate_id', 0],
  284. ['frequency', 1],
  285. ['singleIds', ''],
  286. ['manyIds', ''],
  287. ['judgeIds', ''],
  288. ['grade', ''],
  289. ['sort', 0]
  290. ]);
  291. if ($data['tid'] <= 0) return Json::fail('请选择试题分类');
  292. if (!$data['title']) return Json::fail('请输入试卷标题');
  293. if ($type == 2 && !$data['image']) return Json::fail('请添加试卷封面图');
  294. if ($data['single_number'] < 0 || $data['many_number'] < 0 || $data['judge_number'] < 0) return Json::fail('各类型题目数量不能小于0');
  295. $data['item_number'] = bcadd($data['single_number'], bcadd($data['many_number'], $data['judge_number'], 0), 0);
  296. $total_single_score = bcmul($data['single_number'], $data['single_score'], 0);
  297. $total_many_score = bcmul($data['many_number'], $data['many_score'], 0);
  298. $total_judge_score = bcmul($data['judge_number'], $data['judge_score'], 0);
  299. $data['total_score'] = bcadd($total_single_score, bcadd($total_many_score, $total_judge_score, 0), 0);
  300. if ($data['item_number'] > 100) return Json::fail('试卷最多100道');
  301. if ($type == 1) {
  302. unset(
  303. $data['txamination_time'],
  304. $data['image'],
  305. $data['pay_type'],
  306. $data['money'],
  307. $data['member_pay_type'],
  308. $data['member_money']
  309. );
  310. }
  311. $singleIds = json_decode($data['singleIds']);
  312. $manyIds = json_decode($data['manyIds']);
  313. $judgeIds = json_decode($data['judgeIds']);
  314. $grade = json_decode($data['grade'], true);
  315. if ($id) {
  316. $data['status'] = $this->isAudit == 1 ? 0 : 1;
  317. $res = TestPaperModel::edit($data, $id);
  318. $res1 = true;
  319. if ($type == 2) {
  320. $res1 = TestPaperScoreGrade::testPaperScoreGradeAdd($id, $grade);
  321. }
  322. TestPaperQuestions::where('test_id', $id)->delete();
  323. if ($data['is_group'] == 1) {
  324. $res2 = TestPaperQuestions::addTestPaperQuestions($id, $type, (int)$data['single_number'], $singleIds, $data['single_score']);
  325. $res3 = TestPaperQuestions::addTestPaperQuestions($id, $type, (int)$data['many_number'], $manyIds, $data['many_score']);
  326. $res4 = TestPaperQuestions::addTestPaperQuestions($id, $type, (int)$data['judge_number'], $judgeIds, $data['judge_score']);
  327. } else {
  328. $res2 = TestPaperQuestions::addRandomGroupQuestions($id, $type, 1, $data['cate_id'], (int)$data['single_number'], $data['single_score']);
  329. $res3 = TestPaperQuestions::addRandomGroupQuestions($id, $type, 2, $data['cate_id'], (int)$data['many_number'], $data['many_score']);
  330. $res4 = TestPaperQuestions::addRandomGroupQuestions($id, $type, 3, $data['cate_id'], (int)$data['judge_number'], $data['judge_score']);
  331. }
  332. $res5 = $this->inspectTestQuestions($id);
  333. } else {
  334. $data['type'] = $type;
  335. $data['add_time'] = time();
  336. $data['status'] = $this->isAudit == 1 ? 0 : 1;
  337. $data['mer_id'] = $this->merchantId;
  338. if (TestPaperModel::be(['title' => $data['title'], 'mer_id' => $this->merchantId, 'is_del' => 0])) {
  339. return Json::fail('标题已存在!');
  340. }
  341. $res = TestPaperModel::insertGetId($data);
  342. $res1 = true;
  343. if ($type == 2) {
  344. $res1 = TestPaperScoreGrade::testPaperScoreGradeAdd($res, $grade);
  345. }
  346. if ($data['is_group'] == 1) {
  347. $res2 = TestPaperQuestions::addTestPaperQuestions($res, $type, (int)$data['single_number'], $singleIds, $data['single_score']);
  348. $res3 = TestPaperQuestions::addTestPaperQuestions($res, $type, (int)$data['many_number'], $manyIds, $data['many_score']);
  349. $res4 = TestPaperQuestions::addTestPaperQuestions($res, $type, (int)$data['judge_number'], $judgeIds, $data['judge_score']);
  350. } else {
  351. $res2 = TestPaperQuestions::addRandomGroupQuestions($res, $type, 1, $data['cate_id'], (int)$data['single_number'], $data['single_score']);
  352. $res3 = TestPaperQuestions::addRandomGroupQuestions($res, $type, 2, $data['cate_id'], (int)$data['many_number'], $data['many_score']);
  353. $res4 = TestPaperQuestions::addRandomGroupQuestions($res, $type, 3, $data['cate_id'], (int)$data['judge_number'], $data['judge_score']);
  354. }
  355. $res5 = $this->inspectTestQuestions($res);
  356. }
  357. if ($res && $res1 && $res2 && $res3 && $res4 && $res5) {
  358. return Json::successful('添加/编辑成功');
  359. } else {
  360. return Json::fail('添加/编辑失败');
  361. }
  362. }
  363. /**
  364. * 检查试卷试题数量
  365. */
  366. public function inspectTestQuestions($id)
  367. {
  368. if (!$id) return Json::fail('参数错误');
  369. $test = TestPaperModel::get($id);
  370. if (!$test) return Json::fail('试卷不存在');
  371. $single_number = TestPaperQuestions::testPaperQuestionsNumber($id, 1);
  372. $many_number = TestPaperQuestions::testPaperQuestionsNumber($id, 2);
  373. $judge_number = TestPaperQuestions::testPaperQuestionsNumber($id, 3);
  374. if ($single_number < $test['single_number'] || $many_number < $test['many_number'] || $judge_number < $test['judge_number']) {
  375. $total = bcadd($single_number, bcadd($many_number, $judge_number, 0), 0);
  376. $total_single_score = bcmul($single_number, $test['single_score'], 0);
  377. $total_many_score = bcmul($many_number, $test['many_score'], 0);
  378. $total_judge_score = bcmul($judge_number, $test['judge_score'], 0);
  379. $total_score = bcadd($total_single_score, bcadd($total_many_score, $total_judge_score, 0), 0);
  380. $data['single_number'] = $single_number;
  381. $data['many_number'] = $many_number;
  382. $data['judge_number'] = $judge_number;
  383. $data['item_number'] = $total;
  384. $data['total_score'] = $total_score;
  385. return TestPaperModel::edit($data, $id);
  386. } else {
  387. return true;
  388. }
  389. }
  390. /**删除试卷
  391. * @param int $id
  392. */
  393. public function delete($id = 0)
  394. {
  395. if (!$id) return Json::fail('参数错误');
  396. $test = TestPaperModel::get($id);
  397. if (!$test) return Json::fail('要删除的试卷不存在');
  398. $res = parent::getDataModification('test', $id, 'is_del', 1);
  399. if ($res) {
  400. TestPaperQuestions::where('test_id', $id)->delete();
  401. return Json::successful('删除成功');
  402. } else {
  403. return Json::fail('删除失败');
  404. }
  405. }
  406. /**删除试题
  407. * @param int $id
  408. */
  409. public function TestPaperDelete($id = 0)
  410. {
  411. if (!$id) return Json::fail('参数错误');
  412. $paperQuestion = TestPaperQuestions::where('id', $id)->find();
  413. if (!$paperQuestion) return Json::fail('要删除的试题不存在');
  414. TestPaperQuestions::beginTrans();
  415. $res = TestPaperQuestions::where('id', $id)->delete();
  416. if ($res) {
  417. $res1 = $this->inspectTestQuestions($paperQuestion['test_id']);
  418. TestPaperQuestions::checkTrans($res1);
  419. if ($res1) {
  420. return Json::successful('删除成功');
  421. } else {
  422. return Json::fail('删除失败');
  423. }
  424. } else {
  425. return Json::fail('删除失败');
  426. }
  427. }
  428. /**关联证书
  429. * @param int $id
  430. */
  431. public function certificate($related_id = 0)
  432. {
  433. if (!$related_id) return Json::fail('参数错误');
  434. $certificate = CertificateRelated::where(['related' => $related_id, 'obtain' => 2])->find();
  435. if ($certificate) {
  436. $id = $certificate['id'];
  437. } else {
  438. $id = 0;
  439. $certificate = [];
  440. }
  441. $this->assign(['related_id' => $related_id, 'id' => $id, 'certificate' => json_encode($certificate)]);
  442. return $this->fetch();
  443. }
  444. /**获取对应证书
  445. * @param int $obtain
  446. */
  447. public function certificateLists($obtain = 1)
  448. {
  449. $list = Certificate::where(['is_del' => 0, 'obtain' => $obtain, 'mer_id' => $this->merchantId])->order('sort desc,add_time desc')->select();
  450. $list = count($list) > 0 ? $list->toArray() : [];
  451. return Json::successful($list);
  452. }
  453. /**试卷关联证书
  454. * @param int $id
  455. * @param int $obtain
  456. */
  457. public function certificateRecord($id = 0, $obtain = 1)
  458. {
  459. $data = parent::postMore([
  460. ['cid', 0],
  461. ['condition', ''],
  462. ['related', 0],
  463. ['is_show', 0]
  464. ]);
  465. $data['obtain'] = $obtain;
  466. $res = CertificateRelated::addCertificateRelated($data, $id);
  467. if ($res) {
  468. return Json::successful('关联成功');
  469. } else {
  470. return Json::fail('关联失败');
  471. }
  472. }
  473. }