Révision 7942932f
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/votingapi/votingapi.drush.inc | ||
---|---|---|
2 | 2 |
|
3 | 3 |
/** |
4 | 4 |
* @file |
5 |
* Generate votingapi votes, recalculate results for existing votes, or flush
|
|
6 |
* VotingAPI data entirely. |
|
5 |
* Drush commands to generate votingapi votes, recalculate results
|
|
6 |
* for existing votes, or flush VotingAPI data entirely.
|
|
7 | 7 |
*/ |
8 | 8 |
|
9 | 9 |
/** |
... | ... | |
57 | 57 |
return $items; |
58 | 58 |
} |
59 | 59 |
|
60 |
|
|
61 | 60 |
/** |
62 | 61 |
* Command callback. Generate a number of votes. |
63 | 62 |
*/ |
... | ... | |
68 | 67 |
'types' => drush_get_option('types'), |
69 | 68 |
); |
70 | 69 |
|
70 |
require_once(drupal_get_path('module', 'votingapi') . '/votingapi.devel.inc'); |
|
71 | 71 |
votingapi_generate_votes($entity_type, $vote_type, $options); |
72 | 72 |
|
73 | 73 |
drush_log(t('Generating @vtype votes for @etype entities.', array('@vtype' => $vote_type, '@etype' => $entity_type)), 'success'); |
74 | 74 |
} |
75 | 75 |
|
76 |
/** |
|
77 |
* Utility function to generate votes. |
|
78 |
*/ |
|
79 |
function votingapi_generate_votes($entity_type = 'node', $vote_type = 'percent', $options = array()) { |
|
80 |
module_load_include('inc', 'devel_generate'); |
|
81 |
$options += array( |
|
82 |
'age' => 36000, |
|
83 |
'types' => array(), |
|
84 |
'kill' => FALSE, |
|
85 |
); |
|
86 |
|
|
87 |
if (!empty($options['kill_votes'])) { |
|
88 |
db_truncate('votingapi_vote'); |
|
89 |
db_truncate('votingapi_cache'); |
|
90 |
} |
|
91 |
|
|
92 |
if (($schema = drupal_get_schema($entity_type)) && ($entity_id_column = array_shift($schema['primary key']))) { |
|
93 |
// oh look we found a schema yay |
|
94 |
} |
|
95 |
else { |
|
96 |
$entity_type = 'node'; |
|
97 |
$entity_id_column = 'nid'; |
|
98 |
} |
|
99 |
$uids = devel_get_users(); |
|
100 |
|
|
101 |
$query = db_select($entity_type, 'e')->fields('e', array($entity_id_column)); |
|
102 |
if ($entity_type == 'node' && !empty($options['types'])) { |
|
103 |
$query->condition('e.type', $options['types'], 'IN'); |
|
104 |
} |
|
105 |
$results = $query->execute()->fetchAll(PDO::FETCH_ASSOC); |
|
106 |
|
|
107 |
foreach ($results as $entity) { |
|
108 |
_votingapi_cast_votes($entity_type, $entity[$entity_id_column], $options['age'], $uids, $vote_type); |
|
109 |
} |
|
110 |
} |
|
111 |
|
|
112 |
/** |
|
113 |
* Utility function to generate votes on a node by a set of users. |
|
114 |
*/ |
|
115 |
function _votingapi_cast_votes($etype, $eid, $timestamp = 0, $uids = array(), $style = 'percent') { |
|
116 |
$votes = array(); |
|
117 |
static $tags; |
|
118 |
if (!isset($tags)) { |
|
119 |
$tags = explode(' ', devel_create_greeking(30)); |
|
120 |
} |
|
121 |
foreach ($uids as $uid) { |
|
122 |
switch ($style) { |
|
123 |
case 'percent': |
|
124 |
$votes[] = array( |
|
125 |
'uid' => $uid, |
|
126 |
'entity_type' => $etype, |
|
127 |
'entity_id' => $eid, |
|
128 |
'value_type' => 'percent', |
|
129 |
'timestamp' => REQUEST_TIME - mt_rand(0, $timestamp), |
|
130 |
'value' => mt_rand(1, 5) * 20, |
|
131 |
'tag' => $tags[mt_rand(0, 30)], |
|
132 |
); |
|
133 |
break; |
|
134 |
case 'points': |
|
135 |
if (rand(0, 3)) { |
|
136 |
$votes[] = array( |
|
137 |
'uid' => $uid, |
|
138 |
'entity_type' => $etype, |
|
139 |
'entity_id' => $eid, |
|
140 |
'value_type' => 'points', |
|
141 |
'timestamp' => REQUEST_TIME - mt_rand(0, $timestamp), |
|
142 |
'value' => rand(0, 1) ? 1 : -1, |
|
143 |
); |
|
144 |
} |
|
145 |
break; |
|
146 |
} |
|
147 |
} |
|
148 |
votingapi_set_votes($votes, array()); |
|
149 |
} |
|
150 |
|
|
151 | 76 |
/** |
152 | 77 |
* Command callback. Recalculate voting results. |
153 | 78 |
*/ |
... | ... | |
173 | 98 |
drush_log($message, 'success'); |
174 | 99 |
} |
175 | 100 |
|
176 |
|
|
177 | 101 |
/** |
178 | 102 |
* Command callback. Flush votes and results. |
179 | 103 |
*/ |
Formats disponibles : Unified diff
Weekly update of contrib modules