src/AppBundle/Controller/Course/CourseSetController.php line 122

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller\Course;
  3. use AppBundle\Common\ArrayToolkit;
  4. use AppBundle\Common\Paginator;
  5. use AppBundle\Controller\BaseController;
  6. use Biz\Course\CourseException;
  7. use Biz\Course\Service\CourseService;
  8. use Biz\Course\Service\CourseSetService;
  9. use Biz\Task\Service\TaskService;
  10. use Biz\Taxonomy\Service\CategoryService;
  11. use Biz\Taxonomy\Service\TagService;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class CourseSetController extends BaseController
  14. {
  15.     public function showAction(Request $request$id)
  16.     {
  17.         $courseSet $this->getCourseSetService()->getCourseSet($id);
  18.         $course $this->getCourseService()->getCourse($courseSet['defaultCourseId']);
  19.         $previewAs $request->query->get('previewAs');
  20.         if (empty($course)) {
  21.             $this->createNewException(CourseException::NOTFOUND_COURSE());
  22.         }
  23.         $this->getCourseSetService()->hitCourseSet($id);
  24.         return $this->redirect($this->generateUrl('course_show', ['id' => $course['id'], 'previewAs' => $previewAs]));
  25.     }
  26.     public function courseSetsBlockAction(array $courseSets$view 'list'$mode 'default')
  27.     {
  28.         $userIds = [];
  29.         $service $this->getCourseService();
  30.         $courseSets array_map(function ($set) use (&$userIds$service) {
  31.             $set['course'] = $service->getFirstPublishedCourseByCourseSetId($set['id']);
  32.             if (!empty($set['course']['teacherIds']) && is_array($set['course']['teacherIds'])) {
  33.                 $userIds array_merge($userIds$set['course']['teacherIds']);
  34.             }
  35.             return $set;
  36.         }, $courseSets);
  37.         $users $this->getUserService()->findUsersByIds($userIds);
  38.         return $this->render("course-set/block/course-block-{$view}.html.twig", [
  39.             'courseSets' => $this->getWebExtension()->filterCourseSetsVipRight($courseSets),
  40.             'users' => $users,
  41.             'mode' => $mode,
  42.         ]);
  43.     }
  44.     public function archiveAction()
  45.     {
  46.         $conditions = [
  47.             'status' => 'published',
  48.             'parentId' => '0',
  49.         ];
  50.         $conditions $this->getCourseService()->appendReservationConditions($conditions);
  51.         $paginator = new Paginator(
  52.             $this->get('request'),
  53.             $this->getCourseSetService()->countCourseSets($conditions),
  54.             30
  55.         );
  56.         $courseSets $this->getCourseSetService()->searchCourseSets(
  57.             $conditions,
  58.             'latest',
  59.             $paginator->getOffsetCount(),
  60.             $paginator->getPerPageCount()
  61.         );
  62.         $userIds = [];
  63.         foreach ($courseSets as &$courseSet) {
  64.             $tagIds $this->getTagIdsByCourseSet($courseSet);
  65.             $courseSet['tags'] = $this->getTagService()->findTagsByIds($tagIds);
  66.             $userIds array_merge($userIds, [$courseSet['creator']]);
  67.         }
  68.         $users $this->getUserService()->findUsersByIds($userIds);
  69.         return $this->render('course-set/archive/index.html.twig', [
  70.             'courseSets' => $courseSets,
  71.             'paginator' => $paginator,
  72.             'users' => $users,
  73.         ]);
  74.     }
  75.     public function archiveDetailAction($courseSetId)
  76.     {
  77.         $courseSet $this->getCourseSetService()->getCourseSet($courseSetId);
  78.         $course $this->getCourseService()->getFirstPublishedCourseByCourseSetId($courseSet['id']);
  79.         $tasks $this->getTaskService()->findTasksByCourseId($course['id']);
  80.         $tagIds $this->getTagIdsByCourseSet($courseSet);
  81.         $tags $this->getTagService()->findTagsByIds($tagIds);
  82.         $category $this->getCategoryService()->getCategory($courseSet['categoryId']);
  83.         if (!$course) {
  84.             $courseDescription = [];
  85.         } else {
  86.             $courseDescription $course['about'];
  87.             $courseDescription strip_tags($courseDescription'');
  88.             $courseDescription preg_replace('/ /'''$courseDescription);
  89.             $courseDescription substr($courseDescription0100);
  90.         }
  91.         return $this->render('course-set/archive/course.html.twig', [
  92.             'courseSet' => $courseSet,
  93.             'course' => $course,
  94.             'tasks' => $tasks,
  95.             'tags' => $tags,
  96.             'category' => $category,
  97.             'courseDescription' => $courseDescription,
  98.         ]);
  99.     }
  100.     public function archiveTaskAction($courseSetId$taskId)
  101.     {
  102.         $courseSet $this->getCourseSetService()->getCourseSet($courseSetId);
  103.         $course $this->getCourseService()->getFirstPublishedCourseByCourseSetId($courseSet['id']);
  104.         $tagIds $this->getTagIdsByCourseSet($courseSet);
  105.         $tags $this->getTagService()->findTagsByIds($tagIds);
  106.         $tasks $this->getTaskService()->findTasksByCourseId($course['id']);
  107.         if ('' == $taskId && null != $tasks) {
  108.             $currentTask current($tasks);
  109.         } else {
  110.             $currentTask $this->getTaskService()->getTask($taskId);
  111.         }
  112.         return $this->render('course-set/archive/task.html.twig', [
  113.             'course' => $course,
  114.             'courseSet' => $courseSet,
  115.             'tasks' => $tasks,
  116.             'currentTask' => $currentTask,
  117.             'tags' => $tags,
  118.         ]);
  119.     }
  120.     protected function getTagIdsByCourseSet(array $courseSet)
  121.     {
  122.         $tags $this->getTagService()->findTagsByOwner(['ownerType' => 'course-set''ownerId' => $courseSet['id']]);
  123.         return ArrayToolkit::column($tags'id');
  124.     }
  125.     /**
  126.      * @return CourseService
  127.      */
  128.     protected function getCourseService()
  129.     {
  130.         return $this->createService('Course:CourseService');
  131.     }
  132.     /**
  133.      * @return CourseSetService
  134.      */
  135.     protected function getCourseSetService()
  136.     {
  137.         return $this->createService('Course:CourseSetService');
  138.     }
  139.     /**
  140.      * @return TagService
  141.      */
  142.     protected function getTagService()
  143.     {
  144.         return $this->createService('Taxonomy:TagService');
  145.     }
  146.     /**
  147.      * @return TaskService
  148.      */
  149.     protected function getTaskService()
  150.     {
  151.         return $this->createService('Task:TaskService');
  152.     }
  153.     /**
  154.      * @return CategoryService
  155.      */
  156.     protected function getCategoryService()
  157.     {
  158.         return $this->createService('Taxonomy:CategoryService');
  159.     }
  160. }