Projet

Général

Profil

Paste
Télécharger (4,16 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / votingapi / votingapi.drush.inc @ 384fc62a

1
<?php
2

    
3
/**
4
 * @file
5
 *  Drush commands to generate votingapi votes, recalculate results
6
 *  for existing votes, or flush VotingAPI data entirely.
7
 */
8

    
9
/**
10
 * Implements of hook_drush_help().
11
 */
12
function votingapi_drush_help($section) {
13
  switch ($section) {
14
    case 'drush:generate-votes':
15
      return dt('Usage: drush generate-votes <entity_type> <vote_type>.');
16
    case 'drush:votingapi-recalculate':
17
      return dt('Usage: drush votingapi-recalculate <entity_type>.');
18
    case 'drush:votingapi-flush':
19
      return dt('Usage: drush votingapi-flush <entity_type>.');
20
  }
21
}
22

    
23
/**
24
 * Implements of hook_drush_command().
25
 */
26
function votingapi_drush_command() {
27
  $items['generate-votes'] = array(
28
    'description' => 'Creates dummy voting data.',
29
    'arguments' => array(
30
      'entity_type' => 'The type of entity to generate votes for.',
31
      'vote_type' => 'The type of votes to generate, defaults to \'percent\'.',
32
    ),
33
    'drupal dependencies' => array('devel_generate'),
34
    'options' => array(
35
      'kill' => 'Specify \'kill\' to delete all existing votes before generating new ones.',
36
      'age' => 'The maximum age, in seconds, of each vote.',
37
      'node_types' => 'A comma delimited list of node types to generate votes for, if the entity type is \'node\'.',
38
    ),
39
    'aliases' => array('genv'),
40
  );
41
  $items['votingapi-recalculate'] = array(
42
    'description' => 'Regenerates voting results from raw vote data.',
43
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, // Various D7 code assumes we have a uid.
44
    'arguments' => array(
45
      'entity_type' => 'The type of entity to recalculate vote results for.',
46
    ),
47
    'aliases' => array('vcalc'),
48
  );
49
  $items['votingapi-flush'] = array(
50
    'description' => 'Deletes all existing voting data.',
51
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, // Various D7 code assumes we have a uid.
52
    'arguments' => array(
53
      'entity_type' => 'The type of entity whose voting data should be flushed.',
54
    ),
55
    'aliases' => array('vflush'),
56
  );
57
  return $items;
58
}
59

    
60
/**
61
 * Command callback. Generate a number of votes.
62
 */
63
function drush_votingapi_generate_votes($entity_type = 'node', $vote_type = 'percent') {
64
  $options = array(
65
    'kill' => drush_get_option('kill'),
66
    'age' => drush_get_option('age'),
67
    'types' => drush_get_option('types'),
68
  );
69

    
70
  require_once(drupal_get_path('module', 'votingapi') . '/votingapi.devel.inc');
71
  votingapi_generate_votes($entity_type, $vote_type, $options);
72

    
73
  drush_log(t('Generating @vtype votes for @etype entities.', array('@vtype' => $vote_type, '@etype' => $entity_type)), 'success');
74
}
75

    
76
/**
77
 * Command callback. Recalculate voting results.
78
 */
79
function drush_votingapi_recalculate($entity_type = 'node', $entity_id = NULL) {
80
  // Prep some starter query objects.
81
  $data = array();
82
  if (empty($entity_id)) {
83
    $votes = db_select('votingapi_vote', 'vv')
84
      ->fields('vv', array('entity_type', 'entity_id'))
85
      ->condition('entity_type', $entity_type, '=')->distinct(TRUE)
86
      ->execute()->fetchAll(PDO::FETCH_ASSOC);
87
      $message = t('Rebuilt voting results for @type votes.', array('@type' => $entity_type));
88
  }
89
  else {
90
    $votes[] = array('entity_type' => $entity_type, 'entity_id' => $entity_id);
91
    $message = t('Rebuilt all voting results.');
92
  }
93

    
94
  foreach ($votes as $vote) {
95
    votingapi_recalculate_results($vote['entity_type'], $vote['entity_id'], TRUE);
96
  }
97

    
98
  drush_log($message, 'success');
99
}
100

    
101
/**
102
 * Command callback. Flush votes and results.
103
 */
104
function drush_votingapi_flush($entity_type = NULL, $entity_id = NULL) {
105
  if (drush_confirm(dt("Delete @type voting data?", array('@type' => empty($entity_type) ? dt('all') : $entity_type)))) {
106
    $cache = db_delete('votingapi_cache');
107
    $votes = db_delete('votingapi_vote');
108

    
109
    if (!empty($entity_type)) {
110
      $cache->condition('entity_type', $entity_type);
111
      $votes->condition('entity_type', $entity_type);
112
    }
113
    if (!empty($entity_id)) {
114
      $cache->condition('entity_id', $entity_id);
115
      $votes->condition('entity_id', $entity_id);
116
    }
117

    
118
    $cache->execute();
119
    $votes->execute();
120

    
121
    drush_log(t('Flushed vote data for @type entities.', array('@type' => empty($entity_type) ? t('all') : $entity_type)), 'success');
122
  }
123
}