Index: similargroups.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/similargroups/similargroups.admin.inc,v
retrieving revision 1.2
diff -u -r1.2 similargroups.admin.inc
--- similargroups.admin.inc	4 Jul 2009 02:58:49 -0000	1.2
+++ similargroups.admin.inc	10 Jul 2009 12:41:07 -0000
@@ -7,7 +7,7 @@
 
 function similargroups_settings_form() {
   $form = array();
-  
+
   $form['algorithm'] = array(
     '#title' => t('Choose group similarity algorithm'),
     '#type' => 'radios',
@@ -18,7 +18,7 @@
     ),
     '#description' => t('Different algorithms are useful in different situations. Please leave it as the default classical algorithm unless you have small number of groups and users.')
   );
-  
+
   $form['display_num'] = array(
     '#title' => t('Number of groups/users to display in the block.'),
     '#type' => 'select',
@@ -30,23 +30,41 @@
       10 => '10',
       15 => '15',
     ),
-    '#description' => t('Please specify how many items to show in the similar groups/users list.') 
+    '#description' => t('Please specify how many items to show in the similar groups/users list.')
+  );
+
+  $form['method'] = array(
+  	'#title' 	=> t('Which methods do you want to use?'),
+  	'#type'		=> 'checkboxes',
+  	'#default_value'=>variable_get('similargroups_method',''),
+  	'#options' => array(
+  		'g' => t('Group to Group'),
+  		'u' => t('User to Group'),
+  	),
+  	'#description' => t('Choosing "Group to Group" method will provid a block when you visit a group where similar groups to the given group are displayed. ' .
+  			'Choosing the "User to Group" option will provid the user with a pager where he can see Groups that he might be interested in, based on the users he is in the same group with.'),
   );
-  
+
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Save configuration'),
   );
-  
+
   return $form;
 }
 
 function similargroups_settings_form_submit($form, &$form_state) {
+  $cron_freq = $form_state['values']['cron_freq'];
   $display_num = $form_state['values']['display_num'];
   $algorithm = $form_state['values']['algorithm'];
-  
+  $method = $form_state['values']['method'];
+
   variable_set('similargroups_display_num', $display_num);
   variable_set('similargroups_algorithm', $algorithm);
-  
+  variable_set('similargroups_method', $method);
+
   drupal_set_message(t("The configuration options have been saved."));
+  if ($cron_freq == 'immediately') {
+    drupal_set_message(t("You have selected to build similarity index immediately. Please run cron mannually, or wait for the next cron cycle."));
+  }
 }
\ No newline at end of file
Index: similargroups.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/similargroups/similargroups.module,v
retrieving revision 1.2
diff -u -r1.2 similargroups.module
--- similargroups.module	4 Jul 2009 02:58:49 -0000	1.2
+++ similargroups.module	10 Jul 2009 12:44:14 -0000
@@ -1,4 +1,6 @@
 <?php
+
+
 // $Id: similargroups.module,v 1.2 2009/07/04 02:58:49 danithaca Exp $
 
 /**
@@ -9,114 +11,247 @@
  * Implementation of hook_help().
  */
 function similargroups_help($path, $args) {
-  $output = '';
-  switch ($path) {
-    case "admin/help#similargroups":
-      $output = '<p>'.  t("This module generates related OG groups based on shared membership, and generates similar users based on shared membership to groups.") .'</p>';
-      break;
-  }
-  return $output;
+	$output = '';
+	switch ($path) {
+		case "admin/help#similargroups" :
+			$output = '<p>' . t("This module generates related OG groups based on shared membership, and generates similar users based on shared membership to groups.") . '</p>';
+			break;
+	}
+	return $output;
 }
 
