main.itop-sla-computation.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. // Copyright (C) 2010-2012 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. * Module itop-sla-computation: implements an extensible mechanism
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. /**
  25. * Implements the public interface for utilities
  26. * related to the SLA computation
  27. */
  28. class SLAComputation implements iWorkingTimeComputer
  29. {
  30. protected static $m_oAddOn;
  31. /**
  32. * Generic "extensibility" method: select which extension is actually used
  33. * @param $sClassName string The name of the class (derived from SLAComputationAddOnAPI) to use
  34. * @return void
  35. */
  36. public static function SelectModule($sClassName)
  37. {
  38. if (!class_exists($sClassName))
  39. {
  40. throw new CoreException("Could not select this module, '$sClassName' in not a valid class name");
  41. return;
  42. }
  43. if (($sClassName != 'SLAComputationAddOnAPI') && !is_subclass_of($sClassName, 'SLAComputationAddOnAPI'))
  44. {
  45. throw new CoreException("Could not select this module, the class '$sClassName' is not derived from SLAComputationAddOnAPI (parent class:".get_parent_class($sClassName)." )");
  46. return;
  47. }
  48. self::$m_oAddOn = new $sClassName;
  49. self::$m_oAddOn->Init();
  50. }
  51. /**
  52. * Get the class of the extension actually used
  53. * @return string The name of the extension class used
  54. */
  55. public static function GetModuleInstance()
  56. {
  57. return self::$m_oAddOn;
  58. }
  59. public static function GetDescription()
  60. {
  61. return "SLA computation (depends on the installed module)";
  62. }
  63. /**
  64. * Get the date/time corresponding to a given delay in the future from the present
  65. * considering only the valid (open) hours for a specified object
  66. * @param $oObject DBObject The object for which to compute the deadline
  67. * @param $iDuration integer The duration (in seconds) in the future
  68. * @param $oStartDate DateTime The starting point for the computation
  69. * @return DateTime The date/time for the deadline
  70. */
  71. public function GetDeadline($oObject, $iDuration, DateTime $oStartDate)
  72. {
  73. return self::$m_oAddOn->GetDeadline($oObject, $iDuration, $oStartDate);
  74. }
  75. /**
  76. * Get duration (considering only open hours) elapsed bewteen two given DateTimes
  77. * @param $oObject DBObject The object for which to compute the duration
  78. * @param $oStartDate DateTime The starting point for the computation (default = now)
  79. * @param $oEndDate DateTime The ending point for the computation (default = now)
  80. * @return integer The duration (number of seconds) of open hours elapsed between the two dates
  81. */
  82. public function GetOpenDuration($oObject, DateTime $oStartDate, DateTime $oEndDate)
  83. {
  84. return self::$m_oAddOn->GetOpenDuration($oObject, $oStartDate, $oEndDate);
  85. }
  86. }
  87. /**
  88. * Base class for extensions to the SLA computation mechanism
  89. * This class implements a default behavior, suitable for a simple
  90. * 24x7 (no holiday) computation. To override this behavior, implement
  91. * a derived class from this one, overloading the behavior, and call
  92. * SLAComputation::SetExtension()
  93. */
  94. class SLAComputationAddOnAPI
  95. {
  96. /**
  97. * Called when the module is loaded, used for one time initialization (if needed)
  98. */
  99. public function Init()
  100. {
  101. }
  102. /**
  103. * Get the date/time corresponding to a given delay in the future from the present
  104. * considering only the valid (open) hours for a specified ticket
  105. * @param $oTicket Ticket The ticket for which to compute the deadline
  106. * @param $iDuration integer The duration (in seconds) in the future
  107. * @param $oStartDate DateTime The starting point for the computation
  108. * @return DateTime The date/time for the deadline
  109. */
  110. public static function GetDeadline($oTicket, $iDuration, DateTime $oStartDate)
  111. {
  112. // Default implementation: 24x7, no holidays: to compute the deadline, just add
  113. // the specified duration to the given date/time
  114. $oResult = clone $oStartDate;
  115. $oResult->modify('+'.$iDuration.' seconds');
  116. return $oResult;
  117. }
  118. /**
  119. * Get duration (considering only open hours) elapsed bewteen two given DateTimes
  120. * @param $oTicket Ticket The ticket for which to compute the duration
  121. * @param $oStartDate DateTime The starting point for the computation (default = now)
  122. * @param $oEndDate DateTime The ending point for the computation (default = now)
  123. * @return integer The duration (number of seconds) of open hours elapsed between the two dates
  124. */
  125. public static function GetOpenDuration($oTicket, DateTime $oStartDate, DateTime $oEndDate)
  126. {
  127. return abs($oEndDate->format('U') - $oStartDate->format('U'));
  128. }
  129. }
  130. SLAComputation::SelectModule('SLAComputationAddOnAPI');
  131. ?>