SourceInfoController.class.php 39.7 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 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
<?php
/**
 * Created by PhpStorm.
 * User: tangxingguo
 * Date: 2017/5/5
 * Time: 10:23
 */

namespace Api\Controller\Course;

use Com\PackageValidate;
use Common\Common\ArticleHelper;
use Common\Common\Constant;
use Common\Common\Integral;
use Common\Common\RpcFavoriteHelper;
use Common\Common\TaskCenter;
use Common\Common\StudyMap;
use Common\Common\Train;
use Common\Common\User;
use Common\Common\Teacher;
use Common\Service\ArticleChapterService;
use Common\Service\ArticleService;
use Common\Service\AwardService;
use Common\Service\ClassService;
use Common\Service\ExamService;
use Common\Service\LikeService;
use Common\Service\RightService;
use Common\Service\SourceAttachService;
use Common\Service\SourceService;
use Common\Service\StudyRecordService;
use Common\Service\StudyService;
use Common\Service\StudyTimeService;
use Common\Service\ItemService;

class SourceInfoController extends \Api\Controller\AbstractController
{
    /**
     * 是否必须登录
     */
    protected $_require_login = false;
    /**
     * Info
     * @author tangxingguo
     * @desc 课程详情接口
     * @param int article_id:true 课程ID
     * @param Int article_chapter_id 章节ID
     * @param int source_id 素材ID
     * @param array award_ids 激励IDS
     * @param int is_share:false:1 是否为分享入口(1=不是;2=是)
     * @param int customtask_id 任务ID
     * @param int plan_id 培训计划ID
     * @param int ed_id  培训ID
     * @param int map_id 学习地图ID
     * @param int path_id 学习路径ID
     * @param Int is_preview:true 是否预览 1=预览,0=非预览
     * @return array 课程详情
                    array(
                        'article_id' => 123, // 课程ID
                        'prev' => array(
                            'source_id' => 1, // 上一节素材id,没有上一节值为0
                            'article_chapter_id' => 2, // 上一节素材所在章节id,没有值为0
                        ),
                        'next' => array(
                            'source_id' => 3, // 下一节素材id,没有下一节值为0
                            'article_chapter_id' => 2, // 下一节素材所在章节id,没有值为0
                        ),
                        'data_id' => 'b3ddbc502e307665f346cbd6e52cc10d', // 数据标识
                        'course_type' => 1, // 课程类别(1=常规课程;2=任务类课程;3=线下培训类课程)
                        'article_type' => 1, // 课程类型(1=单课程;2=系列课程)
                        'article_status' => 2, // 课程状态(1=草稿;2=已发布)
                        'article_title' => '好好学', // 课程标题
                        'cm_id' => 1, // 能力模型ID
                        'cm_name' => '超能力', // 能力模型名称
                        'update_time' => 1493709035000, // 课程发布时间
                        'study_total' => 100, // 学习人数
                        'cover_url' => 'http://qy.vchangyi.org', // 封面图片地址
                        'is_show_cover' => 2, // 是否正文显示封面图片(1=不显示,2=显示)
                        'sources' => array( // 素材
                            'source_id' => 'xxx', // 素材主键ID
                            'source_type' => 1, // 素材类型(1=图文素材;2=音图素材;3=视频素材;4=文件素材;5=外部素材)
                            'source_title' => '素材1', // 素材名称
                            'author' => '吆西', // 作者
                            'content' => 'aaa', // 内容描述
                            'user_study_time' => 6, // 用户学习时长,单位:分钟
                            'study_time' => 10, // 限制学习时长,单位:分钟
                            'link' => 'http://www.baidu.com/001.mp3', // 外部素材url(source_type=5时使用)
                            'audio_imgs' => array( // 音图内容(source_type=2时使用)
                                array(
                                'at_id' => '111', // 图片附件ID
                                'at_url' => 'http://www.vchangyi.com/001.jpg', // 图片附件地址
                                'audios' => array( // 音频附件
                                    array(
                                        'at_id' => '111', // 音频附件ID
                                        'at_name' => 'ccc', // 音频附件名称
                                        'at_time' => '123432', // 音频附件时长(毫秒)
                                        'at_url' => 'http://www.vchangyi.com/001.mp3', // 音频附件地址
                                    ),
                                )
                                ),
                            ),
                            'videos' => array( // 视频内容(source_type=3时使用)
                                array(
                                    'at_id' => '8765432345', // 视频附件ID
                                    'at_name' => 'aaa', // 视频附件名称
                                    'at_time' => '12345432', // 视频附件时长(毫秒)
                                    'at_size' => '123233', // 视频附件尺寸(单位字节)
                                    'at_convert_url' => 'http://www.vchangyi.com/001.mp4', // 视频附件转码后的Url(文件、视频附件转码成功后才有值)
                                    'at_cover_url' => 'http://www.vchangyi.com/001.jpg', // 视频封面地址
                                    'at_suffix' => 'xml', // 附件后缀
                                ),
                            ),
                            'files' => array( // 文件内容(source_type=4时使用,只有1个附件)
                                array(
                                    'at_id' => '8765432345', // 文件附件ID
                                    'at_name' => 'aaa', // 文件附件名称
                                    'at_time' => '12345432', // 文件附件时长(毫秒)
                                    'at_size' => '123233', // 文件附件尺寸(单位字节)
                                    'at_url' => 'http://www.vchangyi.com/001.jpg', // 文件附件地址
                                    'at_convert_url' => 'http://www.vchangyi.com/001.doc', // 文件附件转码后的Url(文件、视频附件转码成功后才有值)
                                    'at_suffix' => 'xml', // 附件后缀
                                )
                            ),
                            'attachs' => array( // 附件(source_type=1、2、3时使用,最多5个附件)
                                array(
                                    'at_id' => '8765432345', // 附件ID
                                    'at_name' => 'aaa', // 附件名称
                                    'at_time' => '12345432', // 附件时长(毫秒)
                                    'at_size' => '123233', // 附件尺寸(单位字节)
                                    'at_url' => 'http://www.vchangyi.com/001.jpg', // 附件地址
                                    'at_convert_url' => 'http://www.vchangyi.com/001.xml', // 附件转码后的Url(文件、视频附件转码成功后才有值)
                                    'at_suffix' => '.xml', // 附件后缀
                                ),
                             ),
                            'is_download' => 1, // 附件是否支持下载(1=不支持;2=支持)
                            'source_status' => 2, // 素材状态(1=转码中;2=正常)
                        ),
                        'summary' => '零食增加卫龙系列', // 摘要
                        'class_id' => 2, // 分类ID
                        'class_name' => '内部课程', // 分类名称
                        'ea_name' => '张三', // 创建者
                        'content' => '系列课程介绍', // 系列课程介绍
                        'tags' => '哈哈 啊啊', // 课程标签
                        'is_secret' => 1, // 是否保密(1=不保密,2=保密)
                        'is_share' => 1, // 允许分享(1=不允许,2=允许)
                        'is_comment' => 1, // 评论功能(1=不开启,2=开启)
                        'is_like' => 1, // 点赞功能(1=不开启,2=开启)
                        'is_exam' => 1, // 是否开启测评(1=未开启;2=已开启)
                        'exam_pass' => 1, // 当前课程评测状态(1=未通过;2=已通过)
                        'exam_url' => 'http://qy.vchangyi.com/hahaha.html', // 测评地址(测评开启且为通过测评的情况下返回)
                        'is_step' => 1, // 是否开启闯关(1=未开启;2=已开启)
                        'my_is_like' => 1, // 我是否点赞(1=未点赞,2=已点赞)
                        'is_outside' => 1, // 是否为公司外部人员(1=不是,2=是)
                        'my_is_favorite' => 1, // 我是否收藏(1=未收藏,2=已收藏)
                        'like_list' => array ( // 点赞列表(头像url)
                            'total' => 1000, // 点赞总数
                            'index' => 0, // 我在点赞人员头像列表的位置下标(我未点赞时,返回空字符串)
                            'face_list' => array( // 点赞人员头像列表
                                array(
                                    'uid' => 'uid1',
                                    'face' => 'http://www.vchangyi.com/001.jpg',
                                ),
                                array(
                                    'uid' => 'uid2',
                                    'face' => 'http://www.vchangyi.com/002.jpg',
                                ),
                            )
                        ),
                        'award' => array( // 激励信息
                            'award_id' => 1, // 激励ID
                            'award_action' => '第一个激励', // 激励行为
                            'description' => '第一个激励描述', // 描述
                            'award_type' => 1, // 激励类型(1=勋章;2=积分)
                            'medals' => array( // 勋章
                                'im_id' => 3, // 勋章ID
                                'icon' => 'http://qy.vchangyi.com/icon.jpg', // 勋章图标URL或者前端路径
                                'icon_type' => 1, // 勋章图标来源(1=用户上传;2=系统预设)
                                'name' => '勋章1', // 勋章名称
                                'desc' => '这是一个勋章', // 勋章描述
                            ),
                            'integral' => 3, // 积分
                        ),
                        'course_nature' => 1, // 课程性质(1=必修课;2=选修课;3=公共课)
                        'teacher_task_id' => 1, // 讲师授课任务ID
                        'is_reward' => 1, // 是否启用赞赏(1=不启用;2=启用)
                        'is_score' => 1, // 是否启用评分(1=不启用;2=启用)
                        'teacher' => array( // 讲师信息
                            'teacher_id' => 1, // 讲师ID
                            'uid' => '61EA54B27F00000156F7B12404511F16', // 内部讲师ID
                            'teacher_name' => '苏三', // 讲师姓名
                            'face_url' => '', // 头像
                            'title' => '金牌讲师', // 头衔
                            'remark' => 'xxx', // 介绍
                        ),
                        'score_avg' => 9.0, // 整体评分
                    )
     */

