| Total Complexity | 72 |
| Total Lines | 586 |
| 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 |
||
| 46 | #[Route('/course')] |
||
| 47 | class CourseController extends ToolBaseController |
||
| 48 | { |
||
| 49 | #[Route('/{cid}/home.json', name: 'chamilo_core_course_home_json')] |
||
| 50 | #[Entity('course', expr: 'repository.find(cid)')] |
||
| 51 | public function indexJson(Request $request, CToolRepository $toolRepository, CShortcutRepository $shortcutRepository, ToolChain $toolChain): Response |
||
| 52 | { |
||
| 53 | $course = $this->getCourse(); |
||
| 54 | $sessionId = $this->getSessionId(); |
||
| 55 | |||
| 56 | if (null === $course) { |
||
| 57 | throw $this->createAccessDeniedException(); |
||
| 58 | } |
||
| 59 | |||
| 60 | $this->denyAccessUnlessGranted(CourseVoter::VIEW, $course); |
||
| 61 | |||
| 62 | $session = $request->getSession(); |
||
| 63 | |||
| 64 | $userId = 0; |
||
| 65 | $user = $this->getUser(); |
||
| 66 | if (null !== $user) { |
||
| 67 | $userId = $user->getId(); |
||
| 68 | } |
||
| 69 | |||
| 70 | $courseCode = $course->getCode(); |
||
| 71 | $courseId = $course->getId(); |
||
| 72 | |||
| 73 | if ($user && $user->hasRole('ROLE_INVITEE')) { |
||
| 74 | $isInASession = $sessionId > 0; |
||
| 75 | $isSubscribed = CourseManager::is_user_subscribed_in_course( |
||
| 76 | $userId, |
||
| 77 | $courseCode, |
||
| 78 | $isInASession, |
||
| 79 | $sessionId |
||
| 80 | ); |
||
| 81 | |||
| 82 | if (!$isSubscribed) { |
||
| 83 | throw $this->createAccessDeniedException(); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | $isSpecialCourse = CourseManager::isSpecialCourse($courseId); |
||
| 88 | |||
| 89 | if ($user && $isSpecialCourse && (isset($_GET['autoreg']) && 1 === (int) $_GET['autoreg']) && |
||
| 90 | CourseManager::subscribeUser($userId, $courseId, STUDENT) |
||
| 91 | ) { |
||
| 92 | $session->set('is_allowed_in_course', true); |
||
| 93 | } |
||
| 94 | |||
| 95 | $logInfo = [ |
||
| 96 | 'tool' => 'course-main', |
||
| 97 | ]; |
||
| 98 | Event::registerLog($logInfo); |
||
| 99 | |||
| 100 | $qb = $toolRepository->getResourcesByCourse($course, $this->getSession()); |
||
| 101 | |||
| 102 | $qb->addSelect('tool'); |
||
| 103 | $qb->innerJoin('resource.tool', 'tool'); |
||
| 104 | |||
| 105 | $result = $qb->getQuery()->getResult(); |
||
| 106 | $tools = []; |
||
| 107 | $isCourseTeacher = $this->isGranted('ROLE_CURRENT_COURSE_TEACHER'); |
||
| 108 | |||
| 109 | $skipTools = ['course_tool', 'chat', 'notebook', 'wiki']; |
||
| 110 | |||
| 111 | /** @var CTool $item */ |
||
| 112 | foreach ($result as $item) { |
||
| 113 | if (\in_array($item->getName(), $skipTools, true)) { |
||
| 114 | continue; |
||
| 115 | } |
||
| 116 | $toolModel = $toolChain->getToolFromName($item->getTool()->getName()); |
||
| 117 | |||
| 118 | if (!$isCourseTeacher && 'admin' === $toolModel->getCategory()) { |
||
| 119 | continue; |
||
| 120 | } |
||
| 121 | |||
| 122 | $tools[$toolModel->getCategory()][] = [ |
||
| 123 | 'ctool' => $item, |
||
| 124 | 'tool' => $toolModel, |
||
| 125 | ]; |
||
| 126 | } |
||
| 127 | |||
| 128 | // Get session-career diagram |
||
| 129 | $diagram = ''; |
||
| 130 | /*$allow = api_get_configuration_value('allow_career_diagram'); |
||
| 131 | if (true === $allow) { |
||
| 132 | $htmlHeadXtra[] = api_get_js('jsplumb2.js'); |
||
| 133 | $extra = new ExtraFieldValue('session'); |
||
| 134 | $value = $extra->get_values_by_handler_and_field_variable( |
||
| 135 | api_get_session_id(), |
||
| 136 | 'external_career_id' |
||
| 137 | ); |
||
| 138 | |||
| 139 | if (!empty($value) && isset($value['value'])) { |
||
| 140 | $careerId = $value['value']; |
||
| 141 | $extraFieldValue = new ExtraFieldValue('career'); |
||
| 142 | $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value( |
||
| 143 | 'external_career_id', |
||
| 144 | $careerId, |
||
| 145 | false, |
||
| 146 | false, |
||
| 147 | false |
||
| 148 | ); |
||
| 149 | |||
| 150 | if (!empty($item) && isset($item['item_id'])) { |
||
| 151 | $careerId = $item['item_id']; |
||
| 152 | $career = new Career(); |
||
| 153 | $careerInfo = $career->get($careerId); |
||
| 154 | if (!empty($careerInfo)) { |
||
| 155 | $extraFieldValue = new ExtraFieldValue('career'); |
||
| 156 | $item = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 157 | $careerId, |
||
| 158 | 'career_diagram', |
||
| 159 | false, |
||
| 160 | false, |
||
| 161 | 0 |
||
| 162 | ); |
||
| 163 | |||
| 164 | if (!empty($item) && isset($item['value']) && !empty($item['value'])) { |
||
| 165 | // @var Graph $graph |
||
| 166 | $graph = UnserializeApi::unserialize('career', $item['value']); |
||
| 167 | $diagram = Career::renderDiagram($careerInfo, $graph); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | }*/ |
||
| 173 | |||
| 174 | // Deleting the objects |
||
| 175 | $session->remove('toolgroup'); |
||
| 176 | $session->remove('_gid'); |
||
| 177 | $session->remove('oLP'); |
||
| 178 | $session->remove('lpobject'); |
||
| 179 | |||
| 180 | api_remove_in_gradebook(); |
||
| 181 | Exercise::cleanSessionVariables(); |
||
| 182 | |||
| 183 | $shortcuts = []; |
||
| 184 | if (null !== $user) { |
||
| 185 | $shortcutQuery = $shortcutRepository->getResources($course->getResourceNode()); |
||
| 186 | $shortcuts = $shortcutQuery->getQuery()->getResult(); |
||
| 187 | } |
||
| 188 | $responseData = [ |
||
| 189 | 'course' => $course, |
||
| 190 | 'shortcuts' => $shortcuts, |
||
| 191 | 'diagram' => $diagram, |
||
| 192 | 'tools' => $tools, |
||
| 193 | ]; |
||
| 194 | |||
| 195 | $json = $this->get('serializer')->serialize( |
||
| 196 | $responseData, |
||
| 197 | 'json', |
||
| 198 | [ |
||
| 199 | 'groups' => ['course:read', 'ctool:read', 'tool:read', 'cshortcut:read'], |
||
| 200 | ] |
||
| 201 | ); |
||
| 202 | |||
| 203 | return new Response( |
||
| 204 | $json, |
||
| 205 | Response::HTTP_OK, |
||
| 206 | [ |
||
| 207 | 'Content-type' => 'application/json', |
||
| 208 | ] |
||
| 209 | ); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Redirects the page to a tool, following the tools settings. |
||
| 214 | */ |
||
| 215 | #[Route('/{cid}/tool/{toolName}', name: 'chamilo_core_course_redirect_tool')] |
||
| 216 | public function redirectTool(string $toolName, CToolRepository $repo, ToolChain $toolChain): RedirectResponse |
||
| 217 | { |
||
| 218 | /** @var CTool|null $tool */ |
||
| 219 | $tool = $repo->findOneBy([ |
||
| 220 | 'name' => $toolName, |
||
| 221 | ]); |
||
| 222 | |||
| 223 | if (null === $tool) { |
||
| 224 | throw new NotFoundHttpException($this->trans('Tool not found')); |
||
| 225 | } |
||
| 226 | |||
| 227 | $tool = $toolChain->getToolFromName($tool->getTool()->getName()); |
||
| 228 | $link = $tool->getLink(); |
||
| 229 | |||
| 230 | if (null === $this->getCourse()) { |
||
| 231 | throw new NotFoundHttpException($this->trans('Course not found')); |
||
| 232 | } |
||
| 233 | |||
| 234 | if (strpos($link, 'nodeId')) { |
||
| 235 | $nodeId = (string) $this->getCourse()->getResourceNode()->getId(); |
||
| 236 | $link = str_replace(':nodeId', $nodeId, $link); |
||
| 237 | } |
||
| 238 | |||
| 239 | $url = $link.'?'.$this->getCourseUrlQuery(); |
||
| 240 | |||
| 241 | return $this->redirect($url); |
||
| 242 | } |
||
| 243 | |||
| 244 | public function redirectToShortCut(string $toolName, CToolRepository $repo, ToolChain $toolChain): RedirectResponse |
||
| 245 | { |
||
| 246 | /** @var CTool|null $tool */ |
||
| 247 | $tool = $repo->findOneBy([ |
||
| 248 | 'name' => $toolName, |
||
| 249 | ]); |
||
| 250 | |||
| 251 | if (null === $tool) { |
||
| 252 | throw new NotFoundHttpException($this->trans('Tool not found')); |
||
| 253 | } |
||
| 254 | |||
| 255 | $tool = $toolChain->getToolFromName($tool->getTool()->getName()); |
||
| 256 | $link = $tool->getLink(); |
||
| 257 | |||
| 258 | if (strpos($link, 'nodeId')) { |
||
| 259 | $nodeId = (string) $this->getCourse()->getResourceNode()->getId(); |
||
| 260 | $link = str_replace(':nodeId', $nodeId, $link); |
||
| 261 | } |
||
| 262 | |||
| 263 | $url = $link.'?'.$this->getCourseUrlQuery(); |
||
| 264 | |||
| 265 | return $this->redirect($url); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Edit configuration with given namespace. |
||
| 270 | */ |
||
| 271 | #[Route('/{cid}/settings/{namespace}', name: 'chamilo_core_course_settings')] |
||
| 272 | #[Entity('course', expr: 'repository.find(cid)')] |
||
| 273 | public function updateSettings(Request $request, Course $course, string $namespace, SettingsCourseManager $manager, SettingsFormFactory $formFactory): Response |
||
| 274 | { |
||
| 275 | $this->denyAccessUnlessGranted(CourseVoter::VIEW, $course); |
||
| 276 | |||
| 277 | $schemaAlias = $manager->convertNameSpaceToService($namespace); |
||
| 278 | $settings = $manager->load($namespace); |
||
| 279 | |||
| 280 | $form = $formFactory->create($schemaAlias); |
||
| 281 | |||
| 282 | $form->setData($settings); |
||
| 283 | $form->handleRequest($request); |
||
| 284 | |||
| 285 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 286 | $messageType = 'success'; |
||
| 287 | |||
| 288 | try { |
||
| 289 | $manager->setCourse($course); |
||
| 290 | $manager->save($form->getData()); |
||
| 291 | $message = $this->trans('Update'); |
||
| 292 | } catch (ValidatorException $validatorException) { |
||
| 293 | $message = $this->trans($validatorException->getMessage()); |
||
| 294 | $messageType = 'error'; |
||
| 295 | } |
||
| 296 | $this->addFlash($messageType, $message); |
||
| 297 | |||
| 298 | if ($request->headers->has('referer')) { |
||
| 299 | return $this->redirect($request->headers->get('referer')); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | $schemas = $manager->getSchemas(); |
||
| 304 | |||
| 305 | return $this->render( |
||
| 306 | '@ChamiloCore/Course/settings.html.twig', |
||
| 307 | [ |
||
| 308 | 'course' => $course, |
||
| 309 | 'schemas' => $schemas, |
||
| 310 | 'settings' => $settings, |
||
| 311 | 'form' => $form->createView(), |
||
| 312 | ] |
||
| 313 | ); |
||
| 314 | } |
||
| 315 | |||
| 316 | #[Route('/{id}/about', name: 'chamilo_core_course_about')] |
||
| 317 | public function about(Course $course, IllustrationRepository $illustrationRepository, CCourseDescriptionRepository $courseDescriptionRepository): Response |
||
| 318 | { |
||
| 319 | $courseId = $course->getId(); |
||
| 320 | $userId = $this->getUser()->getId(); |
||
| 321 | $em = $this->getDoctrine()->getManager(); |
||
| 322 | |||
| 323 | /** @var EntityRepository $fieldsRepo */ |
||
| 324 | $fieldsRepo = $em->getRepository(ExtraField::class); |
||
| 325 | /** @var ExtraFieldRelTagRepository $fieldTagsRepo */ |
||
| 326 | $fieldTagsRepo = $em->getRepository(ExtraFieldRelTag::class); |
||
| 327 | |||
| 328 | $courseDescriptions = $courseDescriptionRepository->getResourcesByCourse($course)->getQuery()->getResult(); |
||
| 329 | |||
| 330 | $courseValues = new ExtraFieldValue('course'); |
||
| 331 | |||
| 332 | $urlCourse = api_get_path(WEB_PATH).sprintf('course/%s/about', $courseId); |
||
| 333 | $courseTeachers = $course->getTeachers(); |
||
| 334 | $teachersData = []; |
||
| 335 | |||
| 336 | foreach ($courseTeachers as $teacherSubscription) { |
||
| 337 | $teacher = $teacherSubscription->getUser(); |
||
| 338 | $userData = [ |
||
| 339 | 'complete_name' => UserManager::formatUserFullName($teacher), |
||
| 340 | 'image' => $illustrationRepository->getIllustrationUrl($teacher), |
||
| 341 | 'diploma' => $teacher->getDiplomas(), |
||
| 342 | 'openarea' => $teacher->getOpenarea(), |
||
| 343 | ]; |
||
| 344 | |||
| 345 | $teachersData[] = $userData; |
||
| 346 | } |
||
| 347 | /** @var ExtraField $tagField */ |
||
| 348 | $tagField = $fieldsRepo->findOneBy([ |
||
| 349 | 'extraFieldType' => ExtraField::COURSE_FIELD_TYPE, |
||
| 350 | 'variable' => 'tags', |
||
| 351 | ]); |
||
| 352 | |||
| 353 | $courseTags = []; |
||
| 354 | if (null !== $tagField) { |
||
| 355 | $courseTags = $fieldTagsRepo->getTags($tagField, $courseId); |
||
| 356 | } |
||
| 357 | |||
| 358 | $courseDescription = $courseObjectives = $courseTopics = $courseMethodology = ''; |
||
| 359 | $courseMaterial = $courseResources = $courseAssessment = ''; |
||
| 360 | $courseCustom = []; |
||
| 361 | foreach ($courseDescriptions as $descriptionTool) { |
||
| 362 | switch ($descriptionTool->getDescriptionType()) { |
||
| 363 | case CCourseDescription::TYPE_DESCRIPTION: |
||
| 364 | $courseDescription = $descriptionTool->getContent(); |
||
| 365 | |||
| 366 | break; |
||
| 367 | case CCourseDescription::TYPE_OBJECTIVES: |
||
| 368 | $courseObjectives = $descriptionTool; |
||
| 369 | |||
| 370 | break; |
||
| 371 | case CCourseDescription::TYPE_TOPICS: |
||
| 372 | $courseTopics = $descriptionTool; |
||
| 373 | |||
| 374 | break; |
||
| 375 | case CCourseDescription::TYPE_METHODOLOGY: |
||
| 376 | $courseMethodology = $descriptionTool; |
||
| 377 | |||
| 378 | break; |
||
| 379 | case CCourseDescription::TYPE_COURSE_MATERIAL: |
||
| 380 | $courseMaterial = $descriptionTool; |
||
| 381 | |||
| 382 | break; |
||
| 383 | case CCourseDescription::TYPE_RESOURCES: |
||
| 384 | $courseResources = $descriptionTool; |
||
| 385 | |||
| 386 | break; |
||
| 387 | case CCourseDescription::TYPE_ASSESSMENT: |
||
| 388 | $courseAssessment = $descriptionTool; |
||
| 389 | |||
| 390 | break; |
||
| 391 | case CCourseDescription::TYPE_CUSTOM: |
||
| 392 | $courseCustom[] = $descriptionTool; |
||
| 393 | |||
| 394 | break; |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | $topics = [ |
||
| 399 | 'objectives' => $courseObjectives, |
||
| 400 | 'topics' => $courseTopics, |
||
| 401 | 'methodology' => $courseMethodology, |
||
| 402 | 'material' => $courseMaterial, |
||
| 403 | 'resources' => $courseResources, |
||
| 404 | 'assessment' => $courseAssessment, |
||
| 405 | 'custom' => array_reverse($courseCustom), |
||
| 406 | ]; |
||
| 407 | |||
| 408 | $subscriptionUser = CourseManager::is_user_subscribed_in_course($userId, $course->getCode()); |
||
| 409 | |||
| 410 | /*$allowSubscribe = false; |
||
| 411 | if ($course->getSubscribe() || api_is_platform_admin()) { |
||
| 412 | $allowSubscribe = true; |
||
| 413 | } |
||
| 414 | $plugin = \BuyCoursesPlugin::create(); |
||
| 415 | $checker = $plugin->isEnabled(); |
||
| 416 | $courseIsPremium = null; |
||
| 417 | if ($checker) { |
||
| 418 | $courseIsPremium = $plugin->getItemByProduct( |
||
| 419 | $courseId, |
||
| 420 | \BuyCoursesPlugin::PRODUCT_TYPE_COURSE |
||
| 421 | ); |
||
| 422 | }*/ |
||
| 423 | |||
| 424 | $image = Container::getIllustrationRepository()->getIllustrationUrl($course, 'course_picture_medium'); |
||
| 425 | |||
| 426 | $params = [ |
||
| 427 | 'course' => $course, |
||
| 428 | 'description' => $courseDescription, |
||
| 429 | 'image' => $image, |
||
| 430 | 'syllabus' => $topics, |
||
| 431 | 'tags' => $courseTags, |
||
| 432 | 'teachers' => $teachersData, |
||
| 433 | 'extra_fields' => $courseValues->getAllValuesForAnItem( |
||
| 434 | $course->getId(), |
||
| 435 | null, |
||
| 436 | true |
||
| 437 | ), |
||
| 438 | 'subscription' => $subscriptionUser, |
||
| 439 | 'url' => '', |
||
| 440 | 'is_premium' => '', |
||
| 441 | 'token' => '', |
||
| 442 | ]; |
||
| 443 | |||
| 444 | $metaInfo = '<meta property="og:url" content="'.$urlCourse.'" />'; |
||
| 445 | $metaInfo .= '<meta property="og:type" content="website" />'; |
||
| 446 | $metaInfo .= '<meta property="og:title" content="'.$course->getTitle().'" />'; |
||
| 447 | $metaInfo .= '<meta property="og:description" content="'.strip_tags($courseDescription).'" />'; |
||
| 448 | $metaInfo .= '<meta property="og:image" content="'.$image.'" />'; |
||
| 449 | |||
| 450 | $htmlHeadXtra[] = $metaInfo; |
||
| 451 | $htmlHeadXtra[] = api_get_asset('readmore-js/readmore.js'); |
||
| 452 | |||
| 453 | return $this->render('@ChamiloCore/Course/about.html.twig', $params); |
||
| 454 | } |
||
| 455 | |||
| 456 | #[Route('/{id}/welcome', name: 'chamilo_core_course_welcome')] |
||
| 457 | public function welcome(Course $course): Response |
||
| 458 | { |
||
| 459 | return $this->render('@ChamiloCore/Course/welcome.html.twig', [ |
||
| 460 | 'course' => $course, |
||
| 461 | ]); |
||
| 462 | } |
||
| 463 | |||
| 464 | private function autoLaunch(): void |
||
| 632 | ) |
||
| 633 | ); |
||
| 634 | } |
||
| 635 | } |
||
| 636 | } |
||
| 637 |