function advpoll_form in Advanced Poll 6
Same name and namespace in other branches
- 5 advpoll.module \advpoll_form()
- 6.3 advpoll.module \advpoll_form()
- 6.2 advpoll.module \advpoll_form()
Implementation of hook_form().
This hook displays the form necessary to create/edit the poll.
File
- ./
advpoll.module, line 297 - Advanced Poll - a sophisticated polling module for voting, elections, and group decision-making.
Code
function advpoll_form(&$node, $form_state) {
$mode = _advpoll_get_mode($node->type);
$type = node_get_types('type', $node);
$editing = isset($node->nid);
$form = array();
// Only add javascript once, even if _form is called multiple times.
static $add_js;
if (!$add_js) {
// Pass translatable strings
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);
}
if (variable_get('advpoll_use_question_' . $type->type, ADVPOLL_USE_QUESTION) || isset($node->question) && $node->question !== '') {
$form['question'] = array(
'#type' => 'textfield',
'#title' => t('Question'),
'#default_value' => $node->question,
'#maxlength' => 255,
);
}
$form['choices'] = array(
'#type' => 'hidden',
'#value' => $choices,
);
// Advanced Poll 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',
),
// Don't pollute $form['choice']
'#prefix' => '<div id="more-choices">',
'#suffix' => '</div>',
'#description' => t("If the number of choices above isn't enough, click hereto add more choices."),
'#weight' => 1,
);
// First, loop through any currently existing choices.
$current_choices = 0;
$default_choices = variable_get('advpoll_choices_' . $type->type, '');
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' => variable_get('advpoll_choice_max_length', 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;
}
// Now add on extra choices if we need to.
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' => variable_get('advpoll_choice_max_length', 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'),
'#default_value' => isset($node->max_choices) ? $node->max_choices : variable_get('advpoll_max_choices_' . $type->type, ADVPOLL_MAX_CHOICES),
'#options' => $max_choice_list,
'#description' => t('Limits the total number of choices voters may select.'),
);
$voting_algorithms = advpoll_algorithms($mode);
if (count($voting_algorithms) > 1) {
// Create a select field when the poll supports several algorithms.
$form['settings']['algorithm'] = array(
'#type' => 'select',
'#title' => t('Algorithm'),
'#options' => $voting_algorithms,
'#default_value' => isset($node->algorithm) ? $node->algorithm : variable_get('advpoll_algorithm_' . $type->type, key($voting_algorithms)),
'#description' => t('Voting algorithm to use to calculate the winner.'),
);
}
else {
// Pass the only algorithm as a value.
$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,
);
$default_start_date = '';
$default_end_date = '';
$time = format_date(time(), 'custom', 'Y-m-d H:i:s O');
// Specify default dates if default duration is set and we are creating a node.
if (variable_get('advpoll_runtime_' . $type->type, FALSE) && !$editing) {
$default_start_date = $time;
$default_end_date = format_date(time() + variable_get('advpoll_runtime_' . $type->type, ''), 'custom', 'Y-m-d H:i:s O');
}
$form['settings']['start_date'] = array(
'#type' => 'textfield',
'#title' => t('Starting date'),
'#description' => t('The date that the poll opens. Format: %time. Leave blank if you want the poll to open now.', array(
'%time' => $time,
)),
'#size' => 25,
'#maxlength' => 25,
// Use !empty() because 0 signifies that no date should be used.
'#default_value' => !empty($node->start_date) ? format_date($node->start_date, 'custom', 'Y-m-d H:i:s O') : $default_start_date,
);
$form['settings']['end_date'] = array(
'#type' => 'textfield',
'#title' => t('Ending date'),
'#description' => t('The date that the poll closes. Format: %time. Leave blank if you do not want the poll to close automatically.', array(
'%time' => $time,
)),
'#size' => 25,
'#maxlength' => 25,
// Use !empty() because 0 signifies that no date should be used.
'#default_value' => !empty($node->end_date) ? format_date($node->end_date, 'custom', 'Y-m-d H:i:s O') : $default_end_date,
);
// Settings available for users with 'administer polls' permission.
$default_use_list = isset($node->use_list) ? $node->use_list : variable_get('advpoll_electoral_list_' . $type->type, ADVPOLL_ELECTORAL_LIST);
$default_show_votes = isset($node->show_votes) ? $node->show_votes : variable_get('advpoll_show_votes_' . $type->type, ADVPOLL_SHOW_VOTES);
$default_writeins = isset($node->writeins) ? $node->writeins : variable_get('advpoll_writeins_' . $type->type, ADVPOLL_WRITEINS);
$default_show_writeins = isset($node->show_writeins) ? $node->show_writeins : variable_get('advpoll_show_writeins_' . $type->type, 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,
);
}
else {
// Just pass the values for users without the "administer polls" permission.
$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;
}