    public function Index_post()
    {
        // 验证规则
        $rules = [
            'article_id' => 'require|integer|gt:0',
            'article_chapter_id' => 'integer',
            'source_id' => 'integer',
            'award_ids' => 'array',
            'is_share' => 'integer|in:1,2',
            'customtask_id' => 'integer',
            'plan_id' => 'integer',
            'ed_id' => 'integer',
            'map_id' => 'integer',
            'path_id' => 'integer',
            'is_preview' => 'integer',
        ];

        // 验证数据
        $validate = new PackageValidate($rules, [], array_keys($rules));
        $postData = $validate->postData;

        $article_id = $postData['article_id'];
        $chapter_id = isset($postData['article_chapter_id']) ? $postData['article_chapter_id'] : 0;
        $source_id = isset($postData['source_id']) ? $postData['source_id'] : 0;
        $is_share = isset($postData['is_share']) ? $postData['is_share'] : Constant::RIGHT_INPUT_IS_SHARE_FALSE;
        $customtask_id = isset($postData['customtask_id']) ? $postData['customtask_id'] : 0;
        $plan_id = isset($postData['plan_id']) ? $postData['plan_id'] : 0;
        $ed_id = isset($postData['ed_id']) ? $postData['ed_id'] : 0;
        $map_id = isset($postData['map_id']) ? $postData['map_id'] : 0;
        $path_id = isset($postData['path_id']) ? $postData['path_id'] : 0;
        $is_preview = isset($postData['is_preview']) ? $postData['is_preview'] : 0;

        // 课程状态检查
        $articleServ = new ArticleService();
        $articleInfo = $articleServ->get($article_id);
        if (empty($articleInfo)) {
            // 课程不存在
            E('_ERR_ARTICLE_NOT_FOUND');
        }

        // 根据课程类型,校验相应必传参数
        switch ($articleInfo['course_type']) {
            // 任务类课程
            case Constant::COURSE_TYPE_TASK:
                if (empty($customtask_id) && !$is_preview){
                    E('_ERR_ARTICLE_TASK_ID_EMPTY');
                }
                break;

            // 线下培训类课程
            case Constant::COURSE_TYPE_TRAIN:
                if ((empty($plan_id) || empty($ed_id)) && !$is_preview){
                    E('_ERR_ARTICLE_PLAN_ID_EMPTY');
                }
                break;

            // 其他类课程(学习地图)
            case Constant::COURSE_TYPE_OTHER:
                if ((empty($map_id) || empty($path_id)) && !$is_preview){
                    E('_ERR_ARTICLE_MAP_ID_EMPTY');
                }
                break;
        }

        // 课程分类是否已启用
        $classServ = new ClassService();
        $classInfo = $classServ->get($articleInfo['class_id']);
        if (empty($classInfo) || Constant::CLASS_IS_OPEN_FALSE == $classInfo['is_open']) {

            E('_ERR_CLASS_IS_OPEN_FALSE');
        }

        // 获取分类层级
        $classServ->getLevel($articleInfo['class_id'], $classLevel);
        if ($classLevel == 3) {
            // 如果是三级分类向上查找二级分类是否启用
            $parentClass = $classServ->get($classInfo['parent_id']);
            if (empty($parentClass) || Constant::CLASS_IS_OPEN_FALSE == $parentClass['is_open']) {

                E('_ERR_CLASS_IS_OPEN_FALSE');
            }
        }

        $articleInfo['class_name'] = $classInfo['class_name'];

        // 保密课程、不允许分享到外部:内部人员鉴权访问;外部人员禁止访问。
        // 非保密课程、不允许分享到外部:内部人员鉴权访问;外部人员禁止访问。
        // 非保密课程、允许分享到外部:内部人员鉴权访问、外部人员有权访问。
        // 是分享的课程:内部人员、外部人员均可访问

        // 用户登录信息
        $user = $this->_login->user;
        if (!empty($user) && !$is_preview) {
            // 课程学习权限
            switch ($articleInfo['course_type']) {
                case Constant::COURSE_TYPE_NORMAL:
                    $rightServ = new RightService();
                    $checkRes = $rightServ->checkUserRight($user, $article_id);

                    // 保密课程或非分享入口
                    if (Constant::ARTICLE_IS_SECRET_TRUE == $articleInfo['is_secret']
                        || Constant::ARTICLE_IS_SHARE_FALSE == $articleInfo['is_share']
                        || $is_share != Constant::RIGHT_INPUT_IS_SHARE_TRUE) {
                        // 没有学习权限不允许学习
                        if (!$checkRes) {

                            E('_ERR_ARTICLE_CAN_NOT_STUDY');
                        }
                    }

                    break;

                // 应用判断用户是否有权限访问“任务类”数据
                case Constant::COURSE_TYPE_TASK:
                    $taskCenter = &TaskCenter::instance();
                    $taskCenter->checkCustomtaskRight($customtask_id, $article_id, $user);
                    break;

                // 应用判断用户是否有权限访问“线下培训”数据
                case Constant::COURSE_TYPE_TRAIN:
                    $trainServ = &Train::instance();
                    $trainServ->checkCustomTrainRight($plan_id, $article_id, $user);
                    break;

                // 应用判断用户是否有权限访问“学习地图”数据
                case Constant::COURSE_TYPE_OTHER:
                    $studyMap = new StudyMap($map_id);
                    $studyMap->checkRight($path_id, $article_id, $user);
                    break;
            }
        }

        // 生成考试地址的参数
        $examParams = [
            'article_id' => $article_id,
            'article_chapter_id' => $chapter_id,
            'source_id' => $source_id,
            'customtask_id' => $customtask_id,
            'plan_id' => $plan_id,
            'ed_id' => $ed_id,
            'map_id' => $map_id,
            'path_id' => $path_id,
        ];

        // 系列课程数据
        $articleChapterServ = new ArticleChapterService();
        $studyRecordServ = new StudyRecordService();
        $articleHelper = &ArticleHelper::instance();
        if (Constant::ARTICLE_TYPE_MULTI == $articleInfo['article_type']) {
            // 外部人员检查
            if (!empty($user)) {
                // 闯关开启,课程学习顺序检查
                if (Constant::ARTICLE_IS_CHECK_TRUE == $articleInfo['is_step']) {

                    if (isset($postData['source_id'])) {

                        $orderStudy = $this->_checkStudyOrder($source_id, $article_id, $chapter_id);
                        if (!$orderStudy) {

                            E('_ERR_ARTICLE_SOURCE_STUDY_NEED_ORDER');
                        }
                    }
                }

                // 素材测评,素材是否已学
                if (!empty($chapter_id) && !empty($source_id)) {

                    $chapterInfo = $articleChapterServ->get_by_conds([
                        'article_id' => $article_id,
                        'parent_id' => $chapter_id,
                        'source_id' => $source_id,
                    ]);

                    if (!empty($chapterInfo['et_ids'])) {
                        // 子课程赋值为需测评(方便前端统一逻辑)
                        $articleInfo['is_exam'] = Constant::ARTICLE_IS_CHECK_TRUE;
                        // 检查是否通过测评
                        $articleInfo['exam_pass'] = $articleHelper->getExamResult($article_id, $user, $chapter_id, $source_id, $customtask_id, $plan_id, $ed_id, $map_id, $path_id);
                        // 测评链接
                        $articleInfo['exam_url'] = oaUrl('Frontend/Index/Exam/Index', $examParams);
                    }

                    // 我是否已学
                    $study = $studyRecordServ->get_by_conds([
                        'article_id' => $article_id,
                        'article_chapter_id' => $chapter_id,
                        'source_id' => $source_id,
                        'uid' => $user['memUid'],
                        'customtask_id' => $customtask_id,
                        'plan_id' => $plan_id,
                        'ed_id' => $ed_id,
                        'map_id' => $map_id,
                        'path_id' => $path_id,
                    ]);

                    if (empty($study)) {

                        $articleInfo['my_is_study'] = Constant::ARTICLE_IS_STUDY_FALSE;
                    } else {

                        $articleInfo['my_is_study'] = Constant::ARTICLE_IS_STUDY_TRUE;
                    }
                }
            }

            // 素材学习人数
            if (!empty($source_id)) {
                // 课程登录用户已学素材id集合
                $result = $articleHelper->user_study_sourceIds($article_id, $source_id);
                $articleInfo['study_total'] = count($result);
            }
        }

        // 单课程取素材ID
        if (Constant::ARTICLE_TYPE_SINGLE == $articleInfo['article_type']) {

            $source = $articleChapterServ->get_by_conds(['article_id' => $article_id, 'source_id > ?' => 0]);
            $source_id = $source['source_id'];

            // 课程测评
            if (!empty($user)) {
                if (Constant::ARTICLE_IS_CHECK_TRUE == $articleInfo['is_exam']) {
                    // 检查是否通过测评
                    $articleInfo['exam_pass'] = $articleHelper->getExamResult($article_id, $user, 0, 0, $customtask_id, $plan_id, $ed_id, $map_id, $path_id);
                    // 题目存在
                    $articleInfo['exam_url'] = '';
                    if (!empty($articleInfo['et_ids'])) {
                        $examParams['article_chapter_id'] = 0;
                        $examParams['source_id'] = 0;
                        $articleInfo['exam_url'] = oaUrl('Frontend/Index/Exam/Index', $examParams);
                    }
                }

                // 我是否已学习
                $studyServ = new StudyService();
                $study = $studyServ->get_by_conds([
                    'article_id' => $article_id,
                    'uid' => $user['memUid'],
                    'customtask_id' => $customtask_id,
                    'plan_id' => $plan_id,
                    'ed_id' => $ed_id,
                    'map_id' => $map_id,
                    'path_id' => $path_id,
                ]);

                if (empty($study)) {

                    $articleInfo['my_is_study'] = Constant::ARTICLE_IS_STUDY_FALSE;
                } else {

                    $articleInfo['my_is_study'] = Constant::ARTICLE_IS_STUDY_TRUE;
                }
            }
        }

        // 取素材信息
        if (!empty($source_id)) {

            $sourceInfo = $this->_getSource($source_id, $article_id, $customtask_id, $plan_id, $ed_id, $map_id, $path_id);
            // 外部人员禁止下载附件
            if (empty($user)) {

                $sourceInfo['is_download'] = Constant::ATTACH_IS_DOWNLOAD_FALSE;
            }

            $user_study_time = 0;
            if ($this->uid) {
                // 用户学习时长,单位:秒
                $study_time_s = new StudyTimeService();
                $conditions = [
                    'article_id' => $article_id,
                    'source_id' => $source_id,
                    'uid' => $this->uid,
                    'customtask_id' => $customtask_id,
                    'plan_id' => $plan_id,
                    'ed_id' => $ed_id,
                    'map_id' => $map_id,
                    'path_id' => $path_id,

                ];
                $time_total = $study_time_s->getTotalStudyTime($conditions);

                // 学习时长转为分钟,四舍五入取整
                $user_study_time = $time_total ? round($time_total / 60): 0;
            }

            // 用户学习时长
            $sourceInfo['user_study_time'] = $user_study_time;
            $articleInfo['sources'] = $this->source_info_format($sourceInfo);
        }

        // 点赞列表头像
        list($articleInfo['like_list'], $articleInfo['my_is_like']) = $this->_getLikeList($article_id, $user);

        // 外部人员判断
        if (empty($user)) {

            $articleInfo['is_notice'] = Constant::ARTICLE_IS_COMMENT_FALSE;
            $articleInfo['is_like'] = Constant::ARTICLE_IS_LIKE_FALSE;
            $articleInfo['is_outside'] = Constant::RIGHT_IS_OUTSIDE_YES;
            $articleInfo['is_comment'] = Constant::ARTICLE_IS_COMMENT_FALSE;
            $articleInfo['is_exam'] = Constant::ARTICLE_IS_EXAM_FALSE;
        } else {
            // 内部人员
            $articleInfo['is_outside'] = Constant::RIGHT_IS_OUTSIDE_NO;

            // 激励信息
            $awardInfo = [];
            if (isset($postData['award_ids'])) {

                $awardInfo['medals'] = [];
                $awardServ = new AwardService();
                $awardList = $awardServ->list_by_conds(['award_id' => $postData['award_ids']]);

                // 激励类型:勋章
                if (!empty($awardList)) {

                    $medal_ids = array_column($awardList, 'medal_id');
                    $IntegralServ = new Integral();
                    $integralList = $IntegralServ->listMedal();
                    if (!empty($integralList)) {

                        $integralList = array_combine_by_key($integralList, 'im_id');
                        foreach ($medal_ids as $id) {

                            $awardInfo['medals'][] = isset($integralList[$id]) ? $integralList[$id] : [];
                        }
                    }
                }
            }

            // 常规课程
            if (Constant::COURSE_TYPE_NORMAL == $articleInfo['course_type']) {
                // RPC查询收藏结果
                $param = [
                    'uid' => $user['memUid'],
                    'dataId' => $article_id,
                ];
                $rpcFavorite = &RpcFavoriteHelper::instance();
                $status = $rpcFavorite->getStatus($param);

                $articleInfo['my_is_favorite'] = Constant::ARTICLE_IS_FAVORITE_FALSE;
                if (isset($status['collection']) && RpcFavoriteHelper::COLLECTION_YES == $status['collection']) {

                    $articleInfo['my_is_favorite'] = Constant::ARTICLE_IS_FAVORITE_TRUE;
                }
            }
        }

        $articleInfo['award'] = isset($awardInfo) ? $awardInfo : [];

        $prev_source_id = 0; // 上一节素材id
        $prev_chapter_id = 0; // 上一节素材所在章节id
        $next_source_id = 0; // 下一节素材id
        $next_chapter_id = 0; // 下一节素材所在章节id

        if ($source_id) {
            // 系列课程
            if (Constant::ARTICLE_TYPE_MULTI == $articleInfo['article_type'] && $articleInfo['sources']['source_id']) {

                $conds = [
                    'article_id' => $article_id,
                    'parent_id !=?' => 0
                ];
                // 素材id列表
                $prev_order_option = [
                    'parent_id' => 'ASC',
                    '`order`' => 'ASC'
                ];
                $source_list = $articleChapterServ->list_by_conds($conds, null, $prev_order_option, $field = 'parent_id,source_id');

                $source_ids = array_column($source_list, 'source_id');
                $source_id = $articleInfo['sources']['source_id'];

                // 当前素材的key
                $key = array_keys($source_ids, $source_id);
                $key = current($key);

                $prev_source_id = isset($source_list[$key - 1]['source_id']) ? $source_list[$key - 1]['source_id'] : 0;
                $prev_chapter_id = isset($source_list[$key - 1]['parent_id']) ? $source_list[$key - 1]['parent_id'] : 0;

                $next_source_id = isset($source_list[$key + 1]['source_id']) ? $source_list[$key + 1]['source_id'] : 0;
                $next_chapter_id = isset($source_list[$key + 1]['parent_id']) ? $source_list[$key + 1]['parent_id'] : 0;
            }
        }

        // 上一节
        $articleInfo['prev'] = [
            'source_id' => rintval($prev_source_id),
            'article_chapter_id' => rintval($prev_chapter_id)
        ];

        // 下一节
        $articleInfo['next'] = [
            'source_id' => rintval($next_source_id),
            'article_chapter_id' => rintval($next_chapter_id)
        ];

        // 讲师信息
        $articleInfo['teacher'] = [];
        if ($articleInfo['teacher_id'] > 0) {
            $teacherServ = &Teacher::instance();
            $teacher = $teacherServ->getTeacherInfo($articleInfo['teacher_id']);
            if (!empty($teacher) && $teacher['teacher_status'] == Constant::TEACHER_STATUS_NORMAL) {
                $articleInfo['teacher'] = [
                    'teacher_id' => $teacher['teacher_id'],
                    'uid' => $teacher['uid'],
                    'teacher_name' => $teacher['teacher_name'],
                    'face_url' => $teacher['face_url'],
                    'title' => $teacher['title'],
                    'remark' => $teacher['remark'],
                ];
            }
        }

        // 整体评分
        $itemServ = new ItemService();
        $itemList = $itemServ->list_by_conds(['article_id' => $article_id]);
        $userTotal = 0;
        $scoreTotal = 0;
        foreach ($itemList as $item) {
            $userTotal += $item['user_total'];
            $scoreTotal += $item['score_total'];
        }
        $articleInfo['score_avg'] = 0;
        if ($userTotal != 0) {
            $articleInfo['score_avg'] = round($scoreTotal / $userTotal, 1);
        }

        $this->_result = $this->course_info_format($articleInfo);
    }

