You are here

public function ChecklistapiCommands::infoCommand in Checklist API 8

Show detailed info for a given checklist.

@command checklistapi:info @aliases capi-info,capii,checklistapi-info

Parameters

string $checklist_id: The checklist machine name, e.g., "example_checklist".

Return value

string|void The command output.

File

src/Commands/ChecklistapiCommands.php, line 100

Class

ChecklistapiCommands
Checklist API Drush command fileA Drush commandfile.

Namespace

Drupal\checklistapi\Commands

Code

public function infoCommand($checklist_id) {
  $checklist = checklistapi_checklist_load($checklist_id);

  // Make sure the given checklist exists.
  if (!$checklist) {
    return $this->logger
      ->error(dt('No such checklist "@id".', [
      '@id' => $checklist_id,
    ]));
  }
  $output = [];

  // Print the help.
  if (!empty($checklist->help)) {
    $output[] = strip_tags($checklist->help);
  }

  // Print last updated and progress details.
  if ($checklist
    ->hasSavedProgress()) {
    $output[] = '';
    $output[] = dt('Last updated @date by @user', [
      '@date' => $checklist
        ->getLastUpdatedDate(),
      '@user' => $checklist
        ->getLastUpdatedUser(),
    ]);
    $output[] = dt('@completed of @total (@percent%) complete', [
      '@completed' => $checklist
        ->getNumberCompleted(),
      '@total' => $checklist
        ->getNumberOfItems(),
      '@percent' => round($checklist
        ->getPercentComplete()),
    ]);
  }

  // Loop through groups.
  $groups = $checklist->items;
  foreach (Element::children($groups) as $group_key) {
    $group =& $groups[$group_key];

    // Print group title.
    $output[] = '';
    $output[] = strip_tags($group['#title']) . ':';

    // Loop through items.
    foreach (Element::children($group) as $item_key) {
      $item =& $group[$item_key];
      $saved_item = !empty($checklist->savedProgress['#items'][$item_key]) ? $checklist->savedProgress['#items'][$item_key] : 0;

      // Build title.
      $title = strip_tags($item['#title']);
      if ($saved_item) {

        // Append completion details.
        $user = User::load($saved_item['#uid']);
        $title .= ' - ' . dt('Completed @time by @user', [
          '@time' => \Drupal::service('date.formatter')
            ->format($saved_item['#completed'], 'short'),
          '@user' => $user
            ->getAccountName(),
        ]);
      }

      // Print the list item.
      $output[] = dt(' [@x] !title', [
        '@x' => $saved_item ? 'x' : ' ',
        '!title' => $title,
      ]);
    }
  }
  $output[] = '';
  return implode(PHP_EOL, $output);
}