SnapshotService.class.php
8.51 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* 考试-试卷快照表
* @author: houyingcai
* @email: 594609175@qq.com
* @date : 2017-05-19 17:49:26
* @version $Id$
*/
namespace Common\Service;
use Common\Common\ExamHelper;
use Common\Model\PaperModel;
use Common\Model\PaperTempModel;
use Common\Model\SnapshotModel;
use Common\Model\TopicModel;
class SnapshotService extends AbstractService
{
/** @var TopicModel */
protected $_d_topic;
/** @var PaperModel */
protected $_d_paper;
/** @var PaperTempModel */
protected $_d_temp;
// 构造方法
public function __construct()
{
$this->_d = new SnapshotModel();
$this->_d_topic = new TopicModel();
$this->_d_paper = new PaperModel();
$this->_d_temp = new PaperTempModel();
parent::__construct();
}
/**
* 格式化后台试题列表
*
* @author daijun
*
* @param array $list
* @param array $info
*
* @return array
*/
public function format_admin_list($list = [], $info = [])
{
$return_list = [];
if (empty($list)) {
return $return_list;
}
$exam_help = new ExamHelper();
foreach ($list as $k => $v) {
$return_list[$k]['et_id'] = intval($v['et_id']);
$return_list[$k]['et_type'] = intval($v['et_type']);
$return_list[$k]['title'] = $v['title'];
$return_list[$k]['score'] = intval($v['score']);
// 处理图片数据
$img_list = [];
if (!empty($v['title_pic'])) {
$img_data = explode(',', $v['title_pic']);
foreach ($img_data as $_k => $_v) {
$img_list[$_k]['img_url'] = imgUrlReal($_v);
}
}
$return_list[$k]['title_pic_list'] = $img_list;
// 处理选项数据
$options_data = [];
if (!empty($v['options'])) {
$options_list = unserialize($v['options']);
// 循环组装选项数据
foreach ($options_list as $_k => $_v) {
$options_data[$_k]['option_name'] = $_v['option_name'];
$options_data[$_k]['option_value'] = $_v['option_value'];
$options_data[$_k]['option_image_id'] = $_v['option_image_id'] ? $_v['option_image_id'] : '';
if (!empty($_v['option_image_id'])) {
$options_data[$_k]['option_image_url'] = imgUrlReal($_v['option_image_id']);
} else {
$options_data[$_k]['option_image_url'] = '';
}
}
}
$return_list[$k]['options'] = $options_data;
// 如果是主观题 2:判断题 3:问答题 5:语音题
if (in_array($v['et_type'], [self::TOPIC_TYPE_JUDGMENT, self::TOPIC_TYPE_QUESTION, self::TOPIC_TYPE_VOICE])) {
$answer = $v['answer'];
} else {
// 如果启用了选项打乱,则需转换正确答案
$options_index = implode(',', array_column($options_data, 'option_name'));
$answer = $exam_help->chang_answer_option($options_index, $v['answer']);
}
$return_list[$k]['answer'] = $answer;
$return_list[$k]['answer_resolve'] = $v['answer_resolve'];
$return_list[$k]['order_num'] = intval($v['order_num']);
}
return $return_list;
}
/**
* 获取已选题列表
*
* @author:daijun
*
* @param int $ep_id 试卷ID
* @param string $fields 查询的字段
*
* @return array
*/
public function get_snapshot_list($ep_id = 0, $fields = 'et_id,title')
{
// 查询已选题列表
$list = $this->_d->list_by_conds(['ep_id' => $ep_id], null, ['order_num' => 'ASC'], $fields);
// 格式化数据
foreach ($list as $k => $v) {
$list[$k]['et_id'] = intval($v['et_id']);
}
return $list;
}
/**
* 将选择的试题存入试题快照表
*
* @author:daijun
*
* @param array $param
*
* @return bool
*/
public function add($param = [])
{
// 验证试卷ID
if (empty($param['ep_id'])) {
E('_EMPTY_EP_ID');
}
// 验证所选题目列表
if (empty($param['topic_list'])) {
E('_EMPTY_CHOICE_ETID');
}
// 获取试题ID集合
$et_ids = array_column($param['topic_list'], 'et_id');
if (empty($et_ids)) {
E('_EMPTY_CHOICE_ETID');
}
// 获取试卷详情
$paper = $this->_d_paper->get($param['ep_id']);
// 获取题目列表
$topic_list = $this->_d_topic->list_by_conds(['et_id' => $et_ids], null, [],
'et_id,et_type,title,title_pic,score,options,answer,answer_resolve,answer_coverage,match_type,answer_keyword');
$topic_list = array_combine_by_key($topic_list, 'et_id');
// 试卷总分
$total_score = 0;
$snapshot = [];
// 出题规则:自主选题
if ($paper['ep_type'] == self::TOPIC_CUSTOMER) {
// 组装试卷编号
$i = self::ORDER_NUM;
// 获取试卷临时备选题目列表
$paper_temp_list = $this->_d_temp->list_by_conds(
['ep_id' => $param['ep_id'], 'et_id' => $et_ids],
null,
[],
'ep_id,et_id,score'
);
$paper_temp_list = array_column($paper_temp_list, null, 'et_id');
// 自主选题,分数按照题目分数计算
foreach ($et_ids as $v) {
$temp_item = $paper_temp_list[$v];
$total_score += intval($temp_item['score']);
// 组装试题序号
$arr = $topic_list[$v];
$arr['score'] = $temp_item['score'];
$arr['ep_id'] = $param['ep_id'];
$arr['order_num'] = $i;
$snapshot[] = $arr;
$i++;
}
} else {
// 规则抽题,分数按照rule字段配置的计算
$rule_data = unserialize($paper['rule']);
// 组装试卷编号
$k = self::ORDER_NUM;
foreach ($et_ids as $v) {
$arr = $topic_list[$v];
// 获取试题配置分数
$arr['score'] = $this->get_score_by_type($arr['et_type'], $rule_data);
$total_score += intval($arr['score']);
$arr['ep_id'] = $param['ep_id'];
// 组装试题序号
$arr['order_num'] = $k;
$snapshot[] = $arr;
$k++;
}
}
if (intval($total_score) == 0) {
// 试卷总分不能为0
return false;
}
try {
// 开始事务
$this->start_trans();
// 删除之前存入的试题
$this->_d->delete_by_conds(['ep_id' => $param['ep_id']]);
// 执行数据插入操作
$this->_d->insert_all($snapshot);
// 更新试卷总分
$this->_d_paper->update($param['ep_id'], ['total_score' => $total_score]);
// 提交事务
$this->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
// 事务回滚
$this->_set_error($e->getMessage(), $e->getCode());
$this->rollback();
return false;
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_set_error($e->getMessage(), $e->getCode());
// 事务回滚
$this->rollback();
return false;
}
return true;
}
/**
* 获取试题快照列表
*
* @author caijianhua
*
* @param array $conds 查询条件参数列表
* @param array $page_option 分页参数
* @param array $order_option 排序参数
* @param string $fields 返回字段
*
* @return array|bool
*/
public function conds_snapshot_list($conds, $page_option = null, $order_option = [], $fields = '*')
{
return $this->_d->conds_snapshot_list($conds, $page_option, $order_option, $fields);
}
/**
* 获取试题快照总数
*
* @author caijainhua
*
* @param array $conds 查询条件参数列表
*
* @return int
*/
public function conds_snapshot_count($conds)
{
return $this->_d->conds_snapshot_count($conds);
}
}