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 .= '
';
+ $stars .='
';
};
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