public function execute(CommandContext $context)
 {
     // Get input
     $requestId = $context->get('requestId');
     $participantId = $context->get('participantId');
     // Command for showing the request, redirected to on success/error
     $cmd = CommandFactory::getCommand('ShowManageRoomChange');
     $cmd->setRequestId($requestId);
     // Load the request
     $request = RoomChangeRequestFactory::getRequestById($requestId);
     // Load the participant
     $participant = RoomChangeParticipantFactory::getParticipantById($participantId);
     // Check permissions. Must be an RD for current bed, or an admin
     $rds = $participant->getFutureRdList();
     if (!in_array(UserStatus::getUsername(), $rds) && !Current_User::allow('hms', 'admin_approve_room_change')) {
         throw new PermissionException('You do not have permission to approve this room change.');
     }
     // Transition to CurrRdApproved
     $participant->transitionTo(new ParticipantStateFutureRdApproved($participant, time(), null, UserStatus::getUsername()));
     //TODO If all participants are approved, send notification to Housing
     if ($request->isApprovedByAllFutureRDs()) {
         HMS_Email::sendRoomChangeAdministratorNotice($request);
     }
     // Redirect to the manage request page
     $cmd->redirect();
 }
 public function execute(CommandContext $context)
 {
     PHPWS_Core::initModClass('hms', 'RoomChangeRequestFactory.php');
     PHPWS_Core::initModClass('hms', 'RoomChangeManageView.php');
     $requestId = $context->get('requestId');
     if (!isset($requestId) || is_null($context)) {
         throw new InvalidArgumentException('Missing request id');
     }
     $request = RoomChangeRequestFactory::getRequestById($requestId);
     if (is_null($request) || $request === false) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Invalid room change request id.');
         $cmd = CommandFactory::getCommand('ShowAdminMaintenanceMenu');
         $cmd->redirect();
     }
     $view = new RoomChangeManageView($request);
     $context->setContent($view->show());
 }
 public function execute(CommandContext $context)
 {
     // Get input
     $requestId = $context->get('requestId');
     $participantId = $context->get('participantId');
     // Command for showing the request, redirected to on success/error
     $cmd = CommandFactory::getCommand('ShowManageRoomChange');
     $cmd->setRequestId($requestId);
     // Load the request
     $request = RoomChangeRequestFactory::getRequestById($requestId);
     // Load the participant
     $participant = RoomChangeParticipantFactory::getParticipantById($participantId);
     // Load the Student
     $student = StudentFactory::getStudentByBannerId($participant->getBannerId(), $request->getTerm());
     // Check permissions. Must be the participant or an admin
     if (UserStatus::getUsername() != $student->getUsername() && !Current_User::allow('hms', 'admin_approve_room_change')) {
         throw new PermissionException('You do not have permission to appove this room change.');
     }
     // Check for CAPTCHA if this is the student; admins don't need a CAPTCHA
     $captchaResult = Captcha::verify(true);
     if (UserStatus::getUsername() == $student->getUsername() && $captchaResult === false) {
         // Failed the captcha
         NQ::simple('hms', hms\NotificationView::ERROR, "You didn't type the magic words correctly. Please try again.");
         $cmd = CommandFactory::getCommand('ShowRoomChangeRequestApproval');
         $cmd->redirect();
     }
     // If there was a captcha, then log the activity
     if ($captchaResult !== false) {
         HMS_Activity_Log::log_activity(UserStatus::getUsername(), ACTIVITY_ROOM_CHANGE_AGREED, UserStatus::getUsername(FALSE), 'Request id: ' . $requestId . ' Captcha: ' . $captchaResult);
     }
     // Transition to StudentApproved state
     $participant->transitionTo(new ParticipantStateStudentApproved($participant, time(), null, UserStatus::getUsername()));
     // If all students have approved, notify RDs
     if ($request->isApprovedByAllParticipants()) {
         HMS_Email::sendRoomChangeCurrRDNotice($request);
     }
     // If the student is logged in, redirect to the main menu, other wise go back to the room change management view
     if (UserStatus::getUsername() == $student->getUsername()) {
         NQ::simple('hms', hms\NotificationView::SUCCESS, 'You have agreed to the room change request. You will be notified by email when the reqeust is approved or denied.');
         $menuCmd = CommandFactory::getCommand('ShowStudentMenu');
         $menuCmd->redirect();
     } else {
         $cmd->redirect();
     }
 }
 public function execute(CommandContext $context)
 {
     $requestId = $context->get('requestId');
     $reason = $context->get('cancel-reason');
     // Load the request
     $request = RoomChangeRequestFactory::getRequestById($requestId);
     // TODO Check permissions, based on state
     // Command for redirecting back to the request view on success or error
     $cmd = CommandFactory::getCommand('ShowManageRoomChange');
     $cmd->setRequestId($request->getId());
     // Make sure user gave a reason
     if (!isset($reason) or $reason == '') {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Please enter a cancellation reason.');
         $cmd->redirect();
     }
     // Set the denied reason
     $request->setDeniedReasonPublic($reason);
     $request->save();
     // Transition request to cancelled status
     $request->transitionTo(new RoomChangeStateCancelled($request, time(), null, UserStatus::getUsername()));
     // Transition all participants to cancelled
     // TODO... Do this in the cancelled transition?
     $participants = $request->getParticipants();
     foreach ($participants as $p) {
         $p->transitionTo(new ParticipantStateCancelled($p, time(), null, UserStatus::getUsername()));
         //Release the bed reservation, if any
         $bedId = $p->getToBed();
         if ($bedId != null) {
             $bed = new HMS_Bed($bedId);
             $bed->clearRoomChangeReserved();
             $bed->save();
         }
     }
     // Notify everyone involved
     try {
         PHPWS_Core::initModClass('hms', 'StudentFactory.php');
         $student = StudentFactory::getStudentByUsername(UserStatus::getUsername(), $request->getTerm());
     } catch (StudentNotFoundException $e) {
         $student = null;
     }
     PHPWS_Core::initModClass('hms', 'HMS_Email.php');
     HMS_Email::sendRoomChangeCancelledNotice($request, $student);
     $cmd->redirect();
 }
 public function execute(CommandContext $context)
 {
     // Get input
     $requestId = $context->get('requestId');
     $participantId = $context->get('participantId');
     // Load the request
     $request = RoomChangeRequestFactory::getRequestById($requestId);
     // Load the participant
     $participant = RoomChangeParticipantFactory::getParticipantById($participantId);
     // Load the Student
     $student = StudentFactory::getStudentByBannerId($participant->getBannerId(), $request->getTerm());
     // Check permissions. Must be the participant or an admin
     if (UserStatus::getUsername() != $student->getUsername() && !Current_User::allow('hms', 'admin_approve_room_change')) {
         throw new PermissionException('You do not have permission to decline this room change.');
     }
     // Check for CAPTCHA if this is the student; admins don't need a CAPTCHA
     $captchaResult = Captcha::verify(true);
     if ($captchaResult === false) {
         // Failed the captcha
         NQ::simple('hms', hms\NotificationView::ERROR, "You didn't type the magic words correctly. Please try again.");
         $cmd = CommandFactory::getCommand('ShowRoomChangeRequestApproval');
         $cmd->redirect();
     }
     HMS_Activity_Log::log_activity(UserStatus::getUsername(), ACTIVITY_ROOM_CHANGE_DECLINE, UserStatus::getUsername(FALSE), 'Request id: ' . $requestId . ' Captcha: ' . $captchaResult);
     // Transition request to cancelled status
     $request->transitionTo(new RoomChangeStateCancelled($request, time(), null, UserStatus::getUsername()));
     // Transition all participants to cancelled
     // TODO... Do this in the cancelled transition?
     $participants = $request->getParticipants();
     foreach ($participants as $p) {
         $p->transitionTo(new ParticipantStateCancelled($p, time(), null, UserStatus::getUsername()));
     }
     // TODO Notify everyone that the request was cancelled
     NQ::simple('hms', hms\NotificationView::SUCCESS, 'You have declined the room change request.');
     $menuCmd = CommandFactory::getCommand('ShowStudentMenu');
     $menuCmd->redirect();
 }
 public function execute(CommandContext $context)
 {
     // Get input
     $requestId = $context->get('requestId');
     $participantId = $context->get('participantId');
     // destinationBedId - This can be null for "swap" requests, because it's already known
     $toBedSelected = $context->get('bed_select');
     // Command for showing the request, redirected to on success/error
     $cmd = CommandFactory::getCommand('ShowManageRoomChange');
     $cmd->setRequestId($requestId);
     // Load the request
     $request = RoomChangeRequestFactory::getRequestById($requestId);
     // Load the participant
     $participant = RoomChangeParticipantFactory::getParticipantById($participantId);
     // Check permissions. Must be an RD for current bed, or an admin
     $rds = $participant->getCurrentRdList();
     if (!in_array(UserStatus::getUsername(), $rds) && !Current_User::allow('hms', 'admin_approve_room_change')) {
         throw new PermissionException('You do not have permission to approve this room change.');
     }
     // Check that a destination bed has already been set, or that the RD
     // has just selected a bed
     $toBedId = $participant->getToBed();
     if (is_null($toBedId) && $toBedSelected == '-1') {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Please select a destination bed.');
         $cmd->redirect();
     }
     // Set the selected bed, if needed
     if (is_null($toBedId) && $toBedSelected != '-1') {
         $bed = new HMS_Bed($toBedSelected);
         // Check that the bed isn't already reserved for a room change
         if ($bed->isRoomChangeReserved()) {
             NQ::simple('hms', hms\NotificationView::ERROR, 'The bed you selected is already reserved for a room change. Please choose a different bed.');
             $cmd->redirect();
         }
         // Reserve the bed for room change
         $bed->setRoomChangeReserved();
         $bed->save();
         // Save the bed to this participant
         $participant->setToBed($bed);
         $participant->save();
     }
     // Transition to CurrRdApproved
     $participant->transitionTo(new ParticipantStateCurrRdApproved($participant, time(), null, UserStatus::getUsername()));
     // If the future RD is the same as the current user Logged in, then go ahead and transition to FutureRdApproved too.
     //TODO
     if ($request->isApprovedByAllCurrentRDs()) {
         // If all Current RDs have approved, notify Future RDs
         HMS_Email::sendRoomChangeFutureRDNotice($request);
         // If all Current RDs have approved, notify future roommates
         foreach ($request->getParticipants() as $p) {
             $bed = new HMS_Bed($p->getToBed());
             $room = $bed->get_parent();
             foreach ($room->get_assignees() as $a) {
                 if ($a instanceof Student && $a->getBannerID() != $p->getBannerID()) {
                     HMS_Email::sendRoomChangePreliminaryRoommateNotice($a);
                 }
             }
         }
     }
     // Redirect to the manage request page
     $cmd->redirect();
 }
 public function execute(CommandContext $context)
 {
     // Get input
     $requestId = $context->get('requestId');
     // Get the current term
     $term = Term::getCurrentTerm();
     // Load the request
     $request = RoomChangeRequestFactory::getRequestById($requestId);
     // Load the participants
     $participants = $request->getParticipants();
     // Make sure everyone is checked into their current assignments
     if (!$request->allParticipantsCheckedIn()) {
         // Return the user to the room change request page
         // NB, don't need an error message here because it should already be printed
         // by the RoomChangeParticipantView.
         $cmd = CommandFactory::getCommand('ShowManageRoomChange');
         $cmd->setRequestId($requestId);
         $cmd->redirect();
     }
     // Transition the request to 'Approved'
     $request->transitionTo(new RoomChangeStateApproved($request, time(), null, UserStatus::getUsername()));
     // Remove each participants existing assignment
     foreach ($participants as $participant) {
         $bannerId = $participant->getBannerId();
         // Lookup the student
         $student = StudentFactory::getStudentByBannerId($bannerId, $term);
         // Save student object for later
         $this->students[$bannerId] = $student;
         // Save student's current assignment reason for later re-use
         $assignment = HMS_Assignment::getAssignmentByBannerId($bannerId, $term);
         //TODO - Student might not be assigned!!
         $this->assignmentReasons[$bannerId] = $assignment->getReason();
         // Remove existing assignment
         // TODO: Don't hard code refund percentage
         HMS_Assignment::unassignStudent($student, $term, 'Room Change Request Approved', UNASSIGN_CHANGE, 100);
     }
     // Create new assignments for each participant
     foreach ($participants as $participant) {
         // Grab the student object which was previously saved
         $student = $this->students[$participant->getBannerId()];
         // Create each new assignment
         HMS_Assignment::assignStudent($student, $term, null, $participant->getToBed(), BANNER_MEAL_STD, 'Room Change Approved', FALSE, $this->assignmentReasons[$bannerId]);
         // Release bed reservation
         $bed = new HMS_Bed($participant->getToBed());
         $bed->clearRoomChangeReserved();
         $bed->save();
     }
     // Transition each participant to 'In Process'
     foreach ($participants as $participant) {
         $participant->transitionTo(new ParticipantStateInProcess($participant, time(), null, UserStatus::getUsername()));
         // TODO: Send notifications
     }
     // Notify everyone that they can do the move
     HMS_Email::sendRoomChangeInProcessNotice($request);
     // Notify roommates that their circumstances are going to change
     foreach ($request->getParticipants() as $p) {
         $student = $this->students[$p->getBannerId()];
         // New Roommate
         $newbed = new HMS_Bed($p->getToBed());
         $newroom = $newbed->get_parent();
         foreach ($newroom->get_assignees() as $a) {
             if ($a instanceof Student && $a->getBannerID() != $p->getBannerID()) {
                 HMS_Email::sendRoomChangeApprovedNewRoommateNotice($a, $student);
             }
         }
         // Old Roommate
         $oldbed = new HMS_Bed($p->getFromBed());
         $oldroom = $oldbed->get_parent();
         foreach ($oldroom->get_assignees() as $a) {
             if ($a instanceof Student && $a->getBannerID() != $p->getBannerID()) {
                 HMS_Email::sendRoomChangeApprovedOldRoommateNotice($a, $student);
             }
         }
     }
     // Return the user to the room change request page
     $cmd = CommandFactory::getCommand('ShowManageRoomChange');
     $cmd->setRequestId($requestId);
     $cmd->redirect();
 }