-
 /**
  * Implementation of hook_perm().
  */
 function similargroups_perm() {
-  $perm = array(
-    "administer organic groups",
-  );
-  return $perm;
-}
+	$perm = array (
+		"administer organic groups",
 
 
+	);
+	return $perm;
+}
+
 /**
  * Implementation of hook_menu().
  */
 function similargroups_menu() {
-  $items = array(); 
-  $items['admin/og/similargroups'] = array(
-    'title' => 'Similar groups configuration',
-    'description' => 'Configuration and adjusts of similar groups module',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('similargroups_settings_form'),
-    'access arguments' => array('administer organic groups'),
-    'file' => 'similargroups.admin.inc',
-   );
-  return $items;
+	$items = array ();
+	$items['admin/og/similargroups'] = array (
+		'title' => 'Similar groups configuration',
+		'description' => 'Configuration and adjusts of similar groups module',
+		'page callback' => 'drupal_get_form',
+		'page arguments' => array (
+			'similargroups_settings_form'
+		),
+		'access arguments' => array (
+			'administer organic groups'
+		),
+		'file' => 'similargroups.admin.inc',
+
+
+	);
+
+	$items['similargroups'] = array (
+		'title' => 'Similar groups',
+		'description' => 'Displaying similar groups based on the group membership',
+		'page callback' => 'similargroups_page',
+		'access arguments' => array (
+			'access content'
+		),
+
+	);
+	return $items;
 }
 
 /**
  * Implementation of hook_block()
  */
-function similargroups_block($op='list', $delta=0) {
-  if ($op == 'list') {
-    $block[0]['info'] = t('OG similar groups');
-    return $block;
-  }
-  else if ($op == 'view') {
-    $block = array();
-    if ($delta == 0) {
-      if (arg(0) == 'node' && is_numeric(arg(1))) {
-        $node_id = arg(1);
-        
-        // if the node is an OG group that has users, then display similary groups.
-        if (db_result(db_query("SELECT nid FROM {og} WHERE nid=%d", $node_id)) != NULL) {
-          $nodes = similargroups_get_similar_groups($node_id);
-          if (!empty($nodes)) {
-            $block['subject'] = t('Users who joined this group also joined');
-            $block['content'] = theme('item_list', $nodes);
-          }
-        }
-      }
-    }
-    return $block;
-  }
+function similargroups_block($op = 'list', $delta = 0) {
+	if ($op == 'list') {
+		$block[0]['info'] = t('OG similar groups');
+		return $block;
+	} else
+		if ($op == 'view') {
+			$block = array ();
+			if ($delta == 0) {
+				if (arg(0) == 'node' && is_numeric(arg(1))) {
+					$node_id = arg(1);
+
+					// if the node is an OG group that has users, then display similary groups.
+					if (db_result(db_query("SELECT nid FROM {og} WHERE nid=%d", $node_id)) != NULL) {
+						$nodes = similargroups_get_similar_groups($node_id);
+						if (!empty ($nodes)) {
+							$block['subject'] = t('Users who joined this group also joined');
+							$block['content'] = theme('item_list', $nodes);
+						}
+					}
+				}
+			}
+			return $block;
+		}
 }
 
-
 /**
  * In this function, we compute similar groups using Recommender API
  */
-function similargroups_run_recommender() { 
-  $algorithm = variable_get('similargroups_algorithm', 'classical');
-  if ($algorithm == 'classical') {
-    recommender_similarity_classical('similargroups', '{og_uid}', 'nid', 'uid', 'is_active', array(
-      'missing' => 'zero', 'performance' => 'auto'));
-  }
-  else if ($algorithm == 'coocurrence') {
-    recommender_similarity_coocurrences('similargroups', '{og_uid}', 'nid', 'uid', 'is_active');
-  }
+function similargroups_run_recommender() {
+	$algorithm = variable_get('similargroups_algorithm', 'classical');
+	if ($algorithm == 'classical') {
+		recommender_similarity_classical('similargroups', '{og_uid}', 'nid', 'uid', 'is_active', array (
+			'missing' => 'zero',
+			'performance' => 'auto'
+		));
+	} else
+		if ($algorithm == 'coocurrence') {
+			recommender_similarity_coocurrences('similargroups', '{og_uid}', 'nid', 'uid', 'is_active');
+		}
 }
 
+/**
+
+ * In this function, we compute similar groups using Recommender API
+
+ */
+
+function _similargroups_run_recommender() {
+	$algorithm = variable_get('similargroups_algorithm', 'classical');
+	$method = variable_get('similargroups_method', '');
+	if (_similargroups_use_method('g')) {
+		if ($algorithm == 'classical') {
+			recommender_similarity_classical('similargroups', '{og_uid}', 'nid', 'uid', 'is_active', array (
+				'missing' => 'zero',
+				'performance' => 'auto'
+			));
+		} else
+			if ($algorithm == 'coocurrence') {
+				recommender_similarity_coocurrences('similargroups', '{og_uid}', 'nid', 'uid');
+			}
+	}
+
+	if (_similargroups_use_method('u')) {
+		if ($algorithm == 'classical') {
+			recommender_similarity_classical('similargroups_user', '{og_uid}', 'uid', 'nid', 'is_active', array (
+				'missing' => 'zero',
+				'performance' => 'auto'
+			));
+		}
+	}
+}
 
 /**
  * Get a list of groups that are similar with $node_id
  * Similar groups are defined by the fact that they have similar members
  */
 function similargroups_get_similar_groups($node_id) {
-  $display_num = variable_get('similargroups_display_num', 5);
-  
-  function test_func($item) {
-    // will not accept negative correlated groups.
-    if ($item['score']<=0) return FALSE;
-    
-    $node = node_load($item['id']);
-    og_load_group($node);
-    // only accept non-private and in-directory OG groups
-    if ($node->status==1 && $node->og_private==0 && $node->og_directory==1) {
-      return TRUE;
-    } else {
-      return FALSE;
-    }
-  }  
-  $items = recommender_top_similarity('similargroups', $node_id, $display_num, 'test_func');
-  
-  function format_node($item) {
-    $node = node_load($item['id']);
-    return l($node->title, "/node/{$node->nid}");
-  }
-  $items = array_map('format_node', $items);
-  
-  return $items;
+	$display_num = variable_get('similargroups_display_num', 5);
+
+	function test_func($item) {
+		// will not accept negative correlated groups.
+		if ($item['score'] <= 0)
+			return FALSE;
+
+		$node = node_load($item['id']);
+		og_load_group($node);
+		// only accept non-private and in-directory OG groups
+		if ($node->status == 1 && $node->og_private == 0 && $node->og_directory == 1) {
+			return TRUE;
+		} else {
+			return FALSE;
+		}
+	}
+	$items = recommender_top_similarity('similargroups', $node_id, $display_num, 'test_func');
+
+	function format_node($item) {
+		$node = node_load($item['id']);
+		return l($node->title, "/node/{$node->nid}");
+	}
+	$items = array_map('format_node', $items);
+
+	return $items;
+}
+
+// Add-on provided by rapsli
+// =========================
+function similargroups_get_recommended_groups($uid) {
+	global $user;
+	$uid = $user->uid;
+	$app_id = recommender_get_app_id('similargroups_user');
+	$display_num = variable_get('similargroups_display_num', 5);
+
+	$result = db_query("SELECT mouse2_id uid " .
+	"FROM {recommender_similarity} s " .
+	"WHERE app_id=%d " .
+	"AND mouse1_id=%d " .
+	"AND mouse1_id<>mouse2_id " .
+	"AND similarity > 0 " .
+	"", $app_id, $uid);
+
+	$top_groups = array ();
+
+	while ($u = db_fetch_array($result)) {
+		$user_object = user_load($u['uid']);
+		foreach ($user_object->og_groups as $group) {
+			if (!array_key_exists($group['nid'], $user->og_groups)) { //only recommend groups the user is not part of
+				$top_groups[$group['nid']] = $group;
+			}
+		}
+	}
+	return $top_groups;
+
+}
+
+function similargroups_page($uid = 0) {
+	global $user;
+	if ($uid == 0) {
+		$uid = $user->uid;
+	}
+	$out = theme('similargroups_user', similargroups_get_recommended_groups($uid));
+	return $out;
+}
+
+/**
+*/
+function theme_similargroups_user($ar_groups) {
+	$output = '';
+	$header = array ();
+	$data = array ();
+	foreach ($ar_groups as $group) {
+		$node = node_load(array (
+			'nid' => $group['nid']
+		));
+		$content = '<strong>' . l($group['title'], 'node/' . $group['nid']) . '</strong><br/>' . $node->og_description;
+		$data[] = array (
+			$content
+		);
+	}
+	return theme('table', $header, $data);
+}
+
+/**
+                Implementation of hook_theme
+*/
+function similargroups_theme($existing, $type, $theme, $path) {
+	return array (
+		'similargroups_user' => array (
+			'arguments' => array (
+				'ar_groups' => NULL
+			),
+
+		),
+
+	);
+}
+
+/**
+ * Helper method. Figures out the sites uses a given method
+ */
+function _similargroups_use_method($m) {
+	$method = variable_get('similargroups_method', '');
+	foreach ($method as $item) {
+		if ($m === $item) {
+			return TRUE;
+		}
+	}
+	return FALSE;
 }
\ No newline at end of file

