Question.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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()
  75. {
  76. $id = input('id');
  77. $this->success('', (new QuestionModel)->get($id));
  78. }
  79. /**
  80. * 收藏列表
  81. */
  82. public function collectList()
  83. {
  84. $user_id = $this->auth->id;
  85. $collectQuestion = new QuestionCollectModel();
  86. $list = $collectQuestion::with('question')->where('user_id', $user_id)->order('id desc')->paginate(999, true);
  87. $total = $collectQuestion::where('user_id', $user_id)->count();
  88. $this->success('', compact('list', 'total'));
  89. // $list = $collectQuestion::with([
  90. // 'question.materialQuestions.question'
  91. // ])->where('user_id', $user_id)->order('id desc')->paginate(999, true);
  92. // $total = $list->count();
  93. //
  94. // $list = $list->toArray();
  95. // ddd($list);
  96. // // 合并材料题子题目
  97. // $list['data'] = QuestionModel::mergeMaterialQuestions($list['data']);
  98. // $this->success('', compact('list', 'total'));
  99. }
  100. /**
  101. * 添加收藏
  102. */
  103. public function collectAdd()
  104. {
  105. if (!$question_id = input('question_id/d', 0)) {
  106. $this->error('缺少题目ID');
  107. }
  108. if (!QuestionModel::where('id', $question_id)->count()) {
  109. $this->error('题目数据不存在');
  110. }
  111. if ($res = QuestionCollectModel::updateOrCreate(
  112. [
  113. 'user_id' => $this->auth->id,
  114. 'question_id' => $question_id
  115. ],
  116. [
  117. 'user_id' => $this->auth->id,
  118. 'question_id' => $question_id
  119. ])
  120. ) {
  121. $this->success('收藏成功', $res);
  122. }
  123. $this->error('收藏失败');
  124. }
  125. /**
  126. * 取消收藏
  127. */
  128. public function collectCancel()
  129. {
  130. if (!$question_id = input('question_id/d', 0)) {
  131. $this->error('缺少题目ID');
  132. }
  133. $result = QuestionCollectModel::where('question_id', $question_id)->where('user_id', $this->auth->id)->delete();
  134. if ($result) {
  135. $this->success('取消成功');
  136. }
  137. $this->error('取消失败');
  138. }
  139. /**
  140. * 获取错题列表
  141. */
  142. public function wrongList()
  143. {
  144. $user_id = $this->auth->id;
  145. if ($ids = input('question_ids')) {
  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. ->order('id desc')
  151. ->paginate(999, true)->toArray() : [];
  152. } else {
  153. $total = QuestionWrongModel::where('user_id', $user_id)->count();
  154. $list = $total ? QuestionWrongModel::with('question')
  155. ->where('user_id', $user_id)
  156. ->order('id desc')
  157. ->paginate(999, true)->toArray() : [];
  158. }
  159. if (isset($list['data']) && $list['data']) {
  160. $questions = [];
  161. foreach ($list['data'] as $item) {
  162. $questions[] = array_merge($item['question'], ['wrong_id' => $item['id'], 'user_answer' => $item['user_answer']]);
  163. }
  164. // $list['data'] = \addons\exam\model\QuestionModel::isCollected($user_id, $questions);
  165. $list['data'] = $questions;
  166. } else {
  167. $list['data'] = [];
  168. }
  169. $this->success('', compact('list', 'total'));
  170. }
  171. /*
  172. * 记录错题
  173. */
  174. public function wrongAdd()
  175. {
  176. if (!$question_id = input('question_id/d', 0)) {
  177. $this->error('缺少题目ID');
  178. }
  179. if (QuestionWrongModel::add($this->auth->id, $question_id))
  180. $this->success('记录成功');
  181. else
  182. $this->error('记录失败');
  183. }
  184. /**
  185. * 删除错题
  186. */
  187. public function wrongDelete()
  188. {
  189. if (!$question_id = input('question_id/d', 0)) {
  190. $this->success('缺少错题ID');
  191. }
  192. if (QuestionWrongModel::where('question_id', $question_id)->where('user_id', $this->auth->id)->delete()) {
  193. $this->success('删除成功');
  194. }
  195. $this->error('删除失败');
  196. }
  197. /**
  198. * 清空所有错题
  199. */
  200. public function wrongClear()
  201. {
  202. if (QuestionWrongModel::where('user_id', $this->auth->id)->delete()) {
  203. $this->success('删除成功');
  204. }
  205. $this->error('删除失败');
  206. }
  207. }