| Conditions | 16 |
| Paths | 24 |
| Total Lines | 310 |
| Code Lines | 164 |
| 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 |
||
| 98 | $settings = $repository->getResourceSettings(); |
||
| 99 | |||
| 100 | /*$grid = $this->getGrid($request, $repository, $grid, $resourceNodeId, 'chamilo_core_resource_list'); |
||
| 101 | $parentResourceNode = $this->getParentResourceNode($request); |
||
| 102 | $this->setBreadCrumb($request, $parentResourceNode);*/ |
||
| 103 | |||
| 104 | //return $grid->getGridResponse( |
||
| 105 | return $this->render( |
||
| 106 | $repository->getTemplates()->getFromAction(__FUNCTION__), |
||
| 107 | [ |
||
| 108 | 'parent_id' => $resourceNodeId, |
||
| 109 | 'tool' => $tool, |
||
| 110 | 'type' => $type, |
||
| 111 | 'id' => $resourceNodeId, |
||
| 112 | 'parent_resource_node' => $parentResourceNode, |
||
|
|
|||
| 113 | 'resource_settings' => $settings, |
||
| 114 | ] |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @deprecated in favor of vue CRUD methods |
||
| 120 | * |
||
| 121 | * @Route("/{tool}/{type}/{id}/new_folder", methods={"GET", "POST"}, name="chamilo_core_resource_new_folder") |
||
| 122 | */ |
||
| 123 | public function newFolderAction(Request $request): Response |
||
| 124 | { |
||
| 125 | return $this->createResource($request, 'folder'); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @deprecated in favor of vue CRUD methods |
||
| 130 | * |
||
| 131 | * @Route("/{tool}/{type}/{id}/new", methods={"GET", "POST"}, name="chamilo_core_resource_new") |
||
| 132 | */ |
||
| 133 | public function newAction(Request $request): Response |
||
| 134 | { |
||
| 135 | return $this->createResource($request, 'file'); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @Route("/{tool}/{type}/{id}/disk_space", methods={"GET", "POST"}, name="chamilo_core_resource_disk_space") |
||
| 140 | */ |
||
| 141 | public function diskSpaceAction(Request $request): Response |
||
| 142 | { |
||
| 143 | $nodeId = $request->get('id'); |
||
| 144 | $repository = $this->getRepositoryFromRequest($request); |
||
| 145 | |||
| 146 | /** @var ResourceNode $resourceNode */ |
||
| 147 | $resourceNode = $repository->getResourceNodeRepository()->find($nodeId); |
||
| 148 | |||
| 149 | $this->denyAccessUnlessGranted( |
||
| 150 | ResourceNodeVoter::VIEW, |
||
| 151 | $resourceNode, |
||
| 152 | $this->trans('Unauthorised access to resource') |
||
| 153 | ); |
||
| 154 | |||
| 155 | $this->setBreadCrumb($request, $resourceNode); |
||
| 156 | |||
| 157 | $course = $this->getCourse(); |
||
| 158 | $totalSize = 0; |
||
| 159 | if ($course) { |
||
| 160 | $totalSize = $course->getDiskQuota(); |
||
| 161 | } |
||
| 162 | |||
| 163 | $size = $repository->getResourceNodeRepository()->getSize( |
||
| 164 | $resourceNode, |
||
| 165 | $repository->getResourceType(), |
||
| 166 | $course |
||
| 167 | ); |
||
| 168 | |||
| 169 | $labels[] = $course->getTitle(); |
||
| 170 | $data[] = $size; |
||
| 171 | $sessions = $course->getSessions(); |
||
| 172 | |||
| 173 | foreach ($sessions as $sessionRelCourse) { |
||
| 174 | $session = $sessionRelCourse->getSession(); |
||
| 175 | |||
| 176 | $labels[] = $course->getTitle().' - '.$session->getName(); |
||
| 177 | $size = $repository->getResourceNodeRepository()->getSize( |
||
| 178 | $resourceNode, |
||
| 179 | $repository->getResourceType(), |
||
| 180 | $course, |
||
| 181 | $session |
||
| 182 | ); |
||
| 183 | $data[] = $size; |
||
| 184 | } |
||
| 185 | |||
| 186 | $groups = $course->getGroups(); |
||
| 187 | foreach ($groups as $group) { |
||
| 188 | $labels[] = $course->getTitle().' - '.$group->getName(); |
||
| 189 | $size = $repository->getResourceNodeRepository()->getSize( |
||
| 190 | $resourceNode, |
||
| 191 | $repository->getResourceType(), |
||
| 192 | $course, |
||
| 193 | null, |
||
| 194 | $group |
||
| 195 | ); |
||
| 196 | $data[] = $size; |
||
| 197 | } |
||
| 198 | |||
| 199 | $used = array_sum($data); |
||
| 200 | $labels[] = $this->trans('Free'); |
||
| 201 | $data[] = $totalSize - $used; |
||
| 202 | |||
| 203 | return $this->render( |
||
| 204 | $repository->getTemplates()->getFromAction(__FUNCTION__), |
||
| 205 | [ |
||
| 206 | 'resourceNode' => $resourceNode, |
||
| 207 | 'labels' => $labels, |
||
| 208 | 'data' => $data, |
||
| 209 | ] |
||
| 210 | ); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @deprecated in favor of vue CRUD methods |
||
| 215 | * |
||
| 216 | * @Route("/{tool}/{type}/{id}/edit", methods={"GET", "POST"}) |
||
| 217 | */ |
||
| 218 | public function editAction(Request $request, IllustrationRepository $illustrationRepository): Response |
||
| 219 | { |
||
| 220 | $resourceNodeId = $request->get('id'); |
||
| 221 | |||
| 222 | $repository = $this->getRepositoryFromRequest($request); |
||
| 223 | $resource = $repository->getResourceFromResourceNode($resourceNodeId); |
||
| 224 | $this->denyAccessUnlessValidResource($resource); |
||
| 225 | $settings = $repository->getResourceSettings(); |
||
| 226 | $resourceNode = $resource->getResourceNode(); |
||
| 227 | |||
| 228 | $this->denyAccessUnlessGranted( |
||
| 229 | ResourceNodeVoter::EDIT, |
||
| 230 | $resourceNode, |
||
| 231 | $this->trans('Unauthorised access to resource') |
||
| 232 | ); |
||
| 233 | |||
| 234 | $this->setBreadCrumb($request, $resourceNode); |
||
| 235 | $resourceNodeParentId = $resourceNode->getId(); |
||
| 236 | |||
| 237 | $routeParams = $this->getResourceParams($request); |
||
| 238 | $routeParams['id'] = $resourceNodeParentId; |
||
| 239 | |||
| 240 | $form = $repository->getForm($this->container->get('form.factory'), $resource); |
||
| 241 | |||
| 242 | if ($resourceNode->hasEditableTextContent() && $settings->isAllowToSaveEditorToResourceFile()) { |
||
| 243 | $form->add( |
||
| 244 | $this->fileContentName, |
||
| 245 | CKEditorType::class, |
||
| 246 | [ |
||
| 247 | 'mapped' => false, |
||
| 248 | 'config' => [ |
||
| 249 | 'filebrowserImageBrowseRoute' => 'resources_filemanager', |
||
| 250 | 'filebrowserImageBrowseRouteParameters' => $routeParams, |
||
| 251 | ], |
||
| 252 | ] |
||
| 253 | ); |
||
| 254 | $content = $repository->getResourceNodeFileContent($resourceNode); |
||
| 255 | $form->get($this->fileContentName)->setData($content); |
||
| 256 | } |
||
| 257 | |||
| 258 | $form->handleRequest($request); |
||
| 259 | |||
| 260 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 261 | /** @var AbstractResource $newResource */ |
||
| 262 | $newResource = $form->getData(); |
||
| 263 | |||
| 264 | if ($form->has($this->fileContentName)) { |
||
| 265 | $data = $form->get($this->fileContentName)->getData(); |
||
| 266 | $repository->updateResourceFileContent($newResource, $data); |
||
| 267 | } |
||
| 268 | |||
| 269 | $repository->updateNodeForResource($newResource); |
||
| 270 | |||
| 271 | if ($form->has('illustration')) { |
||
| 272 | $illustration = $form->get('illustration')->getData(); |
||
| 273 | if ($illustration) { |
||
| 274 | $file = $illustrationRepository->addIllustration($newResource, $this->getUser(), $illustration); |
||
| 275 | $em = $illustrationRepository->getEntityManager(); |
||
| 276 | $em->persist($file); |
||
| 277 | $em->flush(); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | $this->addFlash('success', $this->trans('Updated')); |
||
| 282 | |||
| 283 | $resourceNodeParentId = $newResource->getResourceNode()->getParent()->getId(); |
||
| 284 | $routeParams['id'] = $resourceNodeParentId; |
||
| 285 | |||
| 286 | return $this->redirectToRoute('chamilo_core_resource_list', $routeParams); |
||
| 287 | } |
||
| 288 | |||
| 289 | return $this->render( |
||
| 290 | $repository->getTemplates()->getFromAction(__FUNCTION__), |
||
| 291 | [ |
||
| 292 | 'form' => $form->createView(), |
||
| 293 | 'parent' => $resourceNodeParentId, |
||
| 294 | ] |
||
| 295 | ); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Shows a resource information. |
||
| 300 | * |
||
| 301 | * @Route("/{tool}/{type}/{id}/info", methods={"GET", "POST"}, name="chamilo_core_resource_info") |
||
| 302 | */ |
||
| 303 | public function infoAction(Request $request): Response |
||
| 304 | { |
||
| 305 | $nodeId = $request->get('id'); |
||
| 306 | $repository = $this->getRepositoryFromRequest($request); |
||
| 307 | |||
| 308 | /** @var AbstractResource $resource */ |
||
| 309 | $resource = $repository->getResourceFromResourceNode($nodeId); |
||
| 310 | $this->denyAccessUnlessValidResource($resource); |
||
| 311 | $resourceNode = $resource->getResourceNode(); |
||
| 312 | |||
| 313 | $this->denyAccessUnlessGranted( |
||
| 314 | ResourceNodeVoter::VIEW, |
||
| 315 | $resourceNode, |
||
| 316 | $this->trans(sprintf('Unauthorised access to resource #%s', $nodeId)) |
||
| 317 | ); |
||
| 318 | |||
| 319 | $this->setBreadCrumb($request, $resourceNode); |
||
| 320 | |||
| 321 | $tool = $request->get('tool'); |
||
| 322 | $type = $request->get('type'); |
||
| 323 | |||
| 324 | $form = $this->createForm(ResourceCommentType::class, null); |
||
| 325 | |||
| 326 | $params = [ |
||
| 327 | 'resource' => $resource, |
||
| 328 | 'course' => $this->getCourse(), |
||
| 329 | // 'illustration' => $illustration, |
||
| 330 | 'tool' => $tool, |
||
| 331 | 'type' => $type, |
||
| 332 | 'comment_form' => $form->createView(), |
||
| 333 | ]; |
||
| 334 | |||
| 335 | return $this->render( |
||
| 336 | $repository->getTemplates()->getFromAction(__FUNCTION__, $request->isXmlHttpRequest()), |
||
| 337 | $params |
||
| 338 | ); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Preview a file. Mostly used when using a modal. |
||
| 343 | * |
||
| 344 | * @Route("/{tool}/{type}/{id}/preview", methods={"GET"}, name="chamilo_core_resource_preview") |
||
| 345 | */ |
||
| 346 | public function previewAction(Request $request): Response |
||
| 347 | { |
||
| 348 | $nodeId = $request->get('id'); |
||
| 349 | $repository = $this->getRepositoryFromRequest($request); |
||
| 350 | |||
| 351 | /** @var AbstractResource $resource */ |
||
| 352 | $resource = $repository->getResourceFromResourceNode($nodeId); |
||
| 353 | $this->denyAccessUnlessValidResource($resource); |
||
| 354 | |||
| 355 | $resourceNode = $resource->getResourceNode(); |
||
| 356 | $this->denyAccessUnlessGranted( |
||
| 357 | ResourceNodeVoter::VIEW, |
||
| 358 | $resourceNode, |
||
| 359 | $this->trans('Unauthorised access to resource') |
||
| 360 | ); |
||
| 361 | |||
| 362 | $this->setBreadCrumb($request, $resourceNode); |
||
| 363 | |||
| 364 | $tool = $request->get('tool'); |
||
| 365 | $type = $request->get('type'); |
||
| 366 | |||
| 367 | $params = [ |
||
| 368 | 'resource' => $resource, |
||
| 369 | 'tool' => $tool, |
||
| 370 | 'type' => $type, |
||
| 371 | ]; |
||
| 372 | |||
| 373 | return $this->render($repository->getTemplates()->getFromAction(__FUNCTION__), $params); |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @Route("/{tool}/{type}/{id}/change_visibility", name="chamilo_core_resource_change_visibility") |
||
| 378 | */ |
||
| 379 | public function changeVisibilityAction(Request $request): Response |
||
| 380 | { |
||
| 381 | $id = $request->get('id'); |
||
| 382 | |||
| 383 | $repository = $this->getRepositoryFromRequest($request); |
||
| 384 | |||
| 385 | /** @var AbstractResource $resource */ |
||
| 386 | $resource = $repository->getResourceFromResourceNode($id); |
||
| 387 | $this->denyAccessUnlessValidResource($resource); |
||
| 388 | |||
| 389 | $resourceNode = $resource->getResourceNode(); |
||
| 390 | |||
| 391 | $this->denyAccessUnlessGranted( |
||
| 392 | ResourceNodeVoter::EDIT, |
||
| 393 | $resourceNode, |
||
| 394 | $this->trans('Unauthorised access to resource') |
||
| 395 | ); |
||
| 396 | |||
| 397 | /** @var ResourceLink $link */ |
||
| 398 | if ($this->hasCourse()) { |
||
| 399 | $link = $resource->getCourseSessionResourceLink($this->getCourse(), $this->getSession()); |
||
| 400 | } else { |
||
| 401 | $link = $resource->getFirstResourceLink(); |
||
| 402 | } |
||
| 403 | |||
| 404 | $icon = 'fa-eye'; |
||
| 405 | // Use repository to change settings easily. |
||
| 406 | if ($link && ResourceLink::VISIBILITY_PUBLISHED === $link->getVisibility()) { |
||
| 407 | $repository->setVisibilityDraft($resource); |
||
| 408 | $icon = 'fa-eye-slash'; |
||
| 902 |