SnapshotService.class.php 8.51 KB
<?php
/**
 * 考试-试卷快照表
 * @author: houyingcai
 * @email:    594609175@qq.com
 * @date :  2017-05-19 17:49:26
 * @version $Id$
 */

namespace Common\Service;

use Common\Common\ExamHelper;
use Common\Model\PaperModel;
use Common\Model\PaperTempModel;
use Common\Model\SnapshotModel;
use Common\Model\TopicModel;

class SnapshotService extends AbstractService
{
    /** @var TopicModel */
    protected $_d_topic;
    /** @var PaperModel */
    protected $_d_paper;
    /** @var PaperTempModel */
    protected $_d_temp;

    // 构造方法
    public function __construct()
    {
        $this->_d = new SnapshotModel();
        $this->_d_topic = new TopicModel();
        $this->_d_paper = new PaperModel();
        $this->_d_temp = new PaperTempModel();

        parent::__construct();
    }

    /**
     * 格式化后台试题列表
     *
     * @author daijun
     *
     * @param array $list
     * @param array $info
     *
     * @return array
     */
    public function format_admin_list($list = [], $info = [])
    {
        $return_list = [];

        if (empty($list)) {
            return $return_list;
        }

        $exam_help = new ExamHelper();

        foreach ($list as $k => $v) {
            $return_list[$k]['et_id'] = intval($v['et_id']);
            $return_list[$k]['et_type'] = intval($v['et_type']);
            $return_list[$k]['title'] = $v['title'];
            $return_list[$k]['score'] = intval($v['score']);

            // 处理图片数据
            $img_list = [];
            if (!empty($v['title_pic'])) {
                $img_data = explode(',', $v['title_pic']);
                foreach ($img_data as $_k => $_v) {
                    $img_list[$_k]['img_url'] = imgUrlReal($_v);
                }
            }
            $return_list[$k]['title_pic_list'] = $img_list;

            // 处理选项数据
            $options_data = [];
            if (!empty($v['options'])) {
                $options_list = unserialize($v['options']);
                // 循环组装选项数据
                foreach ($options_list as $_k => $_v) {
                    $options_data[$_k]['option_name'] = $_v['option_name'];
                    $options_data[$_k]['option_value'] = $_v['option_value'];
                    $options_data[$_k]['option_image_id'] = $_v['option_image_id'] ? $_v['option_image_id'] : '';
                    if (!empty($_v['option_image_id'])) {
                        $options_data[$_k]['option_image_url'] = imgUrlReal($_v['option_image_id']);
                    } else {
                        $options_data[$_k]['option_image_url'] = '';
                    }

                }
            }

            $return_list[$k]['options'] = $options_data;

            // 如果是主观题 2:判断题 3:问答题  5:语音题
            if (in_array($v['et_type'], [self::TOPIC_TYPE_JUDGMENT, self::TOPIC_TYPE_QUESTION, self::TOPIC_TYPE_VOICE])) {
                $answer = $v['answer'];
            } else {
                // 如果启用了选项打乱,则需转换正确答案
                $options_index = implode(',', array_column($options_data, 'option_name'));
                $answer = $exam_help->chang_answer_option($options_index, $v['answer']);
            }

            $return_list[$k]['answer'] = $answer;
            $return_list[$k]['answer_resolve'] = $v['answer_resolve'];
            $return_list[$k]['order_num'] = intval($v['order_num']);
        }

        return $return_list;
    }

    /**
     * 获取已选题列表
     *
     * @author:daijun
     *
     * @param int $ep_id 试卷ID
     * @param string $fields 查询的字段
     *
     * @return array
     */
    public function get_snapshot_list($ep_id = 0, $fields = 'et_id,title')
    {
        // 查询已选题列表
        $list = $this->_d->list_by_conds(['ep_id' => $ep_id], null, ['order_num' => 'ASC'], $fields);

        // 格式化数据
        foreach ($list as $k => $v) {
            $list[$k]['et_id'] = intval($v['et_id']);
        }

        return $list;
    }

