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