| Conditions | 16 |
| Paths | 67 |
| Total Lines | 171 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | ] |
||
| 510 |