poll.tokens.inc 2.8 KB

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