diff -urNp nodevote_orig/CVS/Entries nodevote_50/CVS/Entries --- nodevote_orig/CVS/Entries Tue Nov 21 09:39:25 2006 +++ nodevote_50/CVS/Entries Wed Dec 31 19:00:00 1969 @@ -1,6 +0,0 @@ -/README.txt/1.8/Thu Aug 31 13:42:52 2006// -/nodevote.install/1.4/Fri Sep 15 20:13:51 2006// -/nodevote.module/1.27/Mon Sep 18 20:47:31 2006// -/star_off.png/1.1/Tue Apr 18 03:34:07 2006/-kb/ -/star_on.png/1.2/Sat Jul 8 18:02:49 2006/-kb/ -D diff -urNp nodevote_orig/CVS/Entries.Extra nodevote_50/CVS/Entries.Extra --- nodevote_orig/CVS/Entries.Extra Tue Nov 21 09:39:25 2006 +++ nodevote_50/CVS/Entries.Extra Wed Dec 31 19:00:00 1969 @@ -1,5 +0,0 @@ -/README.txt////*/// -/nodevote.install////*/// -/nodevote.module////*/// -/star_off.png////*/// -/star_on.png////*/// diff -urNp nodevote_orig/CVS/Repository nodevote_50/CVS/Repository --- nodevote_orig/CVS/Repository Tue Nov 21 09:39:25 2006 +++ nodevote_50/CVS/Repository Wed Dec 31 19:00:00 1969 @@ -1 +0,0 @@ -contributions/modules/nodevote diff -urNp nodevote_orig/CVS/Root nodevote_50/CVS/Root --- nodevote_orig/CVS/Root Tue Nov 21 09:39:25 2006 +++ nodevote_50/CVS/Root Wed Dec 31 19:00:00 1969 @@ -1 +0,0 @@ -:pserver:anonymous@cvs.drupal.org:/cvs/drupal-contrib diff -urNp nodevote_orig/nodevote.info nodevote_50/nodevote.info --- nodevote_orig/nodevote.info Wed Dec 31 19:00:00 1969 +++ nodevote_50/nodevote.info Tue Nov 21 09:39:34 2006 @@ -0,0 +1,4 @@ +; $Id$ +name = Nodevote +description = Allows users to rate nodes. +package = "Voting" \ No newline at end of file diff -urNp nodevote_orig/nodevote.install nodevote_50/nodevote.install --- nodevote_orig/nodevote.install Tue Nov 21 09:39:27 2006 +++ nodevote_50/nodevote.install Tue Nov 21 09:39:34 2006 @@ -1,67 +1,70 @@ - 'nodevote/' . $nid . '/add', - 'callback' => 'nodevote_page', - 'title' => t('nodevote add'), - 'access' => user_access(NODEVOTE_PERM_USE), - 'type' => MENU_CALLBACK); - - $items[] = array( - 'path' => 'nodevote/' . $nid, - 'callback' => 'nodevote_page', - 'title' => t('nodevote view'), - 'access' => user_access(NODEVOTE_PERM_VIEW), - 'type' => MENU_CALLBACK); - - return $items; -} - -function nodevote_perm() { - return array (NODEVOTE_PERM_VIEW, NODEVOTE_PERM_USE); -} - -function nodevote_settings() { - $set = 'types'; - $form[$set] = array( - '#type' => 'fieldset', - '#title' => t('Enable voting for these node types:'), - ); - - foreach(node_get_types() as $type => $name) { - $form[$set][NODEVOTE_TYPE . $type] = array( - '#type' => 'checkbox', - '#title' => $name, - '#return_value' => 1, - '#default_value' => variable_get(NODEVOTE_TYPE . $type, '0'), - ); - } - - $set = 'setting'; - $form[$set] = array( - '#type' => 'fieldset', - '#title' => t('Settings'), -); - - $form[$set][NODEVOTE_VOTE_DESC] = array( - '#type' => 'textfield', - '#title' => t('Vote Description'), - '#default_value' => variable_get(NODEVOTE_VOTE_DESC, 'Score'), - '#size' => 25, - '#maxlength' => 25, - ); - - $_display = array( - 0 => t('Text'), - 1 => t('Stars'), - 2 => t('Both') - ); - $form[$set][NODEVOTE_RESULT_DISPLAY_PAGE]= array( - '#type' => 'radios', - '#title' => t('Vote result display (page view)'), - '#default_value' => variable_get(NODEVOTE_RESULT_DISPLAY_PAGE, 0), - '#options' => $_display, - '#description' => t('Select which format the results of the vote will be displayed in when in node view.')); - - $_display = array( - 0 => t('Text'), - 1 => t('Stars'), - 2 => t('Both'), - 3 => t('None')); - $form[$set][NODEVOTE_RESULT_DISPLAY_TEASER]= array( - '#type' => 'radios', - '#title' => t('Vote result display (teaser view)'), - '#default_value' => variable_get(NODEVOTE_RESULT_DISPLAY_TEASER, 0), - '#options' => $_display, - '#description' => t('Select which format the results of the vote will be displayed in when in teaser view.')); - - $set='advance'; - $form[$set] = array( - '#type' => 'fieldset', - '#title' => t('Advanced Settings'), - ); - $form[$set][NODEVOTE_RESULT_VOTED] = array( - '#type' => 'checkbox', - '#title' => t('Show vote results only if user has voted on node.'), - '#return_value' => 1, - '#default_value' => variable_get(NODEVOTE_RESULT_VOTED, 0), - ); - $form[$set][NODEVOTE_CHANGE_VOTE] = array( - '#type' => 'checkbox', - '#title' => t('Allow user to vote again.'), - '#return_value' => 1, - '#default_value' => variable_get(NODEVOTE_CHANGE_VOTE, 0), - ); - $form[$set][NODEVOTE_VOTE_OWN_NODE] = array( - '#type' => 'checkbox', - '#title' => t('Allow user to vote on his own node.'), - '#return_value' => 1, - '#default_value' => variable_get(NODEVOTE_VOTE_OWN_NODE, 0), - ); - $form[$set][NODEVOTE_DAILY_THRESHOLD] = array( - '#type' => 'select', - '#title' => t('Daily threshold'), - '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 100, 250)), - '#default_value' => variable_get(NODEVOTE_DAILY_THRESHOLD, 10), - '#description' => t('The maximum number of votes a user can cast in a 24 hour period.'), - ); - $form[$set][NODEVOTE_BLOCK_COUNT] = array( - '#type' => 'select', - '#title' => t('Node per block'), - '#options' => drupal_map_assoc(array(3, 5, 7, 10, 15, 20, 25)), - '#default_value' => variable_get(NODEVOTE_BLOCK_COUNT, 3), - '#description' => t('Number of nodes to show in blocks.'), - ); - $form[$set][NODEVOTE_HIGHEST_BLOCK_THRESHOLD] = array( - '#type' => 'select', - '#title' => t('Highest block vote threshold'), - '#options' => drupal_map_assoc(array(3, 5, 7, 10, 15, 20, 25)), - '#default_value' => variable_get(NODEVOTE_HIGHEST_BLOCK_THRESHOLD, 3), - '#description' => t('Nodes must have the given number of votes before they show in the highest block.'), - ); - - return $form; -} - -/** - * Check if the current voter has cast his maximum daily votes. - * The user is allowed to vote if he did not exceed the threshold. - * - * @param $uid - * The the ID of the voting user. - * @param $threshold - * The maximum number votes allowed (per user) in a 24-hour period. - * @return - * True if the user did not exceed the threshold. False otherwise. - */ -function _nodevote_within_threshold($uid, $threshold) { - $number = db_num_rows(db_query("SELECT uid FROM {nodevote} WHERE uid = '%s' AND timestamp > '%d'", $uid, time() - 86400)); - return ($number < $threshold ? TRUE : FALSE); -} - -function nodevote_link($type, $node, $teaser = false) { - if ($type == 'node' && $teaser) { - if (_nodevote_is_votable($node)) { - return array(l(t('Rate it'), "node/$node->nid")); - } - } -} - -function nodevote_user($op) { - switch($op) { - case 'delete': - db_query('DELETE FROM {nodevote} WHERE uid = %d', $user->uid); - break; - } -} - -function nodevote_block($op = 'list', $delta = 0, $edit = array()) { - $num = variable_get(NODEVOTE_BLOCK_COUNT, 3); - $threshold = variable_get(NODEVOTE_HIGHEST_BLOCK_THRESHOLD, 3); - - $block_title = array(); - $block_title[] = t('Most voted for nodes'); - $block_title[] = t('Highest rated nodes'); - $block_title[] = t('Most voting users'); - - switch ($op) { - case 'list': - $blocks[0]['info'] = $block_title[0]; - $blocks[1]['info'] = $block_title[1]; - $blocks[2]['info'] = $block_title[2]; - return $blocks; - - case 'view': - if (user_access(NODEVOTE_PERM_VIEW)) { - switch ($delta) { - case 0: - $title = $block_title[$delta]; - $sql = 'SELECT n.nid, n.title, COUNT(*) AS num_votes - FROM {node} n, {nodevote} nv - WHERE n.nid = nv.nid - GROUP BY n.nid - ORDER BY num_votes DESC'; - $content = node_title_list(db_query_range($sql, 0, $num)); - break; - - case 1: - $title = $block_title[$delta]; - $sql = 'SELECT n.nid, n.title, AVG(vote) AS av_avg - FROM {node} n, {nodevote} nv - WHERE n.nid = nv.nid - GROUP BY n.nid - HAVING COUNT(*) >= %d - ORDER BY av_avg DESC'; - $content = node_title_list(db_query_range($sql, $threshold, 0, $num)); - break; - - case 2: - $title = $block_title[$delta]; - $result = db_query_range('SELECT nv.uid, u.name, COUNT(*) AS num_votes - FROM {nodevote} nv, {users} u - WHERE u.uid = nv.uid - GROUP BY nv.uid - ORDER BY num_votes DESC', 0, $num); - while ($account = db_fetch_object($result)) { - $items[] = $account; - } - $content = theme('user_list', $items); - break; - } - - $block['subject'] = $title; - $block['content'] = $content; - - return $block; - } - } -} - -function nodevote_page() { - global $user; - $edit = $_POST['edit']; - - $nid = (int)arg(1); - $op = arg(2); - - switch($op) { - case 'add': - $vote = $edit['vote']; - - if (_nodevote_validate_vote($vote)) { - if (_nodevote_user_voted($user->uid, $nid)){ - $result = db_query("UPDATE {nodevote} SET vote = $vote WHERE uid = %d AND nid = %d" , $user->uid , $nid); - } - else { - // check to see if the user has voted too many times today - if(_nodevote_within_threshold($user->uid, variable_get(NODEVOTE_DAILY_THRESHOLD, 10))){ - // log the vote - $result = db_query("INSERT INTO {nodevote} VALUES (%d, %d, %d, %d)", $user->uid, $nid, $vote, time()); - // do the userpoints dance - if(module_exist('userpoints')) {userpoints_nodevote($nid); - } - } - else { - // if they've voted too many times today, show a message - $output = t("You can only vote %number times in 24 hours. Please try again later.", array('%number' => variable_get(NODEVOTE_DAILY_THRESHOLD, 10))); - drupal_set_message($output, 'error'); - } - $o = l(t('back to node'), 'node/' . $nid); - } - drupal_goto("node/" . $nid); - } - else { - drupal_set_message(t('You must select a valid vote score.'), 'error'); - } - $o = l(t('back to node'), 'node/' . $nid); - break; - - case 'view': - default: - if (_nodevote_vote_is_visible($node)) { - $data = _nodevote_get_vote_data($node->nid); - $o = theme('nodevote_display_vote', $data['score'], $data['votes'], $data['vote_display']); - } - break; - } - - print theme('page', $o); -} - -function nodevote_nodeapi(&$node, $op, $teaser, $page) { - global $user; - switch($op) { - case 'load': - if (variable_get(NODEVOTE_TYPE . $node->type, '0')) { - if (_nodevote_vote_is_visible($node)) { - $extra['nodevote']->vote_display = 1; - } - if (_nodevote_is_votable($node)) { - $extra['nodevote']->vote_do = 1; - } - } - return $extra; - - case 'view': - if (variable_get(NODEVOTE_TYPE . $node->type, '0')) { - if ($node->nodevote->vote_display) { - $data = _nodevote_get_vote_data($node->nid, $page); - if ($page) { - $node->body .= theme('nodevote_display_vote', $data['score'], $data['votes'], $data['vote_display']); - } - else { - if (variable_get(NODEVOTE_RESULT_DISPLAY_TEASER, 0) != 3) { - $node->teaser .= theme('nodevote_display_vote', $data['score'], $data['votes'], $data['vote_display']); - } - } - } - } - - if ($page) { - if ($node->nodevote->vote_do) { - $vote = theme('nodevote_do_vote',$node); - $node->body .= $vote; - } - } - break; - case 'delete': - $r = db_query('DELETE FROM {nodevote} WHERE nid = %d', $node->nid); - break; - } -} - -function _nodevote_user_voted($uid, $nid) { - if (db_num_rows(db_query('SELECT * FROM {nodevote} WHERE uid = %d AND nid = %d', $uid, $nid))) { - return true; - } - else { - return false; - } -} - -function _nodevote_get_vote_data($nid, $page = true) { - - $result = db_fetch_array(db_query('SELECT COUNT(*) AS votes, AVG(vote) AS score FROM {nodevote} WHERE nid = %d', $nid)); - - $score = $result['score']; - $votes = $result['votes']; - - if ($page) { - $vote_display = variable_get(NODEVOTE_RESULT_DISPLAY_PAGE, 0); - } - else { - $vote_display = variable_get(NODEVOTE_RESULT_DISPLAY_TEASER, 0); - } - - return array('score' => $score, 'votes' => $votes, 'vote_display' => $vote_display); -} - -function theme_nodevote_display_vote($score, $votes, $vote_display) { - - $set = 'display'; - $form[$set] = array( - '#type' => 'fieldset', - '#title' => t('Vote Result'), - ); - switch($vote_display) { - case 1: - $form[$set][] = _nodevote_display_stars($votes, $score); - break; - - case 2: - $form[$set][] = _nodevote_display_stars($votes, $score); - $form[$set][] = _nodevote_display_form_number($votes, $score); - break; - - case 0: - default: - $form[$set][] = _nodevote_display_form_number($votes, $score); - break; - } - - return drupal_get_form('nodevote_form', $form); -} - -function _nodevote_display_form_number($votes, $score) { - $form['display']= array( - '#type' => 'markup', - '#title' => t('Vote Result'), - '#value' => t('Score: %score, Votes: %votes', array( - '%votes' => $votes, - '%score' => number_format($score, 1))), - '#prefix'=> '
', - '#suffix'=> '
', - ); - return $form; -} - -function _nodevote_display_stars($votes, $score) { - global $base_path; - for ($i = 1; $i< NODEVOTE_MAX_SCORE+1; $i++) { - if ($score >= $i) { - $val = 'on'; - $alt = '+'; - } - else { - $val = 'off'; - $alt = '-'; - } - $o .='' . $alt . ''; - }; - $form['display']= array( - '#type' => 'markup', - '#title' => t('Vote Result'), - '#value' => $o, - '#prefix'=> '
', - '#suffix'=> '
', - ); - return $form; -} - -function _nodevote_is_votable($node) { - global $user; - $ret = false; - - // Is user logged in? - if ($user->uid) { - // Is node type votable? - if (variable_get(NODEVOTE_TYPE . $node->type, '0')) { - // Does the user has permission to vote? - if (user_access(NODEVOTE_PERM_USE)) { - // Is the node posted by someone else? - if (($node->uid != $user->uid) || (variable_get(NODEVOTE_VOTE_OWN_NODE, 0))) { - // Did the user already vote on this node? - if ((!_nodevote_user_voted($user->uid, $node->nid)) || (variable_get(NODEVOTE_CHANGE_VOTE, 0))){ - $ret = true; - } - } - } - } - } - return $ret; -} - -function _nodevote_vote_is_visible($node) { - global $user; - $ret = false; - if (user_access(NODEVOTE_PERM_VIEW)) { - if (!variable_get(NODEVOTE_RESULT_VOTED, 0)) { - $ret = true; - } - else { - if (_nodevote_user_voted($user->uid, $node->nid)) { - $ret = true; - } - } - } - return $ret; -} - -function theme_nodevote_do_vote($node) { - global $user; - $score = array(); - $r = 0; - $score[] = t('Select'); - if (_nodevote_user_voted($user->uid, $node->nid)){ - $r = db_result(db_query("SELECT vote FROM {nodevote} WHERE uid = %d AND nid = %d" , $user->uid ,$node->nid)); - $score[] =$r; - } - - for($j=NODEVOTE_MIN_SCORE; $j 'fieldset', - '#title' => t('Votes'), - '#value' => t('
Please rate this node. 1 = worst score, 10 = best score'), - '#prefix' => '
', - '#suffix' => '
', - ); - - $vote_desc = variable_get(NODEVOTE_VOTE_DESC, 'Score'); - $form[$set]['vote'] = array( - '#type' => 'select', - '#title' => $vote_desc, - '#default_value' => $score[$r], - '#options' => $score, - ); - $form[$set]['button'] = array( - '#type' => 'submit', - '#value' => t('Vote'), - ); - - $form['#method'] = 'post'; - $form['#action'] = url('nodevote/' . $node->nid . '/add'); - $form['#attributes'] = array('class' => 'nodevote-form'); - return drupal_get_form('nodevote_form', $form); -} - -function _nodevote_validate_vote($vote) { - if (($vote < NODEVOTE_MIN_SCORE) || ($vote > NODEVOTE_MAX_SCORE)) { - return false; - } - return true; -} + 'nodevote/' . $nid . '/add', + 'callback' => 'nodevote_page', + 'title' => t('Nodevote add'), + 'access' => user_access(NODEVOTE_PERM_USE), + 'type' => MENU_CALLBACK); + + $items[] = array( + 'path' => 'nodevote/' . $nid, + 'callback' => 'nodevote_page', + 'title' => t('Nodevote view'), + 'access' => user_access(NODEVOTE_PERM_VIEW), + 'type' => MENU_CALLBACK); + + $items[] = array( + 'path' => 'admin/settings/nodevote', + 'title' => t('Nodevote'), + 'description' => t('Change settings for voting on nodes.'), + 'callback' => 'drupal_get_form', + 'callback arguments' => 'nodevote_admin_settings', + 'access' => user_access('administer site configuration'), + 'type' => MENU_NORMAL_ITEM, // optional + ); + + return $items; +} + +function nodevote_perm() { + return array (NODEVOTE_PERM_VIEW, NODEVOTE_PERM_USE); +} + +function nodevote_admin_settings() { + $set = 'types'; + $form[$set] = array( + '#type' => 'fieldset', + '#title' => t('Enable voting for these node types:'), + ); + + foreach(node_get_types() as $type => $name) { + $form[$set][NODEVOTE_TYPE . $type] = array( + '#type' => 'checkbox', + '#title' => $name->name, + '#return_value' => 1, + '#default_value' => variable_get(NODEVOTE_TYPE . $type, '0'), + ); + } + + $set = 'setting'; + $form[$set] = array( + '#type' => 'fieldset', + '#title' => t('Settings'), +); + + $form[$set][NODEVOTE_VOTE_DESC] = array( + '#type' => 'textfield', + '#title' => t('Vote Description'), + '#default_value' => variable_get(NODEVOTE_VOTE_DESC, 'Score'), + '#size' => 25, + '#maxlength' => 25, + ); + + $_display = array( + 0 => t('Text'), + 1 => t('Stars'), + 2 => t('Both') + ); + $form[$set][NODEVOTE_RESULT_DISPLAY_PAGE]= array( + '#type' => 'radios', + '#title' => t('Vote result display (page view)'), + '#default_value' => variable_get(NODEVOTE_RESULT_DISPLAY_PAGE, 0), + '#options' => $_display, + '#description' => t('Select which format the results of the vote will be displayed in when in node view.')); + + $_display = array( + 0 => t('Text'), + 1 => t('Stars'), + 2 => t('Both'), + 3 => t('None')); + $form[$set][NODEVOTE_RESULT_DISPLAY_TEASER]= array( + '#type' => 'radios', + '#title' => t('Vote result display (teaser view)'), + '#default_value' => variable_get(NODEVOTE_RESULT_DISPLAY_TEASER, 0), + '#options' => $_display, + '#description' => t('Select which format the results of the vote will be displayed in when in teaser view.')); + + $set='advance'; + $form[$set] = array( + '#type' => 'fieldset', + '#title' => t('Advanced Settings'), + ); + $form[$set][NODEVOTE_RESULT_VOTED] = array( + '#type' => 'checkbox', + '#title' => t('Show vote results only if user has voted on node.'), + '#return_value' => 1, + '#default_value' => variable_get(NODEVOTE_RESULT_VOTED, 0), + ); + $form[$set][NODEVOTE_CHANGE_VOTE] = array( + '#type' => 'checkbox', + '#title' => t('Allow user to vote again.'), + '#return_value' => 1, + '#default_value' => variable_get(NODEVOTE_CHANGE_VOTE, 0), + ); + $form[$set][NODEVOTE_VOTE_OWN_NODE] = array( + '#type' => 'checkbox', + '#title' => t('Allow user to vote on his own node.'), + '#return_value' => 1, + '#default_value' => variable_get(NODEVOTE_VOTE_OWN_NODE, 0), + ); + $form[$set][NODEVOTE_DAILY_THRESHOLD] = array( + '#type' => 'select', + '#title' => t('Daily threshold'), + '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 100, 250)), + '#default_value' => variable_get(NODEVOTE_DAILY_THRESHOLD, 10), + '#description' => t('The maximum number of votes a user can cast in a 24 hour period.'), + ); + $form[$set][NODEVOTE_BLOCK_COUNT] = array( + '#type' => 'select', + '#title' => t('Node per block'), + '#options' => drupal_map_assoc(array(3, 5, 7, 10, 15, 20, 25)), + '#default_value' => variable_get(NODEVOTE_BLOCK_COUNT, 3), + '#description' => t('Number of nodes to show in blocks.'), + ); + $form[$set][NODEVOTE_HIGHEST_BLOCK_THRESHOLD] = array( + '#type' => 'select', + '#title' => t('Highest block vote threshold'), + '#options' => drupal_map_assoc(array(3, 5, 7, 10, 15, 20, 25)), + '#default_value' => variable_get(NODEVOTE_HIGHEST_BLOCK_THRESHOLD, 3), + '#description' => t('Nodes must have the given number of votes before they show in the highest block.'), + ); + + return system_settings_form($form); +} + +/** + * Check if the current voter has cast his maximum daily votes. + * The user is allowed to vote if he did not exceed the threshold. + * + * @param $uid + * The the ID of the voting user. + * @param $threshold + * The maximum number votes allowed (per user) in a 24-hour period. + * @return + * True if the user did not exceed the threshold. False otherwise. + */ +function _nodevote_within_threshold($uid, $threshold) { + $number = db_num_rows(db_query("SELECT uid FROM {nodevote} WHERE uid = '%s' AND timestamp > '%d'", $uid, time() - 86400)); + return ($number < $threshold ? TRUE : FALSE); +} + +function nodevote_link($type, $node, $teaser = false) { + if ($type == 'node' && $teaser) { + if (_nodevote_is_votable($node)) { + return array( + 'title' => t('Rate it'), + 'href' => "node/$node->nid" + ); + } + } +} + +function nodevote_user($op) { + switch($op) { + case 'delete': + db_query('DELETE FROM {nodevote} WHERE uid = %d', $user->uid); + break; + } +} + +function nodevote_block($op = 'list', $delta = 0, $edit = array()) { + $num = variable_get(NODEVOTE_BLOCK_COUNT, 3); + $threshold = variable_get(NODEVOTE_HIGHEST_BLOCK_THRESHOLD, 3); + + $block_title = array(); + $block_title[] = t('Most voted for nodes'); + $block_title[] = t('Highest rated nodes'); + $block_title[] = t('Most voting users'); + + switch ($op) { + case 'list': + $blocks[0]['info'] = $block_title[0]; + $blocks[1]['info'] = $block_title[1]; + $blocks[2]['info'] = $block_title[2]; + return $blocks; + + case 'view': + if (user_access(NODEVOTE_PERM_VIEW)) { + switch ($delta) { + case 0: + $title = $block_title[$delta]; + $sql = 'SELECT n.nid, n.title, COUNT(*) AS num_votes + FROM {node} n, {nodevote} nv + WHERE n.nid = nv.nid + GROUP BY n.nid + ORDER BY num_votes DESC'; + $content = node_title_list(db_query_range($sql, 0, $num)); + break; + + case 1: + $title = $block_title[$delta]; + $sql = 'SELECT n.nid, n.title, AVG(vote) AS av_avg + FROM {node} n, {nodevote} nv + WHERE n.nid = nv.nid + GROUP BY n.nid + HAVING COUNT(*) >= %d + ORDER BY av_avg DESC'; + $content = node_title_list(db_query_range($sql, $threshold, 0, $num)); + break; + + case 2: + $title = $block_title[$delta]; + $result = db_query_range('SELECT nv.uid, u.name, COUNT(*) AS num_votes + FROM {nodevote} nv, {users} u + WHERE u.uid = nv.uid + GROUP BY nv.uid + ORDER BY num_votes DESC', 0, $num); + while ($account = db_fetch_object($result)) { + $items[] = $account; + } + $content = theme('user_list', $items); + break; + } + + $block['subject'] = $title; + $block['content'] = $content; + + return $block; + } + } +} + +function nodevote_page() { + global $user; + $edit = $_POST; + + $nid = (int)arg(1); + $op = arg(2); + + switch($op) { + case 'add': + $vote = $edit['vote']; + + if (_nodevote_validate_vote($vote)) { + if (_nodevote_user_voted($user->uid, $nid)){ + $result = db_query("UPDATE {nodevote} SET vote = $vote WHERE uid = %d AND nid = %d" , $user->uid , $nid); + } + else { + // check to see if the user has voted too many times today + if(_nodevote_within_threshold($user->uid, variable_get(NODEVOTE_DAILY_THRESHOLD, 10))){ + // log the vote + $result = db_query("INSERT INTO {nodevote} VALUES (%d, %d, %d, %d)", $user->uid, $nid, $vote, time()); + // do the userpoints dance + if(module_exists('userpoints')) {userpoints_nodevote($nid); + } + } + else { + // if they've voted too many times today, show a message + $output = t("You can only vote %number times in 24 hours. Please try again later.", array('%number' => variable_get(NODEVOTE_DAILY_THRESHOLD, 10))); + drupal_set_message($output, 'error'); + } + $o = l(t('Back to node'), 'node/' . $nid); + } + drupal_goto("node/" . $nid); + } + else { + drupal_set_message(t('You must select a valid vote score.'), 'error'); + } + $o = l(t('Back to node'), 'node/' . $nid); + break; + + case 'view': + default: + if (_nodevote_vote_is_visible($node)) { + $data = _nodevote_get_vote_data($node->nid); + $o = theme('nodevote_display_vote', $data['score'], $data['votes'], $data['vote_display']); + } + break; + } + + print theme('page', $o); +} + +function nodevote_nodeapi(&$node, $op, $teaser, $page) { + global $user; + switch($op) { + case 'load': + if (variable_get(NODEVOTE_TYPE . $node->type, '0')) { + if (_nodevote_vote_is_visible($node)) { + $extra['nodevote']->vote_display = 1; + } + if (_nodevote_is_votable($node)) { + $extra['nodevote']->vote_do = 1; + } + } + return $extra; + + case 'view': + if (variable_get(NODEVOTE_TYPE . $node->type, '0')) { + if ($node->nodevote->vote_display) { + $data = _nodevote_get_vote_data($node->nid, $page); + if ($page) { + $node->content['nodevote'] = array( + '#value' => theme('nodevote_display_vote', $data['score'], $data['votes'], $data['vote_display']) + ); + } + } + } + + if ($page) { + if ($node->nodevote->vote_do) { + $vote = theme('nodevote_do_vote',$node); + $node->content['nodevote'] = array('#value' => $vote); + } + } + break; + + case 'alter': + if(!$page) { + if (variable_get(NODEVOTE_RESULT_DISPLAY_TEASER, 0) != 3) { + $data = _nodevote_get_vote_data($node->nid, $page); + $node->teaser .= theme('nodevote_display_vote', $data['score'], $data['votes'], $data['vote_display']); + } + } + break; + + case 'delete': + $r = db_query('DELETE FROM {nodevote} WHERE nid = %d', $node->nid); + break; + } +} + +function _nodevote_user_voted($uid, $nid) { + if (db_num_rows(db_query('SELECT * FROM {nodevote} WHERE uid = %d AND nid = %d', $uid, $nid))) { + return true; + } + else { + return false; + } +} + +function _nodevote_get_vote_data($nid, $page = true) { + + $result = db_fetch_array(db_query('SELECT COUNT(*) AS votes, AVG(vote) AS score FROM {nodevote} WHERE nid = %d', $nid)); + + $score = $result['score']; + $votes = $result['votes']; + + if ($page) { + $vote_display = variable_get(NODEVOTE_RESULT_DISPLAY_PAGE, 0); + } + else { + $vote_display = variable_get(NODEVOTE_RESULT_DISPLAY_TEASER, 0); + } + + return array('score' => $score, 'votes' => $votes, 'vote_display' => $vote_display); +} + +function theme_nodevote_display_vote($score, $votes, $vote_display) { + return drupal_get_form('nodevote_display_form', $score, $votes, $vote_display); +} + +function nodevote_display_form($score, $votes, $vote_display) { + $set = 'display'; + $form[$set] = array( + '#type' => 'fieldset', + '#title' => t('Vote Result'), + ); + switch($vote_display) { + case 1: + $form[$set][] = _nodevote_display_stars($votes, $score); + break; + + case 2: + $form[$set][] = _nodevote_display_stars($votes, $score); + $form[$set][] = _nodevote_display_form_number($votes, $score); + break; + + case 0: + default: + $form[$set][] = _nodevote_display_form_number($votes, $score); + break; + } + + return $form; +} + + + +function _nodevote_display_form_number($votes, $score) { + $form['display']= array( + '#type' => 'markup', + '#title' => t('Vote Result'), + '#value' => t('Score: %score, Votes: %votes', array( + '%votes' => $votes, + '%score' => number_format($score, 1))), + '#prefix'=> '
', + '#suffix'=> '
', + ); + return $form; +} + +function _nodevote_display_stars($votes, $score) { + global $base_path; + for ($i = 1; $i< NODEVOTE_MAX_SCORE+1; $i++) { + if ($score >= $i) { + $val = 'on'; + $alt = '+'; + } + else { + $val = 'off'; + $alt = '-'; + } + $o .='' . $alt . ''; + }; + $form['display']= array( + '#type' => 'markup', + '#title' => t('Vote Result'), + '#value' => $o, + '#prefix'=> '
', + '#suffix'=> '
', + ); + return $form; +} + +function _nodevote_is_votable($node) { + global $user; + $ret = false; + + // Is user logged in? + if ($user->uid) { + // Is node type votable? + if (variable_get(NODEVOTE_TYPE . $node->type, '0')) { + // Does the user has permission to vote? + if (user_access(NODEVOTE_PERM_USE)) { + // Is the node posted by someone else? + if (($node->uid != $user->uid) || (variable_get(NODEVOTE_VOTE_OWN_NODE, 0))) { + // Did the user already vote on this node? + if ((!_nodevote_user_voted($user->uid, $node->nid)) || (variable_get(NODEVOTE_CHANGE_VOTE, 0))){ + $ret = true; + } + } + } + } + } + return $ret; +} + +function _nodevote_vote_is_visible($node) { + global $user; + $ret = false; + if (user_access(NODEVOTE_PERM_VIEW)) { + if (!variable_get(NODEVOTE_RESULT_VOTED, 0)) { + $ret = true; + } + else { + if (_nodevote_user_voted($user->uid, $node->nid)) { + $ret = true; + } + } + } + return $ret; +} + +function theme_nodevote_do_vote($node) { + return drupal_get_form('nodevote_rate_form', $node); +} + +function nodevote_rate_form($node) { + global $user; + $score = array(); + $r = 0; + $score[] = t('Select'); + if (_nodevote_user_voted($user->uid, $node->nid)){ + $r = db_result(db_query("SELECT vote FROM {nodevote} WHERE uid = %d AND nid = %d" , $user->uid ,$node->nid)); + $score[] =$r; + } + + for($j=NODEVOTE_MIN_SCORE; $j 'fieldset', + '#title' => t('Votes'), + '#value' => t('
Please rate this node. 1 = worst score, 10 = best score'), + '#prefix' => '
', + '#suffix' => '
', + ); + + $vote_desc = variable_get(NODEVOTE_VOTE_DESC, 'Score'); + $form[$set]['vote'] = array( + '#type' => 'select', + '#title' => $vote_desc, + '#default_value' => $score[$r], + '#options' => $score, + ); + $form[$set]['button'] = array( + '#type' => 'submit', + '#value' => t('Vote'), + ); + + $form['#method'] = 'post'; + $form['#action'] = url('nodevote/' . $node->nid . '/add'); + $form['#attributes'] = array('class' => 'nodevote-form'); + + return $form; +} + +function _nodevote_validate_vote($vote) { + if (($vote < NODEVOTE_MIN_SCORE) || ($vote > NODEVOTE_MAX_SCORE)) { + return false; + } + return true; +}