public static function post() { try { //Get list of user IDs of users that are a member this year $userIds = MembershipDB::getUserIdsThisYear(); //Get list of last N winners $lastWinners = WeeklyWinnerDB::getLastNWinners(WeeklyWinnerConfig::MIN_WINNING_INTERVAL); //Get list of user IDs that have not won the last N times if ($lastWinners) { $possibleWinners = array_diff($userIds, $lastWinners); } else { $possibleWinners = $userIds; } //Pick a random user ID $winnerIdx = rand(0, count($possibleWinners) - 1); //Add chosen user ID as winner WeeklyWinnerDB::addWeeklyWinner($possibleWinners[$winnerIdx]); //Show the page again, this is the same as GETing so we just call get WeeklyWinnerController::get(); } catch (Exception $ex) { $page = new Page(); $page->addView('weeklyWinner/WeeklyWinnerTopView'); $page->data['ErrorMessageNoDescriptionWithLinkView']['errorTitle'] = 'Kan geen nieuwe winnaar loten.'; $page->data['ErrorMessageNoDescriptionWithLinkView']['tryAgainUrl'] = $_SERVER['REQUEST_URI']; $page->addView('error/ErrorMessageNoDescriptionWithLinkView'); $page->showWithMenu(); } }
public static function run() { try { //Get list of user IDs of users that are a member this year $userIds = MembershipDB::getUserIdsThisYear(); //Get list of last N winners $lastWinners = WeeklyWinnerDB::getLastNWinners(WeeklyWinnerConfig::MIN_WINNING_INTERVAL); //Get list of user IDs that have not won the last N times if ($lastWinners) { $possibleWinners = array_diff($userIds, $lastWinners); } else { $possibleWinners = $userIds; } //Pick a random user ID $winnerIdx = rand(0, count($possibleWinners) - 1); //Add chosen user ID as winner WeeklyWinnerDB::addWeeklyWinner($possibleWinners[$winnerIdx]); } catch (Exception $ex) { var_dump($ex); } }
public static function post() { $page = new Page(); $page->data['title'] = 'Inchecken'; $errMsgs = CheckInFormViewValidator::validate($_POST); $user = null; if (empty($errMsgs)) { $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($_POST['card_number']); } catch (Exception $ex) { //Check-in failed (can't get user) CheckInController::buildCheckInFormView($page, true); $page->data['CheckInFormView']['errMsgs']['global'] = '<h2 class="error_message" id="check_in_form_error_message">Kan gebruiker niet inchecken, probeer het opnieuw.</h2>'; $page->showWithMenu(); exit; } if (!$user) { //There's no user for this card CheckInController::buildCheckInFormView($page, true); $page->data['CheckInFormView']['errMsgs']['global'] = '<h2 class="error_message" id="check_in_form_error_message">Dit kaartnummer is niet gekoppeld aan een gebruiker.</h2>'; $page->showWithMenu(); exit; } else { //Check user in try { $checkInOk = CheckInDB::checkIn($user->userId); } catch (Exception $ex) { //Check-in failed (something went wrong or check-in isn't valid) CheckInController::buildCheckInFormView($page, true); $page->data['CheckInFormView']['errMsgs']['global'] = '<h2 class="error_message" id="check_in_form_error_message">Kan gebruiker niet inchecken, probeer het opnieuw.</h2>'; $page->showWithMenu(); exit; } } if (!$checkInOk) { //The user has already checked in CheckInController::buildCheckInFormView($page, true); $page->data['CheckInFormView']['errMsgs']['global'] = '<h2 class="error_message" id="check_in_form_error_message">Deze gebruiker is de voorbije 12 uur al ingechecked.</h2>'; $page->showWithMenu(); exit; } else { //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) { $weeklyWinnerOk = false; } //If he is the winner, add the winner views and try to send an email to all usermanagers if ($isWinner) { $page->addView('checkIn/CheckInWeeklyWinnerTitleView'); CheckInController::buildCheckInSuccessfulView($page, $user); 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)) { $page->data['ErrorMessageWithDescriptionNoLinkView']['errorTitle'] = 'Kan e-mail niet versturen.'; $page->data['ErrorMessageWithDescriptionNoLinkView']['description'] = 'Kan geen e-mail versturen naar het bestuur om te laten weten dat je gewonnen hebt. Meld dit even aan een tapper of bestuurslid.'; $page->addView('error/ErrorMessageWithDescriptionNoLinkView'); } } catch (Exception $ex) { $page->data['ErrorMessageWithDescriptionNoLinkView']['errorTitle'] = 'Kan e-mail niet versturen.'; $page->data['ErrorMessageWithDescriptionNoLinkView']['errorDescription'] = 'Kan geen e-mail versturen naar het bestuur om te laten weten dat je gewonnen hebt. Meld dit even aan een tapper of bestuurslid.'; $page->addView('error/ErrorMessageWithDescriptionNoLinkView'); } $page->addView('checkIn/CheckInWeeklyWinnerImageView'); $page->showWithMenu(); } else { $page->addView('checkIn/CheckInSuccessfulTitleView'); CheckInController::buildCheckInSuccessfulView($page, $user); if (!$weeklyWinnerOk) { $page->data['ErrorMessageNoDescriptionNoLinkView']['errorTitle'] = 'Kan niet controleren of je de winnaar van de week bent.'; $page->addView('error/ErrorMessageNoDescriptionNoLinkView'); } else { $page->addExtraJsFile('views/checkIn/CheckInRedirector.js'); $page->addExtraJsFile('views/checkIn/checkInSuccessfulOnLoadHandler.js'); } $page->showWithMenu(); } } } else { //Errors in the form, retry CheckInController::buildCheckInFormView($page, true); $page->data['CheckInFormView']['errMsgs'] = array_merge($page->data['CheckInFormView']['errMsgs'], $errMsgs); $page->showWithMenu(); } }
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; } } } } }