<?php


/*
 *	sabreQMS --> sabreScheduler cross-app functions
 * 
 * functions that involve connecting with the external scheduler databse
 */


/*
 * _scheduler_simulator_add()
 * 
 * IN:  $simulator record object
 * OUT: True on success
 *      False on fail
 */

function _scheduler_simulator_add($simulator) {
  
  try {
    
    $rc = 0;

    if ( _st_connect_database(QMS_APP, QMS_DB_SCHEDULER) ) {

      // set fields that are needed to stored in Scheduler
      /*
      $simulator->created_by_user = 1;  //scheduler users not in sync with  QMS users
      unset($simulator->customer_id);
      unset($simulator->status_code);
       */


      //$rc = drupal_write_record('sch_simulators', $simulator);
      
      $rc = db_insert('sch_simulators')
              ->fields(array(
                  'simulator_id' => $simulator->simulator_id,
                  'sim_name' => $simulator->sim_name,
                  'faa_id' => $simulator->faa_id,
                  'device_id_internal' => $simulator->device_id_internal,
                  'active' => $simulator->active,
                  'created_date' => $simulator->created_date,
                  'created_by_user' => 1,
              ))->execute();

      _st_disconnect_database(QMS_APP);
 
    }
    return $rc;
  }
  catch(Exception $e) {
		watchdog(QMS_APP, '_scheduler_simulator_add()' . $e->getMessage(), array(), WATCHDOG_ERROR);
    _st_disconnect_database(QMS_APP);
  }
}


/*
 * _scheduler_simulator_delete()
 * 
 * IN:       $simulator_id
 * RETURNS:  $num_deleted (0 or 1), returns FALSE if $simulator_id is invalid.
 */

function _scheduler_simulator_delete($simulator_id) {
  
  try {
    
    if ( !$simulator_id ) return False;
      
    $sch_num_deleted = 0;

    if ( _st_connect_database(QMS_APP, QMS_DB_SCHEDULER) ) {

      // check if the same simulator in the QMS database has it's deleted flag set to true, if so, delete both
      $sql = "SELECT deleted FROM {sch_simulators} WHERE simulator_id = :sid";
      $flag = db_query($sql, array(':sid' => $simulator_id))->fetchField();

      if ( $flag == 1 ) { // already flagged for delete from Scheduler...delete both from QMS & Scheduler
        // delete from database
        $sch_num_deleted = db_delete('sch_simulators')
                    ->condition('simulator_id', $simulator_id)
                    ->execute();
      }

      _st_disconnect_database(QMS_APP);
    }
    return $sch_num_deleted;
  }
  catch(Exception $e) {
		watchdog(QMS_APP, '_scheduler_simulator_delete()' . $e->getMessage(), array(), WATCHDOG_ERROR);
    _st_disconnect_database(QMS_APP);
  }
}


/*
 * _scheduler_get_simulator_utilization_time()
 * 
 * IN:       $simulator_id
 * RETURNS:  $utilization (time in seconds (unix timestamp sum)
 */

function _scheduler_get_simulator_utilization_time($simulator_id, $begin_date, $end_date) {
  try {
    $utilization_time = 0;

    if ( _st_connect_database(QMS_APP, QMS_DB_SCHEDULER) ) {

      $query = db_select('sch_assignments', 'a');			
      $query->innerJoin('sch_schedules', 'sch', 'a.schedule_id = sch.schedule_id');
      $query->groupBy('sch.simulator_id');
      $query->addExpression('SUM(total_time)', 'utilization_time');
      $query->condition('a.assignment_date', $begin_date, '>=');
      $query->condition('a.assignment_date', $end_date, '<=');
      $query->condition('sch.simulator_id', $simulator_id);
      
      $result = $query->execute();

      if ( $result->rowCount()) {
        $utilization_time = $result->fetchField();
      }
      _st_disconnect_database(QMS_APP);
    }

    return $utilization_time;
  }
  catch(Exception $e) {
		watchdog(QMS_APP, '_scheduler_get_simulator_utilization_time()' . $e->getMessage(), 
             array(), WATCHDOG_ERROR);
    _st_disconnect_database(QMS_APP);
  }
}

