ImgDataUpdateController.class.php
4.09 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
<?php
/**
* 修正调研老数据(多选单选支持多张图片问题)
* Auth:dj
* Date:2017-10-19
*/
namespace Frontend\Controller\Index;
use Common\Service\QuestionService;
use Common\Service\RecordService;
class ImgDataUpdateController extends \Common\Controller\Frontend\AbstractController
{
/**
* 不是必须登录
* @var string $_require_login
*/
protected $_require_login = false;
public function Index()
{
set_time_limit(0);
ini_set("memory_limit", "1128M");
$question = new QuestionService();
// 查询多选和单选问题
$question_list = $question->list_by_conds(array('q_type' => array(1, 2)));
// 获取问题id集合
$qids = array_column($question_list, 'qid');
$record = new RecordService();
// 获取回答详情记录
$record_list = $record->list_by_conds(array('q_id' => $qids));
try {
// 开始事务
$question->start_trans();
// 循环更新问题信息
foreach ($question_list as $v) {
$field = unserialize($v['q_field']);
$field_new = array();
foreach ($field as $_v) {
if (isset($_v['img_options'])) {
// 如果是新数据
continue;
}
$img_options = array();
if (!empty($_v['option_img'])) {
$img_options = array(
array(
'option_img' => strval($_v['option_img']),
'option_img_url' => strval($_v['option_img_url'])
),
);
}
$field_new[] = array(
'option' => strval($_v['option']),
'img_options' =>$img_options
);
}
if(empty($field_new)){
continue;
}
// 组装更新选项
$update_data = array(
'q_field' => serialize($field_new)
);
// 更新问题数据
$question->update($v['qid'], $update_data);
}
// 循环更新回答详情数据
foreach ($record_list as $v) {
if (empty($v['answer'])) {
// 如果答案为空
continue;
}
// 反序列化回答数据
$answer = unserialize($v['answer']);
$answer_new = array();
foreach ($answer as $_v) {
if (isset($_v['img_options'])) {
// 如果是新数据
continue;
}
$img_options = array();
if (!empty($_v['option_img'])) {
$img_options = array(
array(
'option_img' => strval($_v['option_img']),
'option_img_url' => strval($_v['option_img_url'])
),
);
}
$answer_new[] = array(
'option' => strval($_v['option']),
'is_check' => intval($_v['is_check']),
'img_options'=>$img_options
);
}
if(empty($answer_new)){
continue;
}
// 组装更新答案
$record_data = array(
'answer' => serialize($answer_new)
);
// 更新答卷记录数据
$record->update($v['qr_id'], $record_data);
}
// 提交事务
$question->commit();
} catch (\Exception $e) {
// 事务回滚
$question->rollback();
exit('数据更新失败');
}
exit('数据更新成功');
}
}