View source
<?php
function advpoll_info_binary() {
return array(
'name' => 'binary',
'name_label' => t('Binary'),
'description' => t('Vote for or against a number of choices.'),
);
}
function advpoll_algorithms_binary() {
return array(
'plurality' => t('Plurality'),
);
}
function advpoll_voting_binary_form(&$node, $teaser, $page, $status) {
static $binary_form_count = 0;
$form = array(
'#id' => 'advpoll-voting-binary-form-' . $binary_form_count++,
'#attributes' => array(
'class' => 'advpoll-vote',
),
);
$form['ajax'] = array(
'#type' => 'hidden',
'#attributes' => array(
'class' => 'ajax',
),
);
if ($node->choice) {
$list = array();
$check = $node->in_preview;
foreach ($node->choice as $i => $choice) {
if ($choice['label'] && ($node->show_writeins || !$choice['writein'])) {
$list[$i] = _advpoll_choice_markup($choice['label'], $node->format, $check) . ($choice['writein'] ? ' ' . t('(write-in)') : '');
}
}
if ($node->writeins && user_access('add write-ins')) {
$list[$i + 1] = t('(write-in)');
$form['writein_key'] = array(
'#type' => 'value',
'#value' => $i + 1,
);
}
$form['choice'] = array(
'#options' => $list,
'#prefix' => '<div class="vote-choices">',
'#suffix' => '</div>',
);
$max_choices = $node->in_preview ? $node->settings['max_choices'] : $node->max_choices;
if ($max_choices == 1) {
$form['choice']['#type'] = 'radios';
$form['choice']['#default_value'] = -1;
}
else {
$form['choice']['#type'] = 'checkboxes';
}
}
if ($node->writeins && user_access('add write-ins')) {
$form['writein_choice'] = array(
'#prefix' => '<div class="writein-choice">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Write-in vote'),
'#size' => 25,
);
}
$form['nid'] = array(
'#type' => 'hidden',
'#value' => $node->nid,
'#attributes' => array(
'class' => 'edit-nid',
),
);
if (!$node->in_preview && advpoll_eligible($node) && $status == 'open') {
static $binary_vote_count = 0;
$form['vote'] = array(
'#type' => 'submit',
'#value' => t('Vote'),
'#attributes' => array(
'id' => 'edit-vote-binary-' . $binary_vote_count++,
),
);
}
elseif ($node->in_preview) {
}
elseif ($status == 'pending') {
$form['message']['#value'] = t('This poll opens @time.', array(
'@time' => format_date($node->start_date),
));
}
else {
global $user;
$login_message = t('<a href="@login">Login</a> to vote in this poll.', array(
'@login' => url('user/login', drupal_get_destination()),
));
$form['message']['#value'] = $user->uid ? t('You are not eligible to vote in this poll.') : $login_message;
}
$form['#action'] = url('node/' . $node->nid);
return $form;
}
function advpoll_view_results_binary($node, $teaser, $page) {
$content_type = 'advpoll';
$content_id = $node->nid;
$results = votingapi_get_voting_results($content_type, $content_id);
$votes = array();
foreach ($results as $result) {
$vote_value = $result->tag;
if ($vote_value == '_advpoll') {
if ($result->function == 'total_votes') {
$total_votes = $result->value;
}
}
else {
if (isset($node->choice[$vote_value])) {
if (!$votes[$vote_value]) {
$votes[$vote_value] = 0;
}
$votes[$vote_value] = $result->value;
}
}
}
if ($node->choice && $total_votes > 0) {
foreach ($node->choice as $i => $choice) {
if (!isset($votes[$i])) {
$votes[$i] = 0;
}
}
arsort($votes);
foreach ($votes as $i => $count) {
$choice = $node->choice[$i];
$percentage = round(100 * $votes[$i] / $total_votes, 0);
$output .= theme('advpoll_bar', _advpoll_choice_markup($choice['label'], $node->format, FALSE), $percentage, format_plural($count, '1 vote', '@count votes'), $choice);
}
}
return array(
'results' => $output,
'votes' => $total_votes,
);
}
function advpoll_calculate_results_binary(&$results, $votes, $node) {
$voters = array();
foreach ($votes as $vote) {
if ($vote->uid) {
$key = $vote->uid;
}
else {
$key = $vote->hostname;
}
$voters[$key] = TRUE;
}
$results['_advpoll'] = array(
array(
'total_votes' => count($voters),
),
);
}
function advpoll_voting_binary_form_submit($form_id, $form_values) {
$votes = array();
$node = node_load($form_values['nid']);
_advpoll_writeins_voting_form_submit($node, $form_values, $votes, 1);
if ($node->max_choices == 1) {
if (!$node->writeins || !$form_values['choice'][$form_values['writein_key']]) {
$vote->value = 1;
$vote->tag = $form_values['choice'];
$vote->value_type = 'option';
$votes[] = $vote;
}
}
else {
foreach ($form_values['choice'] as $choice => $selected) {
$vote = new stdClass();
if (!$node->writeins || $choice != $form_values['writein_key']) {
$vote->value = $choice;
if ($selected) {
$vote->value_type = 'option';
$vote->tag = $choice;
$vote->value = 1;
$votes[] = $vote;
}
}
}
}
votingapi_set_vote('advpoll', $form_values['nid'], $votes);
_advpoll_vote_response($node, $form_values);
}
function advpoll_voting_binary_form_validate($form_id, $form_values) {
$node = node_load($form_values['nid']);
$ajax = $form_values['ajax'];
if (!advpoll_eligible($node)) {
_advpoll_form_set_error('choice[', t('You are not allowed to vote in this poll.'), $ajax);
}
if (!_advpoll_is_active($node)) {
_advpoll_form_set_error('choice[', t('This poll is closed.'), $ajax);
}
$writein_option = FALSE;
$writein_text = $form_values['writein_key'] ? $form_values['writein_choice'] : '';
list($voted, $cancel_vote) = _advpoll_user_voted($node->nid);
if ($voted) {
_advpoll_form_set_error('choice[', t('You have already voted in this poll.'), $ajax);
drupal_goto('node/' . $node->nid);
}
if ($node->max_choices == 1) {
if ($node->writeins && user_access('add write-ins') && $form_values['choice'] == $form_values['writein_key']) {
$writein_option = TRUE;
}
elseif (!isset($node->choice[$form_values['choice']])) {
_advpoll_form_set_error('choice[', t('At least one choice must be selected.'), $ajax);
}
}
else {
$num_choices = 0;
foreach ($node->choice as $i => $val) {
if ($form_values['choice'][$i]) {
$num_choices++;
}
}
if ($node->writeins && user_access('add write-ins') && $form_values['choice'][$form_values['writein_key']]) {
$num_choices++;
$writein_option = TRUE;
}
if ($node->max_choices != 0 && $num_choices > $node->max_choices) {
$message = t('%num choices were selected but only %max are allowed.', array(
'%num' => $num_choices,
'%max' => $node->max_choices,
));
_advpoll_form_set_error('choice[', $message, $ajax);
}
$min_choices = 1;
if ($num_choices < $min_choices) {
_advpoll_form_set_error('choice[', t('At least one choice must be selected.'), $ajax);
}
}
_advpoll_writeins_voting_form_validate($node, $writein_option, $writein_text, $ajax);
}
function theme_advpoll_voting_binary_form($form) {
$message = drupal_render($form['message']);
$output = "<div class=\"poll\">\n";
$output .= drupal_render($form);
if ($message) {
$output .= "<p class=\"message\">{$message}</p>\n";
}
$output .= "</div>\n";
return $output;
}
function advpoll_cancel_binary($node, $user_vote) {
if ($node->writeins) {
$recalculate = FALSE;
foreach ($user_vote as $vote) {
if ($node->choice[$vote->tag]['writein']) {
$count = db_result(db_query('SELECT COUNT(1) FROM {votingapi_vote} WHERE content_id = %d AND tag = %d', $node->nid, $vote->tag));
if ($count == 0) {
db_query('DELETE FROM {advpoll_choices} WHERE cid = %d', $vote->tag);
$recalculate = TRUE;
}
}
}
if ($recalculate) {
votingapi_recalculate_results('advpoll', $node->nid);
}
}
}