-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvud.module
371 lines (339 loc) · 11.7 KB
/
vud.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
// $Id: vud.module,v 1.1.2.1 2009/05/26 11:36:55 lut4rp Exp $
/**
* @file
* Implements the core voting module.
*/
module_load_include('inc', 'vud', 'vud.theme'); // Include the theme.inc file.
define('VTAG', 'vud'); // Define the Voting API $tag value.
/**
* Implementation of hook_help().
*/
function vud_help($path, $arg) {
switch ($path) {
case 'admin/help#vud':
$output .= '<p>'. t('Provides a configurable up/down voting widget for other modules to use.') .'</p>';
return $output;
}
}
/**
* Advanced menu settings callback.
*/
function vud_admin_advanced_settings() {
$form['vote_widget_settings_advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced settings'),
);
$form['vote_widget_settings_advanced']['vud_reset_vote'] = array(
'#type' => 'radios',
'#title' => t('Allow users to reset their votes'),
'#default_value' => variable_get('vud_reset_vote', 0),
'#options' => array(0 => t('No'), 1 => t('Yes')),
'#description' => t('If yes, a link will be displayed on nodes and comments that will allow users to reset their vote.'),
);
$form['vote_widget_settings_advanced']['vud_tag'] = array(
'#type' => 'textfield',
'#title' => t('Voting API tag'),
'#default_value' => variable_get('vud_tag', 'vud'),
'#description' => t('Since Vote Up/Down uses Voting API, all votes will be tagged with this term. (default: vote)<br />This tag is useful is you have deployed various modules that use Voting API. It should always be a unique value. Usually, there is NO need to change this.'),
);
return system_settings_form($form);
}
/**
* Implementation of hook_menu().
*/
function vud_menu() {
$items = array();
$items['admin/settings/voteupdown'] = array(
'title' => 'Vote Up/Down',
'description' => 'Control the functioning of Vote Up/Down.',
'page callback' => 'drupal_get_form',
'page arguments' => array('vud_admin_advanced_settings'),
'access arguments' => array('administer vote up/down'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/settings/voteupdown/advanced'] = array(
'title' => 'Advanced',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['vud'] = array(
'title' => 'Vote',
'page callback' => 'vud_vote',
'access arguments' => array('use vote up/down'),
'type' => MENU_CALLBACK,
);
$items['user/%user/votes'] = array(
'title' => 'Your votes',
'page callback' => 'vud_user_votes',
'page arguments' => array(1),
'access arguments' => array('access vote up/down statistics'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implementation of hook_comment().
*/
function vud_comment(&$comment, $op) {
switch ($op) {
case 'view':
if (variable_get('vud_widget_comment', 1)) {
if (user_access('view up/down votes')) {
$style = variable_get('vud_widget_style_comment', 0) == 1 ? '_alt' : '';
$comment->comment = theme("vud_widget$style", $comment->cid, 'comment') . $comment->comment;
}
}
break;
}
}
/**
* Comment template preprocess. Adding vud widget to variable list.
*/
function vud_preprocess_comment(&$variables) {
if (user_access('view up/down votes')) {
$comment = $variables['comment'];
if ($comment->cid) {
$style = variable_get('vud_widget_style_comment', 0) == 1 ? '_alt' : '';
$variables['vud'] = theme("vud_widget$style", $comment->cid, 'comment');
}
}
}
/**
* Implementation of hook_link().
*/
function vud_link($type, $object, $teaser = FALSE) {
$links = array();
switch ($type) {
case 'node':
$node = &$object;
$node_type = in_array($node->type, variable_get('vud_node_types', array()), TRUE);
$style = variable_get('vud_widget_style_node', 0) == 1 ? '_alt' : '';
if ($node_type && user_access('view up/down votes')) {
if (variable_get('vud_reset_vote', 0) && user_access('use vote up/down')) {
$token = drupal_get_token("vud/$type/$node->nid/0");
$links['vud_reset'] = array(
'title' => t('Reset vote'),
'href' => "vud/$type/$node->nid/0",
'attributes' => array('title' => t('Reset your vote.')),
'query' => drupal_get_destination() .'&token='. $token,
);
}
if ($teaser && variable_get('vud_link_node', 0) && variable_get('vud_link_node', 0) != 2 && $style != "_alt") {
$links['vud_points'] = array(
'title' => theme('vud_points', $node->nid, $type),
'html' => TRUE,
);
}
else if (!$teaser && variable_get('vud_link_node', 0) > 1 && $style != "_alt") {
$links['vud_points'] = array(
'title' => theme('vud_points', $node->nid, $type),
'html' => TRUE,
);
}
}
break;
case 'comment':
$comment = &$object;
if (variable_get('vud_reset_vote', 0) && user_access('use vote up/down') && (variable_get('vud_widget_comment', 0) || variable_get('vud_link_comment', 0))) {
$token = drupal_get_token("vud/$type/$comment->cid/0");
$links['vud_reset_c'] = array(
'title' => t('Reset vote'),
'href' => "vud/$type/$comment->cid/0",
'attributes' => array('title' => t('Reset your vote.')),
'query' => drupal_get_destination() .'&token='. $token,
);
}
if (variable_get('vud_link_comment', 0) && user_access('view up/down votes')) {
$links['vud_points_c'] = array(
'title' => theme('vud_points', $comment->cid, $type, NULL),
'html' => TRUE,
);
}
break;
}
return $links;
}
/**
* Menu callback; display all votes for a user.
*/
function vud_user_votes() {
if ($account = user_load(array('uid' => arg(1), 'status' => 1))) {
if ($account->status || user_access('administer users')) {
$header = array(
array('data' => t('Node')),
array('data' => t('Vote')),
array('data' => t('Date'))
);
$sql = db_rewrite_sql("SELECT n.nid, n.title, v.value, v.timestamp FROM {node} n LEFT JOIN {votingapi_vote} v ON n.nid = v.content_id WHERE v.uid = %d AND v.tag = '%s' AND v.content_type = 'node' AND n.status = 1 ORDER BY v.timestamp DESC");
$result = pager_query($sql, 25, 0, NULL, $account->uid, variable_get('vud_tag', 'vote'));
while ($node = db_fetch_object($result)) {
$rows[] = array(
l($node->title, 'node/'. $node->nid),
$node->value,
t('!time ago', array('!time' => format_interval(time() - $node->timestamp)))
);
}
drupal_set_title(check_plain($account->name));
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 25);
return $output;
}
else {
drupal_access_denied();
}
}
else {
drupal_not_found();
}
}
/**
* Function for the main voting handler with Ajax support.
*/
function vud_vote($type, $cid, $value, $tag = NULL, $ajax = FALSE, $alt = FALSE) {
if (is_numeric($cid) && is_numeric($value) && isset($_REQUEST['token']) && drupal_valid_token($_REQUEST['token'], "vud/$type/$cid/$value")) {
$vote = array();
// Sanity-check the incoming values.
if ($value > 0) {
$vote['value'] = 1;
}
else if ($value < 0) {
$vote['value'] = -1;
}
else {
$vote['value'] = 0;
}
$vote['value_type'] = 'points';
$tag = $tag ? $tag : variable_get('vud_tag', 'vote');
$vote['tag'] = $tag;
// Do the voting via Voting API.
if ($uid = _vud_get_uid()) {
if ($vote['value'] == 0) {
$criteria = array('content_type' => $type, 'content_id' => $cid, 'uid' => $uid, 'tag' => $tag);
votingapi_delete_votes(votingapi_select_votes($criteria));
votingapi_recalculate_results($type, $cid);
}
else {
$vote['content_id'] = $cid;
$vote['content_type'] = $type;
$vote['uid'] = $uid;
$votes = array(0 => $vote);
votingapi_set_votes($votes);
}
}
if ($ajax) {
$style = ($alt == TRUE ? 'alt' : '');
print theme('vud_points', $cid, $type, NULL, $style);
exit();
}
else {
drupal_goto(drupal_get_destination());
}
}
}
/**
* Themeing functions for widget and points display.
* Each of the functions below has a separate description of its own.
*/
/**
* Preprocessing function for the widget in the alternate +1 style.
*/
function template_preprocess_vud_widget_alt(&$variables) {
global $user;
$cid = $variables['cid'];
$type = $variables['type'];
$tag = isset($variables['tag']) ? $variables['tag'] : variable_get('vud_tag', 'vote');
drupal_add_css(drupal_get_path('module', 'vud') .'/vud.css');
drupal_add_js(drupal_get_path('module', 'vud') .'/ajax_vud.js');
$variables['points'] = theme('vud_points', $cid, $type, $tag, 'alt');
if (user_access('use vote up/down') && ($user->uid)) {
$criteria = array(
'content_type' => $type,
'content_id' => $cid,
'uid' => _vud_get_uid(),
'tag' => $tag,
);
$user_vote = votingapi_select_single_vote_value($criteria);
if ($user_vote > 0) {
$variables['class'] = 'vote-up-act';
}
else {
$variables['class'] = 'vote-up-inact';
}
$token = drupal_get_token("vud/$type/$cid/1");
$variables['title'] = url("vud/$type/$cid/1/$tag/1", array('query' => 'token='. $token));
$variables['link'] = l('', "vud/$type/$cid/1/$tag", array(
'attributes' => array(
'class' => $variables['class'],
'title' => t('Vote up')
),
'query' => drupal_get_destination() .'&token='. $token,
)
);
}
else {
$variables['class'] = 'up-inact';
$variables['title'] = t('You must login to vote!');
}
}
/**
* Preprocess function for the points in the normal display style.
*/
function template_preprocess_vud_points(&$variables) {
$tag = isset($variables['tag']) ? $variables['tag'] : variable_get('vud_tag', 'vote');
$criteria = array(
'content_type' => $variables['type'],
'content_id' => $variables['cid'],
'value_type' => 'points',
'tag' => $tag,
'function' => 'sum'
);
$vote_result = (int)votingapi_select_single_result_value($criteria);
if ($vote_result > 0) {
$variables['class'] = 'positive';
$variables['points'] = '+'. $vote_result;
}
else {
$variables['points'] = $vote_result;
if ($vote_result < 0) {
$variables['class'] = 'negative';
}
else {
$variables['class'] = 'neutral';
}
}
$variables['label'] = format_plural($vote_result, 'point', 'points');
$variables['points_labelled'] = format_plural($vote_result, '1 point', '@count points');
if (isset($variables['style'])) {
$variables['template_files'][] = 'vud_points_'. $variables['style'];
}
}
/**
* Get a user ID for voting.
*/
function _vud_get_uid() {
global $user;
if ($user->uid) {
$uid = $user->uid;
}
else {
$uid = NULL;
}
return $uid;
}
/**
* Check whether the current user has access to the given permission for the
* given node. This requires both that they have access the required permission,
* and that the node type is enabled for vote up/down.
* @param $node
* A node object
* @param $perm
* (Optional) the name of a permission to pass to user_access()
* @return boolean TRUE if the user has access to the node vote up/down statistics.
*/
function vud_node_access($node, $perm = 'access vote up/down statistics') {
if (user_access($perm)) {
return in_array($node->type, variable_get('vud_node_types', array()), TRUE);
}
return FALSE;
}