| 1 |
<?php
|
| 2 |
// $Id: smackdown.module,v 1.2 2007/10/15 17:15:57 sirkitree Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Smackdown module: pit two nodes against one another and vote on the winner.
|
| 7 |
* Choose what content type to reference in the smackdown content type settings.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_help().
|
| 12 |
*/
|
| 13 |
function smackdown_help($section) {
|
| 14 |
switch ($section) {
|
| 15 |
case 'admin/help#smackdown':
|
| 16 |
return t('<h3>Setup</h3>
|
| 17 |
<ol>
|
| 18 |
<li>Setup your module permissions in <a href="/admin/user/access">Access control</a>.</li>
|
| 19 |
<li>Edit the <a href="/admin/content/types/smackdown">smackdown content type</a> and select what content types you would like to use in smackdown creation.</li>
|
| 20 |
<li>Create your smackdowns!</li>
|
| 21 |
</ol>');
|
| 22 |
break;
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Implementation of hook_node_info().
|
| 28 |
*/
|
| 29 |
function smackdown_node_info() {
|
| 30 |
return array(
|
| 31 |
'smackdown' => array(
|
| 32 |
'name' => t('Smackdown'),
|
| 33 |
'module' => 'smackdown',
|
| 34 |
'description' => t("A smackdown pairs two things together and lets users vote on their favorite."),
|
| 35 |
)
|
| 36 |
);
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* Implementation of hook_perm().
|
| 41 |
*
|
| 42 |
* @todo:
|
| 43 |
* create permission for all node types to be referenced as a smackdown
|
| 44 |
*/
|
| 45 |
function smackdown_perm() {
|
| 46 |
return array(
|
| 47 |
'create smackdown',
|
| 48 |
'edit own smackdown',
|
| 49 |
'view smackdowns',
|
| 50 |
'view standings'
|
| 51 |
);
|
| 52 |
}
|
| 53 |
|
| 54 |
/**
|
| 55 |
* Implementation of hook_access().
|
| 56 |
*/
|
| 57 |
function smackdown_access($op, $node) {
|
| 58 |
global $user;
|
| 59 |
|
| 60 |
if ($op == 'create') {
|
| 61 |
// Only users with permission to do so may create this node type.
|
| 62 |
return user_access('create smackdown');
|
| 63 |
}
|
| 64 |
|
| 65 |
// Users who create a node may edit or delete it later, assuming they have the
|
| 66 |
// necessary permissions.
|
| 67 |
if ($op == 'update' || $op == 'delete') {
|
| 68 |
if (user_access('edit own smackdown') && ($user->uid == $node->uid)) {
|
| 69 |
return TRUE;
|
| 70 |
}
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
| 74 |
/**
|
| 75 |
* Implementation of hook_menu().
|
| 76 |
*/
|
| 77 |
function smackdown_menu($may_cache) {
|
| 78 |
|
| 79 |
$items = array();
|
| 80 |
|
| 81 |
if ($may_cache) {
|
| 82 |
$items[] = array(
|
| 83 |
'path' => 'smackdown',
|
| 84 |
'callback' => 'smackdown_page',
|
| 85 |
'type' => MENU_CALLBACK,
|
| 86 |
'access' => user_access('create smackdown'),
|
| 87 |
);
|
| 88 |
|
| 89 |
$items[] = array(
|
| 90 |
'path' => 'smackdown/autocomplete',
|
| 91 |
'title' => t('Smackdown autocomplete'),
|
| 92 |
'callback' => 'smackdown_autocomplete',
|
| 93 |
'type' => MENU_CALLBACK,
|
| 94 |
'access' => user_access('create smackdown'),
|
| 95 |
);
|
| 96 |
|
| 97 |
$items[] = array(
|
| 98 |
'path' => 'smackdown/taxonomy/js',
|
| 99 |
'callback' => 'smackdown_taxonomy_js',
|
| 100 |
'type' => MENU_CALLBACK,
|
| 101 |
'access' => true,
|
| 102 |
'access' => user_access('create smackdown'),
|
| 103 |
);
|
| 104 |
|
| 105 |
$items[] = array(
|
| 106 |
'path' => 'smackdown/standings',
|
| 107 |
'callback' => 'smackdown_standings_page',
|
| 108 |
'type' => DEFAULT_LOCAL_TASK,
|
| 109 |
'access' => user_access('create smackdown'),
|
| 110 |
);
|
| 111 |
|
| 112 |
$enabled = variable_get('smackdown_enabled_smackdown', array());
|
| 113 |
if ($enabled) {
|
| 114 |
foreach ($enabled as $type) {
|
| 115 |
$items[] = array(
|
| 116 |
'path' => 'smackdown/'. $type,
|
| 117 |
'title' => t($type .' Smackdowns'),
|
| 118 |
'type' => MENU_LOCAL_TASK,
|
| 119 |
'access' => user_access('view smackdowns'),
|
| 120 |
);
|
| 121 |
$items[] = array(
|
| 122 |
'path' => 'smackdown/standings/'. $type,
|
| 123 |
'title' => t($type .' Standings'),
|
| 124 |
'type' => MENU_LOCAL_TASK,
|
| 125 |
'access' => user_access('view standings'),
|
| 126 |
);
|
| 127 |
}
|
| 128 |
}
|
| 129 |
|
| 130 |
}
|
| 131 |
else {
|
| 132 |
|
| 133 |
drupal_add_css(drupal_get_path('module', 'smackdown') .'/smackdown.css');
|
| 134 |
|
| 135 |
}
|
| 136 |
|
| 137 |
return $items;
|
| 138 |
}
|
| 139 |
|
| 140 |
/**
|
| 141 |
* Implementation of hook_form_alter
|
| 142 |
*
|
| 143 |
* Adding settings to the smackdown content type. Choose which content type to reference.
|
| 144 |
*/
|
| 145 |
function smackdown_form_alter($form_id, &$form) {
|
| 146 |
if ($form_id == 'node_type_form' && isset($form['identity']['type']) && $form['#node_type']->type == 'smackdown') {
|
| 147 |
$types = node_get_types('types');
|
| 148 |
foreach ($types as $key => $type) {
|
| 149 |
if ($key != 'smackdown') {
|
| 150 |
$options[$key] = $type->type;
|
| 151 |
}
|
| 152 |
}
|
| 153 |
|
| 154 |
$form['workflow']['smackdown_enabled'] = array(
|
| 155 |
'#type' => 'checkboxes',
|
| 156 |
'#title' => t('Use Smackdown on'),
|
| 157 |
'#options' => $options,
|
| 158 |
'#default_value' => variable_get('smackdown_enabled_smackdown', 0),
|
| 159 |
);
|
| 160 |
}
|
| 161 |
}
|
| 162 |
|
| 163 |
/**
|
| 164 |
* Implementation of hook_form().
|
| 165 |
*
|
| 166 |
* This adds two nodereference fields and a node-type selector to our content type submission.
|
| 167 |
*/
|
| 168 |
function smackdown_form(&$node) {
|
| 169 |
drupal_add_js(drupal_get_path('module', 'smackdown') .'/smackdown.js');
|
| 170 |
|
| 171 |
// Set the expected smackdown type, or take one from the url
|
| 172 |
// (i.e. node/add/smackdown/video). Get the type and validate it.
|
| 173 |
$node_ref_type = isset($node->node_ref_type) ? $node->node_ref_type : arg(3);
|
| 174 |
$type_info = node_get_types('type', $node_ref_type);
|
| 175 |
$node_ref_type = isset($type_info->type) ? $type_info->type : FALSE;
|
| 176 |
$node_ref_type_name = isset($type_info->type) ? $type_info->type : FALSE;
|
| 177 |
|
| 178 |
// Get the smackdown type name.
|
| 179 |
$smackdown_type = node_get_types('name', $node->type);
|
| 180 |
|
| 181 |
// Set page title.
|
| 182 |
if ($node_ref_type_name) {
|
| 183 |
drupal_set_title(((arg(1) == 'add') ? t('Submit') : t('Edit')) .' '. $node_ref_type_name .' '. $smackdown_type);
|
| 184 |
}
|
| 185 |
|
| 186 |
$node_ref_1_human = ($node->node_ref_1) ? smackdown_encode($node->node_ref_1) : '';
|
| 187 |
$node_ref_2_human = ($node->node_ref_2) ? smackdown_encode($node->node_ref_2) : '';
|
| 188 |
|
| 189 |
$types = node_get_types('types');
|
| 190 |
if (count(variable_get('smackdown_enabled_smackdown', null)) < 1) {
|
| 191 |
drupal_set_message(l('Please choose a content type to enable.', 'admin/content/types/smackdown'), 'error');
|
| 192 |
}
|
| 193 |
else {
|
| 194 |
foreach (variable_get('smackdown_enabled_smackdown', array()) as $key => $value) {
|
| 195 |
$enabled[$value] = $value;
|
| 196 |
}
|
| 197 |
$enabled_types = array_intersect_key($types, $enabled);
|
| 198 |
|
| 199 |
foreach ($enabled_types as $key => $type) {
|
| 200 |
if ($key != 'smackdown') {
|
| 201 |
$options[$key] = $type->type;
|
| 202 |
}
|
| 203 |
}
|
| 204 |
$options[0] = t('<none>');
|
| 205 |
asort($options);
|
| 206 |
|
| 207 |
// Now we define the form elements spcific to our content type
|
| 208 |
$form['node_ref_type'] = array(
|
| 209 |
'#type' => $node_ref_type ? 'hidden' : 'select',
|
| 210 |
'#title' => t('Smackdown Type'),
|
| 211 |
'#options' => $options,
|
| 212 |
'#default_value' => $node_ref_type,
|
| 213 |
'#weight' => -10,
|
| 214 |
'#required' => TRUE,
|
| 215 |
);
|
| 216 |
}
|
| 217 |
|
| 218 |
$form['smackdown_autocomplete'] = array(
|
| 219 |
'#type' => 'hidden',
|
| 220 |
'#value' => url('smackdown/autocomplete', NULL, NULL, TRUE),
|
| 221 |
);
|
| 222 |
|
| 223 |
$form['smackdown_taxonomy_filter'] = array(
|
| 224 |
'#prefix' => '<div id="smackdown-taxonomy-filter">',
|
| 225 |
'#suffix' => '</div>',
|
| 226 |
'#value' => $node_ref_type ? smackdown_taxonomy_filter($node_ref_type) : ' ',
|
| 227 |
);
|
| 228 |
|
| 229 |
$form['node_ref_1'] = array(
|
| 230 |
'#type' => 'textfield',
|
| 231 |
'#title' => t('Contestant #1'),
|
| 232 |
'#autocomplete_path' => 'smackdown/autocomplete',
|
| 233 |
'#required' => true,
|
| 234 |
'#description' => t('Type the name of a @type.', array('@type' => $node_ref_type_name ? drupal_strtolower($node_ref_type_name) : t('contestant'))),
|
| 235 |
'#default_value' => $node_ref_1_human,
|
| 236 |
);
|
| 237 |
|
| 238 |
$form['node_ref_2'] = array(
|
| 239 |
'#type' => 'textfield',
|
| 240 |
'#title' => t('Contestant #2'),
|
| 241 |
'#autocomplete_path' => 'smackdown/autocomplete',
|
| 242 |
'#required' => true,
|
| 243 |
'#description' => t('Type the name of a @type.', array('@type' => $node_ref_type_name ? drupal_strtolower($node_ref_type_name) : t('contestant'))),
|
| 244 |
'#default_value' => $node_ref_2_human,
|
| 245 |
);
|
| 246 |
|
| 247 |
return $form;
|
| 248 |
|
| 249 |
}
|
| 250 |
|
| 251 |
/**
|
| 252 |
* check to see if there is a duplicate smackdown combination already created
|
| 253 |
*
|
| 254 |
* @return Boolean true or false
|
| 255 |
*
|
| 256 |
* @unfinished
|
| 257 |
*/
|
| 258 |
function smackdown_dup_check($node) {
|
| 259 |
// retreive all smackdowns
|
| 260 |
$result = db_query("SELECT * FROM {smackdowns}");
|
| 261 |
while ($result = db_fetch_object($result)) {
|
| 262 |
$refs[] = array(
|
| 263 |
'ref1' => $result->node_ref_1,
|
| 264 |
'ref2' => $result->node_ref_2,
|
| 265 |
);
|
| 266 |
}
|
| 267 |
|
| 268 |
}
|
| 269 |
|
| 270 |
/**
|
| 271 |
* get a list of all created smackdowns
|
| 272 |
*
|
| 273 |
* @return
|
| 274 |
* an array of smackdown node objects
|
| 275 |
*/
|
| 276 |
function smackdown_get_smackdowns($content_type) {
|
| 277 |
if ($content_type) {
|
| 278 |
// find all smackdowns of the same content type
|
| 279 |
$results = db_query("SELECT s.nid FROM smackdown s INNER JOIN node n ON s.nid = n.nid WHERE s.node_ref_type = '%s' AND n.status = 1", $content_type);
|
| 280 |
while ($row = db_fetch_object($results)) {
|
| 281 |
$nids[] = $row->nid;
|
| 282 |
}
|
| 283 |
return $nids;
|
| 284 |
}
|
| 285 |
else {
|
| 286 |
drupal_set_message("no content type given");
|
| 287 |
}
|
| 288 |
}
|
| 289 |
|
| 290 |
/**
|
| 291 |
* Implementation of hook_submit().
|
| 292 |
* Set a default title for smackdowns.
|
| 293 |
*/
|
| 294 |
function smackdown_submit(&$node){
|
| 295 |
$node_ref_1 = smackdown_cleanse_ref($node->node_ref_1);
|
| 296 |
$node_ref_2 = smackdown_cleanse_ref($node->node_ref_2);
|
| 297 |
|
| 298 |
$title1 = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $node_ref_1));
|
| 299 |
$title2 = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $node_ref_2));
|
| 300 |
|
| 301 |
$node->title = $title1 .' vs. '. $title2;
|
| 302 |
}
|
| 303 |
|
| 304 |
/**
|
| 305 |
* Implementation of hook_insert().
|
| 306 |
*/
|
| 307 |
function smackdown_insert(&$node) {
|
| 308 |
$node->node_ref_1 = smackdown_cleanse_ref($node->node_ref_1);
|
| 309 |
$node->node_ref_2 = smackdown_cleanse_ref($node->node_ref_2);
|
| 310 |
db_query("INSERT INTO {smackdown} (vid, nid, node_ref_1, node_ref_2, node_ref_type) VALUES (%d, %d, %d, %d, '%s')", $node->vid, $node->nid, $node->node_ref_1, $node->node_ref_2, $node->node_ref_type);
|
| 311 |
}
|
| 312 |
|
| 313 |
/**
|
| 314 |
* Implementation of hook_update().
|
| 315 |
*/
|
| 316 |
function smackdown_update(&$node) {
|
| 317 |
if ($node->revision) {
|
| 318 |
smackdown_insert($node);
|
| 319 |
}
|
| 320 |
else {
|
| 321 |
$node->node_ref_1 = smackdown_cleanse_ref($node->node_ref_1);
|
| 322 |
$node->node_ref_2 = smackdown_cleanse_ref($node->node_ref_2);
|
| 323 |
db_query("UPDATE {smackdown} SET node_ref_1 = %d, node_ref_2 = %d, node_ref_type = '%s' WHERE vid = %d", $node->node_ref_1, $node->node_ref_2, $node->node_ref_type, $node->vid);
|
| 324 |
}
|
| 325 |
}
|
| 326 |
|
| 327 |
/**
|
| 328 |
* Implementation of hook_delete().
|
| 329 |
*/
|
| 330 |
function smackdown_delete($node) {
|
| 331 |
db_query('DELETE FROM {smackdown} WHERE nid = %d', $node->nid);
|
| 332 |
}
|
| 333 |
|
| 334 |
/**
|
| 335 |
* Implementation of hook_load().
|
| 336 |
*/
|
| 337 |
function smackdown_load($node) {
|
| 338 |
$additions = db_fetch_object(db_query('SELECT s.*, n1.title as node_ref_1_title, n2.title as node_ref_2_title FROM {smackdown} s INNER JOIN {node} n1 ON s.node_ref_1 = n1.nid INNER JOIN {node} n2 ON s.node_ref_2 = n2.nid WHERE s.vid = %d', $node->vid));
|
| 339 |
$additions = smackdown_results($additions);
|
| 340 |
return $additions;
|
| 341 |
}
|
| 342 |
|
| 343 |
function smackdown_view($node, $teaser = FALSE, $page = FALSE) {
|
| 344 |
$node->content['node_refs'] = array(
|
| 345 |
'#value' => theme('smackdown_refs', $node),
|
| 346 |
'#weight' => 1,
|
| 347 |
);
|
| 348 |
|
| 349 |
// we need to find out what the previous node voted on was
|
| 350 |
$user_log = (count($_SESSION['smackdown_user_log']) > 1) ? array_reverse($_SESSION['smackdown_user_log']) : $_SESSION['smackdown_user_log'];
|
| 351 |
if ($user_log) {
|
| 352 |
$prev_node = node_load($user_log[0]);
|
| 353 |
smackdown_results($prev_node);
|
| 354 |
}
|
| 355 |
|
| 356 |
$node->content['smackdown_widget'] = array(
|
| 357 |
'#value' => smackdown_widget_form($node),
|
| 358 |
'#weight' => 5,
|
| 359 |
);
|
| 360 |
|
| 361 |
if ($prev_node) {
|
| 362 |
$node->content['results'] = array(
|
| 363 |
'#value' => theme('smackdown_results', $node, $prev_node),
|
| 364 |
'#weight' => 6,
|
| 365 |
);
|
| 366 |
}
|
| 367 |
|
| 368 |
return $node;
|
| 369 |
}
|
| 370 |
|
| 371 |
function theme_smackdown_refs($node) {
|
| 372 |
$node_ref_1 = node_load($node->node_ref_1);
|
| 373 |
$node_ref_2 = node_load($node->node_ref_2);
|
| 374 |
$output = "<div class='smackdown ref_1'>". node_view($node_ref_1, true, false, false) ."</div>";
|
| 375 |
$output .= "<div class='smackdown ref_2'>". node_view($node_ref_2, true, false, false) ."</div>";
|
| 376 |
return $output;
|
| 377 |
}
|
| 378 |
|
| 379 |
/**
|
| 380 |
* Function to get the results of the previously voted upon node
|
| 381 |
*/
|
| 382 |
function smackdown_results($node) {
|
| 383 |
if ($node) {
|
| 384 |
$refs = array($node->node_ref_1, $node->node_ref_2);
|
| 385 |
$results = votingapi_get_voting_results('node', $node->nid); // for some reason this returns results in opposite order
|
| 386 |
|
| 387 |
if (empty($results)) {
|
| 388 |
// format the results as being empty
|
| 389 |
$node->results[$node->node_ref_1] = array(
|
| 390 |
'#value' => 0,
|
| 391 |
'#title' => $node->node_ref_1_title,
|
| 392 |
);
|
| 393 |
$node->results[$node->node_ref_2] = array(
|
| 394 |
'#value' => 0,
|
| 395 |
'#title' => $node->node_ref_2_title,
|
| 396 |
);
|
| 397 |
}
|
| 398 |
else {
|
| 399 |
if ($results[0]->function == $refs[0]) {
|
| 400 |
// first result corresponds to the first node_ref
|
| 401 |
$node_ref_1 = node_load($results[0]->function);
|
| 402 |
|
| 403 |
// append the results to the node object
|
| 404 |
$node->results[$node_ref_1->nid] = array(
|
| 405 |
'#value' => $results[0]->value,
|
| 406 |
'#title' => $node_ref_1->title,
|
| 407 |
);
|
| 408 |
|
| 409 |
// check to see if there is a result for the second node_ref
|
| 410 |
if ($results[1]) {
|
| 411 |
$node_ref_2 = node_load($results[1]->function);
|
| 412 |
$node_ref_2->value = $results[1]->value;
|
| 413 |
}
|
| 414 |
else {
|
| 415 |
$node_ref_2 = node_load($refs[1]);
|
| 416 |
$node_ref_2->value = 0;
|
| 417 |
}
|
| 418 |
$node->results[$node_ref_2->nid] = array(
|
| 419 |
'#value' => $node_ref_2->value,
|
| 420 |
'#title' => $node_ref_2->title,
|
| 421 |
);
|
| 422 |
}
|
| 423 |
else {
|
| 424 |
// first result corresponds to the second node_ref
|
| 425 |
$node_ref_2 = node_load($results[0]->function);
|
| 426 |
|
| 427 |
// append the results to the node object
|
| 428 |
$node->results[$node_ref_2->nid] = array(
|
| 429 |
'#value' => $results[0]->value,
|
| 430 |
'#title' => $node_ref_2->title,
|
| 431 |
);
|
| 432 |
|
| 433 |
// check to see if there is a result for the second node_ref
|
| 434 |
if ($results[1]) {
|
| 435 |
$node_ref_1 = node_load($results[1]->function);
|
| 436 |
$node_ref_1->value = $results[1]->value;
|
| 437 |
}
|
| 438 |
else {
|
| 439 |
$node_ref_1 = node_load($refs[0]);
|
| 440 |
$node_ref_1->value = 0;
|
| 441 |
}
|
| 442 |
$node->results[$node_ref_1->nid] = array(
|
| 443 |
'#value' => $results[1]->value,
|
| 444 |
'#title' => $node_ref_1->title,
|
| 445 |
);
|
| 446 |
}
|
| 447 |
}
|
| 448 |
}
|
| 449 |
return $node;
|
| 450 |
}
|
| 451 |
|
| 452 |
function theme_smackdown_results($node, $prev_node) {
|
| 453 |
// turn the results into a percentage
|
| 454 |
$votes = $prev_node->results[$prev_node->node_ref_1]['#value'] + $prev_node->results[$prev_node->node_ref_2]['#value'];
|
| 455 |
$ref_1_percentage = round(($prev_node->results[$prev_node->node_ref_1]['#value'] / $votes) * 100) .'%';
|
| 456 |
$ref_2_percentage = round(($prev_node->results[$prev_node->node_ref_2]['#value'] / $votes) * 100) .'%';
|
| 457 |
$output = "<span class='smackdown_results_title'>Previous Smackdown: </span>";
|
| 458 |
$output .= "<span class='smackdown_results_1'>". l($prev_node->results[$prev_node->node_ref_1]['#title'] .' ', 'node/'. $prev_node->node_ref_1) . $ref_1_percentage ."</span> | ";
|
| 459 |
$output .= "<span class='smackdown_results_2'>". l($prev_node->results[$prev_node->node_ref_2]['#title'] .' ', 'node/'. $prev_node->node_ref_2) . $ref_2_percentage ."</span>";
|
| 460 |
$output .= "<hr/>";
|
| 461 |
$output .= l("Make a Smackdown", 'node/add/smackdown') . ' | ' .
|
| 462 |
l('View Standings Chart', 'smackdown/standings/' . $node->node_ref_type);
|
| 463 |
return $output;
|
| 464 |
}
|
| 465 |
|
| 466 |
/**
|
| 467 |
* Implementation of hook_validate()
|
| 468 |
*/
|
| 469 |
function smackdown_validate(&$node) {
|
| 470 |
|
| 471 |
$node_ref_1_human = $node->node_ref_1;
|
| 472 |
$node_ref_1_machine = smackdown_cleanse_ref($node->node_ref_1);
|
| 473 |
|
| 474 |
$node_ref_2_human = $node->node_ref_2;
|
| 475 |
$node_ref_2_machine = smackdown_cleanse_ref($node->node_ref_2);
|
| 476 |
|
| 477 |
if (empty($node->node_ref_1)) {
|
| 478 |
form_set_error('node_ref_1', t("You must choose a node."));
|
| 479 |
}
|
| 480 |
elseif (empty($node->node_ref_2)) {
|
| 481 |
form_set_error('node_ref_2', t("You must choose a node."));
|
| 482 |
}
|
| 483 |
else {
|
| 484 |
|
| 485 |
// make sure actual nids entered exist
|
| 486 |
if (!db_result(db_query("SELECT nid FROM {node} WHERE nid = %d", $node_ref_1_machine))) {
|
| 487 |
form_set_error('node_ref_1', t("That id does not exist, make sure you choose from the list."));
|
| 488 |
}
|
| 489 |
if (!db_result(db_query("SELECT nid FROM {node} WHERE nid = %d", $node_ref_2_machine))) {
|
| 490 |
form_set_error('node_ref_2', t("That id does not exist, make sure you choose from the list."));
|
| 491 |
}
|
| 492 |
|
| 493 |
// make sure the node refs are not the same
|
| 494 |
if ($node_ref_1_machine == $node_ref_2_machine) {
|
| 495 |
form_set_error('node_ref_2', t("This node cannot be the same as the first"));
|
| 496 |
}
|
| 497 |
|
| 498 |
// make sure this particular pairing of videos does not already exist
|
| 499 |
if (arg(2) != 'edit') {
|
| 500 |
$dup = db_query("SELECT * FROM {smackdown} WHERE (node_ref_1 = %d AND node_ref_2 = %d) OR (node_ref_1 = %d AND node_ref_2 = %d)", $node_ref_1_machine, $node_ref_2_machine, $node_ref_2_machine, $node_ref_1_machine);
|
| 501 |
if (db_result($dup)) {
|
| 502 |
$duplicate = db_fetch_object($dup);
|
| 503 |
$refs = array('node_ref_1', 'node_ref_2');
|
| 504 |
form_set_error('node_ref_1', t("This particular smackdown already exists. "));
|
| 505 |
form_set_error('node_ref_2', t("Please change one of your choices or ". l("view the one", 'node/'. $duplicate->nid) ." that was already created."));
|
| 506 |
}
|
| 507 |
}
|
| 508 |
}
|
| 509 |
|
| 510 |
}
|
| 511 |
|
| 512 |
/**
|
| 513 |
* Function to autocomplete the node reference fields in our smackdown creation form.
|
| 514 |
*/
|
| 515 |
function smackdown_autocomplete() {
|
| 516 |
$args = func_get_args();
|
| 517 |
$matches = array();
|
| 518 |
|
| 519 |
// check size & set vars depending on size
|
| 520 |
switch(count($args)) {
|
| 521 |
case 1:
|
| 522 |
$string = $args[0];
|
| 523 |
$sql = "SELECT DISTINCT n.nid, n.title FROM {node} n WHERE n.title LIKE '%s%' AND n.status = 1";
|
| 524 |
$result = db_query($sql, $string);
|
| 525 |
while ($row = db_fetch_object($result)) {
|
| 526 |
$matches[$row->title .':['. $row->nid .']'] = $row->title;
|
| 527 |
}
|
| 528 |
break;
|
| 529 |
case 2:
|
| 530 |
$type = $args[0];
|
| 531 |
$string = $args[1];
|
| 532 |
$sql = "SELECT DISTINCT n.nid, n.title FROM {node} n WHERE n.title LIKE '%s%' AND n.type = '%s' AND n.status = 1";
|
| 533 |
$result = db_query($sql, $string, $type);
|
| 534 |
while ($row = db_fetch_object($result)) {
|
| 535 |
$matches[$row->title .':['. $row->nid .']'] = $row->title;
|
| 536 |
}
|
| 537 |
break;
|
| 538 |
case 3:
|
| 539 |
$type = $args[0];
|
| 540 |
$tid = $args[1];
|
| 541 |
$string = $args[2];
|
| 542 |
$prev_matches = FALSE;
|
| 543 |
$sql = "SELECT DISTINCT n.nid, n.title FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid = %d AND n.title LIKE '%s%' AND n.type = '%s' AND n.status = 1 ORDER BY n.title ASC";
|
| 544 |
if (strstr($tid, ',')) {
|
| 545 |
// multiple tids passed, split 'em up
|
| 546 |
$tids = explode(',', $tid);
|
| 547 |
foreach ($tids as $tid) {
|
| 548 |
$result = db_query($sql, $tid, $string, $type);
|
| 549 |
while ($row = db_fetch_object($result)) {
|
| 550 |
$new_matches[$row->title .':['. $row->nid .']'] = $row->title;
|
| 551 |
}
|
| 552 |
|
| 553 |
if (!$prev_matches) {
|
| 554 |
$prev_matches = $new_matches;
|
| 555 |
unset($new_matches);
|
| 556 |
}
|
| 557 |
else {
|
| 558 |
// compare the previous matches to the current
|
| 559 |
$matches = array_intersect_key($new_matches, $prev_matches);
|
| 560 |
$prev_matches = $matches;
|
| 561 |
}
|
| 562 |
}
|
| 563 |
}
|
| 564 |
else {
|
| 565 |
$result = db_query($sql, $tid, $string, $type);
|
| 566 |
while ($row = db_fetch_object($result)) {
|
| 567 |
$matches[$row->title .':['. $row->nid .']'] = $row->title;
|
| 568 |
}
|
| 569 |
}
|
| 570 |
break;
|
| 571 |
}
|
| 572 |
|
| 573 |
drupal_set_header('text/javascript');
|
| 574 |
print drupal_to_js($matches);
|
| 575 |
exit();
|
| 576 |
}
|
| 577 |
|
| 578 |
function smackdown_cleanse_ref($ref) {
|
| 579 |
if (!is_numeric($ref)) {
|
| 580 |
$ref = explode(":[", $ref);
|
| 581 |
$ref = trim($ref[1], ']');
|
| 582 |
}
|
| 583 |
return $ref;
|
| 584 |
}
|
| 585 |
|
| 586 |
function smackdown_encode($value) {
|
| 587 |
$node = node_load($value);
|
| 588 |
return $node->title .':['. $value .']';
|
| 589 |
}
|
| 590 |
|
| 591 |
|
| 592 |
function smackdown_widget_form($node) {
|
| 593 |
return drupal_get_form('smackdown_form_widget', $node);
|
| 594 |
}
|
| 595 |
|
| 596 |
function smackdown_form_widget($node) {
|
| 597 |
$form['vote_ref_1'] = array(
|
| 598 |
'#type' => 'submit',
|
| 599 |
'#value' => $node->node_ref_1,
|
| 600 |
'#theme' => 'smackdown_vote_ref_1',
|
| 601 |
);
|
| 602 |
|
| 603 |
$form['vote_ref_2'] = array(
|
| 604 |
'#type' => 'submit',
|
| 605 |
'#value' => $node->node_ref_2,
|
| 606 |
'#theme' => 'smackdown_vote_ref_2',
|
| 607 |
);
|
| 608 |
|
| 609 |
$form['vote_refs'] = array(
|
| 610 |
'#type' => 'value',
|
| 611 |
'#value' => array('smackdown_nid' => $node->nid, 'ref_1' => $node->node_ref_1, 'ref_2' => $node->node_ref_2, 'ref_type' => $node->node_ref_type),
|
| 612 |
);
|
| 613 |
|
| 614 |
$form['#base'] = 'smackdown_form';
|
| 615 |
|
| 616 |
return $form;
|
| 617 |
}
|
| 618 |
|
| 619 |
function theme_smackdown_vote_ref_1($form) {
|
| 620 |
return '<input type="submit" class="form-submit" value="'. $form['#value'] .'" id="edit-vote-ref-1" name="op"/>';
|
| 621 |
}
|
| 622 |
|
| 623 |
function theme_smackdown_vote_ref_2($form) {
|
| 624 |
return '<input type="submit" class="form-submit" value="'. $form['#value'] .'" id="edit-vote-ref-1" name="op"/>';
|
| 625 |
}
|
| 626 |
|
| 627 |
function smackdown_form_widget_submit($form_id, $form_values) {
|
| 628 |
$nid = $form_values['vote_refs']['smackdown_nid'];
|
| 629 |
$content_type = $form_values['vote_refs']['ref_type'];
|
| 630 |
$value = $form_values['op'];
|
| 631 |
|
| 632 |
if (is_numeric($nid) && is_numeric($value)) {
|
| 633 |
|
| 634 |
// Save voting results.
|
| 635 |
$results = votingapi_add_vote('node', $nid, $value, 'option', 'smackdown');
|
| 636 |
|
| 637 |
// cache
|
| 638 |
if (variable_get('votingapi_calculation_schedule', 'immediate') != 'cron') {
|
| 639 |
votingapi_recalculate_results('node', $nid);
|
| 640 |
}
|
| 641 |
|
| 642 |
// Set user session var to include this $nid
|
| 643 |
$_SESSION['smackdown_user_log'][] = $nid;
|
| 644 |
|
| 645 |
// go to the next smackdown
|
| 646 |
drupal_goto(smackdown_next($content_type));
|
| 647 |
}
|
| 648 |
return;
|
| 649 |
}
|
| 650 |
|
| 651 |
/**
|
| 652 |
* Menu callback to generate a smackdown viewing by taxonomy
|
| 653 |
*
|
| 654 |
* @taxonomy_id
|
| 655 |
* id of the taxonomy to remain within
|
| 656 |
*/
|
| 657 |
function smackdown_page($content_type) {
|
| 658 |
global $conf;
|
| 659 |
|
| 660 |
if (!$conf['smackdown_enabled_smackdown']) {
|
| 661 |
drupal_set_message("You need to ". l("setup which content type to reference", 'admin/content/types/smackdown') .".");
|
| 662 |
return '';
|
| 663 |
}
|
| 664 |
elseif (!$content_type) {
|
| 665 |
if (arg(1)) {
|
| 666 |
$content_type = arg(1);
|
| 667 |
}
|
| 668 |
else {
|
| 669 |
$content_type = $conf['smackdown_enabled_smackdown'];
|
| 670 |
$content_type = $content_type[0];
|
| 671 |
}
|
| 672 |
}
|
| 673 |
// goto in a smackdown node
|
| 674 |
drupal_goto(smackdown_next($content_type));
|
| 675 |
|
| 676 |
}
|
| 677 |
|
| 678 |
/**
|
| 679 |
* Function to determine what the next smackdown should be
|
| 680 |
*
|
| 681 |
* @content_type
|
| 682 |
* type to remain within
|
| 683 |
* @return
|
| 684 |
* url of the next smackdown node to show
|
| 685 |
*/
|
| 686 |
function smackdown_next($content_type) {
|
| 687 |
$smackdown_diff = array();
|
| 688 |
|
| 689 |
// get a list of all smackdowns the user has already voted upon
|
| 690 |
$smackdown_user_log = $_SESSION['smackdown_user_log'];
|
| 691 |
|
| 692 |
// get a list of all created smackdown nids
|
| 693 |
$nids = smackdown_get_smackdowns($content_type);
|
| 694 |
|
| 695 |
// user has voted before?
|
| 696 |
if (empty($smackdown_user_log) && $nids) {
|
| 697 |
// show a random smackdown within the same type
|
| 698 |
$nid = array_rand($nids);
|
| 699 |
$smackdown_diff = array($nids[$nid]);
|
| 700 |
}
|
| 701 |
else {
|
| 702 |
// compare the list of smackdowns to what the user has already voted upon
|
| 703 |
if ($nids) {
|
| 704 |
$smackdown_diff = array_diff($nids, $smackdown_user_log);
|
| 705 |
}
|
| 706 |
|
| 707 |
// are there any smackdowns left?
|
| 708 |
if (!$nids || !$smackdown_diff) {
|
| 709 |
// ask user to create one if allowed
|
| 710 |
if (user_access("create smackdown")) {
|
| 711 |
drupal_set_message("All out of smackdowns, would you like to create one yourself?");
|
| 712 |
}
|
| 713 |
else {
|
| 714 |
drupal_set_message("Login to create additional smackdowns.");
|
| 715 |
}
|
| 716 |
}
|
| 717 |
}
|
| 718 |
|
| 719 |
// TODO: theme error message output
|
| 720 |
if (empty($smackdown_diff)) {
|
| 721 |
return 'node/add/smackdown/'. $content_type;
|
| 722 |
}
|
| 723 |
else {
|
| 724 |
$smackdown_diff = array_values($smackdown_diff);
|
| 725 |
return 'node/'. $smackdown_diff[0];
|
| 726 |
}
|
| 727 |
}
|
| 728 |
|
| 729 |
/**
|
| 730 |
* menu callback for displaying an overview of all smackdwons within a taxonomy
|
| 731 |
* @taxonomy_id
|
| 732 |
* id of the taxonomy to remain within
|
| 733 |
*/
|
| 734 |
function smackdown_standings_page($content_type = NULL) {
|
| 735 |
if (!$content_type) {
|
| 736 |
// set to the first tab
|
| 737 |
foreach (variable_get('smackdown_enabled_smackdown', array()) as $type) {
|
| 738 |
drupal_goto('smackdown/standings/'. $type);
|
| 739 |
}
|
| 740 |
}
|
| 741 |
|
| 742 |
// get a paged list of all smackdowns
|
| 743 |
$nids = smackdown_get_smackdowns($content_type);
|
| 744 |
if ($nids) {
|
| 745 |
foreach ($nids as $nid) {
|
| 746 |
$results[$nid] = smackdown_results(node_load($nid));
|
| 747 |
}
|
| 748 |
}
|
| 749 |
|
| 750 |
// display
|
| 751 |
return theme('smackdown_standings', $results);
|
| 752 |
}
|
| 753 |
|
| 754 |
function theme_smackdown_standings($nodes) {
|
| 755 |
if (!empty($nodes)) {
|
| 756 |
foreach ($nodes as $node) {
|
| 757 |
$votes = $node->results[$node->node_ref_1]['#value'] + $node->results[$node->node_ref_2]['#value'];
|
| 758 |
$ref_1_percentage = ($node->results[$node->node_ref_1]['#value'] != 0) ? round(($node->results[$node->node_ref_1]['#value'] / $votes) * 100) : 0;
|
| 759 |
$ref_2_percentage = ($node->results[$node->node_ref_2]['#value'] != 0) ? round(($node->results[$node->node_ref_2]['#value'] / $votes) * 100) : 0;
|
| 760 |
|
| 761 |
$item = '<div class="title">'. l($node->title, 'node/'. $node->nid) .' ('. $votes .' vote/s)</div>';
|
| 762 |
$item .= '<div class="percent">'. $node->node_ref_1_title .' '. $ref_1_percentage .'%</div>';
|
| 763 |
$item .= '<div class="bar"><div style="width: '. $ref_1_percentage .'%;" class="foreground"></div></div>';
|
| 764 |
$item .= '<div class="percent">'. $node->node_ref_2_title .' '. $ref_2_percentage .'%</div>';
|
| 765 |
$item .= '<div class="bar"><div style="width: '. $ref_2_percentage .'%;" class="foreground"></div></div>';
|
| 766 |
|
| 767 |
$items[] = $item;
|
| 768 |
}
|
| 769 |
|
| 770 |
$output = '<div id="smackdown_standings">'. theme('item_list', $items, 'Smackdown Standings', 'ol') .'</div>';
|
| 771 |
}
|
| 772 |
|
| 773 |
return $output;
|
| 774 |
}
|
| 775 |
|
| 776 |
function smackdown_taxonomy_filter($node_type) {
|
| 777 |
$node_type_name = node_get_types('name', $node_type);
|
| 778 |
|
| 779 |
if (empty($node_type_name)) {
|
| 780 |
return;
|
| 781 |
}
|
| 782 |
|
| 783 |
$form = array();
|
| 784 |
$form['#node']->type = $node_type;
|
| 785 |
$form['type']['#value'] = $node_type;
|
| 786 |
drupal_prepare_form($node_type .'_node_form', $form);
|
| 787 |
|
| 788 |
// Make some small adjustments to the form, and allow for easy overriding
|
| 789 |
// with a custom theme function.
|
| 790 |
$form['taxonomy']['#type'] = 'markup';
|
| 791 |
$form['taxonomy']['#theme'] = 'smackdown_taxonomy_filter';
|
| 792 |
|
| 793 |
// Add a type value so the theme function has context for the node type.
|
| 794 |
$form['taxonomy']['type'] = array(
|
| 795 |
'#type' => 'value',
|
| 796 |
'#value' => $node_type,
|
| 797 |
);
|
| 798 |
|
| 799 |
return drupal_render($form['taxonomy']);
|
| 800 |
}
|
| 801 |
|
| 802 |
function theme_smackdown_taxonomy_filter($form) {
|
| 803 |
$node_type_name = node_get_types('name', $form['type']['#value']);
|
| 804 |
$form['#value'] = t('Filter contestants to include @types in only the following categories.', array('@types' => drupal_strtolower($node_type_name) .'s'));
|
| 805 |
return drupal_render($form);
|
| 806 |
}
|
| 807 |
|
| 808 |
function smackdown_taxonomy_js($node_type) {
|
| 809 |
$output = smackdown_taxonomy_filter($node_type);
|
| 810 |
drupal_set_header('text/javascript');
|
| 811 |
print drupal_to_js($output);
|
| 812 |
exit();
|
| 813 |
}
|
| 814 |
|
| 815 |
/**
|
| 816 |
* array_intersect_key is not supported < php5
|
| 817 |
*/
|
| 818 |
if (!function_exists('array_intersect_key')) {
|
| 819 |
function array_intersect_key($isec, $keys) {
|
| 820 |
$argc = func_num_args();
|
| 821 |
if ($argc > 2) {
|
| 822 |
for ($i = 1; !empty($isec) && $i < $argc; $i++) {
|
| 823 |
$arr = func_get_arg($i);
|
| 824 |
foreach (array_keys($isec) as $key) {
|
| 825 |
if (!isset($arr[$key])) {
|
| 826 |
unset($isec[$key]);
|
| 827 |
}
|
| 828 |
}
|
| 829 |
}
|
| 830 |
return $isec;
|
| 831 |
}
|
| 832 |
else {
|
| 833 |
$res = array();
|
| 834 |
foreach (array_keys($isec) as $key) {
|
| 835 |
if (isset($keys[$key])) {
|
| 836 |
$res[$key] = $isec[$key];
|
| 837 |
}
|
| 838 |
}
|
| 839 |
return $res;
|
| 840 |
}
|
| 841 |
}
|
| 842 |
}
|