Browse Source

[svn r17648] logic changes - it was show only users connected - (see FS#2459)

Isaac Flores 16 years ago
parent
commit
582b590327
4 changed files with 141 additions and 11 deletions
  1. 7 5
      index.php
  2. 109 0
      main/chat/chat_functions.lib.php
  3. 6 6
      main/chat/chat_hidden.php
  4. 19 0
      main/inc/lib/course.lib.php

+ 7 - 5
index.php

@@ -1,4 +1,4 @@
-<?php // $Id: index.php 16947 2008-11-26 14:04:43Z iflorespaz $
+<?php // $Id: index.php 17648 2009-01-11 23:08:49Z iflorespaz $
  
 /*
 ==============================================================================
@@ -27,7 +27,7 @@
 /**
 *	@package dokeos.main
 * 	@author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Refactoring
-* 	@version $Id: index.php 16947 2008-11-26 14:04:43Z iflorespaz $
+* 	@version $Id: index.php 17648 2009-01-11 23:08:49Z iflorespaz $
 *   @todo check the different @todos in this page and really do them
 * 	@todo check if the news management works as expected
 */
@@ -59,7 +59,7 @@ include_once (api_get_path(LIBRARY_PATH).'events.lib.inc.php');
 include_once (api_get_path(LIBRARY_PATH).'system_announcements.lib.php');
 include_once (api_get_path(LIBRARY_PATH).'groupmanager.lib.php');
 include_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
-
+require_once 'main/chat/chat_functions.lib.php';
 $loginFailed = isset($_GET['loginFailed']) ? true : isset($loginFailed);
 $setting_show_also_closed_courses = (api_get_setting('show_closed_courses')=='true') ? true : false;
 
@@ -75,6 +75,7 @@ $this_section = SECTION_CAMPUS;
  * 			this can be usefull when you are on an open course and you need to log in to edit something and you immediately want to check how anonymous users
  * 			will see it.
  */
+ $my_user_id=api_get_user_id();
 if (!empty($_GET['logout'])) {
 	logout();
 }
@@ -260,7 +261,8 @@ function logout() {
 	}
 	
 	if (!isset($_SESSION['login_as'])) {
-		$s_sql_update_logout_date="UPDATE $tbl_track_login SET logout_date=NOW() WHERE login_id='$i_id_last_connection'";
+		$current_date=date('Y-m-d H:i:s',time());
+		$s_sql_update_logout_date="UPDATE $tbl_track_login SET logout_date='".$current_date."' WHERE login_id='$i_id_last_connection'";
 		api_sql_query($s_sql_update_logout_date);
 	}
 	LoginDelete($uid, $_configuration['statistics_database']); //from inc/lib/online.inc.php - removes the "online" status
@@ -283,7 +285,7 @@ function logout() {
 			}
 		}
 	}
-
+	exit_of_chat($uid);
 	api_session_destroy();
 header("Location: index.php$query_string");
 	exit();

+ 109 - 0
main/chat/chat_functions.lib.php

@@ -0,0 +1,109 @@
+<?php
+/*
+==============================================================================
+	Dokeos - elearning and course management software
+
+	Copyright (c) 2004-2008 Dokeos SPRL
+	Copyright (c) 2003 Ghent University (UGent)
+	Copyright (c) 2001 Universite catholique de Louvain (UCL)
+	Copyright (c) various contributors
+
+	For a full list of contributors, see "credits.txt".
+	The full license can be read in "license.txt".
+
+	This program is free software; you can redistribute it and/or
+	modify it under the terms of the GNU General Public License
+	as published by the Free Software Foundation; either version 2
+	of the License, or (at your option) any later version.
+
+	See the GNU General Public License for more details.
+
+	Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
+	Mail: info@dokeos.com
+==============================================================================
+*/
+include_once '../inc/lib/course.lib.php';
+/**
+ * @author isaac flores paz
+ * @param integer
+ * @return void 
+ */
+function exit_of_chat ($user_id) {
+	$list_course=array();
+ 	$list_course=CourseManager::get_courses_list_by_user_id($user_id);
+ 	foreach($list_course as $courses) {
+ 		$response=user_connected_in_chat($user_id,$courses['db_name']);
+ 		if ($response===true) {
+ 			$tbl_chat_connected = Database::get_course_chat_connected_table($courses['db_name']);;
+ 			$sql='DELETE FROM '.$tbl_chat_connected.' WHERE user_id='.$user_id;
+ 			api_sql_query($sql,__FILE__,__LINE__);
+ 		}
+ 	}
+
+}
+/**
+ * @author isaac flores paz
+ * @param integer the user id
+ * @param string the database name
+ * @return boolean
+ */
+function user_connected_in_chat ($user_id,$database_name) {
+ 	$tbl_chat_connected = Database::get_course_chat_connected_table($database_name);
+ 	$sql='SELECT COUNT(*) AS count FROM '.$tbl_chat_connected .' c WHERE user_id='.$user_id;
+ 	$result = api_sql_query($sql,__FILE__,__LINE__);
+ 	$count  = Database::fetch_array($result,'ASSOC');
+ 	if (1==$count['count']) {
+ 		return true;
+ 	} else {
+ 		return false;
+ 	}
+}
+
+/**
+ * @param void
+ * @return void
+ */
+function disconnect_user_of_chat () {
+	$list_info_user_in_chat = array();
+	$list_info_user_in_chat = users_list_in_chat ();
+
+	$cd_date           = date('Y-m-d',time());
+	$cdate_h           = date('H',time());
+	$cdate_m           = date('i',time());
+	$cdate_s           = date('s',time());
+	$cd_count_time_seconds=$cdate_h*3600 + $cdate_m*60 + $cdate_s;
+	
+	foreach ($list_info_user_in_chat as $list_info_user) {
+			$date_db_date = date('Y-m-d',strtotime($list_info_user['last_connection']));
+			$date_db_h  = date('H',strtotime($list_info_user['last_connection']));
+			$date_db_m  = date('i',strtotime($list_info_user['last_connection']));
+			$date_db_s  = date('s',strtotime($list_info_user['last_connection']));
+			$date_count_time_seconds=$date_db_h*3600 + $date_db_m*60 + $date_db_s;
+			if ($cd_date==$date_db_date) {
+				if (($cd_count_time_seconds - $date_count_time_seconds)>10) {
+					$tbl_chat_connected = Database::get_course_chat_connected_table();
+		 			$sql='DELETE FROM '.$tbl_chat_connected.' WHERE user_id='.$list_info_user['user_id'];
+		 			api_sql_query($sql,__FILE__,__LINE__);
+				}
+			}
+		
+
+	}
+}
+
+/**
+ * @param void
+ * @return array user list in chat
+ */
+function users_list_in_chat () {
+	$list_users_in_chat=array();
+ 	$tbl_chat_connected = Database::get_course_chat_connected_table();
+ 	$sql='SELECT user_id,last_connection FROM '.$tbl_chat_connected.' ;';
+ 	$result=api_sql_query($sql,__FILE__,__LINE__);
+ 	while ($row = Database::fetch_array($result,'ASSOC')) {
+ 		$list_users_in_chat[]=$row;
+ 	}
+ 	return $list_users_in_chat;	
+}	
+
+?>

+ 6 - 6
main/chat/chat_hidden.php

@@ -35,7 +35,7 @@ define('FRAME','hidden');
 $language_file = array ('chat');
 
 require('../inc/global.inc.php');
-
+require_once 'chat_functions.lib.php';
 //$tbl_user=$mainDbName."`.`user";
 //$tbl_chat_connected=$_course['dbNameGlu'].'chat_connected';
 $tbl_user 		= Database::get_main_table(TABLE_MAIN_USER);
@@ -67,10 +67,9 @@ $result=api_sql_query($sql);
 
 //The user_id exists so we must do an UPDATE and not a INSERT
 $current_time=date('Y-m-d H:i:s');
-if (Database::num_rows($result)==0){
+if (Database::num_rows($result)==0) {
 	$query="INSERT INTO $tbl_chat_connected(user_id,last_connection) VALUES('".$_user['user_id']."','".$current_time."')";
-}
-else{
+} else {
 	$query="UPDATE $tbl_chat_connected set last_connection='".$current_time."' WHERE user_id='".$_user['user_id']."'";
 }
 
@@ -80,8 +79,9 @@ $query="SELECT COUNT(user_id) FROM $tbl_chat_connected WHERE last_connection>'".
 $result=api_sql_query($query,__FILE__,__LINE__);
 
 $connected_old=intval($_POST['connected_old']);
-list($connected_new)=Database::fetch_row($result);
-
+list($connected_new) = Database::fetch_row($result);
+/*disconnected user of chat*/
+disconnect_user_of_chat ();
 include("header_frame.inc.php");
 ?>
 

+ 19 - 0
main/inc/lib/course.lib.php

@@ -1868,4 +1868,23 @@ class CourseManager
 				@ api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
 		}
 	}
+
+/**
+ * @author isaac flores paz
+ * @param int
+ * @return array
+ */
+	function get_courses_list_by_user_id($user_id) {
+		$course_list=array();
+		$tbl_course			 = Database::get_main_table(TABLE_MAIN_COURSE);
+		$tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
+		$sql='SELECT c.code,c.db_name FROM '.$tbl_course.' c inner join '.$tbl_course_rel_user.' cru on c.code=cru.course_code  WHERE cru.user_id='.$user_id;
+		$result=api_sql_query($sql,__FILE__,__LINE__);
+		while ($row=Database::fetch_array($result,'ASSOC')) {
+			$course_list[]=$row;			
+		}
+		return $course_list;
+		
+	}
+	
 } //end class CourseManager