InstallController.class.php 18.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
<?php
/**
 * 套件应用安装初始化数据
 * 考试
 * @author zhoutao
 * @date 2017-09-27 10:05:30
 */

namespace Rpc\Controller\Common;

use Common\Common\User;
use Common\Service\AnswerService;
use Common\Service\BankService;
use Common\Service\CategoryService;
use Common\Service\PaperService;
use Common\Service\SettingService;
use Common\Service\SnapshotService;
use Common\Service\TopicService;
use Think\Exception;
use Think\Log;
use VcySDK\Cron;
use VcySDK\Service;

class InstallController extends AbstractController
{
    // 应用状态,可用
    const PLUGIN_STATE_AVAILABLE = 1;
    // 应用状态,不可用
    const PLUGIN_STATE_UNAVAILABLE = 2;

    /** @var  AnswerService */
    protected $answerServ;
    /** @var  SettingService */
    protected $settingServ;
    /** @var  BankService */
    protected $bankServ;
    /** @var  TopicService */
    protected $topicServ;
    /** @var  CategoryService */
    protected $categoryServ;
    /** @var  PaperService */
    protected $paperServ;
    /** @var  SnapshotService */
    protected $snapshotServ;

    public function Index()
    {
        // 实例化答卷表
        $this->answerServ = new AnswerService();
        // 实例化设置表
        $this->settingServ = new SettingService();
        // 实例化题库表
        $this->bankServ = new BankService();
        // 实例化题目表
        $this->topicServ = new TopicService();
        // 实例化分类表
        $this->categoryServ = new CategoryService();
        // 实例化试卷表
        $this->paperServ = new PaperService();
        // 实例化试卷试题快照表
        $this->snapshotServ = new SnapshotService();

        // 安装考试统计定时任务
//        $this->userCacheCronAdd();

        // 查询是否已经有分类数据
        $is_category = $this->categoryServ->count();

        if ($is_category > 0) {

            return true;
        }

        try {
            $this->categoryServ->start_trans();

            // 安装默认分类
            $this->default_category_data();
            // 安装默认题库
            $this->default_bank_data();
            // 安装默认试卷
            $this->default_paper_data();

            $this->categoryServ->commit();

        } catch (Exception $e) {
            // 回滚数据
            $this->categoryServ->rollback();
        }
            
        return true;
    }

    /**
     * 安装人员缓存更新
     *
     * @return bool
     */
    protected function userCacheCronAdd()
    {
        $setting = $this->settingServ->get_by_conds(array('key' => 'exam_user_cache'));
        $params = array(
            'crRemark' => 'exam_user_cache', // 考试统计
            'crCron' => '0 0 0/1 * * ?',// 每小时触发
            'crDescription' => '考试人员缓存',
            'crType' => '2',
            'crReqUrl' => oaUrl('Frontend/Callback/UserCache'), // 回调地址
            'crMonitorExecution' => 0,
            'crTimes' => '0'
        );

        $cronSdk = new Cron(Service::instance());
        // 判断计划任务标识,如果不存在添加,存在更新
        if (empty($setting['value'])) {

            $crId_data = $cronSdk->add($params);
            if (!empty($crId_data)) {
                $crId = $crId_data['crId'];
                $data = array(
                    'key' => 'exam_user_cache',
                    'value' => $crId,
                );
                $this->settingServ->insert($data);
                Log::record("add task: {$crId}", Log::INFO);
            }
        }

        return true;
    }

    /**
     * 安装默认分类数据
     *
     * @return bool
     */
    protected function default_category_data()
    {
        $category = array(
            array(
                'ec_name' => '业务技能考核',
                'ec_desc' => '',
                'ec_status' => 1,
                'is_all' => 1
            ),
            array(
                'ec_name' => '岗位知识考核',
                'ec_desc' => '',
                'ec_status' => 1,
                'is_all' => 1
            ),
            array(
                'ec_name' => '管理常识考核',
                'ec_desc' => '',
                'ec_status' => 1,
                'is_all' => 1
            ),
            array(
                'ec_name' => '公司制度考核',
                'ec_desc' => '',
                'ec_status' => 1,
                'is_all' => 1
            ),

        );

        $this->categoryServ->insert_all($category);

        return true;
    }

    /**
     * 安装题库默认数据
     *
     * @return bool
     */
    protected function default_bank_data()
    {
        // 默认题库入库
        $default_bank = array('eb_name' => '默认题库');
        $eb_id = $this->bankServ->insert($default_bank);
        
        if ($eb_id) {
            
            $topic_arr = array(
                array(
                    'eb_id' => $eb_id,
                    'et_type' => 1,
                    'order_num' => 1,
                    'score' => 10,
                    'title' => '考试中心哪一种试卷类型可以重复答题?',
                    'options' => serialize(
                        array(
                            array(
                                'option_name' => 'A',
                                'option_value' => '测评试卷',
                                'option_image_url'=>'',
                                'option_image_id'=>''
                            ),
                            array(
                                'option_name' => 'B',
                                'option_value' => '模拟试卷',
                                'option_image_url'=>'',
                                'option_image_id'=>''
                            ),
                            array(
                                'option_name' => 'C',
                                'option_value' => '正式试卷',
                                'option_image_url'=>'',
                                'option_image_id'=>''
                            ),
                            array(
                                'option_name' => 'D',
                                'option_value' => '测试试卷',
                                'option_image_url'=>'',
                                'option_image_id'=>''
                            )
                        )
                    ),
                    'answer' => 'B',
                    'answer_coverage' => 0,
                    'match_type' => 0,
                    'answer_keyword' => '',
                    'answer_resolve' => '考试中心试卷类型共有两种:测评试卷和模拟试卷,测评试卷适用于一次性考核,考试人员仅能参与一次;模拟试卷适用于多次考核,可以参与多次,重复答题。',
                ),
                array(
                    'eb_id' => $eb_id,
                    'et_type' => 4,
                    'order_num' => 2,
                    'score' => 20,
                    'title' => '考试中心题目类型分别有哪几种?',
                    'options' => serialize(
                        array(
                            array(
                                'option_name' => 'A',
                                'option_value' => '单选题',
                                'option_image_url'=>'',
                                'option_image_id'=>''
                            ),
                            array(
                                'option_name' => 'B',
                                'option_value' => '判断题',
                                'option_image_url'=>'',
                                'option_image_id'=>''
                            ),
                            array(
                                'option_name' => 'C',
                                'option_value' => '多选题',
                                'option_image_url'=>'',
                                'option_image_id'=>''
                            ),
                            array(
                                'option_name' => 'D',
                                'option_value' => '问答题',
                                'option_image_url'=>'',
                                'option_image_id'=>''
                            ),
                            array(
                                'option_name' => 'E',
                                'option_value' => '语音问答题',
                                'option_image_url'=>'',
                                'option_image_id'=>''
                            )
                        )
                    ),
                    'answer' => 'A,B,C,D,E',
                    'answer_coverage' => 0,
                    'match_type' => 0,
                    'answer_keyword' => '',
                    'answer_resolve' => '考试中心题目共有五种类型,分别为单选题,判断题,多选题,问答题,语音问答题。',
                ),
                array(
                    'eb_id' => $eb_id,
                    'et_type' => 2,
                    'order_num' => 3,
                    'score' => 20,
                    'title' => '如果有主观题,可以设置成手动阅卷?',
                    'options' => '',
                    'answer' => '对',
                    'answer_coverage' => 0,
                    'match_type' => 0,
                    'answer_keyword' => '',
                    'answer_resolve' => '当有主观题(问答题,语音问答题)时,可以设置“手动阅卷”模式,选择指定阅卷人在移动端进行批阅试卷。',
                ),
                array(
                    'eb_id' => $eb_id,
                    'et_type' => 2,
                    'order_num' => 4,
                    'score' => 20,
                    'title' => '考试中如果中途退出,考试倒计时将会继续计时?',
                    'options' => '',
                    'answer' => '对',
                    'answer_coverage' => 0,
                    'match_type' => 0,
                    'answer_keyword' => '',
                    'answer_resolve' => '当有主观题(问答题,语音问答题)时,可以设置“手动阅卷”模式,选择指定阅卷人在移动端进行批阅试卷。',
                ),
                array(
                    'eb_id' => $eb_id,
                    'et_type' => 3,
                    'order_num' => 5,
                    'score' => 30,
                    'title' => '考试试卷的出题规则分别有哪几种?回答顺序不分先后,采用分号隔开。',
                    'options' => '',
                    'answer' => '手动自主选题;固定规则抽题;随机生成试卷。',
                    'answer_coverage' => 100,
                    'match_type' => 1,
                    'answer_keyword' => serialize(
                        array(
                            array(
                                'keyword' => '手动自主选题',
                                'percent' => '30',
                            ),
                            array(
                                'keyword' => '固定规则抽题',
                                'percent' => '30',
                            ),
                            array(
                                'keyword' => '随机生成试卷',
                                'percent' => '40',
                            )
                        )
                    ),
                    'answer_resolve' => '考试试卷的出题规则共有三种,分别是:手动自主选题;固定规则抽题;随机生成试卷。其中,手动自主选题即自主选择题库内的题目;固定规则抽题则需要设置一定规则,然后系统按照所设置的规则随机在题库中抽取题目;随机生成试卷是指在移动端随机生成试卷,也需要设置好相应的抽题规则。',
                ),
            );

            // 题库添加题目
            $this->topicServ->insert_all($topic_arr);
            // 更新题库题目类型数量
            foreach ($topic_arr as $topic) {
                $this->topicServ->update_eb_topic_add($topic['et_type'], array('eb_id' => $eb_id));
            }
        }

        return true;
    }

    /**
     * 安装试卷默认数据
     *
     * @return bool
     */
    public function default_paper_data()
    {
        // 获取默认分类
        $category = $this->categoryServ->get_by_conds(
            array(
                'ec_name' => '业务技能考核',
            )
        );
        // 获取默认题库
        $bank = $this->bankServ->get_by_conds(
            array(
                'eb_name' => '默认题库',
            )
        );
        // 获取默认题库下的题目
        $topic_list = $this->topicServ->list_by_conds(
            array('eb_id' => $bank['eb_id']), 
            null, 
            [],
            'et_id,et_type,title,title_pic,score,options,answer,answer_resolve,answer_coverage,match_type,answer_keyword'
        );

        if (!$category || !$bank || !$topic_list) {

            return true;
        }

        $paper_data = array(
            'ep_name' => '【系统预设】考试基础功能测试',
            'ec_id' => $category['ec_id'],
            'paper_type' => 1,
            'exam_type' => 1,
            'ep_type' => 1,
            'search_type' => 1,
            'bank_data' => $bank['eb_id'],
            'tag_data' => '',
            'admin_id' => '',
            'launch_man' => '',
            'check_topic_data' => '',
            'reason' => '',
            'reason_user_id' => '',
            'reason_user' => '',
            'exam_status' => 2,
            'is_old' => 2,
            'cover_id' => '',
            'is_all' => 1,
            'begin_time' => '1506787200000',
            'end_time' => '1917014400000',
            'paper_time' => 10,
            'is_pushmsg' => 0,
            'is_recommend' => 1,
            'pass_score' => 60,
            'integral_action_type' => 2,
            'publish_time' => MILLI_TIME,
            'cate_status' => $category['ec_status'],
            'last_time' => MILLI_TIME,
            'topic_count' => count($topic_list),
            'intro' => '企业移动测评考核管理平台.帮助企业快速组织考试,及时了解员工的能力提升情况。采用多种模式、支持多种题型,随时进行测评,考核结果自动生成,统计情况一目了然。此试卷为预设内容,系统自动改卷,旨在帮助使用者熟悉了解考试应用。',
        );

        $ep_id = $this->paperServ->insert($paper_data);

        // 试卷总分
        $total_score = 0;
        $snapshot = [];

        // 自主选题,分数按照题目分数计算
        foreach ($topic_list as $k => $v) {
            $total_score += intval($v['score']);
            $v['ep_id'] = $ep_id;
            // 组装试题序号
            $v['order_num'] = $k + 1;
            $snapshot[] = $v;
        }

        // 执行数据插入操作
        $this->snapshotServ->insert_all($snapshot);

//        // 实例化定时任务类
//        $cron_serv = new Cron(Service::instance());
//
//        $json_params = json_encode(array('ep_id' => $ep_id));
//
//        // 考试结束10分钟后系统自动将未交卷的试卷交卷(白卷)
//        $auto_submit_time = $paper_data['end_time'] + 600 * 1000;
//
//        // 考试结束系统交卷
//        $conds_remind = array(
//            'crRemark' => 'cron_exam',
//            'crType' => 2,
//            'crParams' => $json_params,
//            'crMethod' => 'POST',
//            'crReqUrl' => oaUrl('Frontend/Callback/AnswerBegin'), // 回调地址
//            'crTimes' => 1,
//            'crCron' => rgmdate((String)$auto_submit_time, 's i G j n ? Y'),
//            'crMonitorExecution' => 0,
//            'crDescription' => '考试时间到',
//        );
//        // 创建定时任务
//        $res_remind = $cron_serv->add($conds_remind); // crId
//        $corn_exam = $res_remind['crId'];
//
//        // 创建考试统计定时任务
//        $paper_statistics = array(
//            'crRemark' => 'cron_statistics',
//            'crType' => 2,
//            'crParams' => $json_params,
//            'crMethod' => 'POST',
//            'crReqUrl' => oaUrl('Frontend/Callback/Statistics'), // 回调地址
//            'crTimes' => 0,
//            'crCron' => '0 0/10 * * * ?',
//            'crMonitorExecution' => 0,
//            'crDescription' => '考试统计',
//        );
//        $res_cron_statistics = $cron_serv->add($paper_statistics); // crId
//        $cron_statistics = $res_cron_statistics['crId'];
//
//        // 排名列表定时任务ID
//        $cron_rank = array(
//            'crRemark' => 'corn_rank_list_update',
//            'crType' => 2,
//            'crParams' => $json_params,
//            'crMethod' => 'POST',
//            'crReqUrl' => oaUrl('Frontend/Callback/RankList'), // 回调地址
//            'crTimes' => 0,
//            'crCron' => '0 0/5 * * * ?',
//            'crMonitorExecution' => 0,
//            'crDescription' => '更新考试成绩排名列表',
//        );
//        $res_rank_data = $cron_serv->add($cron_rank); // crId
//        $cron_rank_id = $res_rank_data['crId'];

        // 更新试卷总分和定时任务
        $this->paperServ->update(
            $ep_id,
            array(
                'total_score' => $total_score,
//                'corn_exam' => $corn_exam,
//                'cron_statistics' => $cron_statistics,
//                'cron_rank_id' => $cron_rank_id
            )
        );

        // 首页feed流推荐
        $feed_right = [];
        $url = rpcUrl('/Public/Rpc/Recommender/ArticleUpdate');
        $data_send = [
            'app' => APP_DIR,
            'dataCategoryId' => '',
            'dataId' => $ep_id,
            'title' => $paper_data['ep_name'],
            'summary' => $paper_data['intro'],
            'dataType' => '模拟',
            'endTime' => $paper_data['end_time'],
            'attachId' => '',
            'pic' => '',
            'url' => 'Exam/Frontend/Index/Msg?ep_id=' . $ep_id,
            'right' => $feed_right
        ];

        \Com\Rpc::phprpc($url)->invoke('Index', $data_send);


        // 获取全部人员
        $userServ = &User::instance();
        $result = $userServ->listByConds([]);

        $uids = $result['total'];

        // 更新试卷未参与人数(如果企业人数太多,可能会造成请求UC超时,在此处更新目的是为了不影响安装)
        $this->paperServ->update(
            $ep_id,
            array(
                'unjoin_count' => $uids,
            )
        );

        return true;
    }
}