From 677b831b2457f776a722dd425296ea956ce05dac Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@users.sourceforge.net>
Date: Sun, 28 Feb 2010 01:33:12 +0000
Subject: [PATCH] We store duration of time spent in seconds as this is a more
 "universal" format. Fix some bugs.

---
 htdocs/html.formother.class.php    |   4 +-
 htdocs/lib/project.lib.php         |  15 +--
 htdocs/projet/tasks/fiche.php      |   2 +-
 htdocs/projet/tasks/task.class.php |  26 +++---
 htdocs/projet/tasks/time.php       | 141 ++++++++++++++++-------------
 5 files changed, 102 insertions(+), 86 deletions(-)

diff --git a/htdocs/html.formother.class.php b/htdocs/html.formother.class.php
index 0b1faa03aa2..5c6eb890cf3 100644
--- a/htdocs/html.formother.class.php
+++ b/htdocs/html.formother.class.php
@@ -389,11 +389,11 @@ function PLineSelect(&$inc, $parent, $lines, $level=0, $selected=0)
 				print $langs->trans("Project").' '.$lines[$i]->projectref;
 				if (empty($lines[$i]->public))
 				{
-					print ' ('.$langs->trans("PrivateProject").')';
+					print ' ('.$langs->trans("Visibility").': '.$langs->trans("PrivateProject").')';
 				}
 				else
 				{
-					print ' ('.$langs->trans("SharedProject").')';
+					print ' ('.$langs->trans("Visibility").': '.$langs->trans("SharedProject").')';
 				}
 				if ($lines[$i]->id) print ' > ';
 				for ($k = 0 ; $k < $level ; $k++)
diff --git a/htdocs/lib/project.lib.php b/htdocs/lib/project.lib.php
index b76d2bd2c3e..42d3c553806 100644
--- a/htdocs/lib/project.lib.php
+++ b/htdocs/lib/project.lib.php
@@ -101,16 +101,16 @@ function task_prepare_head($object)
 	$head[$h][2] = 'task';
 	$h++;
 
-	$head[$h][0] = DOL_URL_ROOT.'/projet/tasks/time.php?id='.$object->id;
-	$head[$h][1] = $langs->trans("TimeSpent");
-	$head[$h][2] = 'time';
-	$h++;
-
 	$head[$h][0] = DOL_URL_ROOT.'/projet/tasks/contact.php?id='.$object->id;
 	$head[$h][1] = $langs->trans("Affectations");
 	$head[$h][2] = 'contact';
 	$h++;
 
+	$head[$h][0] = DOL_URL_ROOT.'/projet/tasks/time.php?id='.$object->id;
+	$head[$h][1] = $langs->trans("TimeSpent");
+	$head[$h][2] = 'time';
+	$h++;
+
 	$head[$h][0] = DOL_URL_ROOT.'/projet/tasks/note.php?id='.$object->id;
 	$head[$h][1] = $langs->trans('Notes');
 	$head[$h][2] = 'note';
@@ -478,8 +478,8 @@ function clean_orphelins($db)
 	// There is orphelins. We clean that
 	$listofid=array();
 
+	// Get list of id in array listofid
 	$sql='SELECT rowid FROM '.MAIN_DB_PREFIX.'projet_task';
