<?php
//une connexion à la base existe
//la table stockant les timers se nomme _timer_ (sauf si son nom est changé
//par le constructeur

if (isset($_timer)) {
    print "<h3>Problème dans timer.class.php : la variable \$_timer existe déjà !</h3>";
    exit();
    }

$_timer=array();  //tableau d'objets de la classe _timer

function t_start($id) {
global $_timer;
if (!isset($_timer[$id])) {
    $_timer[$id]=new _timer($id);
    }
$_timer[$id]->start();
}

function t_stop($id) {
global $_timer;
$_timer[$id]->stop();
}

function t_cumule($id='') {
global $_timer;
if ($id) $_timer[$id]->cumule();
else foreach($_timer as $t) $t->cumule();
}

function t_raz($id='') {
global $_timer;
if ($id) $_timer[$id].raz();
else foreach($_timer as $t) $t->raz();
}

function t_razenr($id='') {
global $_timer;
if ($id) $_timer[$id].razenr();
else foreach($_timer as $t) $t->razenr();
}

function t_raztable() {
t_raz(); //les timers courants
$temp=new _timer('RC69N2BEkjxijixjeazxcàéç"_à'); //bidon : je ne sais pas faire autrement !
$temp->raztable(); //remettre la table à 0

}

////////////////////////////////////////////////////////////////////////////////
class _timer {
var $started,$stopped;
var $id;
var $tinit,$tfin;
var $lap;
var $table='_timer';

function _timer($id,$table='') {
$this->id=$id;
if ($table) $this->table=$table;
$res=@mysql_query("SELECT * FROM $this->table");
if ($res===false) {
    query("CREATE TABLE $this->table
            (
            id varchar(40) NOT NULL default '',
            duree float(8,6) unsigned default '0.000000',
            q int(11) NOT NULL default '0',
            PRIMARY KEY  (id)
            ) TYPE=MyISAM;
          ");
    }
$this->started=false;
$this->stopped=true;
$this->lap=0;
}


function start() {
if (!$this->started) {
        list($msec, $sec)=explode(" ",microtime());
        $this->tinit=(float)$msec  + (float)$sec;
        $this->started=true;
        $this->stopped=false;
    }
}

function stop() {
if (!$this->stopped) {
        list($msec, $sec)=explode(" ",microtime());
        $this->tfin=(float)$msec  + (float)$sec;
        $this->stopped=true;
        $this->started=false;
        $this->lap+=$this->tfin - $this->tinit;
        }
}

function getduree() {
return $this->lap;
}

function raz() {
$this->_timer($this->id);
}


function cumule($q=0) {  //vide lap après le cumul dans la base
$this->stop();
$ret=$this->getduree();
if (mysql_num_rows(query("SELECT * FROM $this->table WHERE id='$this->id'"))) {
        query("UPDATE $this->table SET duree=duree+$this->lap, q=q+$q WHERE id='$this->id'");
        $this->raz();
        }
else $this->writetable();
return $ret;
}

function writetable($q=0) {
query("REPLACE INTO $this->table SET id='$this->id', duree=$this->lap, q=$q");
$this->raz();
}

function readtable() {
$res=query("SELECT duree FROM $this->table WHERE id='$this->id'");
if ($row=mysql_fetch_array($res)) return $row['duree'];
else return false;
}

function razenr() {
$this->raz();
$this->writetable();
}

function delenr() {
$this->raz();
query("DELETE FROM $table WHERE id='$this->id'");
}

function raztable() {
query("DELETE FROM $this->table");
}

function deltable() {
query("DROP TABLE $this->table");
}

} //class

?>