ExamHelper.class.php 21.3 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 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
<?php
/**
 * Created by PhpStorm.
 * User: zhonglei
 * Date: 2017/10/10
 * Time: 15:01
 */

namespace Common\Common;

use Common\Model\ExamAnswerModel;
use Common\Model\QuestionnaireAnswerModel;
use Common\Model\ExamCategoryModel;
use Common\Model\ExamPaperModel;
use Common\Model\TrainRightUsersModel;

class ExamHelper
{

    /**
     * @var ExamAnswerModel
     */
    protected $answerModel = null;

    /**
     * 构造方法
     */
    public function __construct()
    {
        $this->answerModel = new ExamAnswerModel();
    }

    /**
     * 实例化
     * @return ExamHelper
     */
    public static function &instance()
    {
        static $instance;

        if (empty($instance)) {
            $instance = new self();
        }

        return $instance;
    }

    /**
     * 获取考试概览数据
     * @author houyingcai
     *
     * @param string $start_time
     * @param string $end_time
     *
     * @return array
     */
    public function get_exam_data($start_time, $end_time)
    {
        $data = [];

        $paperModel = new ExamPaperModel();

        $test_conds = [
            'exam_status > ?' => Constant::EXAM_DRAFT,
            'exam_type' => Constant::EXAM_TYPE,
            'paper_type' => Constant::PAPER_TYPE_TEST,
        ];
        // 平台现有正式考试总数
        $data['paper_test_nums'] = (int)$paperModel->count_by_conds($test_conds);

        $mock_conds = [
            'exam_status > ?' => Constant::EXAM_DRAFT,
            'exam_type' => Constant::EXAM_TYPE,
            'paper_type' => Constant::PAPER_TYPE_MOCK,
        ];
        // 平台现有模拟考试总数
        $data['paper_mock_nums'] = (int)$paperModel->count_by_conds($mock_conds);

        // 平台现有考试总数
        $data['paper_nums'] = $data['paper_test_nums'] + $data['paper_mock_nums'];

        if (!empty($start_time) && !empty($end_time)) {

            // 时间周期内,新增正式考试数量
            $test_conds['created >= ?'] = $start_time;
            $test_conds['created <= ?'] = $end_time;
            $data['paper_test_add'] = (int)$paperModel->count_by_conds($test_conds);

            // 时间周期内,新增模拟考试数量
            $mock_conds['created >= ?'] = $start_time;
            $mock_conds['created <= ?'] = $end_time;
            $data['paper_mock_add'] = (int)$paperModel->count_by_conds($mock_conds);

            // 获取试卷周期内未结束的考试ep_id 列表
            $time_papers = $paperModel->list_by_conds(
                [
                    'exam_type' => Constant::EXAM_TYPE,
                    'exam_status > ?' => Constant::EXAM_DRAFT,
                    'end_time >= ?' => $start_time
                ],
                null,
                [],
                'ep_id'
            );
            $time_ep_ids = array_column($time_papers, 'ep_id');

            // 时间周期内,考试参与次数
            $join_conds = [
                'ep_id' => $time_ep_ids ? $time_ep_ids : '',
                'my_end_time > ?' => 0,
                'created >= ?' => $start_time,
                'created <= ?' => $end_time,
            ];

            $data['exam_join_nums'] = (int)$this->answerModel->count_by_conds($join_conds);

            // 统计周期内考试参与人数
            $join_user_nums = (int)$this->answerModel->count_join_users([
                'ep_id' => $time_ep_ids ? $time_ep_ids : '',
                'start_time' => $start_time,
                'end_time' => $end_time
            ]);

            $userServ = &User::instance();
            $users = $userServ->listByConds(['memSubscribeStatus' => 1], 1, 1);
            // 考试覆盖率
            $rate = $join_user_nums / $users['total'];
            $data['exam_coverage'] = (sprintf("%.4f", $rate) * 10000)/100;

            // 考试及格次数
            $pass_conds = [
                'ep_id' => $time_ep_ids ? $time_ep_ids : '',
                'my_is_pass' => Constant::PAPER_PASS_TRUE,
                'created >= ?' => $start_time,
                'created <= ?' => $end_time,
            ];
            $data['exam_pass_nums'] = (int)$this->answerModel->count_by_conds($pass_conds);

            // 考试及格率
            $pass_rate = $data['exam_pass_nums'] / $data['exam_join_nums'];
            $data['exam_pass_coverage'] = (sprintf("%.4f", $pass_rate) * 10000)/100;
        }

        return $data;
    }

    /**
     * 获取学霸排行列表
     * @author houyingcai
     *
     * @param array $conds 条件数组
     * @param int|array $page_option 分页参数
     * @param array $order_option 排序
     * @param string $fields 读取字段
     *
     * @return array
     */
    public function super_scholar_rank($conds, $page_option = null, $order_option = [], $fields = '*')
    {
        return $this->answerModel->super_scholar_rank($conds, $page_option, $order_option, $fields);
    }

    /**
     * 根据用户uid集合查询用户考试、调研、培训的参与数据
     *
     * @param array $user_list 用户数组列表
     *
     * @return array
    array(
     * 'uid1' => array(
     * 'exam_pass_total' => 9, // 考试及格数
     * 'exam_total' => 1, // 可考试总数
     * 'questionnaire_total' => 5, // 参加调研数
     * 'train_total' => 10, // 参加培训次数
     * ),
     * )
     */
    public function list_exam_train_questionnaire_data($user_list = [])
    {
        $uids = array_column($user_list, 'memUid');

        if (empty($uids)) {
            return [];
        }

        // 此处查询用户参与考试且及格的考试记录
        $exam_answer_conds = [
            'uid' => $uids,
            'my_is_pass' => Constant::MY_EXAM_PASS, // 通过考试
            'data_type' => Constant::DATA_TYPE_NOMAL // 常规试卷
        ];

        $pass_list = $this->answerModel->list_answer($exam_answer_conds, 'ea_id,ep_id,uid,my_is_pass');

        // 此处查询用户参与调研的记录
        $questionnaire_answer = new QuestionnaireAnswerModel();
        $questionnaire_conds = [
            'uid' => $uids,
            'data_type' => Constant::DATA_TYPE_NOMAL // 常规调研
        ];
        $answer_list = $questionnaire_answer->list_answer_conds($questionnaire_conds);
        // 转换成以用户id为主键的二维数组
        $questionnaire_answer_list = array_combine_by_key($answer_list, 'uid');
        unset($answer_list);
        // 此处查询用户参与培训记录
        $train = new TrainRightUsersModel();
        // 组装获取用户参与培训记录的参数
        $train_conds = [
            'a.ru_uid' => $uids,
        ];
        // 获取用户报名列表数据(包含未报名成功的记录)
        $right_user_list = $train->list_right_users_conds($train_conds,
            'ru_sign_status,ru_pay_status,ru_check_status,ru_uid,ed_is_sign_up,ed_is_charge,ed_is_check');

        $train_join_list = [];
        foreach ($right_user_list as $v) {

            // 组装培训数据
            $education = [
                'ed_is_sign_up' => $v['ed_is_sign_up'],
                'ed_is_charge' => $v['ed_is_charge'],
                'ed_is_check' => $v['ed_is_check']
            ];
            // 组装报名数据
            $right_users = [
                'ru_sign_status' => $v['ru_sign_status'],
                'ru_pay_status' => $v['ru_pay_status'],
                'ru_check_status' => $v['ru_check_status'],
            ];

            $sign_status = $this->get_sign_up_status($education, $right_users);

            if (Constant::EDUCATION_SIGN_SUCCESS == $sign_status) {
                // 如果用户报名状态为成功,则将该条记录加入该用户下
                $train_join_list[$v['ru_uid']][] = $v;
            }
        }

        unset($right_user_list);

        $data = [];

        // 将用户信息转换成以uid为主键的二维数组
        $user_list = array_combine_by_key($user_list, 'memUid');

        $exam_paper = new ExamPaperModel();

        // 此处查询权限为全公司的常规试卷列表
        $exam_is_all_list = $exam_paper->list_by_conds([
            'is_all' => Constant::EXAM_RIGHT_ALL,
            'exam_status' => [Constant::PAPER_PUBLISH, Constant::PAPER_STOP], // 试卷状态已发布或者已终止
            'cate_status' => Constant::EC_OPEN_STATES, // 分类启用
            'exam_type' => Constant::EXAM_TYPE_NOMAL //  常规试卷
        ], null, [], 'ep_id,ep_name');

        foreach ($uids as $v) {
            // 参加培训数
            $data[$v]['train_total'] = count($train_join_list[$v]);
            // 参加调研数
            $data[$v]['questionnaire_total'] = intval($questionnaire_answer_list[$v]['total']);

            // 此处获取该用户有权限的试卷集合(不包含全公司权限的试卷)
            $user_info = $user_list[$v];
            $ep_list = $exam_paper->list_paper_by_user($user_info);
            // 合并全公司权限试卷和用户权限的试卷列表
            $ep_list = array_merge($exam_is_all_list, $ep_list);

            if (empty($ep_list)) {
                $data[$v]['exam_pass_total'] = 0;  // 通过试卷
                $data[$v]['exam_total'] = 0; // 可参与试卷
                continue;
            }

            $ep_ids = array_filter(array_column($ep_list, 'ep_id'));
            unset($ep_list);
            // 循环用户考试通过的记录,获取当前用户的通过数(因为试卷删除时并不删除回答记录)
            $pass_total = 0;
            foreach ($pass_list as $_v) {
                if (in_array($_v['ep_id'], $ep_ids) && $_v['uid'] == $v) {
                    $pass_total++;
                }
            }

            $data[$v]['exam_pass_total'] = $pass_total;  // 通过试卷
            $data[$v]['exam_total'] = count($ep_ids); // 可参与试卷

        }

        unset($exam_is_all_list);
        unset($user_list);
        unset($pass_list);
        unset($train_join_list);

        return $data;
    }


    /**
     * 根据培训设置信息以及用户报名记录判断用户是否报名成功
     *
     * @param array $education 培训设置信息
     * @param array $right_users 用户报名状态信息
     *
     * @return int 报名状态(0:未报名,1:报名成功,2:报名待审核,3:报名未通过)
     */
    private function get_sign_up_status($education = [], $right_users = [])
    {

        // 默认未报名
        $sign_up_status = Constant::EDUCATION_SIGN_NO;
        // 是否开启报名
        $ed_is_sign_up = $education['ed_is_sign_up'];

        // 未开启报名
        if (Constant::EDUCATION_SIGN_CLOSE == $ed_is_sign_up) {
            // 报名成功
            $sign_up_status = Constant::EDUCATION_SIGN_SUCCESS;
        }

        // 开启报名
        if (Constant::EDUCATION_SIGN_OPEN == $ed_is_sign_up) {

            // 报名状态
            $ru_sign_status = $right_users['ru_sign_status'];
            // 支付状态
            $ru_pay_status = $right_users['ru_pay_status'];
            // 审核状态
            $ru_check_status = $right_users['ru_check_status'];

            // 是否收费
            $ed_is_charge = $education['ed_is_charge'];
            // 是否审核
            $ed_is_check = $education['ed_is_check'];

            // 不收费、不审核、状态为已报名
            if (Constant::EDUCATION_UN_CHARGE == $ed_is_charge && Constant::EDUCATION_UN_CHECK == $ed_is_check
                && Constant::EDUCATION_SIGN_STATUS_SIGNED == $ru_sign_status
            ) {

                // 报名成功
                $sign_up_status = Constant::EDUCATION_SIGN_SUCCESS;
            }

            // 收费、不审核、状态为已支付
            if (Constant::EDUCATION_CHARGE == $ed_is_charge && Constant::EDUCATION_UN_CHECK == $ed_is_check
                && Constant::SIGN_CHARGE_PAY == $ru_pay_status
            ) {

                // 报名成功
                $sign_up_status = Constant::EDUCATION_SIGN_SUCCESS;
            }

            // 不收费、审核
            if (Constant::EDUCATION_UN_CHARGE == $ed_is_charge && Constant::EDUCATION_CHECK == $ed_is_check) {

                // 待审核
                if (Constant::SIGN_CHECK_WAIT == $ru_check_status && Constant::EDUCATION_SIGN_STATUS_SIGNED == $right_users['ru_sign_status']) {
                    // 报名待审核
                    $sign_up_status = Constant::EDUCATION_SIGN_WAIT_CHECK;
                }

                // 审核通过
                if (Constant::SIGN_CHECK_PASS == $ru_check_status) {
                    // 报名成功
                    $sign_up_status = Constant::EDUCATION_SIGN_SUCCESS;
                }

                // 审核不通过
                if (Constant::SIGN_CHECK_UN_PASS == $ru_check_status) {
                    // 报名未通过
                    $sign_up_status = Constant::EDUCATION_SIGN_UN_PASS;
                }
            }
        }

        return $sign_up_status;
    }

    /**
     * 获取考试分类状态为已开启的分类列表
     * @author houyingcai
     *
     * @return array
     */
    public function exam_category_open_list()
    {
        $category_model = new ExamCategoryModel();

        $conds = ['ec_status' => Constant::EC_OPEN_STATES];
        $order_option = ['order_num' => 'ASC', 'ec_id' => 'DESC'];
        $fields = 'ec_id,ec_name';

        return $category_model->list_by_conds($conds, null, $order_option, $fields);
    }

    /**
     * 获取考试分类列表
     * @author houyingcai
     *
     * @return array|bool
     */
    public function exam_category_list()
    {
        $category_model = new ExamCategoryModel();

        $order_option = ['order_num' => 'ASC', 'ec_id' => 'DESC'];

        $list = $category_model->list_all(null, $order_option);

        return array_combine_by_key($list, 'ec_id');
    }

    /**
     * 根据UID统计用户考试次数
     * @author houyingcai
     *
     * @param string $uid 员工UID
     *
     * @return int
     */
    public function count_user_data_exam($uid)
    {
        return $this->answerModel->count_user_data_exam($uid);
    }

    /**
     * 根据UID获取员工考试记录
     * @author houyingcai
     *
     * @param string $uid 员工UID
     * @param array $page_option 分页
     * @param array $order_option 排序
     *
     * @return array
     */
    public function list_user_data_exam($uid, $page_option = null, $order_option = [])
    {
        return $this->answerModel->list_user_data_exam($uid, $page_option, $order_option);
    }

    /**
     * 员工考试统计列表总数
     * @author houyingcai
     *
     * @param array $conds 员工UID
     *
     * @return int
     */
    public function count_user_exam_count($conds)
    {
        return $this->answerModel->count_user_exam_count($conds);
    }

    /**
     * 获取员工考试统计列表
     *
     * @param array $conds 查询条件
     * @param array $page_option 分页
     * @param array $order_option 排序
     *
     * @return array|bool
     */
    public function list_user_exam_count($conds, $page_option, $order_option)
    {
        return $this->answerModel->list_user_exam_count($conds, $page_option, $order_option);
    }

    /**
     * 根据试卷ID列表,获取试卷详情列表
     * @author houyingcai
     *
     * @param array $ep_ids 试卷ID列表
     *
     * @return array|bool
     */
    public function list_by_paper_ids($ep_ids)
    {
        if (empty($ep_ids)) {

            return false;
        }
        $category_model = new ExamPaperModel();

        $order_option = ['ep_id' => 'DESC'];
        $fields = 'ep_id,ec_id,ep_name,paper_type,begin_time,end_time,paper_time';
        $list = $category_model->list_by_conds(['ep_id' => $ep_ids], null, $order_option, $fields);

        return array_combine_by_key($list, 'ep_id');
    }

    /**
     * 根据UID获取模拟考试记录
     *
     * @author: houyingcai
     *
     * @param int $ep_id 试卷ID
     * @param string $uid 当前用户ID
     *
     * @return array|bool
     */
    public function mock_record_list($ep_id, $uid)
    {
        //  按照发布时间排序
        $order_option = ['created' => 'DESC'];
        // 查询模拟考试记录总记录数
        $conds = [
            'ep_id' => $ep_id,
            'uid' => $uid,
            'answer_status >= ?' => Constant::EXAM_PAPER_WAIT_READ,
            'my_end_time > ?' => 0,
        ];
        // 查询字段
        $fields = 'ea_id,my_score,my_begin_time,my_time';
        // 模拟答卷列表
        $answer_list = $this->answerModel->list_by_conds($conds, null, $order_option, $fields);

        // 最高得分
        $high_score = 0;
        foreach ($answer_list as $v) {
            if ($v['my_score'] > $high_score) {

                $high_score = $v['my_score'];
            }
        }

        // 我的排名
        $ranking = $this->get_my_rank($ep_id, $uid);

        // 根据用户UID获取用户信息
        $userSer = &User::instance();

        // 获取用户详情
        $user_info = $userSer->getByUid($uid);

        $result = [
            'username' => $user_info['memUsername'],
            'avatar' => $user_info['memFace'],
            'high_score' => $high_score,
            'ranking' => $ranking,
            'list' => $answer_list,
        ];

        return $result;
    }

    /**
     * @author houyingcai
     * @desc 递归获取学霸排名
     *
     * @param string $starttime 开始时间
     * @param string $endtime 结束时间
     * @param int $limit 查询条数
     *
     * @return array
     */
    public function recursion_rank($starttime, $endtime, $limit)
    {
        static $page = 0;
        // 初始化学霸列表
        static $list = [];

        // 获取正规考试的ep_id列表
        $paperModel = new ExamPaperModel();
        $paper_list = $paperModel->list_by_conds(['exam_type' => Constant::EXAM_TYPE], null, [], 'ep_id');
        $ep_ids = array_column($paper_list, 'ep_id');
        // 查询条件
        $conds = [
            'created >= ?' => $starttime,
            'created <= ?' => $endtime,
            'ep_id' => $ep_ids ? $ep_ids : '',
            'answer_status' => 3, // 已批阅
        ];

        // 排序
        $order_by = [
            'avg_score' => 'DESC',
            'my_time' => 'ASC'
        ];

        // 查询字段
        $fields = 'uid,AVG(my_score) AS avg_score';

        // 分页
        $page_option = [$page, $limit];

        // 获取学霸排名列表数据
        $data = $this->super_scholar_rank($conds, $page_option, $order_by, $fields);
        // 数据为空时,直接返回学霸列表
        if (empty($data)) {

            return $list;
        }
        // 格式化数据
        $data = $this->format_recursion_data($data);

        $list = empty($list) ? $data : array_merge($list, $data);

        // 学霸列表数据个数少于规定展示的条数时,则递归查询
        if (count($list) < Constant::DATA_RANK_LIMIT) {

            $page++;
            $this->recursion_rank($starttime, $endtime, $limit);

        } elseif (count($list) > Constant::DATA_RANK_LIMIT) {
            // 学霸列表数据个数大于规定展示的条数时,截取至规定展示的条数
            $list = array_slice($list, 0, Constant::DATA_RANK_LIMIT);
        }

        return array_values($list);
    }

    /**
     * @author houyingcai
     * @desc 格式化数据
     *
     * @param array $data 需要格式化的数据
     *
     * @return array
     */
    private function format_recursion_data($data)
    {
        // 获取用户UID
        $uids = array_column($data, 'uid');

        // 获取已关注的用户详情
        $userServ = &User::instance();
        $users = $userServ->listUsersAll(['memUids' => $uids, 'memSubscribeStatus' => 1]);

        // 重组为以用户UID为键的二维数组
        $users = array_column($users, null, 'memUid');

        // 组装返回的数据
        $format_data = [];
        foreach ($data as $v) {
            if ($users[$v['uid']]) {
                $format_data[$v['uid']] = [
                    'uid' => $v['uid'],
                    'avg_score' => $v['avg_score']
                ];
            }
        }

        return $format_data;
    }

    /**
     * 获取我的考试排名
     *
     * @param int $ep_id 试卷ID
     * @param string $uid 用户ID
     *
     * @return int|string 我的排名
     */
    private function get_my_rank($ep_id = 0, $uid = '')
    {
        // 查询当前试卷的考试记录列表
        $data = $this->answerModel->list_by_conds(['ep_id' => $ep_id], null, [], 'uid,ea_id,my_score,my_time');

        foreach ($data as $key => $row) {
            $my_score[$key] = $row['my_score'];
            $my_time[$key] = $row['my_time'];
        }
        // 使用 成绩倒序,时间升序 排序
        array_multisort($my_score, SORT_DESC, $my_time, SORT_ASC, $data);

        $data_new = [];
        foreach ($data as $k => $v) {
            if (!array_key_exists($v['uid'], $data_new)) {

                $data_new[$v['uid']] = $v;
            } elseif ($v['my_score'] > $data_new[$v['uid']]['my_score']) {

                $data_new[$v['uid']]['my_score'] = $v['my_score'];
            }
        }

        $ranking = 0;
        $k = 0;
        foreach ($data_new as $v) {
            //排名
            if ($uid == $v['uid']) {
                $ranking = $k + 1;
                break;
            }
            $k++;
        }

        return $ranking;
    }

}