From 0edbe84f6eaad1a1bdd72e825448e6121ff5acbc Mon Sep 17 00:00:00 2001
From: Raj Pawan Gumdal <gumdal@gmail.com>
Date: Mon, 8 Jun 2015 22:48:55 +0530
Subject: [PATCH] 1. Ported the module to Drupal 7 2. Added support for
 retroactive revisions 3. Made provisions to store the timestamp of original
 node and revision

---
 userpoints_retroactive/userpoints_retroactive.info |   4 +-
 .../userpoints_retroactive.module                  | 183 +++++++++++++++++----
 2 files changed, 156 insertions(+), 31 deletions(-)

diff --git a/userpoints_retroactive/userpoints_retroactive.info b/userpoints_retroactive/userpoints_retroactive.info
index 5206778..4a5dad7 100755
--- a/userpoints_retroactive/userpoints_retroactive.info
+++ b/userpoints_retroactive/userpoints_retroactive.info
@@ -1,6 +1,6 @@
 name = Userpoints Retroactive
 description = Calculate userpoints for nodes and comments created so far.
 package = Userpoints
-core = 6.x
+core = 7.x
+configure = admin/settings/userpoints/retroactive
 dependencies[] = userpoints
-dependencies[] = not_compatible_with_d7
diff --git a/userpoints_retroactive/userpoints_retroactive.module b/userpoints_retroactive/userpoints_retroactive.module
index d24fb4c..fa019b8 100644
--- a/userpoints_retroactive/userpoints_retroactive.module
+++ b/userpoints_retroactive/userpoints_retroactive.module
@@ -2,30 +2,40 @@
 
 // Based on a script by Miguel Figueiredo <elmig@debianpt.org>, 2006
 
-define(USERPOINTS_PERM_RETROACTIVE, 'retroactive userpoints');
+//define(USERPOINTS_PERM_RETROACTIVE, 'retroactive userpoints');
 
