diff -urpN sites/all/modules/nodevote/nodevote.install sites/all/modules/nodevoteNew/nodevote.install --- sites/all/modules/nodevote/nodevote.install 2008-11-04 19:56:31.000000000 +0000 +++ sites/all/modules/nodevoteNew/nodevote.install 2008-11-03 15:02:22.000000000 +0000 @@ -1,4 +1,10 @@ array('uid', 'nid'), 'indexes' => array('node_nid' => array('nid'), 'user_id' => array('uid')), ); + $schema['nodevote_exclude'] = array( + 'description' => t('Stores the nodes which are excluded from voting'), + 'fields' => array( + 'nid' => array( + 'description' => t('Node ID'), + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'option' => array( + 'description' => t('Option'), + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + ), + 'primary key' => array('nid'), + ); return $schema; } @@ -57,10 +81,47 @@ function nodevote_update_2() { return $ret; } +function nodevote_update_3() { + $schema['nodevote_exclude'] = array( + 'description' => t('Stores the nodes which are excluded from voting'), + 'fields' => array( + 'nid' => array( + 'description' => t('Node ID'), + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'option' => array( + 'description' => t('Option'), + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + ), + 'primary key' => array('nid'), + ); + $ret = array(); + db_create_table($ret, 'nodevote_exclude', $schema['nodevote_exclude']); + return $ret; +} + /** -* Implementation of hook_uninstall(). -*/ + * Implementation of hook_uninstall(). + */ function nodevote_uninstall() { drupal_uninstall_schema('nodevote'); -} - + drupal_uninstall_schema('nodevote_exclude'); + variable_del('nodevote_average'); + variable_del('nodevote_block_count'); + variable_del('nodevote_change_vote'); + variable_del('nodevote_daily_threshold'); + variable_del('nodevote_highest_block_threshold'); + variable_del('nodevote_max_votes'); + variable_del('nodevote_result_display_page'); + variable_del('nodevote_result_display_teaser'); + variable_del('nodevote_result_voted'); + variable_del('nodevote_vote_own_node'); + foreach (node_get_types() as $type => $name) { + variable_del('nodevote_type_'. $type); + } +} \ No newline at end of file diff -urpN sites/all/modules/nodevote/nodevote.module sites/all/modules/nodevoteNew/nodevote.module --- sites/all/modules/nodevote/nodevote.module 2008-11-04 19:56:31.000000000 +0000 +++ sites/all/modules/nodevoteNew/nodevote.module 2008-11-04 20:21:17.000000000 +0000 @@ -1,8 +1,11 @@ 'nodevote_page', 'page arguments' => array(1), - 'title' => t('Nodevote add'), + 'title' => 'Nodevote add', 'access callback' => 'user_access', 'access arguments' => array(NODEVOTE_PERM_USE), 'type' => MENU_CALLBACK @@ -46,27 +61,26 @@ function nodevote_menu() { $items['nodevote/%node'] = array( 'page callback' => 'nodevote_page', 'page arguments' => array(1), - 'title' => t('Nodevote view'), + 'title' => 'Nodevote view', 'access callback' => 'user_access', 'access arguments' => array(NODEVOTE_PERM_VIEW), 'type' => MENU_CALLBACK ); $items['admin/settings/nodevote'] = array( - 'title' => t('Nodevote'), - 'description' => t('Change settings for voting on nodes.'), + 'title' => 'Nodevote', + 'description' => 'Change settings for voting on nodes.', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodevote_admin_settings'), 'access callback' => 'user_access', 'access arguments' => array('administer site configuration'), 'type' => MENU_NORMAL_ITEM, // optional ); - - return $items; + return $items; } function nodevote_perm() { - return array (NODEVOTE_PERM_VIEW, NODEVOTE_PERM_USE); + return array(NODEVOTE_PERM_VIEW, NODEVOTE_PERM_USE, NODEVOTE_PERM_HIDE); } function nodevote_admin_settings() { @@ -76,7 +90,7 @@ function nodevote_admin_settings() { '#title' => t('Enable voting for these node types:'), ); - foreach(node_get_types() as $type => $name) { + foreach (node_get_types() as $type => $name) { $form[$set][NODEVOTE_TYPE . $type] = array( '#type' => 'checkbox', '#title' => $name->name, @@ -120,7 +134,7 @@ function nodevote_admin_settings() { '#type' => 'fieldset', '#title' => t('Advanced Settings'), ); - $form[$set][NODEVOTE_RESULT_VOTED] = array( + $form[$set][NODEVOTE_RESULT_VOTED] = array( '#type' => 'checkbox', '#title' => t('Show vote results only if user has voted on node.'), '#return_value' => 1, @@ -159,11 +173,37 @@ function nodevote_admin_settings() { '#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.'), ); - + $form[$set]['nodevote_max_votes'] = array( + '#type' => 'textfield', + '#title' => t('Maximum number of votes allowed'), + '#size' => 10, + '#required' => TRUE, + '#default_value' => variable_get('nodevote_max_votes', 10), + '#description' => t('Defines the maximum number of votes allowed.'), + ); + if (module_exists('rules')) { + $form[$set]['nodevote_average'] = array( + '#type' => 'textfield', + '#title' => t('Average number of votes'), + '#size' => 10, + '#default_value' => variable_get('nodevote_average', 8), + '#description' => t('This value is used in conjuction with the "Rules" module to define below/above average conditions.'), + ); + } return system_settings_form($form); } /** + * Ensure that the maximum number of votes submitted is numeric. + */ +function nodevote_admin_settings_validate($form, &$form_state) { + if (!is_numeric($form_state['values']['nodevote_max_votes'])) { + form_set_error('nodevote_max_votes', t('The maximum number of votes must be numeric.')); + } +} + + +/** * Check if the current voter has cast his maximum daily votes. * The user is allowed to vote if he did not exceed the threshold. * @@ -184,7 +224,7 @@ function nodevote_link($type, $node, $te if (_nodevote_is_votable($node)) { $links['nodevote_link'] = array( 'title' => t('Vote on it'), - 'href' => "node/$node->nid" + 'href' => 'node/'. $node->nid ); } } @@ -194,7 +234,7 @@ function nodevote_link($type, $node, $te function nodevote_user($op) { global $user; - switch($op) { + switch ($op) { case 'delete': db_query('DELETE FROM {nodevote} WHERE uid = %d', $user->uid); break; @@ -269,34 +309,41 @@ function nodevote_page($node) { $op = arg(2); - switch($op) { + switch ($op) { case 'add': $vote = $edit['vote']; if (_nodevote_validate_vote($vote)) { - if (_nodevote_user_voted($user->uid, $node->nid)){ + if (_nodevote_user_voted($user->uid, $node->nid)) { $result = db_query("UPDATE {nodevote} SET vote = %d WHERE uid = %d AND nid = %d", $vote, $user->uid , $node->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))) { + 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, $node->nid, $vote, time()); + // check if the maximum number of votes has been reached + if (_nodevote_get_num_votes($node->nid)) { + drupal_set_message(t('The Maximum number of votes has been reached')); + if (module_exists('rules')) { + rules_invoke_event('max_votes', $node); + } + } // do the userpoints dance nodevote_do_userpoints(); } - 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/' . $node->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/'. $node->nid); } drupal_goto("node/" . $node->nid); } else { drupal_set_message(t('You must select a valid vote score.'), 'error'); } - $o = l(t('Back to node'), 'node/' . $node->nid); + $o = l(t('Back to node'), 'node/'. $node->nid); break; case 'view': @@ -313,7 +360,7 @@ function nodevote_page($node) { function nodevote_nodeapi(&$node, $op, $teaser, $page) { global $user; - switch($op) { + switch ($op) { case 'load': if (variable_get(NODEVOTE_TYPE . $node->type, '0')) { if (_nodevote_vote_is_visible($node)) { @@ -346,24 +393,62 @@ function nodevote_nodeapi(&$node, $op, $ } break; + case 'insert': + if (user_access(NODEVOTE_PERM_HIDE)) { + if (variable_get(NODEVOTE_TYPE . $node->type, 0)) { + db_query('INSERT INTO {nodevote_exclude} (`nid`, `option`) VALUES (%d, %d)', $node->nid, $node->nodevote_include_exclude); + } + } + break; + + case 'update': + if (user_access(NODEVOTE_PERM_HIDE)) { + if (variable_get(NODEVOTE_TYPE . $node->type, 0)) { + db_query('DELETE FROM {nodevote_exclude} WHERE nid = %d', $node->nid); + db_query('INSERT INTO {nodevote_exclude} (`nid`, `option`) VALUES (%d, %d)', $node->nid, $node->nodevote_include_exclude); + } + } + break; + case 'alter': - if(!$page) { + if (!$page) { if (variable_get(NODEVOTE_RESULT_DISPLAY_TEASER, 0) != 3) { $data = _nodevote_get_vote_data($node->nid, $page); $node->content['nodevote_result'] = array( '#value' => theme('nodevote_display_vote', $vote_data), '#weight' => 10, ); - } + } } break; case 'delete': db_query('DELETE FROM {nodevote} WHERE nid = %d', $node->nid); + if (user_access(NODEVOTE_PERM_HIDE)) { + if (variable_get(NODEVOTE_TYPE . $node->type, 0)) { + db_query('DELETE FROM {nodevote_exclude} WHERE nid = %d', $node->nid); + } + } break; } } +function nodevote_form_alter(&$form, $form_state, $form_id) { + $node = $form['#node']; + if (variable_get(NODEVOTE_TYPE . str_replace(':', '', $form['type']['#value']), 0)) { + if (user_access(NODEVOTE_PERM_HIDE)) { + $form['nodevote_include_exclude'] = array( + '#type' => 'radios', + '#title' => t('Display Nodevote Widget'), + '#default_value' => isset($node->nodevote_include_exclude) ? $node->nodevote_include_exclude : db_result(db_query('SELECT `option` FROM {nodevote_exclude} WHERE nid = %d', $node->nid)), + '#options' => array(0 => t('Yes'), 1 => t('No')), + '#required' => TRUE, + '#description' => t('Select whether to display the nodevote widget on this node.') + ); + } + } +} + function _nodevote_user_voted($uid, $nid) { if (db_result(db_query('SELECT count(nid) FROM {nodevote} WHERE uid = %d AND nid = %d', $uid, $nid))) { return TRUE; @@ -373,6 +458,17 @@ function _nodevote_user_voted($uid, $nid } } +// checks if the maximum number of votes has been reached +function _nodevote_get_num_votes($nid) { + $votes = db_result(db_query('SELECT count(*) FROM {nodevote} WHERE nid = %d', $nid)); + if ($votes != variable_get('nodevote_max_votes', 10)) { + return FALSE; + } + else { + return TRUE; + } +} + 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)); @@ -394,7 +490,7 @@ function theme_nodevote_display_vote($vo $votes = $vote_data['votes']; $vote_display = $vote_data['vote_display']; - switch($vote_display) { + switch ($vote_display) { case 1: $result = _nodevote_display_stars($votes, $score); break; @@ -411,7 +507,7 @@ function theme_nodevote_display_vote($vo } $output .= '
'; - $output .= '

' . t('Vote Result') . '

'; + $output .= '

'. t('Vote Result') .'

'; $output .= $result; $output .= '
'; @@ -419,7 +515,7 @@ function theme_nodevote_display_vote($vo } function _nodevote_display_number($votes, $score) { - return '
' . t('Score: @score, Votes: @votes', array('@votes' => $votes, '@score' => number_format($score, 1))); + return '
'. t('Score: @score, Votes: @votes', array('@votes' => $votes, '@score' => number_format($score, 1))); } function _nodevote_display_stars($votes, $score) { @@ -433,7 +529,7 @@ function _nodevote_display_stars($votes, $val = 'off'; $alt = '-'; } - $stars .='' . $alt . ''; + $stars .=''. $alt .''; }; return $stars; @@ -453,8 +549,11 @@ function _nodevote_is_votable($node) { // 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; + if ((!_nodevote_user_voted($user->uid, $node->nid)) || (variable_get(NODEVOTE_CHANGE_VOTE, 0))) { + // Has the maximum number of votes been reached + if (!_nodevote_get_num_votes($node->nid)) { + $ret = TRUE; + } } } } @@ -463,17 +562,34 @@ function _nodevote_is_votable($node) { return $ret; } +function _nodevote_widget_is_visible($node) { + $result = db_fetch_object(db_query('SELECT `option` FROM {nodevote_exclude} WHERE nid = %d', $node->nid)); + if (!$result) { + return TRUE; + } + else { + if ($result->option == 0) { + return TRUE; + } + elseif ($result->option == 1) { + return FALSE; + } + } +} + 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)) { + if (_nodevote_widget_is_visible($node)) { + if (!variable_get(NODEVOTE_RESULT_VOTED, 0)) { $ret = TRUE; } + else { + if (_nodevote_user_voted($user->uid, $node->nid)) { + $ret = TRUE; + } + } } } return $ret; @@ -502,12 +618,12 @@ function nodevote_rate_form($form_state, $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; + 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'), + '#value' => t('
Please rate this node. 1 = worst score, 10 = best score'), '#prefix' => '
', '#suffix' => '
', - ); + ); $form[$set]['vote'] = array( '#type' => 'select', @@ -532,7 +648,7 @@ function nodevote_rate_form($form_state, ); $form['#method'] = 'post'; - $form['#action'] = url('nodevote/' . $node->nid . '/add'); + $form['#action'] = url('nodevote/'. $node->nid .'/add'); $form['#attributes'] = array('class' => 'nodevote-form'); return $form; @@ -551,7 +667,7 @@ function _nodevote_validate_vote($vote) } function nodevote_userpoints($op, $points = 0, $uid = 0, $event = '') { - switch($op) { + switch ($op) { case 'setting': $group = 'nodevote'; $form[$group] = array( @@ -579,5 +695,4 @@ function nodevote_do_userpoints() { $points = variable_get(NODEVOTE_USERPOINTS, 0); userpoints_userpointsapi('points', $points, $user->uid, 'nodevote'); } -} - +} \ No newline at end of file diff -urpN sites/all/modules/nodevote/nodevote.rules.inc sites/all/modules/nodevoteNew/nodevote.rules.inc --- sites/all/modules/nodevote/nodevote.rules.inc 1970-01-01 01:00:00.000000000 +0100 +++ sites/all/modules/nodevoteNew/nodevote.rules.inc 2008-11-04 20:21:21.000000000 +0000 @@ -0,0 +1,136 @@ + array( + 'label' => t('Reached maximum votes'), + 'help' => t("A node has reached the maximum number of votes it is allowed."), + 'arguments' => rules_events_hook_nodevote_arguments(t('maximum votes')), + 'module' => 'Nodevote', + ), + ); +} + +/* + * Returns some arguments suitable for hook nodevote + */ +function rules_events_hook_nodevote_arguments() { + return array( + 'node' => array( + 'type' => 'node', + 'label' => t('Node voted on'), + 'handler' => 'rules_events_argument_nodevote_node', + ), + 'node_author' => array( + 'type' => 'user', + 'label' => t('Node content author'), + 'handler' => 'rules_events_argument_nodevote_node_author', + ), + ) + rules_events_global_user_argument(); +} + +/** + * Gets the vote's node + */ +function rules_events_argument_nodevote_node($node) { + return node_load($node->nid); +} + +/** + * Gets the author of the note + */ +function rules_events_argument_nodevote_node_author($node) { + return user_load(array('uid' => $node->uid)); +} + +/** + * Implementation of hook_rules_condition_info(). + * @ingroup rules + */ +function nodevote_rules_condition_info() { + $items = array(); + $items['nodevote_rules_above_average'] = array( + 'label' => t('Votes are above average'), + 'arguments' => array( + 'node' => array('type' => 'node', 'label' => t('Content')), + ), + 'module' => 'Nodevote', + 'help' => t('Evaluates to TRUE, if the given node is above average in votes.'), + ); + $items['nodevote_rules_below_average'] = array( + 'label' => t('Votes are below average'), + 'arguments' => array( + 'node' => array('type' => 'node', 'label' => t('Content')), + ), + 'module' => 'Nodevote', + 'help' => t('Evaluates to TRUE, if the given node is below average in votes.'), + ); + return $items; +} + +function nodevote_rules_above_average($node) { + if (_nodevote_get_average_votes($node) >= variable_get('nodevote_average', 8)) + return TRUE; + else + return FALSE; +} + +function nodevote_rules_below_average($node) { + if (_nodevote_get_average_votes($node) < variable_get('nodevote_average', 8)) + return TRUE; + else + return FALSE; +} + +function _nodevote_get_average_votes($node) { + return db_result(db_query('SELECT AVG(vote) AS score FROM {nodevote} WHERE nid = %d', $node->nid)); +} + +function nodevote_rules_action_info() { + $items = array(); + $items['clear_votes'] = array( + 'label' => t('Clear all votes'), + 'arguments' => array( + 'node' => array('type' => 'node', 'label' => t('Content')), + ), + 'module' => 'Nodevote', + 'help' => t('Clears all votes from the node.'), + ); + $items['hide_widget'] = array( + 'label' => t('Hide Nodevote widget'), + 'arguments' => array( + 'node' => array('type' => 'node', 'label' => t('Content')), + ), + 'module' => 'Nodevote', + 'help' => t('Hides the nodevote widget for the selected node.'), + ); + $items['show_widget'] = array( + 'label' => t('Show Nodevote widget'), + 'arguments' => array( + 'node' => array('type' => 'node', 'label' => t('Content')), + ), + 'module' => 'Nodevote', + 'help' => t('Shows the nodevote widget for the selected node.'), + ); + return $items; +} + +function nodevote_rules_hide_widget($node) { + db_query('DELETE FROM {nodevote_exclude} WHERE nid = %d', $node->nid); + db_query('INSERT INTO {nodevote_exclude} (`nid`, `option`) VALUES (%d, 1)', $node->nid); +} + +function nodevote_rules_show_widget($node) { + db_query('DELETE FROM {nodevote_exclude} WHERE nid = %d', $node->nid); + db_query('INSERT INTO {nodevote_exclude} (`nid`, `option`) VALUES (%d, 0)', $node->nid); +}