| Conditions | 2 |
| Paths | 2 |
| Total Lines | 114 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 277 | public function display() |
||
| 278 | { |
||
| 279 | // Action links |
||
| 280 | $content = '<div class="actions">'; |
||
| 281 | $content .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'">'; |
||
| 282 | $content .= Display::return_icon( |
||
| 283 | 'back.png', |
||
| 284 | get_lang('Back to').' '.get_lang('Administration'), |
||
| 285 | '', |
||
| 286 | ICON_SIZE_MEDIUM |
||
| 287 | ); |
||
| 288 | $content .= '</a>'; |
||
| 289 | $content .= '<a href="'.api_get_self().'?action=add&'.api_get_cidreq().'">'; |
||
| 290 | $content .= Display::return_icon( |
||
| 291 | 'add.png', |
||
| 292 | get_lang('Add'), |
||
| 293 | '', |
||
| 294 | ICON_SIZE_MEDIUM |
||
| 295 | ); |
||
| 296 | $content .= '</a>'; |
||
| 297 | $content .= '</div>'; |
||
| 298 | |||
| 299 | // 1. Set entity |
||
| 300 | $source = new Entity('ChamiloCourseBundle:CExerciseCategory'); |
||
| 301 | // 2. Get query builder from repo. |
||
| 302 | $qb = Container::getExerciseCategoryRepository()->getResourcesByCourse(api_get_course_entity()); |
||
| 303 | |||
| 304 | // 3. Set QueryBuilder to the source. |
||
| 305 | $source-> initQueryBuilder($qb); |
||
| 306 | |||
| 307 | // 4. Get the grid builder. |
||
| 308 | $builder = Container::$container->get('apy_grid.factory'); |
||
| 309 | |||
| 310 | // 5. Set parameters and properties. |
||
| 311 | $grid = $builder->createBuilder( |
||
| 312 | 'grid', |
||
| 313 | $source, |
||
| 314 | [ |
||
| 315 | 'persistence' => false, |
||
| 316 | 'route' => 'home', |
||
| 317 | 'filterable' => true, |
||
| 318 | 'sortable' => true, |
||
| 319 | 'max_per_page' => 10, |
||
| 320 | ] |
||
| 321 | )->add( |
||
| 322 | 'id', |
||
| 323 | 'number', |
||
| 324 | [ |
||
| 325 | 'title' => '#', |
||
| 326 | 'primary' => 'true', |
||
| 327 | ] |
||
| 328 | )->add( |
||
| 329 | 'name', |
||
| 330 | 'text', |
||
| 331 | [ |
||
| 332 | 'title' => 'name', |
||
| 333 | ] |
||
| 334 | )->add( |
||
| 335 | 'description', |
||
| 336 | 'text', |
||
| 337 | [ |
||
| 338 | 'title' => 'description', |
||
| 339 | ] |
||
| 340 | ); |
||
| 341 | $grid = $grid->getGrid(); |
||
| 342 | |||
| 343 | if (Container::getAuthorizationChecker()->isGranted(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)) { |
||
| 344 | // Add row actions |
||
| 345 | $myRowAction = new RowAction( |
||
| 346 | get_lang('Edit'), |
||
| 347 | 'legacy_main', |
||
| 348 | false, |
||
| 349 | '_self', |
||
| 350 | ['class' => 'btn btn-secondary'] |
||
| 351 | ); |
||
| 352 | $myRowAction->setRouteParameters( |
||
| 353 | ['id', 'name' => 'exercise/category.php', 'cidReq' => api_get_course_id(), 'action' => 'edit'] |
||
| 354 | ); |
||
| 355 | $grid->addRowAction($myRowAction); |
||
| 356 | |||
| 357 | $myRowAction = new RowAction( |
||
| 358 | get_lang('Delete'), |
||
| 359 | 'legacy_main', |
||
| 360 | true, |
||
| 361 | '_self', |
||
| 362 | ['class' => 'btn btn-danger', 'form_delete' => true] |
||
| 363 | ); |
||
| 364 | $myRowAction->setRouteParameters( |
||
| 365 | ['id', 'name' => 'exercise/category.php', 'cidReq' => api_get_course_id(), 'action' => 'delete'] |
||
| 366 | ); |
||
| 367 | $grid->addRowAction($myRowAction); |
||
| 368 | |||
| 369 | // Add mass actions |
||
| 370 | $deleteMassAction = new MassAction( |
||
| 371 | 'Delete', |
||
| 372 | ['ExerciseCategoryManager', 'deleteResource'], |
||
| 373 | true, |
||
| 374 | [] |
||
| 375 | ); |
||
| 376 | $grid->addMassAction($deleteMassAction); |
||
| 377 | } |
||
| 378 | |||
| 379 | // 8. Set route and request |
||
| 380 | $grid |
||
| 381 | ->setRouteUrl(api_get_self().'?'.api_get_cidreq()) |
||
| 382 | ->handleRequest(Container::getRequest()) |
||
| 383 | ; |
||
| 384 | |||
| 385 | $content .= Container::$container->get('twig')->render( |
||
| 386 | '@ChamiloTheme/Resource/grid.html.twig', |
||
| 387 | ['grid' => $grid] |
||
| 388 | ); |
||
| 389 | |||
| 390 | return $content; |
||
| 391 | } |
||
| 393 |