Sidebar Friends Module with messaging and activity

Register an Account
Pligg Chat Room
Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-28-2007, 04:58 PM
AnAlienHolakres3's Avatar
Pligg Donor/Coder
 
Join Date: Jul 2007
Location: Prague
Posts: 116
Send a message via ICQ to AnAlienHolakres3
Hi folks, this module has been discsussed overe here, but nobody has done something functional (something like "it would be great if somebody...you know it). So I decided to spend many hours with my poor programming knowledge to develop it. As a fact: This is not official module at all, this can be considered as tutorial.

The result looks like this:


Functionality:
Show userīs friends
If is user not logged in or has no friends, this module is empty, but it is shown like other sidebar modules

Show userīs friendīs status:
- online
- idle
- offline

This status is represented by icons (but it can be changed anyway).
It is of course possible to change language of columns displayed (details later).

Send message to userīs friends
- It works smoothly, however in IE 6 when user send the message and click "close" is not possible to scroll down. This is not bug of this addon nor messaging module, i beleive it is bug in Internet Explorer. The solution is pretty simple - just press f5 to refresh or - the better way- download far better Opera or Firefox.

Requirements:
- Pligg (i have 9.7 but it should work on older versions)
- Php Designer or Notepad++ or similar tool,windows Notepad only for masochists

Needed downloads:
Alternative icons:
friends_online.ico
friends_idle.ico
friends_offline.ico
user_message.png

Place it into your ../templates/yget/images/ folder

Part 2 - What to do:
The first question to be answered is how we recognize if user has a friend?
This has been already done by friends addon. I hope you have it. So the second and more important thing is how we recognize if user is online,idle or offline?

After testing Iīve done it in two ways:
1)analysing friendīs inactivity time
2)checking if user is logged in and logged out


So there is first task for you. We need add 2 columns into userīs database. So log in to your phpadmin (or similar tool) and run sql query:
Code:
ALTER TABLE  your_domain_users
ADD COLUMN user_lastactivity timestamp;
ALTER TABLE  your_domain_users
ADD COLUMN user_logged_in boolean;
You are to get information message that operation has been successfully accomplished or something similar.

2)Open friend.php which is located in ../libs/ folder. We need to modify sql query for our new 2 columns. So modify these two functions:
function get_friend_list():
Code:
    // returns an array of people you've added as a friend
global $db, $current_user;
//echo "SELECT " . table_users . ".user_login FROM " . table_friends . " INNER JOIN " . table_users . " ON friends.friend_to = " . table_users . ".user_id WHERE (((friends.friend_from)=$current_user->user_id));";
$friends = $db->get_results("SELECT " . table_users . ".user_login, " . table_users . ".user_avatar_source, " . 
table_users . ".user_email, " . table_users . ".user_id , " .table_users . ".user_lastactivity , " . 
table_users . ".user_logged_in FROM " . table_friends . " INNER JOIN " . 
table_users . " ON " . table_friends . ".friend_to = " . 
table_users . ".user_id WHERE (((" . table_friends . ".friend_from)= " . $current_user->user_id . "));");
        return $friends;

    }
function get_friend_list_2():
Code:
function get_friend_list_2()
    {
        // returns an array of people who have added you as a friend
        global $db, $current_user;
        $friends = $db->get_results("SELECT " . table_users . ".user_login, " . table_users .
        ".user_avatar_source, " . table_users . ".user_email, " . table_users . ".user_id , " .
        table_users . ".user_lastactivity , " . table_users . ".user_logged_in FROM " . 
        table_friends . " INNER JOIN " . table_users . " ON " . table_friends . ".friend_to = " . 
        table_users . ".user_id WHERE (((" . table_friends . ".friend_from)= " . $current_user->user_id . "));");
        return $friends;
    }
Save the file. We wont longer need it.

3) Open sidebar.tpl in your ../templates/yget/ folder. You may have another template but this could work one way or another.

