Question.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace addons\exam\controller;
  3. use addons\exam\enum\CommonStatus;
  4. use addons\exam\model\QuestionCollectModel;
  5. use addons\exam\model\QuestionModel;
  6. use addons\exam\model\UserModel;
  7. use app\admin\model\exam\QuestionWrongModel;
  8. /**
  9. * 试题接口
  10. */
  11. class Question extends Base
  12. {
  13. protected $noNeedLogin = [];
  14. protected $noNeedRight = ['*'];
  15. /**
  16. * 看题模式
  17. */
  18. public function lookPage()
  19. {
  20. $model = new QuestionModel();
  21. $total = $model->where('cate_id', $this->request->param('cate_id'))->count('id');
  22. $this->success('', compact('total'));
  23. }
  24. /**
  25. * 练习模式
  26. */
  27. public function train()
  28. {
  29. $param = $this->request->param();
  30. $param['user_id'] = $this->auth->id;
  31. // 验证是否需要绑定手机号
  32. UserModel::isMustBindMobile($this->auth->getUser());
  33. $list = QuestionModel::getList($param);
  34. // $total = $list['total'];
  35. $this->success('', $list);
  36. }
  37. /**
  38. * 根据关键词模糊查询10条题目
  39. */
  40. public function search()
  41. {
  42. $query = QuestionModel::with(
  43. [
  44. 'cates' => function ($query) {
  45. $query->field('id,name');
  46. },
  47. 'materialParent' => function ($query) {
  48. $query->field('id,title');
  49. },
  50. ]
  51. )->where('status', CommonStatus::NORMAL);
  52. if ($keyword = input('keyword/s', '', 'trim,strip_tags,htmlspecialchars,xss_clean')) {
  53. if (mb_strlen($keyword) < 2) {
  54. $this->error('请输入不少于2个字的关键词进行搜索');
  55. }
  56. $query->where('title', 'like', '%' . $keyword . '%');
  57. }
  58. if ($sort_type = input('sort_type/s')) {
  59. $query->order($sort_type);
  60. }
  61. if (input('sort_rand/d', 0)) {
  62. $query->orderRaw('rand()');
  63. }
  64. $list = $query->paginate(15, true)->toArray();
  65. // 最多搜索5页
  66. if (input('page/d') >= 5) {
  67. $list['has_more'] = false;
  68. }
  69. $this->success('', ['list' => $list]);
  70. }
  71. /**
  72. * 试题详情
  73. */
  74. public function detail($id)
  75. {
  76. $this->success('', (new QuestionModel)->get($id));
  77. }
  78. /**
  79. * 收藏列表
  80. */
  81. public function collectList()
  82. {
  83. $user_id = $this->auth->id;
  84. $collectQuestion = new QuestionCollectModel();
  85. $list = $collectQuestion::with('question')->where('user_id', $user_id)->order('id desc')->paginate(999, true);
  86. $total = $collectQuestion::where('user_id', $user_id)->count();
  87. $this->success('', compact('list', 'total'));
  88. // $list = $collectQuestion::with([
  89. // 'question.materialQuestions.question'
  90. // ])->where('user_id', $user_id)->order('id desc')->paginate(999, true);
  91. // $total = $list->count();
  92. //
  93. // $list = $list->toArray();
  94. // ddd($list);
  95. // // 合并材料题子题目
  96. // $list['data'] = QuestionModel::mergeMaterialQuestions($list['data']);
  97. // $this->success('', compact('list', 'total'));
  98. }
  99. /**
  100. * 添加收藏
  101. */
  102. public function collectAdd()
  103. {
  104. if (!$question_id = input('question_id/d', 0)) {
  105. $this->error('缺少题目ID');
  106. }
  107. if (!QuestionModel::where('id', $question_id)->count()) {
  108. $this->error('题目数据不存在');
  109. }
  110. $res = QuestionCollectModel::updateOrCreate(
  111. [
  112. 'user_id' => $this->auth->id,
  113. 'question_id' => $question_id,
  114. ],
  115. [
  116. 'user_id' => $this->auth->id,
  117. 'question_id' => $question_id,
  118. ]
  119. );
  120. $this->success('收藏成功', $res);
  121. }
  122. /**
  123. * 取消收藏
  124. */
  125. public function collectCancel()
  126. {
  127. if (!$question_id = input('question_id/d', 0)) {
  128. $this->error('缺少题目ID');
  129. }
  130. QuestionCollectModel::where('question_id', $question_id)->where('user_id', $this->auth->id)->delete();
  131. $this->success('取消收藏成功');
  132. }
  133. /**
  134. * 获取错题列表
  135. */
  136. public function wrongList()
  137. {
  138. $user_id = $this->auth->id;
  139. if ($ids = input('question_ids')) {
  140. $ids = explode(',', $ids);
  141. // 必须是int类型
  142. $ids = array_filter(array_map('intval', $ids));
  143. if (!$ids) {
  144. $this->error('题目ID有误');
  145. }
  146. $total = QuestionWrongModel::where('user_id', $user_id)->whereIn('question_id', $ids)->count();
  147. $list = $total ? QuestionWrongModel::with('question')
  148. ->whereIn('question_id', $ids)
  149. ->where('user_id', $user_id)
  150. ->orderRaw("find_in_set(question_id, '" . implode(',', $ids) . "')")// 保持原有顺序
  151. // ->order('id desc')
  152. ->paginate(999, true)->toArray() : [];
  153. } else {
  154. $total = QuestionWrongModel::where('user_id', $user_id)->count();
  155. $list = $total ? QuestionWrongModel::with('question')
  156. ->where('user_id', $user_id)
  157. ->order('id desc')
  158. ->paginate(999, true)->toArray() : [];
  159. }
  160. if (isset($list['data']) && $list['data']) {
  161. $questions = [];
  162. foreach ($list['data'] as $item) {
  163. $questions[] = array_merge($item['question'], [
  164. 'wrong_id' => $item['id'],
  165. 'user_answer' => $item['user_answer'],
  166. 'source' => $item['kind'],
  167. ]);
  168. }
  169. $list['data'] = \addons\exam\model\QuestionModel::isCollected($user_id, $questions);
  170. } else {
  171. $list['data'] = [];
  172. }
  173. $this->success('', compact('list', 'total'));
  174. }
  175. /**
  176. * 记录错题
  177. */
  178. public function wrongAdd()
  179. {
  180. if (!$question_id = input('question_id/d', 0)) {
  181. $this->error('缺少题目ID');
  182. }
  183. $question = QuestionModel::get($question_id);
  184. if (!$question) {
  185. $this->error('题目数据不存在');
  186. }
  187. $source = input('source/s', 'TRAINING');
  188. if (in_array($question['kind'], ['FILL', 'SHORT'])) {
  189. $user_answer = input('user_answer/a', []);
  190. } else {
  191. $user_answer = input('user_answer/s', '');
  192. }
  193. QuestionModel::recordWrong($question['kind'], $question_id, $this->auth->id, $user_answer, $source, [
  194. 'cate_id' => $question['cate_id'],
  195. ]);
  196. $this->success('记录成功');
  197. }
  198. /**
  199. * 删除错题
  200. */
  201. public function wrongDelete()
  202. {
  203. if (!$question_id = input('question_id/d', 0)) {
  204. $this->success('缺少错题ID');
  205. }
  206. QuestionWrongModel::where('question_id', $question_id)->where('user_id', $this->auth->id)->delete();
  207. $this->success('删除成功');
  208. }
  209. /**
  210. * 清空所有错题
  211. */
  212. public function wrongClear()
  213. {
  214. QuestionWrongModel::where('user_id', $this->auth->id)->delete();
  215. $this->success('删除成功');
  216. }
  217. }