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

Diff of /contributions/modules/voting/voting.module

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

revision 1.5, Fri Sep 9 19:01:38 2005 UTC revision 1.6, Tue Jul 11 00:23:32 2006 UTC
# Line 38  function voting_menu($type) { Line 38  function voting_menu($type) {
38   * Implementation of hook_settings()   * Implementation of hook_settings()
39   */   */
40  function voting_settings() {  function voting_settings() {
41    // form group: Colors          if (!function_exists('votingapi_set_vote')) {
42    $group  = form_textfield(t('Background color for voting control'), 'voting_bgcolor',                  drupal_set_message('The voting module depends on the voting API module.  Please install the voting API module before attempting to use this module.', 'error');
43      variable_get('voting_bgcolor', '0xffffff'), 8, 8);          }
   $group .= form_textfield(t("Star 'on' fill color"), 'voting_on_fill',  
     variable_get('voting_on_fill', '0x990000'), 8, 8);  
   $group .= form_textfield(t("Star 'on' border color"), 'voting_on_border',  
     variable_get('voting_on_border', '0x330000'), 8, 8);  
   $group .= form_textfield(t("Star 'off' fill color"), 'voting_off_fill',  
     variable_get('voting_off_fill', '0xcccccc'), 8, 8);  
   $group .= form_textfield(t("Star 'off' border color"), 'voting_off_border',  
     variable_get('voting_off_border', '0x333333'), 8, 8);  
   $group .= form_textfield(t("Text color"), 'voting_txt_color',  
     variable_get('voting_txt_color', '0x000000'), 8, 8);  
   $output .= form_group(t('Colors (0xffffff for white, 0x000000 for black, etc.)'), $group);  
   
         // form group: Text Strings  
   $group  = form_textfield(t('Star 1'), 'voting_txt_vote1',  
     variable_get('voting_txt_vote1', 'Awful'), 20, 20);  
   $group .= form_textfield(t('Star 2'), 'voting_txt_vote2',  
     variable_get('voting_txt_vote2', 'Poor'), 20, 20);  
   $group .= form_textfield(t('Star 3'), 'voting_txt_vote3',  
     variable_get('voting_txt_vote3', 'Average'), 20, 20);  
   $group .= form_textfield(t('Star 4'), 'voting_txt_vote4',  
     variable_get('voting_txt_vote4', 'Good'), 20, 20);  
   $group .= form_textfield(t('Star 5'), 'voting_txt_vote5',  
     variable_get('voting_txt_vote5', 'Excellent'), 20, 20);  
   $group .= form_textfield(t('Message shown BEFORE a user votes'), 'voting_txt_before_vote',  
     variable_get('voting_txt_before_vote', 'Rate This:'), 20, 20);  
   $group .= form_textfield(t('Message shown AFTER a user votes'), 'voting_txt_after_vote',  
     variable_get('voting_txt_after_vote', 'My Vote:'), 20, 20);  
   $group .= form_textfield(t('No permission to vote message'), 'voting_txt_login',  
     variable_get('voting_txt_login', 'Please login or register to vote.'), 40, 40);  
   $output .= form_group(t('Text Messages (Leave blank for defaults)'), $group);  
   
         // form group: Other  
         $group = form_radios(t('Voting controls on nodes'), 'voting_location',  
           variable_get('voting_location', 1), array(t('Display above the node'), t('Display below the node'), t('Do not display')),  
                 t('Position of the voting control.  Select "Do not display" if you are calling the voting module function directly from your theme and/or module code.'));  
         $group .= form_textfield(t("Voting IP timeout (in seconds)"), 'voting_ip_timeout',  
     variable_get('voting_ip_timeout', 3600), 8, 8,  
                 t('Controls if and how often users with the same IP address can vote for the same thing.  Set it to 0 to disable this feature.'));  
   
         $output .= form_group(t('Other'), $group);  
44    
45    return $output;          // General options
46            $form['general'] = array(
47                    '#type' => 'fieldset',
48                    '#title' => t('General options'),
49                    '#collapsible' => true,
50                    '#collapsed' => false,
51            );
52            $form['general']['voting_location'] = array(
53                    '#type' => 'radios',
54                    '#title' => t('Voting controls on nodes'),
55                    '#default_value' => variable_get('voting_location', 1),
56                    '#options' => array(t('Display above the node'), t('Display below the node'), t('Do not display')),
57                    '#description' => t('Position of the voting control.  Select "Do not display" if you are calling the voting module function directly from your theme and/or module code.'),
58            );
59            $form['general']['voting_location'] = array(
60                    '#type' => 'radios',
61                    '#title' => t('Show voting in teaser'),
62                    '#default_value' => variable_get('voting_show_in_teaser', 0),
63                    '#options' => array(1 => t('yes'), 0 => t('no')),
64            );
65            $form['general']['voting_ip_timeout'] = array(
66                    '#type' => 'textfield',
67                    '#title' => t("Voting IP timeout (in seconds)"),
68                    '#default_value' => variable_get('voting_ip_timeout', 3600),
69                    '#size' => 8,
70                    '#maxlength' => 8,
71                    '#description' => t('Controls if and how often users with the same IP address can vote for the same thing.  Set it to 0 to disable this feature.'),
72            );
73    
74            // Colors
75            $form['colors'] = array(
76                    '#type' => 'fieldset',
77                    '#title' => t('Colors'),
78                    '#collapsible' => true,
79                    '#collapsed' => true,
80            );
81            $form['colors']['voting_bgcolor'] = array(
82                    '#type' => 'textfield',
83                    '#title' => t('Background color for voting control'),
84                    '#default_value' => variable_get('voting_bgcolor', '0xffffff'),
85                    '#size' => 8,
86                    '#maxlength' => 8,
87            );
88            $form['colors']['voting_on_fill'] = array(
89                    '#type' => 'textfield',
90                    '#title' => t("Star 'on' fill color"),
91                    '#default_value' => variable_get('voting_on_fill', '0x990000'),
92                    '#size' => 8,
93                    '#maxlength' => 8,
94            );
95            $form['colors']['voting_on_border'] = array(
96                    '#type' => 'textfield',
97                    '#title' => t("Star 'on' border color"),
98                    '#default_value' => variable_get('voting_on_border', '0x330000'),
99                    '#size' => 8,
100                    '#maxlength' => 8,
101            );
102            $form['colors']['voting_off_fill'] = array(
103                    '#type' => 'textfield',
104                    '#title' => t("Star 'off' fill color"),
105                    '#default_value' => variable_get('voting_off_fill', '0xcccccc'),
106                    '#size' => 8,
107                    '#maxlength' => 8,
108            );
109            $form['colors']['voting_off_border'] = array(
110                    '#type' => 'textfield',
111                    '#title' => t("Star 'off' border color"),
112                    '#default_value' => variable_get('voting_off_border', '0x333333'),
113                    '#size' => 8,
114                    '#maxlength' => 8,
115            );
116            $form['colors']['voting_txt_color'] = array(
117                    '#type' => 'textfield',
118                    '#title' => t("Text color"),
119                    '#default_value' => variable_get('voting_txt_color', '0x000000'),
120                    '#size' => 8,
121                    '#maxlength' => 8,
122            );
123    
124            // Text strings
125            $form['text'] = array(
126                    '#type' => 'fieldset',
127                    '#title' => t('Text Strings'),
128                    '#collapsible' => true,
129                    '#collapsed' => true,
130            );
131            $form['text']['voting_txt_vote1'] = array(
132                    '#type' => 'textfield',
133                    '#title' => t('Star 1'),
134                    '#default_value' => variable_get('voting_txt_vote1', 'Awful'),
135                    '#size' => 20,
136                    '#maxlength' => 20,
137            );
138            $form['text']['voting_txt_vote2'] = array(
139                    '#type' => 'textfield',
140                    '#title' => t('Star 2'),
141                    '#default_value' => variable_get('voting_txt_vote2', 'Poor'),
142                    '#size' => 20,
143                    '#maxlength' => 20,
144            );
145            $form['text']['voting_txt_vote3'] = array(
146                    '#type' => 'textfield',
147                    '#title' => t('Star 3'),
148                    '#default_value' => variable_get('voting_txt_vote3', 'Average'),
149                    '#size' => 20,
150                    '#maxlength' => 20,
151            );
152            $form['text']['voting_txt_vote4'] = array(
153                    '#type' => 'textfield',
154                    '#title' => t('Star 4'),
155                    '#default_value' => variable_get('voting_txt_vote4', 'Good'),
156                    '#size' => 20,
157                    '#maxlength' => 20,
158            );
159            $form['text']['voting_txt_vote5'] = array(
160                    '#type' => 'textfield',
161                    '#title' => t('Star 5'),
162                    '#default_value' => variable_get('voting_txt_vote5', 'Excellent'),
163                    '#size' => 20,
164                    '#maxlength' => 20,
165            );
166            $form['text']['voting_txt_before_vote'] = array(
167                    '#type' => 'textfield',
168                    '#title' => t('Message shown BEFORE a user votes'),
169                    '#default_value' => variable_get('voting_txt_before_vote', 'Rate This:'),
170                    '#size' => 20,
171                    '#maxlength' => 20,
172            );
173            $form['text']['voting_txt_after_vote'] = array(
174                    '#type' => 'textfield',
175                    '#title' => t('Message shown AFTER a user votes'),
176                    '#default_value' => variable_get('voting_txt_after_vote', 'My Vote:'),
177                    '#size' => 20,
178                    '#maxlength' => 20,
179            );
180            $form['text']['voting_txt_login'] = array(
181                    '#type' => 'textfield',
182                    '#title' => t('No permission to vote message'),
183                    '#default_value' => variable_get('voting_txt_login', 'Please login or register to vote.'),
184                    '#size' => 40,
185                    '#maxlength' => 40,
186            );
187      return $form;
188  }  }
189    
190  /**  /**
# Line 117  function voting_get_vote($content_type, Line 219  function voting_get_vote($content_type,
219    return $vote;    return $vote;
220  }  }
221    
222    function voting_convert_to_percent($vote) {
223            // round to nearest integer
224            $vote = round($vote);
225            // set limits
226            $vote = ($vote < 1) ? 1 : $vote;
227            $vote = ($vote > 5) ? 5 : $vote;
228            // convert to percentage
229            switch($vote) {
230                    case 5: return 90;
231                    case 4: return 70;
232                    case 3: return 50;
233                    case 2: return 30;
234                    case 1: return 10;
235                    default: return 0;
236            }
237    }
238    
239    function voting_convert_from_percent($vote) {
240            // round to nearest ten
241            $vote = 10 * round($vote / 10);
242            // set limits
243            $vote = ($vote < 0) ? 0 : $vote;
244            $vote = ($vote > 100) ? 100 : $vote;
245            // convert to scale from 1.0 to 5.0
246            switch($vote) {
247                    case 100:
248                    case 90: return 5.0;
249                    case 80: return 4.5;
250                    case 70: return 4.0;
251                    case 60: return 3.5;
252                    case 50: return 3.0;
253                    case 40: return 2.5;
254                    case 30: return 2.0;
255                    case 20: return 1.5;
256                    case 10: return 1.0;
257                    default: return 0;
258            }
259    }
260    
261  /**  /**
262   * This function is used with a Macromedia Flash based voting control to send and receive data in   * This function is used with a Macromedia Flash based voting control to send and receive data in
263   * one step.  First, Flash is passed the content_type and content_id from the page where it is used.   * one step.  First, Flash is passed the content_type and content_id from the page where it is used.
# Line 124  function voting_get_vote($content_type, Line 265  function voting_get_vote($content_type,
265   * not passed, so this function will not insert a vote, but it will return the current voting information.   * not passed, so this function will not insert a vote, but it will return the current voting information.
266   * If a user votes (or changes their vote), this function is called again from Flash, this time with   * If a user votes (or changes their vote), this function is called again from Flash, this time with
267   * a vote.   * a vote.
  *  
268   */   */
269  function voting_flash() {  function voting_flash() {
270    global $user;    global $user;
271    
272    $content_type = $_POST['content_type'];    $content_type = $_POST['content_type'];
273    $content_id = $_POST['content_id'];    $content_id = $_POST['content_id'];
274    $new_vote = $_POST['vote'];    $user_vote = $_POST['vote'];
   $vote = voting_get_vote($content_type, $content_id); // get average vote, number of votes, and user's current vote (if any)  
275    
276    if ($new_vote) { // if a new vote is sent...          if ($user_vote) { // if a new vote is sent...
277      if ($vote->voting_id) { // ...and the user has already voted                  $vote->value = voting_convert_to_percent($user_vote);
278        db_query("UPDATE {votes} SET vote=%d, timestamp=%d, hostname='%s' WHERE voting_id=%d", $new_vote, time(), $_SERVER['REMOTE_ADDR'], $vote->voting_id);      $vote->value_type = 'percent';
279        setcookie ("vote_{$content_type}_{$content_id}", $prev_vote->voting_id, time()+60*60*24*30); // expire in 30 days      $vote->tag = 'vote';
280      } else { // ...and the user has NOT voted yet                  votingapi_set_vote($content_type, $content_id, $vote);
281        $result = db_query("SELECT MAX(voting_id) FROM {votes}");          } else {
282        $voting_id = $result ? db_result($result, 0) : 0;                  $vote = votingapi_get_user_votes($content_type, $content_id);
283        $voting_id++;                  $user_vote = voting_convert_from_percent($vote[0]->value);
284        db_query("INSERT INTO {votes} (voting_id, content_type, content_id, vote, uid, timestamp, hostname) VALUES (%d, '%s', %d, %d, %d, %d, '%s')",          }
285          $voting_id, $content_type, $content_id, $new_vote, $user->uid, time(), $_SERVER['REMOTE_ADDR']);  
286        setcookie ("vote_{$content_type}_{$content_id}", $voting_id, time()+60*60*24*30); // expire in 30 days          // get average and count
287      }          $average = votingapi_get_voting_result($content_type, $content_id, 'percent', 'vote', 'average');
288                  $vote = voting_get_vote($content_type, $content_id); // get new updated average vote and number of votes          $avg_vote = voting_convert_from_percent($average->value);
289    }          $count = votingapi_get_voting_result($content_type, $content_id, 'percent', 'vote', 'count');
290    
291    // return the current vote info to flash    // return the current vote info to flash
292    $vote->vote = ($vote->vote) ? $vote->vote : 0;    $user_vote = ($user_vote) ? $user_vote : 0;
293    $vote->avg_vote = ($vote->avg_vote) ? $vote->avg_vote : 0;    $avg_vote = ($avg_vote) ? $avg_vote : 0;
294    $vote->num_votes = ($vote->num_votes) ? $vote->num_votes : 0;    $num_votes = ($count->value) ? $count->value : 0;
295    echo "vote=$vote->vote&avg_vote=$vote->avg_vote&num_votes=$vote->num_votes";    echo "vote=$user_vote&avg_vote=$avg_vote&num_votes=$num_votes";
296  }  }
297    
298  /**  /**
# Line 182  function voting_control_generic($content Line 321  function voting_control_generic($content
321  function theme_voting_control_flash($content_type, $content_id, $attributes = array()) {  function theme_voting_control_flash($content_type, $content_id, $attributes = array()) {
322    global $user;    global $user;
323    
324    // required variables    // optional user_access() dependent variables
   $url = urlencode('voting/flash'); // callback URL that retrieves voting info and process votes  
   
   // optional: user_access() dependent variables  
325    if (user_access('vote on content') && user_access('show average without voting')) {    if (user_access('vote on content') && user_access('show average without voting')) {
326      $mode = 'show_avg_first';      $mode = 'show_avg_first';
327    } elseif (user_access('show average without voting')) {    } elseif (user_access('show average without voting')) {
328      $mode = 'show_avg_only';      $mode = 'show_avg_only';
329    }    }
330    
331          // optional: variables set from administer/settings/voting          // optional variables set from administer/settings/voting
332    $star_on_fill = variable_get('voting_on_fill', '0x990000');    $star_on_fill = variable_get('voting_on_fill', '0x990000');
333    $star_on_border = variable_get('voting_on_border', '0x330000');    $star_on_border = variable_get('voting_on_border', '0x330000');
334    $star_off_fill = variable_get('voting_off_fill', '0xcccccc');    $star_off_fill = variable_get('voting_off_fill', '0xcccccc');
# Line 218  function theme_voting_control_flash($con Line 354  function theme_voting_control_flash($con
354    $optional_vars .= "&txt_login=$txt_login&txt_before_vote=$txt_before_vote&txt_after_vote=$txt_after_vote";    $optional_vars .= "&txt_login=$txt_login&txt_before_vote=$txt_before_vote&txt_after_vote=$txt_after_vote";
355    
356          // create the Flash filename, including the required variables and optional querystring          // create the Flash filename, including the required variables and optional querystring
357          $filename = drupal_get_path('module', 'voting') . "/voting.swf?content_type=$content_type&content_id=$content_id&url=$url" . $optional_vars;          $filename = base_path() . drupal_get_path('module', 'voting') . "/voting.swf?content_type=$content_type&content_id=$content_id&base_url=" . base_path() . $optional_vars;
358    
359          // create the HTML to put the Flash object on the page          // create the HTML to put the Flash object on the page
360          //          //
# Line 230  function theme_voting_control_flash($con Line 366  function theme_voting_control_flash($con
366     width="210" height="35" id="voting" align="">' . "\n";     width="210" height="35" id="voting" align="">' . "\n";
367    $output .= '<param name=movie value="' . $filename . '">' . "\n";    $output .= '<param name=movie value="' . $filename . '">' . "\n";
368    $output .= '<param name=quality value=high>' . "\n";    $output .= '<param name=quality value=high>' . "\n";
369    $output .= '<embed src="' . $filename . '" quality=high bgcolor=#161620 width="210" height="35" name="voting" align=""    $output .= '<embed src="' . $filename . '" quality="high" width="210" height="35" name="voting" align=""
370     type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>' . "\n";     type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>' . "\n";
371    $output .= "</object>\n";    $output .= "</object>\n";
372    $output .= "</td></tr></table>\n";    $output .= "</td></tr></table>\n";
# Line 242  function theme_voting_control_flash($con Line 378  function theme_voting_control_flash($con
378   * Implementation of hook_nodeapi()   * Implementation of hook_nodeapi()
379   */   */
380  function voting_nodeapi(&$node, $op, $teaser, $page) {  function voting_nodeapi(&$node, $op, $teaser, $page) {
   
381    switch ($op) {    switch ($op) {
382      case 'view':      case 'view':
383        if (!$teaser) {        if ($node->voting) { // if voting is enabled for this node and this view (votes don't show on teasers)
384                                  switch (variable_get('voting_location', 1)) {                                  switch (variable_get('voting_location', 1)) {
385                                          case 0: // display above the node                                          case 0: // display above the node
386                                                  $node->body = voting_control_node($node, $teaser, $page) . $node->body;                                                  $node->body = voting_control_node($node, $teaser, $page) . $node->body;
# Line 257  function voting_nodeapi(&$node, $op, $te Line 392  function voting_nodeapi(&$node, $op, $te
392                                  }                                  }
393        }        }
394        break;        break;
     case 'settings': // default workflow settings (administer/content/configure/content types)  
       return form_radios(t('Voting'), 'voting_'. $node->type, variable_get('voting_'. $node->type, 0), array(t('Disabled'), t('Enabled')));  
     case 'form admin': // editing a node  
       if (user_access('administer nodes')) {  
         $selected = isset($node->voting) ? $node->voting : variable_get('voting_' . $node->type, 0);  
         $output = form_radios('', 'voting', $selected, array(t('Disabled'), t('Enabled')));  
                                 //return form_group_collapsible(t('Voting options'), $output, TRUE); // for Drupal 4.7+  
         return form_group(t('Voting options'), $output); // for Drupal 4.6 and before  
       }  
       break;  
395      case 'load':      case 'load':
396        $result = db_query("SELECT voting FROM {node_voting} WHERE nid = %d", $node->nid);        $result = db_query("SELECT voting FROM {node_voting} WHERE nid = %d", $node->nid);
397        if ($voting = db_fetch_object($result)) {        if ($voting = db_fetch_object($result)) {
398          $node->voting = $voting->voting;          $node->voting = $voting->voting;
399        } else {        } else {
400          $node->voting = variable_get("voting_$node->type", 0);          $node->voting = variable_get("voting_nodeapi_{$node->type}", 'never');
401        }        }
402        if ($teaser) {        if ($teaser && !variable_get('voting_show_in_teaser', 0)) {
403          $node->voting = 0;          $node->voting = 0;
404        }        }
405        break;        break;
# Line 298  function voting_nodeapi(&$node, $op, $te Line 423  function voting_nodeapi(&$node, $op, $te
423        break;        break;
424      case 'delete':      case 'delete':
425        db_query('DELETE FROM {node_voting} WHERE nid = %d', $node->nid);        db_query('DELETE FROM {node_voting} WHERE nid = %d', $node->nid);
426        db_query("DELETE FROM {votes} WHERE content_type='node' AND content_id = %d", $node->nid);        //db_query("DELETE FROM {votes} WHERE content_type='node' AND content_id = %d", $node->nid);
427          break;
428      }
429    }
430    
431    
432    /**
433     * @defgroup event_nodeapi Functions for nodeapi integration
434     */
435    function voting_form_alter($form_id, &$form) {
436      global $user;
437      $type = (isset($form['type']) && isset($form['type']['#value'])) ? $form['type']['#value'] : NULL;
438      $node = isset($form['#node']) ? $form['#node'] : NULL;
439    
440      switch ($form_id) {
441        // node settings form
442        case $type .'_node_settings':
443          $form['workflow']['voting_nodeapi_'. $type] = array(
444            '#type' => 'radios',
445            '#title' => t('Show voting control'),
446            '#default_value' => variable_get('voting_nodeapi_'. $type, 'never'),
447            '#options' => array('always' => t('Always'), 'usually' => t('Usually'), 'sometimes' => 'Sometimes', 'never' => t('Never')),
448            '#description' => t('<strong>Always: </strong>A voting control will be displayed with all nodes of this types (including existing nodes).<br />
449                                            <strong>Usually: </strong>Voting controls will usually be shown with this node type, but a setting in the node edit form will allow voting administrators to disable some voting controls.<br />
450                                            <strong>Sometimes: </strong>Voting control will be disabed by default, but a setting in the node edit form will allow voting administrators to enable some voting controls.<br />
451                                            <strong>Never: </strong>Voting controls will never be shown with this node type.')
452          );
453        break;        break;
454    
455        // node edit form
456        case $type .'_node_form':
457                            switch (variable_get('voting_nodeapi_' . $type, 'never')) {
458                                    case 'always':
459                                            break; // don't display an option if voting is set to 'always' for this content type
460                                    case 'never':
461                                            break; // don't display an option if voting is set to 'never' for this content type
462                                    case 'usually':
463                                            $default = 1;
464                                    case 'sometimes':
465                                            $default = 0;
466    
467                                            $selected = isset($node->voting) ? $node->voting : $default;
468                                            $form['voting'] = array(
469                                                    '#type' => 'fieldset',
470                                                    '#title' => t('Voting'),
471                                                    '#collapsible' => true,
472                                                    '#collapsed' => true,
473                                                    );
474                                            $form['voting']['voting'] = array(
475                                                    '#type' => 'radios',
476                                                    '#title' => 'Voting',
477                                                    '#default_value' => $selected,
478                                                    '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
479                                            );
480                            }
481            break;
482    }    }
483  }  }
484    
485    
486  /** ---------------------------------------------------------------------------------------------------  /** ---------------------------------------------------------------------------------------------------
487   * Voting Filter   * Voting Filter
488   *   *

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

  ViewVC Help
Powered by ViewVC 1.1.2