
--- /sites/all/modules/plus1.module  Wed Mar 18 06:04:42 2009
+++ /sites/all/modules/plus1_new.module      Wed Feb 24 12:15:14 2010
@@ -17,10 +17,10 @@ function plus1_perm() {
 */
 function plus1_menu() {

-  $items['plus1/vote/%'] = array(
+  $items['plus1/vote/%/%'] = array(
     'title' => t('Vote'),
     'page callback' => 'plus1_vote',
-    'page arguments' => array(2),
+    'page arguments' => array(2,3),
     'access arguments' => array('vote on content'),
     'type' => MENU_CALLBACK,
   );
@@ -51,15 +51,24 @@ function plus1_settings() {
   $form['plus1_nodetypes_fieldset'] = array(
     '#type' => 'fieldset',
     '#title' => t('Content type settings'),
-    '#description' => t('Select all node types to which a +1 voting widget can
be added.'),
+    '#description' => t('Select all node and user types to which a +1 voting wi
dget can be added.'),
   );

   $form['plus1_nodetypes_fieldset']['plus1_nodetypes'] = array(
+    '#title' => t('Select content types to which add the voting widget'),
     '#type' => 'checkboxes',
     '#options' => node_get_types('names'),
     '#default_value' => variable_get('plus1_nodetypes', array('story')),
   );

+  $form['plus1_nodetypes_fieldset']['plus1_usertypes'] = array(
+    '#title' => t('Select user types to which add the voting widget'),
+    '#type' => 'checkboxes',
+    '#options' => user_roles($membersonly = TRUE, $permission = NULL),
+    '#default_value' => variable_get('plus1_usertypes', array('authenticated'))
,
+  );
+
+
   $form['plus1_display'] = array(
     '#type' => 'fieldset',
     '#title' => t('Display settings'),
@@ -89,6 +98,14 @@ function plus1_settings() {
     '#default_value' => variable_get('plus1_you_voted', t('You voted')),
     '#description' => t('Enter, for example, <em>Dugg</em>, <em>You voted</em>,
 or <em>Voted</em>.'),
   );
+
+  $form['plus1_text']['plus1_your_content'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Feedback provided to voter when it is his content or profile
'),
+    '#default_value' => variable_get('plus1_your_content', t('Your content')),
+    '#description' => t('Enter, for example, <em>Dugg</em>, <em>Your content</e
m>, or <em>Own</em>.'),
+  );
+

   $form['plus1_weight'] = array(
     '#type' => 'fieldset',
@@ -109,32 +126,36 @@ function plus1_settings() {

 /**
 * Page callback.
-* @param $nid
-* A node ID.
+* @param $c_type
+* A content type.
+* @param $c_id
+* A node/profile ID.
 * @param $ajax
 * Equal to 'json' when the function is called by jQuery.
 * Submits the vote request and refreshes the page without JavaScript.
 * Otherwise, it submits the vote request and returns JSON to be parsed by jQuer
y.
 */
-function plus1_vote($nid) {
+function plus1_vote($c_type, $c_id) {
   global $user;
   $json = isset($_GET['json']) ? 'json' : NULL;
-  if (!drupal_valid_token($_GET['token'], $nid)) {
+  if (!drupal_valid_token($_GET['token'], $c_id)) {
     watchdog('Plus1', 'Voting form error: Invalid token.');
     return drupal_access_denied();
   }

-  $voted = plus1_get_votes($nid, $user->uid);
+  $voted = plus1_get_votes($c_type, $c_id, $user->uid);
   // If the voter has not already voted.
   if (!$voted) {
     $votes[] = array(
-      'content_id' => $nid,
+      'content_type' => $c_type,
+      'content_id' => $c_id,
       'value_type' => 'points',
       'value' => 1,
     );
     votingapi_set_votes($votes);
     $criteria = array(
-      'content_id' => $nid,
+      'content_type' => $c_type,
+      'content_id' => $c_id,
       'function' => 'sum',
     );
     $results = votingapi_select_results($criteria);
@@ -146,7 +167,7 @@ function plus1_vote($nid) {
     else {
       // Go to the full node view.
       drupal_set_message(t('Thank you for your vote.'));
-      drupal_goto('node/'. $nid);
+      drupal_goto($c_type .'/'. $c_id);
     }
   }
 }
@@ -154,15 +175,18 @@ function plus1_vote($nid) {
 /**
 * Return the number of votes for a given node ID/user ID pair.
 *
-* @param $nid
-* A node ID.
+* @param $c_type
+* A content type.
+* @param $c_id
+* A node/profile ID.
 * @param $uid
 * A user ID.
 * @return Integer
 * Number of votes the user has cast on this node.
 */
-function plus1_get_votes($nid, $uid) {
-  $criteria['content_id'] = $nid;
+function plus1_get_votes($c_type, $c_id, $uid) {
+  $criteria['content_type'] = $c_type;
+  $criteria['content_id'] = $c_id;
   $criteria['value_type'] = 'points';
   if ($uid == 0) {
     $criteria['vote_source'] = ip_address();
@@ -177,13 +201,16 @@ function plus1_get_votes($nid, $uid) {
 /**
 * Return the total score of a node.
 *
-* @param $nid
-* A node ID.
+* @param $c_type
+* A content type.
+* @param $c_id
+* A node/profile ID.
 * @return Integer
 * The score.
 */
-function plus1_get_score($nid) {
-  $criteria['content_id'] = $nid;
+function plus1_get_score($c_type, $c_id) {
+  $criteria['content_type'] = $c_type;
+  $criteria['content_id'] = $c_id;
   $criteria['value_type'] = 'points';
   $criteria['function'] = 'sum';
   $results = votingapi_select_results($criteria);
@@ -198,8 +225,8 @@ function plus1_get_score($nid) {
 /**
 * Create voting widget to display on the webpage.
 */
-function plus1_jquery_widget($node, $teaser, $page) {
-  $score = plus1_get_score($node->nid);
+function plus1_jquery_widget($type, $datas, $teaser, $page) {
+  $score = ($type=='node') ? plus1_get_score($type, $datas->nid): plus1_get_sco
re($type, $datas->uid);
   global $user;
   // If user is not logged-in.
   if ($user->uid == 0) {
@@ -208,10 +235,10 @@ function plus1_jquery_widget($node, $tea
   }
   else {
     $logged_in = TRUE;
-    $is_author = ($node->uid == $user->uid)? TRUE : FALSE;
+    $is_author = ($datas->uid == $user->uid)? TRUE : FALSE;
   }
-  $voted = plus1_get_votes($node->nid, $user->uid);
-  return theme('plus1_widget', $node, $score, $logged_in, $is_author, $voted, $
teaser, $page);
+  $voted = ($type=='node') ? plus1_get_votes($type, $datas->nid, $user->uid) :
plus1_get_votes($type, $datas->uid, $user->uid);
+  return theme('plus1_widget', $type, $datas, $score, $logged_in, $is_author, $
voted, $teaser, $page);
 }

 /**
@@ -225,13 +252,14 @@ function plus1_nodeapi(&$node, $op, $tea
         // Show the widget.
         if (($teaser && variable_get('plus1_in_teaser', 0)) || (!$teaser && var
iable_get('plus1_in_full_view', 1))) {
           $node->content['plus1_widget'] = array(
-            '#value' => plus1_jquery_widget($node, $teaser, $page),
+            '#value' => plus1_jquery_widget('node', $node, $teaser, $page),
             '#weight' => (int) variable_get('plus1_weight', '100'),
           );
         }
       }
       break;
     case 'delete':
+      $criteria['content_type'] = 'node';
       $criteria['content_id'] = $node->nid;
       $votes = votingapi_select_votes($criteria);
       votingapi_delete_votes($votes);
@@ -240,12 +268,41 @@ function plus1_nodeapi(&$node, $op, $tea
 }

 /**
+* Implementation of hook_user().
+*/
+function plus1_user($op, &$edit, &$account, $category = NULL) {
+  switch ($op) {
+    case 'view':
+      // Only show the voting widget in allowed user types.
+        // $account->content['tervehdys'] = array('#value' => $account->roles[3
].' oujee '.var_dump(variable_get('plus1_usertypes', array('authenticated'))),);

+        $role_keys = array_keys($account->roles);
+      if (array_intersect($role_keys, variable_get('plus1_usertypes', array('au
thenticated')))) {
+        // Show the widget.
+        if (($teaser && variable_get('plus1_in_teaser', 0)) || (!$teaser && var
iable_get('plus1_in_full_view', 1))) {
+          $account->content['plus1_widget'] = array(
+            '#value' => plus1_jquery_widget('user', $account, $teaser, $page),
+            '#weight' => (int) variable_get('plus1_weight', '100'),
+          );
+        }
+      }
+      break;
+    case 'delete':
+      $criteria['content_type'] = 'user';
+      $criteria['content_id'] = $account->uid;
+      $votes = votingapi_select_votes($criteria);
+      votingapi_delete_votes($votes);
+      break;
+  }
+}
+
+
+/**
 * Implementation of hook_theme().
 */
 function plus1_theme() {
   return array(
     'plus1_widget' => array(
-      'arguments' => array('node', 'score', 'logged_in', 'is_author', 'voted',
'teaser', 'page'),
+      'arguments' => array('type', 'datas', 'score', 'logged_in', 'is_author',
'voted', 'teaser', 'page'),
     ),
   );
 }
@@ -271,7 +328,7 @@ function plus1_theme() {
 * The JavaScript adds presentation, ie: fade in.
 *
 */
-function theme_plus1_widget($node, $score, $logged_in, $is_author, $voted, $tea
ser, $page) {
+function theme_plus1_widget($type, $datas, $score, $logged_in, $is_author, $vot
ed, $teaser, $page) {

   // Load the JavaScript and CSS files.
   // You are free to load your own JavaScript files in your theming function to
 override.
@@ -301,11 +358,15 @@ function theme_plus1_widget($node, $scor
     else if ($voted) { // User already voted.
       $output .= check_plain(variable_get('plus1_you_voted', t('You voted')));
     }
+    else if ($is_author) { // User owner.
+      $output .= check_plain(variable_get('plus1_your_content', t('Your content
')));
+    }
     else if (user_access('vote on content')) {
       // User is eligible to vote.
       // The class name provided by Drupal.settings.plus1.link_class what we wi
ll search for in our jQuery later.
-      $output .= '<div class="plus1-vote">' . l(t('Vote'), 'plus1/vote/' . $nod
e->nid,
-        array('query' => 'token=' . drupal_get_token($node->nid), 'attributes'
=> array('class' => $link_class))) . '</div>';
+      $c_id = ($type=='node')? $datas->nid : $datas->uid ;
+      $output .= '<div class="plus1-vote">' . l(t('Vote'), 'plus1/vote/'. $type
 .'/' . $c_id,
+        array('query' => 'token=' . drupal_get_token($c_id), 'attributes' => ar
ray('class' => $link_class))) . '</div>';
     }
     $output .= '</div>';
   }
