TestPaper.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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\admin\controller\questions;
  12. use app\admin\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\admin\model\questions\TestPaper as TestPaperModel;
  19. use app\admin\model\questions\TestPaperCategory;
  20. use app\admin\model\questions\QuestionsCategpry;
  21. use app\admin\model\questions\Questions;
  22. use app\admin\model\questions\TestPaperQuestions;
  23. use app\admin\model\questions\TestPaperScoreGrade;
  24. use app\admin\model\questions\TestPaperObtain;
  25. use app\admin\model\questions\Certificate;
  26. use app\admin\model\questions\CertificateRelated;
  27. use app\admin\model\questions\ExaminationRecord;
  28. use app\admin\model\special\Special;
  29. use app\admin\model\system\Recommend;
  30. use app\admin\model\system\RecommendRelation;
  31. use app\admin\model\merchant\Merchant;
  32. use app\admin\model\system\WebRecommend;
  33. use app\admin\model\system\WebRecommendRelation;
  34. use app\admin\model\user\User;
  35. /**
  36. * 试卷
  37. * Class TestPaper
  38. */
  39. class TestPaper extends AuthController
  40. {
  41. /**
  42. * 试卷列表
  43. */
  44. public function index($type = 1)
  45. {
  46. $this->assign([
  47. 'type' => $type,
  48. 'mer_list' => Merchant::getMerchantList(),
  49. 'category' => TestPaperCategory::taskCategoryAll(2, $type)
  50. ]);
  51. return $this->fetch();
  52. }
  53. /**
  54. * 获取试卷列表
  55. */
  56. public function getTestPaperExercisesList($type = 1)
  57. {
  58. $where = parent::getMore([
  59. ['page', 1],
  60. ['limit', 20],
  61. ['pid', 0],
  62. ['is_show', ''],
  63. ['status', 1],
  64. ['mer_id', 0],
  65. ['title', '']
  66. ]);
  67. $where['type'] = $type;
  68. return Json::successlayui(TestPaperModel::testPaperExercisesList($where));
  69. }
  70. /**资料审核
  71. * @return mixed
  72. */
  73. public function examine($type = 1)
  74. {
  75. $category = TestPaperCategory::taskCategoryAll(2, $type);
  76. $mer_list = Merchant::getMerchantList();
  77. $this->assign([
  78. 'type' => $type,
  79. 'category' => $category,
  80. 'mer_list' => $mer_list
  81. ]);
  82. return $this->fetch();
  83. }
  84. /**获得审核资料
  85. * @throws \think\Exception
  86. */
  87. public function get_test_paper_examine_list($type = 1)
  88. {
  89. $where = parent::getMore([
  90. ['page', 1],
  91. ['limit', 20],
  92. ['pid', 0],
  93. ['is_show', ''],
  94. ['status', ''],
  95. ['mer_id', 0],
  96. ['title', '']
  97. ]);
  98. $where['type'] = $type;
  99. return Json::successlayui(TestPaperModel::testPaperExercisesExamineList($where));
  100. }
  101. /**审核
  102. * @param $id
  103. * @param $type
  104. * @return mixed|void
  105. * @throws \think\exception\DbException
  106. */
  107. public function examineDetails($id,$type)
  108. {
  109. if (!$id) return Json::fail('参数错误');
  110. $details = TestPaperModel::get($id);
  111. if (!$details) return Json::fail('试卷不存在');
  112. $grade = [];
  113. if ($details && $id) {
  114. $single_tmp_list = TestPaperQuestions::gettestPaperQuestions($id, 1);
  115. $many_tmp_list = TestPaperQuestions::gettestPaperQuestions($id, 2);
  116. $judge_tmp_list = TestPaperQuestions::gettestPaperQuestions($id, 3);
  117. if ($type == 2) $grade = TestPaperScoreGrade::testPaperScoreGradeList($id);
  118. } else {
  119. $single_tmp_list = [];
  120. $many_tmp_list = [];
  121. $judge_tmp_list = [];
  122. }
  123. $this->assign([
  124. 'type' => $type,
  125. 'details' => json_encode($details),
  126. 'grade' => json_encode($grade),
  127. 'single_tmp_list' => json_encode($single_tmp_list),
  128. 'many_tmp_list' => json_encode($many_tmp_list),
  129. 'judge_tmp_list' => json_encode($judge_tmp_list)
  130. ]);
  131. return $this->fetch('material');
  132. }
  133. /**不通过
  134. * @param $id
  135. * @throws \think\exception\DbException
  136. */
  137. public function fail($id)
  138. {
  139. $fail_msg = parent::postMore([
  140. ['message', ''],
  141. ]);
  142. if (!TestPaperModel::be(['id' => $id, 'status' => 0])) return Json::fail('操作记录不存在或状态错误!');
  143. $test = TestPaperModel::get($id);
  144. if (!$test) return Json::fail('操作记录不存!');
  145. if ($test->status != 0) return Json::fail('您已审核,请勿重复操作');
  146. TestPaperModel::beginTrans();
  147. $res = TestPaperModel::changeFail($id, $test['mer_id'], $fail_msg['message']);
  148. if ($res) {
  149. TestPaperModel::commitTrans();
  150. return Json::successful('操作成功!');
  151. } else {
  152. TestPaperModel::rollbackTrans();
  153. return Json::fail('操作失败!');
  154. }
  155. }
  156. /**通过
  157. * @param $id
  158. * @throws \think\exception\DbException
  159. */
  160. public function succ($id)
  161. {
  162. if (!TestPaperModel::be(['id' => $id, 'status' => 0])) return Json::fail('操作记录不存在或状态错误!');
  163. $test = TestPaperModel::get($id);
  164. if (!$test) return Json::fail('操作记录不存!');
  165. if ($test->status != 0) return Json::fail('您已审核,请勿重复操作');
  166. TestPaperModel::beginTrans();
  167. $res = TestPaperModel::changeSuccess($id, $test['mer_id']);
  168. if ($res) {
  169. TestPaperModel::commitTrans();
  170. return Json::successful('操作成功!');
  171. } else {
  172. TestPaperModel::rollbackTrans();
  173. return Json::fail('操作失败!');
  174. }
  175. }
  176. /**
  177. * 用户答题记录
  178. */
  179. public function answerNotes($type = 1, $test_id = 0, $uid = 0)
  180. {
  181. $this->assign(['type' => $type, 'test_id' => $test_id, 'uid' => $uid, 'testPaper' => TestPaperModel::testPaperList($type)]);
  182. return $this->fetch('record');
  183. }
  184. /**
  185. * 获取试卷列表
  186. */
  187. public function getExaminationRecords($type = 1, $testId = 0, $uid = 0)
  188. {
  189. $where = parent::getMore([
  190. ['page', 1],
  191. ['limit', 20],
  192. ['test_id', 0],
  193. ['title', ''],
  194. ['excel', 0]
  195. ]);
  196. $where['type'] = $type;
  197. if ($testId) $where['test_id'] = $testId;
  198. if ($uid) $where['uid'] = $uid;
  199. return Json::successlayui(ExaminationRecord::getExaminationRecord($where));
  200. }
  201. /**
  202. * 用户答题
  203. */
  204. public function answers($record_id = 0, $test_id = 0, $type = 1, $uid =0)
  205. {
  206. $dat=TestPaperModel::where('id',$test_id)->field('single_sort,many_sort,judge_sort')->find();
  207. $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']]);
  208. return $this->fetch();
  209. }
  210. /**答题信息
  211. * @param $uid
  212. * @return void
  213. * @throws \think\db\exception\DataNotFoundException
  214. * @throws \think\db\exception\ModelNotFoundException
  215. * @throws \think\exception\DbException
  216. */
  217. public function getUserInformation($uid)
  218. {
  219. if(!$uid) return Json::fail('参数错误');
  220. $data=User::where(['uid'=>$uid])->field('nickname,name,uid,avatar')->find();
  221. return Json::successful($data);
  222. }
  223. /**成绩
  224. * @param int $record_id
  225. * @param int $test_id
  226. * @param int $uid
  227. * @throws \think\db\exception\DataNotFoundException
  228. * @throws \think\db\exception\ModelNotFoundException
  229. * @throws \think\exception\DbException
  230. */
  231. public function getUserAchievement($record_id=0,$test_id=0,$uid=0)
  232. {
  233. if(!$record_id || !$test_id || !$uid) return Json::fail('参数错误');
  234. $dat=TestPaperModel::where('id',$test_id)->field('title,item_number,total_score')->find();
  235. $record=ExaminationRecord::where(['id'=>$record_id,'test_id'=>$test_id,'uid'=>$uid,'type'=>2])->find();
  236. $data['title']=$dat['title'];
  237. $data['item_number']=$dat['item_number'];
  238. $data['total_score']=$dat['total_score'];
  239. $data['yes_questions']=$record['yes_questions'];
  240. $data['score']=$record['score'];
  241. $data['start_time']=date('Y-m-d H:i',$record['start_time']);
  242. return Json::successful($data);
  243. }
  244. /**试卷中的试题答题情况
  245. * @param int $id
  246. * @param int $type
  247. */
  248. public function getTestPaperAnswers($test_id=0,$record_id=0,$question_type=1)
  249. {
  250. if(!$test_id || !$record_id) return Json::fail('参数错误');
  251. return Json::successful(TestPaperQuestions::getExaminationRecordAnswers($test_id,$record_id,$question_type));
  252. }
  253. /**
  254. * 快速编辑
  255. * @param string $field 字段名
  256. * @param int $id 修改的主键
  257. * @param string value 修改后的值
  258. * @return json
  259. */
  260. public function set_value($field = '', $id = '', $value = '', $test = 0)
  261. {
  262. if (!$field || !$id || $value == '') Json::fail('缺少参数3');
  263. if ($field == 'sort' && bcsub($value, 0, 0) < 0) return Json::fail('排序不能为负数');
  264. if ($test) {
  265. $model_type = 'paper';
  266. } else {
  267. $model_type = 'test';
  268. }
  269. $res = parent::getDataModification($model_type, $id, $field, $value);
  270. if ($res)
  271. return Json::successful('保存成功');
  272. else
  273. return Json::fail('保存失败');
  274. }
  275. /**关联试题 手动组题
  276. * @param int $id
  277. */
  278. public function questions($question_type = 0, $id = 1)
  279. {
  280. $this->assign(['id' => $id, 'question_type' => $question_type, 'cateList' => QuestionsCategpry::taskCategoryAll(2)]);
  281. return $this->fetch('questions');
  282. }
  283. /**
  284. * 获取题库列表
  285. */
  286. public function getTestQuestionsList($question_type = '', $id = 0)
  287. {
  288. $where = parent::getMore([
  289. ['page', 1],
  290. ['limit', 20],
  291. ['pid', 0],
  292. ['title', '']
  293. ]);
  294. $where['type'] = $question_type;
  295. $arrays = [];
  296. if ($id) {
  297. $arrays = TestPaperQuestions::where(['test_id' => $id])->column('questions_id');
  298. }
  299. $list = Questions::questionsList($where, $arrays);
  300. return Json::successlayui($list);
  301. }
  302. /**试题分类
  303. * @param int $id
  304. * @param int $type
  305. */
  306. public function cate_questions()
  307. {
  308. $list = QuestionsCategpry::taskCategoryAll(2);
  309. return Json::successful($list);
  310. }
  311. /**
  312. * 查看试卷
  313. */
  314. public function test_paper($id = 0, $type = 1)
  315. {
  316. $this->assign(['id' => $id, 'type' => $type]);
  317. return $this->fetch();
  318. }
  319. /**试卷中的试题
  320. * @param int $id
  321. * @param int $type
  322. */
  323. public function getTestPaperList($id = 0, $type = 1)
  324. {
  325. $where = parent::getMore([
  326. ['page', 1],
  327. ['limit', 20],
  328. ]);
  329. return Json::successlayui(TestPaperQuestions::getTestPaperList($where, $id, $type));
  330. }
  331. /**添加/编辑
  332. * @param int $id
  333. * @return mixed
  334. */
  335. public function add($id = 0, $type = 1)
  336. {
  337. $test = $id > 0 ? TestPaperModel::get($id) : [];
  338. $grade = [];
  339. if ($test && $id) {
  340. $single_tmp_list = TestPaperQuestions::gettestPaperQuestions($id, 1);
  341. $many_tmp_list = TestPaperQuestions::gettestPaperQuestions($id, 2);
  342. $judge_tmp_list = TestPaperQuestions::gettestPaperQuestions($id, 3);
  343. if ($type == 2) $grade = TestPaperScoreGrade::testPaperScoreGradeList($id);
  344. } else {
  345. $single_tmp_list = [];
  346. $many_tmp_list = [];
  347. $judge_tmp_list = [];
  348. }
  349. $this->assign([
  350. 'id' => $id,
  351. 'type' => $type,
  352. 'test' => json_encode($test),
  353. 'grade' => json_encode($grade),
  354. 'single_tmp_list' => json_encode($single_tmp_list),
  355. 'many_tmp_list' => json_encode($many_tmp_list),
  356. 'judge_tmp_list' => json_encode($judge_tmp_list)
  357. ]);
  358. return $this->fetch();
  359. }
  360. /**
  361. * 获取试题分类
  362. */
  363. public function add_cate_list($type = 1)
  364. {
  365. $category = TestPaperCategory::taskCategoryAll(2, $type);
  366. return Json::successful($category);
  367. }
  368. /**添加/编辑试题
  369. * @param int $id
  370. */
  371. public function save_add($id = 0, $type = 1)
  372. {
  373. $data = parent::postMore([
  374. ['title', ''],
  375. ['image', ''],
  376. ['tid', 0],
  377. ['is_show', 1],
  378. ['item_number', 0],
  379. ['total_score', 0],
  380. ['single_number', 0],
  381. ['single_score', 0],
  382. ['many_number', 0],
  383. ['many_score', 0],
  384. ['judge_number', 0],
  385. ['judge_score', 0],
  386. ['single_sort', 0],
  387. ['many_sort', 0],
  388. ['judge_sort', 0],
  389. ['txamination_time', 0],
  390. ['fake_sales', 0],
  391. ['difficulty', 1],
  392. ['pay_type', 0],
  393. ['money', 0],
  394. ['member_pay_type', 0],
  395. ['member_money', 0],
  396. ['is_score', 0],
  397. ['is_group', 1],
  398. ['cate_id', 0],
  399. ['frequency', 1],
  400. ['singleIds', ''],
  401. ['manyIds', ''],
  402. ['judgeIds', ''],
  403. ['grade', ''],
  404. ['sort', 0]
  405. ]);
  406. if ($data['tid'] <= 0) return Json::fail('请选择试题分类');
  407. if (!$data['title']) return Json::fail('请输入试卷标题');
  408. if ($type == 2 && !$data['image']) return Json::fail('请添加试卷封面图');
  409. if ($data['single_number'] < 0 || $data['many_number'] < 0 || $data['judge_number'] < 0) return Json::fail('各类型题目数量不能小于0');
  410. $data['item_number'] = bcadd($data['single_number'], bcadd($data['many_number'], $data['judge_number'], 0), 0);
  411. $total_single_score = bcmul($data['single_number'], $data['single_score'], 0);
  412. $total_many_score = bcmul($data['many_number'], $data['many_score'], 0);
  413. $total_judge_score = bcmul($data['judge_number'], $data['judge_score'], 0);
  414. $data['total_score'] = bcadd($total_single_score, bcadd($total_many_score, $total_judge_score, 0), 0);
  415. if ($data['item_number'] > 100) return Json::fail('试卷最多100道');
  416. if ($type == 1) {
  417. unset(
  418. $data['txamination_time'],
  419. $data['image'],
  420. $data['pay_type'],
  421. $data['money'],
  422. $data['member_pay_type'],
  423. $data['member_money']
  424. );
  425. }
  426. $singleIds = json_decode($data['singleIds']);
  427. $manyIds = json_decode($data['manyIds']);
  428. $judgeIds = json_decode($data['judgeIds']);
  429. $grade = json_decode($data['grade'], true);
  430. TestPaperModel::beginTrans();
  431. if ($id) {
  432. $res = TestPaperModel::edit($data, $id);
  433. $res1 = true;
  434. if ($type == 2) {
  435. $res1 = TestPaperScoreGrade::testPaperScoreGradeAdd($id, $grade);
  436. }
  437. TestPaperQuestions::where('test_id', $id)->delete();
  438. if ($data['is_group'] == 1) {
  439. $res2 = TestPaperQuestions::addTestPaperQuestions($id, $type, (int)$data['single_number'], $singleIds, $data['single_score']);
  440. $res3 = TestPaperQuestions::addTestPaperQuestions($id, $type, (int)$data['many_number'], $manyIds, $data['many_score']);
  441. $res4 = TestPaperQuestions::addTestPaperQuestions($id, $type, (int)$data['judge_number'], $judgeIds, $data['judge_score']);
  442. } else {
  443. $res2 = TestPaperQuestions::addRandomGroupQuestions($id, $type, 1, $data['cate_id'], (int)$data['single_number'], $data['single_score']);
  444. $res3 = TestPaperQuestions::addRandomGroupQuestions($id, $type, 2, $data['cate_id'], (int)$data['many_number'], $data['many_score']);
  445. $res4 = TestPaperQuestions::addRandomGroupQuestions($id, $type, 3, $data['cate_id'], (int)$data['judge_number'], $data['judge_score']);
  446. }
  447. $res5 = $this->inspectTestQuestions($id);
  448. } else {
  449. $data['type'] = $type;
  450. $data['add_time'] = time();
  451. if (TestPaperModel::be(['title' => $data['title'], 'is_del' => 0])) {
  452. return Json::fail('标题已存在!');
  453. }
  454. $res = TestPaperModel::insertGetId($data);
  455. $res1 = true;
  456. if ($type == 2) {
  457. $res1 = TestPaperScoreGrade::testPaperScoreGradeAdd($res, $grade);
  458. }
  459. if ($data['is_group'] == 1) {
  460. $res2 = TestPaperQuestions::addTestPaperQuestions($res, $type, (int)$data['single_number'], $singleIds, $data['single_score']);
  461. $res3 = TestPaperQuestions::addTestPaperQuestions($res, $type, (int)$data['many_number'], $manyIds, $data['many_score']);
  462. $res4 = TestPaperQuestions::addTestPaperQuestions($res, $type, (int)$data['judge_number'], $judgeIds, $data['judge_score']);
  463. } else {
  464. $res2 = TestPaperQuestions::addRandomGroupQuestions($res, $type, 1, $data['cate_id'], (int)$data['single_number'], $data['single_score']);
  465. $res3 = TestPaperQuestions::addRandomGroupQuestions($res, $type, 2, $data['cate_id'], (int)$data['many_number'], $data['many_score']);
  466. $res4 = TestPaperQuestions::addRandomGroupQuestions($res, $type, 3, $data['cate_id'], (int)$data['judge_number'], $data['judge_score']);
  467. }
  468. $res5 = $this->inspectTestQuestions($res);
  469. }
  470. if ($res && $res1 && $res2 && $res3 && $res4 && $res5) {
  471. TestPaperModel::commitTrans();
  472. return Json::successful('添加/编辑成功');
  473. } else {
  474. TestPaperModel::rollbackTrans();
  475. return Json::fail('添加/编辑失败');
  476. }
  477. }
  478. /**
  479. * 检查试卷试题数量
  480. */
  481. public function inspectTestQuestions($id)
  482. {
  483. if (!$id) return Json::fail('参数错误');
  484. $test = TestPaperModel::get($id);
  485. if (!$test) return Json::fail('试卷不存在');
  486. $single_number = TestPaperQuestions::testPaperQuestionsNumber($id, 1);
  487. $many_number = TestPaperQuestions::testPaperQuestionsNumber($id, 2);
  488. $judge_number = TestPaperQuestions::testPaperQuestionsNumber($id, 3);
  489. if ($single_number < $test['single_number'] || $many_number < $test['many_number'] || $judge_number < $test['judge_number']) {
  490. $total = bcadd($single_number, bcadd($many_number, $judge_number, 0), 0);
  491. $total_single_score = bcmul($single_number, $test['single_score'], 0);
  492. $total_many_score = bcmul($many_number, $test['many_score'], 0);
  493. $total_judge_score = bcmul($judge_number, $test['judge_score'], 0);
  494. $total_score = bcadd($total_single_score, bcadd($total_many_score, $total_judge_score, 0), 0);
  495. $data['single_number'] = $single_number;
  496. $data['many_number'] = $many_number;
  497. $data['judge_number'] = $judge_number;
  498. $data['item_number'] = $total;
  499. $data['total_score'] = $total_score;
  500. return TestPaperModel::edit($data, $id);
  501. } else {
  502. return true;
  503. }
  504. }
  505. /**删除试卷
  506. * @param int $id
  507. */
  508. public function delete($id = 0)
  509. {
  510. if (!$id) return Json::fail('参数错误');
  511. $test = TestPaperModel::get($id);
  512. if (!$test) return Json::fail('要删除的试卷不存在');
  513. $res = parent::getDataModification('test', $id, 'is_del', 1);
  514. if ($res) {
  515. TestPaperQuestions::where('test_id', $id)->delete();
  516. return Json::successful('删除成功');
  517. } else {
  518. return Json::fail('删除失败');
  519. }
  520. }
  521. /**删除试题
  522. * @param int $id
  523. */
  524. public function TestPaperDelete($id = 0)
  525. {
  526. if (!$id) return Json::fail('参数错误');
  527. $paperQuestion = TestPaperQuestions::where('id', $id)->find();
  528. if (!$paperQuestion) return Json::fail('要删除的试题不存在');
  529. TestPaperQuestions::beginTrans();
  530. $res = TestPaperQuestions::where('id', $id)->delete();
  531. if ($res) {
  532. $res1 = $this->inspectTestQuestions($paperQuestion['test_id']);
  533. TestPaperQuestions::checkTrans($res1);
  534. if ($res1) {
  535. return Json::successful('删除成功');
  536. } else {
  537. return Json::fail('删除失败');
  538. }
  539. } else {
  540. return Json::fail('删除失败');
  541. }
  542. }
  543. /**关联证书
  544. * @param int $id
  545. */
  546. public function certificate($related_id = 0)
  547. {
  548. if (!$related_id) return Json::fail('参数错误');
  549. $certificate = CertificateRelated::where(['related' => $related_id, 'obtain' => 2])->find();
  550. if ($certificate) {
  551. $id = $certificate['id'];
  552. } else {
  553. $id = 0;
  554. $certificate = [];
  555. }
  556. $this->assign(['related_id' => $related_id, 'id' => $id, 'certificate' => json_encode($certificate)]);
  557. return $this->fetch();
  558. }
  559. /**获取对应证书
  560. * @param int $obtain
  561. */
  562. public function certificateLists($obtain = 1)
  563. {
  564. $list = Certificate::where(['is_del' => 0, 'obtain' => $obtain])->order('sort desc,add_time desc')->select();
  565. $list = count($list) > 0 ? $list->toArray() : [];
  566. return Json::successful($list);
  567. }
  568. /**试卷关联证书
  569. * @param int $id
  570. * @param int $obtain
  571. */
  572. public function certificateRecord($id = 0, $obtain = 1)
  573. {
  574. $data = parent::postMore([
  575. ['cid', 0],
  576. ['condition', ''],
  577. ['related', 0],
  578. ['is_show', 0]
  579. ]);
  580. $data['obtain'] = $obtain;
  581. $res = CertificateRelated::addCertificateRelated($data, $id);
  582. if ($res) {
  583. return Json::successful('关联成功');
  584. } else {
  585. return Json::fail('关联失败');
  586. }
  587. }
  588. /**
  589. * 添加推荐
  590. * @param int $product_id
  591. * @return mixed
  592. * @throws \think\exception\DbException
  593. */
  594. public function recommend($test_id = 0)
  595. {
  596. if (!$test_id) $this->failed('缺少参数');
  597. $testPaper = TestPaperModel::get($test_id);
  598. if (!$testPaper) $this->failed('没有查到此试卷');
  599. if ($testPaper->is_del) $this->failed('此试卷已删除');
  600. $type = $testPaper->type;
  601. $form = Form::create(Url::build('save_recommend', ['test_id' => $test_id]), [
  602. Form::select('recommend_id', '推荐')->setOptions(function () use ($type) {
  603. $types = $type == 1 ? 11 : 12;
  604. $list = Recommend::where(['is_show' => 1, 'type' => $types])->where('is_fixed', 0)->field('title,id')->order('sort desc,add_time desc')->select();
  605. $menus = [];
  606. foreach ($list as $menu) {
  607. $menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
  608. }
  609. return $menus;
  610. })->filterable(1),
  611. Form::number('sort', '排序')->min(0)
  612. ]);
  613. $form->setMethod('post')->setTitle('推荐设置')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},800);');
  614. $this->assign(compact('form'));
  615. return $this->fetch('public/form-builder');
  616. }
  617. /**
  618. * 保存推荐
  619. * @param int $special_id
  620. * @throws \think\exception\DbException
  621. */
  622. public function save_recommend($test_id = 0)
  623. {
  624. if (!$test_id) $this->failed('缺少参数');
  625. $data = parent::postMore([
  626. ['recommend_id', 0],
  627. ['sort', 0],
  628. ]);
  629. if (!$data['recommend_id']) return Json::fail('请选择推荐');
  630. $recommend = Recommend::get($data['recommend_id']);
  631. if (!$recommend) return Json::fail('导航菜单不存在');
  632. $data['add_time'] = time();
  633. $data['type'] = $recommend->type;
  634. $data['link_id'] = $test_id;
  635. if (RecommendRelation::be(['type' => $recommend->type, 'link_id' => $test_id, 'recommend_id' => $data['recommend_id']])) return Json::fail('已推荐,请勿重复推荐');
  636. if (RecommendRelation::set($data))
  637. return Json::successful('推荐成功');
  638. else
  639. return Json::fail('推荐失败');
  640. }
  641. /**取消推荐
  642. * @param int $id
  643. */
  644. public function cancel_recommendation($id = 0, $test_id = 0)
  645. {
  646. if (!$id || !$test_id) $this->failed('缺少参数');
  647. if (RecommendRelation::be(['id' => $id, 'link_id' => $test_id])) {
  648. $res = RecommendRelation::where(['id' => $id, 'link_id' => $test_id])->delete();
  649. if ($res)
  650. return Json::successful('取消推荐成功');
  651. else
  652. return Json::fail('取消推荐失败');
  653. } else {
  654. return Json::fail('推荐不存在');
  655. }
  656. }
  657. /**
  658. * 添加推荐
  659. * @param int $data_id
  660. * @return mixed
  661. * @throws \think\exception\DbException
  662. */
  663. public function web_recommend($test_id = 0)
  664. {
  665. if (!$test_id) $this->failed('缺少参数');
  666. $testPaper = TestPaperModel::get($test_id);
  667. if (!$testPaper) $this->failed('没有查到此试卷');
  668. if ($testPaper->is_del) $this->failed('此试卷已删除');
  669. $type = $testPaper->type;
  670. $form = Form::create(Url::build('save_web_recommend', ['test_id' => $test_id]), [
  671. Form::select('recommend_id', '推荐')->setOptions(function () use ($type) {
  672. $types = $type == 1 ? 7 : 8;
  673. $model = WebRecommend::where(['is_show' => 1, 'type' => $types]);
  674. $list = $model->field('title,id')->order('sort desc,add_time desc')->select();
  675. $menus = [];
  676. foreach ($list as $menu) {
  677. $menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
  678. }
  679. return $menus;
  680. })->filterable(1),
  681. Form::number('sort', '排序'),
  682. ]);
  683. $form->setMethod('post')->setTitle('推荐设置')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},800);');
  684. $this->assign(compact('form'));
  685. return $this->fetch('public/form-builder');
  686. }
  687. /**
  688. * 保存推荐
  689. * @param int $special_id
  690. * @throws \think\exception\DbException
  691. */
  692. public function save_web_recommend($test_id = 0)
  693. {
  694. if (!$test_id) $this->failed('缺少参数');
  695. $data = parent::postMore([
  696. ['recommend_id', 0],
  697. ['sort', 0],
  698. ]);
  699. if (!$data['recommend_id']) return Json::fail('请选择推荐');
  700. $recommend = WebRecommend::get($data['recommend_id']);
  701. if (!$recommend) return Json::fail('导航菜单不存在');
  702. $data['add_time'] = time();
  703. $data['type'] = $recommend->type;
  704. $data['link_id'] = $test_id;
  705. if (WebRecommendRelation::be(['type' => $recommend->type, 'link_id' => $test_id, 'recommend_id' => $data['recommend_id']])) return Json::fail('已推荐,请勿重复推荐');
  706. if (WebRecommendRelation::set($data))
  707. return Json::successful('推荐成功');
  708. else
  709. return Json::fail('推荐失败');
  710. }
  711. /**取消推荐
  712. * @param int $id
  713. */
  714. public function cancel_web_recommendation($id = 0, $test_id = 0)
  715. {
  716. if (!$id || !$test_id) return Json::fail('缺少参数');
  717. if (WebRecommendRelation::be(['id' => $id, 'link_id' => $test_id])) {
  718. $res = WebRecommendRelation::where(['id' => $id, 'link_id' => $test_id])->delete();
  719. if ($res)
  720. return Json::successful('取消推荐成功');
  721. else
  722. return Json::fail('取消推荐失败');
  723. } else {
  724. return Json::fail('推荐不存在');
  725. }
  726. }
  727. }