| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
*
|
| 6 |
* This file implements a selection voting mode
|
| 7 |
*/
|
| 8 |
|
| 9 |
// $Id: selection.module,v 1.13 2009/07/27 23:25:43 anarcat Exp $
|
| 10 |
|
| 11 |
function selection_help($section) {
|
| 12 |
$help = "";
|
| 13 |
switch ($section) {
|
| 14 |
case 'admin/modules#description':
|
| 15 |
break;
|
| 16 |
default:
|
| 17 |
if ($section == 'node/add#decisions-selection') {
|
| 18 |
$help = t('Creates a decision where the user selects one or many options.');
|
| 19 |
}
|
| 20 |
break;
|
| 21 |
}
|
| 22 |
return $help;
|
| 23 |
}
|
| 24 |
|
| 25 |
function selection_node_info() {
|
| 26 |
return array('decisions_selection' => array('name' => t('Decisions - selection'), 'module' => 'decisions', 'description' => t('Creates a decision where the user selects one or many options.')));
|
| 27 |
}
|
| 28 |
|
| 29 |
function selection_perm() { return decisions_perm(); }
|
| 30 |
function selection_acces($op, $node, $account) { return decisions_access($op, $node, $account); }
|
| 31 |
function selection_form(&$node) { return decisions_form($node); }
|
| 32 |
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Implementation of the decisions_algorithms() hook
|
| 36 |
*/
|
| 37 |
function selection_decisions_algorithms() {
|
| 38 |
return array('plurality' => t('The plurality algorithm determines the winner based solely on the number of votes received: the one with the larger number of votes wins. See the <a href="http://en.wikipedia.org/wiki/Plurality_voting_system">Plurality voting system on Wikipedia</a> for more information'));
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Implementation of the decisions_voting_hook_form() hook for the selection module.
|
| 43 |
*
|
| 44 |
* This creates a list of choices to allow the user to vote on choices.
|
| 45 |
*/
|
| 46 |
function selection_decisions_voting_form(&$node, $teaser, $page) {
|
| 47 |
if ($node->choice) {
|
| 48 |
$list = array();
|
| 49 |
|
| 50 |
// Put options in random order if randomize option
|
| 51 |
// selected on node create/edit form.
|
| 52 |
if($node->randomize) {
|
| 53 |
$node->choice = _decisions_randomize_options($node->choice, $node->choices);
|
| 54 |
}
|
| 55 |
|
| 56 |
if ($node->maxchoices == 1) {
|
| 57 |
// plurality voting
|
| 58 |
foreach ($node->choice as $i => $choice) {
|
| 59 |
if ($choice['label']) {
|
| 60 |
$list[$i] = check_plain($choice['label']);
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
$form['choice'] = array(
|
| 65 |
'#type' => 'radios',
|
| 66 |
'#title' => $page ? '' : check_plain($node->title),
|
| 67 |
'#default_value' => -1,
|
| 68 |
'#options' => $list
|
| 69 |
);
|
| 70 |
}
|
| 71 |
else {
|
| 72 |
// approval voting
|
| 73 |
foreach ($node->choice as $i => $choice) {
|
| 74 |
if ($choice['label']) {
|
| 75 |
$list[$i] = check_plain($choice['label']);
|
| 76 |
}
|
| 77 |
}
|
| 78 |
$form['choice'] = array(
|
| 79 |
'#type' => 'checkboxes',
|
| 80 |
'#title' => $page ? '' : check_plain($node->title),
|
| 81 |
'#options' => $list,
|
| 82 |
);
|
| 83 |
}
|
| 84 |
}
|
| 85 |
$form['nid'] = array(
|
| 86 |
'#type' => 'hidden',
|
| 87 |
'#value' => $node->nid
|
| 88 |
);
|
| 89 |
$form['vote'] = array(
|
| 90 |
'#type' => 'submit',
|
| 91 |
'#value' => t('Vote')
|
| 92 |
);
|
| 93 |
$form['#action'] = url('node/'. $node->nid);
|
| 94 |
return $form;
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Implementation of the decisions_view_results() hook for the selection module
|
| 99 |
*
|
| 100 |
* TODO: implement (http://drupal.org/node/48249)
|
| 101 |
*/
|
| 102 |
function selection_decisions_view_results($node, $teaser, $page) {
|
| 103 |
|
| 104 |
$output = "";
|
| 105 |
$results = votingapi_select_votes(array('content_id' => $node->nid, 'content_type' => 'decisions'));
|
| 106 |
// Count the votes for each choice
|
| 107 |
$votes = array();
|
| 108 |
|
| 109 |
foreach ($results as $result) {
|
| 110 |
|
| 111 |
// approval
|
| 112 |
$voteval = $result['tag'];
|
| 113 |
|
| 114 |
if (!array_key_exists($voteval, $votes)) {
|
| 115 |
$votes[$voteval] = 0;
|
| 116 |
}
|
| 117 |
// the value is the position chosen in this case, we just want to increment
|
| 118 |
$votes[$voteval]++;
|
| 119 |
}
|
| 120 |
|
| 121 |
$total_votes = array_sum($votes);
|
| 122 |
if ($node->choice && $total_votes > 0) {
|
| 123 |
// TODO: Those <div>s and <br />s should be in a theme function. First collect all the data in a structure, then theme it.
|
| 124 |
// display results for each possible choice
|
| 125 |
$output .= '<div class="poll">';
|
| 126 |
foreach ($node->choice as $i => $ch) {
|
| 127 |
if (!array_key_exists($i, $votes)) {
|
| 128 |
$votes[$i] = 0;
|
| 129 |
}
|
| 130 |
$percentage = round(100 * $votes[$i] / $total_votes, 0);
|
| 131 |
$output .= theme('decisions_bar', check_plain($ch['label']), $percentage, format_plural($votes[$i], '1 vote', '@count votes'));
|
| 132 |
}
|
| 133 |
$output .= '</div>';
|
| 134 |
}
|
| 135 |
|
| 136 |
$output .= '<br />';
|
| 137 |
return $output;
|
| 138 |
}
|
| 139 |
|
| 140 |
/**
|
| 141 |
* implementation of the format_votes() hook.
|
| 142 |
*
|
| 143 |
* formats how a user's votes should be displayed.
|
| 144 |
*
|
| 145 |
* @returns a formatted string
|
| 146 |
*/
|
| 147 |
function selection_decisions_format_votes($node, $votes) {
|
| 148 |
$unordered_votes = array();
|
| 149 |
foreach ($votes as $vote) {
|
| 150 |
// Just need one dimensional results
|
| 151 |
if ($vote->value > 0) {
|
| 152 |
$unordered_votes[] = check_plain($node->choice[$vote->tag]['label']);
|
| 153 |
}
|
| 154 |
}
|
| 155 |
return implode(', ', $unordered_votes);
|
| 156 |
}
|
| 157 |
|
| 158 |
|
| 159 |
/**
|
| 160 |
* implementation of the submit() hook
|
| 161 |
*
|
| 162 |
* registers the vote as a key for this node using votingapi_set_vote()
|
| 163 |
*/
|
| 164 |
function selection_decisions_vote($node, $form_values) {
|
| 165 |
$votes = array();
|
| 166 |
if ($node->maxchoices == 1) {
|
| 167 |
// plurality voting
|
| 168 |
$vote = array(
|
| 169 |
'value' => 1,
|
| 170 |
'tag' => $form_values['choice'],
|
| 171 |
'value_type' => 'option',
|
| 172 |
'content_type' => 'decisions',
|
| 173 |
'content_id' => $node->nid);
|
| 174 |
$votes[] = $vote;
|
| 175 |
}
|
| 176 |
else {
|
| 177 |
// approval voting
|
| 178 |
foreach ($node->choice as $key => $choice) {
|
| 179 |
if (isset($form_values['choice'][$key]) && $form_values['choice'][$key]) {
|
| 180 |
$vote = array(
|
| 181 |
'value' => 1,
|
| 182 |
'value_type' => 'option',
|
| 183 |
'tag' => $key,
|
| 184 |
'content_type' => 'decisions',
|
| 185 |
'content_id' => $node->nid
|
| 186 |
);
|
| 187 |
$votes[] = $vote;
|
| 188 |
}
|
| 189 |
}
|
| 190 |
}
|
| 191 |
votingapi_add_votes($votes);
|
| 192 |
}
|
| 193 |
|
| 194 |
/**
|
| 195 |
* implementation of the vote_validate() hook
|
| 196 |
*
|
| 197 |
* check if the submitted key exists, just to make sure the form is not bypassed.
|
| 198 |
*
|
| 199 |
* @returns boolean true if the form is valid
|
| 200 |
*/
|
| 201 |
function selection_decisions_vote_validate($node, $form_values) {
|
| 202 |
$ok = TRUE;
|
| 203 |
if ($node->maxchoices == 1) {
|
| 204 |
// plurality voting
|
| 205 |
if (!array_key_exists($form_values['choice'], $node->choice)) {
|
| 206 |
form_set_error('choice', 'At least one choice must be selected.');
|
| 207 |
$ok = FALSE;
|
| 208 |
}
|
| 209 |
}
|
| 210 |
else {
|
| 211 |
// approval voting
|
| 212 |
|
| 213 |
// array used to check which values are set
|
| 214 |
$setvalues = array();
|
| 215 |
foreach ($node->choice as $key => $choice) {
|
| 216 |
|
| 217 |
// need a minchoices choice to do this
|
| 218 |
//if (empty($_POST['edit']['Choice_' . $key])) {
|
| 219 |
//form_set_error('Choice_'.$key, "choice $key cannot be empty");
|
| 220 |
//$ok = FALSE;
|
| 221 |
//}
|
| 222 |
|
| 223 |
// see if the box is checked
|
| 224 |
if (!empty($form_values['choice'][$key])) {
|
| 225 |
$numchoices++;
|
| 226 |
}
|
| 227 |
}
|
| 228 |
|
| 229 |
// too many choices ranked
|
| 230 |
if ($node->maxchoices != 0 && $numchoices > $node->maxchoices) {
|
| 231 |
form_set_error('choice',
|
| 232 |
t('%num choices were selected but only %max are allowed.',
|
| 233 |
array('%num' => $numchoices, '%max' => $node->maxchoices)));
|
| 234 |
$ok = FALSE;
|
| 235 |
}
|
| 236 |
|
| 237 |
// not enough choices ranked
|
| 238 |
$minchoices = 1;
|
| 239 |
if ($numchoices < $minchoices) {
|
| 240 |
form_set_error('choice', t('At least one choice must be selected.'));
|
| 241 |
$ok = FALSE;
|
| 242 |
}
|
| 243 |
}
|
| 244 |
return $ok;
|
| 245 |
}
|
| 246 |
|