UDT for a time account

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Locked
SimonSchaufi

UDT for a time account

Post by SimonSchaufi »

Hi!
As a developer i thought about a small UDT to count the time you are staying online in the admin panel.
This is my idea:
a timestamp and the minutes you spend already in a textfile (i dont know how to access database via UDT)

Example:
Login: current timestamp | 0 (min)
Logout: diverence between login timestamp and current timestamp | 20 (min)

Login: old logout timestamp | 20
Logout: diverence between login timestamp and current timestamp | 30

Can somebody help me or give me a hint please?

if it is better to count the milliseconds (like the timestamp) for the minutes, no problem.

";
echo "Min: ".$datum['minutes']."";

$data = $time_now . "|" . $minutes . "\n";

$fp = fopen($zaehler_anmelden, "w");
flock($fp,2);
fputs($fp,$data);
flock($fp,3);
fclose($fp);
?>
Last edited by SimonSchaufi on Mon Sep 17, 2007 1:38 pm, edited 1 time in total.
cyberman

Re: future UDT for counting time online

Post by cyberman »

SimonSchaufi wrote: to count the time you are staying online in the admin panel.
Please look at admins log. You will found there login and logout time - you have only to substract.
dont know how to access database via UDT
There are some threads about inside forum :).

Normally AdoDb does the connection to database. You have only to request the database.
SimonSchaufi

Re: future UDT for counting time online

Post by SimonSchaufi »

solved!

solution comes later...
SimonSchaufi

Re: UDT for a time account

Post by SimonSchaufi »

Install it first!

Code: Select all

/*
 * Use in a page or template like this: {time_account show=true}
 * Install with {time_account install=true} BE CAREFULL!!! Execute only once!
 * Uninstall with {time_account uninstall=true}
 * Written by Simon Schaufelberger 2007
 */

global $gCms;
$db = &$gCms->db;

$userid = get_userid(false);

/*
 * Calculates the seconds, minutes and hours from a given timestamp
 * returns an array of it
 */
function mod_time($timestamp){
	$tmp = array();
	$tmp['seconds'] = $timestamp % 60;
	$tmp['minutes'] = ($timestamp / 60) % 60;
	$tmp['hours'] = ($timestamp / pow(60, 2)) % 60;
	return $tmp;
}

/* Display the time in a page or template if parameter show="true" */

if($params['install'] == true){
	$sql_install = 'CREATE TABLE '.cms_db_prefix().'time_account ('
	. ' user_id int(11) NOT NULL,'
	. ' time int(11) NOT NULL,'
	. ' PRIMARY KEY (user_id) )';

	$dbresult = $db->Execute($sql_install);

	if( !$dbresult ){
		echo 'DB error: '. $db->ErrorMsg();
		exit;
	}
	echo "<p>UDT installed!</p>";
}
elseif($params['uninstall'] == true){
	$sql_uninstall = 'DROP TABLE '.cms_db_prefix().'time_account';

	$dbresult = $db->Execute($sql_uninstall);

	if( !$dbresult ){
		echo 'DB error: '. $db->ErrorMsg();
		exit;
	}
	echo "<p>UDT uninstalled! Please remove it from the page or template!</p>";
}

if($userid && $params['show']== true){
	/* Select the last login time from adminlog */
	$sql = 'SELECT MAX(timestamp) AS login_time '
	. ' FROM '.cms_db_prefix().'adminlog '
	. ' WHERE (action = "User Login") '
	. ' AND user_id = ?';

	$dbresult = $db->Execute($sql, array($userid));

	if(!$dbresult){
		echo 'DB error: '. $db->ErrorMsg();
		exit;
	}

	if($last_login = $dbresult->FetchRow()){
		$logout_time = time();
		$login_time = $last_login['login_time'];
		$time_div = $logout_time - $login_time;

		$sql = 'SELECT time '
		. ' FROM '.cms_db_prefix().'time_account '
		. ' WHERE ( user_id = ? ) ';

		$dbresult = $db->Execute($sql, array($userid));

		if( !$dbresult ){
			echo 'DB error: '. $db->ErrorMsg();
			exit;
		}
		if($dbresult && $new_time = $dbresult->FetchRow()){
			$date = mod_time($new_time['time'] + $time_div);

			echo "<p>My time account: ";
			echo $date['hours'] . ":";
			echo $date['minutes'] . ":";
			echo $date['seconds'] . " (h:min:sec)</p>";
		}
	}
}
elseif($userid){
	/* Select the last login time from adminlog */
	$sql = 'SELECT MAX(timestamp) AS login_time '
	. ' FROM '.cms_db_prefix().'adminlog '
	. ' WHERE (action = "User Login") '
	. ' AND user_id = ?';

	$dbresult = $db->Execute($sql, array($userid));

	if(!$dbresult){
		echo 'DB error: '. $db->ErrorMsg();
		exit;
	}

	//Is the user already in the time_account table?
	$sql = 'SELECT time '
	. ' FROM '.cms_db_prefix().'time_account '
	. ' WHERE user_id = ?';

	$dbresult2 = $db->Execute($sql, array($userid));

	if(!$dbresult2){
		echo 'DB error: '. $db->ErrorMsg();
		exit;
	}

	/* Add a new user to the table */
	if($dbresult2->RecordCount() == 0 && $last_login = $dbresult->FetchRow()){
		$logout_time = time();
		$login_time = $last_login['login_time'];
		$time_div = $logout_time - $login_time;

		$sql = 'INSERT INTO '.cms_db_prefix().'time_account (user_id, time) VALUES (?, ?)';

		$dbresult = $db->Execute($sql, array($userid, $time_div));

		if( !$dbresult ){
			echo 'DB error: '. $db->ErrorMsg();
			exit;
		}
	} /* Update his existing time account */
	elseif($last_login = $dbresult->FetchRow()){
		$logout_time = time();
		$login_time = $last_login['login_time'];
		$time_div = $logout_time - $login_time;

		$sql = 'UPDATE '.cms_db_prefix().'time_account SET time = time + ? WHERE user_id = ?';

		$dbresult = $db->Execute($sql, array($time_div, $userid));

		if( !$dbresult ){
			echo 'DB error: '. $db->ErrorMsg();
			exit;
		}
	}
}
else{
	echo "<p>Not logged in. Please login to see your time account!</p>";
}
Locked

Return to “Modules/Add-Ons”