获取调查问卷统计

{
    "errno": 0,
    "errdesc": "操作成功",
    "timestamp": 1584168603,
    "data": {
        "survey_info": {
            "id": "1",
            "title": "你吃了吗?",
            "begin_time": "2020-03-10",
            "end_time": "2020-03-20"
        },
        "question_list": [
            {
                "id": "1",
                "survey_id": "1",
                "subject": "吃什么?",
                "type": "2",
                "weight": "0",
                "create_time": "1583892834",
                "deleted": "0",
                "answer_arr": [
                    "1",
                    "2",
                    "3",
                    "4",
                    "1",
                    "2",
                    "3"
                ],
                "item": [
                    {
                        "id": "1",
                        "question_id": "1",
                        "option": "苹果",
                        "create_time": "1583892834",
                        "deleted": "0",
                        "percent": "28.57"
                    },
                    {
                        "id": "2",
                        "question_id": "1",
                        "option": "梨子",
                        "create_time": "1583892834",
                        "deleted": "0",
                        "percent": "28.57"
                    },
                    {
                        "id": "3",
                        "question_id": "1",
                        "option": "香蕉",
                        "create_time": "1583892834",
                        "deleted": "0",
                        "percent": "28.57"
                    },
                    {
                        "id": "4",
                        "question_id": "1",
                        "option": "西瓜",
                        "create_time": "1583895206",
                        "deleted": "0",
                        "percent": "14.28"
                    }
                ]
            },
            {
                "id": "2",
                "survey_id": "1",
                "subject": "好吃吗?",
                "type": "1",
                "weight": "0",
                "create_time": "1583894553",
                "deleted": "0",
                "answer_arr": [
                    "5",
                    "5"
                ],
                "item": [
                    {
                        "id": "5",
                        "question_id": "2",
                        "option": "好吃",
                        "create_time": "1583895231",
                        "deleted": "0",
                        "percent": "100.00"
                    },
                    {
                        "id": "6",
                        "question_id": "2",
                        "option": "不好吃",
                        "create_time": "1583895231",
                        "deleted": "0",
                        "percent": "0.00"
                    }
                ]
            },
            {
                "id": "3",
                "survey_id": "1",
                "subject": "你有什么意见?",
                "type": "3",
                "weight": "0",
                "create_time": "1583895248",
                "deleted": "0"
            }
        ]
    }
}
/**
 * 获取调查问卷统计
 */
public function getSurveyStat() {
    $survey_id = $_POST['survey_id'];
    // 获取基本信息
    $out_data = [];
    $survey = M('survey');
    $survey_info = $survey->where(['id'=>$survey_id])->field('id,title,begin_time,end_time')->find();
    $survey_info['begin_time'] = date('Y-m-d',$survey_info['begin_time']);
    $survey_info['end_time'] = date('Y-m-d',$survey_info['end_time']);
    $out_data['survey_info'] = $survey_info;

    // 获取问卷题目
    $survey_question = M('survey_question');
    $question_list = $survey_question->where(['survey_id'=>$survey_id,'deleted'=>0])->select();
    $survey_answer = M('survey_answer');
    foreach($question_list as $k=>&$v) {
        if (in_array($v['type'],['1','2'])) { // 单选、多选
            // 获取题目选项和统计结果,如果没有填写则为0(获取总的回答数,然后用每个选项的回答数去除)
            $survey_question_item = M('survey_question_item');
            $survey_question_item_list = $survey_question_item->where(['question_id'=>$v['id'],'deleted'=>0])->select();
            // 获取当前题目的总的回答数
            $survey_answer_list = $survey_answer->where(['question_id'=>$v['id'],'deleted'=>0])->getField('answer',true);
            $answer_arr = [];
            foreach ($survey_answer_list as $sk => &$sv) {
                $tmp_answer = explode(',',trim($sv,','));
                $answer_arr = array_merge($answer_arr,$tmp_answer); // 合并数组
            }
            $answer_item_count = array_count_values($answer_arr); // 获取数组中值的个数
            $total_answer_count = count($answer_arr);
            foreach ($survey_question_item_list as $qk => &$qv) {
                if ($total_answer_count == 0) {
                    $qv['percent'] = 0;
                } else {
                    $tmp_item_count = (int)$answer_item_count[$qv['id']];
                    $qv['percent'] = MathUtil::div($tmp_item_count * 100 , $total_answer_count);
                }
            }

            $v['answer_arr'] = $answer_arr;
            $v['item'] = $survey_question_item_list;
        }
    }

    $out_data['question_list'] = $question_list;
    $this->json->S($out_data);
}
posted @ 2020-03-14 14:51  TBHacker  阅读(205)  评论(0编辑  收藏  举报