forked from marvil07/vote_up_down
/
vud.module
182 lines (164 loc) · 5.49 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
<?php
/**
* @file
* Implements the core voting module on top of Voting API.
*/
// Include the theme.inc file.
module_load_include('inc', 'vud', 'vud.theme');
/**
* Implementation of hook_menu().
*/
function vud_menu() {
$items = array();
$items['admin/config/search/voteupdown/advanced'] = array(
'title' => 'General',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['vote/%/%/%/%/%/%'] = array(
'title' => 'Vote',
'page callback' => 'vud_vote',
'page arguments' => array(1, 2, 3, 4, 5, 6),
'access callback' => 'vud_access_callback',
'access arguments' => array('use vote up/down', 1, 2, 3, 4),
'type' => MENU_CALLBACK,
'file' => 'vud.theme.inc',
);
$items['votereset/%/%/%/%/%'] = array(
'title' => 'Reset vote',
'page callback' => 'vud_reset',
'page arguments' => array(1, 2, 3, 4, 5),
'access callback' => 'vud_access_callback',
'access arguments' => array('reset vote up/down votes', 1, 2, 3, 4),
'type' => MENU_CALLBACK,
'file' => 'vud.theme.inc',
);
$items['vud/%ctools_js/denied/%'] = array(
'title' => 'Vote denied',
'page callback' => 'vud_denied_vote',
'page arguments' => array(1, 3),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* 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;
}
}
/**
* Access callback for votes.
*
* @param $perm
* A string containing the permission required to modify the vote.
* @param $entity_type
* A string containing the type of content being voted on.
* @param $entity_id
* An integer containing the unique ID of the content being voted on.
* @param $value
* An integer containing the vote value, 1 for an up vote, -1 for a down vote.
* @param $tag
* A string containing the voting API tag.
* $param $account
* An object containing the user voting on the content, NULL for the current
* user.
*
* @return
* A boolean flagging whether or not the user has access to the vote.
*/
function vud_access_callback($perm, $entity_type, $entity_id, $value, $tag, $account = NULL) {
if ($account === NULL) {
global $user;
$account = $user;
}
// If user do not pass system permissions, deny soon.
if (user_access($perm, $account) !== TRUE) {
return FALSE;
}
// Invokes hook_vud_access(), gives modules ability to allow or disallow access.
$access_array = module_invoke_all('vud_access', $perm, $entity_type, $entity_id, $value, $tag, $account);
foreach ($access_array as $access_result) {
// If one hook implementation want to deny, end there.
if ($access_result !== TRUE) {
return FALSE;
}
}
// If we are here, account should pass.
return TRUE;
}
/**
* Menu callback; show widget message.
*/
function vud_denied_vote($js = FALSE, $code=VUD_WIDGET_MESSAGE_ERROR) {
$widget_message_codes = array(
VUD_WIDGET_MESSAGE_ERROR => t('Sorry, there was problem on the vote.'),
VUD_WIDGET_MESSAGE_DENIED => t('You are not allowed to vote.'),
);
drupal_alter('vud_widget_message_codes', $widget_message_codes);
if ($js) {
ctools_include('ajax');
ctools_include('modal');
ctools_modal_render('', $widget_message_codes[$code]);
}
else {
return $widget_message_codes[$code];
}
}
/**
* Implements hook_ctools_plugin_directory().
*/
function vud_ctools_plugin_directory($module, $type) {
// Safety: go away if CTools is not at an appropriate version.
if (!module_invoke('ctools', 'api_version', VUD_REQUIRED_CTOOLS_API)) {
return;
}
if ($module == 'vud' && $type == 'widgets') {
return 'widgets';
}
}
/**
* Implementation of votingapi hook_votingapi_results_alter().
*
* Add positive/negative aggregations for VotingAPI cache points.
*/
function vud_votingapi_results_alter(&$cache, $entity_type, $entity_id) {
// positive points
$sql = "SELECT SUM(v.value) as value_positives, v.tag ";
$sql .= "FROM {votingapi_vote} v ";
$sql .= "WHERE v.entity_type = :entity_type AND v.entity_id = :entity_id AND v.value_type = 'points' AND v.value > 0 ";
$sql .= "GROUP BY v.value_type, v.tag";
$result = db_query($sql, array(':entity_type' => $entity_type, ':entity_id' => $entity_id));
foreach ($result as $record) {
$cache[$record->tag]['points']['positives'] = $record->value_positives;
}
// negative points
$sql = "SELECT SUM(v.value) as value_negatives, v.tag ";
$sql .= "FROM {votingapi_vote} v ";
$sql .= "WHERE v.entity_type = :entity_type AND v.entity_id = :entity_id AND v.value_type = 'points' AND v.value < 0 ";
$sql .= "GROUP BY v.value_type, v.tag";
$result = db_query($sql, array(':entity_type' => $entity_type, ':entity_id' => $entity_id));
foreach ($result as $record) {
$cache[$record->tag]['points']['negatives'] = $record->value_negatives;
}
}
/**
* Implementation of hook_votingapi_metadata_alter().
*/
function vud_votingapi_metadata_alter(&$data) {
$data['functions']['positives'] = array(
'name' => t('Positives'),
'description' => t('The sum of all positive votes for a content.'),
'module' => 'vud',
);
$data['functions']['negatives'] = array(
'name' => t('Negatives'),
'description' => t('The sum of all negative votes for a content.'),
'module' => 'vud',
);
}