<?php
// task.php - data for a subtask
// ------------------------------------------------------------------------
// Copyright (c) 2007 Jerome Nivard
// Copyright (c) 2015 Denis Guinnepain
// ------------------------------------------------------------------------
// This file is part of Diamentis

// Diamentis 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.

// Diamentis 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 phpBugTracker; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// ------------------------------------------------------------------------

if ( !defined('INC_FILE_TASK') ) {
      define ('INC_FILE_TASK',1);

include('Inc/Classes/bo/activity.php');
include('Inc/Classes/bo/connexion.php');
include_once('Inc/ConstantesDB.php');

class Task
{
  var $id;
  var $idFD;// vaudrait mieux un objet File !!!
  var $file;// Le voila ! Il faut à terme supprimer le idFD !
	var $idTaskDesc;
	var $initLoad;
	var $raf;
	var $used;// en minutes, depuis le WebCalendar
	var $tabActivity;// liste des activités 

Function Load(&$mdb2) {
    $req="select * from " . TBL_PREFIX . "task where id=".$this->id;
    $result=mysql_query($req,$mdb2);
    if ($result==false) {
      printf("<br>La requête $req a échoué !");
    } else {
        //OpenDBCal($mdb2Cal);
        While ($data=mysql_fetch_object($result)) {			
		$this->idFD=$data->idFD;
		$this->idTaskDesc=$data->idTaskDesc;
		$this->initLoad=$data->initLoad;
		$this->raf=$data->raf;
		$this->used=$data->used;
		//$this->CalculateUsedFromActivities($mdb2Cal);
      }
	//CloseDB($mdb2Cal);
    }
    mysql_free_result($result);
}
  
function Delete(&$mdb2) {
	  // Si 0 partout : supprimer la tache
	$taskDesc = new TaskDescription($this->idTaskDesc);
	$taskDesc->Delete($mdb2);
	$req="delete from " . TBL_PREFIX . "task where id=$this->id";
	$result=mysql_query($req,$mdb2);
	if ($result==false) {
		printf("<br>La requête $req a échoué !");
		$ret = -1;
	} else {
		printf("<br>La requête $req a réussi !");
		$ret = $this->id;
	}
		
}
	
function Update(&$mdb2) {
	  // Si 0 partout : supprimer la tache
// 	$taskDesc = new TaskDescription($this->idTaskDesc);
// 	If ($this->initLoad=="" And $this->raf=="" And $this->used=="") {
// 			$taskDesc->Delete($mdb2);
// 			$req="delete from task where id=$this->id";
// 			$result=mysql_query($req,$mdb2);
// 			if ($result==false) {
// 				printf("<br>La requête $req a échoué !");
// 				$ret = -1;
// 			} else {
// 				printf("<br>La requête $req a réussi !");
// 				$ret = 1;
// 			}
// 		}
	
		If ($this->initLoad=="") {
  		$req = " initLoad=NULL";
		} else {
			$req = " initLoad=".$this->initLoad;
		}
		If ($this->raf=="") {
  		$req .= ", raf=NULL";
		} else {
			$req .= ", raf=".$this->raf;
		}
		If ($this->used=="") {
  		$req .= ", used=NULL";
		} else {
			$req .= ", used=".$this->used;
		}
    $req="update " . TBL_PREFIX . "task set $req where id=$this->id";
    $result=mysql_query($req,$mdb2);
    if ($result==false) {
      printf("<br>La requête $req a échoué !");
			$ret = -1;
    } else {
			//printf("<br>La requête $req a réussi !");
      //mysql_free_result($result);
			$ret = 1;
// 			$taskDesc->Update($mdb2);
    }
		return $ret;
}
  
function Save(&$mdb2) {
	  $ret=-1;
		$reqNames = "insert into " . TBL_PREFIX . "task (idFD, idTaskDesc";
		$reqValues = " values ($this->idFD,$this->idTaskDesc";
		If ($this->initLoad<>'') {
		  //printf("initLoad : %d", $this->initLoad);
			$reqNames = $reqNames.", initLoad";
			$reqValues = $reqValues.", $this->initLoad";
		}
		If ($this->raf<>'') {
			$reqNames = $reqNames.", raf";
			$reqValues = $reqValues.", $this->raf";
		}
		If ($this->used<>'') {
			$reqNames = $reqNames.", used";
			$reqValues = $reqValues.", $this->used";
		}
		$reqNames = $reqNames.")";
		$reqValues = $reqValues.")";
		$req = $reqNames.$reqValues;
		$result=mysql_query($req,$mdb2);
    if ($result==false) {
      printf("<br>La requête $req a échoué !!!");
			$ret = -1;
    } else {
		  //printf("<br>La requête $req a réussi !");
			$ret = 1;
    }
    //mysql_free_result($result);
  	return $ret;
}

function setUsed( &$mdb2) {
    $req="select * from " . TBL_PREFIX . "task where idFD=$idFD";
    $result=mysql_query($req,$mdb2);
    if ($result==false) {
      printf("La requête $req a échoué !");
    } else {
      $rows=mysql_num_rows($result);
      While ($data=mysql_fetch_object($result)) {
        $resProd[]=array($data->Date,$data->Heures);
      }
    }
    mysql_free_result($result);
  }
	
function CalculateUsedFromActivities(&$mdb2)
  {
	  If ($this->id<>NULL) {
      $req="select * from webcal_entry where cal_name=$this->id order by cal_date, cal_time";
      $result=mysql_query($req,$mdb2);
      if ($result==false) {
        printf("<br>La requête $req a échoué !");
      } else {
        $rows=mysql_num_rows($result);
				$this->used=0;
        While ($data=mysql_fetch_object($result)) {
          $this->used=$this->used + $data->cal_duration;
        }
				//If (0<$rows) {
  		//	  printf("<br>Total used = $this->used min");
			//	}
        mysql_free_result($result);
			}
    }
		return $ret;
  }	
	
	function SaveUsed(&$mdb2)
  {
	  If ($this->id<>NULL) {
      $req="update " . TBL_PREFIX . "task set used=$this->used where id=$this->id";
      $result=mysql_query($req,$mdb2);
      if ($result==false) {
        printf("<br>La requête $req a échoué !");
      } else {
  			  printf("<br>La requête $req a réussi.");
          mysql_free_result($result);
	/*
	$file=new File;
	$file->id=$this->idFD;
	$file->CalculateUsed();
	*/
	}
   }
}
	
function GetActivities(&$mdb2) {
  If ($this->id<>NULL) {
      $req="select * from webcal_entry,webcal_entry_user where webcal_entry.cal_name=$this->id and webcal_entry_user.cal_id=webcal_entry.cal_id and webcal_entry_user.cal_status<>'D'";
      $result=mysql_query($req,$mdb2);
      if ($result==false) {
        printf("<br>La requête $req a échoué !");
      } else {
        $rows=mysql_num_rows($result);
	$this->used=0;
        While ($data=mysql_fetch_object($result)) {
          $this->used=$this->used + $data->cal_duration;
          $activity=new Activity;
          $activity->id=$data->cal_id;
          $activity->loadFromDbData($data);
          $this->tabActivity[]=$activity;
        }
	//If (0<$rows) {
  	//	  printf("<br>Total used = $this->used min");
        //	}
        mysql_free_result($result);
      }
  }
  return $ret;
}		
  	
} // Class

} // ifndef
?>
