zoukankan      html  css  js  c++  java
  • 获取调查问卷统计

    {
        "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);
    }
    
  • 相关阅读:
    Android_listview设置每条信息的间距
    Android实现ListView或GridView首行/尾行距离屏幕边缘距离
    实现类似微信的延迟加载的Fragment——LazyFragment
    struts2的Action该方法不能去
    (工具)source insight高速增加时间代码
    猫学习IOS(十五)UI以前的热的打砖块游戏
    java语言内部类和匿名内部类
    JVM截至多少线程可以创建: unable to create new native thread
    linux下一个Oracle11g RAC建立(八)
    转基因小麦--主题在农业科技的最前沿
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/12492163.html
Copyright © 2011-2022 走看看