checklistapi.drush.inc 4.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

/**
 * @file
 * Drush commands for the Checklist API module.
 */

/**
 * Implements hook_drush_command().
 */
function checklistapi_drush_command() {
  $items = array();

  // Table formatting isn't available until Drush 6.
  if (function_exists('drush_format_table')) {
    $items['checklistapi-list'] = array(
17
      'aliases' => array('capi-list', 'capil'),
18
      'description' => 'Get an overview of your installed checklists with progress details.',
19
    );
20 21 22 23 24 25 26 27
    $items['checklistapi-info'] = array(
      'aliases' => array('capi-info', 'capii'),
      'description' => 'Show detailed info for a given checklist.',
      'arguments' => array(
        'checklist' => 'The checklist machine name, e.g., "example_checklist".',
      ),
      'required-arguments' => TRUE,
    );
28 29 30 31 32
  }

  return $items;
}

33 34 35 36 37 38 39 40 41 42
/**
 * Implements hook_drush_help().
 */
function checklistapi_drush_help($section) {
  switch ($section) {
    case 'meta:checklistapi:title':
      return dt('Checklist API commands');
  }
}

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/**
 * Drush callback for checklist listing.
 */
function drush_checklistapi_list() {
  $definitions = checklistapi_get_checklist_info();

  if (empty($definitions)) {
    return drush_print(dt('No checklists available.'));
  }

  // Build table rows.
  $rows = array();
  // The first row is the table header.
  $rows[] = array(
    dt('Checklist'),
    dt('Progress'),
    dt('Last updated'),
    dt('Last updated by'),
  );
  foreach ($definitions as $id => $definition) {
    $checklist = checklistapi_checklist_load($id);
    $row = array();
65 66 67 68
    $row[] = dt('!title (!id)', array(
      '!title' => strip_tags($checklist->title),
      '!id' => $id,
    ));
69 70 71 72 73 74
    $row[] = dt('@completed of @total (@percent%)', array(
      '@completed' => $checklist->getNumberCompleted(),
      '@total' => $checklist->getNumberOfItems(),
      '@percent' => round($checklist->getPercentComplete()),
    ));
    $row[] = $checklist->getLastUpdatedDate();
75
    $row[] = strip_tags($checklist->getLastUpdatedUser());
76 77 78 79 80
    $rows[] = $row;
  }

  return drush_format_table($rows, TRUE);
}
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

/**
 * Implements drush_hook_COMMAND_validate().
 *
 * @see drush_checklistapi_info()
 */
function drush_checklistapi_info_validate() {
  $arguments = drush_get_arguments();
  $id = $arguments[1];
  $checklist = checklistapi_checklist_load($id);

  // Make sure the given checklist exists.
  if (!$checklist) {
    drush_set_error('', dt('No such checklist "!id".', array(
      '!id' => $id,
    )));
  }
98 99

  drush_set_context('checklist', $checklist);
100 101 102 103 104 105
}

/**
 * Drush callback for checklist info.
 */
function drush_checklistapi_info() {
106
  $checklist = drush_get_context('checklist');
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

  // Print the help.
  if (!empty($checklist->help)) {
    drush_print(strip_tags($checklist->help));
  }

  // Print last updated and progress details.
  if ($checklist->hasSavedProgress()) {
    drush_print('');
    drush_print(dt('Last updated @date by !user', array(
      '@date' => $checklist->getLastUpdatedDate(),
      '!user' => strip_tags($checklist->getLastUpdatedUser()),
    )));
    drush_print(dt('@completed of @total (@percent%) complete', array(
      '@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.
    drush_print('');
    drush_print(strip_tags($group['#title']) . ':');

    // Loop through items.
    foreach (element_children($group) as $item_key) {
      $item = &$group[$item_key];
      $saved_item = !empty($checklist->savedProgress[$item_key]) ? $checklist->savedProgress[$item_key] : 0;
      // Build title.
      $title = strip_tags($item['#title']);
      if ($saved_item) {
        // Append completion details.
        $user = user_load($saved_item['#uid']);
        $title .= t(' - Completed @time by !user', array(
          '@time' => format_date($saved_item['#completed'], 'short'),
          '!user' => strip_tags($user->name),
        ));
      }
      // Print the list item.
      drush_print(dt(' [!x] !title', array(
        '!x' => ($saved_item) ? 'x' : ' ',
        '!title' => $title,
      )));
    }
  }
}