| Total Complexity | 55 |
| Total Lines | 435 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 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 |
||
| 42 | class CourseHomeController extends ToolBaseController |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * @Route("/{cid}/home.json", name="chamilo_core_course_home_json") |
||
| 46 | * |
||
| 47 | * @Entity("course", expr="repository.find(cid)") |
||
| 48 | */ |
||
| 49 | public function indexJsonAction(Request $request, CToolRepository $toolRepository, CShortcutRepository $shortcutRepository, ToolChain $toolChain) |
||
| 50 | { |
||
| 51 | $course = $this->getCourse(); |
||
| 52 | if (null === $course) { |
||
| 53 | throw $this->createAccessDeniedException(); |
||
| 54 | } |
||
| 55 | |||
| 56 | $this->denyAccessUnlessGranted(CourseVoter::VIEW, $course); |
||
| 57 | |||
| 58 | $session = $request->getSession(); |
||
| 59 | |||
| 60 | /*$js = '<script>'.api_get_language_translate_html().'</script>'; |
||
| 61 | $htmlHeadXtra[] = $js;*/ |
||
| 62 | |||
| 63 | $userId = 0; |
||
| 64 | $user = $this->getUser(); |
||
| 65 | if (null !== $user) { |
||
| 66 | $userId = $this->getUser()->getId(); |
||
| 67 | } |
||
| 68 | |||
| 69 | $courseCode = $course->getCode(); |
||
| 70 | $courseId = $course->getId(); |
||
| 71 | $sessionId = $this->getSessionId(); |
||
| 72 | |||
| 73 | if ($user && INVITEE === $user->getStatus()) { |
||
| 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, $courseCode, STUDENT) |
||
| 91 | ) { |
||
| 92 | $session->set('is_allowed_in_course', true); |
||
| 93 | } |
||
| 94 | |||
| 95 | /*$action = empty($_GET['action']) ? '' : Security::remove_XSS($_GET['action']); |
||
| 96 | if ('subscribe' === $action && Security::check_token('get')) { |
||
| 97 | Security::clear_token(); |
||
| 98 | $result = CourseManager::autoSubscribeToCourse($courseCode); |
||
| 99 | if ($result && CourseManager::is_user_subscribed_in_course($userId, $courseCode)) { |
||
| 100 | $session->set('is_allowed_in_course', true); |
||
| 101 | } |
||
| 102 | header('Location: '.api_get_self()); |
||
| 103 | exit; |
||
| 104 | } |
||
| 105 | |||
| 106 | $logInfo = [ |
||
| 107 | 'tool' => 'course-main', |
||
| 108 | 'action' => $action, |
||
| 109 | ]; |
||
| 110 | Event::registerLog($logInfo);*/ |
||
| 111 | $logInfo = [ |
||
| 112 | 'tool' => 'course-main', |
||
| 113 | ]; |
||
| 114 | Event::registerLog($logInfo); |
||
| 115 | |||
| 116 | $qb = $toolRepository->getResourcesByCourse($course, $this->getSession()); |
||
| 117 | $qb->addSelect('tool'); |
||
| 118 | $qb->innerJoin('resource.tool', 'tool'); |
||
| 119 | |||
| 120 | $result = $qb->getQuery()->getResult(); |
||
| 121 | $tools = []; |
||
| 122 | $isCourseTeacher = $this->isGranted('ROLE_CURRENT_COURSE_TEACHER'); |
||
| 123 | /** @var CTool $item */ |
||
| 124 | foreach ($result as $item) { |
||
| 125 | if ('course_tool' === $item->getName()) { |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | $toolModel = $toolChain->getToolFromName($item->getTool()->getName()); |
||
| 129 | |||
| 130 | if (!$isCourseTeacher && 'admin' === $toolModel->getCategory()) { |
||
| 131 | continue; |
||
| 132 | } |
||
| 133 | $tools[$toolModel->getCategory()][] = $item; |
||
| 134 | } |
||
| 135 | |||
| 136 | // Get session-career diagram |
||
| 137 | $diagram = ''; |
||
| 138 | /*$allow = api_get_configuration_value('allow_career_diagram'); |
||
| 139 | if (true === $allow) { |
||
| 140 | $htmlHeadXtra[] = api_get_js('jsplumb2.js'); |
||
| 141 | $extra = new ExtraFieldValue('session'); |
||
| 142 | $value = $extra->get_values_by_handler_and_field_variable( |
||
| 143 | api_get_session_id(), |
||
| 144 | 'external_career_id' |
||
| 145 | ); |
||
| 146 | |||
| 147 | if (!empty($value) && isset($value['value'])) { |
||
| 148 | $careerId = $value['value']; |
||
| 149 | $extraFieldValue = new ExtraFieldValue('career'); |
||
| 150 | $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value( |
||
| 151 | 'external_career_id', |
||
| 152 | $careerId, |
||
| 153 | false, |
||
| 154 | false, |
||
| 155 | false |
||
| 156 | ); |
||
| 157 | |||
| 158 | if (!empty($item) && isset($item['item_id'])) { |
||
| 159 | $careerId = $item['item_id']; |
||
| 160 | $career = new Career(); |
||
| 161 | $careerInfo = $career->get($careerId); |
||
| 162 | if (!empty($careerInfo)) { |
||
| 163 | $extraFieldValue = new ExtraFieldValue('career'); |
||
| 164 | $item = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 165 | $careerId, |
||
| 166 | 'career_diagram', |
||
| 167 | false, |
||
| 168 | false, |
||
| 169 | 0 |
||
| 170 | ); |
||
| 171 | |||
| 172 | if (!empty($item) && isset($item['value']) && !empty($item['value'])) { |
||
| 173 | // @var Graph $graph |
||
| 174 | $graph = UnserializeApi::unserialize('career', $item['value']); |
||
| 175 | $diagram = Career::renderDiagram($careerInfo, $graph); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | }*/ |
||
| 181 | |||
| 182 | // Deleting the objects |
||
| 183 | $session->remove('toolgroup'); |
||
| 184 | $session->remove('_gid'); |
||
| 185 | $session->remove('oLP'); |
||
| 186 | $session->remove('lpobject'); |
||
| 187 | |||
| 188 | api_remove_in_gradebook(); |
||
| 189 | Exercise::cleanSessionVariables(); |
||
| 190 | |||
| 191 | $shortcuts = []; |
||
| 192 | if (null !== $user) { |
||
| 193 | $shortcutQuery = $shortcutRepository->getResources($user, $course->getResourceNode(), $course); |
||
| 194 | $shortcuts = $shortcutQuery->getQuery()->getResult(); |
||
| 195 | } |
||
| 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) |
||
| 236 | { |
||
| 237 | /** @var null|CTool $tool */ |
||
| 238 | $tool = $repo->findOneBy([ |
||
| 239 | 'name' => $toolName, |
||
| 240 | ]); |
||
| 241 | |||
| 242 | if (null === $tool) { |
||
| 243 | throw new NotFoundHttpException($this->trans('Tool not found')); |
||
| 244 | } |
||
| 245 | |||
| 246 | $tool = $toolChain->getToolFromName($tool->getTool()->getName()); |
||
| 247 | $link = $tool->getLink(); |
||
| 248 | |||
| 249 | if (strpos($link, 'nodeId')) { |
||
| 250 | $nodeId = (string) $this->getCourse()->getResourceNode()->getId(); |
||
| 251 | $link = str_replace(':nodeId', $nodeId, $link); |
||
| 252 | } |
||
| 253 | |||
| 254 | $url = $link.'?'.$this->getCourseUrlQuery(); |
||
| 255 | |||
| 256 | return $this->redirect($url); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Edit configuration with given namespace. |
||
| 261 | * |
||
| 262 | * @Route("/{cid}/settings/{namespace}", name="chamilo_core_course_settings") |
||
| 263 | * |
||
| 264 | * @Entity("course", expr="repository.find(cid)") |
||
| 265 | * |
||
| 266 | * @return Response |
||
| 267 | */ |
||
| 268 | public function updateSettingsAction(Request $request, Course $course, string $namespace, SettingsCourseManager $manager, SettingsFormFactory $formFactory) |
||
| 305 | ] |
||
| 306 | ); |
||
| 307 | } |
||
| 308 | |||
| 309 | private function autoLaunch(): void |
||
| 482 |