| Total Complexity | 116 |
| Total Lines | 937 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 |
||
| 66 | #[Route('/course')] |
||
| 67 | class CourseController extends ToolBaseController |
||
| 68 | { |
||
| 69 | public function __construct( |
||
| 70 | private readonly EntityManagerInterface $em, |
||
| 71 | private readonly SerializerInterface $serializer, |
||
| 72 | private readonly UserHelper $userHelper, |
||
| 73 | ) {} |
||
| 74 | |||
| 75 | #[IsGranted('ROLE_USER')] |
||
| 76 | #[Route('/{cid}/checkLegal.json', name: 'chamilo_core_course_check_legal_json')] |
||
| 77 | public function checkTermsAndConditionJson( |
||
| 78 | Request $request, |
||
| 79 | LegalRepository $legalTermsRepo, |
||
| 80 | LanguageRepository $languageRepository, |
||
| 81 | ExtraFieldValuesRepository $extraFieldValuesRepository, |
||
| 82 | SettingsManager $settingsManager |
||
| 83 | ): Response { |
||
| 84 | $user = $this->userHelper->getCurrent(); |
||
| 85 | $course = $this->getCourse(); |
||
| 86 | $responseData = [ |
||
| 87 | 'redirect' => false, |
||
| 88 | 'url' => '#', |
||
| 89 | ]; |
||
| 90 | |||
| 91 | if ($user->hasRole('ROLE_STUDENT') |
||
| 92 | && 'true' === $settingsManager->getSetting('registration.allow_terms_conditions') |
||
| 93 | && 'course' === $settingsManager->getSetting('platform.load_term_conditions_section') |
||
| 94 | ) { |
||
| 95 | $termAndConditionStatus = false; |
||
| 96 | $extraValue = $extraFieldValuesRepository->findLegalAcceptByItemId($user->getId()); |
||
| 97 | if (!empty($extraValue['value'])) { |
||
| 98 | $result = $extraValue['value']; |
||
| 99 | $userConditions = explode(':', $result); |
||
| 100 | $version = $userConditions[0]; |
||
| 101 | $langId = (int) $userConditions[1]; |
||
| 102 | $realVersion = $legalTermsRepo->getLastVersion($langId); |
||
| 103 | $termAndConditionStatus = ($version >= $realVersion); |
||
| 104 | } |
||
| 105 | |||
| 106 | if (false === $termAndConditionStatus) { |
||
| 107 | $request->getSession()->set('term_and_condition', ['user_id' => $user->getId()]); |
||
| 108 | |||
| 109 | $redirect = true; |
||
| 110 | |||
| 111 | if ('true' === $settingsManager->getSetting('course.allow_public_course_with_no_terms_conditions') |
||
| 112 | && Course::OPEN_WORLD === $course->getVisibility() |
||
| 113 | ) { |
||
| 114 | $redirect = false; |
||
| 115 | } |
||
| 116 | |||
| 117 | if ($redirect && !$this->isGranted('ROLE_ADMIN')) { |
||
| 118 | $responseData = [ |
||
| 119 | 'redirect' => true, |
||
| 120 | 'url' => '/main/auth/inscription.php', |
||
| 121 | ]; |
||
| 122 | } |
||
| 123 | } else { |
||
| 124 | $request->getSession()->remove('term_and_condition'); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | return new JsonResponse($responseData); |
||
| 129 | } |
||
| 130 | |||
| 131 | #[Route('/{cid}/home.json', name: 'chamilo_core_course_home_json')] |
||
| 132 | public function indexJson( |
||
| 133 | Request $request, |
||
| 134 | CShortcutRepository $shortcutRepository, |
||
| 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 | $user = $this->userHelper->getCurrent(); |
||
| 171 | if (null !== $user) { |
||
| 172 | $userId = $user->getId(); |
||
| 173 | } |
||
| 174 | |||
| 175 | $courseCode = $course->getCode(); |
||
| 176 | $courseId = $course->getId(); |
||
| 177 | |||
| 178 | if ($user && $user->hasRole('ROLE_INVITEE')) { |
||
| 179 | $isSubscribed = CourseManager::is_user_subscribed_in_course( |
||
| 180 | $userId, |
||
| 181 | $courseCode, |
||
| 182 | $isInASession, |
||
| 183 | $sessionId |
||
| 184 | ); |
||
| 185 | |||
| 186 | if (!$isSubscribed) { |
||
| 187 | throw $this->createAccessDeniedException(); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | $isSpecialCourse = CourseManager::isSpecialCourse($courseId); |
||
| 192 | |||
| 193 | if ($user && $isSpecialCourse && (isset($_GET['autoreg']) && 1 === (int) $_GET['autoreg']) |
||
| 194 | && CourseManager::subscribeUser($userId, $courseId, STUDENT) |
||
| 195 | ) { |
||
| 196 | $sessionHandler->set('is_allowed_in_course', true); |
||
| 197 | } |
||
| 198 | |||
| 199 | $logInfo = [ |
||
| 200 | 'tool' => 'course-main', |
||
| 201 | ]; |
||
| 202 | Event::registerLog($logInfo); |
||
| 203 | |||
| 204 | // Deleting the objects |
||
| 205 | $sessionHandler->remove('toolgroup'); |
||
| 206 | $sessionHandler->remove('_gid'); |
||
| 207 | $sessionHandler->remove('oLP'); |
||
| 208 | $sessionHandler->remove('lpobject'); |
||
| 209 | |||
| 210 | api_remove_in_gradebook(); |
||
| 211 | Exercise::cleanSessionVariables(); |
||
| 212 | |||
| 213 | $shortcuts = []; |
||
| 214 | if (null !== $user) { |
||
| 215 | $shortcutQuery = $shortcutRepository->getResources($course->getResourceNode()); |
||
| 216 | $shortcuts = $shortcutQuery->getQuery()->getResult(); |
||
| 217 | } |
||
| 218 | $responseData = [ |
||
| 219 | 'shortcuts' => $shortcuts, |
||
| 220 | 'diagram' => '', |
||
| 221 | ]; |
||
| 222 | |||
| 223 | $json = $this->serializer->serialize( |
||
| 224 | $responseData, |
||
| 225 | 'json', |
||
| 226 | [ |
||
| 227 | 'groups' => ['course:read', 'ctool:read', 'tool:read', 'cshortcut:read'], |
||
| 228 | ] |
||
| 229 | ); |
||
| 230 | |||
| 231 | return new Response( |
||
| 232 | $json, |
||
| 233 | Response::HTTP_OK, |
||
| 234 | [ |
||
| 235 | 'Content-type' => 'application/json', |
||
| 236 | ] |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Redirects the page to a tool, following the tools settings. |
||
| 242 | */ |
||
| 243 | #[Route('/{cid}/tool/{toolName}', name: 'chamilo_core_course_redirect_tool')] |
||
| 244 | public function redirectTool( |
||
| 245 | Request $request, |
||
| 246 | string $toolName, |
||
| 247 | CToolRepository $repo, |
||
| 248 | ToolChain $toolChain |
||
| 249 | ): RedirectResponse { |
||
| 250 | /** @var CTool|null $tool */ |
||
| 251 | $tool = $repo->findOneBy([ |
||
| 252 | 'title' => $toolName, |
||
| 253 | ]); |
||
| 254 | |||
| 255 | if (null === $tool) { |
||
| 256 | throw new NotFoundHttpException($this->trans('Tool not found')); |
||
| 257 | } |
||
| 258 | |||
| 259 | $tool = $toolChain->getToolFromName($tool->getTool()->getTitle()); |
||
| 260 | $link = $tool->getLink(); |
||
| 261 | |||
| 262 | if (null === $this->getCourse()) { |
||
| 263 | throw new NotFoundHttpException($this->trans('Course not found')); |
||
| 264 | } |
||
| 265 | $optionalParams = ''; |
||
| 266 | |||
| 267 | $optionalParams = $request->query->get('cert') ? '&cert='.$request->query->get('cert') : ''; |
||
| 268 | |||
| 269 | if (strpos($link, 'nodeId')) { |
||
| 270 | $nodeId = (string) $this->getCourse()->getResourceNode()->getId(); |
||
| 271 | $link = str_replace(':nodeId', $nodeId, $link); |
||
| 272 | } |
||
| 273 | |||
| 274 | $url = $link.'?'.$this->getCourseUrlQuery().$optionalParams; |
||
| 275 | |||
| 276 | return $this->redirect($url); |
||
| 277 | } |
||
| 278 | |||
| 279 | /*public function redirectToShortCut(string $toolName, CToolRepository $repo, ToolChain $toolChain): RedirectResponse |
||
| 280 | * { |
||
| 281 | * $tool = $repo->findOneBy([ |
||
| 282 | * 'name' => $toolName, |
||
| 283 | * ]); |
||
| 284 | * if (null === $tool) { |
||
| 285 | * throw new NotFoundHttpException($this->trans('Tool not found')); |
||
| 286 | * } |
||
| 287 | * $tool = $toolChain->getToolFromName($tool->getTool()->getTitle()); |
||
| 288 | * $link = $tool->getLink(); |
||
| 289 | * if (strpos($link, 'nodeId')) { |
||
| 290 | * $nodeId = (string) $this->getCourse()->getResourceNode()->getId(); |
||
| 291 | * $link = str_replace(':nodeId', $nodeId, $link); |
||
| 292 | * } |
||
| 293 | * $url = $link.'?'.$this->getCourseUrlQuery(); |
||
| 294 | * return $this->redirect($url); |
||
| 295 | * }*/ |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Edit configuration with given namespace. |
||
| 299 | */ |
||
| 300 | #[Route('/{course}/settings/{namespace}', name: 'chamilo_core_course_settings')] |
||
| 301 | public function updateSettings( |
||
| 302 | Request $request, |
||
| 303 | #[MapEntity(expr: 'repository.find(cid)')] |
||
| 304 | Course $course, |
||
| 305 | string $namespace, |
||
| 306 | SettingsCourseManager $manager, |
||
| 307 | SettingsFormFactory $formFactory |
||
| 308 | ): Response { |
||
| 309 | $this->denyAccessUnlessGranted(CourseVoter::VIEW, $course); |
||
| 310 | |||
| 311 | $schemaAlias = $manager->convertNameSpaceToService($namespace); |
||
| 312 | $settings = $manager->load($namespace); |
||
| 313 | |||
| 314 | $form = $formFactory->create($schemaAlias); |
||
| 315 | |||
| 316 | $form->setData($settings); |
||
| 317 | $form->handleRequest($request); |
||
| 318 | |||
| 319 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 320 | $messageType = 'success'; |
||
| 321 | |||
| 322 | try { |
||
| 323 | $manager->setCourse($course); |
||
| 324 | $manager->save($form->getData()); |
||
| 325 | $message = $this->trans('Update'); |
||
| 326 | } catch (ValidatorException $validatorException) { |
||
| 327 | $message = $this->trans($validatorException->getMessage()); |
||
| 328 | $messageType = 'error'; |
||
| 329 | } |
||
| 330 | $this->addFlash($messageType, $message); |
||
| 331 | |||
| 332 | if ($request->headers->has('referer')) { |
||
| 333 | return $this->redirect($request->headers->get('referer')); |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | $schemas = $manager->getSchemas(); |
||
| 338 | |||
| 339 | return $this->render( |
||
| 340 | '@ChamiloCore/Course/settings.html.twig', |
||
| 341 | [ |
||
| 342 | 'course' => $course, |
||
| 343 | 'schemas' => $schemas, |
||
| 344 | 'settings' => $settings, |
||
| 345 | 'form' => $form, |
||
| 346 | ] |
||
| 347 | ); |
||
| 348 | } |
||
| 349 | |||
| 350 | #[Route('/{id}/about', name: 'chamilo_core_course_about')] |
||
| 351 | public function about( |
||
| 352 | Course $course, |
||
| 353 | IllustrationRepository $illustrationRepository, |
||
| 354 | CCourseDescriptionRepository $courseDescriptionRepository, |
||
| 355 | EntityManagerInterface $em, |
||
| 356 | Request $request |
||
| 357 | ): Response { |
||
| 358 | $courseId = $course->getId(); |
||
| 359 | |||
| 360 | $user = $this->userHelper->getCurrent(); |
||
| 361 | |||
| 362 | $fieldsRepo = $em->getRepository(ExtraField::class); |
||
| 363 | |||
| 364 | /** @var TagRepository $tagRepo */ |
||
| 365 | $tagRepo = $em->getRepository(Tag::class); |
||
| 366 | |||
| 367 | $courseDescriptions = $courseDescriptionRepository->getResourcesByCourse($course)->getQuery()->getResult(); |
||
| 368 | |||
| 369 | $courseValues = new ExtraFieldValue('course'); |
||
| 370 | |||
| 371 | $urlCourse = api_get_path(WEB_PATH).\sprintf('course/%s/about', $courseId); |
||
| 372 | $courseTeachers = $course->getTeachersSubscriptions(); |
||
| 373 | $teachersData = []; |
||
| 374 | |||
| 375 | foreach ($courseTeachers as $teacherSubscription) { |
||
| 376 | $teacher = $teacherSubscription->getUser(); |
||
| 377 | $userData = [ |
||
| 378 | 'complete_name' => UserManager::formatUserFullName($teacher), |
||
| 379 | 'image' => $illustrationRepository->getIllustrationUrl($teacher), |
||
| 380 | 'diploma' => $teacher->getDiplomas(), |
||
| 381 | 'openarea' => $teacher->getOpenarea(), |
||
| 382 | ]; |
||
| 383 | |||
| 384 | $teachersData[] = $userData; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** @var ExtraField $tagField */ |
||
| 388 | $tagField = $fieldsRepo->findOneBy([ |
||
| 389 | 'itemType' => ExtraField::COURSE_FIELD_TYPE, |
||
| 390 | 'variable' => 'tags', |
||
| 391 | ]); |
||
| 392 | |||
| 393 | $courseTags = []; |
||
| 394 | if (null !== $tagField) { |
||
| 395 | $courseTags = $tagRepo->getTagsByItem($tagField, $courseId); |
||
| 396 | } |
||
| 397 | |||
| 398 | $courseDescription = $courseObjectives = $courseTopics = $courseMethodology = ''; |
||
| 399 | $courseMaterial = $courseResources = $courseAssessment = ''; |
||
| 400 | $courseCustom = []; |
||
| 401 | foreach ($courseDescriptions as $descriptionTool) { |
||
| 402 | switch ($descriptionTool->getDescriptionType()) { |
||
| 403 | case CCourseDescription::TYPE_DESCRIPTION: |
||
| 404 | $courseDescription = $descriptionTool->getContent(); |
||
| 405 | |||
| 406 | break; |
||
| 407 | |||
| 408 | case CCourseDescription::TYPE_OBJECTIVES: |
||
| 409 | $courseObjectives = $descriptionTool; |
||
| 410 | |||
| 411 | break; |
||
| 412 | |||
| 413 | case CCourseDescription::TYPE_TOPICS: |
||
| 414 | $courseTopics = $descriptionTool; |
||
| 415 | |||
| 416 | break; |
||
| 417 | |||
| 418 | case CCourseDescription::TYPE_METHODOLOGY: |
||
| 419 | $courseMethodology = $descriptionTool; |
||
| 420 | |||
| 421 | break; |
||
| 422 | |||
| 423 | case CCourseDescription::TYPE_COURSE_MATERIAL: |
||
| 424 | $courseMaterial = $descriptionTool; |
||
| 425 | |||
| 426 | break; |
||
| 427 | |||
| 428 | case CCourseDescription::TYPE_RESOURCES: |
||
| 429 | $courseResources = $descriptionTool; |
||
| 430 | |||
| 431 | break; |
||
| 432 | |||
| 433 | case CCourseDescription::TYPE_ASSESSMENT: |
||
| 434 | $courseAssessment = $descriptionTool; |
||
| 435 | |||
| 436 | break; |
||
| 437 | |||
| 438 | case CCourseDescription::TYPE_CUSTOM: |
||
| 439 | $courseCustom[] = $descriptionTool; |
||
| 440 | |||
| 441 | break; |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | $topics = [ |
||
| 446 | 'objectives' => $courseObjectives, |
||
| 447 | 'topics' => $courseTopics, |
||
| 448 | 'methodology' => $courseMethodology, |
||
| 449 | 'material' => $courseMaterial, |
||
| 450 | 'resources' => $courseResources, |
||
| 451 | 'assessment' => $courseAssessment, |
||
| 452 | 'custom' => array_reverse($courseCustom), |
||
| 453 | ]; |
||
| 454 | |||
| 455 | $subscriptionUser = false; |
||
| 456 | |||
| 457 | if ($user) { |
||
| 458 | $subscriptionUser = CourseManager::is_user_subscribed_in_course($user->getId(), $course->getCode()); |
||
| 459 | } |
||
| 460 | |||
| 461 | $allowSubscribe = CourseManager::canUserSubscribeToCourse($course->getCode()); |
||
| 462 | |||
| 463 | $image = Container::getIllustrationRepository()->getIllustrationUrl($course, 'course_picture_medium'); |
||
| 464 | |||
| 465 | $params = [ |
||
| 466 | 'course' => $course, |
||
| 467 | 'description' => $courseDescription, |
||
| 468 | 'image' => $image, |
||
| 469 | 'syllabus' => $topics, |
||
| 470 | 'tags' => $courseTags, |
||
| 471 | 'teachers' => $teachersData, |
||
| 472 | 'extra_fields' => $courseValues->getAllValuesForAnItem( |
||
| 473 | $course->getId(), |
||
| 474 | null, |
||
| 475 | true |
||
| 476 | ), |
||
| 477 | 'subscription' => $subscriptionUser, |
||
| 478 | 'url' => '', |
||
| 479 | 'is_premium' => '', |
||
| 480 | 'token' => '', |
||
| 481 | 'base_url' => $request->getSchemeAndHttpHost(), |
||
| 482 | 'allow_subscribe' => $allowSubscribe, |
||
| 483 | ]; |
||
| 484 | |||
| 485 | $metaInfo = '<meta property="og:url" content="'.$urlCourse.'" />'; |
||
| 486 | $metaInfo .= '<meta property="og:type" content="website" />'; |
||
| 487 | $metaInfo .= '<meta property="og:title" content="'.$course->getTitle().'" />'; |
||
| 488 | $metaInfo .= '<meta property="og:description" content="'.strip_tags($courseDescription).'" />'; |
||
| 489 | $metaInfo .= '<meta property="og:image" content="'.$image.'" />'; |
||
| 490 | |||
| 491 | $htmlHeadXtra[] = $metaInfo; |
||
| 492 | $htmlHeadXtra[] = api_get_asset('readmore-js/readmore.js'); |
||
| 493 | |||
| 494 | return $this->render('@ChamiloCore/Course/about.html.twig', $params); |
||
| 495 | } |
||
| 496 | |||
| 497 | #[Route('/{id}/welcome', name: 'chamilo_core_course_welcome')] |
||
| 498 | public function welcome(Course $course): Response |
||
| 499 | { |
||
| 500 | return $this->render('@ChamiloCore/Course/welcome.html.twig', [ |
||
| 501 | 'course' => $course, |
||
| 502 | ]); |
||
| 503 | } |
||
| 504 | |||
| 505 | private function findIntroOfCourse(Course $course): ?CTool |
||
| 506 | { |
||
| 507 | $qb = $this->em->createQueryBuilder(); |
||
| 508 | |||
| 509 | $query = $qb->select('ct') |
||
| 510 | ->from(CTool::class, 'ct') |
||
| 511 | ->where('ct.course = :c_id') |
||
| 512 | ->andWhere('ct.title = :title') |
||
| 513 | ->andWhere( |
||
| 514 | $qb->expr()->orX( |
||
| 515 | $qb->expr()->eq('ct.session', ':session_id'), |
||
| 516 | $qb->expr()->isNull('ct.session') |
||
| 517 | ) |
||
| 518 | ) |
||
| 519 | ->setParameters([ |
||
| 520 | 'c_id' => $course->getId(), |
||
| 521 | 'title' => 'course_homepage', |
||
| 522 | 'session_id' => 0, |
||
| 523 | ]) |
||
| 524 | ->getQuery() |
||
| 525 | ; |
||
| 526 | |||
| 527 | $results = $query->getResult(); |
||
| 528 | |||
| 529 | return \count($results) > 0 ? $results[0] : null; |
||
| 530 | } |
||
| 531 | |||
| 532 | #[Route('/{id}/getToolIntro', name: 'chamilo_core_course_gettoolintro')] |
||
| 533 | public function getToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response |
||
| 534 | { |
||
| 535 | $sessionId = (int) $request->get('sid'); |
||
| 536 | |||
| 537 | // $session = $this->getSession(); |
||
| 538 | $responseData = []; |
||
| 539 | $ctoolRepo = $em->getRepository(CTool::class); |
||
| 540 | $sessionRepo = $em->getRepository(Session::class); |
||
| 541 | $createInSession = false; |
||
| 542 | |||
| 543 | $session = null; |
||
| 544 | |||
| 545 | if (!empty($sessionId)) { |
||
| 546 | $session = $sessionRepo->find($sessionId); |
||
| 547 | } |
||
| 548 | |||
| 549 | $ctool = $this->findIntroOfCourse($course); |
||
| 550 | |||
| 551 | if ($session) { |
||
| 552 | $ctoolSession = $ctoolRepo->findOneBy(['title' => 'course_homepage', 'course' => $course, 'session' => $session]); |
||
| 553 | |||
| 554 | if (!$ctoolSession) { |
||
| 555 | $createInSession = true; |
||
| 556 | } else { |
||
| 557 | $ctool = $ctoolSession; |
||
| 558 | } |
||
| 559 | } |
||
| 560 | |||
| 561 | if ($ctool) { |
||
| 562 | $ctoolintroRepo = $em->getRepository(CToolIntro::class); |
||
| 563 | |||
| 564 | /** @var CToolIntro $ctoolintro */ |
||
| 565 | $ctoolintro = $ctoolintroRepo->findOneBy(['courseTool' => $ctool]); |
||
| 566 | if ($ctoolintro) { |
||
| 567 | $responseData = [ |
||
| 568 | 'iid' => $ctoolintro->getIid(), |
||
| 569 | 'introText' => $ctoolintro->getIntroText(), |
||
| 570 | 'createInSession' => $createInSession, |
||
| 571 | 'cToolId' => $ctool->getIid(), |
||
| 572 | ]; |
||
| 573 | } |
||
| 574 | $responseData['c_tool'] = [ |
||
| 575 | 'iid' => $ctool->getIid(), |
||
| 576 | 'title' => $ctool->getTitle(), |
||
| 577 | ]; |
||
| 578 | } |
||
| 579 | |||
| 580 | return new JsonResponse($responseData); |
||
| 581 | } |
||
| 582 | |||
| 583 | #[Route('/{id}/addToolIntro', name: 'chamilo_core_course_addtoolintro')] |
||
| 584 | public function addToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response |
||
| 585 | { |
||
| 586 | $data = $request->getContent(); |
||
| 587 | $data = json_decode($data); |
||
| 588 | $ctoolintroId = $data->iid; |
||
| 589 | $sessionId = $data->sid ?? 0; |
||
| 590 | |||
| 591 | $sessionRepo = $em->getRepository(Session::class); |
||
| 592 | $session = null; |
||
| 593 | if (!empty($sessionId)) { |
||
| 594 | $session = $sessionRepo->find($sessionId); |
||
| 595 | } |
||
| 596 | |||
| 597 | $ctool = $em->getRepository(CTool::class); |
||
| 598 | $check = $ctool->findOneBy(['title' => 'course_homepage', 'course' => $course, 'session' => $session]); |
||
| 599 | if (!$check) { |
||
| 600 | $toolRepo = $em->getRepository(Tool::class); |
||
| 601 | $toolEntity = $toolRepo->findOneBy(['title' => 'course_homepage']); |
||
| 602 | $courseTool = (new CTool()) |
||
| 603 | ->setTool($toolEntity) |
||
| 604 | ->setTitle('course_homepage') |
||
| 605 | ->setCourse($course) |
||
| 606 | ->setPosition(1) |
||
| 607 | ->setVisibility(true) |
||
| 608 | ->setParent($course) |
||
| 609 | ->setCreator($course->getCreator()) |
||
| 610 | ->setSession($session) |
||
| 611 | ->addCourseLink($course) |
||
| 612 | ; |
||
| 613 | $em->persist($courseTool); |
||
| 614 | $em->flush(); |
||
| 615 | if ($courseTool && !empty($ctoolintroId)) { |
||
| 616 | $ctoolintroRepo = Container::getToolIntroRepository(); |
||
| 617 | |||
| 618 | /** @var CToolIntro $ctoolintro */ |
||
| 619 | $ctoolintro = $ctoolintroRepo->find($ctoolintroId); |
||
| 620 | $ctoolintro->setCourseTool($courseTool); |
||
| 621 | $ctoolintroRepo->update($ctoolintro); |
||
| 622 | } |
||
| 623 | } |
||
| 624 | $responseData = []; |
||
| 625 | $json = $this->serializer->serialize( |
||
| 626 | $responseData, |
||
| 627 | 'json', |
||
| 628 | [ |
||
| 629 | 'groups' => ['course:read', 'ctool:read', 'tool:read', 'cshortcut:read'], |
||
| 630 | ] |
||
| 631 | ); |
||
| 632 | |||
| 633 | return new JsonResponse($responseData); |
||
| 634 | } |
||
| 635 | |||
| 636 | #[Route('/check-enrollments', name: 'chamilo_core_check_enrollments', methods: ['GET'])] |
||
| 637 | public function checkEnrollments(EntityManagerInterface $em, SettingsManager $settingsManager): JsonResponse |
||
| 638 | { |
||
| 639 | $user = $this->userHelper->getCurrent(); |
||
| 640 | |||
| 641 | if (!$user) { |
||
| 642 | return new JsonResponse(['error' => 'User not found'], Response::HTTP_UNAUTHORIZED); |
||
| 643 | } |
||
| 644 | |||
| 645 | $isEnrolledInCourses = $this->isUserEnrolledInAnyCourse($user, $em); |
||
| 646 | $isEnrolledInSessions = $this->isUserEnrolledInAnySession($user, $em); |
||
| 647 | |||
| 648 | if (!$isEnrolledInCourses && !$isEnrolledInSessions) { |
||
| 649 | $defaultMenuEntry = $settingsManager->getSetting('platform.default_menu_entry_for_course_or_session'); |
||
| 650 | $isEnrolledInCourses = 'my_courses' === $defaultMenuEntry; |
||
| 651 | $isEnrolledInSessions = 'my_sessions' === $defaultMenuEntry; |
||
| 652 | } |
||
| 653 | |||
| 654 | return new JsonResponse([ |
||
| 655 | 'isEnrolledInCourses' => $isEnrolledInCourses, |
||
| 656 | 'isEnrolledInSessions' => $isEnrolledInSessions, |
||
| 657 | ]); |
||
| 658 | } |
||
| 659 | |||
| 660 | #[Route('/categories', name: 'chamilo_core_course_form_lists')] |
||
| 661 | public function getCategories( |
||
| 662 | SettingsManager $settingsManager, |
||
| 663 | AccessUrlHelper $accessUrlHelper, |
||
| 664 | CourseCategoryRepository $courseCategoriesRepo |
||
| 665 | ): JsonResponse { |
||
| 666 | $allowBaseCourseCategory = 'true' === $settingsManager->getSetting('course.allow_base_course_category'); |
||
| 667 | $accessUrlId = $accessUrlHelper->getCurrent()->getId(); |
||
| 668 | |||
| 669 | $categories = $courseCategoriesRepo->findAllInAccessUrl( |
||
| 670 | $accessUrlId, |
||
| 671 | $allowBaseCourseCategory |
||
| 672 | ); |
||
| 673 | |||
| 674 | $data = []; |
||
| 675 | $categoryToAvoid = ''; |
||
| 676 | if (!$this->isGranted('ROLE_ADMIN')) { |
||
| 677 | $categoryToAvoid = $settingsManager->getSetting('course.course_category_code_to_use_as_model'); |
||
| 678 | } |
||
| 679 | |||
| 680 | foreach ($categories as $category) { |
||
| 681 | $categoryCode = $category->getCode(); |
||
| 682 | if (!empty($categoryToAvoid) && $categoryToAvoid == $categoryCode) { |
||
| 683 | continue; |
||
| 684 | } |
||
| 685 | $data[] = ['id' => $category->getId(), 'name' => $category->__toString()]; |
||
| 686 | } |
||
| 687 | |||
| 688 | return new JsonResponse($data); |
||
| 689 | } |
||
| 690 | |||
| 691 | #[Route('/search_templates', name: 'chamilo_core_course_search_templates')] |
||
| 692 | public function searchCourseTemplates( |
||
| 693 | Request $request, |
||
| 694 | AccessUrlHelper $accessUrlHelper, |
||
| 695 | CourseRepository $courseRepository |
||
| 696 | ): JsonResponse { |
||
| 697 | $searchTerm = $request->query->get('search', ''); |
||
| 698 | $accessUrl = $accessUrlHelper->getCurrent(); |
||
| 699 | |||
| 700 | $user = $this->userHelper->getCurrent(); |
||
| 701 | |||
| 702 | $courseList = $courseRepository->getCoursesInfoByUser($user, $accessUrl, 1, $searchTerm); |
||
| 703 | $results = ['items' => []]; |
||
| 704 | foreach ($courseList as $course) { |
||
| 705 | $title = $course['title']; |
||
| 706 | $results['items'][] = [ |
||
| 707 | 'id' => $course['id'], |
||
| 708 | 'name' => $title.' ('.$course['code'].') ', |
||
| 709 | ]; |
||
| 710 | } |
||
| 711 | |||
| 712 | return new JsonResponse($results); |
||
| 713 | } |
||
| 714 | |||
| 715 | #[Route('/create', name: 'chamilo_core_course_create')] |
||
| 716 | public function createCourse( |
||
| 717 | Request $request, |
||
| 718 | TranslatorInterface $translator, |
||
| 719 | CourseService $courseService |
||
| 720 | ): JsonResponse { |
||
| 721 | $courseData = json_decode($request->getContent(), true); |
||
| 722 | |||
| 723 | $title = $courseData['name'] ?? null; |
||
| 724 | $wantedCode = $courseData['code'] ?? null; |
||
| 725 | $courseLanguage = $courseData['language']['id'] ?? null; |
||
| 726 | $categoryCode = $courseData['category'] ?? null; |
||
| 727 | $exemplaryContent = $courseData['fillDemoContent'] ?? false; |
||
| 728 | $template = $courseData['template'] ?? ''; |
||
| 729 | |||
| 730 | $params = [ |
||
| 731 | 'title' => $title, |
||
| 732 | 'wanted_code' => $wantedCode, |
||
| 733 | 'course_language' => $courseLanguage, |
||
| 734 | 'exemplary_content' => $exemplaryContent, |
||
| 735 | 'course_template' => $template, |
||
| 736 | ]; |
||
| 737 | |||
| 738 | if ($categoryCode) { |
||
| 739 | $params['course_categories'] = $categoryCode; |
||
| 740 | } |
||
| 741 | |||
| 742 | try { |
||
| 743 | $course = $courseService->createCourse($params); |
||
| 744 | if ($course) { |
||
| 745 | return new JsonResponse([ |
||
| 746 | 'success' => true, |
||
| 747 | 'message' => $translator->trans('Course created successfully.'), |
||
| 748 | 'courseId' => $course->getId(), |
||
| 749 | ]); |
||
| 750 | } |
||
| 751 | } catch (Exception $e) { |
||
| 752 | return new JsonResponse([ |
||
| 753 | 'success' => false, |
||
| 754 | 'message' => $translator->trans($e->getMessage()), |
||
| 755 | ], Response::HTTP_BAD_REQUEST); |
||
| 756 | } |
||
| 757 | |||
| 758 | return new JsonResponse(['success' => false, 'message' => $translator->trans('An error occurred while creating the course.')]); |
||
| 759 | } |
||
| 760 | |||
| 761 | #[Route('/{id}/getAutoLaunchExerciseId', name: 'chamilo_core_course_get_auto_launch_exercise_id', methods: ['GET'])] |
||
| 762 | public function getAutoLaunchExerciseId( |
||
| 763 | Request $request, |
||
| 764 | Course $course, |
||
| 765 | CQuizRepository $quizRepository, |
||
| 766 | EntityManagerInterface $em |
||
| 767 | ): JsonResponse { |
||
| 768 | $data = $request->getContent(); |
||
| 769 | $data = json_decode($data); |
||
| 770 | $sessionId = $data->sid ?? 0; |
||
| 771 | |||
| 772 | $sessionRepo = $em->getRepository(Session::class); |
||
| 773 | $session = null; |
||
| 774 | if (!empty($sessionId)) { |
||
| 775 | $session = $sessionRepo->find($sessionId); |
||
| 776 | } |
||
| 777 | |||
| 778 | $autoLaunchExerciseId = $quizRepository->findAutoLaunchableQuizByCourseAndSession($course, $session); |
||
| 779 | |||
| 780 | return new JsonResponse(['exerciseId' => $autoLaunchExerciseId], Response::HTTP_OK); |
||
| 781 | } |
||
| 782 | |||
| 783 | #[Route('/{id}/getAutoLaunchLPId', name: 'chamilo_core_course_get_auto_launch_lp_id', methods: ['GET'])] |
||
| 784 | public function getAutoLaunchLPId( |
||
| 785 | Request $request, |
||
| 786 | Course $course, |
||
| 787 | CLPRepository $lpRepository, |
||
| 788 | EntityManagerInterface $em |
||
| 789 | ): JsonResponse { |
||
| 790 | $data = $request->getContent(); |
||
| 791 | $data = json_decode($data); |
||
| 792 | $sessionId = $data->sid ?? 0; |
||
| 793 | |||
| 794 | $sessionRepo = $em->getRepository(Session::class); |
||
| 795 | $session = null; |
||
| 796 | if (!empty($sessionId)) { |
||
| 797 | $session = $sessionRepo->find($sessionId); |
||
| 798 | } |
||
| 799 | |||
| 800 | $autoLaunchLPId = $lpRepository->findAutoLaunchableLPByCourseAndSession($course, $session); |
||
| 801 | |||
| 802 | return new JsonResponse(['lpId' => $autoLaunchLPId], Response::HTTP_OK); |
||
| 803 | } |
||
| 804 | |||
| 805 | private function autoLaunch(): void |
||
| 979 | ) |
||
| 980 | ); |
||
| 981 | } |
||
| 982 | } |
||
| 983 | |||
| 984 | // Implement the real logic to check course enrollment |
||
| 985 | private function isUserEnrolledInAnyCourse(User $user, EntityManagerInterface $em): bool |
||
| 986 | { |
||
| 987 | $enrollmentCount = $em |
||
| 988 | ->getRepository(CourseRelUser::class) |
||
| 989 | ->count(['user' => $user]) |
||
| 990 | ; |
||
| 991 | |||
| 992 | return $enrollmentCount > 0; |
||
| 993 | } |
||
| 994 | |||
| 995 | // Implement the real logic to check session enrollment |
||
| 996 | private function isUserEnrolledInAnySession(User $user, EntityManagerInterface $em): bool |
||
| 1003 | } |
||
| 1004 | } |
||
| 1005 |