1<?php
2// Copyright (C) 2010-2014 Combodo SARL
3//
4//   This file is part of iTop.
5//
6//   iTop is free software; you can redistribute it and/or modify
7//   it under the terms of the GNU Affero General Public License as published by
8//   the Free Software Foundation, either version 3 of the License, or
9//   (at your option) any later version.
10//
11//   iTop is distributed in the hope that it will be useful,
12//   but WITHOUT ANY WARRANTY; without even the implied warranty of
13//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14//   GNU Affero General Public License for more details.
15//
16//   You should have received a copy of the GNU Affero General Public License
17//   along with iTop. If not, see <http://www.gnu.org/licenses/>
18
19
20/**
21 * Any extension to compute things like a stop watch deadline or working hours
22 *
23 * @copyright   Copyright (C) 2010-2012 Combodo SARL
24 * @license     http://opensource.org/licenses/AGPL-3.0
25 */
26
27/**
28 * Metric computing for stop watches
29 */
30interface iMetricComputer
31{
32	public static function GetDescription();
33	public function ComputeMetric($oObject);
34}
35
36/**
37 * Working time computing for stop watches
38 */
39interface iWorkingTimeComputer
40{
41	public static function GetDescription();
42
43	/**
44	 * Get the date/time corresponding to a given delay in the future from the present
45	 * considering only the valid (open) hours for a specified object
46	 * @param $oObject DBObject The object for which to compute the deadline
47	 * @param $iDuration integer The duration (in seconds) in the future
48	 * @param $oStartDate DateTime The starting point for the computation
49	 * @return DateTime The date/time for the deadline
50	 */
51	public function GetDeadline($oObject, $iDuration, DateTime $oStartDate);
52
53	/**
54	 * Get duration (considering only open hours) elapsed bewteen two given DateTimes
55	 * @param $oObject DBObject The object for which to compute the duration
56	 * @param $oStartDate DateTime The starting point for the computation (default = now)
57	 * @param $oEndDate DateTime The ending point for the computation (default = now)
58	 * @return integer The duration (number of seconds) of open hours elapsed between the two dates
59	 */
60	public function GetOpenDuration($oObject, DateTime $oStartDate, DateTime $oEndDate);
61}
62
63/**
64 * Default implementation oof deadline computing: NO deadline
65 */
66class DefaultMetricComputer implements iMetricComputer
67{
68	public static function GetDescription()
69	{
70		return "Null";
71	}
72
73	public function ComputeMetric($oObject)
74	{
75		return null;
76	}
77}
78
79/**
80 * Default implementation of working time computing
81 */
82class DefaultWorkingTimeComputer implements iWorkingTimeComputer
83{
84	public static function GetDescription()
85	{
86		return "24x7, no holidays";
87	}
88
89	/**
90	 * Get the date/time corresponding to a given delay in the future from the present
91	 * considering only the valid (open) hours for a specified object
92	 * @param $oObject DBObject The object for which to compute the deadline
93	 * @param $iDuration integer The duration (in seconds) in the future
94	 * @param $oStartDate DateTime The starting point for the computation
95	 * @return DateTime The date/time for the deadline
96	 */
97	public function GetDeadline($oObject, $iDuration, DateTime $oStartDate)
98	{
99		if (class_exists('WorkingTimeRecorder'))
100		{
101			WorkingTimeRecorder::Trace(WorkingTimeRecorder::TRACE_DEBUG, __class__.'::'.__function__);
102		}
103		//echo "GetDeadline - default: ".$oStartDate->format('Y-m-d H:i:s')." + $iDuration<br/>\n";
104		// Default implementation: 24x7, no holidays: to compute the deadline, just add
105		// the specified duration to the given date/time
106		$oResult = clone $oStartDate;
107		$oResult->modify('+'.$iDuration.' seconds');
108		if (class_exists('WorkingTimeRecorder'))
109		{
110			WorkingTimeRecorder::SetValues($oStartDate->format('U'), $oResult->format('U'), $iDuration, WorkingTimeRecorder::COMPUTED_END);
111		}
112		return $oResult;
113	}
114
115	/**
116	 * Get duration (considering only open hours) elapsed bewteen two given DateTimes
117	 * @param $oObject DBObject The object for which to compute the duration
118	 * @param $oStartDate DateTime The starting point for the computation (default = now)
119	 * @param $oEndDate DateTime The ending point for the computation (default = now)
120	 * @return integer The duration (number of seconds) of open hours elapsed between the two dates
121	 */
122	public function GetOpenDuration($oObject, DateTime $oStartDate, DateTime $oEndDate)
123	{
124		if (class_exists('WorkingTimeRecorder'))
125		{
126			WorkingTimeRecorder::Trace(WorkingTimeRecorder::TRACE_DEBUG, __class__.'::'.__function__);
127		}
128		//echo "GetOpenDuration - default: ".$oStartDate->format('Y-m-d H:i:s')." to ".$oEndDate->format('Y-m-d H:i:s')."<br/>\n";
129		$iDuration = abs($oEndDate->format('U') - $oStartDate->format('U'));
130		if (class_exists('WorkingTimeRecorder'))
131		{
132			WorkingTimeRecorder::SetValues($oStartDate->format('U'), $oEndDate->format('U'), $iDuration, WorkingTimeRecorder::COMPUTED_DURATION);
133		}
134		return $iDuration;
135	}
136}
137
138
139?>
140