| Total Complexity | 128 |
| Total Lines | 1029 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 74 | #[Route('/course')] |
||
| 75 | class CourseController extends ToolBaseController |
||
| 76 | { |
||
| 77 | public function __construct( |
||
| 82 | |||
| 83 | #[IsGranted('ROLE_USER')] |
||
| 84 | #[Route('/{cid}/checkLegal.json', name: 'chamilo_core_course_check_legal_json')] |
||
| 85 | public function checkTermsAndConditionJson( |
||
| 86 | Request $request, |
||
| 87 | LegalRepository $legalTermsRepo, |
||
| 88 | LanguageRepository $languageRepository, |
||
| 89 | ExtraFieldValuesRepository $extraFieldValuesRepository, |
||
| 90 | SettingsManager $settingsManager |
||
| 91 | ): Response { |
||
| 92 | $user = $this->userHelper->getCurrent(); |
||
| 93 | $course = $this->getCourse(); |
||
| 94 | $responseData = [ |
||
| 95 | 'redirect' => false, |
||
| 96 | 'url' => '#', |
||
| 97 | ]; |
||
| 98 | |||
| 99 | if ($user->hasRole('ROLE_STUDENT') |
||
| 100 | && 'true' === $settingsManager->getSetting('registration.allow_terms_conditions') |
||
| 101 | && 'course' === $settingsManager->getSetting('platform.load_term_conditions_section') |
||
| 102 | ) { |
||
| 103 | $termAndConditionStatus = false; |
||
| 104 | $extraValue = $extraFieldValuesRepository->findLegalAcceptByItemId($user->getId()); |
||
| 105 | if (!empty($extraValue['value'])) { |
||
| 106 | $result = $extraValue['value']; |
||
| 107 | $userConditions = explode(':', $result); |
||
| 108 | $version = $userConditions[0]; |
||
| 109 | $langId = (int) $userConditions[1]; |
||
| 110 | $realVersion = $legalTermsRepo->getLastVersion($langId); |
||
| 111 | $termAndConditionStatus = ($version >= $realVersion); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (false === $termAndConditionStatus) { |
||
| 115 | $request->getSession()->set('term_and_condition', ['user_id' => $user->getId()]); |
||
| 116 | |||
| 117 | $redirect = true; |
||
| 118 | |||
| 119 | if ('true' === $settingsManager->getSetting('course.allow_public_course_with_no_terms_conditions') |
||
| 120 | && Course::OPEN_WORLD === $course->getVisibility() |
||
| 121 | ) { |
||
| 122 | $redirect = false; |
||
| 123 | } |
||
| 124 | |||
| 125 | if ($redirect && !$this->isGranted('ROLE_ADMIN')) { |
||
| 126 | $responseData = [ |
||
| 127 | 'redirect' => true, |
||
| 128 | 'url' => '/main/auth/inscription.php', |
||
| 129 | ]; |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | $request->getSession()->remove('term_and_condition'); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | return new JsonResponse($responseData); |
||
| 137 | } |
||
| 138 | |||
| 139 | #[Route('/{cid}/home.json', name: 'chamilo_core_course_home_json')] |
||
| 261 | ] |
||
| 262 | ); |
||
| 263 | } |
||
| 264 | #[Route('/{courseId}/next-course', name: 'chamilo_course_next_course')] |
||
| 265 | public function getNextCourse( |
||
| 266 | int $courseId, |
||
| 267 | Request $request, |
||
| 268 | SequenceResourceRepository $repo, |
||
| 269 | Security $security, |
||
| 270 | SettingsManager $settingsManager, |
||
| 271 | EntityManagerInterface $em |
||
| 272 | ): JsonResponse { |
||
| 273 | $sessionId = $request->query->getInt('sid'); |
||
| 274 | $useDependents = $request->query->getBoolean('dependents', false); |
||
| 275 | $user = $security->getUser(); |
||
| 276 | $userId = $user->getId(); |
||
| 277 | |||
| 278 | if ($useDependents) { |
||
| 279 | $sequences = $repo->getDependents($courseId, SequenceResource::COURSE_TYPE); |
||
| 280 | $checked = $repo->checkDependentsForUser($sequences, SequenceResource::COURSE_TYPE, $userId, $sessionId); |
||
| 281 | $isUnlocked = $repo->checkSequenceAreCompleted($checked); |
||
| 282 | $sequenceResource = $repo->findRequirementForResource($courseId, SequenceResource::COURSE_TYPE); |
||
| 283 | } else { |
||
| 284 | $sequences = $repo->getRequirements($courseId, SequenceResource::COURSE_TYPE); |
||
| 285 | |||
| 286 | $hasValidRequirement = false; |
||
| 287 | foreach ($sequences as $sequence) { |
||
| 288 | foreach ($sequence['requirements'] ?? [] as $resource) { |
||
| 289 | if ($resource instanceof Course) { |
||
| 290 | $hasValidRequirement = true; |
||
| 291 | break 2; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | if (!$hasValidRequirement) { |
||
| 297 | return new JsonResponse([]); |
||
| 298 | } |
||
| 299 | |||
| 300 | $checked = $repo->checkRequirementsForUser($sequences, SequenceResource::COURSE_TYPE, $userId, $sessionId); |
||
| 301 | $isUnlocked = $repo->checkSequenceAreCompleted($checked); |
||
| 302 | $sequenceResource = $repo->findRequirementForResource($courseId, SequenceResource::COURSE_TYPE); |
||
| 303 | } |
||
| 304 | |||
| 305 | $graphImage = null; |
||
| 306 | |||
| 307 | if ($sequenceResource && $sequenceResource->hasGraph()) { |
||
| 308 | $graph = $sequenceResource->getSequence()->getUnSerializeGraph(); |
||
| 309 | if ($graph !== null) { |
||
| 310 | $graph->setAttribute('graphviz.node.fontname', 'arial'); |
||
| 311 | $graphviz = new GraphViz(); |
||
| 312 | $graphImage = $graphviz->createImageSrc($graph); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | return new JsonResponse([ |
||
| 317 | 'sequenceList' => array_values($checked), |
||
| 318 | 'allowSubscription' => $isUnlocked, |
||
| 319 | 'graph' => $graphImage, |
||
| 320 | ]); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Redirects the page to a tool, following the tools settings. |
||
| 325 | */ |
||
| 326 | #[Route('/{cid}/tool/{toolName}', name: 'chamilo_core_course_redirect_tool')] |
||
| 327 | public function redirectTool( |
||
| 328 | Request $request, |
||
| 329 | string $toolName, |
||
| 330 | CToolRepository $repo, |
||
| 331 | ToolChain $toolChain |
||
| 332 | ): RedirectResponse { |
||
| 333 | /** @var CTool|null $tool */ |
||
| 334 | $tool = $repo->findOneBy([ |
||
| 335 | 'title' => $toolName, |
||
| 336 | ]); |
||
| 337 | |||
| 338 | if (null === $tool) { |
||
| 339 | throw new NotFoundHttpException($this->trans('Tool not found')); |
||
| 340 | } |
||
| 341 | |||
| 342 | $tool = $toolChain->getToolFromName($tool->getTool()->getTitle()); |
||
| 343 | $link = $tool->getLink(); |
||
| 344 | |||
| 345 | if (null === $this->getCourse()) { |
||
| 346 | throw new NotFoundHttpException($this->trans('Course not found')); |
||
| 347 | } |
||
| 348 | $optionalParams = ''; |
||
| 349 | |||
| 350 | $optionalParams = $request->query->get('cert') ? '&cert='.$request->query->get('cert') : ''; |
||
| 351 | |||
| 352 | if (strpos($link, 'nodeId')) { |
||
| 353 | $nodeId = (string) $this->getCourse()->getResourceNode()->getId(); |
||
| 354 | $link = str_replace(':nodeId', $nodeId, $link); |
||
| 355 | } |
||
| 356 | |||
| 357 | $url = $link.'?'.$this->getCourseUrlQuery().$optionalParams; |
||
| 358 | |||
| 359 | return $this->redirect($url); |
||
| 360 | } |
||
| 361 | |||
| 362 | /*public function redirectToShortCut(string $toolName, CToolRepository $repo, ToolChain $toolChain): RedirectResponse |
||
| 363 | * { |
||
| 364 | * $tool = $repo->findOneBy([ |
||
| 365 | * 'name' => $toolName, |
||
| 366 | * ]); |
||
| 367 | * if (null === $tool) { |
||
| 368 | * throw new NotFoundHttpException($this->trans('Tool not found')); |
||
| 369 | * } |
||
| 370 | * $tool = $toolChain->getToolFromName($tool->getTool()->getTitle()); |
||
| 371 | * $link = $tool->getLink(); |
||
| 372 | * if (strpos($link, 'nodeId')) { |
||
| 373 | * $nodeId = (string) $this->getCourse()->getResourceNode()->getId(); |
||
| 374 | * $link = str_replace(':nodeId', $nodeId, $link); |
||
| 375 | * } |
||
| 376 | * $url = $link.'?'.$this->getCourseUrlQuery(); |
||
| 377 | * return $this->redirect($url); |
||
| 378 | * }*/ |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Edit configuration with given namespace. |
||
| 382 | */ |
||
| 383 | #[Route('/{course}/settings/{namespace}', name: 'chamilo_core_course_settings')] |
||
| 384 | public function updateSettings( |
||
| 385 | Request $request, |
||
| 386 | #[MapEntity(expr: 'repository.find(cid)')] |
||
| 387 | Course $course, |
||
| 388 | string $namespace, |
||
| 389 | SettingsCourseManager $manager, |
||
| 390 | SettingsFormFactory $formFactory |
||
| 391 | ): Response { |
||
| 392 | $this->denyAccessUnlessGranted(CourseVoter::VIEW, $course); |
||
| 393 | |||
| 394 | $schemaAlias = $manager->convertNameSpaceToService($namespace); |
||
| 395 | $settings = $manager->load($namespace); |
||
| 396 | |||
| 397 | $form = $formFactory->create($schemaAlias); |
||
| 398 | |||
| 399 | $form->setData($settings); |
||
| 400 | $form->handleRequest($request); |
||
| 401 | |||
| 402 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 403 | $messageType = 'success'; |
||
| 404 | |||
| 405 | try { |
||
| 406 | $manager->setCourse($course); |
||
| 407 | $manager->save($form->getData()); |
||
| 408 | $message = $this->trans('Update'); |
||
| 409 | } catch (ValidatorException $validatorException) { |
||
| 410 | $message = $this->trans($validatorException->getMessage()); |
||
| 411 | $messageType = 'error'; |
||
| 412 | } |
||
| 413 | $this->addFlash($messageType, $message); |
||
| 414 | |||
| 415 | if ($request->headers->has('referer')) { |
||
| 416 | return $this->redirect($request->headers->get('referer')); |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | $schemas = $manager->getSchemas(); |
||
| 421 | |||
| 422 | return $this->render( |
||
| 423 | '@ChamiloCore/Course/settings.html.twig', |
||
| 424 | [ |
||
| 425 | 'course' => $course, |
||
| 426 | 'schemas' => $schemas, |
||
| 427 | 'settings' => $settings, |
||
| 428 | 'form' => $form, |
||
| 429 | ] |
||
| 430 | ); |
||
| 431 | } |
||
| 432 | |||
| 433 | #[Route('/{id}/about', name: 'chamilo_core_course_about')] |
||
| 434 | public function about( |
||
| 435 | Course $course, |
||
| 436 | IllustrationRepository $illustrationRepository, |
||
| 437 | CCourseDescriptionRepository $courseDescriptionRepository, |
||
| 438 | EntityManagerInterface $em, |
||
| 439 | Request $request |
||
| 440 | ): Response { |
||
| 441 | $courseId = $course->getId(); |
||
| 442 | |||
| 443 | $user = $this->userHelper->getCurrent(); |
||
| 444 | |||
| 445 | $fieldsRepo = $em->getRepository(ExtraField::class); |
||
| 446 | |||
| 447 | /** @var TagRepository $tagRepo */ |
||
| 448 | $tagRepo = $em->getRepository(Tag::class); |
||
| 449 | |||
| 450 | $courseDescriptions = $courseDescriptionRepository->getResourcesByCourse($course)->getQuery()->getResult(); |
||
| 451 | |||
| 452 | $courseValues = new ExtraFieldValue('course'); |
||
| 453 | |||
| 454 | $urlCourse = api_get_path(WEB_PATH).\sprintf('course/%s/about', $courseId); |
||
| 455 | $courseTeachers = $course->getTeachersSubscriptions(); |
||
| 456 | $teachersData = []; |
||
| 457 | |||
| 458 | foreach ($courseTeachers as $teacherSubscription) { |
||
| 459 | $teacher = $teacherSubscription->getUser(); |
||
| 460 | $userData = [ |
||
| 461 | 'complete_name' => UserManager::formatUserFullName($teacher), |
||
| 462 | 'image' => $illustrationRepository->getIllustrationUrl($teacher), |
||
| 463 | 'diploma' => $teacher->getDiplomas(), |
||
| 464 | 'openarea' => $teacher->getOpenarea(), |
||
| 465 | ]; |
||
| 466 | |||
| 467 | $teachersData[] = $userData; |
||
| 468 | } |
||
| 469 | |||
| 470 | /** @var ExtraField $tagField */ |
||
| 471 | $tagField = $fieldsRepo->findOneBy([ |
||
| 472 | 'itemType' => ExtraField::COURSE_FIELD_TYPE, |
||
| 473 | 'variable' => 'tags', |
||
| 474 | ]); |
||
| 475 | |||
| 476 | $courseTags = []; |
||
| 477 | if (null !== $tagField) { |
||
| 478 | $courseTags = $tagRepo->getTagsByItem($tagField, $courseId); |
||
| 479 | } |
||
| 480 | |||
| 481 | $courseDescription = $courseObjectives = $courseTopics = $courseMethodology = ''; |
||
| 482 | $courseMaterial = $courseResources = $courseAssessment = ''; |
||
| 483 | $courseCustom = []; |
||
| 484 | foreach ($courseDescriptions as $descriptionTool) { |
||
| 485 | switch ($descriptionTool->getDescriptionType()) { |
||
| 486 | case CCourseDescription::TYPE_DESCRIPTION: |
||
| 487 | $courseDescription = $descriptionTool->getContent(); |
||
| 488 | |||
| 489 | break; |
||
| 490 | |||
| 491 | case CCourseDescription::TYPE_OBJECTIVES: |
||
| 492 | $courseObjectives = $descriptionTool; |
||
| 493 | |||
| 494 | break; |
||
| 495 | |||
| 496 | case CCourseDescription::TYPE_TOPICS: |
||
| 497 | $courseTopics = $descriptionTool; |
||
| 498 | |||
| 499 | break; |
||
| 500 | |||
| 501 | case CCourseDescription::TYPE_METHODOLOGY: |
||
| 502 | $courseMethodology = $descriptionTool; |
||
| 503 | |||
| 504 | break; |
||
| 505 | |||
| 506 | case CCourseDescription::TYPE_COURSE_MATERIAL: |
||
| 507 | $courseMaterial = $descriptionTool; |
||
| 508 | |||
| 509 | break; |
||
| 510 | |||
| 511 | case CCourseDescription::TYPE_RESOURCES: |
||
| 512 | $courseResources = $descriptionTool; |
||
| 513 | |||
| 514 | break; |
||
| 515 | |||
| 516 | case CCourseDescription::TYPE_ASSESSMENT: |
||
| 517 | $courseAssessment = $descriptionTool; |
||
| 518 | |||
| 519 | break; |
||
| 520 | |||
| 521 | case CCourseDescription::TYPE_CUSTOM: |
||
| 522 | $courseCustom[] = $descriptionTool; |
||
| 523 | |||
| 524 | break; |
||
| 525 | } |
||
| 526 | } |
||
| 527 | |||
| 528 | $topics = [ |
||
| 529 | 'objectives' => $courseObjectives, |
||
| 530 | 'topics' => $courseTopics, |
||
| 531 | 'methodology' => $courseMethodology, |
||
| 532 | 'material' => $courseMaterial, |
||
| 533 | 'resources' => $courseResources, |
||
| 534 | 'assessment' => $courseAssessment, |
||
| 535 | 'custom' => array_reverse($courseCustom), |
||
| 536 | ]; |
||
| 537 | |||
| 538 | $subscriptionUser = false; |
||
| 539 | |||
| 540 | if ($user) { |
||
| 541 | $subscriptionUser = CourseManager::is_user_subscribed_in_course($user->getId(), $course->getCode()); |
||
| 542 | } |
||
| 543 | |||
| 544 | $allowSubscribe = CourseManager::canUserSubscribeToCourse($course->getCode()); |
||
| 545 | |||
| 546 | $image = Container::getIllustrationRepository()->getIllustrationUrl($course, 'course_picture_medium'); |
||
| 547 | |||
| 548 | $params = [ |
||
| 549 | 'course' => $course, |
||
| 550 | 'description' => $courseDescription, |
||
| 551 | 'image' => $image, |
||
| 552 | 'syllabus' => $topics, |
||
| 553 | 'tags' => $courseTags, |
||
| 554 | 'teachers' => $teachersData, |
||
| 555 | 'extra_fields' => $courseValues->getAllValuesForAnItem( |
||
| 556 | $course->getId(), |
||
| 557 | null, |
||
| 558 | true |
||
| 559 | ), |
||
| 560 | 'subscription' => $subscriptionUser, |
||
| 561 | 'url' => '', |
||
| 562 | 'is_premium' => '', |
||
| 563 | 'token' => '', |
||
| 564 | 'base_url' => $request->getSchemeAndHttpHost(), |
||
| 565 | 'allow_subscribe' => $allowSubscribe, |
||
| 566 | ]; |
||
| 567 | |||
| 568 | $metaInfo = '<meta property="og:url" content="'.$urlCourse.'" />'; |
||
| 569 | $metaInfo .= '<meta property="og:type" content="website" />'; |
||
| 570 | $metaInfo .= '<meta property="og:title" content="'.$course->getTitle().'" />'; |
||
| 571 | $metaInfo .= '<meta property="og:description" content="'.strip_tags($courseDescription).'" />'; |
||
| 572 | $metaInfo .= '<meta property="og:image" content="'.$image.'" />'; |
||
| 573 | |||
| 574 | $htmlHeadXtra[] = $metaInfo; |
||
| 575 | $htmlHeadXtra[] = api_get_asset('readmore-js/readmore.js'); |
||
| 576 | |||
| 577 | return $this->render('@ChamiloCore/Course/about.html.twig', $params); |
||
| 578 | } |
||
| 579 | |||
| 580 | #[Route('/{id}/welcome', name: 'chamilo_core_course_welcome')] |
||
| 581 | public function welcome(Course $course): Response |
||
| 582 | { |
||
| 583 | return $this->render('@ChamiloCore/Course/welcome.html.twig', [ |
||
| 584 | 'course' => $course, |
||
| 585 | ]); |
||
| 586 | } |
||
| 587 | |||
| 588 | private function findIntroOfCourse(Course $course): ?CTool |
||
| 589 | { |
||
| 590 | $qb = $this->em->createQueryBuilder(); |
||
| 591 | |||
| 592 | $query = $qb->select('ct') |
||
| 593 | ->from(CTool::class, 'ct') |
||
| 594 | ->where('ct.course = :c_id') |
||
| 595 | ->andWhere('ct.title = :title') |
||
| 596 | ->andWhere( |
||
| 597 | $qb->expr()->orX( |
||
| 598 | $qb->expr()->eq('ct.session', ':session_id'), |
||
| 599 | $qb->expr()->isNull('ct.session') |
||
| 600 | ) |
||
| 601 | ) |
||
| 602 | ->setParameters([ |
||
| 603 | 'c_id' => $course->getId(), |
||
| 604 | 'title' => 'course_homepage', |
||
| 605 | 'session_id' => 0, |
||
| 606 | ]) |
||
| 607 | ->getQuery() |
||
| 608 | ; |
||
| 609 | |||
| 610 | $results = $query->getResult(); |
||
| 611 | |||
| 612 | return \count($results) > 0 ? $results[0] : null; |
||
| 613 | } |
||
| 614 | |||
| 615 | #[Route('/{id}/getToolIntro', name: 'chamilo_core_course_gettoolintro')] |
||
| 616 | public function getToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response |
||
| 617 | { |
||
| 618 | $sessionId = (int) $request->get('sid'); |
||
| 619 | |||
| 620 | // $session = $this->getSession(); |
||
| 621 | $responseData = []; |
||
| 622 | $ctoolRepo = $em->getRepository(CTool::class); |
||
| 623 | $sessionRepo = $em->getRepository(Session::class); |
||
| 624 | $createInSession = false; |
||
| 625 | |||
| 626 | $session = null; |
||
| 627 | |||
| 628 | if (!empty($sessionId)) { |
||
| 629 | $session = $sessionRepo->find($sessionId); |
||
| 630 | } |
||
| 631 | |||
| 632 | $ctool = $this->findIntroOfCourse($course); |
||
| 633 | |||
| 634 | if ($session) { |
||
| 635 | $ctoolSession = $ctoolRepo->findOneBy(['title' => 'course_homepage', 'course' => $course, 'session' => $session]); |
||
| 636 | |||
| 637 | if (!$ctoolSession) { |
||
| 638 | $createInSession = true; |
||
| 639 | } else { |
||
| 640 | $ctool = $ctoolSession; |
||
| 641 | } |
||
| 642 | } |
||
| 643 | |||
| 644 | if ($ctool) { |
||
| 645 | $ctoolintroRepo = $em->getRepository(CToolIntro::class); |
||
| 646 | |||
| 647 | /** @var CToolIntro $ctoolintro */ |
||
| 648 | $ctoolintro = $ctoolintroRepo->findOneBy(['courseTool' => $ctool]); |
||
| 649 | if ($ctoolintro) { |
||
| 650 | $responseData = [ |
||
| 651 | 'iid' => $ctoolintro->getIid(), |
||
| 652 | 'introText' => $ctoolintro->getIntroText(), |
||
| 653 | 'createInSession' => $createInSession, |
||
| 654 | 'cToolId' => $ctool->getIid(), |
||
| 655 | ]; |
||
| 656 | } |
||
| 657 | $responseData['c_tool'] = [ |
||
| 658 | 'iid' => $ctool->getIid(), |
||
| 659 | 'title' => $ctool->getTitle(), |
||
| 660 | ]; |
||
| 661 | } |
||
| 662 | |||
| 663 | return new JsonResponse($responseData); |
||
| 664 | } |
||
| 665 | |||
| 666 | #[Route('/{id}/addToolIntro', name: 'chamilo_core_course_addtoolintro')] |
||
| 667 | public function addToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response |
||
| 668 | { |
||
| 669 | $data = json_decode($request->getContent()); |
||
| 670 | $sessionId = $data->sid ?? ($data->resourceLinkList[0]->sid ?? 0); |
||
| 671 | $introText = $data->introText ?? null; |
||
| 672 | |||
| 673 | $session = $sessionId ? $em->getRepository(Session::class)->find($sessionId) : null; |
||
| 674 | $ctoolRepo = $em->getRepository(CTool::class); |
||
| 675 | $ctoolintroRepo = $em->getRepository(CToolIntro::class); |
||
| 676 | |||
| 677 | $ctoolSession = $ctoolRepo->findOneBy([ |
||
| 678 | 'title' => 'course_homepage', |
||
| 679 | 'course' => $course, |
||
| 680 | 'session' => $session, |
||
| 681 | ]); |
||
| 682 | |||
| 683 | if (!$ctoolSession) { |
||
| 684 | $toolEntity = $em->getRepository(Tool::class)->findOneBy(['title' => 'course_homepage']); |
||
| 685 | if ($toolEntity) { |
||
| 686 | $ctoolSession = (new CTool()) |
||
| 687 | ->setTool($toolEntity) |
||
| 688 | ->setTitle('course_homepage') |
||
| 689 | ->setCourse($course) |
||
| 690 | ->setPosition(1) |
||
| 691 | ->setVisibility(true) |
||
| 692 | ->setParent($course) |
||
| 693 | ->setCreator($course->getCreator()) |
||
| 694 | ->setSession($session) |
||
| 695 | ->addCourseLink($course) |
||
| 696 | ; |
||
| 697 | |||
| 698 | $em->persist($ctoolSession); |
||
| 699 | $em->flush(); |
||
| 700 | } |
||
| 701 | } |
||
| 702 | |||
| 703 | $ctoolIntro = $ctoolintroRepo->findOneBy(['courseTool' => $ctoolSession]); |
||
| 704 | if (!$ctoolIntro) { |
||
| 705 | $ctoolIntro = (new CToolIntro()) |
||
| 706 | ->setCourseTool($ctoolSession) |
||
| 707 | ->setIntroText($introText ?? '') |
||
| 708 | ->setParent($course) |
||
| 709 | ; |
||
| 710 | |||
| 711 | $em->persist($ctoolIntro); |
||
| 712 | $em->flush(); |
||
| 713 | |||
| 714 | return new JsonResponse([ |
||
| 715 | 'status' => 'created', |
||
| 716 | 'cToolId' => $ctoolSession->getIid(), |
||
| 717 | 'introIid' => $ctoolIntro->getIid(), |
||
| 718 | 'introText' => $ctoolIntro->getIntroText(), |
||
| 719 | ]); |
||
| 720 | } |
||
| 721 | |||
| 722 | if (null !== $introText) { |
||
| 723 | $ctoolIntro->setIntroText($introText); |
||
| 724 | $em->persist($ctoolIntro); |
||
| 725 | $em->flush(); |
||
| 726 | |||
| 727 | return new JsonResponse([ |
||
| 728 | 'status' => 'updated', |
||
| 729 | 'cToolId' => $ctoolSession->getIid(), |
||
| 730 | 'introIid' => $ctoolIntro->getIid(), |
||
| 731 | 'introText' => $ctoolIntro->getIntroText(), |
||
| 732 | ]); |
||
| 733 | } |
||
| 734 | |||
| 735 | return new JsonResponse(['status' => 'no_action']); |
||
| 736 | } |
||
| 737 | |||
| 738 | #[Route('/check-enrollments', name: 'chamilo_core_check_enrollments', methods: ['GET'])] |
||
| 739 | public function checkEnrollments(EntityManagerInterface $em, SettingsManager $settingsManager): JsonResponse |
||
| 740 | { |
||
| 741 | $user = $this->userHelper->getCurrent(); |
||
| 742 | |||
| 743 | if (!$user) { |
||
| 744 | return new JsonResponse(['error' => 'User not found'], Response::HTTP_UNAUTHORIZED); |
||
| 745 | } |
||
| 746 | |||
| 747 | $isEnrolledInCourses = $this->isUserEnrolledInAnyCourse($user, $em); |
||
| 748 | $isEnrolledInSessions = $this->isUserEnrolledInAnySession($user, $em); |
||
| 749 | |||
| 750 | if (!$isEnrolledInCourses && !$isEnrolledInSessions) { |
||
| 751 | $defaultMenuEntry = $settingsManager->getSetting('platform.default_menu_entry_for_course_or_session'); |
||
| 752 | $isEnrolledInCourses = 'my_courses' === $defaultMenuEntry; |
||
| 753 | $isEnrolledInSessions = 'my_sessions' === $defaultMenuEntry; |
||
| 754 | } |
||
| 755 | |||
| 756 | return new JsonResponse([ |
||
| 757 | 'isEnrolledInCourses' => $isEnrolledInCourses, |
||
| 758 | 'isEnrolledInSessions' => $isEnrolledInSessions, |
||
| 759 | ]); |
||
| 760 | } |
||
| 761 | |||
| 762 | #[Route('/categories', name: 'chamilo_core_course_form_lists')] |
||
| 763 | public function getCategories( |
||
| 764 | SettingsManager $settingsManager, |
||
| 765 | AccessUrlHelper $accessUrlHelper, |
||
| 766 | CourseCategoryRepository $courseCategoriesRepo |
||
| 767 | ): JsonResponse { |
||
| 768 | $allowBaseCourseCategory = 'true' === $settingsManager->getSetting('course.allow_base_course_category'); |
||
| 769 | $accessUrlId = $accessUrlHelper->getCurrent()->getId(); |
||
| 770 | |||
| 771 | $categories = $courseCategoriesRepo->findAllInAccessUrl( |
||
| 772 | $accessUrlId, |
||
| 773 | $allowBaseCourseCategory |
||
| 774 | ); |
||
| 775 | |||
| 776 | $data = []; |
||
| 777 | $categoryToAvoid = ''; |
||
| 778 | if (!$this->isGranted('ROLE_ADMIN')) { |
||
| 779 | $categoryToAvoid = $settingsManager->getSetting('course.course_category_code_to_use_as_model'); |
||
| 780 | } |
||
| 781 | |||
| 782 | foreach ($categories as $category) { |
||
| 783 | $categoryCode = $category->getCode(); |
||
| 784 | if (!empty($categoryToAvoid) && $categoryToAvoid == $categoryCode) { |
||
| 785 | continue; |
||
| 786 | } |
||
| 787 | $data[] = ['id' => $category->getId(), 'name' => $category->__toString()]; |
||
| 788 | } |
||
| 789 | |||
| 790 | return new JsonResponse($data); |
||
| 791 | } |
||
| 792 | |||
| 793 | #[Route('/search_templates', name: 'chamilo_core_course_search_templates')] |
||
| 794 | public function searchCourseTemplates( |
||
| 795 | Request $request, |
||
| 796 | AccessUrlHelper $accessUrlHelper, |
||
| 797 | CourseRepository $courseRepository |
||
| 798 | ): JsonResponse { |
||
| 799 | $searchTerm = $request->query->get('search', ''); |
||
| 800 | $accessUrl = $accessUrlHelper->getCurrent(); |
||
| 801 | |||
| 802 | $user = $this->userHelper->getCurrent(); |
||
| 803 | |||
| 804 | $courseList = $courseRepository->getCoursesInfoByUser($user, $accessUrl, 1, $searchTerm); |
||
| 805 | $results = ['items' => []]; |
||
| 806 | foreach ($courseList as $course) { |
||
| 807 | $title = $course['title']; |
||
| 808 | $results['items'][] = [ |
||
| 809 | 'id' => $course['id'], |
||
| 810 | 'name' => $title.' ('.$course['code'].') ', |
||
| 811 | ]; |
||
| 812 | } |
||
| 813 | |||
| 814 | return new JsonResponse($results); |
||
| 815 | } |
||
| 816 | |||
| 817 | #[Route('/create', name: 'chamilo_core_course_create')] |
||
| 818 | public function createCourse( |
||
| 819 | Request $request, |
||
| 820 | TranslatorInterface $translator, |
||
| 821 | CourseService $courseService |
||
| 822 | ): JsonResponse { |
||
| 823 | $courseData = json_decode($request->getContent(), true); |
||
| 824 | |||
| 825 | $title = $courseData['name'] ?? null; |
||
| 826 | $wantedCode = $courseData['code'] ?? null; |
||
| 827 | $courseLanguage = $courseData['language'] ?? null; |
||
| 828 | $categoryCode = $courseData['category'] ?? null; |
||
| 829 | $exemplaryContent = $courseData['fillDemoContent'] ?? false; |
||
| 830 | $template = $courseData['template'] ?? ''; |
||
| 831 | |||
| 832 | $params = [ |
||
| 833 | 'title' => $title, |
||
| 834 | 'wanted_code' => $wantedCode, |
||
| 835 | 'course_language' => $courseLanguage, |
||
| 836 | 'exemplary_content' => $exemplaryContent, |
||
| 837 | 'course_template' => $template, |
||
| 838 | ]; |
||
| 839 | |||
| 840 | if ($categoryCode) { |
||
| 841 | $params['course_categories'] = $categoryCode; |
||
| 842 | } |
||
| 843 | |||
| 844 | try { |
||
| 845 | $course = $courseService->createCourse($params); |
||
| 846 | if ($course) { |
||
| 847 | return new JsonResponse([ |
||
| 848 | 'success' => true, |
||
| 849 | 'message' => $translator->trans('Course created successfully.'), |
||
| 850 | 'courseId' => $course->getId(), |
||
| 851 | ]); |
||
| 852 | } |
||
| 853 | } catch (Exception $e) { |
||
| 854 | return new JsonResponse([ |
||
| 855 | 'success' => false, |
||
| 856 | 'message' => $translator->trans($e->getMessage()), |
||
| 857 | ], Response::HTTP_BAD_REQUEST); |
||
| 858 | } |
||
| 859 | |||
| 860 | return new JsonResponse(['success' => false, 'message' => $translator->trans('An error occurred while creating the course.')]); |
||
| 861 | } |
||
| 862 | |||
| 863 | #[Route('/{id}/getAutoLaunchExerciseId', name: 'chamilo_core_course_get_auto_launch_exercise_id', methods: ['GET'])] |
||
| 864 | public function getAutoLaunchExerciseId( |
||
| 865 | Request $request, |
||
| 866 | Course $course, |
||
| 867 | CQuizRepository $quizRepository, |
||
| 868 | EntityManagerInterface $em |
||
| 869 | ): JsonResponse { |
||
| 870 | $data = $request->getContent(); |
||
| 871 | $data = json_decode($data); |
||
| 872 | $sessionId = $data->sid ?? 0; |
||
| 873 | |||
| 874 | $sessionRepo = $em->getRepository(Session::class); |
||
| 875 | $session = null; |
||
| 876 | if (!empty($sessionId)) { |
||
| 877 | $session = $sessionRepo->find($sessionId); |
||
| 878 | } |
||
| 879 | |||
| 880 | $autoLaunchExerciseId = $quizRepository->findAutoLaunchableQuizByCourseAndSession($course, $session); |
||
| 881 | |||
| 882 | return new JsonResponse(['exerciseId' => $autoLaunchExerciseId], Response::HTTP_OK); |
||
| 883 | } |
||
| 884 | |||
| 885 | #[Route('/{id}/getAutoLaunchLPId', name: 'chamilo_core_course_get_auto_launch_lp_id', methods: ['GET'])] |
||
| 886 | public function getAutoLaunchLPId( |
||
| 887 | Request $request, |
||
| 888 | Course $course, |
||
| 889 | CLpRepository $lpRepository, |
||
| 890 | EntityManagerInterface $em |
||
| 891 | ): JsonResponse { |
||
| 892 | $data = $request->getContent(); |
||
| 893 | $data = json_decode($data); |
||
| 894 | $sessionId = $data->sid ?? 0; |
||
| 895 | |||
| 896 | $sessionRepo = $em->getRepository(Session::class); |
||
| 897 | $session = null; |
||
| 898 | if (!empty($sessionId)) { |
||
| 899 | $session = $sessionRepo->find($sessionId); |
||
| 900 | } |
||
| 901 | |||
| 902 | $autoLaunchLPId = $lpRepository->findAutoLaunchableLPByCourseAndSession($course, $session); |
||
| 903 | |||
| 904 | return new JsonResponse(['lpId' => $autoLaunchLPId], Response::HTTP_OK); |
||
| 905 | } |
||
| 906 | |||
| 907 | private function autoLaunch(): void |
||
| 1079 | ) |
||
| 1080 | ); |
||
| 1081 | } |
||
| 1082 | } |
||
| 1083 | |||
| 1084 | // Implement the real logic to check course enrollment |
||
| 1085 | private function isUserEnrolledInAnyCourse(User $user, EntityManagerInterface $em): bool |
||
| 1086 | { |
||
| 1087 | $enrollmentCount = $em |
||
| 1088 | ->getRepository(CourseRelUser::class) |
||
| 1089 | ->count(['user' => $user]) |
||
| 1090 | ; |
||
| 1091 | |||
| 1092 | return $enrollmentCount > 0; |
||
| 1093 | } |
||
| 1094 | |||
| 1095 | // Implement the real logic to check session enrollment |
||
| 1096 | private function isUserEnrolledInAnySession(User $user, EntityManagerInterface $em): bool |
||
| 1103 | } |
||
| 1104 | } |
||
| 1105 |