| Conditions | 30 |
| Paths | 1201 |
| Total Lines | 170 |
| Code Lines | 104 |
| 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 |
||
| 48 | public function indexJsonAction(Request $request, CToolRepository $toolRepository, CShortcutRepository $shortcutRepository, ToolChain $toolChain) |
||
| 49 | { |
||
| 50 | $course = $this->getCourse(); |
||
| 51 | if (null === $course) { |
||
| 52 | throw $this->createAccessDeniedException(); |
||
| 53 | } |
||
| 54 | $session = $request->getSession(); |
||
| 55 | |||
| 56 | $js = '<script>'.api_get_language_translate_html().'</script>'; |
||
| 57 | $htmlHeadXtra[] = $js; |
||
| 58 | |||
| 59 | $userId = 0; |
||
| 60 | $user = $this->getUser(); |
||
| 61 | if (null !== $user) { |
||
| 62 | $userId = $this->getUser()->getId(); |
||
| 63 | } |
||
| 64 | |||
| 65 | $courseCode = $course->getCode(); |
||
| 66 | $courseId = $course->getId(); |
||
| 67 | $sessionId = $this->getSessionId(); |
||
| 68 | |||
| 69 | if ($user && api_is_invitee()) { |
||
| 70 | $isInASession = $sessionId > 0; |
||
| 71 | $isSubscribed = CourseManager::is_user_subscribed_in_course( |
||
| 72 | $userId, |
||
| 73 | $courseCode, |
||
| 74 | $isInASession, |
||
| 75 | $sessionId |
||
| 76 | ); |
||
| 77 | |||
| 78 | if (!$isSubscribed) { |
||
| 79 | api_not_allowed(true); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | $isSpecialCourse = CourseManager::isSpecialCourse($courseId); |
||
| 84 | |||
| 85 | if ($user && $isSpecialCourse && (isset($_GET['autoreg']) && 1 === (int) $_GET['autoreg']) && |
||
| 86 | CourseManager::subscribeUser($userId, $courseCode, STUDENT) |
||
| 87 | ) { |
||
| 88 | $session->set('is_allowed_in_course', true); |
||
| 89 | } |
||
| 90 | |||
| 91 | $action = empty($_GET['action']) ? '' : Security::remove_XSS($_GET['action']); |
||
| 92 | if ('subscribe' === $action && Security::check_token('get')) { |
||
| 93 | Security::clear_token(); |
||
| 94 | $result = CourseManager::autoSubscribeToCourse($courseCode); |
||
| 95 | if ($result && CourseManager::is_user_subscribed_in_course($userId, $courseCode)) { |
||
| 96 | $session->set('is_allowed_in_course', true); |
||
| 97 | } |
||
| 98 | header('Location: '.api_get_self()); |
||
| 99 | exit; |
||
| 100 | } |
||
| 101 | |||
| 102 | $logInfo = [ |
||
| 103 | 'tool' => 'course-main', |
||
| 104 | 'action' => $action, |
||
| 105 | ]; |
||
| 106 | Event::registerLog($logInfo); |
||
| 107 | |||
| 108 | /* Introduction section (editable by course admins) */ |
||
| 109 | /*$introduction = Display::return_introduction_section( |
||
| 110 | TOOL_COURSE_HOMEPAGE, |
||
| 111 | [ |
||
| 112 | 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/', |
||
| 113 | 'CreateDocumentDir' => 'document/', |
||
| 114 | 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/', |
||
| 115 | ] |
||
| 116 | );*/ |
||
| 117 | |||
| 118 | $qb = $toolRepository->getResourcesByCourse($course, $this->getSession()); |
||
| 119 | $qb->addSelect('tool'); |
||
| 120 | $qb->innerJoin('resource.tool', 'tool'); |
||
| 121 | |||
| 122 | $result = $qb->getQuery()->getResult(); |
||
| 123 | $tools = []; |
||
| 124 | /** @var CTool $item */ |
||
| 125 | foreach ($result as $item) { |
||
| 126 | if ('course_tool' === $item->getName()) { |
||
| 127 | continue; |
||
| 128 | } |
||
| 129 | $toolModel = $toolChain->getToolFromName($item->getTool()->getName()); |
||
| 130 | |||
| 131 | if ('admin' === $toolModel->getCategory() && !$this->isGranted('ROLE_CURRENT_COURSE_TEACHER')) { |
||
| 132 | continue; |
||
| 133 | } |
||
| 134 | $tools[$toolModel->getCategory()][] = $item; |
||
| 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($user, $course->getResourceNode(), $course); |
||
| 195 | $shortcuts = $shortcutQuery->getQuery()->getResult(); |
||
| 196 | } |
||
| 197 | |||
| 198 | $responseData = [ |
||
| 199 | 'course' => $course, |
||
| 200 | 'shortcuts' => $shortcuts, |
||
| 201 | 'diagram' => $diagram, |
||
| 202 | 'tools' => $tools, |
||
| 203 | ]; |
||
| 204 | |||
| 205 | $json = $this->get('serializer')->serialize( |
||
| 206 | $responseData, |
||
| 207 | 'json', |
||
| 208 | [ |
||
| 209 | 'groups' => ['course:read', 'ctool:read', 'tool:read', 'cshortcut:read'], |
||
| 210 | ] |
||
| 211 | ); |
||
| 212 | |||
| 213 | return new Response( |
||
| 214 | $json, |
||
| 215 | Response::HTTP_OK, |
||
| 216 | [ |
||
| 217 | 'Content-type' => 'application/json', |
||
| 218 | ] |
||
| 492 |