Now you can choose WHERE new sidebar module should be placed. Personally i choose before tags, so i placed this code:

Code:
{assign var=sidebar_module value="sidebar_friends"}{include file=$the_template_sidebar_modules."/wrapper.tpl"}
before code for sidebar tags (it begins with {if $Enable_tags..})).

You can customize it - if you want to be this module visible only if the user is logged in, the code should look like this:
Code:
{if $user_authenticated eq true}
{assign var=sidebar_module value="sidebar_friends"}{include file=$the_template_sidebar_modules."/wrapper.tpl"}
{/if}
Personally i recommend to use this feature always (without this condition) beceause not registered users can see functionality, but it is your decision anyway. Save file, no longer needed.

4) Create a new file, name it sidebar_friends.tpl and place it into your ../templates/yget/sidebar_modules folder

Add to this file these 3 lines:
Code:
{* ----- show the sidebar friends box ----- *}
    {php}include('sidebar_friends.php');{/php}
{* ---------------------------------------- *}
Save.You can close it.

Last edited by AnAlienHolakres3; 11-22-2008 at 10:32 AM.
Reply With Quote
  #2 (permalink)  
Old 08-28-2007, 05:22 PM
AnAlienHolakres3's Avatar
Pligg Donor/Coder
 
Join Date: Jul 2007
Location: Prague
Posts: 116
Send a message via ICQ to AnAlienHolakres3
5) Create next new file, name it sidebar_friends.php and place it into your ../ folder (where are other php files,it is your main folder).

This file is quite long, so copy all what you see and paste it into your new file.
PHP Code:
<?php
//@created by AnAlienHolakres3 for Pligg community

include_once('Smarty.class.php');
$main_smarty = new Smarty;


include_once(
'config.php');
include_once(
mnminclude.'html1.php');
include_once(
mnminclude.'link.php');
include_once(
mnminclude.'tags.php');
include_once(
mnminclude.'search.php');
include_once(
mnminclude.'smartyvariables.php');
include_once(
mnminclude.'friend.php');
include_once(
mnminclude.'user.php');

// -------------------------------------------------------------------------------------

global $the_template$main_smarty$db;

//============================================================================
// Function isFriendOnline($friendActivity,$friendLogged)  decides if is user online,idle or offline and 
// returns its numbers. 
//@param friendActivity=timestamp of last friend activity
//@param friendLogged=false or true if is user logged in
// @return 0 if user is OFFLINE => last activity > 20 minutes
// @return 1 if user is ONLINE => last activity < 10 minutes
// @return 2 if user is IDLE  =>    10minutes < last activity < 20 minutes

// you can set,ONLINE and IDLE  TIME parameters in MINUTES:
//============================================================================
function isFriendOnline($friendActivity,$friendLogged) {
    
    
$online =  10;
    
$idle 20;    
    
$friendLastActivity strtotime($friendActivity);

$onlineLimit time() - $online*60;
$idleLimit =  time() - $idle*60;
  
  
    if ((
$idleLimit $friendLastActivity) && $friendLogged){    
        if((
$onlineLimit $friendLastActivity) && $friendLogged){
                        return 
1;
        }else          return 
2;
    }else           return 
0;

}
//============================================================================
// Function do_viewfriendsside() is modified function of do_viewfriends in user.php, it 
// does nearly the same thing but modified for using in sidebar and also update database
// for current user activity
//============================================================================
function do_viewfriendsside(){
    global 
$db$user$the_template,$current_user,$main_smarty;
    
    
$friend = new Friend;
    
$friends $friend->get_friend_list();    
    
    
//refresh user activity
    
$sql "UPDATE " table_users " SET user_lastactivity = FROM_UNIXTIME(".time().") WHERE user_id=" $current_user->user_id ."";
        
$db->query($sql);

    if (
$friends){
      echo 
"<table border=\"0\" style=\"margin-bottom:25px\"><th style=\"text-align=center\">" .$main_smarty->get_config_vars('PLIGG_Visual_Friends_Username')."</th><th align=\"center\">".
      
$main_smarty->get_config_vars('PLIGG_Visual_Friends_Status')."</th><th>" $main_smarty->get_config_vars('PLIGG_Visual_Friends_Message')."</th>";
        foreach(
$friends as $myfriend) {
            
$friend_avatar get_avatar('small'$myfriend->user_avatar_source$myfriend->user_login$myfriend->user_email);
            
$friendActivity=$myfriend->user_lastactivity;         //time of last activity
            
$friendLogged=$myfriend->user_logged_in;               //user logged in =1, logged out = 0
            
$friendName $myfriend->user_login;

             
//if is username too long
                 
if (strlen($friendName)>16) {
                  
$friendName substr($friendName,0,14)."..";
                 };
            
          echo 
'<tr>';
            echo 
'<td><img src="'.$friend_avatar.'" align="absmiddle"/> <a href="'.getmyurl('user2'$myfriend->user_login'profile'). '">' $friendName.'</a></td>';
            
            
            if (
isFriendOnline($friendActivity,$friendLogged)==0){
              echo 
'<td><img src='.my_pligg_base.'/templates/'.$the_template.'/images/friends_offline.ico alt="offline" style="margin-left:4px"></td>';}    
              
             elseif (
isFriendOnline($friendActivity,$friendLogged)==1) {
              echo 
'<td><img src='.my_pligg_base.'/templates/'.$the_template.'/images/friends_online.ico alt="online" style="margin-left:4px"></td>';}
                    
            elseif(
isFriendOnline($friendActivity,$friendLogged)==2) { 
                 echo 
'<td><img src='.my_pligg_base.'/templates/'.$the_template.'/images/friends_idle.ico alt="offline" style="margin-left:4px"></td>';}
            
            echo 
'<td><a href="#view_message" rel="view_message~!~view=small_msg_compose~!~login=' .$myfriend->user_login '" class="lbOn">
            <img src='
.my_pligg_base.'/templates/'.$the_template.'/images/user_message.png border=0 style="margin-left:12px"></a></td>';                    
            echo 
'</tr>';
        }
        echo 
'</table>';
    }
    else {
        return 
false;
    }
}

    echo 
"<div class=\"tlb\" style=\"margin-top:-15px\"><span><a onclick=\"new Effect.toggle('sidefriends','blind', {queue: 'end'}); \">  <img src=\"".my_base_url."/templates/" $the_template "/images/expand.png\"  width=\"30\" height=\"29\"/></a></span><a href=\"".my_base_url."/user.php?login=test&view=viewfriends\">".$main_smarty->get_config_vars('PLIGG_Visual_Friends')."</a></div><div id=sidefriends>";


do_viewfriendsside();

echo 
"<li class=\"rmore\"><a href=\"".my_base_url."/user.php?login=test&view=viewfriends\">".$main_smarty->get_config_vars("PLIGG_Visual_What_Is_Pligg_Read_More")."</a></li>";
echo 
"</div>";

?>
.

Now you can save it.

6) open lang.conf in your ../libs/ folder.
Finde the section where sidebar is and add this code:
Code:
/////////////////////////////////////////////////////////////////////////////////
//<SECTION>SIDEBAR</SECTION><ADDED>0.97</ADDED>
PLIGG_Visual_Friends = "your_language"
//<SECTION>SIDEBAR</SECTION><ADDED>0.97</ADDED>
PLIGG_Visual_Friends_Username = "your_language"
//<SECTION>SIDEBAR</SECTION><ADDED>0.97</ADDED>
PLIGG_Visual_Friends_Status = "your_language"
//<SECTION>SIDEBAR</SECTION><ADDED>0.97</ADDED>
PLIGG_Visual_Friends_Message = "your_language"
Save and close.

7) Open login.php file which is located in your base folder ( ../ )
We need add to this file 2 things:
1) when user is logged in, in our new column user_logged_in should be set value to true.
2) when user is logged out,in this column should be false.

