/[drupal]/contributions/modules/smackdown/smackdown.module
ViewVC logotype

Diff of /contributions/modules/smackdown/smackdown.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.2.5.2.17, Mon Sep 7 07:30:58 2009 UTC revision 1.1.2.5.2.18, Fri Oct 23 16:39:34 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: smackdown.module,v 1.1.2.5.2.16 2009/07/28 23:41:36 sirkitree Exp $  // $Id: smackdown.module,v 1.1.2.5.2.17 2009/09/07 07:30:58 sirkitree Exp $
3    
4  /**  /**
5   * @file smackdown.module   * @file smackdown.module
# Line 8  Line 8 
8   * Choose what content type to reference in the smackdown content type's   * Choose what content type to reference in the smackdown content type's
9   * nodereference settings. (admin/content/node-type/smackdown/fields)   * nodereference settings. (admin/content/node-type/smackdown/fields)
10   */   */
11    
12    /**
13     * Implementation of hook_init().
14     */
15    function smackdown_init() {
16      if ($_GET['smackdown_vote'] && module_exists('context')) {
17        context_set_by_condition('smackdown', TRUE);
18      }
19    }
20    
21  /**  /**
22   * Implementation of hook_perm().   * Implementation of hook_perm().
23   */   */
24  function smackdown_perm() {  function smackdown_perm() {
25    return array('view smackdowns', 'vote on smackdowns');    return array('vote on smackdowns');
26  }  }
27    
28    
# Line 21  function smackdown_perm() { Line 30  function smackdown_perm() {
30   * Implementation of hook_menu().   * Implementation of hook_menu().
31   */   */
32  function smackdown_menu() {  function smackdown_menu() {
33    $items['smackdown/vote'] = array(    $items['smackdown/vote/%smackdown_token'] = array(
34      'page callback'     => 'smackdown_vote',      'page callback'     => 'smackdown_vote',
35      'access arguments'  => array('vote on smackdowns'),      'access arguments'  => array('vote on smackdowns'),
36      'type'              => MENU_CALLBACK,      'type'              => MENU_CALLBACK,
37    );    );
38    $items['node/%node/voting-results'] = array(    $items['node/%smackdown/voting-results'] = array(
39      'title'             => 'Results',      'title'             => 'Results',
40      'page callback'     => 'smackdown_results',      'page callback'     => 'smackdown_results',
41      'page arguments'    => array('page', 1),      'page arguments'    => array('page', 1),
42      'access callback'   => 'smackdown_results_access',      'access callback'   => 'node_access',
43      'access arguments'  => array(1),      'access arguments'  => array('view', 1),
44      'type'              => MENU_LOCAL_TASK,      'type'              => MENU_LOCAL_TASK,
45    );    );
46    return $items;    return $items;
47  }  }
48    
49  function smackdown_results_access($node) {  /**
50    global $user;   * Menu load function to do a token verification
51    if (in_array($node->type, variable_get('smackdown_types', array())) && user_access('view smackdowns', $user)) {   */
52      return TRUE;  function smackdown_token_load($token) {
53      return drupal_valid_token($token, $_GET['sid']);
54    }
55    
56    /**
57     * Menu load function to verify the node is a smackdown
58     */
59    function smackdown_load($nid) {
60      $node = node_load($nid);
61      if (in_array($node->type, variable_get('smackdown_types', array()))) {
62        return $node;
63    }    }
64    return FALSE;    return FALSE;
65  }  }
# Line 69  function smackdown_vote($cid = NULL, $si Line 88  function smackdown_vote($cid = NULL, $si
88        'value'         => $cid,        'value'         => $cid,
89        'value_type'    => 'option',        'value_type'    => 'option',
90        'tag'           => 'smackdown',        'tag'           => 'smackdown',
91          'location'      => "node/$sid/voting-results",
92      );      );
93        drupal_alter('smackdown_vote', $vote);
94        $location = url($vote['location'], array('absolute' => TRUE, 'query' => "smackdown_vote=$sid"));
95      // Save voting results.      // Save voting results.
96      $results = votingapi_set_votes($vote);      $results = votingapi_set_votes($vote);
   
     // Set user session var to include this $nid  
     $_SESSION['smackdown_vote'] = $sid;  
97    
98      drupal_set_header('text/javascript');      drupal_json(array('results' => $results, 'location' => $location));
     print drupal_to_js(array('results' => $results));  
99      exit();      exit();
100    }    }
101  }  }
# Line 85  function smackdown_vote($cid = NULL, $si Line 103  function smackdown_vote($cid = NULL, $si
103  /**  /**
104   * Implementation of hook_nodeapi().   * Implementation of hook_nodeapi().
105   */   */
106  function smackdown_nodeapi(&$node, $op) {  function smackdown_nodeapi(&$node, $op, $a3, $page = FALSE) {
107    // make sure to only compare to the smackdown you are looking at in the URL    // make sure to only compare to the smackdown you are looking at in the URL
108    if ($op == 'view' && in_array($node->type, variable_get('smackdown_types', array())) && $node->nid == arg(1)) {    if ($op == 'view' && in_array($node->type, variable_get('smackdown_types', array())) && $page) {
109      static $added = FALSE;      static $added = FALSE;
110      if (!$added) {      if (!$added) {
111        if (user_access('vote on smackdowns')) {        if (user_access('vote on smackdowns')) {
# Line 110  function smackdown_nodeapi(&$node, $op) Line 128  function smackdown_nodeapi(&$node, $op)
128              'nid' => $node_array[$field_names[1]][0]['nid'],              'nid' => $node_array[$field_names[1]][0]['nid'],
129            ),            ),
130            'location' => 'node/'. $node->nid .'/voting-results',            'location' => 'node/'. $node->nid .'/voting-results',
131              'token' => drupal_get_token($node->nid),
132          ),          ),
133        );        );
134        drupal_add_js($settings, 'setting');        drupal_add_js($settings, 'setting');
135    
136        // a log of previous votes are stored in $_COOKIE        // a log of previous votes are stored in $_GET
137        if (isset($_SESSION['smackdown_vote'])) {        if (isset($_GET['smackdown_vote'])) {
138          $prev_results = smackdown_results('short', node_load($_SESSION['smackdown_vote']));          $prev_results = smackdown_results('short', node_load($_GET['smackdown_vote']));
         unset($_SESSION['smackdown_vote']);  
139          // append the results of the previous smackdown to $node->content          // append the results of the previous smackdown to $node->content
140          $node->content['prev_results'] = array('#value' => $prev_results, '#weight' => $node->content['body']['#weight']--);          $node->content['prev_results'] = array('#value' => $prev_results, '#weight' => $node->content['body']['#weight']--);
141        }        }

Legend:
Removed from v.1.1.2.5.2.17  
changed lines
  Added in v.1.1.2.5.2.18

  ViewVC Help
Powered by ViewVC 1.1.2