SourceListController.class.php 16.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
<?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\StudyMap;
use Common\Common\IntegralStrategy;
use Common\Service\ArticleService;
use Common\Service\ExamService;
use Common\Service\StudyService;
use Common\Service\CompleteService;
use Common\Service\UserActionService;

class SourceListController extends \Api\Controller\AbstractController
{

    /**
     * 是否必须登录
     */
    protected $_require_login = false;

    /**
     * SourceList
     *
     * @author tangxingguo
     * @desc 素材章节列表
     *
     * @param int article_id:true 课程ID
     * @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(
     *             'list' => array( // 素材列表
     *                 'source_id' => 1, // 素材ID
     *                 'source_title' => '八荣八耻', // 素材标题
     *                 'source_key' => 'L171015173834', // 素材标识
     *                 'study_status' => 1, // 学习状态(1=未学,2=已学)
     *             ),
     *             'exam' => array(
     *                 'is_exam' => 1, // 是否开启测评(1=未开启;2=已开启)
     *                 'exam_pass' => 1, // 当前课程评测状态(1=未通过;2=已通过)
     *                 'exam_url' => 'http://qy.vchangyi.com', // 测评地址(测评开启且为通过测评的情况下返回)
     *             )
     * );
     */
    public function Index_post()
    {

        // 验证规则
        $rules = [
            'article_id' => 'require|integer|gt:0',
            '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));
        $article_id = $validate->postData['article_id'];
        $customtask_id = isset($validate->postData['customtask_id']) ? $validate->postData['customtask_id'] : 0;
        $plan_id = isset($validate->postData['plan_id']) ? $validate->postData['plan_id'] : 0;
        $ed_id = isset($validate->postData['ed_id']) ? $validate->postData['ed_id'] : 0;
        $map_id = isset($validate->postData['map_id']) ? $validate->postData['map_id'] : 0;
        $path_id = isset($validate->postData['path_id']) ? $validate->postData['path_id'] : 0;
        $is_preview = isset($validate->postData['is_preview']) ? $validate->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;
        }

        // 取素材章节列表
        $articleHelper = &ArticleHelper::instance();
        $sourceList = $articleHelper->getChapterData($article_id);

        // 初始化所有素材未完成测评
        $is_complete = Constant::ARTICLE_ALL_SOURCE_EXAM_IS_FAIL;

        // 登录用户信息
        $user = $this->_login->user;
        // 测评
        $examInfo = [];

        // 非外部人员
        if (!empty($user)) {
            // 当前课程已读的素材章节
            $recordConds = [
                'article_id' => $article_id,
                'uid' => $user['memUid'],
            ];

            // 根据课程类型,组合请求参数
            switch ($articleInfo['course_type']) {
                // 任务类课程
                case Constant::COURSE_TYPE_TASK:
                    $recordConds['customtask_id'] = $customtask_id;
                    break;

                // 线下培训类课程
                case Constant::COURSE_TYPE_TRAIN:
                    $recordConds['plan_id'] = $plan_id;
                    $recordConds['ed_id'] = $ed_id;
                    break;

                // 其他类课程(学习地图)
                case Constant::COURSE_TYPE_OTHER:
                    $recordConds['map_id'] = $map_id;
                    $recordConds['path_id'] = $path_id;
                    break;
            }

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

            // 是否已学
            list($sourceList, $allStudy) = $this->_getStudyStatus($sourceList, $source_ids);
            // 检查子课程(素材)是否全部测评通过
            list($allExam, $sourceList) = $this->_getAllExamStatus($sourceList, $article_id, $customtask_id, $plan_id, $ed_id, $map_id, $path_id);

            // 全部学习、测评完成才算整个课程完成
            $is_complete = ($allExam && $allStudy) ? Constant::ARTICLE_EXAM_IS_PASS : Constant::ARTICLE_EXAM_IS_FAIL;

            // 课程全部学习完成,检查评测
            if ($allStudy && $allExam) {
                // 有测评、子课程全部通过测评,添加测评连接
                if (Constant::ARTICLE_IS_CHECK_TRUE == $articleInfo['is_exam']) {

                    $examInfo['is_exam'] = Constant::ARTICLE_IS_CHECK_TRUE;
                    // 检查是否通过测评
                    $examInfo['exam_pass'] = $articleHelper->getExamResult($article_id, $user, 0, 0, $customtask_id, $plan_id, $ed_id, $map_id, $path_id);

                    // 题目存在
                    if (!empty($articleInfo['et_ids'])) {
                        $examParams = [
                            'article_id' => $article_id,
                            'customtask_id' => $customtask_id,
                            'plan_id' => $plan_id,
                            'ed_id' => $ed_id,
                            'map_id' => $map_id,
                            'path_id' => $path_id,
                        ];
                        $examInfo['exam_url'] = oaUrl('Frontend/Index/Exam/Index', $examParams);
                    }
                }

                // 完成学习修改状态
                $studyServ = new StudyService();
                $studyData = [
                    '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,
                ];
                $study = $studyServ->get_by_conds($studyData);

                if (empty($study)) {
                    // 写入学习数据
                    $studyData['username'] = $user['memUsername'];
                    $studyServ->insert($studyData);

                    switch ($articleInfo['course_type']) {
                        // 常规课程
                        case Constant::COURSE_TYPE_NORMAL:
                            // 更新课程主表已学习总数
                            $articleServ->update($articleInfo['article_id'], ['study_total = study_total + ?' => 1]);

                            // 记录已完成数据
                            $completeServ = new CompleteService();
                            $complete = $completeServ->get_by_conds([
                                'article_id' => $articleInfo['article_id'],
                                'uid' => $user['memUid'],
                            ]);

                            if (empty($complete)) {
                                $completeServ->insert([
                                    'article_id' => $articleInfo['article_id'],
                                    'uid' => $user['memUid'],
                                    'username' => $user['memUsername'],
                                ]);
                            }

                            // 领取激励
                            $articleHelper = &ArticleHelper::instance();
                            $articleHelper->getAwardByUser($user);

                            // 触发积分策略
                            $this->_triggerIntegral($articleInfo);
                            break;

                        // 任务类课程
                        case Constant::COURSE_TYPE_TASK:
                            break;

                        // 线下培训类课程
                        case Constant::COURSE_TYPE_TRAIN:
                            break;

                        // 其他类课程
                        case Constant::COURSE_TYPE_OTHER:
                            // 不需要测评,触发地图埋点
                            if (Constant::ARTICLE_IS_EXAM_FALSE == $articleInfo['is_exam']) {
                                $mapServ = new StudyMap($map_id);

                                // 地图学习权限检查
                                $mapServ->checkRight($path_id, $articleInfo['article_id'], $user);

                                // 地图埋点
                                $mapServ->studyContent($path_id, $articleInfo['article_id'], $user);

                                // 触发积分策略
                                $this->_triggerIntegral($articleInfo);
                            }
                            break;
                    }
                }
            }

            $is_outside = Constant::RIGHT_IS_OUTSIDE_NO;
        } else {

            $is_outside = Constant::RIGHT_IS_OUTSIDE_YES;
        }

        $this->_result = [
            'list' => $sourceList,
            'exam' => $examInfo,
            'is_complete' => $is_complete,
            'is_outside' => $is_outside
        ];
    }

    /**
     * @desc 获取章节是否已学,是否全部学习完毕
     * @author tangxingguo
     *
     * @param array $source_list 章节列表
     * @param array $source_ids 登录用户已学素材id集合
     *
     * @return mixed
     */
    private function _getStudyStatus($source_list, $source_ids)
    {

        // 章节是否全部学习
        $all_study = true;
        // 素材列表组装学习状态
        foreach ($source_list as $key => $value) {

            foreach ($value['source'] as $item => $source) {
                // 未学
                $study_status = Constant::ARTICLE_SOURCE_STUDY_STATUS_FALSE;
                // 素材id存在与用户已学素材id集合
                if (in_array($source['source_id'], $source_ids)) {
                    // 已学
                    $study_status = Constant::ARTICLE_SOURCE_STUDY_STATUS_TRUE;
                }

                $source_list[$key]['source'][$item]['study_status'] = $study_status;
                // 未学
                if (Constant::ARTICLE_SOURCE_STUDY_STATUS_FALSE == $study_status) {
                    // 章节全部学习状态:否
                    $all_study = false;
                }
            }
        }

        return [$source_list, $all_study];
    }

    /**
     * 取所有子课程是否评测通过
     *
     * @author tangxingguo
     *
     * @param array $sourceList 素材列表
     * @param int $articleId 课程ID
     * @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
     *
     * @return array
     */
    private function _getAllExamStatus($sourceList, $articleId, $customtask_id = 0, $plan_id = 0, $ed_id = 0, $map_id = 0, $path_id = 0)
    {

        // 当前课程内所有子课程(素材)测评通过数据
        $examServ = new ExamService();
        $conds = [
            'article_id' => $articleId,
            'uid' => $this->uid,
            'is_pass' => Constant::ARTICLE_EXAM_IS_PASS,
            'source_id > ?' => 0,
            'customtask_id' => $customtask_id,
            'plan_id' => $plan_id,
            'ed_id' => $ed_id,
            'map_id' => $map_id,
            'path_id' => $path_id,
        ];
        $examList = $examServ->list_by_conds($conds);

        // 素材测评状态
        $allExam = true; // 所有课程均通过测评
        foreach ($sourceList as $k => $v) {

            foreach ($v['source'] as $item => $source) {
                // 初始化测评为未通过
                $examStatus = Constant::ARTICLE_EXAM_IS_FAIL;
                if (!empty($source['et_ids'])) {
                    // 素材需要测评
                    if (!empty($examList)) {
                        foreach ($examList as $key => $value) {
                            // 章节匹配,素材匹配
                            if ($v['article_chapter_id'] == $value['article_chapter_id']
                                && $source['source_id'] == $value['source_id']) {

                                $examStatus = Constant::ARTICLE_EXAM_IS_PASS;
                                unset($examList[$key]);
                                break;
                            }
                        }
                    }

                    // 需要测评,重置学习状态为测评结果(字段取名不当,应该是完成状态)
                    $sourceList[$k]['source'][$item]['study_status'] = $examStatus;
                } else {
                    // 素材不需要测评,设置测评为PASS
                    $examStatus = Constant::ARTICLE_EXAM_IS_PASS;
                }

                // 章节未测评通过
                if (Constant::ARTICLE_EXAM_IS_FAIL == $examStatus) {

                    $allExam = Constant::ARTICLE_ALL_SOURCE_EXAM_IS_FAIL == $examStatus ? false : true;
                }
            }
        }

        return [$allExam, $sourceList];
    }

    /**
     * 触发积分策略
     * @param array $article 课程详情
     * @return array
     *          array(
     *              'integrals' => 1, // 获得的积分数
     *          )
     */
    private function _triggerIntegral($article)
    {
        // 系列课程
        if (Constant::ARTICLE_TYPE_MULTI == $article['article_type']) {
            $action_key = Constant::INT_ACT_CHILD_LEARNING;
            $triggerKey = [Constant::INT_TRIGGER_CHILD_COMPLETE, Constant::INT_TRIGGER_CHILD_COMPLETE_NUM];

        // 单课程
        } else {
            $action_key = Constant::INT_ACT_ONE_LEARNING;
            $triggerKey = [Constant::INT_TRIGGER_ONE_COMPLETE, Constant::INT_TRIGGER_ONE_COMPLETE_NUM];
        }

        // 保存行为
        $userActionServ = new UserActionService();
        $saveRes = $userActionServ->save($this->uid, $article['article_id'], $action_key);

        // 行为触发
        if ($saveRes) {
            // 学习课程数量
            $studyServ = new StudyService();
            $studyNum = $studyServ->count_by_conds(['uid' => $this->uid]);

            $trigger_types = [
                [
                    'triggerKey' => $triggerKey[0],
                    'value' => 1,
                ],
                [
                    'triggerKey' => $triggerKey[1],
                    'value' => $studyNum,
                ],
            ];

            $articleHelper = ArticleHelper::instance();
            $triggers = $articleHelper->buildTrigger($article, $action_key, $trigger_types);

            if (!empty($triggers)) {
                $strategyServ = &IntegralStrategy::instance();
                $integrals = $strategyServ->triggerPoint($this->uid, $article['article_id'], $action_key, $triggers);
            }
        }

        $this->_result = ['integrals' => isset($integrals) && !empty($integrals) ? $integrals : 0];
    }
}