Total Complexity | 59 |
Total Lines | 467 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CourseHomeController 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 CourseHomeController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | #[Route('/course')] |
||
39 | class CourseHomeController extends ToolBaseController |
||
40 | { |
||
41 | /** |
||
42 | * @Route("/{cid}/home.json", name="chamilo_core_course_home_json") |
||
43 | * |
||
44 | * @Entity("course", expr="repository.find(cid)") |
||
45 | */ |
||
46 | public function indexJsonAction(Request $request, CToolRepository $toolRepository, CShortcutRepository $shortcutRepository, ToolChain $toolChain): Response |
||
47 | { |
||
48 | $course = $this->getCourse(); |
||
49 | |||
50 | if (null === $course) { |
||
51 | throw $this->createAccessDeniedException(); |
||
52 | } |
||
53 | |||
54 | $this->denyAccessUnlessGranted(CourseVoter::VIEW, $course); |
||
55 | |||
56 | $session = $request->getSession(); |
||
57 | |||
58 | $userId = 0; |
||
59 | $user = $this->getUser(); |
||
60 | if (null !== $user) { |
||
61 | $userId = $user->getId(); |
||
62 | } |
||
63 | |||
64 | $courseCode = $course->getCode(); |
||
65 | $courseId = $course->getId(); |
||
66 | $sessionId = $this->getSessionId(); |
||
67 | |||
68 | if ($user && $user->hasRole('ROLE_INVITEE')) { |
||
69 | $isInASession = $sessionId > 0; |
||
70 | $isSubscribed = CourseManager::is_user_subscribed_in_course( |
||
71 | $userId, |
||
72 | $courseCode, |
||
73 | $isInASession, |
||
74 | $sessionId |
||
75 | ); |
||
76 | |||
77 | if (!$isSubscribed) { |
||
78 | throw $this->createAccessDeniedException(); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | $isSpecialCourse = CourseManager::isSpecialCourse($courseId); |
||
83 | |||
84 | if ($user && $isSpecialCourse && (isset($_GET['autoreg']) && 1 === (int) $_GET['autoreg']) && |
||
85 | CourseManager::subscribeUser($userId, $courseId, STUDENT) |
||
86 | ) { |
||
87 | $session->set('is_allowed_in_course', true); |
||
88 | } |
||
89 | |||
90 | /*$action = empty($_GET['action']) ? '' : Security::remove_XSS($_GET['action']); |
||
91 | if ('subscribe' === $action && Security::check_token('get')) { |
||
92 | Security::clear_token(); |
||
93 | $result = CourseManager::autoSubscribeToCourse($courseCode); |
||
94 | if ($result && CourseManager::is_user_subscribed_in_course($userId, $courseCode)) { |
||
95 | $session->set('is_allowed_in_course', true); |
||
96 | } |
||
97 | header('Location: '.api_get_self()); |
||
98 | exit; |
||
99 | } |
||
100 | |||
101 | $logInfo = [ |
||
102 | 'tool' => 'course-main', |
||
103 | 'action' => $action, |
||
104 | ]; |
||
105 | Event::registerLog($logInfo);*/ |
||
106 | $logInfo = [ |
||
107 | 'tool' => 'course-main', |
||
108 | ]; |
||
109 | Event::registerLog($logInfo); |
||
110 | |||
111 | $qb = $toolRepository->getResourcesByCourse($course, $this->getSession()); |
||
112 | |||
113 | $qb->addSelect('tool'); |
||
114 | $qb->innerJoin('resource.tool', 'tool'); |
||
115 | |||
116 | $result = $qb->getQuery()->getResult(); |
||
117 | $tools = []; |
||
118 | $isCourseTeacher = $this->isGranted('ROLE_CURRENT_COURSE_TEACHER'); |
||
119 | |||
120 | /** @var CTool $item */ |
||
121 | foreach ($result as $item) { |
||
122 | if ('course_tool' === $item->getName()) { |
||
123 | continue; |
||
124 | } |
||
125 | $toolModel = $toolChain->getToolFromName($item->getTool()->getName()); |
||
126 | |||
127 | if (!$isCourseTeacher && 'admin' === $toolModel->getCategory()) { |
||
128 | continue; |
||
129 | } |
||
130 | |||
131 | $tools[$toolModel->getCategory()][] = [ |
||
132 | 'ctool' => $item, |
||
133 | 'tool' => $toolModel, |
||
134 | ]; |
||
135 | } |
||
136 | |||
137 | // Get session-career diagram |
||
138 | $diagram = ''; |
||
139 | /*$allow = api_get_configuration_value('allow_career_diagram'); |
||
140 | if (true === $allow) { |
||
141 | $htmlHeadXtra[] = api_get_js('jsplumb2.js'); |
||
142 | $extra = new ExtraFieldValue('session'); |
||
143 | $value = $extra->get_values_by_handler_and_field_variable( |
||
144 | api_get_session_id(), |
||
145 | 'external_career_id' |
||
146 | ); |
||
147 | |||
148 | if (!empty($value) && isset($value['value'])) { |
||
149 | $careerId = $value['value']; |
||
150 | $extraFieldValue = new ExtraFieldValue('career'); |
||
151 | $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value( |
||
152 | 'external_career_id', |
||
153 | $careerId, |
||
154 | false, |
||
155 | false, |
||
156 | false |
||
157 | ); |
||
158 | |||
159 | if (!empty($item) && isset($item['item_id'])) { |
||
160 | $careerId = $item['item_id']; |
||
161 | $career = new Career(); |
||
162 | $careerInfo = $career->get($careerId); |
||
163 | if (!empty($careerInfo)) { |
||
164 | $extraFieldValue = new ExtraFieldValue('career'); |
||
165 | $item = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
166 | $careerId, |
||
167 | 'career_diagram', |
||
168 | false, |
||
169 | false, |
||
170 | 0 |
||
171 | ); |
||
172 | |||
173 | if (!empty($item) && isset($item['value']) && !empty($item['value'])) { |
||
174 | // @var Graph $graph |
||
175 | $graph = UnserializeApi::unserialize('career', $item['value']); |
||
176 | $diagram = Career::renderDiagram($careerInfo, $graph); |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | }*/ |
||
182 | |||
183 | // Deleting the objects |
||
184 | $session->remove('toolgroup'); |
||
185 | $session->remove('_gid'); |
||
186 | $session->remove('oLP'); |
||
187 | $session->remove('lpobject'); |
||
188 | |||
189 | api_remove_in_gradebook(); |
||
190 | Exercise::cleanSessionVariables(); |
||
191 | |||
192 | $shortcuts = []; |
||
193 | if (null !== $user) { |
||
194 | $shortcutQuery = $shortcutRepository->getResources($course->getResourceNode()); |
||
195 | $shortcuts = $shortcutQuery->getQuery()->getResult(); |
||
196 | } |
||
197 | $responseData = [ |
||
198 | 'course' => $course, |
||
199 | 'shortcuts' => $shortcuts, |
||
200 | 'diagram' => $diagram, |
||
201 | 'tools' => $tools, |
||
202 | ]; |
||
203 | |||
204 | $json = $this->get('serializer')->serialize( |
||
205 | $responseData, |
||
206 | 'json', |
||
207 | [ |
||
208 | 'groups' => ['course:read', 'ctool:read', 'tool:read', 'cshortcut:read'], |
||
209 | ] |
||
210 | ); |
||
211 | |||
212 | return new Response( |
||
213 | $json, |
||
214 | Response::HTTP_OK, |
||
215 | [ |
||
216 | 'Content-type' => 'application/json', |
||
217 | ] |
||
218 | ); |
||
219 | /*return $this->render( |
||
220 | '@ChamiloCore/Course/home.html.twig', |
||
221 | [ |
||
222 | 'course' => $course, |
||
223 | 'shortcuts' => $shortcuts, |
||
224 | 'diagram' => $diagram, |
||
225 | 'tools' => $tools, |
||
226 | ] |
||
227 | );*/ |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Redirects the page to a tool, following the tools.yml settings. |
||
232 | * |
||
233 | * @Route("/{cid}/tool/{toolName}", name="chamilo_core_course_redirect_tool") |
||
234 | */ |
||
235 | public function redirectTool(string $toolName, CToolRepository $repo, ToolChain $toolChain): RedirectResponse |
||
261 | } |
||
262 | |||
263 | public function redirectToShortCut(string $toolName, CToolRepository $repo, ToolChain $toolChain): RedirectResponse |
||
264 | { |
||
265 | /** @var CTool|null $tool */ |
||
266 | $tool = $repo->findOneBy([ |
||
267 | 'name' => $toolName, |
||
268 | ]); |
||
269 | |||
270 | if (null === $tool) { |
||
271 | throw new NotFoundHttpException($this->trans('Tool not found')); |
||
272 | } |
||
273 | |||
274 | $tool = $toolChain->getToolFromName($tool->getTool()->getName()); |
||
275 | $link = $tool->getLink(); |
||
276 | |||
277 | if (strpos($link, 'nodeId')) { |
||
278 | $nodeId = (string) $this->getCourse()->getResourceNode()->getId(); |
||
279 | $link = str_replace(':nodeId', $nodeId, $link); |
||
280 | } |
||
281 | |||
282 | $url = $link.'?'.$this->getCourseUrlQuery(); |
||
283 | |||
284 | return $this->redirect($url); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Edit configuration with given namespace. |
||
289 | * |
||
290 | * @Route("/{cid}/settings/{namespace}", name="chamilo_core_course_settings") |
||
291 | * |
||
292 | * @Entity("course", expr="repository.find(cid)") |
||
293 | */ |
||
294 | public function updateSettings(Request $request, Course $course, string $namespace, SettingsCourseManager $manager, SettingsFormFactory $formFactory): Response |
||
333 | ] |
||
334 | ); |
||
335 | } |
||
336 | |||
337 | private function autoLaunch(): void |
||
505 | ) |
||
506 | ); |
||
510 |