From b7b322e6612d99451faefab99e1379abee4eee61 Mon Sep 17 00:00:00 2001 From: mc2contributor Date: Thu, 27 Apr 2023 11:46:39 -0600 Subject: [PATCH] API endpoint to update timespent record --- htdocs/projet/class/api_tasks.class.php | 59 +++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/htdocs/projet/class/api_tasks.class.php b/htdocs/projet/class/api_tasks.class.php index 64690f90950..e77dea9e483 100644 --- a/htdocs/projet/class/api_tasks.class.php +++ b/htdocs/projet/class/api_tasks.class.php @@ -516,6 +516,7 @@ class Tasks extends DolibarrApi * @param string $note Note * * @url POST {id}/addtimespent + * NOTE: Should be "POST {id}/timespent", since POST already implies "add" * * @return array */ @@ -562,6 +563,64 @@ class Tasks extends DolibarrApi ); } + /** + * Update time spent for a task of a project. + * You can test this API with the following input message + * { "date": "2016-12-31 23:15:00", "duration": 1800, "user_id": 1, "note": "My time test" } + * + * @param int $id Task ID + * @param int $timespent_id Time spent ID (llx_projet_task_time.rowid) + * @param datetime $date Date (YYYY-MM-DD HH:MI:SS in GMT) + * @param int $duration Duration in seconds (3600 = 1h) + * @param int $user_id User (Use 0 for connected user) + * @param string $note Note + * + * @url PUT {id}/timespent/{timespent_id} + * + * @return array + */ + public function putTimeSpent($id, $timespent_id, $date, $duration, $user_id = 0, $note = '') + { + if (!DolibarrApiAccess::$user->rights->projet->creer) { + throw new RestException(401); + } + if ($this->task->fetch($id) <= 0) { + throw new RestException(404, 'Task not found'); + } + if ($this->task->fetchTimeSpent($timespent_id) <= 0) { + throw new RestException(404, 'Timespent not found'); + } + elseif ($this->task->id != $id) { + throw new RestException(404, 'Timespent not found in selected task'); + } + + if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $newdate = dol_stringtotime($date, 1); + $this->task->timespent_date = $newdate; + $this->task->timespent_datehour = $newdate; + $this->task->timespent_withhour = 1; + $this->task->timespent_duration = $duration; + $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id; + $this->task->timespent_note = $note; + + $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0); + if ($result == 0) { + throw new RestException(304, 'Error nothing done.'); + } + if ($result < 0) { + throw new RestException(500, 'Error when updating time spent: '.$this->task->error); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Time spent updated' + ) + ); + } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /**