ScoreInfoController.class.php 2.12 KB
<?php
/**
 * Created by PhpStorm.
 * User: tangxingguo
 * Date: 2018-3-29
 * Time: 15:06:45
 */

namespace Api\Controller\Course;

use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\Train;
use Common\Service\CourseArticleService;
use Common\Service\CourseScoreDetailService;

class ScoreInfoController extends \Api\Controller\AbstractController
{
    /**
     * ScoreInfo
     * @author tangxingguo
     * @desc 评分详情接口
     * @param Int article_id:true 课程ID
     * @return Array
                array(
                    'score_info' => array(
                        'score_avg' => 1, // 整体评分
                        'content_score_avg' => 1, // 内容评分
                        'teacher_score_avg' => 1, // 讲师评分
                        'list' => array( // 项目列表
                            'item_id' => 1, // 评分项ID
                            'content' => 1, // 评分项
                            'item_score_avg' => 1, // 评分值
                        ),
                    ),
                    'my_score' => 1 // 我是否评分(1=未评;2=已评)
                );
     */
    public function Index_post()
    {
        $rules = [
            'article_id' => 'require|integer',
        ];

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

        // 取课程
        $articleServ = new CourseArticleService();
        $article = $articleServ->get_by_conds(['article_id' => $articleId]);
        if (empty($article)) {
            E('_ERR_ARTICLE_DATA_NOT_FOUND');
        }

        // 用户是否已评分
        $detailServ = new CourseScoreDetailService();
        $count = $detailServ->count_by_conds(['article_id' => $articleId, 'uid' => $this->uid]);
        $my_score = $count > 0 ? Constant::SCORE_STATUS_IS_TRUE : Constant::SCORE_STATUS_IS_FALSE;

        $articleScore = &Train::instance()->getCourseScore([$articleId]);

        $this->_result = [
            'score_info' => $articleScore[$articleId],
            'my_score' => $my_score
        ];
    }
}