1 |
1 |
<?php
|
|
2 |
|
|
3 |
/**
|
|
4 |
* @file
|
|
5 |
* Pollfield module
|
|
6 |
*/
|
|
7 |
|
|
8 |
/**
|
|
9 |
* Implements hook_init().
|
|
10 |
*
|
|
11 |
* Executed at the begining of page requests to add custom css.
|
|
12 |
*/
|
|
13 |
function pollfield_init() {
|
|
14 |
$css_file = drupal_get_path('module', 'pollfield') . '/pollfield.css';
|
|
15 |
drupal_add_css($css_file, 'module');
|
|
16 |
}
|
|
17 |
|
|
18 |
/**
|
|
19 |
* Implements hook_menu().
|
|
20 |
*
|
|
21 |
* Provides a simple user interface that tells the developer where to go.
|
|
22 |
*/
|
|
23 |
function pollfield_menu() {
|
|
24 |
$items = array();
|
|
25 |
$items['pollfield/add_choice'] = array(
|
|
26 |
'title' => 'Add choice',
|
|
27 |
'page callback' => 'pollfield_add_choice',
|
|
28 |
'access arguments' => array('access content'),
|
|
29 |
'type' => MENU_CALLBACK,
|
|
30 |
);
|
|
31 |
return $items;
|
|
32 |
}
|
|
33 |
|
2 |
34 |
/**
|
3 |
35 |
* Implements hook_field_info().
|
4 |
36 |
*
|
5 |
37 |
* Provides the description of the field.
|
6 |
38 |
*/
|
7 |
39 |
function pollfield_field_info() {
|
8 |
|
|
9 |
40 |
return array(
|
10 |
41 |
// We name our field as the associative name of the array.
|
11 |
|
'pollfield_poll' => array(//this is name of field that evetrhing else is connected
|
|
42 |
// This is name of field that evetrhing else is connected.
|
|
43 |
'pollfield_poll' => array(
|
12 |
44 |
'label' => t('Pollfield module'),
|
13 |
45 |
'description' => t('This is to store poll as a field and not as a node.'),
|
14 |
|
'default_widget' => 'pollfield_main',//each field can have many widget
|
|
46 |
'default_widget' => 'pollfield_main', // Each field can have many widget.
|
15 |
47 |
'default_formatter' => 'pollfield_default',
|
16 |
48 |
),
|
17 |
49 |
);
|
18 |
|
|
19 |
50 |
}
|
20 |
|
/*
|
|
51 |
|
|
52 |
/**
|
21 |
53 |
* Implements hook_field_validate().
|
22 |
54 |
*
|
23 |
55 |
* This hook gives us a chance to validate content that's in our
|
... | ... | |
31 |
63 |
*/
|
32 |
64 |
function pollfield_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
|
33 |
65 |
foreach ($items as $delta => $item) {
|
34 |
|
|
35 |
|
|
|
66 |
|
36 |
67 |
}
|
37 |
68 |
}
|
38 |
69 |
|
... | ... | |
44 |
75 |
* the form API flag an error when required fields are empty.
|
45 |
76 |
*/
|
46 |
77 |
function pollfield_field_is_empty($item, $field) {
|
47 |
|
//for now field are always full
|
|
78 |
// For now field are always full.
|
48 |
79 |
return FALSE;
|
49 |
80 |
}
|
50 |
81 |
|
... | ... | |
58 |
89 |
* @see field_example_field_formatter_view()
|
59 |
90 |
*/
|
60 |
91 |
function pollfield_field_formatter_info() {
|
61 |
|
|
62 |
92 |
return array(
|
63 |
93 |
// This formatter just displays the hex value in the color indicated.
|
64 |
94 |
'pollfield_default' => array(
|
... | ... | |
87 |
117 |
}
|
88 |
118 |
|
89 |
119 |
/**
|
90 |
|
* hook field create
|
91 |
|
* used to serialize some pollfield settings
|
|
120 |
* Implements hook_field_presave().
|
|
121 |
*
|
|
122 |
* Used to serialize some pollfield settings.
|
92 |
123 |
*/
|
93 |
|
function pollfield_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
94 |
|
|
95 |
|
foreach ($items as $delta=>$item){
|
96 |
|
//poll settings
|
97 |
|
$poll_settings['poll_type'] = $item['poll_type'];
|
98 |
|
$poll_settings['delete_votes'] = $item['delete_votes'];
|
99 |
|
$poll_settings['cancelvote_allow'] = $item['cancelvote_allow'];
|
100 |
|
$poll_settings['result_allow'] = $item['result_allow'];
|
101 |
|
$items[$delta]['poll_features'] = serialize($poll_settings);
|
102 |
|
//chioces
|
103 |
|
//delete empty choices
|
104 |
|
$choices_items=array();
|
105 |
|
foreach ($item['group'] as $key => $value) {
|
106 |
|
if (strlen($value['choice'])<2){
|
107 |
|
|
108 |
|
}else{
|
109 |
|
$choices_items[]=$value;
|
110 |
|
}
|
111 |
|
}
|
112 |
|
$choices=serialize($choices_items);
|
113 |
|
$items[$delta]['choice'] = $choices;
|
114 |
|
}
|
115 |
|
}
|
116 |
|
|
|
124 |
function pollfield_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
|
125 |
foreach ($items as $delta => $item) {
|
|
126 |
// Poll settings.
|
|
127 |
$poll_settings = array(
|
|
128 |
'poll_type' => $item['poll_type'],
|
|
129 |
'cancelvote_allow' => $item['cancelvote_allow'],
|
|
130 |
'result_allow' => $item['result_allow'],
|
|
131 |
);
|
|
132 |
$items[$delta]['poll_features'] = serialize($poll_settings);
|
|
133 |
|
|
134 |
// Delete all votes.
|
|
135 |
if ($item['delete_votes'] && !empty($entity->nid)) {
|
|
136 |
db_delete('pollfield_votes')
|
|
137 |
->condition('nid', $entity->nid)
|
|
138 |
->execute();
|
|
139 |
}
|
|
140 |
|
|
141 |
// Delete empty choices.
|
|
142 |
$choices_items = array();
|
|
143 |
foreach ($item['group'] as $key => $value) {
|
|
144 |
$choice = trim($value['choice']);
|
|
145 |
if (!empty($choice)) {
|
|
146 |
$choices_items[$key] = $value;
|
|
147 |
}
|
|
148 |
}
|
|
149 |
$items[$delta]['choice'] = serialize($choices_items);
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
117 |
153 |
/**
|
118 |
154 |
* Implements hook_field_formatter_view().
|
119 |
155 |
*
|
... | ... | |
127 |
163 |
*/
|
128 |
164 |
function pollfield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
|
129 |
165 |
$element = array();
|
130 |
|
|
|
166 |
|
131 |
167 |
switch ($display['type']) {
|
132 |
168 |
// This formatter simply outputs the field as text and with a color.
|
133 |
169 |
case 'pollfield_default':
|
134 |
170 |
foreach ($items as $delta => $item) {
|
135 |
|
$elements['item']=$item;
|
136 |
|
$elements['entity']=$entity;
|
137 |
|
$elements['delta']=$delta;
|
138 |
|
$elements['field']=$field;
|
139 |
|
$output=theme('pollfield_default_formatter',array('elements'=>$elements));
|
|
171 |
$elements = array(
|
|
172 |
'item' => $item,
|
|
173 |
'entity' => $entity,
|
|
174 |
'delta' => $delta,
|
|
175 |
'field' => $field,
|
|
176 |
);
|
140 |
177 |
$element[$delta] = array(
|
141 |
|
// We create a render array to produce the desired markup,
|
142 |
|
// "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
|
143 |
|
// See theme_html_tag().
|
144 |
|
'#markup' => $output,
|
|
178 |
'#markup' => theme('pollfield_default_formatter', array('elements' => $elements)),
|
145 |
179 |
);
|
146 |
180 |
}
|
147 |
181 |
break;
|
... | ... | |
150 |
184 |
// background color. If there are many fields, the last one will win.
|
151 |
185 |
case 'pollfield_choices':
|
152 |
186 |
foreach ($items as $delta => $item) {
|
153 |
|
$element[$delta] = array(
|
154 |
|
// We create a render array to produce the desired markup,
|
155 |
|
// "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
|
156 |
|
// See theme_html_tag().
|
|
187 |
// We create a render array to produce the desired markup,
|
|
188 |
// "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
|
|
189 |
// See theme_html_tag().
|
|
190 |
$element[$delta] = array(
|
157 |
191 |
'#type' => 'html_tag',
|
158 |
192 |
'#tag' => 'p',
|
159 |
193 |
'#value' => t('This is choices formater'),
|
160 |
194 |
);
|
161 |
195 |
}
|
162 |
196 |
break;
|
163 |
|
case 'pollfield_question':
|
|
197 |
|
|
198 |
case 'pollfield_question':
|
164 |
199 |
foreach ($items as $delta => $item) {
|
165 |
|
$element[$delta] = array(
|
166 |
|
// We create a render array to produce the desired markup,
|
167 |
|
// "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
|
168 |
|
// See theme_html_tag().
|
|
200 |
// We create a render array to produce the desired markup,
|
|
201 |
// "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
|
|
202 |
// See theme_html_tag().
|
|
203 |
$element[$delta] = array(
|
169 |
204 |
'#type' => 'html_tag',
|
170 |
205 |
'#tag' => 'p',
|
171 |
206 |
'#value' => t('This is defualt formater'),
|
172 |
207 |
);
|
173 |
208 |
}
|
174 |
209 |
break;
|
175 |
|
case 'pollfield_results':
|
|
210 |
|
|
211 |
case 'pollfield_results':
|
176 |
212 |
foreach ($items as $delta => $item) {
|
177 |
|
$element[$delta] = array(
|
178 |
|
// We create a render array to produce the desired markup,
|
179 |
|
// "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
|
180 |
|
// See theme_html_tag().
|
|
213 |
// We create a render array to produce the desired markup,
|
|
214 |
// "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
|
|
215 |
// See theme_html_tag().
|
|
216 |
$element[$delta] = array(
|
181 |
217 |
'#type' => 'html_tag',
|
182 |
218 |
'#tag' => 'p',
|
183 |
219 |
'#value' => t('This is results formater'),
|
184 |
220 |
);
|
185 |
221 |
}
|
186 |
222 |
break;
|
187 |
|
case 'pollfield_runtime':
|
|
223 |
|
|
224 |
case 'pollfield_runtime':
|
188 |
225 |
foreach ($items as $delta => $item) {
|
189 |
|
$element[$delta] = array(
|
190 |
|
// We create a render array to produce the desired markup,
|
191 |
|
// "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
|
192 |
|
// See theme_html_tag().
|
|
226 |
// We create a render array to produce the desired markup,
|
|
227 |
// "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
|
|
228 |
// See theme_html_tag().
|
|
229 |
$element[$delta] = array(
|
193 |
230 |
'#type' => 'html_tag',
|
194 |
231 |
'#tag' => 'p',
|
195 |
232 |
'#value' => t('This is runtime formater'),
|
... | ... | |
200 |
237 |
|
201 |
238 |
return $element;
|
202 |
239 |
}
|
|
240 |
|
203 |
241 |
/**
|
204 |
242 |
* Implements hook_field_widget_info().
|
205 |
243 |
*
|
... | ... | |
222 |
260 |
),
|
223 |
261 |
);
|
224 |
262 |
}
|
|
263 |
|
225 |
264 |
/**
|
226 |
265 |
* Implements hook_field_widget_form().
|
227 |
266 |
*
|
... | ... | |
230 |
269 |
*
|
231 |
270 |
* We provide one of three different forms, depending on the widget type of
|
232 |
271 |
* the Form API item provided.
|
233 |
|
*
|
234 |
272 |
*/
|
235 |
273 |
function pollfield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
|
236 |
274 |
$value = isset($items[$delta]['question']) ? $items[$delta]['question'] : '';
|
237 |
|
|
|
275 |
|
238 |
276 |
$widget = $element;
|
239 |
277 |
$widget['#delta'] = $delta;
|
|
278 |
|
|
279 |
if (isset($form['#node'])) {
|
|
280 |
$node = $form['#node'];
|
|
281 |
}
|
|
282 |
//dpr($form);
|
|
283 |
if (!empty($items[$delta]['poll_features'])) {
|
|
284 |
if (is_array($items[$delta]['poll_features'])) {
|
|
285 |
$poll_features = $items[$delta]['poll_features'];
|
|
286 |
}
|
|
287 |
else {
|
|
288 |
$poll_features = unserialize($items[$delta]['poll_features']);
|
|
289 |
}
|
|
290 |
}
|
240 |
291 |
|
241 |
292 |
switch ($instance['widget']['type']) {
|
242 |
|
|
243 |
293 |
case 'pollfield_main':
|
244 |
|
|
245 |
|
//question
|
|
294 |
// Question.
|
246 |
295 |
$widget['question'] = array(
|
247 |
296 |
'#type' => 'textfield',
|
248 |
297 |
'#title' => t('Question'),
|
249 |
|
'#default_value'=>$value,
|
|
298 |
'#default_value' => $value,
|
|
299 |
);
|
|
300 |
// Status.
|
|
301 |
$admin_pollfield = TRUE;
|
|
302 |
$_active = array(
|
|
303 |
t('Closed'),
|
|
304 |
t('Active'),
|
|
305 |
);
|
|
306 |
$widget['active'] = array(
|
|
307 |
'#type' => 'radios',
|
|
308 |
'#access' => $admin_pollfield,
|
|
309 |
'#title' => t('Status'),
|
|
310 |
// If no value exists, always default to active (1).
|
|
311 |
'#default_value' => isset($items[$delta]['active']) ? $items[$delta]['active'] : 1,
|
|
312 |
'#options' => $_active,
|
|
313 |
'#description' => t('When a pollfield is closed, visitors can no longer vote for it.'),
|
|
314 |
);
|
|
315 |
// Runtime - duration.
|
|
316 |
$widget['runtime'] = array(
|
|
317 |
'#type' => 'select',
|
|
318 |
'#access' => $admin_pollfield,
|
|
319 |
'#title' => t('Duration'),
|
|
320 |
// If no value exists, always default to Unlimited (0).
|
|
321 |
'#default_value' => isset($items[$delta]['runtime']) ? $items[$delta]['runtime'] : 0,
|
|
322 |
'#options' => pollfield_duration_options(),
|
|
323 |
'#description' => t('After this period, the pollfield will be closed automatically.'),
|
|
324 |
);
|
|
325 |
// Anonymous voting.
|
|
326 |
$widget['anonymous'] = array(
|
|
327 |
'#type' => 'select',
|
|
328 |
'#access' => $admin_pollfield,
|
|
329 |
'#title' => t('Anonymous voting'),
|
|
330 |
// If no value exists, always default to Not allowed (anon_non).
|
|
331 |
'#default_value' => isset($items[$delta]['anonymous']) ? $items[$delta]['anonymous'] : 'anon_non',
|
|
332 |
'#options' => array(
|
|
333 |
'anon_non' => t('Not allowed'),
|
|
334 |
'anon_by_ip' => t('Allowed, filtered by user IP'),
|
|
335 |
'anon_by_cookie' => t('Allowed, filtered by user cookie'),
|
|
336 |
'anon_all' => t('Allowed'),
|
|
337 |
),
|
|
338 |
'#description' => t('Allow or disallow anonymous users to vote on this poll. Filtering by user IP or cookie ensures only one vote is allowed per user IP.'),
|
|
339 |
);
|
|
340 |
// Cancel permission.
|
|
341 |
$options_allow = array(
|
|
342 |
t('Disallow'),
|
|
343 |
t('Allow'),
|
|
344 |
);
|
|
345 |
$widget['cancelvote_allow'] = array(
|
|
346 |
'#type' => 'radios',
|
|
347 |
'#access' => $admin_pollfield,
|
|
348 |
'#title' => t('Cancel Vote'),
|
|
349 |
'#default_value' => isset($poll_features['cancelvote_allow']) ? $poll_features['cancelvote_allow'] : 1,
|
|
350 |
'#options' => $options_allow,
|
|
351 |
'#description' => t('Allow or disallow "Cancel vote" button. "Cancel vote" button allows voters to cancel their vote and choose a different one if they want.'),
|
|
352 |
);
|
|
353 |
// Show result befor a voting.
|
|
354 |
$widget['result_allow'] = array(
|
|
355 |
'#type' => 'radios',
|
|
356 |
'#access' => $admin_pollfield,
|
|
357 |
'#title' => t('Show results before voting'),
|
|
358 |
'#default_value' => isset($poll_features['result_allow']) ? $poll_features['result_allow'] : 0,
|
|
359 |
'#options' => $options_allow,
|
|
360 |
'#description' => t('Allow or disallow users to see current poll results before voting on this poll. If set to disallow, anonymous users can never see the results. Permissions under pollfield module override this setting.'),
|
250 |
361 |
);
|
251 |
|
//status
|
252 |
|
$admin_pollfield=TRUE;
|
253 |
|
$_active = array(0 => t('Closed'), 1 => t('Active'));
|
254 |
|
$widget['active']= array(
|
255 |
|
'#type' => 'radios',
|
256 |
|
'#access' => $admin_pollfield,
|
257 |
|
'#title' => t('Status'),
|
258 |
|
// If no value exists, always default to active (1)
|
259 |
|
'#default_value' => isset($items[$delta]['active']) ? $items[$delta]['active'] : 0,
|
260 |
|
'#options' => $_active,
|
261 |
|
'#description' => t('When a pollfield is closed, visitors can no longer vote for it.')
|
262 |
|
);
|
263 |
|
//runtime - duration
|
264 |
|
$widget['runtime'] = array(
|
265 |
|
'#type' => 'select',
|
266 |
|
'#access' => $admin_pollfield,
|
267 |
|
'#title' => t('Duration'),
|
268 |
|
// If no value exists, always default to Unlimited (0)
|
269 |
|
'#default_value' => isset($items[$delta]['runtime']) ? $items[$delta]['runtime'] : 0,
|
270 |
|
'#options' => pollfield_duration_options(),
|
271 |
|
'#description' => t('After this period, the pollfield will be closed automatically.')
|
272 |
|
);
|
273 |
|
//anonymous voting
|
274 |
|
$widget['anonymous'] = array(
|
275 |
|
'#type' => 'select',
|
276 |
|
'#access' => $admin_pollfield,
|
277 |
|
'#title' => t('Anonymous voting'),
|
278 |
|
// If no value exists, always default to Not allowed (anon_non)
|
279 |
|
'#default_value' => isset($items[$delta]['anonymous']) ? $items[$delta]['anonymous'] : 'anon_non',
|
280 |
|
'#options' => array(
|
281 |
|
'anon_non' => t('Not allowed'),
|
282 |
|
'anon_by_ip' => t('Allowed, filtered by user IP'),
|
283 |
|
'anon_by_cookie' => t('Allowed filtered by user cookie'),
|
284 |
|
'anon_all' => t('Allowed')
|
285 |
|
),
|
286 |
|
'#description' => t('Allow or disallow anonymous users to vote on this poll. Filtering by user IP or cookie ensures only one vote is allowed per user IP.')
|
287 |
|
);
|
288 |
|
//cancel permission
|
289 |
|
$_cancelvote = array(0 => t('Disallow'), 1 => t('Allow'));
|
290 |
|
$widget['cancelvote_allow'] = array(
|
291 |
|
'#type' => 'radios',
|
292 |
|
'#access' => $admin_pollfield,
|
293 |
|
'#title' => t('Cancel Vote'),
|
294 |
|
'#default_value' => isset($items[$delta]['cancelvote_allow']) ? $items[$delta]['cancelvote_allow'] : 1,
|
295 |
|
'#options' => $_cancelvote,
|
296 |
|
'#description' => t('Allow or disallow "Cancel vote" button. "Cancel vote" button allows voters to cancel their vote and choose a different one if they want.'),
|
297 |
|
);
|
298 |
|
//show result befora voting
|
299 |
|
$_cancelvote = array(0 => t('Disallow'), 1 => t('Allow'));
|
300 |
|
$widget['result_allow'] = array(
|
301 |
|
'#type' => 'radios',
|
302 |
|
'#access' => $admin_pollfield,
|
303 |
|
'#title' => t('Show results before voting'),
|
304 |
|
'#default_value' => isset($items[$delta]['cancelvote_allow']) ? $items[$delta]['cancelvote_allow'] : 0,
|
305 |
|
'#options' => $_cancelvote,
|
306 |
|
'#description' => t('Allow or disallow users to see current poll results before voting on this poll. If set to disallow, anonymous users can never see the results. Permissions under pollfield module override this setting.'),
|
307 |
|
);
|
308 |
|
|
309 |
|
// Poll type
|
310 |
|
// Can users vote on multiple choices or just one?
|
311 |
|
$_poll_type = array(0 => t('Single choice'), 1 => t('Multiple choice'));
|
312 |
|
$widget['poll_type'] = array(
|
313 |
|
'#type' => 'radios',
|
314 |
|
'#title' => t('Poll type'),
|
315 |
|
'#default_value' => isset($poll_type) ? $poll_type : 1,
|
316 |
|
'#options' => $_poll_type,
|
317 |
|
'#description' => t('In a Single choice poll only one of the responses can be voted for while in a Multiple choice poll it is possible to choose more than one at the same time.'),
|
318 |
|
);
|
319 |
|
//delete votes from this poll
|
320 |
|
$_delete_votes = array(0 => t('Not delete'), 1 => t('Delete'));
|
321 |
|
$widget['delete_votes'] = array(
|
322 |
|
'#type' => 'checkbox',
|
323 |
|
'#title' => t('Delete all votes'),
|
324 |
|
'#description' => t('Delete all votes on this poll. THIS ACTION CAN NOT BE UNDONE!'),
|
325 |
|
'#default_value' => 0,
|
326 |
|
'#access' => $admin_pollfield,
|
327 |
|
);
|
328 |
|
|
329 |
|
//serialize features
|
330 |
|
$widget['poll_features']= array(
|
331 |
|
'#type' => 'hidden',
|
332 |
|
'#value' => isset($items[$delta]['poll_features']) ? $items[$delta]['poll_features'] : 0,
|
333 |
|
);
|
334 |
|
$widget['choice']= array(
|
335 |
|
'#type' => 'hidden',
|
336 |
|
'#value' => isset($items[$delta]['choice']) ? $items[$delta]['choice'] : 0,
|
337 |
|
);
|
338 |
|
|
339 |
|
//choices
|
340 |
|
$choices = unserialize($items[$delta]['choice']);
|
341 |
|
$number_of_choices = count($choices);
|
342 |
|
$number_in_data_base = count($choices);
|
343 |
|
//ajax wrapper
|
344 |
|
$wrapper = 'ajax-wrapper-pollfield-'.$form_state['node']->nid.'-'.$delta;
|
345 |
|
$field_name = $field['field_name'];
|
346 |
|
|
347 |
|
if (!isset($form_state['count_choices'])){
|
348 |
|
$form_state['count_choices']=$number_of_choices;
|
349 |
|
}else{
|
350 |
|
$number_of_choices=$form_state['count_choices'];
|
351 |
|
}
|
352 |
|
//forming field definition for form return in ajax
|
353 |
|
$form_state['field_name']=$field_name;
|
354 |
|
|
355 |
|
foreach ($form['#node']->$field_name as $lang => $fileds){
|
356 |
|
$field_lang=$lang;
|
357 |
|
}
|
358 |
|
$form_state['field_lang']=$field_lang;
|
359 |
|
$form_state['field_delta']=$delta;
|
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
|
364 |
|
$widget['group'] = array(
|
365 |
|
'#prefix' => '<div id="'.$wrapper.'">',
|
366 |
|
'#suffix' => '</div>',
|
367 |
|
);
|
368 |
|
$count_chioces=0;
|
369 |
|
|
370 |
|
for ($i=0;$i<$number_of_choices+1;$i++){
|
371 |
|
|
372 |
|
if (true){
|
373 |
|
$widget['group'][$count_chioces] = array(
|
374 |
|
'#type' => 'fieldset',
|
375 |
|
'#title' => t('Choice #%delta', array('%delta' => intval($i + 1))),
|
376 |
|
'#tree' => TRUE,
|
377 |
|
'#description' => t('If you want to delete it just delete text from Response textfield.')
|
378 |
|
);
|
379 |
|
if ($count_chioces<$number_in_data_base){
|
380 |
|
$defaule_value = isset($choices[$i]['choice']) ? $choices[$i]['choice'] : '';
|
381 |
|
}
|
382 |
|
else {
|
383 |
|
$defaule_value = '';
|
384 |
|
}
|
385 |
|
$widget['group'][$count_chioces]['choice'] = array(
|
386 |
|
'#title' => t('Response'),
|
387 |
|
'#type' => 'textfield',
|
388 |
|
'#default_value' => $defaule_value,
|
389 |
|
'#rows' => 2,
|
390 |
|
'#weight' => $count_chioces,
|
391 |
|
|
392 |
|
);
|
393 |
|
|
394 |
|
if ($count_chioces<$number_in_data_base){
|
395 |
|
$defaule_value = isset($choices[$count_chioces]['votes']) ? $choices[$count_chioces]['votes'] : '';
|
396 |
|
}
|
397 |
|
else {
|
398 |
|
$defaule_value = '';
|
399 |
|
}
|
400 |
|
$widget['group'][$count_chioces]['votes'] = array(
|
401 |
|
'#title' => t('Starting votes count (optional)'),
|
402 |
|
'#access' => $admin_pollfield,
|
403 |
|
'#type' => 'textfield',
|
404 |
|
'#default_value' => $defaule_value,
|
405 |
|
'#element_validate' => array('_pollfield_is_digits_validate'),
|
406 |
|
|
407 |
|
'#size' => 10,
|
408 |
|
'#weight' => $count_chioces+1,
|
409 |
|
);
|
410 |
|
|
411 |
|
$count_chioces++;
|
412 |
|
}
|
413 |
|
}
|
414 |
|
|
415 |
|
|
416 |
|
|
417 |
|
$widget['add-more'] = array(
|
418 |
|
'#type' => 'submit',
|
419 |
|
'#value' => t('More choices'),
|
420 |
|
'#description' => t("If the amount of boxes above isn't enough, click here to add more choices."),
|
421 |
|
'#submit'=>array('pollfield_more_choice_callback_submit'),
|
422 |
|
|
423 |
|
'#ajax' => array(
|
424 |
|
// #ajax has two required keys: callback and wrapper.
|
425 |
|
// 'callback' is a function that will be called when this element changes.
|
426 |
|
'callback' => 'pollfield_more_choice_callback_js',
|
427 |
|
// 'wrapper' is the HTML id of the page element that will be replaced.
|
428 |
|
'wrapper' => $wrapper,
|
429 |
|
// There are also several optional keys - see ajax_example_autocheckboxes
|
430 |
|
// below for details on 'method', 'effect' and 'speed' and
|
431 |
|
// ajax_example_dependent_dropdown for 'event'.
|
432 |
|
),
|
433 |
|
);
|
434 |
|
|
435 |
|
break;
|
436 |
|
|
|
362 |
|
|
363 |
// Poll type
|
|
364 |
// Can users vote on multiple choices or just one?
|
|
365 |
$_poll_type = array(
|
|
366 |
'single' => t('Single choice'),
|
|
367 |
'multiple' => t('Multiple choice'),
|
|
368 |
);
|
|
369 |
$widget['poll_type'] = array(
|
|
370 |
'#type' => 'radios',
|
|
371 |
'#title' => t('Poll type'),
|
|
372 |
'#default_value' => isset($poll_features['poll_type']) ? $poll_features['poll_type'] : 'multiple',
|
|
373 |
'#options' => $_poll_type,
|
|
374 |
'#description' => t('In a Single choice poll only one of the responses can be voted for while in a Multiple choice poll it is possible to choose more than one at the same time.'),
|
|
375 |
);
|
|
376 |
|
|
377 |
// Delete votes from this poll.
|
|
378 |
$widget['delete_votes'] = array(
|
|
379 |
'#type' => 'checkbox',
|
|
380 |
'#title' => t('Delete all votes'),
|
|
381 |
'#description' => t('Delete all votes on this poll. <strong>This action can not be undone!</strong>'),
|
|
382 |
'#default_value' => FALSE,
|
|
383 |
'#access' => $admin_pollfield,
|
|
384 |
);
|
|
385 |
|
|
386 |
$widget['poll_features'] = array(
|
|
387 |
'#type' => 'value',
|
|
388 |
'#value' => isset($poll_features) ? $poll_features : NULL,
|
|
389 |
);
|
|
390 |
$widget['choice'] = array(
|
|
391 |
'#type' => 'value',
|
|
392 |
'#value' => isset($items[$delta]['choice']) ? $items[$delta]['choice'] : array(),
|
|
393 |
);
|
|
394 |
|
|
395 |
// Choices.
|
|
396 |
$choices = !empty($items[$delta]['choice']) ? unserialize($items[$delta]['choice']) : array();
|
|
397 |
$number_in_data_base = $number_of_choices = count($choices);
|
|
398 |
// Ajax wrapper.
|
|
399 |
$wrapper = 'pollfield-ajax-wrapper-' . $delta;
|
|
400 |
$field_name = $field['field_name'];
|
|
401 |
if (isset($form_state['count_choices'])) {
|
|
402 |
$number_of_choices = $form_state['count_choices'];
|
|
403 |
}
|
|
404 |
else {
|
|
405 |
$form_state['count_choices'] = $number_of_choices;
|
|
406 |
}
|
|
407 |
// Forming field definition for form return in ajax.
|
|
408 |
$form_state['field_name'] = $field_name;
|
|
409 |
|
|
410 |
if (isset($node->$field_name)) {
|
|
411 |
global $language;
|
|
412 |
if (isset($node->{$field_name}[$language->language])) {
|
|
413 |
$field_lang = $node->{$field_name}[$language->language];
|
|
414 |
}
|
|
415 |
else {
|
|
416 |
$field_lang = LANGUAGE_NONE;
|
|
417 |
}
|
|
418 |
}
|
|
419 |
else {
|
|
420 |
$field_lang = LANGUAGE_NONE;
|
|
421 |
}
|
|
422 |
$form_state['field_lang'] = $field_lang;
|
|
423 |
$form_state['field_delta'] = $delta;
|
|
424 |
|
|
425 |
$widget['group'] = array(
|
|
426 |
'#prefix' => '<div id="' . $wrapper . '">',
|
|
427 |
'#suffix' => '</div>',
|
|
428 |
);
|
|
429 |
|
|
430 |
for ($i = 1; $i <= ($number_of_choices + 1); $i++) {
|
|
431 |
$widget['group'][$i] = array(
|
|
432 |
'#type' => 'fieldset',
|
|
433 |
'#title' => t('Choice #%delta', array('%delta' => $i)),
|
|
434 |
'#collapsible' => TRUE,
|
|
435 |
'#collapsed' => FALSE,
|
|
436 |
'#weight' => $i,
|
|
437 |
);
|
|
438 |
$widget['group'][$i]['choice'] = array(
|
|
439 |
'#title' => t('Response'),
|
|
440 |
'#type' => 'textfield',
|
|
441 |
'#default_value' => isset($choices[$i]['choice']) ? $choices[$i]['choice'] : '',
|
|
442 |
'#rows' => 2,
|
|
443 |
'#description' => t('If you want to delete this choice leave the field empty.'),
|
|
444 |
);
|
|
445 |
$widget['group'][$i]['votes'] = array(
|
|
446 |
'#title' => t('Starting votes count (optional)'),
|
|
447 |
'#access' => $admin_pollfield,
|
|
448 |
'#type' => 'textfield',
|
|
449 |
'#default_value' => isset($choices[$i]['votes']) ? $choices[$i]['votes'] : '',
|
|
450 |
'#element_validate' => array('_pollfield_is_digits_validate'),
|
|
451 |
'#size' => 10,
|
|
452 |
);
|
|
453 |
}
|
|
454 |
|
|
455 |
$widget['add-more'] = array(
|
|
456 |
'#type' => 'submit',
|
|
457 |
'#value' => t('More choices'),
|
|
458 |
'#description' => t("If the amount of boxes above isn't enough, click here to add more choices."),
|
|
459 |
'#submit' => array('pollfield_more_choice_callback_submit'),
|
|
460 |
'#ajax' => array(
|
|
461 |
'callback' => 'pollfield_more_choice_callback_js',
|
|
462 |
'wrapper' => $wrapper,
|
|
463 |
),
|
|
464 |
);
|
|
465 |
break;
|
437 |
466 |
}
|
438 |
|
|
439 |
|
//question is name of field in field database
|
|
467 |
|
|
468 |
// Question is name of field in field database.
|
440 |
469 |
$element['question'] = $widget['question'];
|
441 |
|
$element['active'] = $widget['active'];
|
442 |
|
$element['anonymous'] = $widget['anonymous'];
|
443 |
|
$element['runtime']= $widget['runtime'];
|
444 |
|
$element['poll_type']= $widget['poll_type'];
|
445 |
|
$element['cancelvote_allow'] = $widget['cancelvote_allow'];
|
446 |
|
$element['result_allow'] = $widget['result_allow'];
|
447 |
|
$element['delete_votes'] = $widget['delete_votes'];
|
448 |
|
$element['group'] = $widget['group'];
|
449 |
|
$element['add-more'] = $widget['add-more'];
|
450 |
|
//hidden elements for serialized fields
|
451 |
|
$element['poll_features'] = $widget['poll_features'];
|
452 |
|
$element['choice'] = $widget['choice'];
|
453 |
|
return $element;
|
|
470 |
$element['active'] = $widget['active'];
|
|
471 |
$element['anonymous'] = $widget['anonymous'];
|
|
472 |
$element['runtime'] = $widget['runtime'];
|
|
473 |
$element['poll_type'] = $widget['poll_type'];
|
|
474 |
$element['cancelvote_allow'] = $widget['cancelvote_allow'];
|
|
475 |
$element['result_allow'] = $widget['result_allow'];
|
|
476 |
$element['delete_votes'] = $widget['delete_votes'];
|
|
477 |
$element['group'] = $widget['group'];
|
|
478 |
$element['add-more'] = $widget['add-more'];
|
|
479 |
// Hidden elements for serialized fields.
|
|
480 |
$element['poll_features'] = $widget['poll_features'];
|
|
481 |
$element['choice'] = $widget['choice'];
|
|
482 |
|
|
483 |
return $element;
|
454 |
484 |
}
|
455 |
485 |
|
456 |
486 |
/**
|
457 |
487 |
* ajax more choice callback
|
458 |
488 |
*/
|
459 |
|
function pollfield_more_choice_callback_js($form, $form_state){
|
460 |
|
$field_name = $form_state['field_name'];
|
461 |
|
$field_lang = $form_state['field_lang'];
|
462 |
|
$field_delta = $form_state['field_delta'];
|
463 |
|
$form_choices = $form[$field_name][$field_lang][$field_delta]['group'];
|
464 |
|
|
465 |
|
return $form_choices;
|
|
489 |
function pollfield_more_choice_callback_js($form, $form_state) {
|
|
490 |
$field = $form[$form_state['field_name']];
|
|
491 |
$lang = $form_state['field_lang'];
|
|
492 |
if (!isset($field[$lang])) {
|
|
493 |
$lang = LANGUAGE_NONE;
|
|
494 |
}
|
|
495 |
$delta = $form_state['field_delta'];
|
|
496 |
return $field[$lang][$delta]['group'];
|
466 |
497 |
}
|
467 |
|
|
|
498 |
|
468 |
499 |
/**
|
469 |
500 |
* submit more choice callback
|
470 |
501 |
*/
|
471 |
|
function pollfield_more_choice_callback_submit($form, &$form_state){
|
472 |
|
$form_state['count_choices']=$form_state['count_choices']+1;
|
473 |
|
|
474 |
|
$form_state['rebuild'] = TRUE;
|
|
502 |
function pollfield_more_choice_callback_submit($form, &$form_state) {
|
|
503 |
$form_state['count_choices'] += 1;
|
|
504 |
$form_state['rebuild'] = TRUE;
|
475 |
505 |
}
|
476 |
506 |
|
477 |
507 |
/**
|
... | ... | |
485 |
515 |
* @see form_error()
|
486 |
516 |
*/
|
487 |
517 |
function pollfield_field_widget_error($element, $error, $form, &$form_state) {
|
488 |
|
|
489 |
518 |
switch ($error['error']) {
|
490 |
519 |
case 'field_example_invalid':
|
491 |
520 |
form_error($element, $error['message']);
|
... | ... | |
493 |
522 |
}
|
494 |
523 |
}
|
495 |
524 |
|
496 |
|
|
497 |
525 |
/**
|
498 |
|
* Implements hook_menu().
|
499 |
|
*
|
500 |
|
* Provides a simple user interface that tells the developer where to go.
|
501 |
|
*/
|
502 |
|
function pollfield_menu() {
|
503 |
|
$items = array();
|
504 |
|
$items['pollfield/add_choice'] = array(
|
505 |
|
'title' => 'Add choice',
|
506 |
|
'page callback' => 'pollfield_add_choice',
|
507 |
|
'access arguments' => array('access content'),
|
508 |
|
'type' => MENU_CALLBACK,
|
509 |
|
);
|
510 |
|
return $items;
|
511 |
|
}
|
512 |
|
/**
|
513 |
|
* Save vote vote information.
|
514 |
|
*
|
515 |
|
* @param $form_values
|
516 |
|
* Array of input to the vote form.
|
517 |
|
* @param $type
|
518 |
|
* String of the vote type. Single or multiple choice.
|
519 |
|
* @param $cookie_db
|
520 |
|
* User cookie information. Used for voter checking.
|
521 |
|
*
|
522 |
|
* @return
|
523 |
|
* Always seems to return FALSE
|
524 |
|
* :TODO: figure out if this should return something more valuable
|
|
526 |
* Save vote information.
|
525 |
527 |
*/
|
526 |
528 |
function pollfield_save_choice($vote, $node) {
|
|
529 |
// Get total votes.
|
|
530 |
$field = field_get_items('node', $node, $vote->field_name);
|
|
531 |
$total_votes = isset($field[0]['votes']) ? $field[0]['votes'] : 0;
|
527 |
532 |
|
528 |
|
$voted = FALSE;
|
529 |
|
|
530 |
|
$table = 'field_revision_'.$vote->field_name;
|
531 |
|
$table_data = 'field_data_'.$vote->field_name;
|
532 |
|
// Get total votes
|
533 |
|
$field_votes = $vote->field_name .'_votes';
|
534 |
|
$query = "SELECT ".$field_votes." FROM {".$table."} WHERE entity_id = ".$vote->nid." AND delta = ".$vote->field_name_delta;
|
535 |
|
$total_votes = db_query($query)->fetchField();
|
536 |
|
|
537 |
|
$vote->table=$table;
|
|
533 |
$vote->table = 'field_revision_' . $vote->field_name;
|
538 |
534 |
// Make sure the voter filled out the poll form correctly.
|
539 |
535 |
$vote_error = FALSE;
|
|
536 |
|
540 |
537 |
if ($vote->type == 'single' && !isset($vote->choice)) {
|
541 |
538 |
$vote_error = TRUE;
|
542 |
539 |
}
|
543 |
540 |
else {
|
544 |
|
$choices_selected = count($vote->choice);
|
545 |
|
if ($choices_selected == 0) {
|
|
541 |
if (!isset($vote->choice) || !count($vote->choice)) {
|
546 |
542 |
$vote_error = TRUE;
|
547 |
543 |
}
|
548 |
544 |
}
|
549 |
545 |
if ($vote_error) {
|
550 |
|
drupal_set_message(t('Choice is not selected. Select your answer and vote.'));
|
551 |
|
return 0;
|
|
546 |
drupal_set_message(t('Choice is not selected. Select your answer and vote.'), 'warning');
|
|
547 |
return FALSE;
|
552 |
548 |
}
|
553 |
|
|
554 |
|
// Check if the user has already voted on this pollfield
|
555 |
|
$elements['entity']=$node;$elements['field']['field_name']=$vote->field_name;$elements['delta']=$vote->field_name_delta;
|
556 |
|
$elements['item']=$vote->item;
|
557 |
|
$voted = pollfield_user_voted($elements);
|
558 |
|
|
559 |
|
if ($voted == 0) {
|
560 |
|
|
561 |
|
// Save a single vote or pass or loop through an array of votes
|
562 |
|
// to save for multiple choice polls
|
563 |
|
if ($vote->type == 'single') {
|
564 |
|
$choice = $vote->choice;
|
565 |
|
$record_vote = pollfield_save_vote_record($vote, $choice);
|
566 |
|
}
|
567 |
|
else {
|
568 |
|
$choices = $vote->choice;
|
569 |
|
foreach ($choices as $choice) {
|
570 |
|
$record_vote = pollfield_save_vote_record($vote, $choice);
|
571 |
|
}
|
|
549 |
|
|
550 |
// Check if the user has already voted on this pollfield.
|
|
551 |
$elements['entity'] = $node;
|
|
552 |
$elements['field']['field_name'] = $vote->field_name;
|
|
553 |
$elements['delta'] = $vote->field_name_delta;
|
|
554 |
$elements['item'] = $vote->item;
|
|
555 |
|
|
556 |
if (!pollfield_user_voted($elements)) {
|
|
557 |
// Save votes.
|
|
558 |
foreach ($vote->choice as $choice) {
|
|
559 |
pollfield_save_vote_record($vote, $choice);
|
572 |
560 |
}
|
573 |
561 |
|
574 |
|
// Increment total votes for this pollfield
|
|
562 |
// Increment total votes for this pollfield.
|
575 |
563 |
$total_votes++;
|
576 |
|
$query = "UPDATE {".$table."} SET ".$field_votes." = ".$total_votes." WHERE entity_id = ".$vote->nid." AND delta=".$vote->field_name_delta;
|
577 |
|
db_query($query);
|
578 |
|
$query = "UPDATE {".$table_data."} SET ".$field_votes." = ".$total_votes." WHERE entity_id = ".$vote->nid." AND delta=".$vote->field_name_delta;
|
579 |
|
db_query($query);
|
580 |
|
|
581 |
|
// Any time a vote is recorded, clear the CCK cache so the votes can be updated.
|
|
564 |
$node->{$vote->field_name}[LANGUAGE_NONE][0]['votes'] = $total_votes;
|
|
565 |
field_attach_update('node', $node);
|
|
566 |
|
|
567 |
// Any time a vote is recorded, clear the CCK cache
|
|
568 |
// so the votes can be updated.
|
582 |
569 |
pollfield_clear($node);
|
583 |
570 |
|
584 |
571 |
drupal_set_message(t('Your vote was recorded.'));
|
585 |
572 |
}
|
586 |
573 |
else {
|
587 |
574 |
drupal_set_message(t('You already voted or you are not allowed to vote'), 'error');
|
|
575 |
return FALSE;
|
588 |
576 |
}
|
589 |
|
return 0;
|
|
577 |
return TRUE;
|
590 |
578 |
}
|
591 |
579 |
|
592 |
580 |
/**
|
593 |
|
* Write a vote to the database
|
|
581 |
* Write a vote to the database.
|
594 |
582 |
*/
|
595 |
583 |
function pollfield_save_vote_record($vote, $choice) {
|
596 |
|
|
597 |
|
// Store user id if the user is logged in
|
|
584 |
// Store user id if the user is logged in.
|
598 |
585 |
if ($vote->uid > 0) {
|
599 |
|
db_query("INSERT INTO {pollfield_votes} (nid, field_table, field_name, uid, delta, field_name_delta)
|
600 |
|
VALUES (:nid, :table, :field_name , :uid, :choice, :delta)",
|
601 |
|
array(':nid'=>$vote->nid, ':table'=>$vote->table, ':field_name'=>$vote->field_name, ':uid'=>$vote->uid, ':choice'=>$choice, ':delta'=>$vote->field_name_delta));
|
|
586 |
db_insert('pollfield_votes')
|
|
587 |
->fields(array(
|
|
588 |
'nid' => $vote->nid,
|
|
589 |
'field_table' => $vote->table,
|
|
590 |
'field_name' => $vote->field_name,
|
|
591 |
'uid' => $vote->uid,
|
|
592 |
'delta' => $choice,
|
|
593 |
'field_name_delta' => $vote->field_name_delta,
|
|
594 |
))
|
|
595 |
->execute();
|
602 |
596 |
}
|
603 |
|
// Store IP or cookie if the user is anonymous
|
|
597 |
// Store IP or cookie if the user is anonymous.
|
604 |
598 |
else {
|
605 |
|
db_query("INSERT INTO {pollfield_votes} (nid, field_table, field_name, uid, delta, hostname, field_name_delta, cookie)
|
606 |
|
VALUES (:nid, :table, :field_name , :uid, :choice, :hostename, :delta, :cookie)",
|
607 |
|
array(':nid'=>$vote->nid, ':table'=>$table, ':field_name'=>$vote->field_name, ':uid'=>$vote->uid, ':choice'=>$choice,
|
608 |
|
'hostname:'=>$vote->hostname,':delta'=>$vote->field_name_delta, ':cookie'=>$vote->cookie));
|
609 |
|
|
610 |
|
db_query("INSERT INTO {pollfield_votes} (nid, field_table, field_name, delta, hostname, field_name_delta, cookie)
|
611 |
|
VALUES (%d, '%s', '%s', %d, '%s', %d, '%s')",
|
612 |
|
$vote->nid, $vote->field_table, $vote->field_name, $choice, $vote->hostname, $vote->field_name_delta, $vote->cookie);
|
|
599 |
db_insert('pollfield_votes')
|
|
600 |
->fields(array(
|
|
601 |
'nid' => $vote->nid,
|
|
602 |
'field_table' => $vote->table,
|
|
603 |
'field_name' => $vote->field_name,
|
|
604 |
'uid' => $vote->uid,
|
|
605 |
'delta' => $choice,
|
|
606 |
'hostname' => $vote->hostname,
|
|
607 |
'field_name_delta' => $vote->field_name_delta,
|
|
608 |
'cookie' => $vote->cookie,
|
|
609 |
))
|
|
610 |
->execute();
|
613 |
611 |
}
|
614 |
612 |
}
|
615 |
613 |
|
616 |
614 |
/**
|
617 |
|
* Cancel a user's vote on a poll
|
|
615 |
* Cancel a user's vote on a poll.
|
618 |
616 |
*/
|
619 |
|
function pollfield_cancel_choice($vote) {
|
|
617 |
function pollfield_cancel_choice($vote, $node) {
|
|
618 |
// Get total votes.
|
|
619 |
$field = field_get_items('node', $node, $vote->field_name);
|
|
620 |
$total_votes = isset($field[0]['votes']) ? $field[0]['votes'] : 0;
|
620 |
621 |
|
|
622 |
// Cancel all the votes by this user on this poll.
|
|
623 |
pollfield_cancel_vote_record($vote);
|
621 |
624 |
|
622 |
|
// Get total votes
|
623 |
|
$field_votes = $vote->field_name .'_votes';
|
624 |
|
$table = 'field_revision_'.$vote->field_name;
|
625 |
|
$table_data = 'field_data_'.$vote->field_name;
|
626 |
|
$query = "SELECT ".$field_votes." FROM {".$table."} WHERE entity_id=".$vote->nid." AND delta=".$vote->field_name_delta;
|
627 |
|
$total_votes = db_query($query)->fetchField();
|
628 |
|
|
629 |
|
|
630 |
|
// Cancel all the votes by this user on this poll
|
631 |
|
$cancel_vote = pollfield_cancel_vote_record($vote);
|
632 |
|
|
633 |
|
// Decrement total votes for this pollfield
|
|
625 |
// Decrement total votes for this pollfield.
|
634 |
626 |
$total_votes--;
|
635 |
|
|
636 |
|
$query="UPDATE {".$table."} SET ".$field_votes."=".$total_votes." WHERE entity_id=".$vote->nid." AND delta=".$vote->field_name_delta;
|
637 |
|
db_query($query);
|
638 |
|
$query="UPDATE {".$table_data."} SET ".$field_votes."=".$total_votes." WHERE entity_id=".$vote->nid." AND delta=".$vote->field_name_delta;
|
639 |
|
db_query($query);
|
640 |
|
|
641 |
|
return 0;
|
|
627 |
$total_votes = ($total_votes < 0) ? 0 : $total_votes;
|
|
628 |
$node->{$vote->field_name}[LANGUAGE_NONE][0]['votes'] = $total_votes;
|
|
629 |
field_attach_update('node', $node);
|
|
630 |
|
|
631 |
return TRUE;
|
642 |
632 |
}
|
643 |
633 |
|
644 |
634 |
/**
|
645 |
|
* Delete votes from the database
|
|
635 |
* Delete votes from the database.
|
646 |
636 |
*
|
647 |
|
* Delete votes made by a specific user on a
|
648 |
|
* specific pollfield.
|
649 |
|
*
|
650 |
|
* @param $vote
|
651 |
|
* Vote object
|
|
637 |
* Delete votes made by a specific user on a specific pollfield.
|
652 |
638 |
*/
|
653 |
639 |
function pollfield_cancel_vote_record($vote) {
|
|
640 |
$query = db_delete('pollfield_votes')
|
|
641 |
->condition('nid', $vote->nid)
|
|
642 |
->condition('field_table', $vote->field_table)
|
|
643 |
->condition('field_name', $vote->field_name)
|
|
644 |
->condition('field_name_delta', $vote->field_name_delta);
|
654 |
645 |
|
655 |
646 |
if ($vote->uid) {
|
656 |
|
db_query("DELETE from {pollfield_votes}
|
657 |
|
WHERE nid = :nid AND field_table=:field_table AND field_name=:field_name AND uid = :uid AND field_name_delta=:field_name_delta",
|
658 |
|
array(':nid'=>$vote->nid, ':field_table'=>$vote->field_table, ':field_name'=>$vote->field_name, ':uid'=>$vote->uid,
|
659 |
|
':field_name_delta'=>$vote->field_name_delta));
|
|
647 |
$query->condition('uid', $vote->uid);
|
|
648 |
$query->execute();
|
660 |
649 |
}
|
661 |
650 |
else {
|
662 |
651 |
if ($vote->anonymous == 'anon_by_ip') {
|
663 |
|
db_query("DELETE from {pollfield_votes}
|
664 |
|
WHERE nid = :nid AND field_table=:field_table AND field_name=:field_name AND hostname = :hostname AND field_name_delta=:field_name_delta",
|
665 |
|
array(':nid'=>$vote->nid, ':field_table'=>$vote->field_table, 'field_name'=>$vote->field_name,':hostname'=>$vote->hostname, ':field_name_delta'=>$vote->field_name_delta));
|
|
652 |
$query->condition('hostname', $vote->hostname);
|
|
653 |
$query->execute();
|
666 |
654 |
}
|
667 |
|
if ($vote->anonymous == 'anon_by_cookie') {
|
668 |
|
db_query("DELETE from {pollfield_votes}
|
669 |
|
WHERE nid = :nid AND field_table=:field_table AND field_name=:field_name AND cookie = :cookie AND field_name_delta=:field_name_delta",
|
670 |
|
array(':nid'=>$vote->nid, ':field_table'=>$vote->field_table, ':field_name'=>$vote->field_name,
|
671 |
|
':cookie'=>$vote->cookie, ':field_name_delta'=>$vote->field_name_delta));
|
|
655 |
elseif ($vote->anonymous == 'anon_by_cookie') {
|
|
656 |
$query->condition('cookie', $vote->cookie);
|
|
657 |
$query->execute();
|
672 |
658 |
}
|
673 |
659 |
}
|
674 |
660 |
}
|
675 |
|
|
|
661 |
|
676 |
662 |
/**
|
677 |
663 |
* Saving or canceling user poll vote.
|
678 |
664 |
*/
|
679 |
|
function pollfield_vote() {
|
680 |
|
$form_values = $_POST;
|
681 |
|
|
682 |
|
// Build vote object
|
683 |
|
$vote->nid = $form_values['nid'];
|
684 |
|
$vote->field_name = $form_values['field_name'];
|
685 |
|
$vote->field_name_delta = $form_values['field_name_delta'];
|
686 |
|
$vote->field_table = $form_values['table'];
|
687 |
|
$vote->cancel = $form_values['cancel'];
|
688 |
|
if (isset($form_values['choice']))
|
689 |
|
$vote->choice = $form_values['choice'];
|
690 |
|
$item = unserialize($form_values['item']);
|
691 |
|
$vote->item = $item;
|
692 |
|
// Information about the voter
|
|
665 |
function pollfield_vote($form, &$form_state) {
|
|
666 |
|
|
667 |
$vote = new stdClass();
|
|
668 |
$values = $form_state['input'];
|
|
669 |
// Build vote object.
|
|
670 |
$vote->nid = $values['nid'];
|
|
671 |
$vote->field_name = $values['field_name'];
|
|
672 |
$vote->field_name_delta = $values['field_name_delta'];
|
|
673 |
$vote->field_table = $values['table'];
|
|
674 |
$vote->cancel = $values['cancel'];
|
|
675 |
if (isset($values['choice'])) {
|
|
676 |
if (is_array($values['choice'])) {
|
|
677 |
$choices = array_filter($values['choice']);
|
|
678 |
$vote->choice = array_keys($choices);
|
|
679 |
}
|
|
680 |
else {
|
|
681 |
$vote->choice = array($values['choice']);
|
|
682 |
}
|
|
683 |
}
|
|
684 |
$item = unserialize($values['item']);
|
|
685 |
$vote->item = $item;
|
|
686 |
|
|
687 |
// Information about the voter.
|
693 |
688 |
global $user;
|
694 |
689 |
$vote->uid = $user->uid;
|
695 |
|
$vote->hostname = $_SERVER['REMOTE_ADDR'];
|
|
690 |
$vote->hostname = ip_address();
|
696 |
691 |
|
697 |
|
// If we need a cookie add it here
|
698 |
|
$cookie = pollfield_cookie_id_generator();
|
699 |
|
if ($cookie != 0) {
|
700 |
|
$vote->cookie = serialize($cookie);
|
701 |
|
}
|
702 |
|
else {
|
703 |
|
$vote->cookie = $cookie;
|
704 |
|
}
|
|
692 |
// If we need a cookie add it here.
|
|
693 |
$cookie = pollfield_cookie_id_generator($values);
|
|
694 |
$vote->cookie = ($cookie) ? serialize($cookie) : $cookie;
|
705 |
695 |
|
706 |
696 |
$node = node_load($vote->nid);
|
707 |
697 |
|
708 |
|
|
709 |
|
|
710 |
|
// Get poll features
|
711 |
|
$field_name = $vote->field_name;
|
712 |
|
$field = $node->$field_name;
|
|
698 |
// Get poll features.
|
713 |
699 |
$poll_features = unserialize($item['poll_features']);
|
714 |
700 |
$vote->anonymous = $item['anonymous'];
|
715 |
701 |
|
716 |
|
// Cancel vote
|
717 |
|
if ($vote->nid && $vote->cancel == 1) {
|
718 |
|
pollfield_cancel_choice($vote);
|
719 |
|
}
|
720 |
|
|
721 |
|
// Vote
|
722 |
|
if ($vote->nid && $vote->cancel == 0) {
|
723 |
|
if ($poll_features['poll_type'] == '0') {
|
724 |
|
$vote->type = 'single';
|
|
702 |
if ($vote->nid) {
|
|
703 |
// Cancel vote.
|
Weekly update of contrib modules