diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-03-15 19:42:49 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-03-15 19:42:49 -0400 |
commit | 3c8736c1fe593fe2ea30b09fa99ec2e34ca8c65a (patch) | |
tree | 16ec59c17982648939155cedecf817a50781001d | |
parent | 084272c60ea97f3a835cfccbb3303227d00085e8 (diff) |
Calculate intersection between time tracking and timetable
-rw-r--r-- | app/Controller/Calendar.php | 6 | ||||
-rw-r--r-- | app/Model/SubtaskTimeTracking.php | 2 | ||||
-rw-r--r-- | app/Model/Timetable.php | 76 |
3 files changed, 80 insertions, 4 deletions
diff --git a/app/Controller/Calendar.php b/app/Controller/Calendar.php index 1c7ac7c0..2a11edab 100644 --- a/app/Controller/Calendar.php +++ b/app/Controller/Calendar.php @@ -14,7 +14,7 @@ use Model\Task as TaskModel; class Calendar extends Base { /** - * Show calendar view + * Show calendar view for projects * * @access public */ @@ -59,9 +59,7 @@ class Calendar extends Base ->filterByDueDateRange($start, $end) ->toCalendarEvents(); - $subtask_timeslots = $this->subtaskTimeTracking->getProjectCalendarEvents($project_id, $start, $end); - - $this->response->json(array_merge($due_tasks, $subtask_timeslots)); + $this->response->json($due_tasks); } /** diff --git a/app/Model/SubtaskTimeTracking.php b/app/Model/SubtaskTimeTracking.php index ab0d7c54..a984533f 100644 --- a/app/Model/SubtaskTimeTracking.php +++ b/app/Model/SubtaskTimeTracking.php @@ -138,6 +138,8 @@ class SubtaskTimeTracking extends Base ->addCondition($this->getCalendarCondition($start, $end)) ->findAll(); + $result = $this->timetable->calculateEventsIntersect($user_id, $result, $start, $end); + return $this->toCalendarEvents($result); } diff --git a/app/Model/Timetable.php b/app/Model/Timetable.php index 205c2c20..da2ec10c 100644 --- a/app/Model/Timetable.php +++ b/app/Model/Timetable.php @@ -25,6 +25,82 @@ class Timetable extends Base private $timeoff; /** + * Get a set of events by using the intersection between the timetable and the time tracking data + * + * @access public + * @param integer $user_id + * @param array $events Time tracking data + * @param string $start ISO8601 date + * @param string $end ISO8601 date + * @return array + */ + public function calculateEventsIntersect($user_id, array $events, $start, $end) + { + $start_dt = new DateTime($start); + $start_dt->setTime(0, 0); + + $end_dt = new DateTime($end); + $end_dt->setTime(23, 59); + + $timetable = $this->calculate($user_id, $start_dt, $end_dt); + + // The user has no timetable + if (empty($this->week)) { + return $events; + } + + $results = array(); + + foreach ($events as $event) { + $results = array_merge($results, $this->calculateEventIntersect($event, $timetable)); + } + + return $results; + } + + /** + * Get a serie of events based on the timetable and the provided event + * + * @access public + * @param integer $user_id + * @param array $events Time tracking data + * @param string $start ISO8601 date + * @param string $end ISO8601 date + * @return array + */ + public function calculateEventIntersect(array $event, array $timetable) + { + $events = array(); + + foreach ($timetable as $slot) { + + $start_ts = $slot[0]->getTimestamp(); + $end_ts = $slot[1]->getTimestamp(); + + if ($start_ts > $event['end']) { + break; + } + + if ($event['start'] <= $start_ts) { + $event['start'] = $start_ts; + } + + if ($event['start'] >= $start_ts && $event['start'] <= $end_ts) { + + if ($event['end'] >= $end_ts) { + $events[] = array_merge($event, array('end' => $end_ts)); + } + else { + $events[] = $event; + break; + } + } + } + + return $events; + } + + /** * Calculate effective worked hours by taking into consideration the timetable * * @access public |