PublishExamController.class.php 2.82 KB
<?php
/**
 * 考试发布提醒
 */

namespace Frontend\Controller\Temp;

use Common\Common\Cache;
use Common\Service\AnswerService;
use Common\Service\PaperService;
use Common\Service\RightService;

class PublishExamController extends AbstractController
{
    /** @var  RightService  用户回复信息表 */
    protected $right_s;
    /** @var PaperService 试卷信息表 */
    protected $paper_s;
    /** @var AnswerService 回答信息表 */
    protected $answer_s;

    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {
            return false;
        }

        // 实例化权限表
        $this->right_s = new RightService();
        // 实例化试卷表
        $this->paper_s = new PaperService();
        // 实例化回答表
        $this->answer_s = new AnswerService();

        return true;
    }

    public function Index()
    {
        set_time_limit(0);
        // 获取需要发送的试卷id
        $ep_id = I('get.ep_id');

        // 非空判断
        if (empty($ep_id)) {
            return true;
        }
        // 获取试卷基本详情
        $data = $this->paper_s->get($ep_id);
        if (empty($data)) {
            return true;
        }

        $paper_info = [];
        try {
            // 实例化缓存
            $cache = &Cache::instance();

            // 获取当前试卷缓存信息
            $paper_info = $cache->get('Common.Exam_' . $ep_id);

        } catch (\Think\Exception $e) {
            \Think\Log::record($e);

        } catch (\Exception $e) {
            \Think\Log::record($e);

        }

        if (!empty($paper_info['join_uids'])) {

            $params['uids'] = $paper_info['join_uids'];

        } else {
            $conds = array(
                'epc_id' => $ep_id,
                'er_type' => AnswerService::RIGHT_PAPER
            );

            $right_serv = new RightService();

            // 参与考试的权限范围
            if ($data['is_all'] != RightService::AUTH_ALL) {

                $rights = $right_serv->list_by_conds($conds);
                // 格式化权限范围
                $right_view = $right_serv->format_db_data($rights);
            }

            $right_view['is_all'] = $data['is_all'];

            // 获取要参加考试的全部人员
            $all_join = $right_serv->list_uids_by_right($right_view);

            $params['uids'] = $all_join;
        }


        $params['name'] = $data['ep_name'];
        $params['description'] = $data['intro'];
        $params['img_id'] = $data['cover_id'];
        $params['is_cover_open'] = $data['is_cover_open'];
        $params['begin_time'] = $data['begin_time'];
        $params['end_time'] = $data['end_time'];
        $params['id'] = $ep_id;

        // 发送考试前提醒
        $this->paper_s->send_msg($params, 1);

        return true;
    }
}