main.itop-sla-computation.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Module itop-sla-computation: implements an extensible mechanism
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. /**
  25. * Static class that implements the public interface for utilities
  26. * related to the SLA computation
  27. */
  28. class SLAComputation
  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, '$sModuleName' 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. /**
  60. * Get the date/time corresponding to a given delay in the future from the present
  61. * considering only the valid (open) hours for a specified ticket
  62. * @param $oTicket Ticket The ticket for which to compute the deadline
  63. * @param $iDuration integer The duration (in seconds) in the future
  64. * @param $oStartDate DateTime The starting point for the computation (default = now)
  65. * @return DateTime The date/time for the deadline
  66. */
  67. public static function GetDeadline($oTicket, $iDuration, $oStartDate = null)
  68. {
  69. if ($oStartDate == null)
  70. {
  71. $oStartDate = new DateTime();
  72. }
  73. return self::$m_oAddOn->GetDeadline($oTicket, $iDuration, $oStartDate);
  74. }
  75. /**
  76. * Get duration (considering only open hours) elapsed bewteen two given DateTimes
  77. * @param $oTicket Ticket The ticket for which to compute the deadline
  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 static function GetOpenDuration($oTicket, DateTime $oStartDate, DateTime $oEndDate)
  83. {
  84. return self::$m_oAddOn->GetOpenDuration($oTicket, $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. ?>