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