Conditions | 24 |
Paths | > 20000 |
Total Lines | 174 |
Code Lines | 117 |
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 |
||
328 | public function get_table_data( |
||
329 | $from = 1, |
||
330 | $perPage = null, |
||
331 | $column = null, |
||
332 | $direction = null, |
||
333 | $sort = null |
||
334 | ) { |
||
335 | $is_western_name_order = api_is_western_name_order(); |
||
336 | |||
337 | // create page navigation if needed |
||
338 | $totalitems = $this->datagen->get_total_items_count(); |
||
339 | |||
340 | if ($this->limit_enabled && $totalitems > GRADEBOOK_ITEM_LIMIT) { |
||
341 | $selectlimit = GRADEBOOK_ITEM_LIMIT; |
||
342 | } else { |
||
343 | $selectlimit = $totalitems; |
||
344 | } |
||
345 | |||
346 | $header = null; |
||
347 | if ($this->limit_enabled && $totalitems > GRADEBOOK_ITEM_LIMIT) { |
||
348 | $header .= '<table style="width: 100%; text-align: right; margin-left: auto; margin-right: auto;" border="0" cellpadding="2">' |
||
349 | .'<tbody>' |
||
350 | .'<tr>'; |
||
351 | |||
352 | // previous X |
||
353 | $header .= '<td style="width:100%;">'; |
||
354 | if ($this->offset >= GRADEBOOK_ITEM_LIMIT) { |
||
355 | $header .= '<a href="'.api_get_self() |
||
356 | .'?selectcat='.Security::remove_XSS($_GET['selectcat']) |
||
357 | .'&offset='.(($this->offset) - GRADEBOOK_ITEM_LIMIT) |
||
358 | .(isset($_GET['search']) ? '&search='.Security::remove_XSS($_GET['search']) : '').'">' |
||
359 | .Display::getMdiIcon( |
||
360 | ActionIcon::PREVIOUS, |
||
361 | 'ch-tool-icon', |
||
362 | null, |
||
363 | ICON_SIZE_MEDIUM, |
||
364 | get_lang('Previous page') |
||
365 | ) |
||
366 | .'</a>'; |
||
367 | } else { |
||
368 | $header .= Display::getMdiIcon( |
||
369 | ActionIcon::PREVIOUS, |
||
370 | 'ch-tool-icon-disabled', |
||
371 | null, |
||
372 | ICON_SIZE_MEDIUM, |
||
373 | get_lang('Previous page') |
||
374 | ); |
||
375 | } |
||
376 | $header .= ' '; |
||
377 | // next X |
||
378 | $calcnext = (($this->offset + (2 * GRADEBOOK_ITEM_LIMIT)) > $totalitems) ? |
||
379 | ($totalitems - (GRADEBOOK_ITEM_LIMIT + $this->offset)) : GRADEBOOK_ITEM_LIMIT; |
||
380 | |||
381 | if ($calcnext > 0) { |
||
382 | $header .= '<a href="'.api_get_self() |
||
383 | .'?selectcat='.Security::remove_XSS($_GET['selectcat']) |
||
384 | .'&offset='.($this->offset + GRADEBOOK_ITEM_LIMIT) |
||
385 | .(isset($_GET['search']) ? '&search='.Security::remove_XSS($_GET['search']) : '').'">' |
||
386 | .Display::getMdiIcon(ActionIcon::NEXT, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Next page')) |
||
387 | .'</a>'; |
||
388 | } else { |
||
389 | $header .= Display::getMdiIcon( |
||
390 | ActionIcon::NEXT, |
||
391 | 'ch-tool-icon-disabled', |
||
392 | null, |
||
393 | ICON_SIZE_MEDIUM, |
||
394 | get_lang('Next page') |
||
395 | ); |
||
396 | } |
||
397 | $header .= '</td>'; |
||
398 | $header .= '</tbody></table>'; |
||
399 | echo $header; |
||
400 | } |
||
401 | |||
402 | // retrieve sorting type |
||
403 | if ($is_western_name_order) { |
||
404 | $users_sorting = (0 == $this->column ? FlatViewDataGenerator::FVDG_SORT_FIRSTNAME : FlatViewDataGenerator::FVDG_SORT_LASTNAME); |
||
405 | } else { |
||
406 | $users_sorting = (0 == $this->column ? FlatViewDataGenerator::FVDG_SORT_LASTNAME : FlatViewDataGenerator::FVDG_SORT_FIRSTNAME); |
||
407 | } |
||
408 | |||
409 | if ('DESC' === $this->direction) { |
||
410 | $users_sorting |= FlatViewDataGenerator::FVDG_SORT_DESC; |
||
411 | } else { |
||
412 | $users_sorting |= FlatViewDataGenerator::FVDG_SORT_ASC; |
||
413 | } |
||
414 | |||
415 | // step 1: generate columns: evaluations and links |
||
416 | $header_names = $this->datagen->get_header_names($this->offset, $selectlimit); |
||
417 | $userRowSpan = false; |
||
418 | foreach ($header_names as $item) { |
||
419 | if (is_array($item)) { |
||
420 | $userRowSpan = true; |
||
421 | break; |
||
422 | } |
||
423 | } |
||
424 | |||
425 | $thAttributes = ''; |
||
426 | if ($userRowSpan) { |
||
427 | $thAttributes = 'rowspan=2'; |
||
428 | } |
||
429 | |||
430 | $this->set_header(0, $header_names[0], true, $thAttributes); |
||
431 | $this->set_header(1, $header_names[1], true, $thAttributes); |
||
432 | |||
433 | $column = 2; |
||
434 | $firstHeader = []; |
||
435 | while ($column < count($header_names)) { |
||
436 | $headerData = $header_names[$column]; |
||
437 | |||
438 | if (is_array($headerData)) { |
||
439 | $countItems = count($headerData['items']); |
||
440 | |||
441 | $this->set_header( |
||
442 | $column, |
||
443 | $headerData['header'], |
||
444 | false, |
||
445 | 'colspan="'.$countItems.'"' |
||
446 | ); |
||
447 | |||
448 | foreach ($headerData['items'] as $item) { |
||
449 | $firstHeader[] = '<span class="text-center">'.$item.'</span>'; |
||
450 | } |
||
451 | } else { |
||
452 | $this->set_header($column, $headerData, false, $thAttributes); |
||
453 | } |
||
454 | $column++; |
||
455 | } |
||
456 | |||
457 | $data_array = $this->datagen->get_data( |
||
458 | $users_sorting, |
||
459 | $from, |
||
460 | $this->per_page, |
||
461 | $this->offset, |
||
462 | $selectlimit |
||
463 | ); |
||
464 | |||
465 | $table_data = []; |
||
466 | |||
467 | if (!empty($firstHeader)) { |
||
468 | $table_data[] = $firstHeader; |
||
469 | } |
||
470 | |||
471 | $columnOffset = empty($this->datagen->params['show_official_code']) ? 0 : 1; |
||
472 | |||
473 | foreach ($data_array as $user_row) { |
||
474 | $user_id = $user_row[0]; |
||
475 | unset($user_row[0]); |
||
476 | $userInfo = api_get_user_info($user_id); |
||
477 | if ($is_western_name_order) { |
||
478 | $user_row[1 + $columnOffset] = $this->build_name_link( |
||
479 | $user_id, |
||
480 | $userInfo['firstname'] |
||
481 | ); |
||
482 | $user_row[2 + $columnOffset] = $this->build_name_link( |
||
483 | $user_id, |
||
484 | $userInfo['lastname'] |
||
485 | ); |
||
486 | } else { |
||
487 | $user_row[1 + $columnOffset] = $this->build_name_link( |
||
488 | $user_id, |
||
489 | $userInfo['lastname'] |
||
490 | ); |
||
491 | $user_row[2 + $columnOffset] = $this->build_name_link( |
||
492 | $user_id, |
||
493 | $userInfo['firstname'] |
||
494 | ); |
||
495 | } |
||
496 | $user_row = array_values($user_row); |
||
497 | |||
498 | $table_data[] = $user_row; |
||
499 | } |
||
500 | |||
501 | return $table_data; |
||
502 | } |
||
515 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: