AnswerTempService.class.php 1.18 KB
<?php
/**
 * 随机答卷临时表
 * Created by PhpStorm.
 * User: yingcai
 * Date: 2017/8/9
 * Time: 下午3:35
 */

namespace Common\Service;

use Common\Model\AnswerDetailTempModel;
use Common\Model\AnswerTempModel;

class AnswerTempService extends AbstractService
{

    /** @var AnswerDetailTempModel */
    protected $answer_dt_model;
    // 构造方法
    public function __construct()
    {

        $this->_d = new AnswerTempModel();
        $this->answer_dt_model = new AnswerDetailTempModel();
        parent::__construct();
    }

    /**
     * 根据试卷ID获取一份答卷
     *
     * @param $ep_id int 试卷ID
     *
     * @return array
     */
    public function get_answer_temp($ep_id)
    {
        $data = [];

        // 获取所有临时试卷ID列表
        $eat_ids = $this->_d->list_by_conds(['ep_id' => $ep_id], null, [], 'eat_id');
        $eat_ids = array_column($eat_ids, 'eat_id');

        // 随机获取一个临时试卷ID
        $eat_id = array_rand($eat_ids);

        if (!$eat_id) {
            // 根据答卷ID获取答卷详情
            $data = $this->answer_dt_model->list_by_conds(['eat_id' => $eat_id]);
        }

        return $data;
    }
}