function advpoll_ranking_submit in Advanced Poll 7.3
Same name and namespace in other branches
- 7 advpoll_ranking/advpoll_ranking.module \advpoll_ranking_submit()
- 7.2 advpoll_ranking/advpoll_ranking.module \advpoll_ranking_submit()
Submit handler for ranking polls.
1 string reference to 'advpoll_ranking_submit'
- advpoll_ranking_choice_form in advpoll_ranking/
advpoll_ranking.module - Form view for ranking forms used in Borda and Instant Run-off.
File
- advpoll_ranking/
advpoll_ranking.module, line 377
Code
function advpoll_ranking_submit($form, &$form_state) {
$data = advpoll_get_form_data($form_state, 1);
$count = count($data->choices);
$nid = $form_state['build_info']['args'][1]->nid;
$votes = array();
/* Even though content type is advpoll, we'll track type as ranking for the
* purpose of managing votes in the voting API table.
*/
$writein = '';
$message = advpoll_form_submit_check($data, $nid);
if ($message) {
$form['message'] = array(
'#type' => 'markup',
'#prefix' => '<div id="message">',
'#suffix' => '</div>',
'#markup' => $message,
);
return $form;
}
// Check to see if a write-in exists and was filled in.
if ($data->write_in) {
if (isset($form_state['input']['write_in'])) {
$writein = trim($form_state['input']['write_in']);
$writein_weight = $form_state['input']['write_in_weight'];
// Sanitize and check to see if there's a valid write in afterward.
// Note - no reason for a normal user to add markup so strip it.
$writein = filter_xss($writein);
$writein = check_plain($writein);
// Check for conditions under which checking for write in is appropriate.
if ($writein_weight == 'no-js' || $writein_weight > 0) {
if ($writein) {
$writein_choice = advpoll_process_writein($nid, $writein, $data);
$data->choices[] = $writein_choice;
if ($writein_weight == 'no-js') {
$writein_weight = 1;
}
$votes[] = array(
'rank' => $writein_weight,
'id' => $writein_choice['choice_id'],
);
}
elseif ($writein_weight != 'no-js') {
/* Assumes the user placed it in selection table but did not fill in
* the form element.
*/
$form['message'] = array(
'#type' => 'markup',
'#prefix' => '<div id="message">',
'#suffix' => '</div>',
'#markup' => t('Please type in a valid write-in choice or select a different option.'),
);
return $form;
}
}
}
}
/* If the poll only allows one vote but there is a vote, that is the write in
* value. No need to process. Otherwise, we need to process it.
*/
if ($data->max_choices > 1 || !$votes) {
$votes = advpoll_ranking_process_results($form_state['input']['choice'], $data->choices, $votes);
}
if (count($votes) > 0 && count($votes) <= $data->max_choices) {
advpoll_ranking_process_votes($data, $nid, $votes);
if ($data->behavior == 'borda' || $data->behavior == 'borda_all') {
$element['#markup'] = advpoll_display_borda_results($nid, $data);
}
else {
$element['#markup'] = advpoll_display_runoff_results($nid, $data);
}
return $element;
}
else {
$form['message'] = array(
'#type' => 'markup',
'#prefix' => '<div id="message">',
'#suffix' => '</div>',
'#markup' => t('Select up to @quantity @votes.', array(
'@quantity' => $data->max_choices,
'@votes' => format_plural($data->max_choices, 'vote', 'votes'),
)),
);
return $form;
}
}