computing.inc.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * Any extension to compute things like a stop watch deadline or working hours
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. /**
  25. * Metric computing for stop watches
  26. */
  27. interface iMetricComputer
  28. {
  29. public static function GetDescription();
  30. public function ComputeMetric($oObject);
  31. }
  32. /**
  33. * Working time computing for stop watches
  34. */
  35. interface iWorkingTimeComputer
  36. {
  37. public static function GetDescription();
  38. /**
  39. * Get the date/time corresponding to a given delay in the future from the present
  40. * considering only the valid (open) hours for a specified object
  41. * @param $oObject DBObject The object for which to compute the deadline
  42. * @param $iDuration integer The duration (in seconds) in the future
  43. * @param $oStartDate DateTime The starting point for the computation
  44. * @return DateTime The date/time for the deadline
  45. */
  46. public function GetDeadline($oObject, $iDuration, DateTime $oStartDate);
  47. /**
  48. * Get duration (considering only open hours) elapsed bewteen two given DateTimes
  49. * @param $oObject DBObject The object for which to compute the duration
  50. * @param $oStartDate DateTime The starting point for the computation (default = now)
  51. * @param $oEndDate DateTime The ending point for the computation (default = now)
  52. * @return integer The duration (number of seconds) of open hours elapsed between the two dates
  53. */
  54. public function GetOpenDuration($oObject, DateTime $oStartDate, DateTime $oEndDate);
  55. }
  56. /**
  57. * Default implementation oof deadline computing: NO deadline
  58. */
  59. class DefaultMetricComputer implements iMetricComputer
  60. {
  61. public static function GetDescription()
  62. {
  63. return "Null";
  64. }
  65. public function ComputeMetric($oObject)
  66. {
  67. return null;
  68. }
  69. }
  70. /**
  71. * Default implementation of working time computing
  72. */
  73. class DefaultWorkingTimeComputer implements iWorkingTimeComputer
  74. {
  75. public static function GetDescription()
  76. {
  77. return "24x7, no holidays";
  78. }
  79. /**
  80. * Get the date/time corresponding to a given delay in the future from the present
  81. * considering only the valid (open) hours for a specified object
  82. * @param $oObject DBObject The object for which to compute the deadline
  83. * @param $iDuration integer The duration (in seconds) in the future
  84. * @param $oStartDate DateTime The starting point for the computation
  85. * @return DateTime The date/time for the deadline
  86. */
  87. public function GetDeadline($oObject, $iDuration, DateTime $oStartDate)
  88. {
  89. if (class_exists('WorkingTimeRecorder'))
  90. {
  91. WorkingTimeRecorder::Trace(WorkingTimeRecorder::TRACE_DEBUG, __class__.'::'.__function__);
  92. }
  93. //echo "GetDeadline - default: ".$oStartDate->format('Y-m-d H:i:s')." + $iDuration<br/>\n";
  94. // Default implementation: 24x7, no holidays: to compute the deadline, just add
  95. // the specified duration to the given date/time
  96. $oResult = clone $oStartDate;
  97. $oResult->modify('+'.$iDuration.' seconds');
  98. if (class_exists('WorkingTimeRecorder'))
  99. {
  100. WorkingTimeRecorder::SetValues($oStartDate->format('U'), $oResult->format('U'), $iDuration, WorkingTimeRecorder::COMPUTED_END);
  101. }
  102. return $oResult;
  103. }
  104. /**
  105. * Get duration (considering only open hours) elapsed bewteen two given DateTimes
  106. * @param $oObject DBObject The object for which to compute the duration
  107. * @param $oStartDate DateTime The starting point for the computation (default = now)
  108. * @param $oEndDate DateTime The ending point for the computation (default = now)
  109. * @return integer The duration (number of seconds) of open hours elapsed between the two dates
  110. */
  111. public function GetOpenDuration($oObject, DateTime $oStartDate, DateTime $oEndDate)
  112. {
  113. if (class_exists('WorkingTimeRecorder'))
  114. {
  115. WorkingTimeRecorder::Trace(WorkingTimeRecorder::TRACE_DEBUG, __class__.'::'.__function__);
  116. }
  117. //echo "GetOpenDuration - default: ".$oStartDate->format('Y-m-d H:i:s')." to ".$oEndDate->format('Y-m-d H:i:s')."<br/>\n";
  118. $iDuration = abs($oEndDate->format('U') - $oStartDate->format('U'));
  119. if (class_exists('WorkingTimeRecorder'))
  120. {
  121. WorkingTimeRecorder::SetValues($oStartDate->format('U'), $oEndDate->format('U'), $iDuration, WorkingTimeRecorder::COMPUTED_DURATION);
  122. }
  123. return $iDuration;
  124. }
  125. }
  126. ?>