From da2e8dd34849a6eec95866bcf72efc01b2249fb7 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Wed, 24 Feb 2010 14:54:55 +0000 Subject: [PATCH] Fix: management of time spent in task card --- htdocs/lib/project.lib.php | 2 +- htdocs/projet/activity/list.php | 19 ++-- htdocs/projet/tasks/task.class.php | 141 +++++++++++++++++++++++++++-- htdocs/projet/tasks/time.php | 27 ++++-- 4 files changed, 164 insertions(+), 25 deletions(-) diff --git a/htdocs/lib/project.lib.php b/htdocs/lib/project.lib.php index de698849b29..b76d2bd2c3e 100644 --- a/htdocs/lib/project.lib.php +++ b/htdocs/lib/project.lib.php @@ -268,7 +268,7 @@ function PLinesb(&$inc, $parent, $lines, &$level, &$projectsrole) //var_dump($lines[$i]); //var_dump($projectsrole[$lines[$i]->projectid]); // If at least one role for project - if ($lines[$i]->public || ! empty($projectsrole[$lines[$i]->projectid])) $disabled=0; + if ($lines[$i]->public || ! empty($projectsrole[$lines[$i]->projectid]) || $user->rights->projet->all->creer) $disabled=0; print ''; print $form->select_date('',$lines[$i]->id,'','','',"addtime"); diff --git a/htdocs/projet/activity/list.php b/htdocs/projet/activity/list.php index 94a63440396..c1bd319f305 100644 --- a/htdocs/projet/activity/list.php +++ b/htdocs/projet/activity/list.php @@ -60,27 +60,28 @@ if ($_POST["action"] == 'createtask' && $user->rights->projet->creer) if ($_POST["action"] == 'addtime' && $user->rights->projet->creer) { - foreach ($_POST as $key => $post) + foreach ($_POST as $key => $time) { - //$pro->CreateTask($user, $_POST["task_name"]); if (substr($key,0,4) == 'task') { - if ($post > 0) + if ($time > 0) { - $post=intval($post)+(($post-intval($post))*(1+2/3)); - $post=price2num($post); + $time = intval($time)+(($time-intval($time))*(1+2/3)); + $time = price2num($time); $id = str_replace("task","",$key); - $task=new Task($db); + $task = new Task($db); $task->fetch($id); - $date = dol_mktime(12,0,0,$_POST["$id"."month"],$_POST["$id"."day"],$_POST["$id"."year"]); - $task->addTimeSpent($user, $post, $date); + $task->timespent_duration = $time; + $task->timespent_date = dol_mktime(12,0,0,$_POST["$id"."month"],$_POST["$id"."day"],$_POST["$id"."year"]); + + $task->addTimeSpent($user); } else { - if ($post != '') $mesg='
'.$langs->trans("ErrorBadValue").'
'; + if ($time != '') $mesg='
'.$langs->trans("ErrorBadValue").'
'; } } } diff --git a/htdocs/projet/tasks/task.class.php b/htdocs/projet/tasks/task.class.php index ed8bbb10f3c..3946bb03987 100644 --- a/htdocs/projet/tasks/task.class.php +++ b/htdocs/projet/tasks/task.class.php @@ -56,6 +56,12 @@ class Task extends CommonObject var $statut; var $note_private; var $note_public; + + var $timespent_id; + var $timespent_duration; + var $timespent_date; + var $timespent_user; + var $timespent_note; /** @@ -299,6 +305,7 @@ class Task extends CommonObject function delete($user, $notrigger=0) { global $conf, $langs; + $error=0; $this->db->begin(); @@ -586,7 +593,7 @@ class Task extends CommonObject * \param time Time spent * \param date date */ - function addTimeSpent($user, $time, $date, $notrigger=0) + function addTimeSpent($user, $notrigger=0) { $result = 0; @@ -597,12 +604,12 @@ class Task extends CommonObject $sql.= ", fk_user"; $sql.= ") VALUES ("; $sql.= $this->id; - $sql.= ", '".$this->db->idate($date)."'"; - $sql.= ", ".$time; + $sql.= ", '".$this->db->idate($this->timespent_date)."'"; + $sql.= ", ".$this->timespent_duration; $sql.= ", ".$user->id; $sql.= ")"; - dol_syslog("Task::addTimeSpent sql=".$sql, LOG_DEBUG); + 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"); @@ -621,17 +628,17 @@ class Task extends CommonObject else { $this->error=$this->db->lasterror(); - dol_syslog("Task::addTimeSpent error -2 ".$this->error,LOG_ERR); + dol_syslog(get_class($this)."::addTimeSpent error -2 ".$this->error,LOG_ERR); $result = -2; } if ($result == 0) { $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task"; - $sql.= " SET duration_effective = duration_effective + '".price2num($time)."'"; + $sql.= " SET duration_effective = duration_effective + '".price2num($this->timespent_duration)."'"; $sql.= " WHERE rowid = ".$this->id; - dol_syslog("Project::addTimeSpent sql=".$sql, LOG_DEBUG); + dol_syslog(get_class($this)."::addTimeSpent sql=".$sql, LOG_DEBUG); if ($this->db->query($sql) ) { $result = 0; @@ -639,13 +646,131 @@ class Task extends CommonObject else { $this->error=$this->db->lasterror(); - dol_syslog("Task::addTimeSpent error -3 ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::addTimeSpent error -3 ".$this->error, LOG_ERR); $result = -2; } } return $result; } + + /** + * \brief Load object in memory from database + * \param id id object + * \return int <0 if KO, >0 if OK + */ + function fetchTimeSpent($id) + { + global $langs; + + $sql = "SELECT"; + $sql.= " t.rowid,"; + $sql.= " t.fk_task,"; + $sql.= " t.task_date,"; + $sql.= " t.task_duration,"; + $sql.= " t.fk_user,"; + $sql.= " t.note"; + $sql.= " FROM ".MAIN_DB_PREFIX."projet_task_time as t"; + $sql.= " WHERE t.rowid = ".$id; + + dol_syslog(get_class($this)."::fetchTimeSpent sql=".$sql, LOG_DEBUG); + $resql=$this->db->query($sql); + if ($resql) + { + if ($this->db->num_rows($resql)) + { + $obj = $this->db->fetch_object($resql); + + $this->timespent_id = $obj->rowid; + $this->id = $obj->fk_task; + $this->timespent_date = $obj->task_date; + $this->timespent_duration = $obj->task_duration; + $this->timespent_user = $obj->fk_user; + $this->timespent_note = $obj->note; + } + + $this->db->free($resql); + + return 1; + } + else + { + $this->error="Error ".$this->db->lasterror(); + dol_syslog(get_class($this)."::fetchTimeSpent ".$this->error, LOG_ERR); + return -1; + } + } + + /** + * \brief Delete time spent + * \param user User that delete + * \param notrigger 0=launch triggers after, 1=disable triggers + * \return int <0 if KO, >0 if OK + */ + function delTimeSpent($user, $notrigger=0) + { + global $conf, $langs; + + $error=0; + + $this->db->begin(); + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task_time"; + $sql.= " WHERE rowid = ".$this->timespent_id; + + dol_syslog(get_class($this)."::delTimeSpent sql=".$sql); + $resql = $this->db->query($sql); + if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); } + + if (! $error) + { + if (! $notrigger) + { + // Call triggers + include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); + $interface=new Interfaces($this->db); + $result=$interface->run_triggers('TASK_TIMESPENT_DELETE',$this,$user,$langs,$conf); + if ($result < 0) { $error++; $this->errors=$interface->errors; } + // End call triggers + } + } + + if (! $error) + { + $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)."::delTimeSpent sql=".$sql, LOG_DEBUG); + if ($this->db->query($sql) ) + { + $result = 0; + } + else + { + $this->error=$this->db->lasterror(); + dol_syslog(get_class($this)."::addTimeSpent error -3 ".$this->error, LOG_ERR); + $result = -2; + } + } + + // Commit or rollback + if ($error) + { + foreach($this->errors as $errmsg) + { + dol_syslog(get_class($this)."::delTimeSpent ".$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; + } + else + { + $this->db->commit(); + return 1; + } + } } ?> \ No newline at end of file diff --git a/htdocs/projet/tasks/time.php b/htdocs/projet/tasks/time.php index 86d7f367b54..601af23ed05 100644 --- a/htdocs/projet/tasks/time.php +++ b/htdocs/projet/tasks/time.php @@ -38,9 +38,18 @@ if ($_POST["action"] == 'updateline' && ! $_POST["cancel"] && $user->rights->pro } -if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == "yes" && $user->rights->projet->creer) +if ($_REQUEST["action"] == 'confirm_delete' && $_REQUEST["confirm"] == "yes" && $user->rights->projet->creer) { - + $task = new Task($db); + $task->fetchTimeSpent($_GET['lineid']); + $result = $task->delTimeSpent($user); + + if (!$result) + { + $langs->load("errors"); + $mesg='
'.$langs->trans($task->error).'
'; + $_POST["action"]=''; + } } @@ -78,7 +87,7 @@ if ($_GET["id"] > 0) if ($_GET["action"] == 'deleteline') { - $ret=$html->form_confirm($_SERVER["PHP_SELF"]."?id=".$_GET["id"],$langs->trans("DeleteATimeSpent"),$langs->trans("ConfirmDeleteATimeSpent"),"confirm_delete",'','',1); + $ret=$html->form_confirm($_SERVER["PHP_SELF"]."?id=".$_GET["id"].'&lineid='.$_GET["lineid"],$langs->trans("DeleteATimeSpent"),$langs->trans("ConfirmDeleteATimeSpent"),"confirm_delete",'','',1); if ($ret == 'html') print '
'; } @@ -131,8 +140,8 @@ if ($_GET["id"] > 0) /* * List of time spent */ - $sql = "SELECT t.rowid, t.task_date, t.task_duration, t.fk_user"; - $sql.= ", u.rowid as userid, u.name, u.firstname"; + $sql = "SELECT t.rowid, t.task_date, t.task_duration, t.fk_user, t.note"; + $sql.= ", u.name, u.firstname"; $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as t"; $sql .= " , ".MAIN_DB_PREFIX."user as u"; $sql .= " WHERE t.fk_task =".$task->id; @@ -163,6 +172,7 @@ if ($_GET["id"] > 0) print ''; print ''.$langs->trans("By").''; print ''.$langs->trans("Date").''; + print ''.$langs->trans("Note").''; print ''.$langs->trans("TimeSpent").''; print ' '; print "\n"; @@ -173,7 +183,7 @@ if ($_GET["id"] > 0) print ""; // User - $user->id = $task_time->userid; + $user->id = $task_time->fk_user; $user->nom = $task_time->name; $user->prenom = $task_time->firstname; print ''.$user->getNomUrl(1).''; @@ -181,7 +191,10 @@ if ($_GET["id"] > 0) // Date print ''.dol_print_date($db->jdate($task_time->task_date),'%A').' '.dol_print_date($db->jdate($task_time->task_date),'daytext').''; - // Time spent + // Note + print ''.dol_nl2br($task_time->note).''; + + // Time spent $heure = intval($task_time->task_duration); $minutes = round((($task_time->task_duration - $heure) * 60),0); $minutes = substr("00"."$minutes", -2);