Skip to content
Snippets Groups Projects
Commit ac27d4bf authored by Steven Wittens's avatar Steven Wittens
Browse files

Fixed broken poll results:

- If you are allowed to vote, results are now shown on a separate node tab "node/id/results".
- Poll voting now submits to a separate URL and uses drupal_goto to go back to the poll node
parent d615cb24
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -186,21 +186,6 @@ function poll_link($type, $node = 0, $main) { ...@@ -186,21 +186,6 @@ function poll_link($type, $node = 0, $main) {
if ($type == 'page' && user_access('access content')) { if ($type == 'page' && user_access('access content')) {
$links[] = l(t('polls'), 'poll', array('title' => t('View the list of polls on this site.'))); $links[] = l(t('polls'), 'poll', array('title' => t('View the list of polls on this site.')));
} }
else if ($type == 'node' && $node->type == 'poll') {
/*
** Add links to allow the user to switch between the results and the voting
** form, if he/she hasn't voted yet.
*/
if ($node->allowvotes) {
if (arg(3) == 'results') {
$links[] = l(t('voting form'), 'node/'. $node->nid);
}
else {
$links[] = l(t('view results'), 'node/'. $node->nid .'/results');
}
}
}
return $links; return $links;
} }
...@@ -216,6 +201,25 @@ function poll_menu() { ...@@ -216,6 +201,25 @@ function poll_menu() {
'callback' => 'poll_page', 'callback' => 'poll_page',
'access' => user_access('access content'), 'access' => user_access('access content'),
'type' => MENU_SUGGESTED_ITEM); 'type' => MENU_SUGGESTED_ITEM);
$items[] = array('path' => 'poll/vote',
'title' => t('vote'),
'callback' => 'poll_vote',
'access' => user_access('vote on polls'),
'type' => MENU_CALLBACK);
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(array('nid' => arg(1)));
if ($node->type == 'poll' && $node->allowvotes) {
$items[] = array('path' => 'node/'. arg(1) .'/results',
'title' => t('results'),
'callback' => 'poll_results',
'access' => user_access('access content'),
'weight' => 3,
'type' => MENU_LOCAL_TASK);
}
}
return $items; return $items;
} }
...@@ -315,7 +319,7 @@ function poll_view_voting(&$node, $main, $page, $block) { ...@@ -315,7 +319,7 @@ function poll_view_voting(&$node, $main, $page, $block) {
$form .= form_hidden('nid', $node->nid); $form .= form_hidden('nid', $node->nid);
$form .= form_submit(t('Vote'), 'vote') .'</div>'; $form .= form_submit(t('Vote'), 'vote') .'</div>';
$output .= form($form, 'post', url('node/'. $node->nid)); $output .= form($form, 'post', url('poll/vote/'. $node->nid));
$output .= '</div>'; $output .= '</div>';
return $output; return $output;
...@@ -355,12 +359,28 @@ function poll_view_results(&$node, $main, $page, $block) { ...@@ -355,12 +359,28 @@ function poll_view_results(&$node, $main, $page, $block) {
return $output; return $output;
} }
function poll_view_processvote(&$node) { /**
$edit = $_POST['edit']; * Callback for the 'results' tab for polls you can vote on
$choice = $edit['choice']; */
$vote = $_POST['vote']; function poll_results() {
if ($node = node_load(array('nid' => arg(1)))) {
print theme('page', node_show($node, 0), $node->title);
}
else {
drupal_not_found();
}
}
/**
* Callback for processing a vote
*/
function poll_vote(&$node) {
$nid = arg(2);
if ($node = node_load(array('nid' => $nid))) {
$edit = $_POST['edit'];
$choice = $edit['choice'];
$vote = $_POST['vote'];
if ($vote == t('Vote')) {
if (isset($choice) && isset($node->choice[$choice])) { if (isset($choice) && isset($node->choice[$choice])) {
if ($node->allowvotes) { if ($node->allowvotes) {
$id = poll_uid(); $id = poll_uid();
...@@ -378,6 +398,11 @@ function poll_view_processvote(&$node) { ...@@ -378,6 +398,11 @@ function poll_view_processvote(&$node) {
else { else {
drupal_set_message(t("You didn't specify a valid poll choice."), 'error'); drupal_set_message(t("You didn't specify a valid poll choice."), 'error');
} }
drupal_goto('node/'. $nid);
}
else {
drupal_not_found();
} }
} }
...@@ -391,12 +416,7 @@ function poll_view_processvote(&$node) { ...@@ -391,12 +416,7 @@ function poll_view_processvote(&$node) {
function poll_view(&$node, $main = 0, $page = 0, $block = 0) { function poll_view(&$node, $main = 0, $page = 0, $block = 0) {
global $user; global $user;
if (!$block) { if ($node->allowvotes && ($block || arg(2) != 'results')) {
// Because the voting form is embedded in the node-display, we process the data here
poll_view_processvote($node);
}
if ($node->allowvotes && (arg(2) != $node->nid || arg(3) != 'results')) {
$output .= poll_view_voting($node, $main, $page, $block); $output .= poll_view_voting($node, $main, $page, $block);
} }
else { else {
...@@ -410,6 +430,9 @@ function poll_view(&$node, $main = 0, $page = 0, $block = 0) { ...@@ -410,6 +430,9 @@ function poll_view(&$node, $main = 0, $page = 0, $block = 0) {
$links = link_node($node, $main); $links = link_node($node, $main);
$links[] = l(t('older polls'), 'poll', array('title' => t('View the list of polls on this site.'))); $links[] = l(t('older polls'), 'poll', array('title' => t('View the list of polls on this site.')));
if ($node->allowvotes && $block) {
$links[] = l(t('results'), 'node/'. $node->nid .'/results', array('title' => t('View the current poll results.')));
}
$output .= '<div class="links">'. theme("links", $links) .'</div>'; $output .= '<div class="links">'. theme("links", $links) .'</div>';
} }
......
...@@ -186,21 +186,6 @@ function poll_link($type, $node = 0, $main) { ...@@ -186,21 +186,6 @@ function poll_link($type, $node = 0, $main) {
if ($type == 'page' && user_access('access content')) { if ($type == 'page' && user_access('access content')) {
$links[] = l(t('polls'), 'poll', array('title' => t('View the list of polls on this site.'))); $links[] = l(t('polls'), 'poll', array('title' => t('View the list of polls on this site.')));
} }
else if ($type == 'node' && $node->type == 'poll') {
/*
** Add links to allow the user to switch between the results and the voting
** form, if he/she hasn't voted yet.
*/
if ($node->allowvotes) {
if (arg(3) == 'results') {
$links[] = l(t('voting form'), 'node/'. $node->nid);
}
else {
$links[] = l(t('view results'), 'node/'. $node->nid .'/results');
}
}
}
return $links; return $links;
} }
...@@ -216,6 +201,25 @@ function poll_menu() { ...@@ -216,6 +201,25 @@ function poll_menu() {
'callback' => 'poll_page', 'callback' => 'poll_page',
'access' => user_access('access content'), 'access' => user_access('access content'),
'type' => MENU_SUGGESTED_ITEM); 'type' => MENU_SUGGESTED_ITEM);
$items[] = array('path' => 'poll/vote',
'title' => t('vote'),
'callback' => 'poll_vote',
'access' => user_access('vote on polls'),
'type' => MENU_CALLBACK);
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(array('nid' => arg(1)));
if ($node->type == 'poll' && $node->allowvotes) {
$items[] = array('path' => 'node/'. arg(1) .'/results',
'title' => t('results'),
'callback' => 'poll_results',
'access' => user_access('access content'),
'weight' => 3,
'type' => MENU_LOCAL_TASK);
}
}
return $items; return $items;
} }
...@@ -315,7 +319,7 @@ function poll_view_voting(&$node, $main, $page, $block) { ...@@ -315,7 +319,7 @@ function poll_view_voting(&$node, $main, $page, $block) {
$form .= form_hidden('nid', $node->nid); $form .= form_hidden('nid', $node->nid);
$form .= form_submit(t('Vote'), 'vote') .'</div>'; $form .= form_submit(t('Vote'), 'vote') .'</div>';
$output .= form($form, 'post', url('node/'. $node->nid)); $output .= form($form, 'post', url('poll/vote/'. $node->nid));
$output .= '</div>'; $output .= '</div>';
return $output; return $output;
...@@ -355,12 +359,28 @@ function poll_view_results(&$node, $main, $page, $block) { ...@@ -355,12 +359,28 @@ function poll_view_results(&$node, $main, $page, $block) {
return $output; return $output;
} }
function poll_view_processvote(&$node) { /**
$edit = $_POST['edit']; * Callback for the 'results' tab for polls you can vote on
$choice = $edit['choice']; */
$vote = $_POST['vote']; function poll_results() {
if ($node = node_load(array('nid' => arg(1)))) {
print theme('page', node_show($node, 0), $node->title);
}
else {
drupal_not_found();
}
}
/**
* Callback for processing a vote
*/
function poll_vote(&$node) {
$nid = arg(2);
if ($node = node_load(array('nid' => $nid))) {
$edit = $_POST['edit'];
$choice = $edit['choice'];
$vote = $_POST['vote'];
if ($vote == t('Vote')) {
if (isset($choice) && isset($node->choice[$choice])) { if (isset($choice) && isset($node->choice[$choice])) {
if ($node->allowvotes) { if ($node->allowvotes) {
$id = poll_uid(); $id = poll_uid();
...@@ -378,6 +398,11 @@ function poll_view_processvote(&$node) { ...@@ -378,6 +398,11 @@ function poll_view_processvote(&$node) {
else { else {
drupal_set_message(t("You didn't specify a valid poll choice."), 'error'); drupal_set_message(t("You didn't specify a valid poll choice."), 'error');
} }
drupal_goto('node/'. $nid);
}
else {
drupal_not_found();
} }
} }
...@@ -391,12 +416,7 @@ function poll_view_processvote(&$node) { ...@@ -391,12 +416,7 @@ function poll_view_processvote(&$node) {
function poll_view(&$node, $main = 0, $page = 0, $block = 0) { function poll_view(&$node, $main = 0, $page = 0, $block = 0) {
global $user; global $user;
if (!$block) { if ($node->allowvotes && ($block || arg(2) != 'results')) {
// Because the voting form is embedded in the node-display, we process the data here
poll_view_processvote($node);
}
if ($node->allowvotes && (arg(2) != $node->nid || arg(3) != 'results')) {
$output .= poll_view_voting($node, $main, $page, $block); $output .= poll_view_voting($node, $main, $page, $block);
} }
else { else {
...@@ -410,6 +430,9 @@ function poll_view(&$node, $main = 0, $page = 0, $block = 0) { ...@@ -410,6 +430,9 @@ function poll_view(&$node, $main = 0, $page = 0, $block = 0) {
$links = link_node($node, $main); $links = link_node($node, $main);
$links[] = l(t('older polls'), 'poll', array('title' => t('View the list of polls on this site.'))); $links[] = l(t('older polls'), 'poll', array('title' => t('View the list of polls on this site.')));
if ($node->allowvotes && $block) {
$links[] = l(t('results'), 'node/'. $node->nid .'/results', array('title' => t('View the current poll results.')));
}
$output .= '<div class="links">'. theme("links", $links) .'</div>'; $output .= '<div class="links">'. theme("links", $links) .'</div>';
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment