api.taskmantracking.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Tasks execution tracking report
  4. */
  5. class TaskmanTracking {
  6. /**
  7. * Contains currently tracked tasks as taskid=>trackid
  8. *
  9. * @var array
  10. */
  11. protected $trackingTasks = array();
  12. /**
  13. * Contains all of available taskman tasks as id=>taskdata
  14. *
  15. * @var array
  16. */
  17. protected $allTasks = array();
  18. /**
  19. * Messages object placeholder
  20. *
  21. * @var object
  22. */
  23. protected $messages = '';
  24. /**
  25. * Available jobtypes as jobtypeid=>name
  26. *
  27. * @var array
  28. */
  29. protected $allJobtypes = array();
  30. /**
  31. * Available active and inactive employee
  32. *
  33. * @var array
  34. */
  35. protected $allEmployee = array();
  36. /**
  37. * Contains current administrators login
  38. *
  39. * @var string
  40. */
  41. protected $myLogin = '';
  42. /**
  43. * Tracked tasks database abstraction layer
  44. *
  45. * @var object
  46. */
  47. protected $trackDb = '';
  48. public function __construct() {
  49. $this->setLogin();
  50. $this->initMessages();
  51. $this->initDbs();
  52. $this->loadTrackedTasks();
  53. $this->loadAllTasks();
  54. $this->loadEmployee();
  55. $this->loadJobtypes();
  56. }
  57. /**
  58. * Sets current administrator login, for further usage
  59. *
  60. * @return void
  61. */
  62. protected function setLogin() {
  63. $this->myLogin = ubRouting::filters(whoami(), 'login');
  64. }
  65. /**
  66. * Inits messages helper object
  67. *
  68. * @return void
  69. */
  70. protected function initMessages() {
  71. $this->messages = new UbillingMessageHelper();
  72. }
  73. /**
  74. * Inits required database abstraction layers
  75. *
  76. * @return void
  77. */
  78. protected function initDbs() {
  79. $this->trackDb = new NyanORM('taskmantrack');
  80. }
  81. /**
  82. * Loads all of employees from database
  83. *
  84. * @return void
  85. */
  86. protected function loadEmployee() {
  87. $this->allEmployee = ts_GetAllEmployee();
  88. }
  89. /**
  90. * Loads available jobtypes from database
  91. *
  92. * @return void
  93. */
  94. protected function loadJobtypes() {
  95. $this->allJobtypes = ts_GetAllJobtypes();
  96. }
  97. /**
  98. * Loads existing tasks tracks from database where
  99. *
  100. * @return void
  101. */
  102. protected function loadTrackedTasks() {
  103. $this->trackDb->where('admin', '=', $this->myLogin);
  104. $this->trackingTasks = $this->trackDb->getAll('taskid');
  105. }
  106. /**
  107. * Loads all of existing taskman tasks into protected property
  108. *
  109. * @return void
  110. */
  111. protected function loadAllTasks() {
  112. $this->allTasks = ts_GetAllTasks();
  113. }
  114. /**
  115. * Sets some task as tracked
  116. *
  117. * @param int $taskid
  118. *
  119. * @return bool
  120. */
  121. public function setTaskTracked($taskid) {
  122. $result = true;
  123. $taskid = ubRouting::filters($taskid, 'int');
  124. if (isset($this->allTasks[$taskid])) {
  125. if (!isset($this->trackingTasks[$taskid])) {
  126. $this->trackDb->data('taskid', $taskid);
  127. $this->trackDb->data('admin', $this->myLogin);
  128. $this->trackDb->create();
  129. log_register('TASKMAN TRACK [' . $taskid . ']');
  130. }
  131. } else {
  132. $result = false;
  133. }
  134. return ($result);
  135. }
  136. /**
  137. * Sets some task as untracked
  138. *
  139. * @param int $taskid
  140. *
  141. * @return void
  142. */
  143. public function setTaskUntracked($taskid) {
  144. $taskid = ubRouting::filters($taskid, 'int');
  145. $this->trackDb->where('taskid', '=', $taskid);
  146. $this->trackDb->delete();
  147. log_register('TASKMAN UNTRACK [' . $taskid . ']');
  148. }
  149. /**
  150. * Renders list of currently tracked tasks
  151. *
  152. * @return string
  153. */
  154. public function render() {
  155. $result = '';
  156. if (!empty($this->trackingTasks)) {
  157. $cells = wf_TableCell(__('ID'));
  158. $cells .= wf_TableCell(__('Address'));
  159. $cells .= wf_TableCell(__('Job type'));
  160. $cells .= wf_TableCell(__('Phone'));
  161. $cells .= wf_TableCell(__('Who should do'));
  162. $cells .= wf_TableCell(__('Worker done'));
  163. $cells .= wf_TableCell(__('Target date'));
  164. $cells .= wf_TableCell(__('Finish date'));
  165. $cells .= wf_TableCell(__('Time'));
  166. $cells .= wf_TableCell(__('Actions'));
  167. $rows = wf_TableRow($cells, 'row1');
  168. foreach ($this->trackingTasks as $taskid => $trackid) {
  169. if (isset($this->allTasks[$taskid])) {
  170. $taskData = $this->allTasks[$taskid];
  171. $taskCreationTime = strtotime($taskData['date']);
  172. $taskEndTime = ($taskData['status']) ? strtotime($taskData['enddate']) : time();
  173. $timeSpent = $taskEndTime - $taskCreationTime;
  174. $rowStyle = ($taskData['status']) ? 'donetask' : 'undone';
  175. $cells = wf_TableCell($taskid);
  176. $cells .= wf_TableCell($taskData['address']);
  177. $cells .= wf_TableCell(@$this->allJobtypes[$taskData['jobtype']]);
  178. $cells .= wf_TableCell($taskData['phone']);
  179. $cells .= wf_TableCell(@$this->allEmployee[$taskData['employee']]);
  180. $cells .= wf_TableCell(@$this->allEmployee[$taskData['employeedone']]);
  181. $cells .= wf_TableCell($taskData['startdate']);
  182. $cells .= wf_TableCell($taskData['enddate']);
  183. $cells .= wf_TableCell(zb_formatTimeDays($timeSpent));
  184. $actLinks = wf_Link('?module=taskman&edittask=' . $taskid, web_edit_icon()) . ' ';
  185. $actLinks .= wf_JSAlert('?module=taskmantrack&untrackid=' . $taskid, wf_img('skins/icon_cleanup.png', __('Stop tracking this task')), $this->messages->getEditAlert());
  186. $cells .= wf_TableCell($actLinks);
  187. $rows .= wf_TableRow($cells, $rowStyle);
  188. } else {
  189. //task is deleted
  190. }
  191. }
  192. $result = wf_TableBody($rows, '100%', 0, 'sortable');
  193. } else {
  194. $result = $this->messages->getStyledMessage(__('You are currently not tracking any tasks'), 'info');
  195. }
  196. return ($result);
  197. }
  198. }