    /**
     * 根据素材ID取素材详情
     * @param int $sourceId 素材ID
     * @param int $articleId 课程ID(常规类权限载体)
     * @param int $customtaskId 任务ID(任务类课程权限载体)
     * @param int $planId 培训计划ID(线下培训类课程权限载体)
     * @param int $edId 培训ID(线下培训类课程权限载体)
     * @param int $mapId 学习地图ID(线下培训类课程权限载体)
     * @param int $pathId 学习路径ID(线下培训类课程权限载体)
     * @return array 素材详情
     */
    private function _getSource($sourceId, $articleId, $customtaskId, $planId, $edId, $mapId, $pathId)
    {
        $sourceServ = new SourceService();
        $sourceInfo = $sourceServ->get($sourceId);

        if (empty($sourceInfo)) {

            return [];
        }

        // 音频素材反序列号
        $sourceInfo['audio_imgs'] = empty($sourceInfo['audio_imgs']) ? '' : unserialize($sourceInfo['audio_imgs']);
        // 取素材附件
        $attach = [
            'videos' => [],
            'files' => [],
            'attachs' => []
        ];

        $attachServ = new SourceAttachService();
        $list = $attachServ->list_by_conds(['source_id' => $sourceId]);
        if ($list) {

            foreach ($list as $v) {
                // 取尾缀
                $v['at_suffix'] = end(explode('.', $v['at_name']));
                switch ($v['at_type']) {
                    // 视频文件
                    case Constant::ATTACH_TYPE_VIDEO:
                        $attach['videos'][] = $v;
                        break;
                    // 文件附件
                    case Constant::ATTACH_TYPE_FILE:
                        // 附件地址加入鉴权参数
                        $_id = [
                            'article_id' => $articleId,
                            'customtask_id' => $customtaskId,
                            'plan_id' => $planId,
                            'ed_id' => $edId,
                            'map_id' => $mapId,
                            'path_id' => $pathId,
                        ];
                        $params = [
                            '_id' => json_encode($_id),
                            'user' => $this->_login->user,
                        ];
                        $v['at_url'] = fileUrl($v['at_url'], $params);
                        $v['at_convert_url'] = fileUrl($v['at_convert_url'], $params);

                        if (Constant::SOURCE_TYPE_FILE == $sourceInfo['source_type']) {

                            $attach['files'][] = $v;
                        } else {

                            $attach['attachs'][] = $v;
                        }

                        break;
                }
            }
        }

        // 素材加入附件
        $sourceInfo = array_merge($sourceInfo, $attach);

        return $sourceInfo;
    }

    /**
     * 取点赞信息
     * @param int $articleId 课程ID
     * @param array $user 当前人员信息
     * @return array
     *          + array $like_list 点赞信息
     *          + array $uids 已点赞的人员ID
     */
    private function _getLikeList($articleId, $user)
    {
        if (empty($user)) {

            $user['memUid'] = '';
        }

        $like_list = [
            'total' => 0,
            'index' => '',
            'face_list' => []
        ];
        $likeServ = new LikeService();
        list($start, $perpage) = page_limit(1, 10);
        $likeList = $likeServ->list_by_conds(['article_id' => $articleId], [$start, $perpage], ['created' => 'DESC']);

        // 已点赞的人员ID
        if ($likeList) {
            // 点赞人员头像列表
            $uids = array_column($likeList, 'uid');
            $userServ = &User::instance();
            $users = $userServ->listAll(['memUids' => $uids]);

            if (!empty($users)) {

                $users = array_combine_by_key($users, 'memUid');
            }

            $like_list['index'] = '';
            foreach ($uids as $index => $uid) {
                // 我的点赞在点赞人员头像列表的位置下标
                if ($uid == $user['memUid']) {

                    $like_list['index'] = $index;
                }

                $like_list['face_list'][] = [
                    'uid' => $uid,
                    'face' => isset($users[$uid]) ? $users[$uid]['memFace'] : '',
                ];
            }

            $like_list['total'] = $likeServ->count_by_conds(['article_id' => $articleId]);
        }

        // 我是否点赞
        $count = $likeServ->count_by_conds(['article_id' => $articleId, 'uid' => $this->uid]);
        $my_is_study = $count > 0 ? Constant::ARTICLE_IS_LIKE_TRUE : Constant::ARTICLE_IS_LIKE_FALSE;

        return [$like_list, $my_is_study];
    }

    /**
     * @desc 开启闯关的情况下,检查章节学习顺序
     * @author tangxingguo
     * @param int $sourceId 当前章节
     * @param int $article_id 课程ID
     * @param int $article_chapter_id 当前章节ID
     * @return bool 可以学习返回true,不可学习返回false
     */
    private function _checkStudyOrder($sourceId, $article_id, $article_chapter_id)
    {
        // 取素材顺序
        $articleHelper = ArticleHelper::instance();
        $list = $articleHelper->getChapterData($article_id);

        $sourceList = [];
        foreach ($list as $sources) {

            foreach ($sources['source'] as $v) {

                $sourceList[] = $v;
            }
        }

        if (empty($sourceList)) {

            return false;
        }

        // 第一个章节,返回可学
        if ($sourceList[0]['source_id'] == $sourceId && $sourceList[0]['parent_id'] == $article_chapter_id) {

            return true;
        } else {
            // 取当前章节之前的章节(需要学习完成的课程)
            $needStudy = [];
            foreach ($sourceList as $v) {

                if ($sourceId != $v['source_id']) {

                    $needStudy[] = $v;
                } else {

                    break;
                }
            }

            // 课程登录用户已学素材id集合
            $result = $articleHelper->user_study_sourceIds($article_id, 0, $this->uid);
            $source_ids = $result[$this->uid]['source_ids'] ? $result[$this->uid]['source_ids'] : [];
            if (empty($source_ids)) {

                return false;
            }

            // 对比需要学与已学
            foreach ($needStudy as $source) {
                // 初始当前章节未学
                $isStudy = false;
                if (in_array($source['source_id'], $source_ids)) {
                    // 修改当前章节学习状态为已学
                    $isStudy = true;
                }

                // 当前章节未学,所有章节没有全部学习完成
                if ($isStudy == false) {

                    return false;
                }
            }
        }

        return true;
    }

    /**
     * 课程详情数据格式化
     * @param array $data 待格式化数据
     *
     * @return array
     */
    private function course_info_format($data = [])
    {
        if (empty($data)) {
            return $data;
        }

        return [
            'article_id' => rintval($data['article_id']),
            'data_id' => $data['data_id'],
            'course_type' => rintval($data['course_type']),
            'article_type' => rintval($data['article_type']),
            'article_status' => rintval($data['article_status']),
            'article_title' => $data['article_title'],
            'cm_id' => rintval($data['cm_id']),
            'cm_name' => isset($data['cm_name']) ? $data['cm_name'] : '',
            'update_time' => rintval($data['update_time']),
            'study_total' => rintval($data['study_total']),
            'cover_id' => $data['cover_id'],
            'cover_url' => $data['cover_url'],
            'is_show_cover' => rintval($data['is_show_cover']),
            'my_is_study' => rintval($data['my_is_study']),
            'summary' => $data['summary'],
            'class_id' => rintval($data['class_id']),
            'class_name' => $data['class_name'],
            'ea_name' => $data['ea_name'],
            'content' => $data['content'],
            'tags' => $data['tags'],
            'is_secret' => rintval($data['is_secret']),
            'is_share' => rintval($data['is_share']),
            'is_comment' => rintval($data['is_comment']),
            'is_like' => rintval($data['is_like']),
            'is_exam' => rintval($data['is_exam']),
            'exam_pass' => rintval($data['exam_pass']),
            'exam_url' => isset($data['exam_url']) ? $data['exam_url'] : '',
            'is_step' => rintval($data['is_step']),
            'my_is_like' => rintval($data['my_is_like']),
            'is_outside' => rintval($data['is_outside']),
            'my_is_favorite' => rintval($data['my_is_favorite']),
            'sources' => isset($data['sources']) ? $data['sources'] : [],
            'like_list' => $data['like_list'],
            'award' => $data['award'],
            'prev' => $data['prev'],
            'next' => $data['next'],
            'course_nature' => $data['course_nature'],
            'is_reward' => $data['is_reward'],
            'is_score' => $data['is_score'],
            'teacher_task_id' => $data['teacher_task_id'],
            'teacher' => $data['teacher'],
            'score_avg' => $data['score_avg'],
            'comment_anonymous' => $data['comment_anonymous']
        ];
    }

    /**
     * 素材数据格式化
     * @param array $data
     *
     * @return array
     */
    private function source_info_format($data = [])
    {

        if (empty($data)) {

            return $data;
        }

        return [
            'source_id' => rintval($data['source_id']),
            'source_title' => $data['source_title'],
            'source_status' => rintval($data['source_status']),
            'source_key' => $data['source_key'],
            'source_type' => rintval($data['source_type']),
            'ea_name' => $data['ea_name'],
            'author' => $data['author'],
            'content' => $data['content'],
            'update_time' => rintval($data['update_time']),
            'study_time' => rintval($data['study_time']),
            'user_study_time' => rintval($data['user_study_time']),
            'link' => $data['link'],
            'is_download' => rintval($data['is_download']),
            'audio_imgs' => $data['audio_imgs'],
            'videos' => $data['videos'],
            'files' => $data['files'],
            'attachs' => $data['attachs']
        ];
    }
}