| Total Complexity | 104 |
| Total Lines | 822 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| 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 |
||
| 57 | #[Route('/course')] |
||
| 58 | class CourseController extends ToolBaseController |
||
| 59 | { |
||
| 60 | public function __construct( |
||
| 61 | private readonly EntityManagerInterface $em, |
||
| 62 | private readonly SerializerInterface $serializer |
||
| 63 | ) {} |
||
| 64 | |||
| 65 | #[Route('/{cid}/checkLegal.json', name: 'chamilo_core_course_check_legal_json')] |
||
| 126 | } |
||
| 127 | |||
| 128 | #[Route('/{cid}/home.json', name: 'chamilo_core_course_home_json')] |
||
| 129 | #[Entity('course', expr: 'repository.find(cid)')] |
||
| 130 | public function indexJson( |
||
| 131 | Request $request, |
||
| 132 | CToolRepository $toolRepository, |
||
| 133 | CShortcutRepository $shortcutRepository, |
||
| 134 | ToolChain $toolChain, |
||
| 135 | EntityManagerInterface $em, |
||
| 136 | ): Response { |
||
| 137 | $requestData = json_decode($request->getContent(), true); |
||
| 138 | // Sort behaviour |
||
| 139 | if (!empty($requestData) && isset($requestData['toolItem'])) { |
||
| 140 | $index = $requestData['index']; |
||
| 141 | $toolItem = $requestData['toolItem']; |
||
| 142 | $toolId = (int) $toolItem['iid']; |
||
| 143 | |||
| 144 | /** @var CTool $cTool */ |
||
| 145 | $cTool = $em->find(CTool::class, $toolId); |
||
| 146 | |||
| 147 | if ($cTool) { |
||
| 148 | $cTool->setPosition($index + 1); |
||
| 149 | $em->persist($cTool); |
||
| 150 | $em->flush(); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | $course = $this->getCourse(); |
||
| 155 | $sessionId = $this->getSessionId(); |
||
| 156 | $isInASession = $sessionId > 0; |
||
| 157 | |||
| 158 | if (null === $course) { |
||
| 159 | throw $this->createAccessDeniedException(); |
||
| 160 | } |
||
| 161 | |||
| 162 | if (empty($sessionId)) { |
||
| 163 | $this->denyAccessUnlessGranted(CourseVoter::VIEW, $course); |
||
| 164 | } |
||
| 165 | |||
| 166 | $sessionHandler = $request->getSession(); |
||
| 167 | |||
| 168 | $userId = 0; |
||
| 169 | |||
| 170 | /** @var ?User $user */ |
||
| 171 | $user = $this->getUser(); |
||
| 172 | if (null !== $user) { |
||
| 173 | $userId = $user->getId(); |
||
| 174 | } |
||
| 175 | |||
| 176 | $courseCode = $course->getCode(); |
||
| 177 | $courseId = $course->getId(); |
||
| 178 | |||
| 179 | if ($user && $user->hasRole('ROLE_INVITEE')) { |
||
| 180 | $isSubscribed = CourseManager::is_user_subscribed_in_course( |
||
| 181 | $userId, |
||
| 182 | $courseCode, |
||
| 183 | $isInASession, |
||
| 184 | $sessionId |
||
| 185 | ); |
||
| 186 | |||
| 187 | if (!$isSubscribed) { |
||
| 188 | throw $this->createAccessDeniedException(); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | $isSpecialCourse = CourseManager::isSpecialCourse($courseId); |
||
| 193 | |||
| 194 | if ($user && $isSpecialCourse && (isset($_GET['autoreg']) && 1 === (int) $_GET['autoreg']) |
||
| 195 | && CourseManager::subscribeUser($userId, $courseId, STUDENT) |
||
| 196 | ) { |
||
| 197 | $sessionHandler->set('is_allowed_in_course', true); |
||
| 198 | } |
||
| 199 | |||
| 200 | $logInfo = [ |
||
| 201 | 'tool' => 'course-main', |
||
| 202 | ]; |
||
| 203 | Event::registerLog($logInfo); |
||
| 204 | |||
| 205 | // Deleting the objects |
||
| 206 | $sessionHandler->remove('toolgroup'); |
||
| 207 | $sessionHandler->remove('_gid'); |
||
| 208 | $sessionHandler->remove('oLP'); |
||
| 209 | $sessionHandler->remove('lpobject'); |
||
| 210 | |||
| 211 | api_remove_in_gradebook(); |
||
| 212 | Exercise::cleanSessionVariables(); |
||
| 213 | |||
| 214 | $shortcuts = []; |
||
| 215 | if (null !== $user) { |
||
| 216 | $shortcutQuery = $shortcutRepository->getResources($course->getResourceNode()); |
||
| 217 | $shortcuts = $shortcutQuery->getQuery()->getResult(); |
||
| 218 | } |
||
| 219 | $responseData = [ |
||
| 220 | 'shortcuts' => $shortcuts, |
||
| 221 | 'diagram' => '', |
||
| 222 | ]; |
||
| 223 | |||
| 224 | $json = $this->serializer->serialize( |
||
| 225 | $responseData, |
||
| 226 | 'json', |
||
| 227 | [ |
||
| 228 | 'groups' => ['course:read', 'ctool:read', 'tool:read', 'cshortcut:read'], |
||
| 229 | ] |
||
| 230 | ); |
||
| 231 | |||
| 232 | return new Response( |
||
| 233 | $json, |
||
| 234 | Response::HTTP_OK, |
||
| 235 | [ |
||
| 236 | 'Content-type' => 'application/json', |
||
| 237 | ] |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Redirects the page to a tool, following the tools settings. |
||
| 243 | */ |
||
| 244 | #[Route('/{cid}/tool/{toolName}', name: 'chamilo_core_course_redirect_tool')] |
||
| 245 | public function redirectTool(string $toolName, CToolRepository $repo, ToolChain $toolChain): RedirectResponse |
||
| 246 | { |
||
| 247 | /** @var CTool|null $tool */ |
||
| 248 | $tool = $repo->findOneBy([ |
||
| 249 | 'name' => $toolName, |
||
| 250 | ]); |
||
| 251 | |||
| 252 | if (null === $tool) { |
||
| 253 | throw new NotFoundHttpException($this->trans('Tool not found')); |
||
| 254 | } |
||
| 255 | |||
| 256 | $tool = $toolChain->getToolFromName($tool->getTool()->getName()); |
||
| 257 | $link = $tool->getLink(); |
||
| 258 | |||
| 259 | if (null === $this->getCourse()) { |
||
| 260 | throw new NotFoundHttpException($this->trans('Course not found')); |
||
| 261 | } |
||
| 262 | |||
| 263 | if (strpos($link, 'nodeId')) { |
||
| 264 | $nodeId = (string) $this->getCourse()->getResourceNode()->getId(); |
||
| 265 | $link = str_replace(':nodeId', $nodeId, $link); |
||
| 266 | } |
||
| 267 | |||
| 268 | $url = $link.'?'.$this->getCourseUrlQuery(); |
||
| 269 | |||
| 270 | return $this->redirect($url); |
||
| 271 | } |
||
| 272 | |||
| 273 | /*public function redirectToShortCut(string $toolName, CToolRepository $repo, ToolChain $toolChain): RedirectResponse |
||
| 274 | { |
||
| 275 | $tool = $repo->findOneBy([ |
||
| 276 | 'name' => $toolName, |
||
| 277 | ]); |
||
| 278 | |||
| 279 | if (null === $tool) { |
||
| 280 | throw new NotFoundHttpException($this->trans('Tool not found')); |
||
| 281 | } |
||
| 282 | |||
| 283 | $tool = $toolChain->getToolFromName($tool->getTool()->getName()); |
||
| 284 | $link = $tool->getLink(); |
||
| 285 | |||
| 286 | if (strpos($link, 'nodeId')) { |
||
| 287 | $nodeId = (string) $this->getCourse()->getResourceNode()->getId(); |
||
| 288 | $link = str_replace(':nodeId', $nodeId, $link); |
||
| 289 | } |
||
| 290 | |||
| 291 | $url = $link.'?'.$this->getCourseUrlQuery(); |
||
| 292 | |||
| 293 | return $this->redirect($url); |
||
| 294 | }*/ |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Edit configuration with given namespace. |
||
| 298 | */ |
||
| 299 | #[Route('/{cid}/settings/{namespace}', name: 'chamilo_core_course_settings')] |
||
| 300 | #[Entity('course', expr: 'repository.find(cid)')] |
||
| 301 | public function updateSettings( |
||
| 302 | Request $request, |
||
| 303 | Course $course, |
||
| 304 | string $namespace, |
||
| 305 | SettingsCourseManager $manager, |
||
| 306 | SettingsFormFactory $formFactory |
||
| 307 | ): Response { |
||
| 308 | $this->denyAccessUnlessGranted(CourseVoter::VIEW, $course); |
||
| 309 | |||
| 310 | $schemaAlias = $manager->convertNameSpaceToService($namespace); |
||
| 311 | $settings = $manager->load($namespace); |
||
| 312 | |||
| 313 | $form = $formFactory->create($schemaAlias); |
||
| 314 | |||
| 315 | $form->setData($settings); |
||
| 316 | $form->handleRequest($request); |
||
| 317 | |||
| 318 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 319 | $messageType = 'success'; |
||
| 320 | |||
| 321 | try { |
||
| 322 | $manager->setCourse($course); |
||
| 323 | $manager->save($form->getData()); |
||
| 324 | $message = $this->trans('Update'); |
||
| 325 | } catch (ValidatorException $validatorException) { |
||
| 326 | $message = $this->trans($validatorException->getMessage()); |
||
| 327 | $messageType = 'error'; |
||
| 328 | } |
||
| 329 | $this->addFlash($messageType, $message); |
||
| 330 | |||
| 331 | if ($request->headers->has('referer')) { |
||
| 332 | return $this->redirect($request->headers->get('referer')); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | $schemas = $manager->getSchemas(); |
||
| 337 | |||
| 338 | return $this->render( |
||
| 339 | '@ChamiloCore/Course/settings.html.twig', |
||
| 340 | [ |
||
| 341 | 'course' => $course, |
||
| 342 | 'schemas' => $schemas, |
||
| 343 | 'settings' => $settings, |
||
| 344 | 'form' => $form->createView(), |
||
| 345 | ] |
||
| 346 | ); |
||
| 347 | } |
||
| 348 | |||
| 349 | #[Route('/{id}/about', name: 'chamilo_core_course_about')] |
||
| 350 | public function about( |
||
| 351 | Course $course, |
||
| 352 | IllustrationRepository $illustrationRepository, |
||
| 353 | CCourseDescriptionRepository $courseDescriptionRepository, |
||
| 354 | EntityManagerInterface $em |
||
| 355 | ): Response { |
||
| 356 | $courseId = $course->getId(); |
||
| 357 | |||
| 358 | /** @var ?User $user */ |
||
| 359 | $user = $this->getUser(); |
||
| 360 | |||
| 361 | $fieldsRepo = $em->getRepository(ExtraField::class); |
||
| 362 | |||
| 363 | /** @var TagRepository $tagRepo */ |
||
| 364 | $tagRepo = $em->getRepository(Tag::class); |
||
| 365 | |||
| 366 | $courseDescriptions = $courseDescriptionRepository->getResourcesByCourse($course)->getQuery()->getResult(); |
||
| 367 | |||
| 368 | $courseValues = new ExtraFieldValue('course'); |
||
| 369 | |||
| 370 | $urlCourse = api_get_path(WEB_PATH).sprintf('course/%s/about', $courseId); |
||
| 371 | $courseTeachers = $course->getTeachersSubscriptions(); |
||
| 372 | $teachersData = []; |
||
| 373 | |||
| 374 | foreach ($courseTeachers as $teacherSubscription) { |
||
| 375 | $teacher = $teacherSubscription->getUser(); |
||
| 376 | $userData = [ |
||
| 377 | 'complete_name' => UserManager::formatUserFullName($teacher), |
||
| 378 | 'image' => $illustrationRepository->getIllustrationUrl($teacher), |
||
| 379 | 'diploma' => $teacher->getDiplomas(), |
||
| 380 | 'openarea' => $teacher->getOpenarea(), |
||
| 381 | ]; |
||
| 382 | |||
| 383 | $teachersData[] = $userData; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** @var ExtraField $tagField */ |
||
| 387 | $tagField = $fieldsRepo->findOneBy([ |
||
| 388 | 'itemType' => ExtraField::COURSE_FIELD_TYPE, |
||
| 389 | 'variable' => 'tags', |
||
| 390 | ]); |
||
| 391 | |||
| 392 | $courseTags = []; |
||
| 393 | if (null !== $tagField) { |
||
| 394 | $courseTags = $tagRepo->getTagsByItem($tagField, $courseId); |
||
| 395 | } |
||
| 396 | |||
| 397 | $courseDescription = $courseObjectives = $courseTopics = $courseMethodology = ''; |
||
| 398 | $courseMaterial = $courseResources = $courseAssessment = ''; |
||
| 399 | $courseCustom = []; |
||
| 400 | foreach ($courseDescriptions as $descriptionTool) { |
||
| 401 | switch ($descriptionTool->getDescriptionType()) { |
||
| 402 | case CCourseDescription::TYPE_DESCRIPTION: |
||
| 403 | $courseDescription = $descriptionTool->getContent(); |
||
| 404 | |||
| 405 | break; |
||
| 406 | |||
| 407 | case CCourseDescription::TYPE_OBJECTIVES: |
||
| 408 | $courseObjectives = $descriptionTool; |
||
| 409 | |||
| 410 | break; |
||
| 411 | |||
| 412 | case CCourseDescription::TYPE_TOPICS: |
||
| 413 | $courseTopics = $descriptionTool; |
||
| 414 | |||
| 415 | break; |
||
| 416 | |||
| 417 | case CCourseDescription::TYPE_METHODOLOGY: |
||
| 418 | $courseMethodology = $descriptionTool; |
||
| 419 | |||
| 420 | break; |
||
| 421 | |||
| 422 | case CCourseDescription::TYPE_COURSE_MATERIAL: |
||
| 423 | $courseMaterial = $descriptionTool; |
||
| 424 | |||
| 425 | break; |
||
| 426 | |||
| 427 | case CCourseDescription::TYPE_RESOURCES: |
||
| 428 | $courseResources = $descriptionTool; |
||
| 429 | |||
| 430 | break; |
||
| 431 | |||
| 432 | case CCourseDescription::TYPE_ASSESSMENT: |
||
| 433 | $courseAssessment = $descriptionTool; |
||
| 434 | |||
| 435 | break; |
||
| 436 | |||
| 437 | case CCourseDescription::TYPE_CUSTOM: |
||
| 438 | $courseCustom[] = $descriptionTool; |
||
| 439 | |||
| 440 | break; |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | $topics = [ |
||
| 445 | 'objectives' => $courseObjectives, |
||
| 446 | 'topics' => $courseTopics, |
||
| 447 | 'methodology' => $courseMethodology, |
||
| 448 | 'material' => $courseMaterial, |
||
| 449 | 'resources' => $courseResources, |
||
| 450 | 'assessment' => $courseAssessment, |
||
| 451 | 'custom' => array_reverse($courseCustom), |
||
| 452 | ]; |
||
| 453 | |||
| 454 | $subscriptionUser = false; |
||
| 455 | |||
| 456 | if ($user) { |
||
| 457 | $subscriptionUser = CourseManager::is_user_subscribed_in_course($user->getId(), $course->getCode()); |
||
| 458 | } |
||
| 459 | |||
| 460 | /*$allowSubscribe = false; |
||
| 461 | if ($course->getSubscribe() || api_is_platform_admin()) { |
||
| 462 | $allowSubscribe = true; |
||
| 463 | } |
||
| 464 | $plugin = \BuyCoursesPlugin::create(); |
||
| 465 | $checker = $plugin->isEnabled(); |
||
| 466 | $courseIsPremium = null; |
||
| 467 | if ($checker) { |
||
| 468 | $courseIsPremium = $plugin->getItemByProduct( |
||
| 469 | $courseId, |
||
| 470 | \BuyCoursesPlugin::PRODUCT_TYPE_COURSE |
||
| 471 | ); |
||
| 472 | }*/ |
||
| 473 | |||
| 474 | $image = Container::getIllustrationRepository()->getIllustrationUrl($course, 'course_picture_medium'); |
||
| 475 | |||
| 476 | $params = [ |
||
| 477 | 'course' => $course, |
||
| 478 | 'description' => $courseDescription, |
||
| 479 | 'image' => $image, |
||
| 480 | 'syllabus' => $topics, |
||
| 481 | 'tags' => $courseTags, |
||
| 482 | 'teachers' => $teachersData, |
||
| 483 | 'extra_fields' => $courseValues->getAllValuesForAnItem( |
||
| 484 | $course->getId(), |
||
| 485 | null, |
||
| 486 | true |
||
| 487 | ), |
||
| 488 | 'subscription' => $subscriptionUser, |
||
| 489 | 'url' => '', |
||
| 490 | 'is_premium' => '', |
||
| 491 | 'token' => '', |
||
| 492 | ]; |
||
| 493 | |||
| 494 | $metaInfo = '<meta property="og:url" content="'.$urlCourse.'" />'; |
||
| 495 | $metaInfo .= '<meta property="og:type" content="website" />'; |
||
| 496 | $metaInfo .= '<meta property="og:title" content="'.$course->getTitle().'" />'; |
||
| 497 | $metaInfo .= '<meta property="og:description" content="'.strip_tags($courseDescription).'" />'; |
||
| 498 | $metaInfo .= '<meta property="og:image" content="'.$image.'" />'; |
||
| 499 | |||
| 500 | $htmlHeadXtra[] = $metaInfo; |
||
| 501 | $htmlHeadXtra[] = api_get_asset('readmore-js/readmore.js'); |
||
| 502 | |||
| 503 | return $this->render('@ChamiloCore/Course/about.html.twig', $params); |
||
| 504 | } |
||
| 505 | |||
| 506 | #[Route('/{id}/welcome', name: 'chamilo_core_course_welcome')] |
||
| 507 | public function welcome(Course $course): Response |
||
| 508 | { |
||
| 509 | return $this->render('@ChamiloCore/Course/welcome.html.twig', [ |
||
| 510 | 'course' => $course, |
||
| 511 | ]); |
||
| 512 | } |
||
| 513 | |||
| 514 | private function findIntroOfCourse(Course $course) |
||
| 515 | { |
||
| 516 | $qb = $this->em->createQueryBuilder(); |
||
| 517 | |||
| 518 | $query = $qb->select('ct') |
||
| 519 | ->from(CTool::class, 'ct') |
||
| 520 | ->where('ct.course = :c_id') |
||
| 521 | ->andWhere('ct.name = :name') |
||
| 522 | ->andWhere( |
||
| 523 | $qb->expr()->orX( |
||
| 524 | $qb->expr()->eq('ct.session', ':session_id'), |
||
| 525 | $qb->expr()->isNull('ct.session') |
||
| 526 | ) |
||
| 527 | ) |
||
| 528 | ->setParameters([ |
||
| 529 | 'c_id' => $course->getId(), |
||
| 530 | 'name' => 'course_homepage', |
||
| 531 | 'session_id' => 0, |
||
| 532 | ]) |
||
| 533 | ->getQuery() |
||
| 534 | ; |
||
| 535 | |||
| 536 | return $query->getOneOrNullResult(); |
||
| 537 | } |
||
| 538 | |||
| 539 | #[Route('/{id}/getToolIntro', name: 'chamilo_core_course_gettoolintro')] |
||
| 540 | public function getToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response |
||
| 541 | { |
||
| 542 | $sessionId = (int) $request->get('sid'); |
||
| 543 | |||
| 544 | // $session = $this->getSession(); |
||
| 545 | $responseData = []; |
||
| 546 | $ctoolRepo = $em->getRepository(CTool::class); |
||
| 547 | $sessionRepo = $em->getRepository(Session::class); |
||
| 548 | $createInSession = false; |
||
| 549 | |||
| 550 | $session = null; |
||
| 551 | |||
| 552 | if (!empty($sessionId)) { |
||
| 553 | $session = $sessionRepo->find($sessionId); |
||
| 554 | } |
||
| 555 | |||
| 556 | $ctool = $this->findIntroOfCourse($course); |
||
| 557 | |||
| 558 | if ($session) { |
||
| 559 | $ctoolSession = $ctoolRepo->findOneBy(['name' => 'course_homepage', 'course' => $course, 'session' => $session]); |
||
| 560 | |||
| 561 | if (!$ctoolSession) { |
||
| 562 | $createInSession = true; |
||
| 563 | } else { |
||
| 564 | $ctool = $ctoolSession; |
||
| 565 | } |
||
| 566 | } |
||
| 567 | |||
| 568 | if ($ctool) { |
||
| 569 | $ctoolintroRepo = $em->getRepository(CToolIntro::class); |
||
| 570 | |||
| 571 | /** @var CToolIntro $ctoolintro */ |
||
| 572 | $ctoolintro = $ctoolintroRepo->findOneBy(['courseTool' => $ctool]); |
||
| 573 | if ($ctoolintro) { |
||
| 574 | $responseData = [ |
||
| 575 | 'iid' => $ctoolintro->getIid(), |
||
| 576 | 'introText' => $ctoolintro->getIntroText(), |
||
| 577 | 'createInSession' => $createInSession, |
||
| 578 | 'cToolId' => $ctool->getIid(), |
||
| 579 | ]; |
||
| 580 | } |
||
| 581 | $responseData['c_tool'] = [ |
||
| 582 | 'iid' => $ctool->getIid(), |
||
| 583 | 'name' => $ctool->getName(), |
||
| 584 | ]; |
||
| 585 | } |
||
| 586 | |||
| 587 | return new JsonResponse($responseData); |
||
| 588 | } |
||
| 589 | |||
| 590 | #[Route('/{id}/addToolIntro', name: 'chamilo_core_course_addtoolintro')] |
||
| 591 | public function addToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response |
||
| 592 | { |
||
| 593 | $data = $request->getContent(); |
||
| 594 | $data = json_decode($data); |
||
| 595 | $ctoolintroId = $data->iid; |
||
| 596 | $sessionId = $data->sid ?? 0; |
||
| 597 | |||
| 598 | $sessionRepo = $em->getRepository(Session::class); |
||
| 599 | $session = null; |
||
| 600 | if (!empty($sessionId)) { |
||
| 601 | $session = $sessionRepo->find($sessionId); |
||
| 602 | } |
||
| 603 | |||
| 604 | $ctool = $em->getRepository(CTool::class); |
||
| 605 | $check = $ctool->findOneBy(['name' => 'course_homepage', 'course' => $course, 'session' => $session]); |
||
| 606 | if (!$check) { |
||
| 607 | $toolRepo = $em->getRepository(Tool::class); |
||
| 608 | $toolEntity = $toolRepo->findOneBy(['name' => 'course_homepage']); |
||
| 609 | $courseTool = (new CTool()) |
||
| 610 | ->setTool($toolEntity) |
||
| 611 | ->setName('course_homepage') |
||
| 612 | ->setCourse($course) |
||
| 613 | ->setPosition(1) |
||
| 614 | ->setVisibility(true) |
||
| 615 | ->setParent($course) |
||
| 616 | ->setCreator($course->getCreator()) |
||
| 617 | ->setSession($session) |
||
| 618 | ->addCourseLink($course) |
||
| 619 | ; |
||
| 620 | $em->persist($courseTool); |
||
| 621 | $em->flush(); |
||
| 622 | if ($courseTool && !empty($ctoolintroId)) { |
||
| 623 | $ctoolintroRepo = Container::getToolIntroRepository(); |
||
| 624 | |||
| 625 | /** @var CToolIntro $ctoolintro */ |
||
| 626 | $ctoolintro = $ctoolintroRepo->find($ctoolintroId); |
||
| 627 | $ctoolintro->setCourseTool($courseTool); |
||
| 628 | $ctoolintroRepo->update($ctoolintro); |
||
| 629 | } |
||
| 630 | } |
||
| 631 | $responseData = []; |
||
| 632 | $json = $this->serializer->serialize( |
||
| 633 | $responseData, |
||
| 634 | 'json', |
||
| 635 | [ |
||
| 636 | 'groups' => ['course:read', 'ctool:read', 'tool:read', 'cshortcut:read'], |
||
| 637 | ] |
||
| 638 | ); |
||
| 639 | |||
| 640 | return new JsonResponse($responseData); |
||
| 641 | } |
||
| 642 | |||
| 643 | #[Route('/check-enrollments', name: 'chamilo_core_check_enrollments', methods: ['GET'])] |
||
| 644 | public function checkEnrollments(EntityManagerInterface $em, SettingsManager $settingsManager): JsonResponse |
||
| 645 | { |
||
| 646 | /** @var User|null $user */ |
||
| 647 | $user = $this->getUser(); |
||
| 648 | |||
| 649 | if (!$user) { |
||
| 650 | return new JsonResponse(['error' => 'User not found'], Response::HTTP_UNAUTHORIZED); |
||
| 651 | } |
||
| 652 | |||
| 653 | $isEnrolledInCourses = $this->isUserEnrolledInAnyCourse($user, $em); |
||
| 654 | $isEnrolledInSessions = $this->isUserEnrolledInAnySession($user, $em); |
||
| 655 | |||
| 656 | if (!$isEnrolledInCourses && !$isEnrolledInSessions) { |
||
| 657 | $defaultMenuEntry = $settingsManager->getSetting('platform.default_menu_entry_for_course_or_session'); |
||
| 658 | $isEnrolledInCourses = 'my_courses' === $defaultMenuEntry; |
||
| 659 | $isEnrolledInSessions = 'my_sessions' === $defaultMenuEntry; |
||
| 660 | } |
||
| 661 | |||
| 662 | return new JsonResponse([ |
||
| 663 | 'isEnrolledInCourses' => $isEnrolledInCourses, |
||
| 664 | 'isEnrolledInSessions' => $isEnrolledInSessions, |
||
| 665 | ]); |
||
| 666 | } |
||
| 667 | |||
| 668 | private function autoLaunch(): void |
||
| 842 | ) |
||
| 843 | ); |
||
| 844 | } |
||
| 845 | } |
||
| 846 | |||
| 847 | private function generateToolUrl(AbstractTool $tool): string |
||
| 848 | { |
||
| 849 | $link = $tool->getLink(); |
||
| 850 | $course = $this->getCourse(); |
||
| 851 | |||
| 852 | if (strpos($link, 'nodeId')) { |
||
| 853 | $nodeId = (string) $course->getResourceNode()->getId(); |
||
| 854 | $link = str_replace(':nodeId', $nodeId, $link); |
||
| 855 | } |
||
| 856 | |||
| 857 | return $link.'?'.$this->getCourseUrlQuery(); |
||
| 858 | } |
||
| 859 | |||
| 860 | // Implement the real logic to check course enrollment |
||
| 861 | private function isUserEnrolledInAnyCourse(User $user, EntityManagerInterface $em): bool |
||
| 862 | { |
||
| 863 | $enrollmentCount = $em |
||
| 864 | ->getRepository(CourseRelUser::class) |
||
| 865 | ->count(['user' => $user]) |
||
| 866 | ; |
||
| 867 | |||
| 868 | return $enrollmentCount > 0; |
||
| 869 | } |
||
| 870 | |||
| 871 | // Implement the real logic to check session enrollment |
||
| 872 | private function isUserEnrolledInAnySession(User $user, EntityManagerInterface $em): bool |
||
| 879 | } |
||
| 880 | } |
||
| 881 |