UserAnswerController.php
7.6 KB
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
<?php
// +----------------------------------------------------------------------
// | 用户答题结果模块
// +----------------------------------------------------------------------
// | Author: 魏强
// +----------------------------------------------------------------------
namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use think\Db;
use func\func;
/**
* Class UserAnswerController
* @package app\admin\controller
* @adminMenuRoot(
* 'name' => '用户答题列表',
* 'action' => 'index',
* 'parent' => '',
* 'display'=> true,
* 'order' => 100,
* 'icon' => 'pencil',
* 'remark' => '用户答题列表'
* )
*/
class UserAnswerController extends AdminBaseController
{
public function index()
{
$request = $this->request->param();
$where = [];
if(!empty($request['mobile'])) {
$where['u.mobile'] = ['like', "%$request[mobile]%"];
}
!empty($request['start_time']) ? $start_time = $request['start_time'] : $start_time = 0;
!empty($request['end_time']) ? $end_time = $request['end_time'] : $end_time = time();
$list = Db::name('user_answer')
->alias('ua')
->field('ua.* ,u.user_nickname ,u.mobile')
->join('user u','ua.user_id = u.id')
->where($where)
->whereTime('ua.create_time','between',[$start_time,$end_time])
->order('ua.create_time','DESC')
->paginate(2,false,['query'=>$request])
->each(function ($item){
//用户当前优惠券情况
$user_coupon = Db::name('user_coupon')
->alias('uc')
->field('c.title ,uc.type')
->join('coupon c','uc.coupon_id = c.id')
->where('uc.user_id',$item['user_id'])
->where('uc.delete_time',0)
->select();
count($user_coupon) != 0 ? $item['user_coupon_text'] = '' : $item['user_coupon_text'] = '无优惠券';
foreach($user_coupon as $uc){
$uc['type'] == 0 ? $user_coupon_type_text = '未使用' : $user_coupon_type_text = '已使用';
$item['user_coupon_text'] .= $uc['title']."($user_coupon_type_text) ";
}
//获取题目信息
$str = trim(trim(htmlspecialchars_decode($item['str']),'['),']');
$arr_str = explode(',',$str);
$item['question_text'] = [];
$all_score = 0;
//循环查询每道题目的内容
for($i=0;$i<count($arr_str);$i++) {
$arr_str[$i] = trim($arr_str[$i], '"');
$arr_q = explode('-', $arr_str[$i]);
if (!empty($arr_q[0]) && !empty($arr_q[1])) {
//题目
$question_title = Db::name('question')->where('id', $arr_q[0])->find();
//选项
$question_choose = Db::name('question')->where('id', $arr_q[1])->find();
$all_score +=$question_choose['score'];
$item['question_text'][] = [
'order'=>$question_title['order_id'],
'title'=>$question_choose['title'],
'score'=>$question_choose['score']
];
}
}
$item['all_score'] = $all_score;
return $item;
});
$this->assign('page',$list->render());
$this->assign('list', $list);
return $this->fetch();
}
/**
* 导出答题结果
* @adminMenu(
* 'name' => '导出答题结果',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 1000,
* 'icon' => '',
* 'remark' => '导出答题结果',
* 'param' => ''
* )
*/
public function Exl(){
$request = $this->request->param();
$where = [];
if(!empty($request['mobile'])) {
$where['u.mobile'] = ['like', "%$request[mobile]%"];
}
!empty($request['start_time']) ? $start_time = $request['start_time'] : $start_time = 0;
!empty($request['end_time']) ? $end_time = $request['end_time'] : $end_time = time();
$list = Db::name('user_answer')
->alias('ua')
->field('ua.* ,u.user_nickname ,u.mobile')
->join('user u','ua.user_id = u.id')
->where($where)
->whereTime('ua.create_time','between',[$start_time,$end_time])
->order('u.create_time','DESC')
->select()
->each(function ($item){
//用户当前优惠券情况
$user_coupon = Db::name('user_coupon')
->alias('uc')
->field('c.title ,uc.type')
->join('coupon c','uc.coupon_id = c.id')
->where('uc.user_id',$item['user_id'])
->where('uc.delete_time',0)
->select();
count($user_coupon) != 0 ? $item['user_coupon_text'] = '' : $item['user_coupon_text'] = '无优惠券';
foreach($user_coupon as $uc){
$uc['type'] == 0 ? $user_coupon_type_text = '未使用' : $user_coupon_type_text = '已使用';
$item['user_coupon_text'] .= $uc['title']."($user_coupon_type_text) ";
}
//获取题目信息
$str = trim(trim(htmlspecialchars_decode($item['str']),'['),']');
$arr_str = explode(',',$str);
$item['question_text'] = [];
$all_score = 0;
for($i=0;$i<count($arr_str);$i++) {
$arr_str[$i] = trim($arr_str[$i], '"');
$arr_q = explode('-', $arr_str[$i]);
if (!empty($arr_q[0]) && !empty($arr_q[1])) {
//题目
$question_title = Db::name('question')->where('id', $arr_q[0])->find();
//选项
$question_choose = Db::name('question')->where('id', $arr_q[1])->find();
$all_score +=$question_choose['score'];
$item['question_text'][] = [
'order'=>$question_title['order_id'],
'title'=>$question_choose['title'],
'score'=>$question_choose['score']
];
}
}
$item['all_score'] = $all_score;
return $item;
});
$exl = new func();
$exl_title = ['用户昵称','用户电话','题目编号','用户答案','答题分数','答题总分数','当前优惠券信息','答题时间'];
$exl_dis = [];
foreach ($list as $li){
foreach ($li['question_text'] as $qt){
$exl_dis[] = [
$exl->filterEmoji($li['user_nickname']),
$li['mobile'],
$qt['order'],
$qt['title'],
$qt['score'],
$li['all_score'],
$li['user_coupon_text'],
date('Y-m-d H:i',$li['create_time'])
];
}
}
$exl->exportExcel($exl_title, $exl_dis, '用户答题列表', './', true);
}
}