| 1 |
<?php
|
| 2 |
/* $Id: simplevote.module,v 1.10 2006/05/05 04:57:09 eaton Exp $ */
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* A demonstration of VotingAPI.
|
| 7 |
*/
|
| 8 |
|
| 9 |
function simplevote_menu($may_cache) {
|
| 10 |
theme('add_style', theme('simplevote_css_path'));
|
| 11 |
$items = array();
|
| 12 |
if ($may_cache) {
|
| 13 |
$items[] = array('path' => 'vote', 'title' => t('vote on content'),
|
| 14 |
'callback' => 'simplevote_vote', 'access' => true, 'type' => MENU_CALLBACK);
|
| 15 |
}
|
| 16 |
return $items;
|
| 17 |
}
|
| 18 |
|
| 19 |
function simplevote_help($section) {
|
| 20 |
switch ($section) {
|
| 21 |
case 'admin/modules#description':
|
| 22 |
return t('Adds a five-star vote widget to every node and comment.');
|
| 23 |
break;
|
| 24 |
}
|
| 25 |
}
|
| 26 |
|
| 27 |
function simplevote_vote($type, $cid, $value) {
|
| 28 |
// sanity-check the incoming values.
|
| 29 |
if (is_numeric($cid) && is_numeric($value)) {
|
| 30 |
if ($value > 100) {
|
| 31 |
$value = 100;
|
| 32 |
}
|
| 33 |
|
| 34 |
$vote->value = $value;
|
| 35 |
$vote->value_type = 'percent';
|
| 36 |
$vote->tag = VOTINGAPI_VALUE_DEFAULT_TAG;
|
| 37 |
|
| 38 |
votingapi_set_vote($type, $cid, $vote);
|
| 39 |
}
|
| 40 |
drupal_goto(drupal_get_destination());
|
| 41 |
}
|
| 42 |
|
| 43 |
function theme_simplevote_widget($cid, $type) {
|
| 44 |
// Get the current vote. It should come in as a percentage,
|
| 45 |
$vote = votingapi_get_voting_result($type, $cid, 'percent', VOTINGAPI_VALUE_DEFAULT_TAG, 'average');
|
| 46 |
|
| 47 |
if ($vote) {
|
| 48 |
$stars = round($vote->value);
|
| 49 |
}
|
| 50 |
else {
|
| 51 |
$stars = 0;
|
| 52 |
}
|
| 53 |
|
| 54 |
$output = '<div class="simplevote_widget">' . t("Rating");
|
| 55 |
for ($i = 20; $i <= 100; $i += 20) {
|
| 56 |
$output .= theme('simplevote_icon', $type, $cid, $i, $stars >= $i);
|
| 57 |
}
|
| 58 |
$output .= '</div>';
|
| 59 |
return $output;
|
| 60 |
}
|
| 61 |
|
| 62 |
function theme_simplevote_icon($type, $cid, $value, $filled) {
|
| 63 |
global $user;
|
| 64 |
$url = 'vote/' . $type . '/' . $cid . '/' . $value;
|
| 65 |
|
| 66 |
if ($filled) {
|
| 67 |
$class = 'vote-on';
|
| 68 |
}
|
| 69 |
else {
|
| 70 |
$class = 'vote-off';
|
| 71 |
}
|
| 72 |
|
| 73 |
// If the user isn't logged in, show the vote but don't let them change it.
|
| 74 |
// We do this by writing out a span with the same classes, rather than an <A> tag.
|
| 75 |
if ($user->uid == 0) {
|
| 76 |
return '<span class="'.$class.'"></span>';
|
| 77 |
}
|
| 78 |
else {
|
| 79 |
return l('', $url, array('class' => $class), drupal_get_destination(), NULL, FALSE, TRUE);
|
| 80 |
}
|
| 81 |
}
|
| 82 |
|
| 83 |
function theme_simplevote_css_path() {
|
| 84 |
return drupal_get_path('module', 'simplevote') . '/theme/simplevote.css';
|
| 85 |
}
|
| 86 |
|
| 87 |
function simplevote_nodeapi(&$node, $op, $teaser, $page) {
|
| 88 |
switch ($op) {
|
| 89 |
case 'view':
|
| 90 |
if ($teaser == false) {
|
| 91 |
$node->body = theme('simplevote_widget', $node->nid, 'node') . $node->body;
|
| 92 |
}
|
| 93 |
break;
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|
| 97 |
function simplevote_votingapi_format($vobj, $field) {
|
| 98 |
if ($vobj->value_type == 'percent') {
|
| 99 |
switch ($field) {
|
| 100 |
case 'value':
|
| 101 |
$score = round($vobj->value / 20, 1);
|
| 102 |
return $score . ' stars';
|
| 103 |
break;
|
| 104 |
case 'result':
|
| 105 |
switch ($vobj->function) {
|
| 106 |
case 'average':
|
| 107 |
$score = round($vobj->value / 20, 1);
|
| 108 |
return $score . ' stars';
|
| 109 |
break;
|
| 110 |
case 'count':
|
| 111 |
return $vobj->value . ' votes';
|
| 112 |
break;
|
| 113 |
}
|
| 114 |
break;
|
| 115 |
}
|
| 116 |
}
|
| 117 |
}
|
| 118 |
|
| 119 |
function simplevote_votingapi_action_sets() {
|
| 120 |
$sets = array(
|
| 121 |
'simple_promotion' => array(
|
| 122 |
'description' => 'Promotes a node to the front page if enough users rate it highly.',
|
| 123 |
'content_type' => 'node',
|
| 124 |
'condition_mask' => 'AND',
|
| 125 |
'enabled' => 1,
|
| 126 |
'source' => 'SimpleVote',
|
| 127 |
|
| 128 |
'conditions' => array(
|
| 129 |
'is_promoted' => array(
|
| 130 |
'description' => 'Node is not promoted',
|
| 131 |
'handler' => 'votingapi_node_properties_handler',
|
| 132 |
'data' => array(
|
| 133 |
'property' => 'promote',
|
| 134 |
'comparison' => '!=',
|
| 135 |
'value' => 1,
|
| 136 |
),
|
| 137 |
),
|
| 138 |
'min_voters' => array(
|
| 139 |
'description' => 'More than 5 voters',
|
| 140 |
'handler' => 'votingapi_vote_result_handler',
|
| 141 |
'data' => array(
|
| 142 |
'function' => 'count',
|
| 143 |
'tag' => 'vote',
|
| 144 |
'comparison' => '>',
|
| 145 |
'value' => 5,
|
| 146 |
),
|
| 147 |
),
|
| 148 |
'min_vote' => array(
|
| 149 |
'description' => 'Average higher than 75',
|
| 150 |
'handler' => 'votingapi_vote_result_handler',
|
| 151 |
'data' => array(
|
| 152 |
'function' => 'average',
|
| 153 |
'tag' => 'vote',
|
| 154 |
'comparison' => '>',
|
| 155 |
'value' => 75,
|
| 156 |
),
|
| 157 |
),
|
| 158 |
),
|
| 159 |
'actions' => array(
|
| 160 |
'action_node_promote',
|
| 161 |
),
|
| 162 |
),
|
| 163 |
);
|
| 164 |
|
| 165 |
return $sets;
|
| 166 |
}
|
| 167 |
|
| 168 |
function simplevote_elements() {
|
| 169 |
// A simple element that maps n points to a list of radio butons.
|
| 170 |
// Useful for 'five star' rating widgets and so on.
|
| 171 |
$type['rating'] = array(
|
| 172 |
'#input' => TRUE,
|
| 173 |
'#first' => 1,
|
| 174 |
'#points' => 5,
|
| 175 |
'#process' => array('expand_rating' => array())
|
| 176 |
);
|
| 177 |
return $type;
|
| 178 |
}
|
| 179 |
|
| 180 |
function theme_rating($element) {
|
| 181 |
if ($element['#title'] || $element['#description']) {
|
| 182 |
return theme('form_element', $element['#title'], '<div class="container-inline">' . $element['#children'] . '</div>', $element['#description'], $element['#id'], $element['#required'], form_get_error($element));
|
| 183 |
}
|
| 184 |
else {
|
| 185 |
return '<div class="container-inline">' . $element['#children'] . '</div>';
|
| 186 |
}
|
| 187 |
}
|
| 188 |
|
| 189 |
/**
|
| 190 |
* Roll out a single radios element to a list of radios,
|
| 191 |
* using the options array as index.
|
| 192 |
*/
|
| 193 |
function expand_rating($element) {
|
| 194 |
if ($element['#points'] > 1) {
|
| 195 |
for ($i = $element['#first']; $i <= $element['#points']; $i++) {
|
| 196 |
$element[$i] = array(
|
| 197 |
'#type' => 'radio',
|
| 198 |
'#return_value' => $i,
|
| 199 |
'#default_value' => $element['#default_value'],
|
| 200 |
'#parents' => $element['#parents'],
|
| 201 |
'#spawned' => TRUE,
|
| 202 |
);
|
| 203 |
}
|
| 204 |
}
|
| 205 |
return $element;
|
| 206 |
}
|