| 1 |
<?php
|
| 2 |
// $Id: poll.tokens.inc,v 1.1 2009/08/19 20:19:36 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Builds placeholder replacement tokens for values specific to Poll nodes.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implement hook_token_info().
|
| 11 |
*/
|
| 12 |
function poll_token_info() {
|
| 13 |
$node['poll-votes'] = array(
|
| 14 |
'name' => t("Poll votes"),
|
| 15 |
'description' => t("The number of votes that have been cast on a poll node."),
|
| 16 |
);
|
| 17 |
$node['poll-winner'] = array(
|
| 18 |
'name' => t("Poll winner"),
|
| 19 |
'description' => t("The winning poll answer."),
|
| 20 |
);
|
| 21 |
$node['poll-winner-votes'] = array(
|
| 22 |
'name' => t("Poll winner votes"),
|
| 23 |
'description' => t("The number of votes received by the winning poll answer."),
|
| 24 |
);
|
| 25 |
$node['poll-winner-percent'] = array(
|
| 26 |
'name' => t("Poll winner percent"),
|
| 27 |
'description' => t("The percentage of votes received by the winning poll answer."),
|
| 28 |
);
|
| 29 |
$node['poll-duration'] = array(
|
| 30 |
'name' => t("Poll duration"),
|
| 31 |
'description' => t("The length of time the poll node is set to run."),
|
| 32 |
);
|
| 33 |
|
| 34 |
return array(
|
| 35 |
'tokens' => array('node' => $node),
|
| 36 |
);
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* Implement hook_tokens().
|
| 41 |
*/
|
| 42 |
function poll_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
| 43 |
$url_options = array('absolute' => TRUE);
|
| 44 |
$sanitize = !empty($options['sanitize']);
|
| 45 |
$replacements = array();
|
| 46 |
|
| 47 |
if ($type == 'node' && !empty($data['node']) && $data['node']->type == 'poll') {
|
| 48 |
$node = $data['node'];
|
| 49 |
|
| 50 |
$total_votes = 0;
|
| 51 |
$highest_votes = 0;
|
| 52 |
foreach ($node->choice as $choice) {
|
| 53 |
if ($choice['chvotes'] > $highest_votes) {
|
| 54 |
$winner = $choice;
|
| 55 |
$highest_votes = $choice['chvotes'];
|
| 56 |
}
|
| 57 |
$total_votes = $total_votes + $choice['chvotes'];
|
| 58 |
}
|
| 59 |
foreach ($tokens as $name => $original) {
|
| 60 |
switch ($name) {
|
| 61 |
case 'poll-votes':
|
| 62 |
$replacements[$original] = $total_votes;
|
| 63 |
break;
|
| 64 |
|
| 65 |
case 'poll-winner':
|
| 66 |
if (isset($winner)) {
|
| 67 |
$replacements[$original] = $sanitize ? filter_xss($winner['chtext']) : $winner['chtext'];
|
| 68 |
}
|
| 69 |
break;
|
| 70 |
|
| 71 |
case 'poll-winner-votes':
|
| 72 |
if (isset($winner)) {
|
| 73 |
$replacements[$original] = $winner['chvotes'];
|
| 74 |
}
|
| 75 |
break;
|
| 76 |
|
| 77 |
case 'poll-winner-percent':
|
| 78 |
if (isset($winner)) {
|
| 79 |
$percent = ($winner['chvotes'] / $total_votes) * 100;
|
| 80 |
$replacements[$original] = number_format($percent, 0);
|
| 81 |
}
|
| 82 |
break;
|
| 83 |
|
| 84 |
case 'poll-duration':
|
| 85 |
$replacements[$original] = format_interval($node->runtime, 1, $language_code);
|
| 86 |
break;
|
| 87 |
}
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
return $replacements;
|
| 92 |
}
|