So first task is modify login function:
Code:
// if user tries to log in
if(isset($_POST["processlogin"]) || isset($_GET["processlogin"])){
	if($_POST["processlogin"] == 1) { // users logs in with username and password
		$username = trim($_POST['username']);
		$password = trim($_POST['password']);
		$persistent = $_POST['persistent'];
		if($current_user->Authenticate($username, $password, $persistent) == false) {
			$errorMsg=$main_smarty->get_config_vars('PLIGG_Visual_Login_Error');
		} else {
			if(strlen($_REQUEST['return']) > 1) {
				$return = $_REQUEST['return'];
			} else {
				$return =  my_pligg_base.'/';
			}
			//friends activity
			$db->query('UPDATE `' . table_users . '` SET `user_logged_in` =true WHERE `user_login` = "'.$username.'"');
			$db->query('UPDATE `' . table_users . '` SET `user_lastactivity` = FROM_UNIXTIME('.time().') WHERE `user_login` = "'.$username.'"');
			//friends activity
			
			define('logindetails', $username . ";" . $password . ";" . $return);
			
			check_actions('login_success_pre_redirect');

			if(strpos($_SERVER['SERVER_SOFTWARE'], "IIS") && strpos(php_sapi_name(), "cgi") >= 0){
				echo '<SCRIPT LANGUAGE="JavaScript">window.location="' . $return . '";</script>';
				echo $main_smarty->get_config_vars('PLIGG_Visual_IIS_Logged_In') . '<a href = "'.$return.'">' . $main_smarty->get_config_vars('PLIGG_Visual_IIS_Continue') . '</a>';
			} else {
			header('Location: '.$return);
			}
			die;
		}
	}
Then modify logout function:
Code:
// if user requests to logout
if(isset($_GET["op"])){
	if($_GET["op"] === 'logout') {
	     //friends activity
		global $db, $user;
		$db->query('UPDATE `' . table_users . '` SET `user_logged_in` =false WHERE `user_id` = "'.$current_user->user_id.'"');
		//friends activity
		
		$current_user->Logout($_REQUEST['return']);
	}
}
Thatīs all for this file, you can save it.

8. Now, open file leigthbox_screen.css placed in your ../modules/lightbox/templates/css folder. This customization is needed for displaying message window correctly,but it contains IE hack(not valid but who cares anyway).

Code:
.leightbox {
	color: #333;
	display: none;
	position: fixed;
	_position: absolute;
	top: 5%;
	left: 25%;
	width: 50%;
	height: 350px;
	padding: 1em;
	border: .5em solid #B8B8B8;
	background-color: white;
	text-align: left;
	z-index:1001;
	overflow: auto;	
}

Save. Thats all. Now you can relogin to see effect.
I know itīs temporary dirty solution but it works. If there is some problem with it feel free to let me know.
P.S. If ANYBODY has a clue how to solve problem with searching tags containing special characters (gives no result) please let me know.
Reply With Quote
  #3 (permalink)  
Old 08-28-2007, 05:38 PM
chuckroast's Avatar
Pligg Developer/Coder/Designer
Pligg Version: SVN
Pligg Template: Galleria
 
Join Date: Jun 2006
Posts: 3,828
Let me be the first to say great job AnAlienHolakres3


Quote:
P.S. If ANYBODY has a clue how to solve problem with searching tags containing special characters (gives no result) please let me know.
I'm not sure if this is what your looking for, but it may help.


Get the full Pligg Module Pack today.

Anyone want the domain MyHubb.com ? Bid Now!




Reply With Quote
  #4 (permalink)  
Old 08-29-2007, 08:34 AM
beatniak's Avatar
Pligg Donor
 
Join Date: Apr 2006
Location: NL - 52.100863;5.108356
Posts: 197
Great job AnAlienHolakres3! Looks great, was very easy to implement in my own template.

Some of my experiences with the code:
  1. When using pligg 9.6, you should overwrite the lightbox module with the lightbox folder from 9.7 in your 9.6 modules folder.
  2. To avoid errors, in sidebar_friends.php the read more and header link should be:
    Code:
    <a href=\"".my_pligg_base."/user.php?view=viewfriends\">
    
  3. Online/offline/idle status isn't working (it shows everyone offline).
  4. I changed some of the code in sidebar_friends.php (that couldn't have broken the offline/online status) to have a better friends online box:
    Code:
    echo '<tr>';
          echo '<td width="50%"><img src="'.$friend_avatar.'" align="absmiddle"/> <a href="'.getmyurl('user2', $myfriend->user_login, 'profile'). '">' . $friendName.'</a></td>';
     
    if (isFriendOnline($friendActivity,$friendLogged)==0){
      echo '<td width="25%"><img src="'.my_pligg_base.'/templates/'.$the_template.'/images/friend_offline.png" alt="offline" style="margin-left:4px"></td>';}    
      
     elseif (isFriendOnline($friendActivity,$friendLogged)==1) {
      echo '<td width="25%"><img src="'.my_pligg_base.'/templates/'.$the_template.'/images/friend_online.png" alt="online" style="margin-left:4px"></td>';}
            
    elseif(isFriendOnline($friendActivity,$friendLogged)==2) { 
         echo '<td><img src="'.my_pligg_base.'/templates/'.$the_template.'/images/friend_idle.png" alt="idle" style="margin-left:4px"></td>';}
    
Other than these few bugs, great module! Thanks for the work.

Last edited by beatniak; 08-29-2007 at 08:39 AM.
Reply With Quote
  #5 (permalink)  
Old 09-05-2007, 12:47 AM
Constant Pligger
 
Join Date: Jun 2006
Posts: 108
cool work AnAlienHolakres3

it would be cool if this mod will be included in the next Pligg version.

btw, should we still expect an upgrade despite the for sale sign on pligg?

noyP
Reply With Quote
  #6 (permalink)  
Old 09-05-2007, 07:16 AM
AnAlienHolakres3's Avatar
Pligg Donor/Coder
 
Join Date: Jul 2007
Location: Prague
Posts: 116
Send a message via ICQ to AnAlienHolakres3
Unfortunately this (just like all my add-ons) is not official,structure of official module is a little different,but I think it shouldn be so great problem to do it into official form. You are welcome to do it)). On the other hand - integration into new version of Pligg only with this tut shouldnt be a problem as well
Reply With Quote
  #7 (permalink)  
Old 10-15-2007, 08:22 PM
New Pligger
 
Join Date: Oct 2007
Posts: 4
Super! But can you attach all files that you change. Thank you.
Reply With Quote
  #8 (permalink)  
Old 11-20-2007, 08:57 AM
computerbar's Avatar
Pligg Donor
Pligg Version: 9.9.5
Pligg Template: yget,lemontwi
 
Join Date: Nov 2007
Location: UK
Posts: 67
Thanks AnAlien
Every thing was spot on.

Last edited by computerbar; 12-27-2007 at 05:47 PM.
Reply With Quote
  #9 (permalink)  
Old 01-21-2008, 07:25 AM
Banned
Pligg Version: 9.8.
Pligg Template: Custom
 
Join Date: Feb 2007
Location: Canada
Posts: 796
Im getting a parse error when trying to install on 9.9.

Quote:
Parse error: syntax error, unexpected T_GLOBAL, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/newsdots/public_html/libs/friend.php on line 45
Geoserv
Reply With Quote
  #10 (permalink)  
Old 01-23-2008, 12:17 AM
Casual Pligger
 
Join Date: Oct 2007
Posts: 80
I hope next release of pligg will have this cool thing built in.
Reply With Quote
Reply

Thread Tools
Display Modes




Pligg Modules and Pligg Templates from Pligg Pro Find support on the Pligg CMS Forum - 24 hours a day! Make a donation to support Pligg CMS development