public static function post() { //Get the request body as json $checkInRequest = json_decode(file_get_contents('php://input')); //Check if the json was valid and a card number is given if (!$checkInRequest || !isset($checkInRequest->cardNumber)) { header('HTTP/1.1 400 Bad Request'); } else { //Create response object $response = new APICheckInResponse(); //Check if the card number is valid if (!CheckInController::validateCardNumber($checkInRequest->cardNumber)) { $response->errorCode = APICheckInResponse::MALFORMED_CARDNUMBER; echo json_encode($response); exit; } else { $user = null; $checkInOk = false; $weeklyWinnerOk = true; //Get user to check in try { //Get the user who's card number for this year was entered $user = UserDB::getBasicUserByCardNumber($checkInRequest->cardNumber); } catch (Exception $ex) { $response->errorCode = APICheckInResponse::CANNOT_GET_USER_DATA; echo json_encode($response); exit; } if (!$user) { //There's no user for this card $response->errorCode = APICheckInResponse::NO_USER_FOR_CARD_NUMBER; echo json_encode($response); exit; } else { //We have a user so get the names in the response $response->userFirstName = $user->firstName; $response->userLastName = $user->lastName; //Check user in try { $checkInOk = CheckInDB::checkIn($user->userId); } catch (Exception $ex) { //Check-in failed (something went wrong or check-in isn't valid) $response->errorCode = APICheckInResponse::CANNOT_CHECK_IN; echo json_encode($response); exit; } } if (!$checkInOk) { //The user has already checked in $response->errorCode = APICheckInResponse::ALREADY_CHECKED_IN; echo json_encode($response); exit; } else { $response->checkInSuccessful = true; //Check in successful, check whether he is the winner of the week $isWinner = false; try { //Check if this user is the winner of the week $weeklyWinnerData = WeeklyWinnerDB::getThisWeeksWinnerData(); $isWinner = $weeklyWinnerData && $weeklyWinnerData->userId == $user->userId && !$weeklyWinnerData->hasCollectedPrize; //If he is the winner we set in the database that the user collected his prize if ($isWinner) { $newWeeklyWinnerData = new WeeklyWinnerData($weeklyWinnerData->startOfWeek, $weeklyWinnerData->userId, true); WeeklyWinnerDB::updateWeeklyWinnerData($weeklyWinnerData, $newWeeklyWinnerData); } } catch (Exception $ex) { $response->errorCode = APICheckInResponse::CANNOT_CHECK_WEEKLY_WINNER; echo json_encode($response); exit; } $response->isWeeklyWinner = $isWinner; //If he is the winner, add the winner views and try to send an email to all usermanagers if ($isWinner) { try { $select = array('email' => true); $searchFilter = array('isUserManager' => true); $searchUsers = UserDB::getSearchUsers($select, $searchFilter, null); $extras['common']['winnerFirstName'] = $user->firstName; $extras['common']['winnerLastName'] = $user->lastName; $failedAddresses = Email::sendEmails('WeeklyWinnerNotification.html', 'Winnaar van de week', EmailConfig::FROM_ADDRESS, array_column($searchUsers, 'user'), $extras); if (!empty($failedAddresses)) { $response->errorCode = APICheckInResponse::CANNOT_SEND_WINNER_NOTIFICATIONS; } echo json_encode($response); exit; } catch (Exception $ex) { $response->errorCode = APICheckInResponse::CANNOT_SEND_WINNER_NOTIFICATIONS; echo json_encode($response); exit; } } else { echo json_encode($response); exit; } } } } }