UpdateController.class.php 12.5 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhonglei
 * Date: 2017/6/5
 * Time: 14:33
 */
namespace Rpc\Controller\Exam;

use Common\Common\ArticleHelper;
use Common\Service\CompleteService;
use Think\Log;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\IntegralStrategy;
use Common\Service\ArticleService;
use Common\Service\ArticleChapterService;
use Common\Service\StudyService;
use Common\Service\StudyRecordService;
use Common\Service\ExamService;
use Common\Service\UserActionService;

class UpdateController extends \Rpc\Controller\AbstractController
{
    /**
     * Update
     * @author zhonglei
     * @desc 更新课程测评结果RPC接口
     * @param String uid:true 用户ID
     * @param Int article_id:true 课程ID
     * @param Int article_chapter_id:true 章节ID,对课程测评时为0
     * @param Int source_id:true 素材ID,对课程测评时为0
     * @param Int customtask_id:true 常规课程传0
     * @param int plan_id:true 培训计划ID(常规课程传0,线下培训类课程必须)
     * @param int ed_id:true  培训ID(常规课程传0,线下培训类课程必须)
     * @param Int is_pass:true 测评是否通过(1=未通过;2=已通过)
     * @param Int yes_total:true 测评答对题数
     * @param Int no_total:true 测评答错题数
     * @param Int map_id 学习地图ID
     * @param Int path_id 学习路径ID
     * @return array
     */
    public function Index()
    {
        Log::record('params: ' . var_export($this->_params, true), Log::INFO);
        $post_data = $this->_params;
        $result = [];

        // 验证规则
        $rules = [
            'uid' => 'require',
            'article_id' => 'require|integer',
            'article_chapter_id' => 'integer',
            'source_id' => 'integer',
            'customtask_id' => 'integer',
            'map_id' => 'integer',
            'path_id' => 'integer',
            'plan_id' => 'integer',
            'ed_id' => 'integer',
            'is_pass' => 'require|in:1,2',
            'yes_total' => 'require|integer',
            'no_total' => 'require|integer',
        ];

        // 验证请求数据
        $validate = new PackageValidate();
        $validate->postData = $post_data;
        $validate->validateParams($rules);

        $customtask_id = isset($post_data['customtask_id']) ? $post_data['customtask_id'] : 0;
        $plan_id = isset($post_data['plan_id']) ? $post_data['plan_id'] : 0;
        $ed_id = isset($post_data['ed_id']) ? $post_data['ed_id'] : 0;
        $map_id = $post_data['map_id'] ?? 0;
        $path_id = $post_data['path_id'] ?? 0;

        // 测评通过题数百分比
        $post_data['rate'] = round($post_data['yes_total'] / ($post_data['yes_total'] + $post_data['no_total']), 2) * 100;

        $articleServ = new ArticleService();
        $article = $articleServ->get($post_data['article_id']);

        // 未找到课程
        if (empty($article)) {
            Log::record('not found article', Log::INFO);
            return $result;
        }

        // 数据中心-课程中心改造 1 start {
        $examServ = new ExamService();
        $exam_data = [
            'article_id' => $post_data['article_id'],
            'customtask_id' => $customtask_id,
            'plan_id' => $plan_id,
            'ed_id' => $ed_id,
            'map_id' => $map_id,
            'path_id' => $path_id,
            'uid' => $post_data['uid'],
            'is_pass' => $post_data['is_pass'],
        ];
        // 数据中心-课程中心改造 1 end }

        // 对课程进行测评
        if (empty($post_data['article_chapter_id']) && empty($post_data['source_id'])) {
            $studyServ = new StudyService();
            $study = $studyServ->get_by_conds([
                'article_id' => $post_data['article_id'],
                'uid' => $post_data['uid'],
            ]);

            // 未找到课程已学数据
            if (empty($study)) {
                Log::record('not found study', Log::INFO);
                return $result;
            }

            // 记录考试结果
            $exam_data['username'] = $study['username'];
            $examServ->insert($exam_data);

            // 数据中心-课程中心改造 2 start {
            // 计算课程是否已完成(主课程测评通过,说明子课程已全部学习且无测评或子课程测评已通过)
            if ($post_data['is_pass'] == Constant::ARTICLE_EXAM_IS_PASS) {
                $complete_data = [
                    'article_id' => $post_data['article_id'],
                    'uid' => $post_data['uid'],
                    'username' => $study['username'],
                ];
            }
            // 数据中心-课程中心改造 2 end }

        // 对章节素材进行测评
        } else {
            $recordServ = new StudyRecordService();
            $record = $recordServ->get_by_conds([
                'article_id' => $post_data['article_id'],
                'article_chapter_id' => $post_data['article_chapter_id'],
                'source_id' => $post_data['source_id'],
                'uid' => $post_data['uid'],
            ]);

            // 未找到章节素材已学数据
            if (empty($record)) {
                Log::record('not found study record', Log::INFO);
                return $result;
            }

            // 记录考试结果
            $exam_data['article_chapter_id'] = $post_data['article_chapter_id'];
            $exam_data['source_id'] = $post_data['source_id'];
            $exam_data['username'] = $record['username'];
            $examServ->insert($exam_data);

            // 数据中心-课程中心改造 3 start {
            // 计算课程是否已完成(若主课程无测评,则子课程全部测评通过,即为已完成)
            if ($article['is_exam'] == Constant::ARTICLE_IS_EXAM_FALSE && $post_data['is_pass'] == Constant::ARTICLE_EXAM_IS_PASS) {
                $articleHelper = &ArticleHelper::instance();
                $all_study = $articleHelper->getStudyStatus($post_data['uid'], $post_data['article_id'], $customtask_id, $plan_id, $ed_id, $map_id, $path_id);
                if ($all_study) {
                    $complete_data = [
                        'article_id' => $post_data['article_id'],
                        'uid' => $post_data['uid'],
                        'username' => $record['username'],
                    ];
                }
            }
            // 数据中心-课程中心改造 3 end }
        }

        // 数据中心-课程中心改造 4 start {
        // 记录课程已完成(仅限常规课程)
        if ($article['course_type'] == Constant::COURSE_TYPE_NORMAL && !empty($complete_data)) {
            $completeServ = new CompleteService();
            $complete = $completeServ->get_by_conds([
                'article_id' => $post_data['article_id'],
                'uid' => $post_data['uid'],
            ]);
            if (empty($complete)) {
                $completeServ->insert($complete_data);
            }
        }
        // 数据中心-课程中心改造 4 end }
        if (($article['credit_strategy_setting'] != Constant::COURSE_CREDIT_STRATEGY_DISABLE ||
            $article['strategy_setting'] != Constant::COURSE_STRATEGY_DISABLE) &&
            !in_array($article['course_type'], [Constant::COURSE_TYPE_TASK, Constant::COURSE_TYPE_TRAIN])) {
            // 对课程进行测评
            if (empty($post_data['article_chapter_id']) && empty($post_data['source_id'])) {
                $result = $this->_triggerArticleExam($article, $post_data);

            // 对章节素材进行测评
            } else {
                $chapterServ = new ArticleChapterService();
                $article_chapter = $chapterServ->get_by_conds([
                    'parent_id' => $post_data['article_chapter_id'],
                    'source_id' => $post_data['source_id'],
                ]);

                if (!empty($article_chapter)) {
                    $result = $this->_triggerSourceExam($article, $article_chapter, $post_data);
                }
            }
        }

        return $result;
    }

    /**
     * 触发课程测评埋点
     * @author zhonglei
     * @param array $article 课程数据
     * @param array $post_data 请求数据
     * @return array
     */
    private function _triggerArticleExam($article, $post_data)
    {
        $action_key = $article['article_type'] == Constant::ARTICLE_TYPE_SINGLE ? Constant::INT_ACT_ONE_EVALUATION : Constant::INT_ACT_CHILD_EVALUATION;
        $trigger_types = [];

        // 测评通过
        if ($post_data['is_pass'] == Constant::ARTICLE_EXAM_IS_PASS) {
            $actionServ = new UserActionService();
            $act_result = $actionServ->save($post_data['uid'], $article['article_id'], $action_key);

            // 首次测评通过
            if ($act_result) {
                // 获取单课程/系列课程测评通过总数
                $count = $actionServ->countByActionKey($post_data['uid'], $action_key);

                // 单课程
                if ($article['article_type'] == Constant::ARTICLE_TYPE_SINGLE) {
                    // 单课程测评通过
                    $trigger_types[] = [
                        'triggerKey' => Constant::INT_TRIGGER_ONE_PASS,
                        'value' => 1,
                    ];

                    // 单课程测评通过数
                    $trigger_types[] = [
                        'triggerKey' => Constant::INT_TRIGGER_ONE_PASS_NUM,
                        'value' => $count,
                    ];

                // 系列课程
                } else {
                    // 系列课程测评通过
                    $trigger_types[] = [
                        'triggerKey' => Constant::INT_TRIGGER_CHILD_PASS,
                        'value' => 1,
                    ];

                    // 单课程测评通过数
                    $trigger_types[] = [
                        'triggerKey' => Constant::INT_TRIGGER_CHILD_PASS_NUM,
                        'value' => $count,
                    ];
                }
            }
        }

        // 单课程
        if ($article['article_type'] == Constant::ARTICLE_TYPE_SINGLE) {
            // 单课程测评通过题数百分比
            $trigger_types[] = [
                'triggerKey' => Constant::INT_TRIGGER_ONE_PASS_RATE,
                'value' => $post_data['rate'],
            ];

        // 系列课程
        } else {
            // 系列课程测评通过题数百分比
            $trigger_types[] = [
                'triggerKey' => Constant::INT_TRIGGER_CHILD_PASS_RATE,
                'value' => $post_data['rate'],
            ];
        }

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

        // 触发积分规则,获得积分数
        $result = [];
        if (!empty($triggers)) {
            $strategyServ = &IntegralStrategy::instance();
            $result = $strategyServ->triggerPoint($post_data['uid'], $article['article_id'], $action_key, $triggers);
        }

        return $result;
    }

    /**
     * 触发子课程测评埋点
     * @author zhonglei
     * @param array $article 课程数据
     * @param array $article_chapter 章节素材数据
     * @param array $post_data 请求数据
     * @return array
     */
    private function _triggerSourceExam($article, $article_chapter, $post_data)
    {
        $action_key = Constant::INT_ACT_SERIES_EVALUATION;
        $trigger_types = [];

        // 测评通过
        if ($post_data['is_pass'] == Constant::ARTICLE_EXAM_IS_PASS) {
            $actionServ = new UserActionService();
            $act_result = $actionServ->save($post_data['uid'], $article_chapter['article_chapter_id'], $action_key);

            // 首次测评通过
            if ($act_result) {
                $trigger_types[] = [
                    'triggerKey' => Constant::INT_TRIGGER_SERIES_PASS,
                    'value' => 1,
                ];
            }
        }

        // 通过题数百分比
        $trigger_types[] = [
            'triggerKey' => Constant::INT_TRIGGER_SERIES_PASS_RATE,
            'value' => $post_data['rate'],
        ];

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

        $result = [];
        if (!empty($triggers)) {
            $strategyServ = &IntegralStrategy::instance();
            $result = $strategyServ->triggerPoint($post_data['uid'], 'article_chapter_id_' . $article_chapter['article_chapter_id'], $action_key, $triggers);
        }

        return $result;
    }
}