AnswerAnalysisController.class.php 6.35 KB
<?php
/**
 * Created by PhpStorm.
 * User: zoulongbo
 * Date: 2018/8/7
 * Time: 10:26
 */

namespace Apicp\Controller\Export;

use Com\PythonExcel;
use Common\Common\ExportDownload;
use Common\Service\TopicService;
use Common\Service\AnswerDetailService;
use Common\Service\AnswerService;
use Common\Service\PaperService;
use Common\Service\RandomSnapshotService;
use Common\Service\SnapshotService;
use Common\Service\StatisticsService;

class AnswerAnalysisController extends AbstractController
{

    /** @var PaperService */
    protected $paper_serv;
    /** @var SnapshotService */
    protected $snapshot_serv;
    /** @var AnswerService */
    protected $answer_serv;
    /** @var AnswerDetailService */
    protected $answer_detail_serv;
    /** @var RandomSnapshotService */
    protected $random_snapshot_serv;
    /** @var StatisticsService */
    protected $statistics_serv;

    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {

            return false;
        }

        $this->paper_serv = new PaperService();

        $this->snapshot_serv = new SnapshotService();

        $this->answer_serv = new AnswerService();

        $this->answer_detail_serv = new AnswerDetailService();

        $this->random_snapshot_serv = new RandomSnapshotService();

        $this->statistics_serv = new StatisticsService();

        return true;
    }


    /**
     * 试题分析导出接口
     * @desc 试题分析导出接口
     * @param string ep_id  试卷id
     * @return array
     */
    public function Index_post()
    {
        $params = I('post.');
        if (empty($params)) {
            E('_ERR_PARAMS_NOT_NULL');
        }

        // 试卷id
        $ep_id = intval($params['ep_id']);
        if (!$ep_id) {

            E('_EMPTY_EP_ID');
        }

        // 根据试卷id获取试卷详情
        $paper = $this->paper_serv->get($ep_id);
        if (empty($paper)) {

            E('_ERR_PAPER_NOT_FOUND');
        }

        $list = [];
        $options = [];
        //当前可分析 题目类型
        $et_types = [
            TopicService::TOPIC_TYPE_SINGLE,
            TopicService::TOPIC_TYPE_MULTIPLE
        ];

        $ep_cond = ['ep_id' => $ep_id];

        // 查询题
        $topics = $this->snapshot_serv->list_by_conds($ep_cond);
        $topics = array_combine_by_key($topics, 'es_id');

        $statistics = $this->statistics_serv->list_by_conds($ep_cond);
        $statistics = array_combine_by_key($statistics, 'esr_id');


        //循环大法  开始组装数据
        foreach ($topics as $key => $topic) {

            $et_type = $topic['et_type'];
            // 单选、判断、多选,统计各选项使用率
            if (in_array($et_type, $et_types)) {


                if (!empty($statistics[$key])) {

                    $this->get_options($topic, $statistics[$key], $options);
                    $count = $statistics[$key]['answer_num'];

                    $list[] = [
                        'id' => $topic['order_num'],
                        'title' => $topic['title'],
                        'et_type' => $topic['et_type'],
                        'count' => intval($count),
                        'answer' => $topic['answer'],
                        'right_rate' => $topic['right_rate'],
                        'options' => $options,

                    ];
                }

            }

        }
        //print_r($list);die();
        // 导出结果
        $this->_download($list);
    }

    /**
     * 组装options数据
     *
     * @param $et_type int 试题类型
     * @param $topic array 试题快照
     * @param $statistics array 试题统计
     * @param $options array 需要格式化的数据
     *
     * @return bool
     */
    protected function get_options(&$topic, $statistics, &$options)
    {
        if (!empty($statistics)) {

            $options_count = unserialize($statistics['options']);
            $answer_count = array_sum($options_count);

            if (intval($answer_count) == 0) {
                $answer_count = 1; // 被除数不能为0
            }

            // 选择题

            $options_data = unserialize($topic['options']);

            // 循环组装选项数据
            foreach ($options_data as $k => $v) {

                $options[$k]['option_name'] = $v['option_name'];
                $options[$k]['count'] = $options_count[$v['option_name']];
                $rate = $options_count[$v['option_name']] / $answer_count;
                $options[$k]['percentage'] = round($rate * 100, 2) . '%';

                if ($topic['answer'] == $options[$k]['option_name']) {
                    $topic['right_rate'] = $options[$k]['percentage'];
                }
            }
        }

        return true;
    }


    /**
     * 导出模板
     *
     * @param array $list 列表数据
     * @param int $watch_type 导出类型
     */
    private function _download($list = [])
    {
        $row_data = [];

        $file_name = '试题分析' . date('_YmdHi');
        $title = array(
            '题目序号',
            '正确率',
            '正确答案'
        );

        $option_titles = [];

        foreach ($list as $k => $v) {
            $row_data[$k] = [
                $v['id'],
                $v['right_rate'],
                $v['answer']
            ];

            foreach ($v['options'] as $key => $value) {
                //获取 数据中操作选项
                $option_titles[] = $value['option_name'];

                //获取操作选项 个数
                array_push($row_data[$k], $value['count']);

            }
        }

        $option_titles = array_unique($option_titles);
        sort($option_titles);

        //去除 重复选项
        $title = array_merge($title, $option_titles);

        // Python导出excel
        $realpath = ExportDownload::get_down_dir($this->_login->user['eaId'] . microtime(true)) . $file_name . ".xls";
        $ret = PythonExcel::instance()->write($realpath, $title, $row_data);
        if ($ret) {
            $conditon = [
                'title' => $file_name,
                'ea_id' => $this->_login->user['eaId'],
                'username' => $this->_login->user['eaRealname'],
                'type' => ExportDownload::EXCEL_TYPE,
                'url' => $realpath
            ];

            ExportDownload::insert_down_load($conditon);
        }

        return true;
    }


}