| Total Complexity | 116 |
| Total Lines | 928 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CourseController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CourseController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 65 | #[Route('/course')] |
||
| 66 | class CourseController extends ToolBaseController |
||
| 67 | { |
||
| 68 | public function __construct( |
||
| 69 | private readonly EntityManagerInterface $em, |
||
| 70 | private readonly SerializerInterface $serializer, |
||
| 71 | private readonly UserHelper $userHelper, |
||
| 72 | ) {} |
||
| 73 | |||
| 74 | #[Route('/cid_reset', methods: ['GET'])] |
||
| 85 | } |
||
| 86 | |||
| 87 | #[Route('/{cid}/checkLegal.json', name: 'chamilo_core_course_check_legal_json')] |
||
| 88 | public function checkTermsAndConditionJson( |
||
| 89 | Request $request, |
||
| 90 | LegalRepository $legalTermsRepo, |
||
| 91 | LanguageRepository $languageRepository, |
||
| 92 | ExtraFieldValuesRepository $extraFieldValuesRepository, |
||
| 93 | SettingsManager $settingsManager |
||
| 94 | ): Response { |
||
| 95 | $user = $this->userHelper->getCurrent(); |
||
| 96 | $course = $this->getCourse(); |
||
| 97 | $responseData = [ |
||
| 98 | 'redirect' => false, |
||
| 99 | 'url' => '#', |
||
| 100 | ]; |
||
| 101 | |||
| 102 | if ($user && $user->hasRole('ROLE_STUDENT') |
||
| 103 | && 'true' === $settingsManager->getSetting('allow_terms_conditions') |
||
| 104 | && 'course' === $settingsManager->getSetting('load_term_conditions_section') |
||
| 105 | ) { |
||
| 106 | $termAndConditionStatus = false; |
||
| 107 | $extraValue = $extraFieldValuesRepository->findLegalAcceptByItemId($user->getId()); |
||
| 108 | if (!empty($extraValue['value'])) { |
||
| 109 | $result = $extraValue['value']; |
||
| 110 | $userConditions = explode(':', $result); |
||
| 111 | $version = $userConditions[0]; |
||
| 112 | $langId = (int) $userConditions[1]; |
||
| 113 | $realVersion = $legalTermsRepo->getLastVersion($langId); |
||
| 114 | $termAndConditionStatus = ($version >= $realVersion); |
||
| 115 | } |
||
| 116 | |||
| 117 | if (false === $termAndConditionStatus) { |
||
| 118 | $request->getSession()->set('term_and_condition', ['user_id' => $user->getId()]); |
||
| 119 | } else { |
||
| 120 | $request->getSession()->remove('term_and_condition'); |
||
| 121 | } |
||
| 122 | |||
| 123 | $termsAndCondition = $request->getSession()->get('term_and_condition'); |
||
| 124 | if (null !== $termsAndCondition) { |
||
| 125 | $redirect = true; |
||
| 126 | $allow = 'true' === Container::getSettingsManager() |
||
| 127 | ->getSetting('course.allow_public_course_with_no_terms_conditions') |
||
| 128 | ; |
||
| 129 | |||
| 130 | if (true === $allow |
||
| 131 | && null !== $course->getVisibility() |
||
| 132 | && Course::OPEN_WORLD === $course->getVisibility() |
||
| 133 | ) { |
||
| 134 | $redirect = false; |
||
| 135 | } |
||
| 136 | if ($redirect && !$this->isGranted('ROLE_ADMIN')) { |
||
| 137 | $url = '/main/auth/inscription.php'; |
||
| 138 | $responseData = [ |
||
| 139 | 'redirect' => true, |
||
| 140 | 'url' => $url, |
||
| 141 | ]; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | return new JsonResponse($responseData); |
||
| 147 | } |
||
| 148 | |||
| 149 | #[Route('/{cid}/home.json', name: 'chamilo_core_course_home_json')] |
||
| 254 | ] |
||
| 255 | ); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Redirects the page to a tool, following the tools settings. |
||
| 260 | */ |
||
| 261 | #[Route('/{cid}/tool/{toolName}', name: 'chamilo_core_course_redirect_tool')] |
||
| 295 | } |
||
| 296 | |||
| 297 | /*public function redirectToShortCut(string $toolName, CToolRepository $repo, ToolChain $toolChain): RedirectResponse |
||
| 298 | { |
||
| 299 | $tool = $repo->findOneBy([ |
||
| 300 | 'name' => $toolName, |
||
| 301 | ]); |
||
| 302 | |||
| 303 | if (null === $tool) { |
||
| 304 | throw new NotFoundHttpException($this->trans('Tool not found')); |
||
| 305 | } |
||
| 306 | |||
| 307 | $tool = $toolChain->getToolFromName($tool->getTool()->getTitle()); |
||
| 308 | $link = $tool->getLink(); |
||
| 309 | |||
| 310 | if (strpos($link, 'nodeId')) { |
||
| 311 | $nodeId = (string) $this->getCourse()->getResourceNode()->getId(); |
||
| 312 | $link = str_replace(':nodeId', $nodeId, $link); |
||
| 313 | } |
||
| 314 | |||
| 315 | $url = $link.'?'.$this->getCourseUrlQuery(); |
||
| 316 | |||
| 317 | return $this->redirect($url); |
||
| 318 | }*/ |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Edit configuration with given namespace. |
||
| 322 | */ |
||
| 323 | #[Route('/{course}/settings/{namespace}', name: 'chamilo_core_course_settings')] |
||
| 369 | ] |
||
| 370 | ); |
||
| 371 | } |
||
| 372 | |||
| 373 | #[Route('/{id}/about', name: 'chamilo_core_course_about')] |
||
| 374 | public function about( |
||
| 375 | Course $course, |
||
| 376 | IllustrationRepository $illustrationRepository, |
||
| 377 | CCourseDescriptionRepository $courseDescriptionRepository, |
||
| 378 | EntityManagerInterface $em, |
||
| 379 | Request $request |
||
| 380 | ): Response { |
||
| 381 | $courseId = $course->getId(); |
||
| 382 | |||
| 383 | $user = $this->userHelper->getCurrent(); |
||
| 384 | |||
| 385 | $fieldsRepo = $em->getRepository(ExtraField::class); |
||
| 386 | |||
| 387 | /** @var TagRepository $tagRepo */ |
||
| 388 | $tagRepo = $em->getRepository(Tag::class); |
||
| 389 | |||
| 390 | $courseDescriptions = $courseDescriptionRepository->getResourcesByCourse($course)->getQuery()->getResult(); |
||
| 391 | |||
| 392 | $courseValues = new ExtraFieldValue('course'); |
||
| 393 | |||
| 394 | $urlCourse = api_get_path(WEB_PATH).sprintf('course/%s/about', $courseId); |
||
| 395 | $courseTeachers = $course->getTeachersSubscriptions(); |
||
| 396 | $teachersData = []; |
||
| 397 | |||
| 398 | foreach ($courseTeachers as $teacherSubscription) { |
||
| 399 | $teacher = $teacherSubscription->getUser(); |
||
| 400 | $userData = [ |
||
| 401 | 'complete_name' => UserManager::formatUserFullName($teacher), |
||
| 402 | 'image' => $illustrationRepository->getIllustrationUrl($teacher), |
||
| 403 | 'diploma' => $teacher->getDiplomas(), |
||
| 404 | 'openarea' => $teacher->getOpenarea(), |
||
| 405 | ]; |
||
| 406 | |||
| 407 | $teachersData[] = $userData; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** @var ExtraField $tagField */ |
||
| 411 | $tagField = $fieldsRepo->findOneBy([ |
||
| 412 | 'itemType' => ExtraField::COURSE_FIELD_TYPE, |
||
| 413 | 'variable' => 'tags', |
||
| 414 | ]); |
||
| 415 | |||
| 416 | $courseTags = []; |
||
| 417 | if (null !== $tagField) { |
||
| 418 | $courseTags = $tagRepo->getTagsByItem($tagField, $courseId); |
||
| 419 | } |
||
| 420 | |||
| 421 | $courseDescription = $courseObjectives = $courseTopics = $courseMethodology = ''; |
||
| 422 | $courseMaterial = $courseResources = $courseAssessment = ''; |
||
| 423 | $courseCustom = []; |
||
| 424 | foreach ($courseDescriptions as $descriptionTool) { |
||
| 425 | switch ($descriptionTool->getDescriptionType()) { |
||
| 426 | case CCourseDescription::TYPE_DESCRIPTION: |
||
| 427 | $courseDescription = $descriptionTool->getContent(); |
||
| 428 | |||
| 429 | break; |
||
| 430 | |||
| 431 | case CCourseDescription::TYPE_OBJECTIVES: |
||
| 432 | $courseObjectives = $descriptionTool; |
||
| 433 | |||
| 434 | break; |
||
| 435 | |||
| 436 | case CCourseDescription::TYPE_TOPICS: |
||
| 437 | $courseTopics = $descriptionTool; |
||
| 438 | |||
| 439 | break; |
||
| 440 | |||
| 441 | case CCourseDescription::TYPE_METHODOLOGY: |
||
| 442 | $courseMethodology = $descriptionTool; |
||
| 443 | |||
| 444 | break; |
||
| 445 | |||
| 446 | case CCourseDescription::TYPE_COURSE_MATERIAL: |
||
| 447 | $courseMaterial = $descriptionTool; |
||
| 448 | |||
| 449 | break; |
||
| 450 | |||
| 451 | case CCourseDescription::TYPE_RESOURCES: |
||
| 452 | $courseResources = $descriptionTool; |
||
| 453 | |||
| 454 | break; |
||
| 455 | |||
| 456 | case CCourseDescription::TYPE_ASSESSMENT: |
||
| 457 | $courseAssessment = $descriptionTool; |
||
| 458 | |||
| 459 | break; |
||
| 460 | |||
| 461 | case CCourseDescription::TYPE_CUSTOM: |
||
| 462 | $courseCustom[] = $descriptionTool; |
||
| 463 | |||
| 464 | break; |
||
| 465 | } |
||
| 466 | } |
||
| 467 | |||
| 468 | $topics = [ |
||
| 469 | 'objectives' => $courseObjectives, |
||
| 470 | 'topics' => $courseTopics, |
||
| 471 | 'methodology' => $courseMethodology, |
||
| 472 | 'material' => $courseMaterial, |
||
| 473 | 'resources' => $courseResources, |
||
| 474 | 'assessment' => $courseAssessment, |
||
| 475 | 'custom' => array_reverse($courseCustom), |
||
| 476 | ]; |
||
| 477 | |||
| 478 | $subscriptionUser = false; |
||
| 479 | |||
| 480 | if ($user) { |
||
| 481 | $subscriptionUser = CourseManager::is_user_subscribed_in_course($user->getId(), $course->getCode()); |
||
| 482 | } |
||
| 483 | |||
| 484 | /*$allowSubscribe = false; |
||
| 485 | if ($course->getSubscribe() || api_is_platform_admin()) { |
||
| 486 | $allowSubscribe = true; |
||
| 487 | } |
||
| 488 | $plugin = \BuyCoursesPlugin::create(); |
||
| 489 | $checker = $plugin->isEnabled(); |
||
| 490 | $courseIsPremium = null; |
||
| 491 | if ($checker) { |
||
| 492 | $courseIsPremium = $plugin->getItemByProduct( |
||
| 493 | $courseId, |
||
| 494 | \BuyCoursesPlugin::PRODUCT_TYPE_COURSE |
||
| 495 | ); |
||
| 496 | }*/ |
||
| 497 | |||
| 498 | $image = Container::getIllustrationRepository()->getIllustrationUrl($course, 'course_picture_medium'); |
||
| 499 | |||
| 500 | $params = [ |
||
| 501 | 'course' => $course, |
||
| 502 | 'description' => $courseDescription, |
||
| 503 | 'image' => $image, |
||
| 504 | 'syllabus' => $topics, |
||
| 505 | 'tags' => $courseTags, |
||
| 506 | 'teachers' => $teachersData, |
||
| 507 | 'extra_fields' => $courseValues->getAllValuesForAnItem( |
||
| 508 | $course->getId(), |
||
| 509 | null, |
||
| 510 | true |
||
| 511 | ), |
||
| 512 | 'subscription' => $subscriptionUser, |
||
| 513 | 'url' => '', |
||
| 514 | 'is_premium' => '', |
||
| 515 | 'token' => '', |
||
| 516 | 'base_url' => $request->getSchemeAndHttpHost(), |
||
| 517 | ]; |
||
| 518 | |||
| 519 | $metaInfo = '<meta property="og:url" content="'.$urlCourse.'" />'; |
||
| 520 | $metaInfo .= '<meta property="og:type" content="website" />'; |
||
| 521 | $metaInfo .= '<meta property="og:title" content="'.$course->getTitle().'" />'; |
||
| 522 | $metaInfo .= '<meta property="og:description" content="'.strip_tags($courseDescription).'" />'; |
||
| 523 | $metaInfo .= '<meta property="og:image" content="'.$image.'" />'; |
||
| 524 | |||
| 525 | $htmlHeadXtra[] = $metaInfo; |
||
| 526 | $htmlHeadXtra[] = api_get_asset('readmore-js/readmore.js'); |
||
| 527 | |||
| 528 | return $this->render('@ChamiloCore/Course/about.html.twig', $params); |
||
| 529 | } |
||
| 530 | |||
| 531 | #[Route('/{id}/welcome', name: 'chamilo_core_course_welcome')] |
||
| 532 | public function welcome(Course $course): Response |
||
| 533 | { |
||
| 534 | return $this->render('@ChamiloCore/Course/welcome.html.twig', [ |
||
| 535 | 'course' => $course, |
||
| 536 | ]); |
||
| 537 | } |
||
| 538 | |||
| 539 | private function findIntroOfCourse(Course $course): ?CTool |
||
| 540 | { |
||
| 541 | $qb = $this->em->createQueryBuilder(); |
||
| 542 | |||
| 543 | $query = $qb->select('ct') |
||
| 544 | ->from(CTool::class, 'ct') |
||
| 545 | ->where('ct.course = :c_id') |
||
| 546 | ->andWhere('ct.title = :title') |
||
| 547 | ->andWhere( |
||
| 548 | $qb->expr()->orX( |
||
| 549 | $qb->expr()->eq('ct.session', ':session_id'), |
||
| 550 | $qb->expr()->isNull('ct.session') |
||
| 551 | ) |
||
| 552 | ) |
||
| 553 | ->setParameters([ |
||
| 554 | 'c_id' => $course->getId(), |
||
| 555 | 'title' => 'course_homepage', |
||
| 556 | 'session_id' => 0, |
||
| 557 | ]) |
||
| 558 | ->getQuery() |
||
| 559 | ; |
||
| 560 | |||
| 561 | $results = $query->getResult(); |
||
| 562 | |||
| 563 | return \count($results) > 0 ? $results[0] : null; |
||
| 564 | } |
||
| 565 | |||
| 566 | #[Route('/{id}/getToolIntro', name: 'chamilo_core_course_gettoolintro')] |
||
| 567 | public function getToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response |
||
| 568 | { |
||
| 569 | $sessionId = (int) $request->get('sid'); |
||
| 570 | |||
| 571 | // $session = $this->getSession(); |
||
| 572 | $responseData = []; |
||
| 573 | $ctoolRepo = $em->getRepository(CTool::class); |
||
| 574 | $sessionRepo = $em->getRepository(Session::class); |
||
| 575 | $createInSession = false; |
||
| 576 | |||
| 577 | $session = null; |
||
| 578 | |||
| 579 | if (!empty($sessionId)) { |
||
| 580 | $session = $sessionRepo->find($sessionId); |
||
| 581 | } |
||
| 582 | |||
| 583 | $ctool = $this->findIntroOfCourse($course); |
||
| 584 | |||
| 585 | if ($session) { |
||
| 586 | $ctoolSession = $ctoolRepo->findOneBy(['title' => 'course_homepage', 'course' => $course, 'session' => $session]); |
||
| 587 | |||
| 588 | if (!$ctoolSession) { |
||
| 589 | $createInSession = true; |
||
| 590 | } else { |
||
| 591 | $ctool = $ctoolSession; |
||
| 592 | } |
||
| 593 | } |
||
| 594 | |||
| 595 | if ($ctool) { |
||
| 596 | $ctoolintroRepo = $em->getRepository(CToolIntro::class); |
||
| 597 | |||
| 598 | /** @var CToolIntro $ctoolintro */ |
||
| 599 | $ctoolintro = $ctoolintroRepo->findOneBy(['courseTool' => $ctool]); |
||
| 600 | if ($ctoolintro) { |
||
| 601 | $responseData = [ |
||
| 602 | 'iid' => $ctoolintro->getIid(), |
||
| 603 | 'introText' => $ctoolintro->getIntroText(), |
||
| 604 | 'createInSession' => $createInSession, |
||
| 605 | 'cToolId' => $ctool->getIid(), |
||
| 606 | ]; |
||
| 607 | } |
||
| 608 | $responseData['c_tool'] = [ |
||
| 609 | 'iid' => $ctool->getIid(), |
||
| 610 | 'title' => $ctool->getTitle(), |
||
| 611 | ]; |
||
| 612 | } |
||
| 613 | |||
| 614 | return new JsonResponse($responseData); |
||
| 615 | } |
||
| 616 | |||
| 617 | #[Route('/{id}/addToolIntro', name: 'chamilo_core_course_addtoolintro')] |
||
| 618 | public function addToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response |
||
| 619 | { |
||
| 620 | $data = $request->getContent(); |
||
| 621 | $data = json_decode($data); |
||
| 622 | $ctoolintroId = $data->iid; |
||
| 623 | $sessionId = $data->sid ?? 0; |
||
| 624 | |||
| 625 | $sessionRepo = $em->getRepository(Session::class); |
||
| 626 | $session = null; |
||
| 627 | if (!empty($sessionId)) { |
||
| 628 | $session = $sessionRepo->find($sessionId); |
||
| 629 | } |
||
| 630 | |||
| 631 | $ctool = $em->getRepository(CTool::class); |
||
| 632 | $check = $ctool->findOneBy(['title' => 'course_homepage', 'course' => $course, 'session' => $session]); |
||
| 633 | if (!$check) { |
||
| 634 | $toolRepo = $em->getRepository(Tool::class); |
||
| 635 | $toolEntity = $toolRepo->findOneBy(['title' => 'course_homepage']); |
||
| 636 | $courseTool = (new CTool()) |
||
| 637 | ->setTool($toolEntity) |
||
| 638 | ->setTitle('course_homepage') |
||
| 639 | ->setCourse($course) |
||
| 640 | ->setPosition(1) |
||
| 641 | ->setVisibility(true) |
||
| 642 | ->setParent($course) |
||
| 643 | ->setCreator($course->getCreator()) |
||
| 644 | ->setSession($session) |
||
| 645 | ->addCourseLink($course) |
||
| 646 | ; |
||
| 647 | $em->persist($courseTool); |
||
| 648 | $em->flush(); |
||
| 649 | if ($courseTool && !empty($ctoolintroId)) { |
||
| 650 | $ctoolintroRepo = Container::getToolIntroRepository(); |
||
| 651 | |||
| 652 | /** @var CToolIntro $ctoolintro */ |
||
| 653 | $ctoolintro = $ctoolintroRepo->find($ctoolintroId); |
||
| 654 | $ctoolintro->setCourseTool($courseTool); |
||
| 655 | $ctoolintroRepo->update($ctoolintro); |
||
| 656 | } |
||
| 657 | } |
||
| 658 | $responseData = []; |
||
| 659 | $json = $this->serializer->serialize( |
||
| 660 | $responseData, |
||
| 661 | 'json', |
||
| 662 | [ |
||
| 663 | 'groups' => ['course:read', 'ctool:read', 'tool:read', 'cshortcut:read'], |
||
| 664 | ] |
||
| 665 | ); |
||
| 666 | |||
| 667 | return new JsonResponse($responseData); |
||
| 668 | } |
||
| 669 | |||
| 670 | #[Route('/check-enrollments', name: 'chamilo_core_check_enrollments', methods: ['GET'])] |
||
| 671 | public function checkEnrollments(EntityManagerInterface $em, SettingsManager $settingsManager): JsonResponse |
||
| 672 | { |
||
| 673 | $user = $this->userHelper->getCurrent(); |
||
| 674 | |||
| 675 | if (!$user) { |
||
| 676 | return new JsonResponse(['error' => 'User not found'], Response::HTTP_UNAUTHORIZED); |
||
| 677 | } |
||
| 678 | |||
| 679 | $isEnrolledInCourses = $this->isUserEnrolledInAnyCourse($user, $em); |
||
| 680 | $isEnrolledInSessions = $this->isUserEnrolledInAnySession($user, $em); |
||
| 681 | |||
| 682 | if (!$isEnrolledInCourses && !$isEnrolledInSessions) { |
||
| 683 | $defaultMenuEntry = $settingsManager->getSetting('platform.default_menu_entry_for_course_or_session'); |
||
| 684 | $isEnrolledInCourses = 'my_courses' === $defaultMenuEntry; |
||
| 685 | $isEnrolledInSessions = 'my_sessions' === $defaultMenuEntry; |
||
| 686 | } |
||
| 687 | |||
| 688 | return new JsonResponse([ |
||
| 689 | 'isEnrolledInCourses' => $isEnrolledInCourses, |
||
| 690 | 'isEnrolledInSessions' => $isEnrolledInSessions, |
||
| 691 | ]); |
||
| 692 | } |
||
| 693 | |||
| 694 | #[Route('/categories', name: 'chamilo_core_course_form_lists')] |
||
| 695 | public function getCategories( |
||
| 696 | SettingsManager $settingsManager, |
||
| 697 | AccessUrlHelper $accessUrlHelper, |
||
| 698 | CourseCategoryRepository $courseCategoriesRepo |
||
| 699 | ): JsonResponse { |
||
| 700 | $allowBaseCourseCategory = 'true' === $settingsManager->getSetting('course.allow_base_course_category'); |
||
| 701 | $accessUrlId = $accessUrlHelper->getCurrent()->getId(); |
||
| 702 | |||
| 703 | $categories = $courseCategoriesRepo->findAllInAccessUrl( |
||
| 704 | $accessUrlId, |
||
| 705 | $allowBaseCourseCategory |
||
| 706 | ); |
||
| 707 | |||
| 708 | $data = []; |
||
| 709 | $categoryToAvoid = ''; |
||
| 710 | if (!$this->isGranted('ROLE_ADMIN')) { |
||
| 711 | $categoryToAvoid = $settingsManager->getSetting('course.course_category_code_to_use_as_model'); |
||
| 712 | } |
||
| 713 | |||
| 714 | foreach ($categories as $category) { |
||
| 715 | $categoryCode = $category->getCode(); |
||
| 716 | if (!empty($categoryToAvoid) && $categoryToAvoid == $categoryCode) { |
||
| 717 | continue; |
||
| 718 | } |
||
| 719 | $data[] = ['id' => $category->getId(), 'name' => $category->__toString()]; |
||
| 720 | } |
||
| 721 | |||
| 722 | return new JsonResponse($data); |
||
| 723 | } |
||
| 724 | |||
| 725 | #[Route('/search_templates', name: 'chamilo_core_course_search_templates')] |
||
| 726 | public function searchCourseTemplates( |
||
| 727 | Request $request, |
||
| 728 | AccessUrlHelper $accessUrlHelper, |
||
| 729 | CourseRepository $courseRepository |
||
| 730 | ): JsonResponse { |
||
| 731 | $searchTerm = $request->query->get('search', ''); |
||
| 732 | $accessUrl = $accessUrlHelper->getCurrent(); |
||
| 733 | |||
| 734 | $user = $this->userHelper->getCurrent(); |
||
| 735 | |||
| 736 | $courseList = $courseRepository->getCoursesInfoByUser($user, $accessUrl, 1, $searchTerm); |
||
| 737 | $results = ['items' => []]; |
||
| 738 | foreach ($courseList as $course) { |
||
| 739 | $title = $course['title']; |
||
| 740 | $results['items'][] = [ |
||
| 741 | 'id' => $course['id'], |
||
| 742 | 'name' => $title.' ('.$course['code'].') ', |
||
| 743 | ]; |
||
| 744 | } |
||
| 745 | |||
| 746 | return new JsonResponse($results); |
||
| 747 | } |
||
| 748 | |||
| 749 | #[Route('/create', name: 'chamilo_core_course_create')] |
||
| 750 | public function createCourse( |
||
| 751 | Request $request, |
||
| 752 | TranslatorInterface $translator, |
||
| 753 | CourseService $courseService |
||
| 754 | ): JsonResponse { |
||
| 755 | $courseData = json_decode($request->getContent(), true); |
||
| 756 | |||
| 757 | $title = $courseData['name'] ?? null; |
||
| 758 | $wantedCode = $courseData['code'] ?? null; |
||
| 759 | $courseLanguage = $courseData['language']['id'] ?? null; |
||
| 760 | $categoryCode = $courseData['category'] ?? null; |
||
| 761 | $exemplaryContent = $courseData['fillDemoContent'] ?? false; |
||
| 762 | $template = $courseData['template'] ?? ''; |
||
| 763 | |||
| 764 | $params = [ |
||
| 765 | 'title' => $title, |
||
| 766 | 'wanted_code' => $wantedCode, |
||
| 767 | 'course_language' => $courseLanguage, |
||
| 768 | 'exemplary_content' => $exemplaryContent, |
||
| 769 | 'course_template' => $template, |
||
| 770 | ]; |
||
| 771 | |||
| 772 | if ($categoryCode) { |
||
| 773 | $params['course_categories'] = $categoryCode; |
||
| 774 | } |
||
| 775 | |||
| 776 | try { |
||
| 777 | $course = $courseService->createCourse($params); |
||
| 778 | if ($course) { |
||
| 779 | return new JsonResponse([ |
||
| 780 | 'success' => true, |
||
| 781 | 'message' => $translator->trans('Course created successfully.'), |
||
| 782 | 'courseId' => $course->getId(), |
||
| 783 | ]); |
||
| 784 | } |
||
| 785 | } catch (Exception $e) { |
||
| 786 | return new JsonResponse([ |
||
| 787 | 'success' => false, |
||
| 788 | 'message' => $translator->trans($e->getMessage()), |
||
| 789 | ], Response::HTTP_BAD_REQUEST); |
||
| 790 | } |
||
| 791 | |||
| 792 | return new JsonResponse(['success' => false, 'message' => $translator->trans('An error occurred while creating the course.')]); |
||
| 793 | } |
||
| 794 | |||
| 795 | private function autoLaunch(): void |
||
| 969 | ) |
||
| 970 | ); |
||
| 971 | } |
||
| 972 | } |
||
| 973 | |||
| 974 | // Implement the real logic to check course enrollment |
||
| 975 | private function isUserEnrolledInAnyCourse(User $user, EntityManagerInterface $em): bool |
||
| 976 | { |
||
| 977 | $enrollmentCount = $em |
||
| 978 | ->getRepository(CourseRelUser::class) |
||
| 979 | ->count(['user' => $user]) |
||
| 980 | ; |
||
| 981 | |||
| 982 | return $enrollmentCount > 0; |
||
| 983 | } |
||
| 984 | |||
| 985 | // Implement the real logic to check session enrollment |
||
| 986 | private function isUserEnrolledInAnySession(User $user, EntityManagerInterface $em): bool |
||
| 993 | } |
||
| 994 | } |
||
| 995 |