QuestionService.class.php
33.1 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
<?php
/**
* QuestionService.class.php
* 调研问题信息表
*
* @author: dj
* @copyright: vchangyi.com
*/
namespace Common\Service;
use Common\Common\User;
use Common\Model\AnswerModel;
use Common\Model\BaseinfoModel;
use Common\Model\QuestionModel;
use Common\Model\RecordModel;
class QuestionService extends AbstractService
{
// 必填问题
const REQUIRED_YES = 2;
// 非必填问题
const REQUIRED_NO = 1;
// 含有其他选项
const OTHER_YES = 1;
// 不含有其他选项
const OTHER_NO = 0;
// 选项类型(1单选,2复选,3下拉选择,14性别选择)
const OPTION_TYPE = [1, 2, 3, 14];
// 数字,金额类型
const NUMBER_TYPE = [7, 8];
// 段落类型
const NOTE_TYPE = 12;
// 多行文本
const TXT_ROWS_TYPE = 5;
// 上传图片和上传文件类型
const FILE_TYPE = [10, 11];
// 评分类型
const SCORE_TYPE = 9;
// 地址类型
const ADDRESS_TYPE = 22;
// 存在最长长度和最短长度的类型
const LENGTH_TYPE = [4, 5];
// 微信类型
const WECHAT_TYPE = 17;
// 性别
const SEX = 14;
// 上传图片
const PICTURE = 10;
// 选项数
const OPTION_NUM = 2;
// 客观题型(1单选,2复选,3下拉选择,9评分)
const OBJECTIVE = [1, 2, 3, 9];
// 主观题型(4单行文本,5多行文本,12段落说明)
const SUBJECTIVE = [4, 5, 12];
// 问题类型
const QUESTION_TYPES = [
1=>'单项选择',
2=>'多项选择',
3=>'下拉选择',
4=>'单行文本',
5=>'多行文本',
6=>'日期时间',
7=>'数字',
8=>'金额',
9=>'评分',
10=>'上传图片',
11=>'上传文件',
12=>'段落说明',
13=>'姓名',
14=>'性别',
15=>'手机号',
16=>'邮箱',
17=>'微信号',
18=>'生日',
19=>'公司',
20=>'部门',
21=>'职位',
22=>'地址'
];
/**
* @var AnswerModel 调研回答用户记录表
*/
protected $_d_answer;
/**
* @var RecordModel 调研回答详情表
*/
protected $_d_record;
/**
* @var BaseinfoModel 调研基本信息表
*/
protected $_d_baseinfo;
// 构造方法
public function __construct()
{
$this->_d = new QuestionModel();
$this->_d_answer = new AnswerModel();
$this->_d_record = new RecordModel();
$this->_d_baseinfo = new BaseinfoModel();
parent::__construct();
/*
问题类型:
1=单项选择[radio],
2=多项选择[checkbox],
3=下拉选择[select],
4=单行文本[text],
5=多行文本[textarea],
6=日期时间[datetime],
7=数字[number],
8=金额[money],
9=评分[score],
10=上传图片[img],
11=上传文件[file],
12=段落说明[note],
13=姓名[name],
14=性别[sex],
15=手机号[mobile],
16=邮箱[email],
17=微信号[wechat],
18=生日[birthday],
19=公司[company],
20=部门[department],
21=职位[job],
22=地址[address])
*/
}
// 数据验证
public function checkData($data)
{
// 问题列表是否为空
if (empty($data)) {
E('_EMPTY_QUESTION');
}
// 遍历判断问题的各项设置
foreach ($data as $v) {
// 问题标题不能为空(段落除外)
if (empty($v['q_title']) && self::NOTE_TYPE != $v['q_type']) {
E('_EMPTY_QUESTION_TITLE');
}
// 问题类型不能为空
if (empty($v['q_type'])) {
E('_EMPTY_QUESTION_TYPE');
}
// 问题是否必填不能为空
if (!isset($v['q_required'])) {
E('_EMPTY_QUESTION_REQUIRE');
}
if (isset($v['q_other']) && !in_array($v['q_other'], [self::OTHER_YES, self::OTHER_NO])) {
E('_ERR_Q_OTHER');
}
// 如果是单选,复选,下拉选择,则最少需要设置两个选项
if (in_array($v['q_type'], self::OPTION_TYPE)) {
if (empty($v['q_option']) || count($v['q_option']) < self::OPTION_NUM) {
E('_EMPTY_QUESTION_OPTION');
}
// 此处需要判断是不是存在重复选项,如果存在则抛错
$option_data = array_column($v['q_option'], 'option');
// 选项总数
$option_count = count($option_data);
$filter_option_data = [];
foreach ($option_data as $val) {
if (isset($val) && $val != '') {
array_push($filter_option_data, $val);
}
}
// 选项为空
if (count($filter_option_data) != $option_count) {
E('_ERR_EPEAT_OPTION');
}
// 选项重复
if (count(array_unique($filter_option_data)) != $option_count) {
E('_ERR_REPEAT_OPTION');
}
}
continue;
}
return true;
}
/**
* 组装调研问题数据
*
* @param int $qu_id 调研ID
* @param array $data 调研问题数据
*
* @return array 入库问题数据
*/
public function create_data($qu_id, $data)
{
$attach_ids = [];
$add_data = [];
$q_order = 0;
foreach ($data as $v) {
// 过滤错误数据
if (empty($v['q_type']) || (empty($v['q_title']) && self::NOTE_TYPE != $v['q_type'])) {
continue;
}
// 如果问题类型是数字或者金额,保留两位小数,其他的保留整数
if (in_array($v['q_type'], self::NUMBER_TYPE)) {
$q_min = round($v['q_min'], 2); // 最小值
$q_max = round($v['q_max'], 2); // 最大值
} else {
$q_min = intval($v['q_min']); // 最小值/最少选项数量/最小长度
$q_max = intval($v['q_max']); // 最大值/最多选项数量/最大长度/评分满分值/上传图片最大数量
}
$field = '';
// 如果是单选,复选,下拉框,
if (in_array($v['q_type'], self::OPTION_TYPE)) {
// 此处需要给问题选项新增一个is_check字段(是否选中 0=未选中,1=已选中 )
foreach ($v['q_option'] as &$_v) {
$_v['is_check'] = 0;
if (!isset($_v['img_options'])) {
$_v['img_options'] = [];
} else {
$option_img_ids = array_column($_v['img_options'], 'option_img');
$attach_ids = array_merge($attach_ids, $option_img_ids);
}
}
$field = serialize($v['q_option']);
}
$add_data[] = [
'qu_id' => rintval($qu_id), // 调研id
'q_title' => $v['q_title'], // 问题名称
'q_placeholder' => $v['q_placeholder'], // 问题描述
'q_type' => rintval($v['q_type']), // 问题类型
'q_order' => $q_order, // 问题排序
'q_other_title' => $v['q_other_title'], // 其他选项标题
'q_required' => rintval($v['q_required']), // 是否必填(0:非必填,1:必填)
'q_other' => rintval($v['q_other']), // 是否包含其他选项(0:不包含,1:包含)
'q_min' => $q_min,
'q_max' => $q_max,
'q_data_type' => rintval($v['q_data_type']), // 日期时间/生日格式(1:日期时间,2:日期,3:时间,4:年月日,5:年月,6:月日)
'q_money_unit' => $v['q_money_unit'], // 金额单位
'q_field' => $field // 把选项组合成以,分隔的形式
];
$q_order++;
}
return [$add_data, $attach_ids];
}
/**
* 遍历检查调研回答
*
* @param int $qu_id 调研ID
* @param int $a_id 用户回答ID
* @param array $answer_list 回答列表
*
* @return array|bool 回答数组
*/
public function check_answer($qu_id, $a_id, $answer_list)
{
$answer_data = [];
$question_list = $this->_d->list_by_conds(['qu_id' => $qu_id], null, ['q_order' => 'ASC']);
// 把问题答案转换成以问题ID为key的数组
$answer_list = array_combine_by_key($answer_list, 'qid');
$attach_ids = [];
foreach ($question_list as $v) {
// 获取此问题的答案信息
$answer = $answer_list[$v['qid']];
// 如果问题是单选,复选,下拉选择,性别选择
if (in_array($v['q_type'], self::OPTION_TYPE)) {
// 如果该问题为必填选项,并且问题答案为空
if (self::REQUIRED_YES == $v['q_required']
&& empty($answer['q_answer_other'])
&& empty($answer['q_answer_option'])) {
E('_EMPTY_ANSWER');
}
// 其他选项答案数量
$answer_option_num = 0;
if (!empty($answer['q_answer_other'])) {
$answer_option_num = 1;
}
// 答案选中的总选项数量
$answer_option_all = count($answer['q_answer_option']) + $answer_option_num;
// 如果为多选类型,并且设置最少或者最毒选项数量,问题答案不为空的情况下验证当前答案数量是否符合设置
if ($v['q_type'] == 2 && $answer_option_all > 0) {
if (intval($v['q_min']) > 0 && $answer_option_all < intval($v['q_min'])) {
E('_ERR_ANSWER_OPTION_MIN');
}
if (intval($v['q_max']) > 0 && $answer_option_all > intval($v['q_max'])) {
E('_ERR_ANSWER_OPTION_MAX');
}
}
// 组装入库数据
$answer_data[] = [
'qu_id' => $qu_id,
'a_id' => $a_id,
'q_id' => $v['qid'],
'answer' => !empty($answer['q_answer_option']) ? serialize($answer['q_answer_option']) : '',
'other' => $answer['q_answer_other']
];
} elseif (in_array($v['q_type'], self::FILE_TYPE)) {
// 如果问题是图片、附件类型
// 如果该问题为必填选项,并且问题答案为空
if (self::REQUIRED_YES == $v['q_required'] && empty($answer['q_answer_option'])) {
E('_EMPTY_ANSWER');
}
// 组装入库数据
$answer_data[] = [
'qu_id' => $qu_id,
'a_id' => $a_id,
'q_id' => $v['qid'],
'answer' => !empty($answer['q_answer_option']) ? serialize($answer['q_answer_option']) : '',
'other' => $answer['q_answer_other']
];
// 收集附件ID
$img_ids = array_column($answer['q_answer_option'], 'option_img');
$attach_ids = array_merge($attach_ids, $img_ids);
} else {
// 如果该问题为必填选项,类型不是段落类型,并且问题答案为空,抛错
if (self::REQUIRED_YES == $v['q_required'] && $v['q_type'] != self::NOTE_TYPE
&& empty($answer['q_answer'])) {
E('_EMPTY_ANSWER');
}
// 此处检查地址详情的必填信息
if (self::ADDRESS_TYPE == $v['q_type'] && self::REQUIRED_YES == $v['q_required']
&& self::OTHER_YES == $v['q_other'] && empty($answer['q_answer_other'])) {
E('_EMPTY_ADDRESS_DETAIL');
}
// 如果答案不为空,此处检查字符长度合法性
if (!empty($answer['q_answer']) && in_array($v['q_type'], self::LENGTH_TYPE)) {
// 答案实际长度
$length = mb_strlen($answer['q_answer'], 'UTF8');
// 如果设置了最小长度,检查合法性
if ($length < intval($v['q_min']) && intval($v['q_min']) > 0) {
E('_ERR_ANSWER_MIN_LENGTH');
}
// 如果设置了最大长度,检查合法性
if ($length > intval($v['q_max']) && intval($v['q_max']) > 0) {
E('_ERR_ANSWER_MAX_LENGTH');
}
}
// 如果答案不为空并且设置了最小值,此处检查合法性
if (!empty($answer['q_answer']) && in_array($v['q_type'], self::NUMBER_TYPE)) {
// 答案的实际数字
$number = round($answer['q_answer'], 2);
// 如果设置了最小值 ,检查合法性
if ($number < round($v['q_min'], 2) && $v['q_min'] > 0) {
E('_ERR_ANSWER_MIN_VALUE');
}
// 如果设置了最大值 ,检查合法性
if ($number > round($v['q_max'], 2) && $v['q_max'] > 0) {
E('_ERR_ANSWER_MAX_VALUE');
}
}
// 验证微信
if (self::WECHAT_TYPE == $v['q_type'] && self::REQUIRED_YES == $v['q_required']) {
$wx_reg = "/^[a-zA-Z]{1}[-_a-zA-Z0-9]{5,19}$/";
if (!preg_match($wx_reg, $answer['q_answer'])) {
E('_ERR_ANSWER_WECHAT');
}
}
// 组装入库数据
$answer_data[] = [
'qu_id' => $qu_id,
'a_id' => $a_id,
'q_id' => $v['qid'],
'answer' => $answer['q_answer'],
'other' => $answer['q_answer_other']
];
}
}
return [array_values($answer_data), $attach_ids];
}
/**
* 格式化返回数据
*
* @param $data array 需要格式化的数据
*
* @return mixed
*/
public function format_data(&$data)
{
$data['qid'] = rintval($data['qid']);
$data['qu_id'] = rintval($data['qu_id']);
$data['q_type'] = rintval($data['q_type']);
$data['q_order'] = rintval($data['q_order']);
$data['q_required'] = rintval($data['q_required']);
$data['q_other'] = rintval($data['q_other']);
$data['q_data_type'] = rintval($data['q_data_type']);
$data['created'] = rintval($data['created']);
// 如果问题类型是数字或者金额,保留两位小数,其他的保留整数
if (in_array($data['q_type'], self::NUMBER_TYPE)) {
$data['q_min'] = round($data['q_min'], 2);
$data['q_max'] = round($data['q_max'], 2);
} else {
$data['q_min'] = rintval($data['q_min']);
$data['q_max'] = rintval($data['q_max']);
}
$data['q_answer'] = '';
$data['q_answer_other'] = '';
$data['q_answer_option'] = [];
unset($data['deleted'], $data['updated'], $data['status'], $data['domain']);
return $data;
}
/**
* 格式化问题选项中的图片
*
* @param array $option 选项列表
*
* @return mixed
*/
public function _format_img_url(&$option)
{
// 如果选项不为空
if (is_array($option)) {
foreach ($option as $k => $v) {
// 如果选项存在图片
if (count($v['img_options'])) {
foreach ($v['img_options'] as $_k => $_v) {
$option[$k]['img_options'][$_k]['option_img_url'] = imgUrl($_v['option_img']);
}
}
}
}
return $option;
}
/**
* 获取调研问题回答统计数据
*
* @param array $param 请求参数
*
* @return array 返回结果
*/
public function get_result_data($param = [])
{
// 获取问题基本信息
$question = $this->get($param['q_id']);
// 获取调研基本信息
$base_info = $this->_d_baseinfo->get($question['qu_id']);
// 组装基本信息
$res_data = [
'q_id' => rintval($question['qid']),
'q_title' => $question['q_title'],
'q_placeholder' => $question['q_placeholder'],
'q_type' => rintval($question['q_type']),
'q_required' => rintval($question['q_required']),
'q_other' => rintval($question['q_other'])
];
// 默认值
$page = !empty($param['page']) ? intval($param['page']) : 1;
$limit = !empty($param['limit']) ? intval($param['limit']) : 5;
// 根据不同的问题类型,查询组织返回数据
if (in_array($question['q_type'], self::OPTION_TYPE)) {
// -----单选、多选、下拉、性别-----
// 获取统计结果数据并格式化
$result_data = $this->get_option_answer_result($question, $page, $limit);
// 合并组装返回结果
$result = array_merge($res_data, $result_data);
} elseif (self::SCORE_TYPE == $question['q_type']) {
// -----评分类型----
// 获取统计结果数据并格式化
$result_data = $this->get_stars_answer_result($question);
// 合并组装返回结果
$result = array_merge($res_data, $result_data);
} elseif (in_array($question['q_type'], self::NUMBER_TYPE)) {
// 数字、金额类型
// 获取统计结果数据并格式化
$result_data = $this->get_number_answer_result($question, $page, $limit, $base_info['anonymous']);
// 合并组装返回结果
$result = array_merge($res_data, $result_data);
} elseif (in_array($question['q_type'], self::FILE_TYPE)) {
// 图片、文件类型
// 获取统计结果数据并格式化
$result_data = $this->get_file_answer_result($question, $page, $limit, $base_info['anonymous']);
// 合并组装返回结果
$result = array_merge($res_data, $result_data);
} elseif (self::NOTE_TYPE == $question['q_type']) {
// 段落类型,无需查询,直接返回
$res_data['q_answer_list'] = [];
$result = $res_data;
} else {
// 单行文本,多行文本,日期
// 获取统计结果数据并格式化
$result_data = $this->get_text_answer_result($question, $page, $limit, $base_info['anonymous']);
// 合并组装返回结果
$result = array_merge($res_data, $result_data);
}
return $result;
}
/**
* 获取多选,单选。下拉,性别的数据统计信息
*
* @param array $question 问题信息
*
* @return array
*/
public function get_option_answer_result($question = [])
{
// 获取该试题的回答记录
$record_list = $this->_d_record->list_by_where($question['qid']);
// 【总参与人数】
$q_total = count($record_list);
// 题目选项
$options = unserialize($question['q_field']);
$data_x = array_column($options, 'option');
$other_key = -1;
// 包含其他选项
if (self::OTHER_YES == $question['q_other']) {
array_push($data_x, '其他');
$other_key = array_search('其他', $data_x);
}
// 初始data_y为与data_x等长的数组
$data_y = [];
$data_y = array_pad($data_y, count($data_x), 0);
// 初始q_option为与data_x等长的数组
$q_option = [];
$q_option = array_pad($q_option, count($data_x), 0);
// 获取选项数据
$qu_options = unserialize($question['q_field']);
$qu_options = array_combine_by_key($qu_options, 'option');
foreach ($record_list as $key => $record) {
$answer_option = unserialize($record['answer']);
// 取出答案数据
$check_answer_option = array_column($answer_option, 'option');
foreach ($data_x as $key_x => $val_x) {
if (in_array($val_x, $check_answer_option)) {
$data_y[$key_x]++;
}
$q_option[$key_x] = [
'option' => $val_x,
'is_other' => self::OTHER_NO,
'option_img' => $qu_options[$key_x]['option_img'] ? $qu_options[$key_x]['option_img'] : '',
'option_img_url' => $qu_options[$key_x]['option_img'] ? imgUrl($qu_options[$key_x]['option_img']) : ''
];
if (self::OTHER_YES == $question['q_other'] && !empty($record['other'])) {
$q_option[$other_key] = [
'option' => '其他',
'is_other' => self::OTHER_YES,
'option_img' => '',
'option_img_url' => ''
];
}
}
if (self::OTHER_YES == $question['q_other'] && !empty($record['other'])) {
$data_y[$other_key]++;
}
}
$total_sum = array_sum($data_y);
foreach ($data_y as $key_y => $val_y) {
// 百分比
$option_percent = round(($val_y / $total_sum) * 100, 2);
// 赋值
$q_option[$key_y]['option_num'] = $val_y;
$q_option[$key_y]['option_percent'] = $option_percent . '%';
}
return [
'q_total' => $q_total, // 总参与人数
'q_data_x' => $data_x, // X轴数据
'q_data_y' => $data_y, // Y轴数据
'q_option' => $q_option // 选项数据
];
}
/**
* 获取评分类型的数据统计信息
*
* @param array $question $question 问题信息
*
* @return array
*/
public function get_stars_answer_result($question = [])
{
// 获取评分最大值
$stars_max = $question['q_max'];
// 获取该试题的回答记录
$record_list = $this->_d_record->list_by_where($question['qid']);
// 获取总参与人数
$res_data['q_total'] = count($record_list);
$data_x = []; // X轴数据
$data_y = []; // Y轴数据
$q_option = [];
$score = 0;
// 循环星级选项数据
for ($i = 1; $i <= $stars_max; $i++) {
// 添加到X轴
$data_x[] = $i . '分';
// 当前选项填写人数
$option_num = 0;
$options_data = [];
$options_data['is_other'] = self::OTHER_NO;
$options_data['option'] = $i . '分';
// 循环回答记录数据
foreach ($record_list as $v) {
if ($v['answer'] == $i) {
// 累积计算总分
$score += $i;
// 累加参加人数
$option_num++;
}
}
// 赋值回答人数
$options_data['option_num'] = $option_num;
// 计算百分比
$option_percent = round(($option_num / $res_data['q_total']) * 100, 2);
// 赋值百分比
$options_data['option_percent'] = $option_percent . "%";
// 赋值y轴数据
$data_y[] = $option_percent;
$q_option[] = $options_data;
}
// 计算平均分
$res_data['q_answer_avg'] = round(($score / $res_data['q_total']), 2);
$res_data['q_data_x'] = $data_x;
$res_data['q_data_y'] = $data_y;
$res_data['q_option'] = $q_option;
return $res_data;
}
/**
* 获取数字金额类型的数据统计信息
*
* @param array $question $question 问题信息
* @param int $page 页码
* @param int $limit 每页条数
* @param int $anonymous 是否匿名(1:实名,0匿名)
*
* @return array
*/
public function get_number_answer_result($question = [], $page = 1, $limit = 5, $anonymous = 0)
{
// 获取该试题的回答记录
$record_list = $this->_d_record->list_by_where($question['qid']);
// 获取总参与人数
$res_data['q_total'] = count($record_list);
// 计算总分
$total_score = 0;
$score_list = array_column($record_list, 'answer');
foreach ($score_list as $v) {
$total_score += $v;
}
$q_answer_list = [];
// 如果回答记录不为空,则进行分页
if ($res_data['q_total'] > 0) {
// 分页格式化
$q_answer_list = $this->format_other_list($record_list, $page, $limit, $anonymous);
}
// 计算平均分
$res_data['q_answer_avg'] = round(($total_score / $res_data['q_total']), 2);
$res_data['q_answer_list'] = $q_answer_list;
return $res_data;
}
/**
* 格式化其他答案的列表
*
* @param array $record_list 答案详情列表
* @param int $page 页码
* @param int $limit 每页条数
* @param int $anonymous 是否匿名(1:实名,0:匿名)
*
* @return array 返回结果
*/
public function format_other_list($record_list, $page = 1, $limit = 5, $anonymous = 1)
{
$pagedata = $this->page_array($limit, $page, $record_list);
// 回答记录ID集合
$a_ids = array_column($pagedata, 'a_id');
// 获取回答记录列表
$answer_list = $this->_d_answer->list_by_conds(['a_id' => $a_ids]);
// 转换成以a_id为key的二维数组
$answer_list = array_combine_by_key($answer_list, 'a_id');
$list = [];
// 如果是匿名填写
if (BaseinfoService::ANONYMOUS == $anonymous) {
$i = 1;
foreach ($pagedata as $v) {
// 获取回答记录详情
$answer = $answer_list[$v['a_id']];
$list[] = [
'uid' => !empty($answer['uid']) ? $answer['uid'] : $answer['openid'],
'user_name' => '匿名' . $i,
'user_dp' => '',
'answer_content' => $v['answer'],
'answer_time' => $answer['created']
];
$i++;
}
return $list;
}
// 获取回答记录的用户ID集合
$uids = array_column($answer_list, 'uid');
// 查询用户列表,将用户列表转换成以用户uid为主键的二维数组
$user_list = $this->getUser($uids);
$list = [];
foreach ($pagedata as $v) {
// 获取回答记录详情
$answer = $answer_list[$v['a_id']];
$user_dp = '';
if (empty($answer['uid'])) {
// 如果回答人uid为空,则是外部用户
$uid = $answer['openid'];
$user_name = $answer['username'] ? $answer['username'] : '外部用户';
} else {
// 根据回答人ID获取用户详情
$user_info = $user_list[$answer['uid']];
$uid = $answer['uid'];
$user_name = $user_info['memUsername'];
// 获取部门数组
$dp_array = $user_info['dpName'];
// 将部门名称组成一维数组
$dp_list = array_column($dp_array, 'dpName');
if (!empty($dp_list)) {
$user_dp = implode(',', $dp_list);
}
}
$list[] = [
'uid' => $uid,
'user_name' => $user_name,
'user_dp' => $user_dp,
'answer_content' => $v['answer'],
'answer_time' => $answer['created']
];
}
return $list;
}
/**
* 数组分页
*
* @param int $limit 每页条数
* @param int $page 页码
* @param array $array 传入数组
*
* @return array 返回结果
*/
private function page_array($limit, $page, $array)
{
// 判断当前页面是否为空 如果为空就表示为第一页面
$page = empty($page) ? 1 : $page;
// 计算每次分页的开始位置
$start = ($page - 1) * $limit;
$total = count($array);
$page_data = [];
if ($total) {
$page_data = array_slice($array, $start, $limit);
}
return $page_data;
}
/**
* 获取图片,文件 类型的数据统计信息
*
* @param array $question $question 问题信息
* @param int $page 页码
* @param int $limit 每页条数
* @param int $anonymous 是否匿名(1:实名,0:匿名)
*
* @return array
*/
public function get_file_answer_result($question = [], $page = 1, $limit = 5, $anonymous = 1)
{
// 获取该试题的回答记录
$record_list = $this->_d_record->list_by_where($question['qid']);
// 获取总参与人数
$total = count($record_list);
$q_answer_list = [];
// 如果回答记录不为空,则进行分页
if ($total) {
// 分页格式化
$q_answer_list = $this->format_other_list($record_list, $page, $limit, $anonymous);
}
// 循环数据,格式化图片显示
foreach ($q_answer_list as &$v) {
$answer_option_data = [];
if (!empty($v['answer_content'])) {
// 反序列化答案数据
$answer_option_data = unserialize($v['answer_content']);
}
$pic_data = [];
// 此处需要处理答案中包含的图片的显示路径
foreach ($answer_option_data as $_v) {
if (!empty($_v['option_img'])) {
$pic_data[]['imgUrl'] = imgUrl($_v['option_img']);
}
}
// 删掉多余字段
unset($v['answer_content']);
// 新增图片列表数据
$v['file_list'] = $pic_data;
}
return [
'q_total' => $total,
'q_answer_list' => $q_answer_list
];
}
/**
* 获取单行文本,多行文本,日期时间 类型的数据统计信息
*
* @param array $question $question 问题信息
* @param int $page 页码
* @param int $limit 每页条数
* @param int $anonymous 是否匿名(1:实名,0匿名)
*
* @return array
*/
public function get_text_answer_result($question = [], $page = 1, $limit = 5, $anonymous = 1)
{
// 获取该试题的回答记录
$record_list = $this->_d_record->list_by_where($question['qid']);
// 获取总参与人数
$res_data['q_total'] = count($record_list);
$q_answer_list = [];
// 如果回答记录不为空,则进行分页
if ($res_data['q_total']) {
// 分页格式化
$q_answer_list = $this->format_other_list($record_list, $page, $limit, $anonymous);
}
$res_data['q_answer_list'] = $q_answer_list;
return $res_data;
}
/**
* 获取其他选项答案数据列表
*
* @param array $param 传入参数
*
* @return array
*/
public function get_other_result_list($param = [])
{
// 获取该试题的回答记录
$record_list = $this->_d_record->list_by_where($param['q_id']);
// 获取问题基本信息
$question = $this->_d->get($param['q_id']);
// 获取试卷基本信息
$base_info = $this->_d_baseinfo->get($question['qu_id']);
// 其他选项列表数据
$q_other_list = [];
foreach ($record_list as $_v) {
// 如果答案为空,则continue;
if (empty($_v['other'])) {
continue;
}
// 组装其他答案数据列表
$q_other_list[] = [
'a_id' => $_v['a_id'],
'answer' => $_v['other']
];
}
// 总数
$total = count($q_other_list);
// 判断分页参数是否为空,为空赋默认值
$page = !empty($params['page']) ? rintval($params['page']) : 1;
$limit = !empty($params['limit']) ? rintval($params['limit']) : 10;
$list = [];
// 格式化其他选项列表数据
if (!empty($q_other_list)) {
$list = $this->format_other_list($q_other_list, $page, $limit, $base_info['anonymous']);
}
return [
'page' => $page,
'limit' => $limit,
'total' => $total,
'list' => $list
];
}
}