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