CourseHelper.class.php 15.8 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
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 17/10/12
 * Time: 17:12
 */

namespace Common\Common;

use VcySDK\Service;
use VcySDK\Member;
use Common\Model\CourseArticleModel;
use Common\Model\CourseSourceModel;
use Common\Model\CourseRightModel;
use Common\Model\CourseStudyTimeModel;
use Common\Model\CourseCompleteModel;
use Common\Model\CommentModel;
use Common\Model\LikeModel;

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

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

        return $instance;
    }

    /**
     * 获取用户权限数据
     * @author zhonglei
     *
     * @param array $user 用户信息
     *
     * @return array
     */
    public function getUserRight($user)
    {
        $data = [];

        if (!is_array($user) || empty($user)) {
            return $data;
        }

        // 标签
        if (isset($user['tagName']) && !empty($user['tagName'])) {
            // 获取标签ID
            $data[Constant::RIGHT_TYPE_TAG] = array_column($user['tagName'], 'tagId');

            // 获取标签成员
            $tagServ = &Tag::instance();
            $members = $tagServ->listAllMember(['tagIds' => $data[Constant::RIGHT_TYPE_TAG]]);

            // 获取标签成员中的部门ID
            $dp_ids = array_column($members, 'dpId');

            if (!empty($dp_ids)) {
                $data[Constant::RIGHT_TYPE_DEPARTMENT] = array_filter(array_unique($dp_ids));
            }
        }

        // 部门
        if (isset($user['dpName']) && !empty($user['dpName'])) {
            $dp_ids = array_column($user['dpName'], 'dpId');

            // 合并标签成员中的部门ID
            if (isset($data[Constant::RIGHT_TYPE_DEPARTMENT])) {
                $dp_ids = array_unique(array_merge($data[Constant::RIGHT_TYPE_DEPARTMENT], $dp_ids));
            }

            // 使用缓存
            $dpServ = new Department(false);
            $parent_ids = [];

            // 取父级部门ID
            foreach ($dp_ids as $dp_id) {
                $dpServ->list_parent_cdids($dp_id, $parent_ids);
            }

            // FIXME zhonglei 2017年09月07日17:57:07 经产品确认去掉取子级部门,暂时先注释掉代码
            /*
            // 取子级部门ID
            $child_ids = $dpServ->list_childrens_by_cdid($dp_ids);
            // 合并部门ID
            $dp_ids = array_merge($dp_ids, array_values($parent_ids), array_values($child_ids));
            */
            $dp_ids = array_merge($dp_ids, array_values($parent_ids));

            $data[Constant::RIGHT_TYPE_DEPARTMENT] = array_unique($dp_ids);
        }

        // 全公司
        $data[Constant::RIGHT_TYPE_ALL] = Constant::RIGHT_IS_ALL_TRUE;

        // 用户
        $data[Constant::RIGHT_TYPE_USER] = [$user['memUid']];

        // 职位
        if (isset($user['job']['jobId'])) {
            $data[Constant::RIGHT_TYPE_JOB] = [$user['job']['jobId']];

            // 兼容UC人员列表接口返回的职位数据
        } elseif (isset($user['jobList']) && !empty($user['jobList'])) {
            $data[Constant::RIGHT_TYPE_JOB] = array_column($user['jobList'], 'jobId');
        }

        // 角色
        if (isset($user['role']['roleId'])) {
            $data[Constant::RIGHT_TYPE_ROLE] = [$user['role']['roleId']];

            // 兼容UC人员列表接口返回的角色数据
        } elseif (isset($user['roleList']) && !empty($user['roleList'])) {
            $data[Constant::RIGHT_TYPE_ROLE] = array_column($user['roleList'], 'roleId');
        }

        return $data;
    }

    /**
     * 根据权限数据构建查询条件
     * @author zhonglei
     *
     * @param array $rights 权限数据
     *
     * @return array
     */
    private function _buildRightConds($rights)
    {
        if (!is_array($rights) || empty($rights)) {
            return [];
        }

        $obj_ids = [];

        foreach ($rights as $k => $v) {
            // 全公司
            if ($k == Constant::RIGHT_TYPE_ALL) {
                $obj_ids[] = Constant::RIGHT_IS_ALL_TRUE;

                // 部门、标签、人员、职位、角色
            } else {
                $obj_ids = array_merge($obj_ids, $v);
            }
        }

        return empty($obj_ids) ? [] : ['obj_id' => $obj_ids];
    }

    /**
     * 根据需要的数据类型和权限数据获取数据
     * @author liyifei
     *
     * @param array $user 用户信息
     * @param string $select_data 要查询的数据(article_id、class_id)
     *
     * @return array
     */
    public function listArticleIdByRight($user, $select_data)
    {
        $rightModel = new CourseRightModel();
        $rights = $this->getUserRight($user);
        $conds = $this->_buildRightConds($rights);
        $conds["{$select_data} > ?"] = 0;
        $list = $rightModel->list_by_conds($conds);

        $articleIds = [];
        if (!empty($list)) {
            $articleIds = array_column($list, 'article_id');
        }

        return $articleIds;
    }

    /**
     * 获取多个人员可学课程id
     * @author tangxingguo
     *
     * @param array $users 人员信息
     * @param array $articles 常规课程id集合
     *
     * @return array
     */
    public function articleCountByUsers($users, $articles = [])
    {
        $userRights = [];
        $rights = [];
        $userArticleIds = [];

        // 权限按照人员分组
        foreach ($users as $user) {
            $rightRaw = $this->getUserRight($user);
            $userRights[$user['memUid']] = $this->_buildRightConds($rightRaw);
        }

        // 合并所有人员的权限数据
        foreach ($userRights as $uid => $formatRight) {
            $rights['obj_id'] = isset($rights['obj_id']) ? $rights['obj_id'] : [];
            $rights['obj_id'] = array_merge($rights['obj_id'], $formatRight['obj_id']);
            $rights['obj_id'] = array_unique($rights['obj_id']);
        }

        // 拉出所有人员可学总数据
        $rightModel = new CourseRightModel();
        $conds['article_id'] = $articles;
        $list = $rightModel->list_by_conds($conds);

        // 按照人员权限匹配可学articleId
        foreach ($list as $rightInfo) {
            $objId = $rightInfo['obj_id'];
            foreach ($userRights as $uid => $formatRight) {
                if (in_array($objId, $formatRight['obj_id'])) {
                    $userArticleIds[$uid][] = $rightInfo['article_id'];
                }
            }
        }

        unset($list);

        return $userArticleIds;
    }

    /**
     * 整体数据总览-员工学习概览
     * @author liyifei
     *
     * @param int $starttime
     * @param int $endtime
     *
     * @return array
     */
    public function listUserStudyData($starttime, $endtime)
    {
        // 查询常规课程id集合
        $course_article = new CourseArticleModel();
        $course_article_list = $course_article->list_by_conds(['course_type' => Constant::COURSE_TYPE_NORMAL], null, [],
            'article_id,data_id,article_type');
        $articles = array_column($course_article_list, 'article_id');

        // 员工总数
        $userServ = User::instance();
        $result = $userServ->listByConds(['memSubscribeStatus' => 1],1,1);
        $total = $result['total'];

        // 学习人数
        $studyTimeModel = new CourseStudyTimeModel();
        $studyUserTotal = $studyTimeModel->getStudyUserTotal($starttime, $endtime,$articles);

        // 学习时长(秒)
        $conds = [
            'created >= ?' => $starttime,
            'created <= ?' => $endtime,
            'article_id' => $articles
        ];
        $timeTotal = $studyTimeModel->getTotalStudyTime($conds);

        // 该时段内,每天员工总数
        $userServ = &User::instance();
        $totalList = $userServ->memberCountByDay($starttime, $endtime);
        if (!empty($totalList)) {
            $totalList = array_combine_by_key($totalList, 'days');
        }

        // 该时段内,每天学习时长(秒)
        $studyTimeModel = new CourseStudyTimeModel();
        $studyTimeList = $studyTimeModel->listStudyTimeTotalByDays($starttime, $endtime,$articles);
        if (!empty($studyTimeList)) {
            $studyTimeList = array_combine_by_key($studyTimeList, 'days');
        }

        // 该时段内,人均学习时长(秒)
        $starttimeTemp = $starttime;
        $perTimeTotal = 0;
        $solt = 24 * 60 * 60 * 1000;
        $countDays = 0;
        while ($endtime > $starttimeTemp) {
            $formatDate = rgmdate($starttimeTemp, 'Y-m-d');
            if (!empty($studyTimeList)) {
                $userTotal = isset($totalList[$formatDate]) ? $totalList[$formatDate]['total'] : 1;
                $studyTimeTotal = isset($studyTimeList[$formatDate]) ? $studyTimeList[$formatDate]['total'] : 0;
                $perTimeTotal += $studyTimeTotal > 0 ? round($studyTimeTotal / $userTotal) : 0;
            }

            $starttimeTemp += $solt;
            $countDays += 1;
        }
        $perTimeTotal = $countDays > 0 ? round($perTimeTotal / $countDays) : 0;

        // 学习课程总数
        $studyCourseTotal = $studyTimeModel->getUserStudyCourseTotal($starttime, $endtime,$articles);

        // 学习素材总数
        $studySourseTotal = $studyTimeModel->getUserStudySourceTotal($starttime, $endtime,$articles);

        // 通过学习次数(已完成)
        $completeModel = new CourseCompleteModel();
        $studyCompleteTotal = $completeModel->count_by_conds($conds);

        unset($conds['article_id']);
        // 点赞数(埋点数据)
        $conds['app'] = Constant::APP_COURSE;
        $likeModel = new LikeModel();
        $likeTotal = $likeModel->count_by_conds($conds);

        // 评论次数(埋点数据)
        $commentModel = new CommentModel();
        $commentTotal = $commentModel->count_by_conds($conds);

        return [
            'user_total' => $total,
            'study_user_total' => (int)$studyUserTotal,
            'time_total' => (int)$timeTotal,
            'per_time_total' => (int)$perTimeTotal,
            'study_course_total' => (int)$studyCourseTotal,
            'study_sourse_total' => (int)$studySourseTotal,
            'study_complete_total' => (int)$studyCompleteTotal,
            'like_total' => (int)$likeTotal,
            'comment_total' => (int)$commentTotal,
        ];
    }

    /**
     * 整体数据总览-课程数据概览
     * @author liyifei
     *
     * @param int $starttime 开始时间
     * @param int $endtime 结束时间
     *
     * @return array
     */
    public function listCourseData($starttime, $endtime)
    {
        $conds = [
            'course_type' => Constant::COURSE_TYPE_NORMAL,
            'created >= ?' => $starttime,
            'created <= ?' => $endtime,
        ];

        // 课程总数
        $courseModel = new CourseArticleModel();
        $courseTotal = $courseModel->count_by_conds(['course_type' => Constant::COURSE_TYPE_NORMAL]);

        // 素材总数
        $sourceModel = new CourseSourceModel();
        $sourceTotal = $sourceModel->count();

        // 新增课程数
        $newCourseTotal = $courseModel->count_by_conds($conds);

        // 新增各类型素材数
        $listSourceTypeTotal = $sourceModel->listSourceTypeTotal($starttime, $endtime);
        if (!empty($listSourceTypeTotal)) {
            $listSourceTypeTotal = array_combine_by_key($listSourceTypeTotal, 'source_type');
        }

        // 新增图文素材数
        $newImgSourseTotal = isset($listSourceTypeTotal[Constant::SOURCE_TYPE_IMG_TEXT]['source_total']) ? $listSourceTypeTotal[Constant::SOURCE_TYPE_IMG_TEXT]['source_total'] : 0;

        // 新增音图素材数
        $newAudioSourseTotal = isset($listSourceTypeTotal[Constant::SOURCE_TYPE_AUDIO_IMG]['source_total']) ? $listSourceTypeTotal[Constant::SOURCE_TYPE_AUDIO_IMG]['source_total'] : 0;

        // 新增视频素材数
        $newVideoSourseTotal = isset($listSourceTypeTotal[Constant::SOURCE_TYPE_VEDIO]['source_total']) ? $listSourceTypeTotal[Constant::SOURCE_TYPE_VEDIO]['source_total'] : 0;

        // 新增文件素材数
        $newFileSourseTotal = isset($listSourceTypeTotal[Constant::SOURCE_TYPE_FILE]['source_total']) ? $listSourceTypeTotal[Constant::SOURCE_TYPE_FILE]['source_total'] : 0;

        // 新增外部素材数
        $newOutsideSourseTotal = isset($listSourceTypeTotal[Constant::SOURCE_TYPE_LINK]['source_total']) ? $listSourceTypeTotal[Constant::SOURCE_TYPE_LINK]['source_total'] : 0;

        // 新增素材数
        $newSourceTotal = $newImgSourseTotal + $newAudioSourseTotal + $newVideoSourseTotal + $newFileSourseTotal + $newOutsideSourseTotal;

        return [
            'course_total' => (int)$courseTotal,
            'sourse_total' => (int)$sourceTotal,
            'new_course_total' => (int)$newCourseTotal,
            'new_sourse_total' => (int)$newSourceTotal,
            'new_img_sourse_total' => (int)$newImgSourseTotal,
            'new_audio_sourse_total' => (int)$newAudioSourseTotal,
            'new_video_sourse_total' => (int)$newVideoSourseTotal,
            'new_file_sourse_total' => (int)$newFileSourseTotal,
            'new_outside_sourse_total' => (int)$newOutsideSourseTotal,
        ];
    }

    /**
     * 员工学习数据:仅搜索人员
     * @author liyifei
     *
     * @param array $userConds 人员搜索条件
     *
     * @return array
     */
    public function userStudyListByUser($userConds)
    {
        $userList = [];

        // 搜索人员

            $userServ = &User::instance();
            $userList = $userServ->listUsersAll($userConds);
            if (!empty($userList)) {
                $userList = array_combine_by_key($userList, 'memUid');
            }


        return $userList;
    }

    /**
     * 员工学习数据:仅搜索课程
     * @author liyifei
     *
     * @param array $articleConds 课程搜索条件
     *
     * @return array
     */
    public function userStudyListByArticle($articleConds)
    {
        $articleList = [];

        if (is_array($articleConds) && !empty($articleConds)) {
            // 补全课程搜索条件(常规课程、已发布的课程)
            $articleConds['course_type'] = Constant::COURSE_TYPE_NORMAL;
            $articleConds['article_status'] = Constant::ARTICLE_STATUS_SEND;

            $courseModel = new CourseArticleModel();
            $articleList = $courseModel->list_by_conds($articleConds);
            if (!empty($articleList)) {
                $articleList = array_combine_by_key($articleList, 'article_id');
            }
        }

        return $articleList;
    }

    /**
     * 员工学习数据:搜索人员和课程
     * @author liyifei
     *
     * @param array $userConds 人员搜索条件
     * @param array $articleConds 课程搜索条件
     *
     * @return array
     */
    public function userStudyListByUserArticle($userConds, $articleConds)
    {
        $userList = [];

        // 获取课程列表
        $articleList = $this->userStudyListByArticle($articleConds);
        if (!empty($articleList)) {
            $articleIds = array_keys($articleList);

            // 获取课程适用范围
            $rightModel = new CourseRightModel();
            $rights = $rightModel->list_by_conds(['article_id' => $articleIds, 'class_id' => 0, 'award_id' => 0]);
            $formatRights = $rightModel->formatDBData($rights);

            // 合并课程适用范围、人员搜索条件
            $ucConds = $rightModel->mergeRightData($userConds, $formatRights);

            // 通过UC,筛选在课程适用范围内,且符合人员搜索条件的人员
            $userServ = &User::instance();
            $userList = $userServ->listUsersAll($ucConds);
            if (!empty($userList)) {
                $userList = array_combine_by_key($userList, 'memUid');
            }
        }

        return [$userList, $articleList];
    }
}