-function userpoints_retroactive_help($path, $arg) {
+/*function userpoints_retroactive_help($path, $arg) {
   switch ($path) {
     case 'admin/settings/userpoints/retroactive':
       $output = t('Award users !points for nodes, comments and votes they created in the past.', userpoints_translation());
       break;
   }
   return $output;
-}
+}*/
 
 function userpoints_retroactive_menu() {
   $items['admin/settings/userpoints/retroactive'] = array(
     'page callback'    => 'userpoints_retroactive_page',
     'title'            => t('Retroactive'),
-    'access arguments' => array(USERPOINTS_PERM_RETROACTIVE),
+    'access arguments' => array('access content'),
     'type'             => MENU_NORMAL_ITEM
   );
 
   return $items;
 }
 
-function userpoints_retroactive_perm() {
+/*function userpoints_retroactive_perm() {
   return array (USERPOINTS_PERM_RETROACTIVE);
+}*/
+
+// Raj: To add role permission in Drupal 7
+function userpoints_retroactive_permission()
+{
+    return array(
+        'Userpoints Retroactive' => array(
+            'title' => t('Award users points for nodes, revisions, comments and votes they created in the past'),
+        ),
+    );
 }
 
 function userpoints_retroactive_page() {
@@ -43,6 +53,15 @@ function userpoints_retroactive() {
     '#maxlength'     => 3,
   );
 
+  // Raj:
+  $form['node_date'] = array(
+  		'#type'          => 'textfield',
+  		'#title'         => t('Retain date for Userpoints Retroactive', userpoints_translation()),
+  		'#default_value' => t('NO'),
+  		'#size'          => 3,
+  		'#maxlength'     => 3,
+  );
+  
   $form['submit'] = array(
     '#type'  => 'submit',
     '#value' => t('Submit'),
@@ -53,16 +72,26 @@ function userpoints_retroactive() {
 
 function userpoints_retroactive_submit($form, &$form_state) {
   if ($form_state['values']['confirm'] == t('YES')) {
-    userpoints_retroactive_do();
+  	// Raj:
+  	$retainDate = $form_state['values']['node_date'];
+    userpoints_retroactive_do($retainDate);
     drupal_set_message(t('Retroactive !points awarded.', userpoints_translation()));
     drupal_goto('admin/settings/userpoints');
   }
 }
 
-function userpoints_retroactive_do() {
-  set_time_limit(240);
+function userpoints_retroactive_do($retainDate)
+{
+//   set_time_limit(240);
   timer_start('up_retro');
-  $num = userpoints_retroactive_nodes();
+  
+  // Raj: To empty userpoints table - https://www.drupal.org/node/1002160
+  db_query('TRUNCATE {userpoints}');
+  db_query('TRUNCATE {userpoints_txn}');
+  db_query('TRUNCATE {userpoints_total}');
+  
+  $num = userpoints_retroactive_nodes($retainDate);
+  $num += userpoints_retroactive_revisions();
   $num += userpoints_retroactive_comments();
   if (module_exists('votingapi')) {
     $num += userpoints_retroactive_votingapi();
@@ -72,32 +101,127 @@ function userpoints_retroactive_do() {
   watchdog('userpoints', t('Userpoints retroactive processed %count items in %ms milliseconds'), array('%ms' => $ms, '%count' => $num));
 }
 
-function userpoints_retroactive_nodes() {
-  $num = 0;
-  $result = db_query("SELECT uid, type, COUNT(uid) AS val FROM {node} WHERE status = 1 AND uid > 0 GROUP BY uid, type");
-  while($node = db_fetch_object($result)) {
-    $weight = variable_get(USERPOINTS_POST . $node->type, 0);
-    $tid = variable_get(USERPOINTS_POST . $node->type . '_category', 0);
-    $params = array(
-      'uid'       => $node->uid,
-      'tid'       => $tid,
-      'points'    => ($node->val * $weight),
-      'moderate'  => FALSE,
-      'display'   => FALSE,
-      'operation' => t('Retroactive node'),
-    );
-    userpoints_userpointsapi($params);
-    $num++;
-  }
+function truncate_string($string,$length=100,$append="&hellip;")
+{
+	// http://stackoverflow.com/a/3161830/260665
+	$string = trim($string);
+
+	if(strlen($string) > $length) {
+		$string = wordwrap($string, $length);
+		$string = explode("\n", $string, 2);
+		$string = $string[0] . $append;
+	}
+
+	return $string;
+}
+
+function userpoints_retroactive_nodes($retainDate)
+{
+    $num = 0;
+    // Raj: Setting default weight as 50 for new nodes
+    $weight = 50;
+    try
+    {
+        if ($retainDate == t('YES'))
+        {
+            // Raj:
+            //         $result = db_query("SELECT uid, type, COUNT(uid) AS val FROM {node} WHERE status = 1 AND uid > 0 GROUP BY uid, type");
+            $result = entity_load('node');
+            foreach($result as $node)
+            {
+            $nodeOperation = $node->title;
+            $nodeOperation = truncate_string($nodeOperation, 30);
+            $params = array(
+                    'uid'       => $node->uid,
+                    'points'    => $weight,
+                    'moderate'  => FALSE,
+                    'display'   => FALSE,
+                    'operation' => $nodeOperation,
+                    'time_stamp'=> $node->created,
+            		'changed'	=> $node->created,
+            );
+            userpoints_userpointsapi($params, YES);
+            $num++;
+            }
+            }
+            else
+            {
+            $result = db_query("SELECT uid, type, COUNT(uid) AS val FROM {node} WHERE status = 1 AND uid > 0 GROUP BY uid, type");
+        foreach($result as $node) {
+            //$weight = variable_get(USERPOINTS_POST . $node->type, 0);
+                $tid = variable_get(USERPOINTS_POST . $node->type . '_category', 0);
+                $params = array(
+                    'uid'       => $node->uid,
+                            'tid'       => $tid,
+                            'points'    => ($node->val * $weight),
+                                    'moderate'  => FALSE,
+                                    'display'   => FALSE,
+                                    'operation' => t('Retroactive node'),
+                                    );
+                userpoints_userpointsapi($params, YES);
+                    $num++;
+            }
+            }
+        } 
+    catch (Exception $e) 
+    {
+        $exc = $e;
+        
+    }
+
   return $num;
 }
 
+
+// node_revision
+function userpoints_retroactive_revisions()
+{
+	$num = 0;
+	$weight = 10; // Weight for each revision
+	
+	try {
+		$result = entity_load('node');	// Loads all nodes
+		foreach($result as $node)
+		{
+			$revisions = node_revision_list($node);
+			foreach ($revisions as $revision)
+			{
+				$revisionOperation = $revision->title;
+				$revisionOperation = truncate_string($revisionOperation, 20);
+				
+				$operation = $revisionOperation . ' - Revision';
+				$params = array(
+						'uid'       => $revision->uid,
+						'points'    => $weight,
+						'moderate'  => FALSE,
+						'display'   => FALSE,
+						'operation' => $operation,
+						'time_stamp'=> $revision->timestamp,
+            			'changed'	=> $revision->timestamp,
+				);
+				userpoints_userpointsapi($params, YES);
+				$num++;
+			}
+		}
+	}
+	catch (Exception $e)
+	{
+		$exc = $e;
+	}
+
+	return $num;
+}
+
 function userpoints_retroactive_comments() {
   $num = 0;
   $weight = variable_get(USERPOINTS_POST_COMMENT, 0);
   $tid = variable_get(USERPOINTS_POST_COMMENT . '_category', 0);
-  $result = db_query('SELECT uid, COUNT(uid) AS val FROM {comments} WHERE status = 0 AND uid > 0 GROUP BY uid');
-  while($comment = db_fetch_object($result)) {
+  $result = db_query('SELECT uid, COUNT(uid) AS val FROM {comment} WHERE status = 0 AND uid > 0 GROUP BY uid');
+  foreach($result as $comment) {
     $params = array(
       'uid'       => $comment->uid,
       'tid'       => $tid,
@@ -117,7 +241,7 @@ function userpoints_retroactive_votingapi() {
   $weight = variable_get('userpoints_votingapi_vote', 0);
   $tid = variable_get('userpoints_votingapi_tid', 0);
   $result = db_query('SELECT uid, COUNT(*) AS val from {votingapi_vote} GROUP BY uid');
-  while($u = db_fetch_object($result)) {
+  foreach($result as $u) {
     $params = array(
       'uid'       => $u->uid,
       'tid'       => $tid,
@@ -131,3 +255,4 @@ function userpoints_retroactive_votingapi() {
   }
   return $num;
 }
+
-- 
2.3.2 (Apple Git-55)

