PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/projet/class/task.class.php

https://github.com/asterix14/dolibarr
PHP | 910 lines | 710 code | 82 blank | 118 comment | 67 complexity | 80e11f2fa115469bcdc5fe1a0384e24d MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2008-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2010 Regis Houssin <regis@dolibarr.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/projet/class/task.class.php
  20. * \ingroup project
  21. * \brief This file is a CRUD class file for Task (Create/Read/Update/Delete)
  22. */
  23. require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
  24. /**
  25. * \class Task
  26. * \brief Class to manage tasks
  27. * \remarks Initialy built by build_class_from_table on 2008-09-10 12:41
  28. */
  29. class Task extends CommonObject
  30. {
  31. public $element='project_task'; //!< Id that identify managed objects
  32. public $table_element='projet_task'; //!< Name of table without prefix where object is stored
  33. var $id;
  34. var $fk_project;
  35. var $fk_task_parent;
  36. var $label;
  37. var $description;
  38. var $duration_effective;
  39. var $date_c;
  40. var $date_start;
  41. var $date_end;
  42. var $progress;
  43. var $priority;
  44. var $fk_user_creat;
  45. var $fk_user_valid;
  46. var $statut;
  47. var $note_private;
  48. var $note_public;
  49. var $timespent_id;
  50. var $timespent_duration;
  51. var $timespent_old_duration;
  52. var $timespent_date;
  53. var $timespent_fk_user;
  54. var $timespent_note;
  55. /**
  56. * Constructor
  57. *
  58. * @param DoliDB $DB Database handler
  59. */
  60. function Task($DB)
  61. {
  62. $this->db = $DB;
  63. return 1;
  64. }
  65. /**
  66. * Create into database
  67. *
  68. * @param User $user User that create
  69. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  70. * @return int <0 if KO, Id of created object if OK
  71. */
  72. function create($user, $notrigger=0)
  73. {
  74. global $conf, $langs;
  75. $error=0;
  76. // Clean parameters
  77. $this->label = trim($this->label);
  78. $this->description = trim($this->description);
  79. // Check parameters
  80. // Put here code to add control on parameters values
  81. // Insert request
  82. $sql = "INSERT INTO ".MAIN_DB_PREFIX."projet_task (";
  83. $sql.= "fk_projet";
  84. $sql.= ", fk_task_parent";
  85. $sql.= ", label";
  86. $sql.= ", description";
  87. $sql.= ", datec";
  88. $sql.= ", fk_user_creat";
  89. $sql.= ", dateo";
  90. $sql.= ", datee";
  91. $sql.= ", progress";
  92. $sql.= ") VALUES (";
  93. $sql.= $this->fk_project;
  94. $sql.= ", ".$this->fk_task_parent;
  95. $sql.= ", '".$this->db->escape($this->label)."'";
  96. $sql.= ", '".$this->db->escape($this->description)."'";
  97. $sql.= ", '".$this->db->idate($this->date_c)."'";
  98. $sql.= ", ".$user->id;
  99. $sql.= ", ".($this->date_start!=''?"'".$this->db->idate($this->date_start)."'":'null');
  100. $sql.= ", ".($this->date_end!=''?"'".$this->db->idate($this->date_end)."'":'null');
  101. $sql.= ", ".($this->progress!=''?$this->progress:0);
  102. $sql.= ")";
  103. $this->db->begin();
  104. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  105. $resql=$this->db->query($sql);
  106. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  107. if (! $error)
  108. {
  109. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."projet_task");
  110. if (! $notrigger)
  111. {
  112. // Call triggers
  113. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  114. $interface=new Interfaces($this->db);
  115. $result=$interface->run_triggers('TASK_CREATE',$this,$user,$langs,$conf);
  116. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  117. // End call triggers
  118. }
  119. }
  120. // Commit or rollback
  121. if ($error)
  122. {
  123. foreach($this->errors as $errmsg)
  124. {
  125. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  126. $this->error.=($this->error?', '.$errmsg:$errmsg);
  127. }
  128. $this->db->rollback();
  129. return -1*$error;
  130. }
  131. else
  132. {
  133. $this->db->commit();
  134. return $this->id;
  135. }
  136. }
  137. /**
  138. * Load object in memory from database
  139. *
  140. * @param int $id Id object
  141. * @return int <0 if KO, >0 if OK
  142. */
  143. function fetch($id)
  144. {
  145. global $langs;
  146. $sql = "SELECT";
  147. $sql.= " t.rowid,";
  148. $sql.= " t.fk_projet,";
  149. $sql.= " t.fk_task_parent,";
  150. $sql.= " t.label,";
  151. $sql.= " t.description,";
  152. $sql.= " t.duration_effective,";
  153. $sql.= " t.dateo,";
  154. $sql.= " t.datee,";
  155. $sql.= " t.fk_user_creat,";
  156. $sql.= " t.fk_user_valid,";
  157. $sql.= " t.fk_statut,";
  158. $sql.= " t.progress,";
  159. $sql.= " t.priority,";
  160. $sql.= " t.note_private,";
  161. $sql.= " t.note_public";
  162. $sql.= " FROM ".MAIN_DB_PREFIX."projet_task as t";
  163. $sql.= " WHERE t.rowid = ".$id;
  164. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  165. $resql=$this->db->query($sql);
  166. if ($resql)
  167. {
  168. if ($this->db->num_rows($resql))
  169. {
  170. $obj = $this->db->fetch_object($resql);
  171. $this->id = $obj->rowid;
  172. $this->ref = $obj->rowid;
  173. $this->fk_project = $obj->fk_projet;
  174. $this->fk_task_parent = $obj->fk_task_parent;
  175. $this->label = $obj->label;
  176. $this->description = $obj->description;
  177. $this->duration_effective = $obj->duration_effective;
  178. $this->date_c = $this->db->jdate($obj->datec);
  179. $this->date_start = $this->db->jdate($obj->dateo);
  180. $this->date_end = $this->db->jdate($obj->datee);
  181. $this->fk_user_creat = $obj->fk_user_creat;
  182. $this->fk_user_valid = $obj->fk_user_valid;
  183. $this->fk_statut = $obj->fk_statut;
  184. $this->progress = $obj->progress;
  185. $this->priority = $obj->priority;
  186. $this->note_private = $obj->note_private;
  187. $this->note_public = $obj->note_public;
  188. }
  189. $this->db->free($resql);
  190. return 1;
  191. }
  192. else
  193. {
  194. $this->error="Error ".$this->db->lasterror();
  195. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  196. return -1;
  197. }
  198. }
  199. /**
  200. * Update database
  201. *
  202. * @param User $user User that modify
  203. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  204. * @return int <0 if KO, >0 if OK
  205. */
  206. function update($user=0, $notrigger=0)
  207. {
  208. global $conf, $langs;
  209. $error=0;
  210. // Clean parameters
  211. if (isset($this->fk_project)) $this->fk_project=trim($this->fk_project);
  212. if (isset($this->fk_task_parent)) $this->fk_task_parent=trim($this->fk_task_parent);
  213. if (isset($this->label)) $this->label=trim($this->label);
  214. if (isset($this->description)) $this->description=trim($this->description);
  215. if (isset($this->duration_effective)) $this->duration_effective=trim($this->duration_effective);
  216. // Check parameters
  217. // Put here code to add control on parameters values
  218. // Update request
  219. $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task SET";
  220. $sql.= " fk_projet=".(isset($this->fk_project)?$this->fk_project:"null").",";
  221. $sql.= " fk_task_parent=".(isset($this->fk_task_parent)?$this->fk_task_parent:"null").",";
  222. $sql.= " label=".(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").",";
  223. $sql.= " description=".(isset($this->description)?"'".$this->db->escape($this->description)."'":"null").",";
  224. $sql.= " duration_effective=".(isset($this->duration_effective)?$this->duration_effective:"null").",";
  225. $sql.= " dateo=".($this->date_start!=''?$this->db->idate($this->date_start):'null').",";
  226. $sql.= " datee=".($this->date_end!=''?$this->db->idate($this->date_end):'null').",";
  227. $sql.= " progress=".$this->progress;
  228. $sql.= " WHERE rowid=".$this->id;
  229. $this->db->begin();
  230. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  231. $resql = $this->db->query($sql);
  232. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  233. if (! $error)
  234. {
  235. if (! $notrigger)
  236. {
  237. // Call triggers
  238. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  239. $interface=new Interfaces($this->db);
  240. $result=$interface->run_triggers('TASK_MODIFY',$this,$user,$langs,$conf);
  241. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  242. // End call triggers
  243. }
  244. }
  245. // Commit or rollback
  246. if ($error)
  247. {
  248. foreach($this->errors as $errmsg)
  249. {
  250. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  251. $this->error.=($this->error?', '.$errmsg:$errmsg);
  252. }
  253. $this->db->rollback();
  254. return -1*$error;
  255. }
  256. else
  257. {
  258. $this->db->commit();
  259. return 1;
  260. }
  261. }
  262. /**
  263. * Delete object in database
  264. *
  265. * @param User $user User that delete
  266. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  267. * @return int <0 if KO, >0 if OK
  268. */
  269. function delete($user, $notrigger=0)
  270. {
  271. global $conf, $langs;
  272. $error=0;
  273. $this->db->begin();
  274. if ($this->hasChildren() > 0)
  275. {
  276. dol_syslog(get_class($this)."::delete Can't delete record as it has some child", LOG_WARNING);
  277. $this->error='ErrorRecordHasChildren';
  278. $this->db->rollback();
  279. return 0;
  280. }
  281. if (! $error)
  282. {
  283. // Delete linked contacts
  284. $res = $this->delete_linked_contact();
  285. if ($res < 0)
  286. {
  287. $this->error='ErrorFailToDeleteLinkedContact';
  288. //$error++;
  289. $this->db->rollback();
  290. return 0;
  291. }
  292. }
  293. // Delete rang of line
  294. //$this->delRangOfLine($this->id, $this->element);
  295. $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task";
  296. $sql.= " WHERE rowid=".$this->id;
  297. dol_syslog(get_class($this)."::delete sql=".$sql);
  298. $resql = $this->db->query($sql);
  299. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  300. if (! $error)
  301. {
  302. if (! $notrigger)
  303. {
  304. // Call triggers
  305. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  306. $interface=new Interfaces($this->db);
  307. $result=$interface->run_triggers('TASK_DELETE',$this,$user,$langs,$conf);
  308. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  309. // End call triggers
  310. }
  311. }
  312. // Commit or rollback
  313. if ($error)
  314. {
  315. foreach($this->errors as $errmsg)
  316. {
  317. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  318. $this->error.=($this->error?', '.$errmsg:$errmsg);
  319. }
  320. $this->db->rollback();
  321. return -1*$error;
  322. }
  323. else
  324. {
  325. $this->db->commit();
  326. return 1;
  327. }
  328. }
  329. /**
  330. * Return nb of children
  331. *
  332. * @return int <0 if KO, 0 if no children, >0 if OK
  333. */
  334. function hasChildren()
  335. {
  336. $ret=0;
  337. $sql = "SELECT COUNT(*) as nb";
  338. $sql.= " FROM ".MAIN_DB_PREFIX."projet_task";
  339. $sql.= " WHERE fk_task_parent=".$this->id;
  340. dol_syslog(get_class($this)."::hasChildren sql=".$sql, LOG_DEBUG);
  341. $resql = $this->db->query($sql);
  342. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  343. else
  344. {
  345. $obj=$this->db->fetch_object($resql);
  346. if ($obj) $ret=$obj->nb;
  347. }
  348. if (! $error)
  349. {
  350. return $ret;
  351. }
  352. else
  353. {
  354. return -1;
  355. }
  356. }
  357. /**
  358. * Renvoie nom clicable (avec eventuellement le picto)
  359. *
  360. * @param int $withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  361. * @param int $option Sur quoi pointe le lien
  362. * @return string Chaine avec URL
  363. */
  364. function getNomUrl($withpicto=0,$option='')
  365. {
  366. global $langs;
  367. $result='';
  368. $lien = '<a href="'.DOL_URL_ROOT.'/projet/tasks/task.php?id='.$this->id.'">';
  369. $lienfin='</a>';
  370. $picto='projecttask';
  371. $label=$langs->trans("ShowTask").': '.$this->ref.($this->label?' - '.$this->label:'');
  372. if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
  373. if ($withpicto && $withpicto != 2) $result.=' ';
  374. if ($withpicto != 2) $result.=$lien.$this->ref.$lienfin;
  375. return $result;
  376. }
  377. /**
  378. * Initialise an instance with random values.
  379. * Used to build previews or test instances.
  380. * id must be 0 if object instance is a specimen.
  381. *
  382. * @return void
  383. */
  384. function initAsSpecimen()
  385. {
  386. $this->id=0;
  387. $this->fk_projet='';
  388. $this->fk_task_parent='';
  389. $this->title='';
  390. $this->duration_effective='';
  391. $this->fk_user_creat='';
  392. $this->statut='';
  393. $this->note='';
  394. }
  395. /**
  396. * Return list of tasks for all projects or for one particular project
  397. * Sort order is on project, TODO then of position of task, and last on title of first level task
  398. *
  399. * @param User $usert Object user to limit tasks affected to a particular user
  400. * @param User $userp Object user to limit projects of a particular user and public projects
  401. * @param int $projectid Project id
  402. * @param int $socid Third party id
  403. * @param int $mode 0=Return list of tasks and their projects, 1=Return projects and tasks if exists
  404. * @return array Array of tasks
  405. */
  406. function getTasksArray($usert=0, $userp=0, $projectid=0, $socid=0, $mode=0)
  407. {
  408. global $conf;
  409. $tasks = array();
  410. //print $usert.'-'.$userp.'-'.$projectid.'-'.$socid.'-'.$mode.'<br>';
  411. // List of tasks (does not care about permissions. Filtering will be done later)
  412. $sql = "SELECT p.rowid as projectid, p.ref, p.title as plabel, p.public,";
  413. $sql.= " t.rowid as taskid, t.label, t.description, t.fk_task_parent, t.duration_effective, t.progress,";
  414. $sql.= " t.dateo as date_start, t.datee as date_end";
  415. if ($mode == 0)
  416. {
  417. $sql.= " FROM ".MAIN_DB_PREFIX."projet as p";
  418. $sql.= ", ".MAIN_DB_PREFIX."projet_task as t";
  419. $sql.= " WHERE t.fk_projet = p.rowid";
  420. $sql.= " AND p.entity = ".$conf->entity;
  421. if ($socid) $sql.= " AND p.fk_soc = ".$socid;
  422. if ($projectid) $sql.= " AND p.rowid in (".$projectid.")";
  423. }
  424. if ($mode == 1)
  425. {
  426. $sql.= " FROM ".MAIN_DB_PREFIX."projet as p";
  427. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task as t on t.fk_projet = p.rowid";
  428. $sql.= " WHERE p.entity = ".$conf->entity;
  429. if ($socid) $sql.= " AND p.fk_soc = ".$socid;
  430. if ($projectid) $sql.= " AND p.rowid in (".$projectid.")";
  431. }
  432. $sql.= " ORDER BY p.ref, t.label";
  433. //print $sql;
  434. dol_syslog("Task::getTasksArray sql=".$sql, LOG_DEBUG);
  435. $resql = $this->db->query($sql);
  436. if ($resql)
  437. {
  438. $num = $this->db->num_rows($resql);
  439. $i = 0;
  440. // Loop on each record found, so each couple (project id, task id)
  441. while ($i < $num)
  442. {
  443. $error=0;
  444. $obj = $this->db->fetch_object($resql);
  445. if ((! $obj->public) && (is_object($userp))) // If not public project and we ask a filter on project owned by a user
  446. {
  447. if (! $this->getUserRolesForProjectsOrTasks($userp, 0, $obj->projectid, 0))
  448. {
  449. $error++;
  450. }
  451. }
  452. if (is_object($usert)) // If we ask a filter on a user affected to a task
  453. {
  454. if (! $this->getUserRolesForProjectsOrTasks(0, $usert, $obj->projectid, $obj->taskid))
  455. {
  456. $error++;
  457. }
  458. }
  459. if (! $error)
  460. {
  461. $tasks[$i]->id = $obj->taskid;
  462. $tasks[$i]->ref = $obj->taskid;
  463. $tasks[$i]->fk_project = $obj->projectid;
  464. $tasks[$i]->projectref = $obj->ref;
  465. $tasks[$i]->projectlabel = $obj->plabel;
  466. $tasks[$i]->label = $obj->label;
  467. $tasks[$i]->description = $obj->description;
  468. $tasks[$i]->fk_parent = $obj->fk_task_parent;
  469. $tasks[$i]->duration = $obj->duration_effective;
  470. $tasks[$i]->progress = $obj->progress;
  471. $tasks[$i]->public = $obj->public;
  472. $tasks[$i]->date_start = $this->db->jdate($obj->date_start);
  473. $tasks[$i]->date_end = $this->db->jdate($obj->date_end);
  474. }
  475. $i++;
  476. }
  477. $this->db->free($resql);
  478. }
  479. else
  480. {
  481. dol_print_error($this->db);
  482. }
  483. return $tasks;
  484. }
  485. /**
  486. * Return list of roles for a user for each projects or each tasks (or a particular project or task).
  487. *
  488. * @param User $userp Return roles on project for this internal user (task id can't be defined)
  489. * @param User $usert Return roles on task for this internal user
  490. * @param int $projectid Project id list separated with , to filter on project
  491. * @param int $taskid Task id to filter on a task
  492. * @return array Array (projectid => 'list of roles for project' or taskid => 'list of roles for task')
  493. */
  494. function getUserRolesForProjectsOrTasks($userp,$usert,$projectid='',$taskid=0)
  495. {
  496. $arrayroles = array();
  497. dol_syslog("Task::getUserRolesForProjectsOrTasks userp=".is_object($userp)." usert=".is_object($usert)." projectid=".$projectid." taskid=".$taskid);
  498. // We want role of user for a projet or role of user for a task. Both are not possible.
  499. if (empty($userp) && empty($usert))
  500. {
  501. $this->error="CallWithWrongParameters";
  502. return -1;
  503. }
  504. if (! empty($userp) && ! empty($usert))
  505. {
  506. $this->error="CallWithWrongParameters";
  507. return -1;
  508. }
  509. /* Liste des taches et role sur les projets ou taches */
  510. $sql = "SELECT pt.rowid as pid, ec.element_id, ctc.code, ctc.source";
  511. if ($userp) $sql.= " FROM ".MAIN_DB_PREFIX."projet as pt";
  512. if ($usert) $sql.= " FROM ".MAIN_DB_PREFIX."projet_task as pt";
  513. $sql.= ", ".MAIN_DB_PREFIX."element_contact as ec";
  514. $sql.= ", ".MAIN_DB_PREFIX."c_type_contact as ctc";
  515. $sql.= " WHERE pt.rowid = ec.element_id";
  516. if ($userp) $sql.= " AND ctc.element = 'project'";
  517. if ($usert) $sql.= " AND ctc.element = 'project_task'";
  518. $sql.= " AND ctc.rowid = ec.fk_c_type_contact";
  519. if ($userp) $sql.= " AND ec.fk_socpeople = ".$userp->id;
  520. if ($usert) $sql.= " AND ec.fk_socpeople = ".$usert->id;
  521. $sql.= " AND ec.statut = 4";
  522. $sql.= " AND ctc.source = 'internal'";
  523. if ($projectid)
  524. {
  525. if ($userp) $sql.= " AND pt.rowid in (".$projectid.")";
  526. if ($usert) $sql.= " AND pt.fk_projet in (".$projectid.")";
  527. }
  528. if ($taskid)
  529. {
  530. if ($userp) $sql.= " ERROR SHOULD NOT HAPPENS";
  531. if ($usert) $sql.= " AND pt.rowid = ".$taskid;
  532. }
  533. //print $sql;
  534. dol_syslog("Task::getUserRolesForProjectsOrTasks sql=".$sql);
  535. $resql = $this->db->query($sql);
  536. if ($resql)
  537. {
  538. $num = $this->db->num_rows($resql);
  539. $i = 0;
  540. while ($i < $num)
  541. {
  542. $obj = $this->db->fetch_object($resql);
  543. if (empty($arrayroles[$obj->pid])) $arrayroles[$obj->pid] = $obj->code;
  544. else $arrayroles[$obj->pid].=','.$obj->code;
  545. $i++;
  546. }
  547. $this->db->free($resql);
  548. }
  549. else
  550. {
  551. dol_print_error($this->db);
  552. }
  553. return $arrayroles;
  554. }
  555. /**
  556. * Return list of id of contacts of task
  557. *
  558. * @param string $source Source
  559. * @return array Array of id of contacts
  560. */
  561. function getListContactId($source='internal')
  562. {
  563. $contactAlreadySelected = array();
  564. $tab = $this->liste_contact(-1,$source);
  565. //var_dump($tab);
  566. $num=count($tab);
  567. $i = 0;
  568. while ($i < $num)
  569. {
  570. if ($source == 'thirdparty') $contactAlreadySelected[$i] = $tab[$i]['socid'];
  571. else $contactAlreadySelected[$i] = $tab[$i]['id'];
  572. $i++;
  573. }
  574. return $contactAlreadySelected;
  575. }
  576. /**
  577. * Add time spent
  578. *
  579. * @param User $user user id
  580. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  581. * @return void
  582. */
  583. function addTimeSpent($user, $notrigger=0)
  584. {
  585. global $conf,$langs;
  586. $ret = 0;
  587. // Clean parameters
  588. if (isset($this->timespent_note)) $this->timespent_note = trim($this->timespent_note);
  589. $sql = "INSERT INTO ".MAIN_DB_PREFIX."projet_task_time (";
  590. $sql.= "fk_task";
  591. $sql.= ", task_date";
  592. $sql.= ", task_duration";
  593. $sql.= ", fk_user";
  594. $sql.= ", note";
  595. $sql.= ") VALUES (";
  596. $sql.= $this->id;
  597. $sql.= ", '".$this->db->idate($this->timespent_date)."'";
  598. $sql.= ", ".$this->timespent_duration;
  599. $sql.= ", ".$this->timespent_fk_user;
  600. $sql.= ", ".(isset($this->timespent_note)?"'".$this->db->escape($this->timespent_note)."'":"null");
  601. $sql.= ")";
  602. dol_syslog(get_class($this)."::addTimeSpent sql=".$sql, LOG_DEBUG);
  603. if ($this->db->query($sql) )
  604. {
  605. $task_id = $this->db->last_insert_id(MAIN_DB_PREFIX."projet_task_time");
  606. $ret = $task_id;
  607. if (! $notrigger)
  608. {
  609. // Call triggers
  610. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  611. $interface=new Interfaces($this->db);
  612. $result=$interface->run_triggers('TASK_TIMESPENT_CREATE',$this,$user,$langs,$conf);
  613. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  614. // End call triggers
  615. }
  616. }
  617. else
  618. {
  619. $this->error=$this->db->lasterror();
  620. dol_syslog(get_class($this)."::addTimeSpent error -1 ".$this->error,LOG_ERR);
  621. $ret = -1;
  622. }
  623. if ($ret >= 0)
  624. {
  625. $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
  626. $sql.= " SET duration_effective = duration_effective + '".price2num($this->timespent_duration)."'";
  627. $sql.= " WHERE rowid = ".$this->id;
  628. dol_syslog(get_class($this)."::addTimeSpent sql=".$sql, LOG_DEBUG);
  629. if (! $this->db->query($sql) )
  630. {
  631. $this->error=$this->db->lasterror();
  632. dol_syslog(get_class($this)."::addTimeSpent error -2 ".$this->error, LOG_ERR);
  633. $ret = -2;
  634. }
  635. }
  636. return $ret;
  637. }
  638. /**
  639. * Load object in memory from database
  640. *
  641. * @param int $id Id object
  642. * @return int <0 if KO, >0 if OK
  643. */
  644. function fetchTimeSpent($id)
  645. {
  646. global $langs;
  647. $sql = "SELECT";
  648. $sql.= " t.rowid,";
  649. $sql.= " t.fk_task,";
  650. $sql.= " t.task_date,";
  651. $sql.= " t.task_duration,";
  652. $sql.= " t.fk_user,";
  653. $sql.= " t.note";
  654. $sql.= " FROM ".MAIN_DB_PREFIX."projet_task_time as t";
  655. $sql.= " WHERE t.rowid = ".$id;
  656. dol_syslog(get_class($this)."::fetchTimeSpent sql=".$sql, LOG_DEBUG);
  657. $resql=$this->db->query($sql);
  658. if ($resql)
  659. {
  660. if ($this->db->num_rows($resql))
  661. {
  662. $obj = $this->db->fetch_object($resql);
  663. $this->timespent_id = $obj->rowid;
  664. $this->id = $obj->fk_task;
  665. $this->timespent_date = $obj->task_date;
  666. $this->timespent_duration = $obj->task_duration;
  667. $this->timespent_user = $obj->fk_user;
  668. $this->timespent_note = $obj->note;
  669. }
  670. $this->db->free($resql);
  671. return 1;
  672. }
  673. else
  674. {
  675. $this->error="Error ".$this->db->lasterror();
  676. dol_syslog(get_class($this)."::fetchTimeSpent ".$this->error, LOG_ERR);
  677. return -1;
  678. }
  679. }
  680. /**
  681. * Update time spent
  682. *
  683. * @param User $user User id
  684. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  685. * @return int <0 if KO, >0 if OK
  686. */
  687. function updateTimeSpent($user, $notrigger=0)
  688. {
  689. $ret = 0;
  690. // Clean parameters
  691. if (isset($this->timespent_note)) $this->timespent_note = trim($this->timespent_note);
  692. $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task_time SET";
  693. $sql.= " task_date = '".$this->db->idate($this->timespent_date)."',";
  694. $sql.= " task_duration = ".$this->timespent_duration.",";
  695. $sql.= " fk_user = ".$this->timespent_fk_user.",";
  696. $sql.= " note = ".(isset($this->timespent_note)?"'".$this->db->escape($this->timespent_note)."'":"null");
  697. $sql.= " WHERE rowid = ".$this->timespent_id;
  698. dol_syslog(get_class($this)."::updateTimeSpent sql=".$sql, LOG_DEBUG);
  699. if ($this->db->query($sql) )
  700. {
  701. if (! $notrigger)
  702. {
  703. // Call triggers
  704. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  705. $interface=new Interfaces($this->db);
  706. $result=$interface->run_triggers('TASK_TIMESPENT_MODIFY',$this,$user,$langs,$conf);
  707. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  708. // End call triggers
  709. }
  710. $ret = 1;
  711. }
  712. else
  713. {
  714. $this->error=$this->db->lasterror();
  715. dol_syslog(get_class($this)."::updateTimeSpent error -1 ".$this->error,LOG_ERR);
  716. $ret = -1;
  717. }
  718. if ($ret == 1 && ($this->timespent_old_duration != $this->timespent_duration))
  719. {
  720. $newDuration = $this->timespent_duration - $this->timespent_old_duration;
  721. $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
  722. $sql.= " SET duration_effective = duration_effective + '".$newDuration."'";
  723. $sql.= " WHERE rowid = ".$this->id;
  724. dol_syslog(get_class($this)."::updateTimeSpent sql=".$sql, LOG_DEBUG);
  725. if (! $this->db->query($sql) )
  726. {
  727. $this->error=$this->db->lasterror();
  728. dol_syslog(get_class($this)."::addTimeSpent error -2 ".$this->error, LOG_ERR);
  729. $ret = -2;
  730. }
  731. }
  732. return $ret;
  733. }
  734. /**
  735. * Delete time spent
  736. *
  737. * @param User $user User that delete
  738. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  739. * @return int <0 if KO, >0 if OK
  740. */
  741. function delTimeSpent($user, $notrigger=0)
  742. {
  743. global $conf, $langs;
  744. $error=0;
  745. $this->db->begin();
  746. $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task_time";
  747. $sql.= " WHERE rowid = ".$this->timespent_id;
  748. dol_syslog(get_class($this)."::delTimeSpent sql=".$sql);
  749. $resql = $this->db->query($sql);
  750. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  751. if (! $error)
  752. {
  753. if (! $notrigger)
  754. {
  755. // Call triggers
  756. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  757. $interface=new Interfaces($this->db);
  758. $result=$interface->run_triggers('TASK_TIMESPENT_DELETE',$this,$user,$langs,$conf);
  759. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  760. // End call triggers
  761. }
  762. }
  763. if (! $error)
  764. {
  765. $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
  766. $sql.= " SET duration_effective = duration_effective - '".$this->timespent_duration."'";
  767. $sql.= " WHERE rowid = ".$this->id;
  768. dol_syslog(get_class($this)."::delTimeSpent sql=".$sql, LOG_DEBUG);
  769. if ($this->db->query($sql) )
  770. {
  771. $result = 0;
  772. }
  773. else
  774. {
  775. $this->error=$this->db->lasterror();
  776. dol_syslog(get_class($this)."::addTimeSpent error -3 ".$this->error, LOG_ERR);
  777. $result = -2;
  778. }
  779. }
  780. // Commit or rollback
  781. if ($error)
  782. {
  783. foreach($this->errors as $errmsg)
  784. {
  785. dol_syslog(get_class($this)."::delTimeSpent ".$errmsg, LOG_ERR);
  786. $this->error.=($this->error?', '.$errmsg:$errmsg);
  787. }
  788. $this->db->rollback();
  789. return -1*$error;
  790. }
  791. else
  792. {
  793. $this->db->commit();
  794. return 1;
  795. }
  796. }
  797. }
  798. ?>