PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/api/database/appointment.class.php

https://github.com/patrickdemond/beartooth
PHP | 123 lines | 62 code | 14 blank | 47 comment | 10 complexity | 045c78c5bcaddd6b2c6bfe0261f9c589 MD5 | raw file
  1. <?php
  2. /**
  3. * appointment.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. * appointment: record
  12. */
  13. class appointment 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. // appointments 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 future home appointment and 1 future site appointment
  34. if( !$this->completed )
  35. {
  36. $now_datetime_obj = util::get_datetime_object();
  37. $modifier = lib::create( 'database\modifier' );
  38. $modifier->where( 'participant_id', '=', $this->participant_id );
  39. $modifier->where( 'datetime', '>', $now_datetime_obj->format( 'Y-m-d H:i:s' ) );
  40. $modifier->where( 'address_id', $this->address_id ? '!=' : '=', NULL );
  41. if( !is_null( $this->id ) ) $modifier->where( 'id', '!=', $this->id );
  42. $appointment_list = static::select( $modifier );
  43. if( 0 < count( $appointment_list ) )
  44. {
  45. $db_appointment = current( $appointment_list );
  46. throw lib::create( 'exception\notice',
  47. sprintf( 'Unable to add the appointment since the participant already has an upcomming '.
  48. '%s appointment scheduled for %s.',
  49. is_null( $this->address_id ) ? 'site' : 'home',
  50. util::get_formatted_datetime( $db_appointment->datetime ) ),
  51. __METHOD__ );
  52. }
  53. }
  54. parent::save();
  55. }
  56. /**
  57. * Determines whether there are open slots available during this appointment's date/time.
  58. * The result will depend on whether the appointment has an address or not. If not then
  59. * it is considered to be a site interview (and so it refers to openings to the site
  60. * calendar), otherwise it is considered to be a home interview (and so it refers to
  61. * openings to the home calendar).
  62. * @author Patrick Emond <emondpd@mcmaster.ca>
  63. * @return boolean
  64. * @throws exception\runtime
  65. * @access public
  66. */
  67. public function validate_date()
  68. {
  69. // make sure the participant is ready for the appointment type (home/site)
  70. // (don't use $this->get_participant(), the record may not have been created yet)
  71. $db_participant = lib::create( 'database\participant', $this->participant_id );
  72. // check the qnaire start date
  73. $start_qnaire_date = $db_participant->get_start_qnaire_date();
  74. if( !is_null( $start_qnaire_date ) && $start_qnaire_date > util::get_datetime_object() )
  75. return false;
  76. // check the qnaire type
  77. $type = is_null( $this->address_id ) ? 'site' : 'home';
  78. $db_effective_qnaire = $db_participant->get_effective_qnaire();
  79. if( is_null( $db_effective_qnaire ) || $db_effective_qnaire->type != $type ) return false;
  80. return true;
  81. }
  82. /**
  83. * Get the state of the appointment as a string:
  84. * completed: the appointment has been completed and the interview is done
  85. * upcoming: the appointment's date/time has not yet occurred
  86. * passed: the appointment's date/time has passed and the interview is not done
  87. * @author Patrick Emond <emondpd@mcmaster.ca>
  88. * @return string
  89. * @access public
  90. */
  91. public function get_state( $ignore_assignments = false )
  92. {
  93. if( is_null( $this->id ) )
  94. {
  95. log::warning( 'Tried to determine state for appointment with no id.' );
  96. return NULL;
  97. }
  98. // first see if the appointment is complete
  99. if( $this->completed ) return 'completed';
  100. $now = util::get_datetime_object()->getTimestamp();
  101. $appointment = util::get_datetime_object( $this->datetime )->getTimestamp();
  102. return $now < $appointment ? 'upcoming' : 'passed';
  103. }
  104. }
  105. // define the join to the participant_site table
  106. $participant_site_mod = lib::create( 'database\modifier' );
  107. $participant_site_mod->where(
  108. 'appointment.participant_id', '=', 'participant_site.participant_id', false );
  109. appointment::customize_join( 'participant_site', $participant_site_mod );