<?php

// -----------------------------------------------------------------------
// This file is part of aroundme
// 
// Copyright (C) 2003, 2005 Barnraiser
// http://www.barnraiser.org/
// info@barnraiser.org
// 
// 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, or (at your option) any
// later version.
// 
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with program; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
// -----------------------------------------------------------------------
// tom calthrop - node statistics - tom@barnraiser.org


class Statistic {
	// CLASS FUNCTIONS
	// Statistic
	// getStatistics
	// getNodeStatistics
	// setStatistic
	
	
	
	// File --------------------------------------------------------------------
	// the constuctor
	// tom calthrop - 20th December 2004
	//
	function Statistic($db, $section_id, $date){
		$this->db = $db;
		$this->section_id = $section_id;
		$this->date = $date;
	} // EO constructor
	
	
	
	// getStatistics ---------------------------------------------------------------
	// returns statistics
	// tom calthrop - 20th December 2004
	//
	function getStatistics() {
		
		$query = "
			SELECT s.statistic_type_id, s.total
			FROM " . $this->db->prefix . "_statistic s
			WHERE "
		;
		
		if (isset($this->section_id)) {
		    $query .= "s.section_id=" . $this->section_id . " AND ";
		}
		
		if (isset($this->section_item_id)) {
		    $query .= "s.section_item_id=" . $this->section_item_id . " AND ";
		}
		
		if (isset($this->user_id)) {
		    $query .= "s.user_id=" . $this->user_id . " AND ";
		}
		
		if (isset($this->no_user_id)) {
			$query .= "s.user_id is null AND ";
		}
		
		$query .= "1=1 ";
			
		$query .= "order by s.statistic_type_id";
		//echo $query;
		$result = $this->db->Execute($query);
	
		if (isset($result->fields)) {
	
			$temp = array();
	
			while (!$result->EOF) {
				$temp1 = $result->fields;
				array_push($temp, $temp1);
				$result->MoveNext();
			}
	
			$result->Close();
	
			return $temp;
		}
		else {
			$error_info = "function=getStatistics class=Statistic.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO getStatistics
	
	
	
	// getNodeStatistics ---------------------------------------------------------------
	// returns node statistics
	// tom calthrop - 11th April 2004
	//
	function getNodeStatistics() {
		$query = "
			SELECT count(user_id) as total_users
			FROM " . $this->db->prefix . "_user
			WHERE status_id=" . $this->user_status['active']
		;
		
		$result = $this->db->Execute($query);
	
		if ($result->fields['total_users']) {
			$temp['total_users'] = $result->fields['total_users'];
			$result->Close();
		}
	
		//total groups
		$query = "
			SELECT count(group_id) as total_groups 
			FROM " . $this->db->prefix . "_group"
		;
		
		$result = $this->db->Execute($query);
	
		if (isset($result->fields['total_groups'])) {
			$temp['total_groups'] = $result->fields['total_groups'];
			$result->Close();
		}
	
		//total online now
		$timestamp = $this->db->UserTimeStamp(mktime()-(60*15), "YmdHis"); //last 15 minutes
	
		$query = "
			SELECT count(user_id) as total_online_users
			FROM " . $this->db->prefix . "_user
			WHERE
			active_time > " . $timestamp
		;
	
		$result = $this->db->Execute($query);
	
		if ($result->fields['total_online_users']) {
			$temp['total_online_users'] = $result->fields['total_online_users'];
			$result->Close();
		}
		$datetime = date("Y-m-d H:i:s");
		$datetime = $this->date->formatDate($datetime);
		$temp['today'] = $datetime;
		
		return $temp;
	
	}//EO getNodeStatistics
	
	

	// setStatistic --------------------------------------------------------------------
	// set a statistic for the group
	// tom calthrop - 21st Feb 2004
	//
	function setStatistic ($total = null) {
		if (empty($total)) {
		    $total = 0;
		}
		
		$query = "
			DELETE FROM " . $this->db->prefix . "_statistic
			WHERE
			statistic_type_id=" . $this->statistic_type_id . " AND 
			section_id=" . $this->section_id . " AND 
			section_item_id=" . $this->section_item_id
		;
		
		if (isset($this->user_id)) {
		    $query .= " AND user_id=" . $this->user_id;
		}
		
		$result = $this->db->Execute($query);
		
		
		if (empty($this->user_id)) {
		    $this->user_id = "null";
		}
		
		$query = "
			INSERT INTO " . $this->db->prefix . "_statistic (
			section_id, 
			section_item_id, 
			statistic_type_id, 
			total, 
			user_id
			) values (
			" . $this->section_id . ",
			" . $this->section_item_id . ",
			" . $this->statistic_type_id . ",
			" . $total . ",
			" . $this->user_id . ")"
		;
		
		$result = $this->db->Execute($query);
		
		if (empty($result)) {
			$error_info = "function=setStatistic class=Statistic.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO setStatistic
	
	
	
	// getStatistic ---------------------------------------------------------------
	// returns a statistic
	// tom calthrop - 20th December 2004
	//
	function getStatistic() {
		
	$query = "
			SELECT s.total
			FROM " . $this->db->prefix . "_statistic s
			WHERE "
		;
		
		if (isset($this->section_id)) {
		    $query .= "s.section_id=" . $this->section_id . " AND ";
		}
		
		if (isset($this->statistic_type_id)) {
		    $query .= "s.statistic_type_id=" . $this->statistic_type_id . " AND ";
		}
		
		if (isset($this->section_item_id)) {
		    $query .= "s.section_item_id=" . $this->section_item_id . " AND ";
		}
		
		if (isset($this->user_id)) {
		    $query .= "s.user_id=" . $this->user_id . " AND ";
		}
		
		if (isset($this->no_user_id)) {
			$query .= "s.user_id=null AND ";
		}
		
		$query .= "1=1";
		
		$result = $this->db->Execute($query);
	
		if (isset($result->fields['total'])) {
	
			$result->Close();
	
			return $result->fields['total'];
		}
		else {
			$error_info = "function=getStatistic class=Statistic.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO getStatistic
}
?>
