PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/api/database/callback.class.php

https://github.com/patrickdemond/beartooth
PHP | 132 lines | 80 code | 13 blank | 39 comment | 12 complexity | 7ade9ef2676b0eebc7451f063650717e MD5 | raw file
  1. <?php
  2. /**
  3. * callback.class.php
  4. *
  5. * @author Patrick Emond <emondpd@mcmaster.ca>
  6. * @filesource
  7. */
  8. namespace beartooth\database;
  9. use cenozo\lib, cenozo\log, beartooth\util;
  10. /**
  11. * callback: record
  12. */
  13. class callback extends \cenozo\database\record
  14. {
  15. /**
  16. * Overrides the parent load method.
  17. * @author Patrick Emond
  18. * @access public
  19. */
  20. public function load()
  21. {
  22. parent::load();
  23. // callbacks are not to the second, so remove the :00 at the end of the datetime field
  24. $this->datetime = substr( $this->datetime, 0, -3 );
  25. }
  26. /**
  27. * Overrides the parent save method.
  28. * @author Patrick Emond
  29. * @access public
  30. */
  31. public function save()
  32. {
  33. // make sure there is a maximum of 1 unassigned callback
  34. if( is_null( $this->assignment_id ) )
  35. {
  36. $modifier = lib::create( 'database\modifier' );
  37. $modifier->where( 'participant_id', '=', $this->participant_id );
  38. $modifier->where( 'assignment_id', '=', NULL );
  39. if( !is_null( $this->id ) ) $modifier->where( 'id', '!=', $this->id );
  40. if( 0 < static::count( $modifier ) )
  41. throw lib::create( 'exception\runtime',
  42. 'Cannot have more than one unassigned callback per participant.', __METHOD__ );
  43. }
  44. parent::save();
  45. }
  46. /**
  47. * Get the state of the callback as a string:
  48. * reached: the callback was met and the participant was reached
  49. * not reached: the callback was met but the participant was not reached
  50. * upcoming: the callback's date/time has not yet occurred
  51. * assignable: the callback is ready to be assigned, but hasn't been
  52. * missed: the callback was missed (never assigned) and the call window has passed
  53. * incomplete: the callback was assigned but the assignment never closed (an error)
  54. * assigned: the callback is currently assigned
  55. * in progress: the callback is currently assigned and currently in a call
  56. * @author Patrick Emond <emondpd@mcmaster.ca>
  57. * @return string
  58. * @access public
  59. */
  60. public function get_state( $ignore_assignments = false )
  61. {
  62. if( is_null( $this->id ) )
  63. {
  64. log::warning( 'Tried to determine state for callback with no id.' );
  65. return NULL;
  66. }
  67. // if the callback's reached column is set, nothing else matters
  68. if( !is_null( $this->reached ) ) return $this->reached ? 'reached' : 'not reached';
  69. $db_participant = lib::create( 'database\participant', $this->participant_id );
  70. $db_site = $db_participant->get_effective_site();
  71. $status = 'unknown';
  72. // settings are in minutes, time() is in seconds, so multiply by 60
  73. $setting_manager = lib::create( 'business\setting_manager' );
  74. $pre_window_time = 60 * $setting_manager->get_setting(
  75. 'callback', 'call pre-window', $db_site );
  76. $now = util::get_datetime_object()->getTimestamp();
  77. $callback = util::get_datetime_object( $this->datetime )->getTimestamp();
  78. // get the status of the callback
  79. $db_assignment = $this->get_assignment();
  80. if( !$ignore_assignments && !is_null( $db_assignment ) )
  81. {
  82. if( !is_null( $db_assignment->end_datetime ) )
  83. { // assignment closed but callback never completed
  84. log::crit(
  85. sprintf( 'Callback %d has assignment which is closed but no status was set.',
  86. $this->id ) );
  87. $status = 'incomplete';
  88. }
  89. else // assignment active
  90. {
  91. $modifier = lib::create( 'database\modifier' );
  92. $modifier->where( 'end_datetime', '=', NULL );
  93. $open_phone_calls = $db_assignment->get_phone_call_count( $modifier );
  94. if( 0 < $open_phone_calls )
  95. { // assignment currently on call
  96. $status = "in progress";
  97. }
  98. else
  99. { // not on call
  100. $status = "assigned";
  101. }
  102. }
  103. }
  104. else if( $now < $callback - $pre_window_time )
  105. {
  106. $status = 'upcoming';
  107. }
  108. else
  109. {
  110. $status = 'assignable';
  111. }
  112. return $status;
  113. }
  114. }
  115. // define the join to the participant_site table
  116. $participant_site_mod = lib::create( 'database\modifier' );
  117. $participant_site_mod->where(
  118. 'callback.participant_id', '=', 'participant_site.participant_id', false );
  119. callback::customize_join( 'participant_site', $participant_site_mod );