    /**
     * 将选择的试题存入试题快照表
     *
     * @author:daijun
     *
     * @param array $param
     *
     * @return bool
     */
    public function add($param = [])
    {
        // 验证试卷ID
        if (empty($param['ep_id'])) {
            E('_EMPTY_EP_ID');
        }

        // 验证所选题目列表
        if (empty($param['topic_list'])) {
            E('_EMPTY_CHOICE_ETID');
        }

        // 获取试题ID集合
        $et_ids = array_column($param['topic_list'], 'et_id');

        if (empty($et_ids)) {
            E('_EMPTY_CHOICE_ETID');
        }

        // 获取试卷详情
        $paper = $this->_d_paper->get($param['ep_id']);

        // 获取题目列表
        $topic_list = $this->_d_topic->list_by_conds(['et_id' => $et_ids], null, [],
            'et_id,et_type,title,title_pic,score,options,answer,answer_resolve,answer_coverage,match_type,answer_keyword');

        $topic_list = array_combine_by_key($topic_list, 'et_id');

        // 试卷总分
        $total_score = 0;
        $snapshot = [];
        // 出题规则:自主选题
        if ($paper['ep_type'] == self::TOPIC_CUSTOMER) {
            // 组装试卷编号
            $i = self::ORDER_NUM;

            // 获取试卷临时备选题目列表
            $paper_temp_list = $this->_d_temp->list_by_conds(
                ['ep_id' => $param['ep_id'], 'et_id' => $et_ids],
                null,
                [],
                'ep_id,et_id,score'
            );
            $paper_temp_list = array_column($paper_temp_list, null, 'et_id');

            // 自主选题,分数按照题目分数计算
            foreach ($et_ids as $v) {

                $temp_item = $paper_temp_list[$v];
                $total_score += intval($temp_item['score']);

                // 组装试题序号
                $arr = $topic_list[$v];
                $arr['score'] = $temp_item['score'];
                $arr['ep_id'] = $param['ep_id'];
                $arr['order_num'] = $i;
                $snapshot[] = $arr;

                $i++;
            }
        } else {
            // 规则抽题,分数按照rule字段配置的计算
            $rule_data = unserialize($paper['rule']);

            // 组装试卷编号
            $k = self::ORDER_NUM;
            foreach ($et_ids as $v) {
                $arr = $topic_list[$v];
                // 获取试题配置分数
                $arr['score'] = $this->get_score_by_type($arr['et_type'], $rule_data);
                $total_score += intval($arr['score']);
                $arr['ep_id'] = $param['ep_id'];
                // 组装试题序号
                $arr['order_num'] = $k;
                $snapshot[] = $arr;

                $k++;
            }
        }

        if (intval($total_score) == 0) {
            // 试卷总分不能为0
            return false;
        }

        try {
            // 开始事务
            $this->start_trans();

            // 删除之前存入的试题
            $this->_d->delete_by_conds(['ep_id' => $param['ep_id']]);
            // 执行数据插入操作
            $this->_d->insert_all($snapshot);
            // 更新试卷总分
            $this->_d_paper->update($param['ep_id'], ['total_score' => $total_score]);

            // 提交事务
            $this->commit();
        } catch (\Think\Exception $e) {
            \Think\Log::record($e);
            // 事务回滚
            $this->_set_error($e->getMessage(), $e->getCode());
            $this->rollback();

            return false;
        } catch (\Exception $e) {

            \Think\Log::record($e);
            $this->_set_error($e->getMessage(), $e->getCode());
            // 事务回滚
            $this->rollback();

            return false;
        }

        return true;
    }

    /**
     * 获取试题快照列表
     *
     * @author  caijianhua
     *
     * @param array $conds 查询条件参数列表
     * @param array $page_option 分页参数
     * @param array $order_option 排序参数
     * @param string $fields 返回字段
     *
     * @return array|bool
     */
    public function conds_snapshot_list($conds, $page_option = null, $order_option = [], $fields = '*')
    {
        return $this->_d->conds_snapshot_list($conds, $page_option, $order_option, $fields);
    }

    /**
     * 获取试题快照总数
     *
     * @author  caijainhua
     *
     * @param array $conds 查询条件参数列表
     *
     * @return int
     */
    public function conds_snapshot_count($conds)
    {
        return $this->_d->conds_snapshot_count($conds);
    }
}