/**
  * It's used for controlling attendance calendar (list, add, edit, delete),
  * render to attendance_calendar view
  * @param string $action (optional, by default 'calendar_list')
  * @param int	 $attendance_id (optional)
  * @param int	 $calendar_id (optional)
  */
 public function attendance_calendar($action = 'calendar_list', $attendance_id = 0, $calendar_id = 0)
 {
     $attendance = new Attendance();
     $calendar_id = intval($calendar_id);
     $data = array();
     $data['attendance_id'] = $attendance_id;
     $attendance_id = intval($attendance_id);
     $groupList = isset($_POST['groups']) ? array($_POST['groups']) : array();
     if ($action == 'calendar_add') {
         if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
             if (!isset($_POST['cancel'])) {
                 if (isset($_POST['repeat'])) {
                     //@todo  check this error_logs
                     $start_datetime = api_strtotime(api_get_utc_datetime($_POST['date_time']), 'UTC');
                     $end_datetime = api_strtotime(api_get_utc_datetime($_POST['end_date_time'] . ' 23:59:59'), 'UTC');
                     $checkdate = api_is_valid_date(api_get_utc_datetime($_POST['end_date_time'] . ' 23:59:59'));
                     $repeat_type = $_POST['repeat_type'];
                     if ($end_datetime > $start_datetime && $checkdate) {
                         $attendance->attendance_repeat_calendar_add($attendance_id, $start_datetime, $end_datetime, $repeat_type, $groupList);
                         $action = 'calendar_list';
                     } else {
                         if (!$checkdate) {
                             $data['error_checkdate'] = true;
                         } else {
                             $data['error_repeat_date'] = true;
                         }
                         $data['repeat'] = true;
                         $action = 'calendar_add';
                     }
                 } else {
                     $datetime = $_POST['date_time'];
                     $datetimezone = api_get_utc_datetime($datetime);
                     if (!empty($datetime)) {
                         $attendance->set_date_time($datetimezone);
                         $attendance->attendance_calendar_add($attendance_id, $groupList);
                         $action = 'calendar_list';
                     } else {
                         $data['error_date'] = true;
                         $action = 'calendar_add';
                     }
                 }
             } else {
                 $action = 'calendar_list';
             }
         }
     } else {
         if ($action == 'calendar_edit') {
             $data['calendar_id'] = $calendar_id;
             if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
                 if (!isset($_POST['cancel'])) {
                     $datetime = $_POST['date_time'];
                     $datetimezone = api_get_utc_datetime($datetime);
                     $attendance->set_date_time($datetimezone);
                     $attendance->attendance_calendar_edit($calendar_id, $attendance_id);
                     $data['calendar_id'] = 0;
                     $action = 'calendar_list';
                 } else {
                     $action = 'calendar_list';
                 }
             }
         } else {
             if ($action == 'calendar_delete') {
                 $attendance->attendance_calendar_delete($calendar_id, $attendance_id);
                 $action = 'calendar_list';
             } else {
                 if ($action == 'calendar_all_delete') {
                     $attendance->attendance_calendar_delete(0, $attendance_id, true);
                     $action = 'calendar_list';
                 }
             }
         }
     }
     $data['action'] = $action;
     $data['attendance_calendar'] = $attendance->get_attendance_calendar($attendance_id, 'all', null, null, true);
     $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id);
     // render to the view
     $this->view->set_data($data);
     $this->view->set_layout('layout');
     $this->view->set_template('attendance_calendar');
     $this->view->render();
 }
 /**
  * It's used for controlling attendace calendar (list, add, edit, delete),
  * render to attendance_calendar view
  * @param string action (optional, by default 'calendar_list')
  * @param int	 attendance id (optional)
  * @param int	 calendar id (optional)
  */
 public function attendance_calendar($action = 'calendar_list', $attendance_id = 0, $calendar_id = 0)
 {
     $attendance = new Attendance();
     $calendar_id = intval($calendar_id);
     $data = array();
     $data['attendance_id'] = $attendance_id;
     $attendance_id = intval($attendance_id);
     if ($action == 'calendar_add') {
         if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
             if (!isset($_POST['cancel'])) {
                 if (isset($_POST['repeat'])) {
                     //@todo  check this error_logs
                     $start_datetime = api_strtotime(api_get_utc_datetime($attendance->build_datetime_from_array($_POST['date_time'])), 'UTC');
                     //error_log('$start_datetime '.$start_datetime);
                     $_POST['end_date_time']['H'] = $_POST['date_time']['H'];
                     $_POST['end_date_time']['i'] = $_POST['date_time']['i'];
                     //error_log($attendance->build_datetime_from_array($_POST['end_date_time']));
                     $end_datetime = api_strtotime(api_get_utc_datetime($attendance->build_datetime_from_array($_POST['end_date_time'])), 'UTC');
                     //error_log('$end_datetime '.$end_datetime);
                     $checkdate = checkdate($_POST['end_date_time']['F'], $_POST['end_date_time']['d'], $_POST['end_date_time']['Y']);
                     $repeat_type = $_POST['repeat_type'];
                     if ($end_datetime > $start_datetime && $checkdate) {
                         $affected_rows = $attendance->attendance_repeat_calendar_add($attendance_id, $start_datetime, $end_datetime, $repeat_type);
                         $action = 'calendar_list';
                     } else {
                         if (!$checkdate) {
                             $data['error_checkdate'] = true;
                         } else {
                             $data['error_repeat_date'] = true;
                         }
                         $data['repeat'] = true;
                         $action = 'calendar_add';
                     }
                 } else {
                     $datetime = $attendance->build_datetime_from_array($_POST['date_time']);
                     $datetimezone = api_get_utc_datetime($datetime);
                     if (!empty($datetime)) {
                         $attendance->set_date_time($datetimezone);
                         $affected_rows = $attendance->attendance_calendar_add($attendance_id);
                         $action = 'calendar_list';
                     } else {
                         $data['error_date'] = true;
                         $action = 'calendar_add';
                     }
                 }
             } else {
                 $action = 'calendar_list';
             }
         }
     } else {
         if ($action == 'calendar_edit') {
             $data['calendar_id'] = $calendar_id;
             if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
                 if (!isset($_POST['cancel'])) {
                     $datetime = $attendance->build_datetime_from_array($_POST['date_time']);
                     $datetimezone = api_get_utc_datetime($datetime);
                     $attendance->set_date_time($datetimezone);
                     $affected_rows = $attendance->attendance_calendar_edit($calendar_id, $attendance_id);
                     $data['calendar_id'] = 0;
                     $action = 'calendar_list';
                 } else {
                     $action = 'calendar_list';
                 }
             }
         } else {
             if ($action == 'calendar_delete') {
                 $affected_rows = $attendance->attendance_calendar_delete($calendar_id, $attendance_id);
                 $action = 'calendar_list';
             } else {
                 if ($action == 'calendar_all_delete') {
                     $affected_rows = $attendance->attendance_calendar_delete(0, $attendance_id, true);
                     $action = 'calendar_list';
                 }
             }
         }
     }
     $data['action'] = $action;
     $data['attendance_calendar'] = $attendance->get_attendance_calendar($attendance_id);
     $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id);
     // render to the view
     $this->view->set_data($data);
     $this->view->set_layout('layout');
     $this->view->set_template('attendance_calendar');
     $this->view->render();
 }
 static function transaction_36($original_data, $web_service_details)
 {
     global $data_list;
     $data = Migration::soap_call($web_service_details, 'asistenciaDetalles', array('uididpersona' => $original_data['item_id'], 'uididprograma' => $original_data['orig_id'], 'uididfecha' => $original_data['info'], 'intIdSede' => $original_data['branch_id']));
     if ($data['error'] == false) {
         $uidIdPrograma = $original_data['orig_id'];
         $uidIdPersona = $original_data['item_id'];
         $attendance_date = $data['date_assist'];
         $attendance_user_status = $data['status'];
         // modified in the asistenciaDetalles function
         $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
         $user_id = self::get_user_id_by_persona_id($uidIdPersona, $data_list);
         if (empty($user_id)) {
             return array('message' => "User does not exists in DB: {$uidIdPersona}", 'status_id' => self::TRANSACTION_STATUS_FAILED);
         }
         if (empty($session_id)) {
             return array('message' => "Session does not exists {$uidIdPrograma}", 'status_id' => self::TRANSACTION_STATUS_FAILED);
         }
         $course_list = SessionManager::get_course_list_by_session_id($session_id);
         if (!empty($course_list)) {
             $course_data = current($course_list);
             if (isset($course_data['code'])) {
                 //Check if user exist in the session
                 $status = SessionManager::get_user_status_in_course_session($user_id, $course_data['id'], $session_id);
                 if ($status === false) {
                     return array('message' => "User #{$user_id} is not registered in course code: {$course_data['code']} - session_id: {$session_id}", 'status_id' => self::TRANSACTION_STATUS_FAILED);
                 }
                 $time = self::get_horario_value($session_id);
                 $attendance_date .= " {$time}:00";
                 $attendance = new Attendance();
                 $course_info = api_get_course_info($course_data['code']);
                 $attendance->set_course_id($course_info['code']);
                 $attendance->set_course_int_id($course_info['real_id']);
                 $attendance->set_session_id($session_id);
                 $attendance_list = $attendance->get_attendances_list($course_info['real_id'], $session_id);
                 if (empty($attendance_list)) {
                     return array('message' => "Attendance not found for course code: {$course_info['code']} - session_id: {$session_id}", 'status_id' => self::TRANSACTION_STATUS_FAILED);
                     //only 1 course per session
                 } else {
                     $attendance_data = current($attendance_list);
                     $attendance_id = $attendance_data['id'];
                     error_log("Attendance found in attendance_id = {$attendance_id} - course code: {$course_info['code']} - session_id: {$session_id} - {$attendance_date}");
                 }
                 $cal_info = $attendance->get_attendance_calendar_data_by_date($attendance_id, $attendance_date);
                 if ($cal_info && isset($cal_info['id'])) {
                     $cal_id = $cal_info['id'];
                 } else {
                     //Creating the attendance date
                     $attendance->set_date_time($attendance_date);
                     $cal_id = $attendance->attendance_calendar_add($attendance_id, true);
                     error_log("Creating a new calendar item {$cal_id}");
                     /*
                                             return array(
                                                 'message' => "Attendance calendar does not exist for date: $attendance_date in attendance_id = $attendance_id - course code: {$course_info['code']} - session_id: $session_id",
                                                 'status_id' => self::TRANSACTION_STATUS_FAILED
                                             );*/
                 }
                 $users_present = array($user_id => $attendance_user_status);
                 $attendance_sheet_before = $attendance->attendance_sheet_get_info($cal_id, $user_id);
                 $result = $attendance->attendance_sheet_add($cal_id, $users_present, $attendance_id, false, true);
                 $attendance_sheet_after = $attendance->attendance_sheet_get_info($cal_id, $user_id);
                 if ($result) {
                     return array('entity' => 'attendance_sheet', 'before' => $attendance_sheet_before, 'after' => $attendance_sheet_after, 'message' => "Attendance sheet edited with", 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL);
                 } else {
                     return array('message' => "Attendance sheet can't be edited attendance_id: {$attendance_id} - cal_id: {$cal_id} - user_id: {$user_id} - course: {$course_info['code']} - session_id: {$session_id} ", 'status_id' => self::TRANSACTION_STATUS_FAILED);
                 }
             } else {
                 $message = "Course is not set";
             }
         } else {
             $message = "NO course found for session id: {$session_id}";
         }
         return array('message' => $message, 'status_id' => self::TRANSACTION_STATUS_FAILED);
     } else {
         return $data;
     }
 }