function advpoll_voting_binary_form in Advanced Poll 6.3
Same name and namespace in other branches
- 5 modes/binary.inc \advpoll_voting_binary_form()
- 6 modes/binary.inc \advpoll_voting_binary_form()
- 6.2 modes/binary.inc \advpoll_voting_binary_form()
Implementation of the view_voting hook for the poll module.
This creates a list of choices to allow the user to vote on choices.
File
- modes/
binary.inc, line 24 - Handle binary (true/false) votes.
Code
function advpoll_voting_binary_form(&$form_state, $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',
),
);
$form['#node'] = $node;
if ($node->choice) {
$list = array();
// If previewing check the format against the current users permissions.
$check = $node->build_mode == NODE_BUILD_PREVIEW;
foreach ($node->choice as $i => $choice) {
// Don't show blank choices or write-in votes if the setting is disabled.
if ($choice['label'] && ($node->show_writeins || !$choice['writein'])) {
$list[$i] = _advpoll_choice_markup($choice['label'], $node->format, $check) . ($choice['writein'] ? ' ' . t('(write-in)') : '');
}
}
// Add write-in checkbox/radio if write-ins are enabled and user has permission.
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,
'#tree' => TRUE,
);
$max_choices = $node->build_mode == NODE_BUILD_PREVIEW ? $node->settings['max_choices'] : $node->max_choices;
if ($max_choices == 1) {
// Plurality voting
$form['choice']['#type'] = 'radios';
$form['choice']['#default_value'] = -1;
}
else {
// Approval voting
$form['choice']['#type'] = 'checkboxes';
}
}
// Add write-in text field if write-ins are enabled and user has permission.
if ($node->writeins && user_access('add write-ins')) {
$form['writein_choice'] = array(
'#type' => 'textfield',
'#title' => t('Write-in vote'),
'#size' => 25,
);
}
$form['nid'] = array(
'#type' => 'hidden',
'#value' => $node->nid,
'#attributes' => array(
'class' => 'edit-nid',
),
);
$advpollSettings = variable_get('advpoll_settings', array());
$votingMode = !empty($advpollSettings['voting_mode']) ? $advpollSettings['voting_mode'] : '0';
$showResults = !empty($advpollSettings['show_results']) ? $advpollSettings['show_results'] : 'aftervote';
$displayButton = true;
// no need to show vote button if user isn't actually able to vote
if ($votingMode == 1 && isset($_COOKIE['advpollvote' . $node->nid])) {
$displayButton = false;
$duration = !empty($advpollSettings['cookie_duration']) ? $advpollSettings['cookie_duration'] : 60;
}
$voted = _advpoll_user_voted($node->nid);
$format = 'F j, Y';
// Hide vote button if user can't vote and instead display appropriate message.
if ($node->build_mode != NODE_BUILD_PREVIEW && advpoll_eligible($node) && $status == 'open' && $node->active && $displayButton && !$voted[0]) {
static $binary_vote_count = 0;
$form['vote'] = array(
'#type' => 'submit',
'#value' => t('Vote'),
'#id' => 'edit-vote-binary-' . $binary_vote_count++,
);
}
elseif (!$displayButton) {
$form['message']['#value'] = t('There is a delay of @time minutes between votes. Try again later.', array(
'@time' => $duration,
));
}
elseif ($status == 'pending') {
$form['message']['#value'] = t('This poll opens on @time.', array(
'@time' => date($format, $node->start_date),
));
}
elseif ($node->active && $showResults === 'afterclose' && $voted[0]) {
$form['message']['#value'] = t('The results will be revealed on @time.', array(
'@time' => date($format, $node->end_date),
));
}
elseif ($node->build_mode == NODE_BUILD_PREVIEW || $voted[0]) {
if ($voted[0] && !user_access('cancel own vote')) {
$form['message']['#value'] = !empty($advpollSettings['vote_completed']) ? $advpollSettings['vote_completed'] : t('Thank you for voting.');
}
// Display nothing.
}
else {
$ineligible = !empty($advpollSettings['ineligible_message']) ? $advpollSettings['ineligible_message'] : t('You are not eligible to vote in this poll.');
global $user;
$login_message = t('<a href="@login">Login</a> to vote in this poll.', array(
'@login' => url('user/login', array(
'query' => drupal_get_destination(),
)),
));
$form['message']['#value'] = isset($user->uid) ? $ineligible : $login_message;
}
$form['#action'] = url('node/' . $node->nid);
return $form;
}