Question.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. if ($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. $this->error('收藏失败');
  123. }
  124. /**
  125. * 取消收藏
  126. */
  127. public function collectCancel()
  128. {
  129. if (!$question_id = input('question_id/d', 0)) {
  130. $this->error('缺少题目ID');
  131. }
  132. $result = QuestionCollectModel::where('question_id', $question_id)->where('user_id', $this->auth->id)->delete();
  133. if ($result) {
  134. $this->success('取消成功');
  135. }
  136. $this->error('取消失败');
  137. }
  138. /**
  139. * 获取错题列表
  140. */
  141. public function wrongList()
  142. {
  143. $user_id = $this->auth->id;
  144. if ($ids = input('question_ids')) {
  145. $total = QuestionWrongModel::where('user_id', $user_id)->whereIn('question_id', $ids)->count();
  146. $list = $total ? QuestionWrongModel::with('question')
  147. ->whereIn('question_id', $ids)
  148. ->where('user_id', $user_id)
  149. ->order('id desc')
  150. ->paginate(999, true)->toArray() : [];
  151. } else {
  152. $total = QuestionWrongModel::where('user_id', $user_id)->count();
  153. $list = $total ? QuestionWrongModel::with('question')
  154. ->where('user_id', $user_id)
  155. ->order('id desc')
  156. ->paginate(999, true)->toArray() : [];
  157. }
  158. if (isset($list['data']) && $list['data']) {
  159. $questions = [];
  160. foreach ($list['data'] as $item) {
  161. $questions[] = array_merge($item['question'], ['wrong_id' => $item['id'], 'user_answer' => $item['user_answer']]);
  162. }
  163. $list['data'] = \addons\exam\model\QuestionModel::isCollected($user_id, $questions);
  164. } else {
  165. $list['data'] = [];
  166. }
  167. $this->success('', compact('list', 'total'));
  168. }
  169. /*
  170. * 记录错题
  171. */
  172. public function wrongAdd()
  173. {
  174. if (!$question_id = input('question_id/d', 0)) {
  175. $this->error('缺少题目ID');
  176. }
  177. if (QuestionWrongModel::add($this->auth->id, $question_id))
  178. $this->success('记录成功');
  179. else
  180. $this->error('记录失败');
  181. }
  182. /**
  183. * 删除错题
  184. */
  185. public function wrongDelete()
  186. {
  187. if (!$question_id = input('question_id/d', 0)) {
  188. $this->success('缺少错题ID');
  189. }
  190. if (QuestionWrongModel::where('question_id', $question_id)->where('user_id', $this->auth->id)->delete()) {
  191. $this->success('删除成功');
  192. }
  193. $this->error('删除失败');
  194. }
  195. /**
  196. * 清空所有错题
  197. */
  198. public function wrongClear()
  199. {
  200. if (QuestionWrongModel::where('user_id', $this->auth->id)->delete()) {
  201. $this->success('删除成功');
  202. }
  203. $this->error('删除失败');
  204. }
  205. }