View source
<?php
define('ADVPOLL_MAX_CHOICES', 1);
define('ADVPOLL_RUNTIME', 0);
define('ADVPOLL_ELECTORAL_LIST', 0);
define('ADVPOLL_SHOW_VOTES', 1);
define('ADVPOLL_WRITEINS', 0);
define('ADVPOLL_SHOW_WRITEINS', 0);
define('ADVPOLL_INITIAL_CHOICES', 5);
define('ADVPOLL_USE_QUESTION', 0);
define('ADVPOLL_CHOICE_MAX_LENGTH', 2048);
function advpoll_help($path, $arg) {
switch ($path) {
case 'admin/modules#description':
return t('A sophisticated polling module for voting, elections, and group decision-making.');
break;
}
}
function advpoll_perm() {
return array(
'create polls',
'edit polls',
'edit own polls',
'vote on polls',
'cancel own vote',
'administer polls',
'inspect all votes',
'show vote results',
'access electoral list',
'add write-ins',
);
}
function advpoll_access($op, $node, $account) {
if ($op == 'create') {
return user_access('create polls', $account);
}
if ($op == 'update') {
if (user_access('edit polls', $account) || $node->uid == $account->uid && user_access('edit own polls', $account)) {
return TRUE;
}
}
}
function advpoll_node_info() {
$modes = _advpoll_list_modes();
$info = array();
foreach ($modes as $mode) {
$info['advpoll_' . $mode['name']] = array(
'name' => t('@name poll', array(
'@name' => $mode['name_label'],
)),
'module' => 'advpoll',
'description' => $mode['description'],
'title_label' => t('@name question', array(
'@name' => $mode['name_label'],
)),
'body_label' => t('Description'),
);
}
return $info;
}
function advpoll_menu() {
$menu['advpoll/cancel/%node'] = array(
'title' => 'Cancel Vote',
'page callback' => 'advpoll_cancel',
'page arguments' => array(
2,
),
'access arguments' => array(
'cancel own vote',
),
'type' => MENU_CALLBACK,
'file' => 'advpoll.admin.inc',
);
$menu['advpoll/js_vote'] = array(
'title' => 'Vote via JavaScript',
'page callback' => 'advpoll_js_vote',
'access arguments' => array(
'vote on polls',
),
'type' => MENU_CALLBACK,
);
$menu['advpoll/js_more_choices'] = array(
'title' => 'More Choices via JavaScript',
'page callback' => 'advpoll_js_more_choices',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$menu['polls'] = array(
'title' => 'Advanced Polls',
'page callback' => 'advpoll_page',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
'file' => 'advpoll.pages.inc',
);
$menu['node/%node/results'] = array(
'title' => 'Results',
'page callback' => 'advpoll_results_page',
'page arguments' => array(
1,
),
'access callback' => '_advpoll_results_access',
'access arguments' => array(
1,
),
'weight' => 3,
'type' => MENU_LOCAL_TASK,
'file' => 'advpoll.pages.inc',
);
$menu['node/%node/votes'] = array(
'title' => 'Votes',
'page callback' => 'advpoll_votes_page',
'page arguments' => array(
1,
),
'access callback' => '_advpoll_votes_access',
'access arguments' => array(
1,
),
'weight' => 3,
'type' => MENU_LOCAL_TASK,
'file' => 'advpoll.pages.inc',
);
$menu['node/%node/electoral_list'] = array(
'title' => 'Electoral list',
'page callback' => 'advpoll_electoral_list_page',
'page arguments' => array(
1,
),
'access callback' => '_advpoll_electoral_list_access',
'access arguments' => array(
1,
),
'weight' => 3,
'type' => MENU_LOCAL_TASK,
'file' => 'advpoll.pages.inc',
);
$menu['node/%node/remove'] = array(
'page callback' => 'advpoll_remove_voter',
'page arguments' => array(
1,
),
'access arguments' => array(
'administer polls',
),
'weight' => 3,
'type' => MENU_CALLBACK,
'file' => 'advpoll.admin.inc',
);
$menu['node/%node/votes/clear'] = array(
'page callback' => 'advpoll_clear_votes_page',
'page arguments' => array(
1,
),
'access callback' => '_advpoll_clear_votes_access',
'access arguments' => array(
1,
),
'weight' => 3,
'type' => MENU_LOCAL_TASK,
'file' => 'advpoll.pages.inc',
);
$menu['node/%node/writeins'] = array(
'title' => 'Write-ins',
'page callback' => 'advpoll_writeins_page',
'page arguments' => array(
1,
),
'access callback' => '_advpoll_writeins_access',
'access arguments' => array(
1,
),
'weight' => 3,
'type' => MENU_LOCAL_TASK,
'file' => 'advpoll.pages.inc',
);
$menu['admin/settings/advpoll'] = array(
'title' => 'Advanced Poll Settings',
'description' => 'Manage Advanced Poll settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'_advpoll_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'advpoll.admin.inc',
);
return $menu;
}
function advpoll_init() {
drupal_add_css(drupal_get_path('module', 'poll') . '/poll.css');
_advpoll_list_modes();
}
function _advpoll_results_access($node) {
return strstr($node->type, 'advpoll_') && _advpoll_is_active($node) && $node->votes > 0 && !$node->voted && _advpoll_can_view_results($node);
}
function _advpoll_electoral_list_access($node) {
return user_access('access electoral list') && strstr($node->type, 'advpoll_') && $node->use_list;
}
function _advpoll_votes_access($node) {
return strstr($node->type, 'advpoll_') && $node->votes > 0 && (user_access('inspect all votes') && $node->show_votes || user_access('administer polls'));
}
function _advpoll_clear_votes_access($node) {
return strstr($node->type, 'advpoll_') && $node->votes > 0 && user_access('administer polls');
}
function _advpoll_writeins_access($node) {
if (!strstr($node->type, 'advpoll_') || !user_access('administer polls')) {
return FALSE;
}
foreach ($node->choice as $choice) {
if ($choice['writein']) {
return TRUE;
}
}
return FALSE;
}
function advpoll_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks['latest_poll']['info'] = t('Advanced Poll: Latest poll');
$items = _advpoll_available_blocks();
if ($items) {
foreach ($items as $poll) {
$blocks['adv_poll_' . $poll->nid]['info'] = t('Advanced Poll: @title', array(
'@title' => $poll->title,
));
}
}
return $blocks;
case 'view':
if ($delta == 'latest_poll') {
$block['subject'] = t('Latest poll');
$block['content'] = theme('advpoll_block_latest_poll');
}
else {
$nid = intval(str_replace('adv_poll_', '', $delta));
$block['subject'] = t('Poll');
$block['content'] = theme('advpoll_view_block_poll', $nid);
}
return $block;
}
}
function _advpoll_available_blocks() {
$timestamp = time();
$result = db_query('SELECT n.nid, n.title FROM {node} n INNER JOIN {advpoll} p ON p.nid = n.nid WHERE n.status = 1
AND p.create_view_block = 1 AND p.start_date < %d
AND p.end_date > %d', $timestamp, $timestamp);
$results = array();
while ($node = db_fetch_object($result)) {
$results[] = $node;
}
return $results;
}
function theme_advpoll_block_latest_poll() {
$node = advpoll_latest_poll();
$output = '';
if ($node) {
$output .= '<h3>' . l($node->title, 'node/' . $node->nid) . '</h3>';
$output .= drupal_render($node->content);
if ($node->voted) {
$output .= '<p>' . l(t('Older polls'), 'polls', array(
'class' => 'old-polls',
'title' => t('View the list of polls on this site.'),
)) . '</p>';
}
}
return $output;
}
function theme_advpoll_view_block_poll($nid) {
$node = advpoll_view(node_load($nid), FALSE, FALSE);
$output = '';
if ($node && $node->status && $node->active && $node->start_date < time() && $node->end_date > time()) {
$output .= '<h3>' . $node->title . '</h3>';
$output .= drupal_render($node->content);
}
return $output;
}
function advpoll_latest_poll() {
$timestamp = time();
$result = db_query('SELECT MAX(n.nid) AS nid FROM {node} n INNER JOIN {advpoll} p ON p.nid = n.nid WHERE
n.status = 1
AND p.active = 1
AND p.start_date < %d
AND p.end_date > %d', $timestamp, $timestamp);
$poll = db_fetch_object($result);
if ($poll->nid) {
$node = advpoll_view(node_load($poll->nid), FALSE, FALSE);
}
return $node;
}
function advpoll_form(&$node, $form_state) {
$mode = _advpoll_get_mode($node->type);
$type = node_get_types('type', $node);
$editing = isset($node->nid);
$form = array();
static $add_js;
if (!$add_js) {
drupal_add_js(array(
'advPoll' => array(
'remove' => t('Remove'),
'addChoice' => t('Add choice'),
'noLimit' => t('No limit'),
),
), 'setting');
drupal_add_js(drupal_get_path('module', 'advpoll') . '/advpoll-form.js', 'module');
drupal_add_css(drupal_get_path('module', 'advpoll') . '/advpoll.css', 'module');
$add_js = TRUE;
}
$form['title'] = array(
'#type' => 'textfield',
'#maxlength' => 255,
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
);
if ($type->has_body) {
$form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
}
if (isset($form_state['values']['choices'])) {
$choices = $form_state['values']['choices'];
if ($form_state['values']['more_choices']) {
$choices *= 2;
}
}
else {
$choices = max(2, isset($node->choice) && count($node->choice) ? count($node->choice) : ADVPOLL_INITIAL_CHOICES);
}
$form['choices'] = array(
'#type' => 'hidden',
'#value' => $choices,
);
$form['choice'] = array(
'#type' => 'fieldset',
'#title' => t('Poll choices'),
'#collapsible' => TRUE,
'#prefix' => '<div class="poll-form" id="poll-choices">',
'#suffix' => '</div>',
'#tree' => TRUE,
'#weight' => 1,
);
if ($editing) {
$form['choice']['choice_note'] = array(
'#value' => '<div id="edit-settings-choice-note" class="description">' . t('Note: adding or removing choices after voting has begun is not recommended.') . '</div>',
);
}
$form['choice']['more_choices'] = array(
'#type' => 'checkbox',
'#title' => t('Need more choices'),
'#value' => 0,
'#parents' => array(
'more_choices',
),
'#prefix' => '<div id="more-choices">',
'#suffix' => '</div>',
'#description' => t("If the number of choices above isn't enough, click here to add more choices."),
'#weight' => 1,
);
$form['footer_message'] = array(
'#type' => 'textfield',
'#title' => t('Footer Message'),
'#description' => t('Optional message that can appear below the poll.'),
'#default_value' => $node->footer_message,
'#maxlength' => 255,
);
$current_choices = 0;
$default_choices = '';
if (isset($node->choice)) {
foreach ($node->choice as $index => $choice) {
$form['choice'][$index]['label'] = array(
'#type' => 'textfield',
'#title' => t('Choice %n', array(
'%n' => $current_choices + 1,
)) . ($choice['writein'] ? ' ' . t('(write-in)') : ''),
'#default_value' => $choice['label'],
'#attributes' => array(
'class' => 'choices',
),
'#maxlength' => ADVPOLL_CHOICE_MAX_LENGTH,
);
$current_choices++;
$next_index = $index + 1;
}
}
elseif ($default_choices != '') {
$default_choices = explode("\n", $default_choices);
foreach ($default_choices as $index => $label) {
$form['choice'][$index]['label'] = array(
'#type' => 'textfield',
'#title' => t('Choice %n', array(
'%n' => $current_choices + 1,
)),
'#default_value' => $label,
'#attributes' => array(
'class' => 'choices',
),
);
$current_choices++;
$next_index = $index + 1;
}
}
else {
$next_index = 1;
}
if ($current_choices < $choices) {
for ($index = $next_index; $current_choices < $choices; $index++, $current_choices++) {
$form['choice'][$index]['label'] = array(
'#type' => 'textfield',
'#title' => t('Choice %n', array(
'%n' => $current_choices + 1,
)),
'#attributes' => array(
'class' => 'choices',
),
'#maxlength' => ADVPOLL_CHOICE_MAX_LENGTH,
);
}
}
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Poll settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 2,
'#tree' => TRUE,
);
$max_choice_list = array();
for ($i = 0; $i <= $choices; $i++) {
$max_choice_list[$i] = $i == 0 ? t('No limit') : $i;
}
$form['settings']['max_choices'] = array(
'#type' => 'select',
'#title' => t('Maximum choices'),
'#options' => $max_choice_list,
'#default_value' => isset($node->max_choices) ? $node->max_choices : ADVPOLL_MAX_CHOICES,
'#description' => t('Limits the total number of choices voters may select.'),
);
$voting_algorithms = advpoll_algorithms($mode);
if (count($voting_algorithms) > 1) {
$form['settings']['algorithm'] = array(
'#type' => 'select',
'#title' => t('Algorithm'),
'#options' => $voting_algorithms,
'#default_value' => isset($node->algorithm) ? $node->algorithm : key($voting_algorithms),
'#description' => t('Voting algorithm to use to calculate the winner.'),
);
}
else {
$form['settings']['algorithm'] = array(
'#type' => 'value',
'#value' => key($voting_algorithms),
);
}
$form['settings']['close'] = array(
'#type' => 'checkbox',
'#title' => t('Close poll'),
'#description' => t('When a poll is closed users may no longer vote on it.'),
'#default_value' => isset($node->active) ? !$node->active : 0,
);
$format = 'Y-m-d';
$date = date($format, time());
$form['settings']['start_date'] = array(
'#type' => 'date_select',
'#default_value' => isset($node->start_date) ? date($format, $node->start_date) : date($format, time()),
'#date_format' => $format,
'#title' => t('Starting date'),
'#description' => t('The date that the poll opens.'),
);
$form['settings']['end_date'] = array(
'#type' => 'date_select',
'#default_value' => isset($node->end_date) ? date($format, $node->end_date) : date($format, time() + 60 * 60 * 24 * 365),
'#date_format' => $format,
'#title' => t('Ending date'),
'#description' => t('Leave blank if you do not want the poll to close automatically.'),
);
$default_use_list = isset($node->use_list) ? $node->use_list : ADVPOLL_ELECTORAL_LIST;
$default_show_votes = isset($node->show_votes) ? $node->show_votes : ADVPOLL_SHOW_VOTES;
$default_writeins = isset($node->writeins) ? $node->writeins : ADVPOLL_WRITEINS;
$default_show_writeins = isset($node->show_writeins) ? $node->show_writeins : ADVPOLL_SHOW_WRITEINS;
if (user_access('administer polls')) {
$form['settings']['admin_note'] = array(
'#value' => '<div id="edit-settings-admin-note" class="description">' . t('The settings below are only available for users with the <em>administer polls</em> permission.') . '</div>',
);
$form['settings']['writeins'] = array(
'#type' => 'checkbox',
'#title' => t('Allow users to cast a write-in vote'),
'#default_value' => $default_writeins,
'#description' => t('Allow voters with the "add write-ins" permission to write-in up to one choice each.'),
'#attributes' => array(
'class' => 'settings-writeins',
),
);
$form['settings']['show_writeins'] = array(
'#type' => 'checkbox',
'#title' => t('Display write-in votes as choices for future voters'),
'#default_value' => $default_show_writeins,
'#description' => t('Allow voters to see and choose from previously submitted write-in votes.'),
'#prefix' => '<div class="edit-settings-show-writeins">',
'#suffix' => '</div>',
);
$form['settings']['use_list'] = array(
'#type' => 'checkbox',
'#title' => t('Restrict voting to electoral list'),
'#description' => t('If enabled, a list of eligible voters will be created and only that group will be able to vote in the poll.'),
'#default_value' => $default_use_list,
);
$form['settings']['show_votes'] = array(
'#type' => 'checkbox',
'#title' => t('Show individual votes'),
'#description' => t('Users with the appropriate permissions will be able to see how each person voted.'),
'#default_value' => $default_show_votes,
);
$form['settings']['create_view_block'] = array(
'#type' => 'checkbox',
'#title' => t('Generate a block for this poll'),
'#description' => t('When checked, a block for this particular poll will be generated upon submission.'),
'#default_value' => isset($node->create_view_block) ? $node->create_view_block : 0,
);
}
else {
$defaults = array(
'use_list' => $default_use_list,
'show_votes' => $default_show_votes,
'writeins' => $default_writeins,
'show_writeins' => $default_show_writeins,
);
foreach ($defaults as $name => $value) {
$form['settings'][$name] = array(
'#type' => 'value',
'#value' => $value,
);
}
}
return $form;
}
function advpoll_more_choices_submit($form, &$form_state) {
node_form_submit_build_node($form, $form_state);
if ($form_state['values']['more_choices']) {
$n = $_GET['q'] == 'advpoll/js_more_choices' ? 1 : 5;
$form_state['choice_count'] = count($form_state['values']['choice']) + $n;
}
}
function advpoll_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
$node_type = $form['old_type']['#value'];
if ($form['module']['#value'] == 'advpoll') {
$advpollSettings = variable_get('advpoll_settings', array());
drupal_add_js(drupal_get_path('module', 'advpoll') . '/advpoll-form.js', 'module');
drupal_add_css(drupal_get_path('module', 'advpoll') . '/advpoll.css', 'module');
$form['advpoll'] = array(
'#type' => 'fieldset',
'#title' => t('Poll settings'),
'#collapsible' => TRUE,
);
$form['advpoll']['advpoll_choices'] = array(
'#type' => 'textarea',
'#title' => t('Default choices'),
'#default_value' => '',
'#description' => t('Add one choice per row. This setting can be overridden on the poll edit page.'),
);
$form['advpoll']['advpoll_max_choices'] = array(
'#type' => 'select',
'#title' => t('Default maximum choices'),
'#options' => array(
0 => t('No limit'),
) + drupal_map_assoc(array(
1,
2,
3,
4,
5,
)),
'#default_value' => ADVPOLL_MAX_CHOICES,
'#description' => t('The default number of maximum choices for new polls. This setting can be overridden on the poll edit page.'),
);
$mode = _advpoll_get_mode($node_type);
$voting_algorithms = advpoll_algorithms($mode);
if (count($voting_algorithms) > 1) {
$form['advpoll']['advpoll_algorithm'] = array(
'#type' => 'select',
'#title' => t('Default algorithm'),
'#options' => $voting_algorithms,
'#default_value' => key($voting_algorithms),
'#description' => t('Default voting algorithm for calculating the winner.'),
);
}
$form['advpoll']['advpoll_runtime'] = array(
'#type' => 'select',
'#title' => t('Default duration'),
'#default_value' => ADVPOLL_RUNTIME,
'#options' => array(
0 => t('Unlimited'),
) + drupal_map_assoc(array(
86400,
172800,
345600,
604800,
1209600,
1814400,
2419200,
4838400,
9676800,
31536000,
), 'format_interval'),
'#description' => t('The date the poll was created is used as start date for the default duration. This setting can be overridden on the poll edit page.'),
);
$form['advpoll']['advpoll_writeins'] = array(
'#type' => 'checkbox',
'#title' => t('Allow users to cast a write-in vote by default'),
'#default_value' => ADVPOLL_WRITEINS,
'#description' => t('Allow voters with the "add write-ins" permission to write-in up to one choice each. Users with the <em>administer polls</em> permission will be able to override this setting.'),
'#attributes' => array(
'class' => 'settings-writeins',
),
);
$form['advpoll']['advpoll_show_writeins'] = array(
'#type' => 'checkbox',
'#title' => t('Display write-in votes as choices for future voters by default'),
'#default_value' => ADVPOLL_SHOW_WRITEINS,
'#description' => t("Allow voters to see and choose from previous voters' write-in votes. Users with the <em>administer polls</em> permission will be able to override this setting."),
'#prefix' => '<div class="edit-settings-show-writeins">',
'#suffix' => '</div>',
);
$form['advpoll']['advpoll_electoral_list'] = array(
'#type' => 'checkbox',
'#title' => t('Use electoral list by default'),
'#description' => t('Use an electoral list by default for new polls. Users with the <em>administer polls</em> permission will be able to override this setting.'),
'#default_value' => ADVPOLL_ELECTORAL_LIST,
);
$form['advpoll']['advpoll_show_votes'] = array(
'#type' => 'checkbox',
'#title' => t('Show individual votes by default'),
'#description' => t('Let users with appropriate permissions see how each person voted by default for new polls. Users with the <em>administer polls</em> permission will be able to override this setting.'),
'#default_value' => ADVPOLL_SHOW_VOTES,
);
$view_results = array(
'always' => t('Always'),
'aftervote' => t('After user has voted'),
'afterclose' => t('After voting has closed'),
);
$form['advpoll']['advpoll_view_results'] = array(
'#type' => 'radios',
'#title' => t('Display results'),
'#description' => t('Determines when users may view the results of the poll.'),
'#default_value' => !empty($advpollSettings['show_results']) ? $advpollSettings['show_results'] : 'aftervote',
'#options' => $view_results,
);
$form['advpoll']['advpoll_use_question'] = array(
'#type' => 'checkbox',
'#title' => t('Use question field'),
'#description' => t('Use a dedicated question field instead of a combined question and title field. It is recommended to rename <em>Title field label</em> above to "Title" if this is checked.'),
'#default_value' => ADVPOLL_USE_QUESTION,
);
}
}
}
function advpoll_load($node) {
$poll = db_fetch_object(db_query('SELECT * FROM {advpoll} WHERE nid = %d', $node->nid));
$result = db_query('SELECT cid, weight, label, writein FROM {advpoll_choices} WHERE nid = %d ORDER BY weight', $node->nid);
$poll->choice = array();
$poll->writein_choices = 0;
while ($choice = db_fetch_array($result)) {
$poll->choice[$choice['cid']] = $choice;
if ($choice['writein'] == 1) {
$poll->writein_choices++;
}
}
$poll->choices = count($poll->choice);
$result = db_query("SELECT value FROM {votingapi_cache} WHERE content_type = 'advpoll' AND content_id = %d AND tag = '_advpoll' AND function = 'total_votes'", $node->nid);
if ($cache = db_fetch_object($result)) {
$poll->votes = $cache->value;
}
else {
$poll->votes = 0;
}
list($poll->voted, $poll->cancel_vote) = _advpoll_user_voted($node->nid);
return $poll;
}
function advpoll_validate($node, &$form) {
$node->choice = array_values($node->choice);
array_unshift($node->choice, '');
unset($node->choice[0]);
$real_choices = 0;
foreach ($_POST['choice'] as $i => $choice) {
if ($choice['label'] != '') {
$real_choices++;
}
}
if ($real_choices < 2) {
form_set_error("choice][{$real_choices}][label", t('You must fill in at least two choices.'));
}
if ($node->settings['max_choices'] < 0) {
form_set_error('settings][max_choices]', t('Maximum choices must be a non-negative integer.'));
}
if ($node->settings['max_choices'] > $real_choices) {
form_set_error('settings][max_choices]', t('Maximum choices cannot be larger than the number of choices submitted.'));
}
if (!empty($node->settings['start_date']) && strtotime($node->settings['start_date']) <= 0) {
form_set_error('settings][start_date', t('You have to specify a valid starting date.'));
}
if (!empty($node->settings['end_date']) && strtotime($node->settings['end_date']) <= 0) {
form_set_error('settings][end_date', t('You have to specify a valid ending date.'));
}
if (!empty($node->settings['end_date']) && $node->settings['end_date'] < $node->settings['start_date']) {
form_set_error('settings][end_date', t('Ending date cannot be before the starting date.'));
}
}
function advpoll_insert($node) {
$mode = _advpoll_get_mode($node->type);
if ($mode) {
db_query("INSERT INTO {advpoll} \n (nid, \n mode, \n use_list, \n active, \n max_choices, \n algorithm, \n show_votes, \n create_view_block, \n start_date, \n end_date, \n writeins, \n show_writeins, \n footer_message) \n VALUES (%d, '%s', %d, %d, %d, '%s', %d, %d, '%s', '%s', %d, %d, '%s')", $node->nid, $mode, $node->settings['use_list'], !$node->settings['close'], $node->settings['max_choices'], $node->settings['algorithm'], $node->settings['show_votes'], $node->settings['create_view_block'], $node->settings['start_date'] ? strtotime($node->settings['start_date']) : 0, $node->settings['end_date'] ? strtotime($node->settings['end_date']) : 0, $node->settings['writeins'], $node->settings['show_writeins'], isset($node->footer_message) ? filter_xss($node->footer_message) : '');
_advpoll_insert_choices($node);
}
}
function advpoll_update($node) {
db_query("UPDATE {advpoll} \n SET active = %d, \n max_choices = %d, \n algorithm = '%s', \n use_list = %d, \n show_votes = %d, \n create_view_block = %d, \n start_date = '%s', \n end_date = '%s', \n writeins = %d, \n show_writeins = %d, \n footer_message = '%s' \n WHERE nid = %d", !$node->settings['close'], $node->settings['max_choices'], $node->settings['algorithm'], $node->settings['use_list'], $node->settings['show_votes'], $node->settings['create_view_block'], $node->settings['start_date'] ? strtotime($node->settings['start_date']) : 0, $node->settings['end_date'] ? strtotime($node->settings['end_date']) : 0, $node->settings['writeins'], $node->settings['show_writeins'], isset($node->footer_message) ? filter_xss($node->footer_message) : '', $node->nid);
_advpoll_insert_choices($node);
votingapi_recalculate_results('advpoll', $node->nid);
}
function advpoll_view($node, $teaser = FALSE, $page = FALSE) {
drupal_add_css(drupal_get_path('module', 'advpoll') . '/advpoll.css', 'module');
$status = _advpoll_is_active($node, TRUE);
$displayResults = _advpoll_check_settings($node);
if ($node->build_mode == NODE_BUILD_PREVIEW || !$displayResults) {
static $add_js = TRUE;
if ($add_js) {
drupal_add_js(drupal_get_path('module', 'advpoll') . '/advpoll-vote.js', 'module');
drupal_add_js('misc/jquery.form.js', 'module');
$add_js = FALSE;
}
$mode = _advpoll_get_mode($node->type);
if ($mode) {
$poll = drupal_get_form('advpoll_voting_' . $mode . '_form', $node, $teaser, $page, $status);
}
}
else {
if (!$node->voted && arg(2) != 'results' && ($status == 'open' || $status == 'pending') && $displayResults) {
$poll = drupal_get_form('advpoll_voting_' . $node->mode . '_form', $node, $teaser, $page, $status);
static $add_js = TRUE;
if ($add_js) {
drupal_add_js(drupal_get_path('module', 'advpoll') . '/advpoll-vote.js', 'module');
drupal_add_js('misc/jquery.form.js', 'module');
$add_js = FALSE;
}
}
else {
if (user_access('show vote results')) {
$poll = advpoll_view_results($node, $teaser, $page);
}
elseif (user_access('cancel own vote')) {
$poll = _advpoll_show_cancel_form($node);
}
}
}
$node->content['poll'] = array(
'#weight' => 2,
'#value' => $poll,
);
return node_prepare($node, $teaser);
}
function advpoll_delete($node) {
db_query('DELETE FROM {advpoll} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {advpoll_choices} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {advpoll_electoral_list} WHERE nid = %d', $node->nid);
db_query("DELETE FROM {votingapi_vote} WHERE content_id = %d AND content_type = 'advpoll'", $node->nid);
db_query("DELETE FROM {votingapi_cache} WHERE content_id = %d AND content_type = 'advpoll'", $node->nid);
}
function advpoll_votingapi_results_alter(&$cache, $content_type, $content_id) {
if ($content_type == 'advpoll') {
$node = node_load($content_id, NULL, TRUE);
$mode = _advpoll_get_mode($node->type);
$function = 'advpoll_calculate_results_' . $mode;
if (function_exists($function)) {
$function($cache, $node);
}
cache_clear_all();
}
}
function _advpoll_user_voted($nid) {
global $user;
$voted = FALSE;
$cancel_vote = FALSE;
if ($user->uid) {
$voted = count(votingapi_select_votes(array(
'uid' => $user->uid,
'content_id' => $nid,
)));
if ($voted) {
$cancel_vote = TRUE;
}
}
else {
$voted = count(votingapi_select_votes(array(
'vote_source' => ip_address(),
'content_id' => $nid,
'uid' => 0,
)));
if ($voted) {
$cancel_vote = TRUE;
}
}
return array(
$voted,
$cancel_vote,
);
}
function advpoll_electoral_list_form(&$form_state, $nid) {
$form['electoral_list'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Administer electoral list'),
'#collapsible' => TRUE,
'#weight' => 2,
);
$form['electoral_list']['add_user'] = array(
'#type' => 'textfield',
'#title' => t('Add user'),
'#size' => 40,
'#description' => t('Add an individual user to the electoral list.'),
);
if (user_access('access user profiles')) {
$form['electoral_list']['add_user']['#autocomplete_path'] = 'user/autocomplete';
}
$result = db_query("SELECT r.name, r.rid FROM {role} r LEFT JOIN {permission} p ON p.rid = r.rid WHERE p.perm LIKE '%vote on polls%' AND r.rid <> 1 ORDER BY r.name");
$role_options = array(
0 => t('(Select a role)'),
);
while ($role = db_fetch_object($result)) {
$role_options[$role->rid] = $role->name;
}
$form['electoral_list']['add_role'] = array(
'#type' => 'select',
'#title' => t('Add users by role'),
'#description' => t('Only roles that have the "vote on polls" permission are listed.'),
'#options' => $role_options,
);
$form['electoral_list']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add to electoral list'),
);
$form['electoral_list']['reset'] = array(
'#type' => 'button',
'#value' => t('Clear electoral list'),
);
$form['nid'] = array(
'#type' => 'hidden',
'#value' => $nid,
);
return $form;
}
function advpoll_electoral_list_form_validate($form, &$form_state) {
if ($form_state['values']['op'] == t('Clear electoral list')) {
if (user_access('administer polls')) {
db_query('DELETE FROM {advpoll_electoral_list} WHERE nid = %d', $form_state['values']['nid']);
drupal_set_message(t('Electoral list cleared.'));
return;
}
}
$add_user = $form_state['values']['electoral_list']['add_user'];
if ($add_user) {
$result = db_query("SELECT uid FROM {users} WHERE name = '%s'", $add_user);
if (!db_fetch_object($result)) {
form_set_error('electoral_list][add_user', t('User %user does not exist.', array(
'%user' => $add_user,
)));
return FALSE;
}
}
}
function advpoll_electoral_list_form_submit($form, &$form_state) {
$add_user = $form_state['values']['electoral_list']['add_user'];
if ($add_user) {
db_query("REPLACE INTO {advpoll_electoral_list} (nid, uid) SELECT '%d', u.uid FROM {users} u WHERE u.name = '%s'", $form_state['values']['nid'], $add_user);
drupal_set_message(t('%user added to electoral list.', array(
'%user' => $add_user,
)));
}
$add_role = $form_state['values']['electoral_list']['add_role'];
if ($add_role) {
$result = db_query('SELECT uid FROM {advpoll_electoral_list} WHERE nid = %d', $form_state['values']['nid']);
$current_list = array(
0,
);
while ($user = db_fetch_object($result)) {
$current_list[] = $user->uid;
}
$user_in_string = implode(',', $current_list);
$is_authenticated = db_result(db_query("SELECT COUNT(*) FROM {role} r WHERE r.name = 'authenticated user' AND r.rid = %d", $add_role));
if ($is_authenticated) {
$result = db_query("INSERT INTO {advpoll_electoral_list} (nid, uid) SELECT '%d', u.uid FROM {users} u WHERE u.uid NOT IN('%s')", $form_state['values']['nid'], $user_in_string);
}
else {
$result = db_query("INSERT INTO {advpoll_electoral_list} (nid, uid) SELECT '%d', u.uid FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid WHERE ur.rid = %d AND u.uid NOT IN('%s')", $form_state['values']['nid'], $add_role, $user_in_string);
}
drupal_set_message(format_plural(db_affected_rows($result), 'Added 1 user to the electoral list.', 'Added @count users to the electoral list.'));
}
}
function _advpoll_can_view_results($node) {
$advpollSettings = variable_get('advpoll_settings', array());
$view_results = !empty($advpollSettings['show_results']) ? $advpollSettings['show_results'] : 'aftervote';
return !_advpoll_is_active($node) || $node->voted && $view_results == 'aftervote' || $view_results == 'always';
}
function advpoll_cancel_form(&$form_state, $nid) {
$form['#action'] = url('advpoll/cancel/' . $nid);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Cancel your vote'),
);
return $form;
}
function _advpoll_is_active($node, $return_status = FALSE) {
$active = TRUE;
$status = 'open';
$start_date = $node->start_date;
$end_date = $node->end_date;
if (!$node->active) {
$active = FALSE;
$status = 'closed';
}
if ($active && $start_date > 0) {
if (!($active = time() >= $start_date)) {
$status = 'pending';
}
}
if ($active && $end_date > 0) {
if (!($active = time() < $end_date)) {
$status = 'passed';
}
}
return $return_status ? $status : $active;
}
function advpoll_binary_node_form_submit($form, &$form_state) {
if (isset($_POST['choice'])) {
$form_state['values']['choice'] = $_POST['choice'];
}
}
function advpoll_ranking_node_form_submit($form, &$form_state) {
if (isset($_POST['choice'])) {
$form_state['values']['choice'] = $_POST['choice'];
}
}
function _advpoll_insert_choices($node) {
$nid = $node->nid;
$weight = 0;
$seen_ids = array();
foreach ($node->choice as $index => $choice) {
if ($choice['label'] != '') {
$choice_exist = db_result(db_query("SELECT COUNT(cid) FROM {advpoll_choices} WHERE nid = %d AND cid = %d", $nid, $index));
if (!$choice_exist) {
db_query("INSERT INTO {advpoll_choices} (nid, label, weight) VALUES (%d, '%s', %d)", $nid, $choice['label'], $weight);
$seen_id = db_last_insert_id('advpoll_choices', 'cid');
}
else {
db_query("UPDATE {advpoll_choices} SET label = '%s', weight = %d WHERE cid = %d", $choice['label'], $weight, $index);
$seen_id = $index;
}
$seen_ids[$seen_id] = 1;
$weight++;
}
}
$all_ids_query = db_query("SELECT cid, label FROM {advpoll_choices} WHERE nid = %d", $nid);
$all_ids = array();
while ($row = db_fetch_array($all_ids_query)) {
$all_ids[$row['cid']] = array(
'label' => $row['label'],
);
}
if (isset($node->choice)) {
foreach ($all_ids as $id => $choice) {
if (!isset($seen_ids[$id])) {
db_query('DELETE FROM {advpoll_choices} WHERE cid = %d', $id);
drupal_set_message(t('Deleted choice %label', array(
'%label' => $choice['label'],
)), 'status');
}
}
}
}
function _advpoll_get_mode($node_type) {
if ($node_type) {
$mode = explode('advpoll_', $node_type, 2);
if ($mode) {
return $mode[1];
}
else {
return '';
}
}
}
function advpoll_clear_votes_form(&$form_state, $nid) {
$form = array();
$form['reset'] = array(
'#value' => t('Clear all votes'),
'#type' => 'submit',
);
$form['#redirect'] = 'node/' . $nid . '/votes/clear';
return $form;
}
function advpoll_clear_votes_confirm_form(&$form_state, $nid) {
$node = node_load($nid);
$form = array();
$form['#nid'] = $node->nid;
$confirm_question = t('Are you sure you want to clear all votes for %title?', array(
'%title' => $node->title,
));
$form['question'] = array(
'#value' => '<h2>' . $confirm_question . '</h2>',
);
$form = confirm_form($form, $confirm_question, 'node/' . $node->nid . '/votes', t('This will delete all votes that have been cast for the poll.'), t('Clear all votes'), t('Cancel'));
unset($form['#theme']);
return $form;
}
function advpoll_clear_votes_confirm_form_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$nid = $form['#nid'];
if ($node = node_load($nid)) {
db_query("DELETE FROM {votingapi_vote} WHERE content_type = 'advpoll' AND content_id = %d", $node->nid);
db_query('DELETE FROM {advpoll_choices} WHERE writein = 1 AND nid = %d', $node->nid);
votingapi_recalculate_results('advpoll', $node->nid);
drupal_set_message(t('Votes have been cleared.'));
watchdog('content', 'Cleared all poll votes (%num_votes).', array(
'%num_votes' => $node->votes,
), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
}
$form_state['redirect'] = 'node/' . $node->nid;
}
}
function _advpoll_block_resultslink($node) {
return array(
'title' => t('Results'),
'href' => 'node/' . $node->nid . '/results',
'attributes' => array(
'title' => t('View the current poll results.'),
),
);
}
function theme_advpoll_results($title, $results, $votes, $links, $nid, $voted, $cancel_vote, $footer_message) {
$output = '<div class="poll">';
if ($results) {
$output .= $results;
$output .= '<div class="total">' . t('Total votes: %votes', array(
'%votes' => $votes,
)) . '</div>';
}
else {
$output .= '<p class="message">' . t('No votes have been recorded for this poll.') . '</p>';
}
if ($footer_message) {
$output .= '<p class="footer-message">' . $footer_message . '</p>';
}
$output .= '</div>';
return $output;
}
function _advpoll_show_cancel_form($node) {
$output = '';
if ($node->voted && !$node->create_view_block && $node->cancel_vote && user_access('cancel own vote') && _advpoll_is_active($node)) {
$output .= drupal_get_form('advpoll_cancel_form', $node->nid);
}
return $output;
}
function theme_advpoll_bar($title, $percentage, $votes, $choice = NULL) {
$output = '<div class="text">' . $title . ($choice && $choice['writein'] ? ' ' . t('(write-in)') : '') . '</div>';
$output .= '<div class="bar"><div style="width: ' . $percentage . '%;" class="foreground"></div></div>';
$output .= '<div class="percent">' . $percentage . '% <span class="votes">(' . $votes . ')</span></div>';
return $output;
}
function _advpoll_vote_response($node, $form_state) {
$advpollSettings = variable_get('advpoll_settings', array());
$msg = !empty($advpollSettings['vote_registered']) ? $advpollSettings['vote_registered'] : t('Your vote was registered.');
$votingMode = $advpollSettings['voting_mode'];
$displayResults = _advpoll_check_settings($node);
if ($form_state['values']['ajax']) {
unset($node->choice);
$result = db_query('SELECT cid, weight, label, writein FROM {advpoll_choices} WHERE nid = %d ORDER BY weight', $node->nid);
while ($choice = db_fetch_array($result)) {
$node->choice[$choice['cid']] = $choice;
}
$node->choices = count($node->choice);
$result = db_query("SELECT value FROM {votingapi_cache} WHERE content_type = 'advpoll' AND content_id = %d AND tag = '_advpoll' AND function = 'total_votes'", $node->nid);
if ($cache = db_fetch_object($result)) {
$node->votes = $cache->value;
}
else {
$node->votes = 0;
}
list($node->voted, $node->cancel_vote) = _advpoll_user_voted($node->nid);
$ajax_output = '';
if (user_access('show vote results')) {
$ajax_output .= advpoll_view_results($node, NULL, NULL);
}
elseif (user_access('cancel own vote') && !$votingMode) {
$ajax_output .= _advpoll_show_cancel_form($node);
}
$ajax_output = str_replace("\n", '', $ajax_output);
drupal_set_header('Content-Type: text/plain; charset=utf-8');
print drupal_to_js(array(
'statusMsgs' => '<div class="messages status">' . $msg . '</div>',
'response' => $ajax_output,
));
exit;
}
else {
drupal_set_message($msg);
}
}
function advpoll_view_results(&$node, $teaser, $page) {
$output = '';
$mode = _advpoll_get_mode($node->type);
if (function_exists('advpoll_view_results_' . $mode)) {
$results = call_user_func('advpoll_view_results_' . $mode, $node, $teaser, $page);
$output .= theme('advpoll_results', check_plain($node->title), $results['results'], $results['votes'], isset($node->links) ? $node->links : array(), $node->nid, $node->voted, $node->cancel_vote, $node->footer_message);
}
$output .= _advpoll_show_cancel_form($node);
return $output;
}
function advpoll_eligible($node, $uid = NULL) {
global $user;
if (!isset($uid)) {
$uid = $user->uid;
}
if ($node->use_list) {
$eligible = db_result(db_query('SELECT COUNT(*) FROM {advpoll_electoral_list} WHERE nid = %d AND uid = %d', $node->nid, $uid));
}
else {
$eligible = user_access('vote on polls');
}
return $eligible;
}
function advpoll_algorithms($mode) {
return call_user_func('advpoll_algorithms_' . $mode);
}
function _advpoll_list_modes() {
static $advpoll_modes;
if (!$advpoll_modes) {
$files = file_scan_directory(dirname(__FILE__) . '/modes/', '^([^\\.].*)\\.inc$', array(
'.',
'..',
'CVS',
), 0, FALSE);
foreach ($files as $file) {
require_once $file->filename;
$mode = $file->name;
if (function_exists('advpoll_info_' . $mode)) {
$advpoll_modes[$mode] = call_user_func('advpoll_info_' . $mode);
}
}
}
return $advpoll_modes;
}
function _advpoll_choice_markup($text, $format = FILTER_FORMAT_DEFAULT, $check = TRUE) {
$text = check_markup($text, $format, $check);
$text = str_replace(array(
'<p>',
'</p>',
), '', $text);
$text = trim($text);
return $text;
}
function _advpoll_form_set_error($name = NULL, $message = '', $ajax = FALSE) {
if ($ajax) {
drupal_set_header('Content-Type: text/plain; charset=utf-8');
print drupal_to_js(array(
'errors' => '<div class="messages error">' . $message . '</div>',
));
exit;
}
else {
return form_set_error('choice[', $message);
}
}
function _advpoll_writeins_voting_form_validate($node, $writein_option, $writein_text, $ajax) {
if ($node->writeins && user_access('add write-ins')) {
if ($writein_text) {
$writein_choice_lower = strtolower($writein_text);
foreach ($node->choice as $i => $value) {
if (strtolower($value['label']) == $writein_choice_lower && ($node->show_writeins || !$value['writein'])) {
_advpoll_form_set_error('writein_choice', t("A write-in vote can not be for an existing choice. Select the choice's option instead."), $ajax);
}
}
}
if ($writein_option && !$writein_text) {
_advpoll_form_set_error('writein_choice', t('If the "write-in" option is selected, a choice must be written in.'), $ajax);
}
if (!$writein_option && $writein_text) {
_advpoll_form_set_error('writein_choice', t('If a choice is written in, the "write-in" option must be selected.'), $ajax);
}
}
}
function advpoll_js_vote() {
}
function _advpoll_writeins_voting_form_submit($node, $form_state, &$votes, $vote_value = 1) {
if (isset($form_state['values']['writein_choice']) && $form_state['values']['writein_choice']) {
$result = db_query("SELECT cid FROM {advpoll_choices} WHERE nid = %d AND LOWER(label) = LOWER('%s')", $node->nid, $form_state['values']['writein_choice']);
$db_vote = db_fetch_object($result);
if ($db_vote && ($second_vote = db_fetch_object($result))) {
$result = db_query("SELECT cid FROM {advpoll_choices} WHERE nid = %d AND label = '%s'", $node->nid, $form_state['values']['writein_choice']);
$db_vote = db_fetch_object($result);
}
if ($db_vote) {
$existing_choice = $db_vote->cid;
$vote = array();
$vote['value'] = $vote_value;
$vote['tag'] = $existing_choice;
$vote['value_type'] = 'option';
$vote['content_type'] = 'advpoll';
$vote['content_id'] = $node->nid;
$vote = _advpoll_votesettings($vote, $node->nid);
$votes[] = $vote;
}
else {
$highest_weight = db_result(db_query("SELECT MAX(weight) FROM {advpoll_choices} WHERE nid = %d", $node->nid));
$next_weight = $highest_weight ? $highest_weight + 1 : 1;
db_query("INSERT INTO {advpoll_choices} (nid, label, weight, writein) VALUES (%d, '%s', %d, 1)", $node->nid, check_plain($form_state['values']['writein_choice']), $next_weight);
$next_cid = db_result(db_query("SELECT cid FROM {advpoll_choices} WHERE nid = %d AND label = '%s'", $node->nid, $form_state['values']['writein_choice']));
$vote = array();
$vote['value'] = $vote_value;
$vote['tag'] = $next_cid;
$vote['value_type'] = 'option';
$vote['content_type'] = 'advpoll';
$vote['content_id'] = $node->nid;
$vote = _advpoll_votesettings($vote, $node->nid);
$votes[] = $vote;
}
}
}
function advpoll_writein_merge_form(&$form_state, $node) {
$form = array();
$form['fieldset'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#title' => t('Merge write-ins'),
);
$form['fieldset']['note'] = array(
'#value' => '<div class="description">' . t('This will delete the write-in and change any votes for it into votes for the selected chocie.') . '</div>',
);
$form['fieldset']['merge'] = array(
'#prefix' => '<div class="container-inline">' . t('Merge') . ' ',
'#suffix' => '</div>',
);
$writein_list = array();
$choice_list = array();
foreach ($node->choice as $index => $choice) {
$choice_list[$index] = $choice['label'];
if ($choice['writein']) {
$writein_list[$index] = $choice['label'];
}
}
$form['fieldset']['merge']['source'] = array(
'#type' => 'select',
'#options' => $writein_list,
);
$form['fieldset']['merge']['into'] = array(
'#value' => t(' into '),
);
$form['fieldset']['merge']['destination'] = array(
'#type' => 'select',
'#options' => $choice_list,
);
$form['fieldset']['merge']['submit'] = array(
'#type' => 'submit',
'#value' => t('Merge'),
);
$form['nid'] = array(
'#type' => 'value',
'#value' => $node->nid,
);
$form['#node'] = $node;
return $form;
}
function advpoll_writein_merge_form_validate($form, &$form_state) {
if ($form_state['values']['source'] == $form_state['values']['destination']) {
form_set_error('destination', t('The write-in cannot be merged into itself.'));
}
}
function advpoll_writein_merge_form_submit($form, &$form_state) {
$node = $form['#node'];
$raw_votes = db_query('SELECT * FROM {votingapi_vote} WHERE content_id = %d', $form_state['values']['nid']);
$voters = array();
$affected_voters = array();
while ($vote = db_fetch_object($raw_votes)) {
$key = $vote->uid . '-' . $vote->vote_source;
if (!isset($voters[$key])) {
$voters[$key] = array();
}
array_push($voters[$key], $vote);
if ($vote->tag == $form_state['values']['source']) {
$affected_voters[$key] = count($voters[$key]) - 1;
}
}
foreach ($affected_voters as $key => $source_index) {
$voted_for_destination = FALSE;
foreach ($voters[$key] as $index => $vote) {
if ($vote->tag == $form_state['values']['destination']) {
$voted_for_destination = TRUE;
break;
}
}
if ($voted_for_destination) {
db_query('DELETE FROM {votingapi_vote} WHERE vote_id = %d AND tag = %d', $voters[$key][$index]->vote_id, $form_state['values']['source']);
}
else {
db_query('UPDATE {votingapi_vote} SET tag = %d WHERE vote_id = %d AND tag = %d', $form_state['values']['destination'], $voters[$key][$index]->vote_id, $form_state['values']['source']);
}
}
db_query('DELETE FROM {advpoll_choices} WHERE cid = %d', $form_state['values']['source']);
votingapi_recalculate_results('advpoll', $form_state['values']['nid']);
drupal_set_message(t('Write-in merged.'));
unset($_REQUEST['destination']);
$writeins_remaining = FALSE;
foreach ($node->choice as $choice) {
if ($choice['writein'] && $choice['cid'] != $form_state['values']['source']) {
$writeins_remaining = TRUE;
break;
}
}
drupal_goto('node/' . $form_state['values']['nid'] . ($writeins_remaining ? '/writeins' : ''));
}
function advpoll_writein_promote_form(&$form_state, $node) {
$form = array();
$form['fieldset'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#title' => t('Promote write-ins'),
);
$form['fieldset']['note'] = array(
'#value' => '<p class="description">' . t('Write-ins can be converted to regular choices. This is useful if users cannot see past write-ins but you want to promote specific write-ins so that they can be seen by users who vote in the future.') . '</p>',
);
$writein_list = array();
foreach ($node->choice as $index => $choice) {
if ($choice['writein']) {
$writein_list[$index] = $choice['label'];
}
}
$form['fieldset']['promote'] = array(
'#type' => 'checkboxes',
'#options' => $writein_list,
);
$form['fieldset']['submit'] = array(
'#type' => 'submit',
'#value' => t('Promote'),
);
$form['nid'] = array(
'#type' => 'value',
'#value' => $node->nid,
);
return $form;
}
function advpoll_writein_promote_form_submit($form, &$form_state) {
if (count($form_state['values']['promote'])) {
db_query('UPDATE {advpoll_choices} SET writein = 0 WHERE nid = %d AND cid IN(%s)', $form_state['values']['nid'], implode(', ', $form_state['values']['promote']));
drupal_set_message(format_plural(count($form_state['values']['promote']), 'Write-in promoted.', 'Write-ins promoted.'));
}
drupal_goto('node/' . $form_state['values']['nid']);
}
function advpoll_theme_registry_alter(&$theme_registry) {
$theme_registry['node']['theme paths'][] = drupal_get_path('module', 'advpoll');
}
function advpoll_theme() {
return array(
'advpoll_page' => array(
'arguments' => array(
'' => '',
),
),
'advpoll_block_latest_poll' => array(
'arguments' => array(),
),
'advpoll_view_block_poll' => array(
'arguments' => array(
'nid' => NULL,
),
),
'advpoll_results' => array(
'arguments' => array(
'title' => '',
'results' => '',
'votes' => '',
'links' => '',
'nid' => '',
'voted' => '',
'cancel_vote' => '',
'footer_message',
),
),
'advpoll_bar' => array(
'arguments' => array(
'title' => '',
'percentage' => '',
'votes' => '',
'choice' => NULL,
),
),
'advpoll_voting_ranking_form' => array(
'template' => 'advpoll-display-ranking-form',
'file' => 'modes/ranking.inc',
'arguments' => array(
'form' => NULL,
),
),
'advpoll_voting_binary_form' => array(
'template' => 'advpoll-display-binary-form',
'file' => 'modes/binary.inc',
'arguments' => array(
'form' => NULL,
),
),
);
}
function advpoll_js_more_choices() {
$form_state = array(
'storage' => NULL,
'submitted' => FALSE,
);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
$choice_form = $form['choice_wrapper']['choice'];
unset($choice_form['#prefix'], $choice_form['#suffix']);
$output = theme('status_messages') . drupal_render($choice_form);
drupal_json(array(
'status' => TRUE,
'data' => $output,
));
}
function advpoll_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'advpoll') . '/views',
);
}
function advpoll_votingapi_relationships() {
$relationships[] = array(
'description' => t('Advanced Poll'),
'content_type' => 'advpoll',
'base_table' => 'node',
'content_id_column' => 'nid',
);
return $relationships;
}
function _advpoll_votesettings($vote, $nid) {
$advpollSettings = variable_get('advpoll_settings', array());
$votingMode = $advpollSettings['voting_mode'];
if ($votingMode) {
$vote['uid'] = 0;
$vote['vote_source'] = time();
if ($votingMode == 1) {
$duration = !empty($advpollSettings['cookie_duration']) ? $advpollSettings['cookie_duration'] : 60;
setcookie('advpollvote' . $nid, 'voted', time() + 60 * $duration);
}
}
return $vote;
}
function _advpoll_check_settings($node) {
$advpollSettings = variable_get('advpoll_settings', array());
$votingMode = !empty($advpollSettings['voting_mode']) ? $advpollSettings['voting_mode'] : '0';
$showResults = !empty($advpollSettings['show_results']) ? $advpollSettings['show_results'] : 'aftervote';
if ($showResults === 'afterclose') {
if (!$node->active) {
return true;
}
else {
return false;
}
}
if ($votingMode == 1 && isset($_COOKIE['advpollvote' . $node->nid])) {
return true;
}
if (!$votingMode && $node->voted) {
return true;
}
return false;
}