AnswerAnalysisController.class.php
6.35 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
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
<?php
/**
* Created by PhpStorm.
* User: zoulongbo
* Date: 2018/8/7
* Time: 10:26
*/
namespace Apicp\Controller\Export;
use Com\PythonExcel;
use Common\Common\ExportDownload;
use Common\Service\TopicService;
use Common\Service\AnswerDetailService;
use Common\Service\AnswerService;
use Common\Service\PaperService;
use Common\Service\RandomSnapshotService;
use Common\Service\SnapshotService;
use Common\Service\StatisticsService;
class AnswerAnalysisController extends AbstractController
{
/** @var PaperService */
protected $paper_serv;
/** @var SnapshotService */
protected $snapshot_serv;
/** @var AnswerService */
protected $answer_serv;
/** @var AnswerDetailService */
protected $answer_detail_serv;
/** @var RandomSnapshotService */
protected $random_snapshot_serv;
/** @var StatisticsService */
protected $statistics_serv;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
$this->paper_serv = new PaperService();
$this->snapshot_serv = new SnapshotService();
$this->answer_serv = new AnswerService();
$this->answer_detail_serv = new AnswerDetailService();
$this->random_snapshot_serv = new RandomSnapshotService();
$this->statistics_serv = new StatisticsService();
return true;
}
/**
* 试题分析导出接口
* @desc 试题分析导出接口
* @param string ep_id 试卷id
* @return array
*/
public function Index_post()
{
$params = I('post.');
if (empty($params)) {
E('_ERR_PARAMS_NOT_NULL');
}
// 试卷id
$ep_id = intval($params['ep_id']);
if (!$ep_id) {
E('_EMPTY_EP_ID');
}
// 根据试卷id获取试卷详情
$paper = $this->paper_serv->get($ep_id);
if (empty($paper)) {
E('_ERR_PAPER_NOT_FOUND');
}
$list = [];
$options = [];
//当前可分析 题目类型
$et_types = [
TopicService::TOPIC_TYPE_SINGLE,
TopicService::TOPIC_TYPE_MULTIPLE
];
$ep_cond = ['ep_id' => $ep_id];
// 查询题
$topics = $this->snapshot_serv->list_by_conds($ep_cond);
$topics = array_combine_by_key($topics, 'es_id');
$statistics = $this->statistics_serv->list_by_conds($ep_cond);
$statistics = array_combine_by_key($statistics, 'esr_id');
//循环大法 开始组装数据
foreach ($topics as $key => $topic) {
$et_type = $topic['et_type'];
// 单选、判断、多选,统计各选项使用率
if (in_array($et_type, $et_types)) {
if (!empty($statistics[$key])) {
$this->get_options($topic, $statistics[$key], $options);
$count = $statistics[$key]['answer_num'];
$list[] = [
'id' => $topic['order_num'],
'title' => $topic['title'],
'et_type' => $topic['et_type'],
'count' => intval($count),
'answer' => $topic['answer'],
'right_rate' => $topic['right_rate'],
'options' => $options,
];
}
}
}
//print_r($list);die();
// 导出结果
$this->_download($list);
}
/**
* 组装options数据
*
* @param $et_type int 试题类型
* @param $topic array 试题快照
* @param $statistics array 试题统计
* @param $options array 需要格式化的数据
*
* @return bool
*/
protected function get_options(&$topic, $statistics, &$options)
{
if (!empty($statistics)) {
$options_count = unserialize($statistics['options']);
$answer_count = array_sum($options_count);
if (intval($answer_count) == 0) {
$answer_count = 1; // 被除数不能为0
}
// 选择题
$options_data = unserialize($topic['options']);
// 循环组装选项数据
foreach ($options_data as $k => $v) {
$options[$k]['option_name'] = $v['option_name'];
$options[$k]['count'] = $options_count[$v['option_name']];
$rate = $options_count[$v['option_name']] / $answer_count;
$options[$k]['percentage'] = round($rate * 100, 2) . '%';
if ($topic['answer'] == $options[$k]['option_name']) {
$topic['right_rate'] = $options[$k]['percentage'];
}
}
}
return true;
}
/**
* 导出模板
*
* @param array $list 列表数据
* @param int $watch_type 导出类型
*/
private function _download($list = [])
{
$row_data = [];
$file_name = '试题分析' . date('_YmdHi');
$title = array(
'题目序号',
'正确率',
'正确答案'
);
$option_titles = [];
foreach ($list as $k => $v) {
$row_data[$k] = [
$v['id'],
$v['right_rate'],
$v['answer']
];
foreach ($v['options'] as $key => $value) {
//获取 数据中操作选项
$option_titles[] = $value['option_name'];
//获取操作选项 个数
array_push($row_data[$k], $value['count']);
}
}
$option_titles = array_unique($option_titles);
sort($option_titles);
//去除 重复选项
$title = array_merge($title, $option_titles);
// Python导出excel
$realpath = ExportDownload::get_down_dir($this->_login->user['eaId'] . microtime(true)) . $file_name . ".xls";
$ret = PythonExcel::instance()->write($realpath, $title, $row_data);
if ($ret) {
$conditon = [
'title' => $file_name,
'ea_id' => $this->_login->user['eaId'],
'username' => $this->_login->user['eaRealname'],
'type' => ExportDownload::EXCEL_TYPE,
'url' => $realpath
];
ExportDownload::insert_down_load($conditon);
}
return true;
}
}