PageRenderTime 95ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/api/ui/push/phone_call_end.class.php

https://github.com/patrickdemond/beartooth
PHP | 133 lines | 91 code | 11 blank | 31 comment | 16 complexity | 6b3bdbf5b924b42967ac6e95a5083d77 MD5 | raw file
  1. <?php
  2. /**
  3. * phone_call_end.class.php
  4. *
  5. * @author Patrick Emond <emondpd@mcmaster.ca>
  6. * @filesource
  7. */
  8. namespace beartooth\ui\push;
  9. use cenozo\lib, cenozo\log, beartooth\util;
  10. /**
  11. * push: phone_call end
  12. *
  13. * Assigns a participant to an phone_call.
  14. */
  15. class phone_call_end extends \cenozo\ui\push
  16. {
  17. /**
  18. * Constructor.
  19. * @author Patrick Emond <emondpd@mcmaster.ca>
  20. * @param array $args Push arguments
  21. * @access public
  22. */
  23. public function __construct( $args )
  24. {
  25. parent::__construct( 'phone_call', 'end', $args );
  26. }
  27. /**
  28. * This method executes the operation's purpose.
  29. *
  30. * @author Patrick Emond <emondpd@mcmaster.ca>
  31. * @access protected
  32. */
  33. protected function execute()
  34. {
  35. parent::execute();
  36. $session = lib::create( 'business\session' );
  37. // disconnect voip
  38. $voip_call = lib::create( 'business\voip_manager' )->get_call();
  39. if( !is_null( $voip_call ) ) $voip_call->hang_up();
  40. // if we are NOT calling a secondary contact then process the call result
  41. if( !array_key_exists( 'secondary_id', $_COOKIE ) )
  42. {
  43. // set the end time and status of the call
  44. $db_phone_call = $session->get_current_phone_call();
  45. if( !is_null( $db_phone_call ) )
  46. {
  47. $event_type_class_name = lib::get_class_name( 'database\event_type' );
  48. $date_obj = util::get_datetime_object();
  49. $db_phone_call->end_datetime = $date_obj->format( 'Y-m-d H:i:s' );
  50. $db_phone_call->status = $this->get_argument( 'status' );
  51. $db_phone_call->save();
  52. // if the participant has never had a contact attempt made then mark the event
  53. $db_interview = $db_phone_call->get_assignment()->get_interview();
  54. $db_participant = $db_interview->get_participant();
  55. $event_type_name = sprintf( 'first attempt (%s)', $db_interview->get_qnaire()->name );
  56. $db_event_type = $event_type_class_name::get_unique_record( 'name', $event_type_name );
  57. if( !is_null( $db_event_type ) )
  58. {
  59. $event_mod = lib::create( 'database\modifier' );
  60. $event_mod->where( 'event_type_id', '=', $db_event_type->id );
  61. if( 0 == $db_participant->get_event_count( $event_mod ) )
  62. {
  63. $db_event = lib::create( 'database\event' );
  64. $db_event->participant_id = $db_participant->id;
  65. $db_event->event_type_id = $db_event_type->id;
  66. $db_event->datetime = util::get_datetime_object()->format( 'Y-m-d H:i:s' );
  67. $db_event->save();
  68. }
  69. }
  70. // if the status is "disconnected" or "wrong number" deactivate the phone and make a note
  71. // that the number has been disconnected
  72. if( 'disconnected' == $db_phone_call->status ||
  73. 'wrong number' == $db_phone_call->status )
  74. {
  75. $db_phone = lib::create( 'database\phone', $db_phone_call->phone_id );
  76. if( !is_null( $db_phone ) )
  77. {
  78. $note = sprintf( 'This phone number has been disabled because a call was made to it '.
  79. 'on %s at %s '.
  80. 'by user id %d (%s) '.
  81. 'with the result of "%s".',
  82. util::get_formatted_date( $db_phone_call->end_datetime ),
  83. util::get_formatted_time( $db_phone_call->end_datetime ),
  84. $session->get_user()->id,
  85. $session->get_user()->name,
  86. $db_phone_call->status );
  87. // keep the old note if there is one
  88. $note = is_null( $db_phone->note ) ? $note : $db_phone->note."\n\n".$note;
  89. // apply the change using an operation (so that Mastodon is also updated)
  90. $args = array(
  91. 'id' => $db_phone->id,
  92. 'columns' => array(
  93. 'active' => false,
  94. 'note' => $note ) );
  95. $operation = lib::create( 'ui\push\phone_edit', $args );
  96. $operation->process();
  97. }
  98. }
  99. else if( 'contacted' == $db_phone_call->status )
  100. { // if the participant has never been reached then add this event
  101. $db_interview = $db_phone_call->get_assignment()->get_interview();
  102. $db_participant = $db_interview->get_participant();
  103. $event_type_name = sprintf( 'reached (%s)', $db_interview->get_qnaire()->name );
  104. $db_event_type = $event_type_class_name::get_unique_record( 'name', $event_type_name );
  105. if( !is_null( $db_event_type ) )
  106. {
  107. $event_mod = lib::create( 'database\modifier' );
  108. $event_mod->where( 'event_type_id', '=', $db_event_type->id );
  109. if( 0 == $db_participant->get_event_count( $event_mod ) )
  110. {
  111. $db_event = lib::create( 'database\event' );
  112. $db_event->participant_id = $db_participant->id;
  113. $db_event->event_type_id = $db_event_type->id;
  114. $db_event->datetime = util::get_datetime_object()->format( 'Y-m-d H:i:s' );
  115. $db_event->save();
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }