src/AppBundle/Controller/Course/LiveCourseSetController.php line 61

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller\Course;
  3. use AppBundle\Common\ArrayToolkit;
  4. use AppBundle\Common\Paginator;
  5. use Biz\System\Service\SettingService;
  6. use Biz\Task\Service\TaskService;
  7. use Symfony\Component\HttpFoundation\Request;
  8. class LiveCourseSetController extends CourseBaseController
  9. {
  10.     public function courseSetsBlockAction($courseSets$view 'list'$mode 'default')
  11.     {
  12.         $courses $this->getCourseService()->findCoursesByCourseSetIds(ArrayToolkit::column($courseSets'id'));
  13.         return $this->forward(
  14.             'AppBundle:Course/LiveCourseSet:coursesBlock',
  15.             [
  16.                 'courses' => $courses,
  17.                 'view' => $view,
  18.                 'mode' => $mode,
  19.             ]
  20.         );
  21.     }
  22.     public function coursesBlockAction($courses$view 'list'$mode 'default')
  23.     {
  24.         $userIds = [];
  25.         foreach ($courses as $course) {
  26.             $userIds array_merge($userIds, empty($course['teacherIds']) ? [] : $course['teacherIds']);
  27.         }
  28.         $users $this->getUserService()->findUsersByIds($userIds);
  29.         foreach ($courses as &$course) {
  30.             if (empty($course['id'])) {
  31.                 $course = [];
  32.             }
  33.         }
  34.         $courses array_filter($courses);
  35.         return $this->render(
  36.             "course/block/courses-block-{$view}.html.twig",
  37.             [
  38.                 'courses' => $courses,
  39.                 'users' => $users,
  40.                 'mode' => $mode,
  41.             ]
  42.         );
  43.     }
  44.     public function exploreAction(Request $request)
  45.     {
  46.         if (!$this->setting('course.live_course_enabled')) {
  47.             return $this->createMessageResponse('info'$this->get('translator')->trans('直播频道已关闭'));
  48.         }
  49.         return $this->render('course-set/live/explore.html.twig');
  50.     }
  51.     public function replayListAction()
  52.     {
  53.         $publishedCourseSetIds $this->_findPublishedLiveCourseSetIds();
  54.         $liveReplayList $this->getTaskService()->searchTasks(
  55.             [
  56.                 'endTime_LT' => time(),
  57.                 'type' => 'live',
  58.                 'copyId' => 0,
  59.                 'status' => 'published',
  60.                 'fromCourseSetIds' => $publishedCourseSetIds,
  61.             ],
  62.             ['startTime' => 'DESC'],
  63.             0,
  64.             10
  65.         );
  66.         return $this->render(
  67.             'course-set/live/replay-list.html.twig',
  68.             [
  69.                 'liveReplayList' => $liveReplayList,
  70.             ]
  71.         );
  72.     }
  73.     public function liveTabAction()
  74.     {
  75.         $currentLiveTasks $this->getTaskService()->findCurrentLiveTasks();
  76.         $dayTasks $this->getTaskService()->searchTasks(
  77.             [
  78.                 'type' => 'live',
  79.                 'status' => 'published',
  80.                 'startTime_GT' => time(),
  81.             ],
  82.             ['startTime' => 'ASC'],
  83.             0,
  84.             PHP_INT_MAX
  85.         );
  86.         $this->filterUnPublishTasks($currentLiveTasks$dayTasks);
  87.         $liveTabs['today']['current'] = $currentLiveTasks;
  88.         $dateTabs = ['today'];
  89.         $today date('Y-m-d');
  90.         foreach ($dayTasks as $key => $value) {
  91.             $timeKey date('Y-m-d'$value['startTime']);
  92.             $shortTimeKey date('m-d'$value['startTime']);
  93.             if ($timeKey === $today) {
  94.                 $liveTabs['today']['future'][] = $value;
  95.             } else {
  96.                 $liveTabs[$shortTimeKey]['future'][] = $value;
  97.                 $dateTabs[] = $shortTimeKey;
  98.             }
  99.         }
  100.         $dateTabs array_unique($dateTabs);
  101.         list($dateTabs$liveTabs) = $this->filterliveTabs($dateTabs$liveTabs4);
  102.         return $this->render(
  103.             'course-set/live/tab.html.twig',
  104.             [
  105.                 'liveTabs' => $liveTabs,
  106.                 'dateTabs' => $dateTabs,
  107.             ]
  108.         );
  109.     }
  110.     private function filterUnPublishTasks(&$currentLiveTasks, &$dayTasks)
  111.     {
  112.         $courseIds array_merge(array_column($currentLiveTasks'courseId'), array_column($dayTasks'courseId'));
  113.         $courseSetIds array_merge(array_column($currentLiveTasks'fromCourseSetId'), array_column($dayTasks'fromCourseSetId'));
  114.         $courses $this->getCourseService()->findCoursesByIds($courseIds);
  115.         $courseSets $this->getCourseSetService()->findCourseSetsByIds($courseSetIds);
  116.         foreach ($currentLiveTasks as $key => $currentLiveTask) {
  117.             if ('published' !== $courses[$currentLiveTask['courseId']]['status']
  118.                 || 'published' !== $courseSets[$currentLiveTask['fromCourseSetId']]['status']) {
  119.                 unset($currentLiveTasks[$key]);
  120.             }
  121.         }
  122.         foreach ($dayTasks as $key => $dayTask) {
  123.             if ('published' !== $courses[$dayTask['courseId']]['status']
  124.                 || 'published' !== $courseSets[$dayTask['fromCourseSetId']]['status']) {
  125.                 unset($dayTasks[$key]);
  126.             }
  127.         }
  128.     }
  129.     private function filterliveTabs($dateTabs$liveTabs$num)
  130.     {
  131.         $dateTabs array_slice($dateTabs0$num);
  132.         foreach ($liveTabs as $key => $value) {
  133.             if (!in_array($key$dateTabs)) {
  134.                 unset($liveTabs[$key]);
  135.             }
  136.         }
  137.         return [$dateTabs$liveTabs];
  138.     }
  139.     public function liveCourseSetsAction(Request $request)
  140.     {
  141.         $categoryId $request->query->get('categoryId''');
  142.         $vipCategoryId $request->query->get('vipCategoryId''');
  143.         $currentPage $request->query->get('page'1);
  144.         $vipCourseSetIds $this->_findVipCourseSetIds($vipCategoryId);
  145.         $futureLiveCourseSets $this->_findFutureLiveCourseSets($vipCourseSetIds$categoryId);
  146.         $paginator = new Paginator(
  147.             $request,
  148.             $this->getCourseSetService()->countCourseSets(
  149.                 [
  150.                     'ids' => $vipCourseSetIds,
  151.                     'type' => 'live',
  152.                     'status' => 'published',
  153.                     'categoryId' => $categoryId,
  154.                 ]
  155.             ),
  156.             10
  157.         );
  158.         $replayLiveCourseSets = [];
  159.         if (count($futureLiveCourseSets) < $paginator->getPerPageCount()) {
  160.             $futureLiveCourseSetIds ArrayToolkit::column($futureLiveCourseSets'id');
  161.             $replayLiveCourseSetIds array_diff($vipCourseSetIds$futureLiveCourseSetIds);
  162.             $replayLiveCourseSets $this->_findReplayLiveCourseSets(
  163.                 $currentPage,
  164.                 $replayLiveCourseSetIds,
  165.                 $categoryId
  166.             );
  167.         }
  168.         $liveCourseSets array_merge($futureLiveCourseSets$replayLiveCourseSets);
  169.         $liveCourseSets ArrayToolkit::index($liveCourseSets'id');
  170.         $liveCourseSets $this->_fillLiveCourseSetAttribute($liveCourseSets);
  171.         $levels = [];
  172.         if ($this->isPluginInstalled('Vip')) {
  173.             $levels ArrayToolkit::index(
  174.                 $this->getLevelService()->searchLevels(['enabled' => 1], [], 0100),
  175.                 'id'
  176.             );
  177.         }
  178.         return $this->render(
  179.             'course-set/live/all-list.html.twig',
  180.             [
  181.                 'liveCourseSets' => $liveCourseSets,
  182.                 'paginator' => $paginator,
  183.                 'request' => $request,
  184.                 'levels' => $levels,
  185.             ]
  186.         );
  187.     }
  188.     private function _fillLiveCourseSetAttribute($liveCourseSets)
  189.     {
  190.         if (empty($liveCourseSets)) {
  191.             return [];
  192.         }
  193.         $liveCourseSetIds array_keys($liveCourseSets);
  194.         $courses $this->getCourseService()->findCoursesByCourseSetIds($liveCourseSetIds);
  195.         $courses ArrayToolkit::index($courses'courseSetId');
  196.         $ret = [];
  197.         foreach ($liveCourseSetIds as $key => $courseSetId) {
  198.             $ret[$courseSetId] = $liveCourseSets[$courseSetId];
  199.             $ret[$courseSetId]['course'] = $courses[$courseSetId];
  200.             $now time();
  201.             //正在直播的课时
  202.             $tasks $this->getTaskService()->searchTasks(
  203.                 ['fromCourseSetId' => $courseSetId'type' => 'live''startTime_LE' => $now'endTime_GT' => $now],
  204.                 ['startTime' => 'ASC'],
  205.                 0,
  206.                 1
  207.             );
  208.             if (empty($tasks)) {
  209.                 //第一个已经结束的课时课程
  210.                 $tasks $this->getTaskService()->searchTasks(
  211.                     ['fromCourseSetId' => $courseSetId'type' => 'live''endTime_LT' => $now],
  212.                     ['startTime' => 'ASC'],
  213.                     0,
  214.                     1
  215.                 );
  216.                 //第一个未开始过的课时
  217.                 $advanceTasks $this->getTaskService()->searchTasks(
  218.                     ['fromCourseSetId' => $courseSetId'type' => 'live''endTime_GT' => $now],
  219.                     ['startTime' => 'ASC'],
  220.                     0,
  221.                     1
  222.                 );
  223.                 if (!empty($advanceTasks)) {
  224.                     $ret[$courseSetId]['advanceTime'] = $advanceTasks[0]['startTime'];
  225.                 }
  226.             }
  227.             if (empty($tasks)) {
  228.                 continue;
  229.             }
  230.             $ret[$courseSetId]['liveStartTime'] = $tasks[0]['startTime'];
  231.             $ret[$courseSetId]['liveEndTime'] = $tasks[0]['endTime'];
  232.             $ret[$courseSetId]['taskId'] = $tasks[0]['id'];
  233.         }
  234.         return $ret;
  235.     }
  236.     private function _findVipCourseSetIds($vipLevelId)
  237.     {
  238.         if (!$this->isPluginInstalled('Vip')) {
  239.             return [];
  240.         }
  241.         $preLevelIds ArrayToolkit::column($this->getLevelService()->findPrevEnabledLevels($vipLevelId), 'id');
  242.         $vipCourseConditions = [
  243.             'status' => 'published',
  244.             'parentId' => 0,
  245.         ];
  246.         if (!empty($vipLevelId)) {
  247.             $preLevelIds array_merge($preLevelIds, [$vipLevelId]);
  248.             $vipCourseConditions['vipLevelIds'] = $preLevelIds;
  249.         }
  250.         $vipCourses $this->getCourseService()->searchCourses(
  251.             $vipCourseConditions,
  252.             'latest',
  253.             0,
  254.             PHP_INT_MAX
  255.         );
  256.         $vipCourseSetIds ArrayToolkit::column($vipCourses'courseSetId');
  257.         return $vipCourseSetIds;
  258.     }
  259.     private function _findFutureLiveCourseSets($courseSetIds$categoryId)
  260.     {
  261.         $futureLiveTasks $this->getTaskService()->findFutureLiveTasks();
  262.         $futureCourseSetIds ArrayToolkit::column($futureLiveTasks'fromCourseSetId');
  263.         $futureLiveCourseSetIds array_intersect($futureCourseSetIds$courseSetIds);
  264.         if (empty($futureLiveCourseSetIds)) {
  265.             $futureLiveCourseSetIds = [-1];
  266.         }
  267.         $condition = [
  268.             'status' => 'published',
  269.             'type' => 'live',
  270.             'ids' => $futureLiveCourseSetIds,
  271.             'categoryId' => $categoryId,
  272.         ];
  273.         $futureLiveCourseSets $this->getCourseSetService()->searchCourseSets(
  274.             $condition,
  275.             'latest',
  276.             0,
  277.             PHP_INT_MAX
  278.         );
  279.         return $futureLiveCourseSets;
  280.     }
  281.     private function _findReplayLiveCourseSets($currentPage$replayLiveCourseSetIds$categoryId)
  282.     {
  283.         $pageSize 10;
  284.         if (isset($conditions['ids'])) {
  285.             $futureLiveCourseSetsCount $this->getCourseSetService()->countCourseSets($conditions);
  286.         } else {
  287.             $futureLiveCourseSetsCount 0;
  288.         }
  289.         $pages $futureLiveCourseSetsCount <= $pageSize floor($futureLiveCourseSetsCount $pageSize);
  290.         if ($pages == $currentPage) {
  291.             $start 0;
  292.             $limit $pageSize - ($futureLiveCourseSetsCount $pageSize);
  293.         } else {
  294.             $start = ($currentPage 1) * $pageSize;
  295.             $limit $pageSize;
  296.         }
  297.         $conditions = [
  298.             'ids' => $replayLiveCourseSetIds,
  299.             'type' => 'live',
  300.             'status' => 'published',
  301.             'categoryId' => $categoryId,
  302.         ];
  303.         $replayLiveCourseSets $this->getCourseSetService()->searchCourseSets(
  304.             $conditions,
  305.             ['createdTime' => 'DESC'],
  306.             $start,
  307.             $limit
  308.         );
  309.         return $replayLiveCourseSets;
  310.     }
  311.     private function _findPublishedLiveCourseSetIds()
  312.     {
  313.         $conditions = [
  314.             'status' => 'published',
  315.             'type' => 'live',
  316.             'parentId' => 0,
  317.         ];
  318.         $publishedCourseSets $this->getCourseSetService()->searchCourseSets(
  319.             $conditions,
  320.             ['createdTime' => 'DESC'],
  321.             0,
  322.             PHP_INT_MAX
  323.         );
  324.         return ArrayToolkit::column($publishedCourseSets'id');
  325.     }
  326.     /**
  327.      * @return TaskService
  328.      */
  329.     protected function getTaskService()
  330.     {
  331.         return $this->createService('Task:TaskService');
  332.     }
  333.     /**
  334.      * @return SettingService
  335.      */
  336.     protected function getSettingService()
  337.     {
  338.         return $this->createService('System:SettingService');
  339.     }
  340.     /**
  341.      * @return LevelService
  342.      */
  343.     protected function getLevelService()
  344.     {
  345.         return $this->createService('VipPlugin:Vip:LevelService');
  346.     }
  347. }