-
 	$resql = $db->query($sql);
 	if ($resql)
 	{
@@ -500,7 +500,8 @@ function clean_orphelins($db)
 	if (sizeof($listofid))
 	{
 		// Removed orphelins records
-		print 'Some orphelins were found and restored to be parents so records are visible again.';
+		print 'Some orphelins were found and restored to be parents so records are visible again: ';
+		print join(',',$listofid);
 
 		$sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
 		$sql.= " SET fk_task_parent = 0";
diff --git a/htdocs/projet/tasks/fiche.php b/htdocs/projet/tasks/fiche.php
index 3af5bbc6341..16389a65b73 100644
--- a/htdocs/projet/tasks/fiche.php
+++ b/htdocs/projet/tasks/fiche.php
@@ -289,7 +289,7 @@ else
 	print '</div>';
 
 	// Test if database is clean. If not we clean it.
-	//print '$nboftaskshown='.$nboftaskshown.' sizeof($tasksarray)='.sizeof($tasksarray).' sizeof($tasksrole)='.sizeof($tasksrole).'<br>';
+	//print 'mode='.$_REQUEST["mode"].' $nboftaskshown='.$nboftaskshown.' sizeof($tasksarray)='.sizeof($tasksarray).' sizeof($tasksrole)='.sizeof($tasksrole).'<br>';
 	if ($_REQUEST["mode"]=='mine')
 	{
 		if ($nboftaskshown < sizeof($tasksrole)) clean_orphelins($db);
diff --git a/htdocs/projet/tasks/task.class.php b/htdocs/projet/tasks/task.class.php
index 963d5d1183b..36149fa7a08 100644
--- a/htdocs/projet/tasks/task.class.php
+++ b/htdocs/projet/tasks/task.class.php
@@ -595,7 +595,7 @@ class Task extends CommonObject
 	 */
 	function addTimeSpent($user, $notrigger=0)
 	{
-		$result = 0;
+		$ret = 0;
 
 		// Clean parameters
 		$this->timespent_duration = intval($this->timespent_duration)+(($this->timespent_duration-intval($this->timespent_duration))*(1+2/3));
@@ -612,15 +612,15 @@ class Task extends CommonObject
 		$sql.= $this->id;
 		$sql.= ", '".$this->db->idate($this->timespent_date)."'";
 		$sql.= ", ".$this->timespent_duration;
-		$sql.= ", ".$user->id;
+		$sql.= ", ".$this->timespent_fk_user;
 		$sql.= ", ".(isset($this->timespent_note)?"'".addslashes($this->timespent_note)."'":"null");
 		$sql.= ")";
 
 		dol_syslog(get_class($this)."::addTimeSpent sql=".$sql, LOG_DEBUG);
 		if ($this->db->query($sql) )
 		{
-			$task_id = $this->db->last_insert_id(MAIN_DB_PREFIX."projet_task");
-			$result = 0;
+			$task_id = $this->db->last_insert_id(MAIN_DB_PREFIX."projet_task_time");
+			$ret = $task_id;
 
 			if (! $notrigger)
 			{
@@ -635,30 +635,26 @@ class Task extends CommonObject
 		else
 		{
 			$this->error=$this->db->lasterror();
-			dol_syslog(get_class($this)."::addTimeSpent error -2 ".$this->error,LOG_ERR);
-			$result = -2;
+			dol_syslog(get_class($this)."::addTimeSpent error -1 ".$this->error,LOG_ERR);
+			$ret = -1;
 		}
 
-		if ($result == 0)
+		if ($ret >= 0)
 		{
 			$sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
 			$sql.= " SET duration_effective = duration_effective + '".price2num($this->timespent_duration)."'";
 			$sql.= " WHERE rowid = ".$this->id;
 
 			dol_syslog(get_class($this)."::addTimeSpent sql=".$sql, LOG_DEBUG);
-			if ($this->db->query($sql) )
-			{
-				$result = 0;
-			}
-			else
+			if (! $this->db->query($sql) )
 			{
 				$this->error=$this->db->lasterror();
-				dol_syslog(get_class($this)."::addTimeSpent error -3 ".$this->error, LOG_ERR);
-				$result = -2;
+				dol_syslog(get_class($this)."::addTimeSpent error -2 ".$this->error, LOG_ERR);
+				$ret = -2;
 			}
 		}
 
-		return $result;
+		return $ret;
 	}
 
     /**
diff --git a/htdocs/projet/tasks/time.php b/htdocs/projet/tasks/time.php
index ae885da36f4..0c64cf8d441 100644
--- a/htdocs/projet/tasks/time.php
+++ b/htdocs/projet/tasks/time.php
@@ -1,6 +1,6 @@
 <?php
 /* Copyright (C) 2005      Rodolphe Quiedeville <rodolphe@quiedeville.org>
- * Copyright (C) 2006-2009 Laurent Destailleur  <eldy@users.sourceforge.net>
+ * Copyright (C) 2006-2010 Laurent Destailleur  <eldy@users.sourceforge.net>
  * Copyright (C) 2010      Regis Houssin        <regis@dolibarr.fr>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -19,14 +19,15 @@
  */
 
 /**
- *	\file       htdocs/projet/tasks/task.php
+ *	\file       htdocs/projet/tasks/time.php
  *	\ingroup    projet
- *	\brief      Fiche taches d'un projet
+ *	\brief      Page to add new time spent on a task
  *	\version    $Id$
  */
 
 require("./pre.inc.php");
 require_once(DOL_DOCUMENT_ROOT."/lib/project.lib.php");
+require_once(DOL_DOCUMENT_ROOT."/lib/date.lib.php");
 
 if (!$user->rights->projet->lire) accessforbidden();
 
@@ -35,28 +36,43 @@ if (!$user->rights->projet->lire) accessforbidden();
  */
 if ($_POST["action"] == 'addtimespent' && $user->rights->projet->creer)
 {
-	if ($_POST["timespent_note"] && $_POST["timespent_duration"])
+	$error=0;
+
+	if (empty($_POST["timespent_durationhour"]) && empty($_POST["timespent_durationmin"]))
+	{
+		$mesg='<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Duration")).'</div>';
+		$error++;
+	}
+
+	if (! $error)
 	{
 		$task = new Task($db);
 		$task->fetch($_POST["id"]);
 
 		$task->timespent_note = $_POST["timespent_note"];
-		$task->timespent_duration = $_POST["timespent_duration"];
+		$task->timespent_duration = $_POST["timespent_durationhour"]*60*60;	// We store duration in seconds
 		$task->timespent_date = dol_mktime(12,0,0,$_POST["timemonth"],$_POST["timeday"],$_POST["timeyear"]);
-		  		
-		$task->addTimeSpent($user);
+		$task->timespent_fk_user = $_POST["userid"];
+
+		$result=$task->addTimeSpent($user);
+		if ($result >= 0)
+		{
+
+		}
+		else
+		{
+			$mesg='<div class="error">'.$langs->trans($task->error).'</div>';
+		}
 	}
 	else
 	{
-		$langs->load("errors");
-		$mesg='<div class="error">'.$langs->trans($task->error).'</div>';
 		$_POST["action"]='';
 	}
 }
 
 if ($_POST["action"] == 'updateline' && ! $_POST["cancel"] && $user->rights->projet->creer)
 {
-	
+
 }
 
 if ($_REQUEST["action"] == 'confirm_delete' && $_REQUEST["confirm"] == "yes" && $user->rights->projet->creer)
@@ -96,7 +112,7 @@ if ($_GET["id"] > 0)
 	{
 		$result=$projectstatic->fetch($task->fk_project);
 		if (! empty($projectstatic->socid)) $projectstatic->societe->fetch($projectstatic->socid);
-		
+
 		// To verify role of users
 		$userAccess = $projectstatic->restrictedProjectArea($user);
 
@@ -105,7 +121,7 @@ if ($_GET["id"] > 0)
 		dol_fiche_head($head, 'time', $langs->trans("Task"),0,'projecttask');
 
 		if ($mesg) print $mesg.'<br>';
-		
+
 		if ($_GET["action"] == 'deleteline')
 		{
 			$ret=$html->form_confirm($_SERVER["PHP_SELF"]."?id=".$_GET["id"].'&lineid='.$_GET["lineid"],$langs->trans("DeleteATimeSpent"),$langs->trans("ConfirmDeleteATimeSpent"),"confirm_delete",'','',1);
@@ -136,58 +152,65 @@ if ($_GET["id"] > 0)
 		print '</td></tr>';
 
 		print '</table>';
+
 		print '</div>';
-		
+
 		/*
 		 * Add time spent
 		 */
 		if ($user->rights->projet->creer && $userAccess)
 		{
+			print '<br>';
+
 			print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'?id='.$task->id.'">';
 			print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
 			print '<input type="hidden" name="action" value="addtimespent">';
 			print '<input type="hidden" name="id" value="'.$task->id.'">';
-			
+
 			print '<table class="noborder" width="100%">';
-			
+
 			print '<tr class="liste_titre">';
-			print '<td width="50%">'.$langs->trans("Note").'</td>';
+			print '<td width="100">'.$langs->trans("Date").'</td>';
 			print '<td>'.$langs->trans("By").'</td>';
-			print '<td>'.$langs->trans("Date").'</td>';
-			print '<td colspan="2">'.$langs->trans("Duration").'</td>';
+			print '<td>'.$langs->trans("Note").'</td>';
+			print '<td align="right">'.$langs->trans("Duration").'</td>';
+			print '<td width="80">&nbsp;</td>';
 			print "</tr>\n";
-			
-			print '<tr>';
-			
-			// Note
-			print '<td nowrap="nowrap" width="50%">';
-			print '<textarea name="timespent_note" cols="80" rows="4"></textarea>';
+
+			print '<tr '.$bc[false].'>';
+
+			// Date
+			print '<td nowrap="nowrap">';
+			$newdate=dol_mktime(12,0,0,$_POST["timemonth"],$_POST["timeday"],$_POST["timeyear"]);
+			print $html->select_date($newdate,'time','','','',"timespent_date");
 			print '</td>';
-			
+
 			// Contributor
 			print '<td nowrap="nowrap">';
-			print '&nbsp;'; // TODO ajout liste deroulante des participants
+			// TODO We should use here a combo list with contacts affected to task only
+			print $html->select_users($_POST["userid"]?$_POST["userid"]:$user->id,'userid');
 			print '</td>';
-			
-			// Date
+
+			// Note
 			print '<td nowrap="nowrap">';
-			print $html->select_date('','time','','','',"timespent_date");
+			print '<textarea name="timespent_note" cols="80" rows="'.ROWS_3.'">'.($_POST['timespent_note']?$_POST['timespent_note']:'').'</textarea>';
 			print '</td>';
-			
+
 			// Duration
-			print '<td nowrap="nowrap">';
-			print '<input size="4" type="text" class="flat" name="timespent_duration" value="">';
+			print '<td nowrap="nowrap" align="right">';
+			print $html->select_duree('timespent_duration');
+			//print '<input size="4" type="text" class="flat" name="timespent_duration" value="">';
 			print '</td>';
-			
-			print '<td>';
+
+			print '<td align="center">';
 			print '<input type="submit" class="button" value="'.$langs->trans("Add").'">';
 			print '</td></tr>';
-			
+
 			print '</table></form>';
 		}
 
 		print '<br>';
-		
+
 		/*
 		 *  List of time spent
 		 */
@@ -218,56 +241,56 @@ if ($_GET["id"] > 0)
 		{
 			dol_print_error($db);
 		}
-		
+
 		print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'?id='.$task->id.'">';
 		print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
 		print '<input type="hidden" name="action" value="updateline">';
 		print '<input type="hidden" name="id" value="'.$task->id.'">';
-		
+
 		print '<table class="noborder" width="100%">';
 		print '<tr class="liste_titre">';
-		print '<td width="50%">'.$langs->trans("Note").'</td>';
+		print '<td width="100">'.$langs->trans("Date").'</td>';
 		print '<td>'.$langs->trans("By").'</td>';
-		print '<td>'.$langs->trans("Date").'</td>';
+		print '<td align="left">'.$langs->trans("Note").'</td>';
 		print '<td align="right">'.$langs->trans("Duration").'</td>';
-		print '<td colspan="2">&nbsp;</td>';
+		print '<td>&nbsp;</td>';
 		print "</tr>\n";
 
 		foreach ($tasks as $task_time)
 		{
 			$var=!$var;
   		    print "<tr ".$bc[$var].">";
-  		    
-  		    // Note
-  		    print '<td width="50%">';
+
+  		    // Date
+  		    print '<td>';
   		    if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
   		    {
-  		    	print '<textarea name="timespent_note" cols="80" rows="4">'.$task_time->note.'</textarea>';
+  		    	print $html->select_date($db->jdate($task_time->task_date),'timeline','','','',"timespent_date");
   		    }
   		    else
   		    {
-  		    	print dol_nl2br($task_time->note);
+  		    	print dol_print_date($db->jdate($task_time->task_date),'day');
   		    }
   		    print '</td>';
-  		    
+
   		    // User
 			$user->id		= $task_time->fk_user;
 		    $user->nom		= $task_time->name;
 		    $user->prenom 	= $task_time->firstname;
 		    print '<td>'.$user->getNomUrl(1).'</td>';
 
-  		    // Date
-  		    print '<td>';
+ 		    // Note
+  		    print '<td align="left">';
   		    if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
   		    {
-  		    	print $html->select_date($task_time->task_date,'timeline','','','',"timespent_date");
+  		    	print '<textarea name="timespent_note" cols="80" rows="4">'.$task_time->note.'</textarea>';
   		    }
   		    else
   		    {
-  		    	print dol_print_date($db->jdate($task_time->task_date),'%A').' '.dol_print_date($db->jdate($task_time->task_date),'daytext');
+  		    	print dol_nl2br($task_time->note);
   		    }
   		    print '</td>';
-  		    
+
   		    // Time spent
   		    print '<td align="right">';
   		    if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
@@ -276,16 +299,12 @@ if ($_GET["id"] > 0)
   		    }
   		    else
   		    {
-  		    	// TODO add function
-  		    	$hour = intval($task_time->task_duration);
-  		    	$minutes = round((($task_time->task_duration - $hour) * 60),0);
-  		    	$minutes = substr("00"."$minutes", -2);
-  		    	print $hour."&nbsp;h&nbsp;".$minutes;
+				print ConvertSecondToTime($task_time->task_duration,'all');
   		    }
   		    print '</td>';
-			
+
 			// Edit and delete icon
-			print '<td align="center" valign="middle">';
+			print '<td align="center" valign="middle" width="80">';
 			if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
   		    {
   		    	print '<input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
@@ -298,14 +317,14 @@ if ($_GET["id"] > 0)
 				print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$task->id.'&amp;action=editline&amp;lineid='.$task_time->rowid.'">';
 				print img_edit();
 				print '</a>';
-				
+
 				print '&nbsp;';
 				print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$task->id.'&amp;action=deleteline&amp;lineid='.$task_time->rowid.'">';
 				print img_delete();
 				print '</a>';
 			}
 			print '</td>';
-			
+
 			print "</tr>\n";
 		}
 
-- 
GitLab