public function execute(CommandContext $context) { PHPWS_Core::initModClass('hms', 'StudentFactory.php'); $roomId = $context->get('roomId'); $roommates = $context->get('roommates'); $mealPlan = $context->get('mealPlan'); $term = PHPWS_Settings::get('hms', 'lottery_term'); $student = StudentFactory::getStudentByUsername(UserStatus::getUsername(), $term); $errorCmd = CommandFactory::getCommand('LotteryShowConfirm'); $errorCmd->setRoomId($roomId); $errorCmd->setRoommates($roommates); $errorCmd->setMealPlan($mealPlan); $successCmd = CommandFactory::getCommand('LotteryShowConfirmed'); $successCmd->setRoomId($roomId); PHPWS_Core::initCoreClass('Captcha.php'); $captcha = Captcha::verify(TRUE); // returns the words entered if correct, FALSE otherwise //$captcha = TRUE; if ($captcha === FALSE) { NQ::simple('hms', hms\NotificationView::ERROR, 'Sorry, the words you eneted were incorrect. Please try again.'); $errorCmd->redirect(); } PHPWS_Core::initModClass('hms', 'HousingApplication.php'); PHPWS_Core::initModClass('hms', 'HMS_Room.php'); PHPWS_Core::initModClass('hms', 'HMS_Bed.php'); PHPWS_Core::initModClass('hms', 'HMS_Assignment.php'); PHPWS_Core::initModClass('hms', 'HMS_Lottery.php'); PHPWS_Core::initModClass('hms', 'StudentFactory.php'); PHPWS_Core::initModClass('hms', 'HMS_Email.php'); PHPWS_Core::initModClass('hms', 'HMS_Activity_Log.php'); PHPWS_Core::initModClass('hms', 'HMS_Util.php'); PHPWS_Core::initModClass('hms', 'RlcMembershipFactory.php'); PHPWS_Core::initModClass('hms', 'RlcAssignmentSelfAssignedState.php'); $room = new HMS_Room($roomId); // Check for an RLC assignment in the self-select status $rlcAssignment = RlcMembershipFactory::getMembership($student, $term); // Check roommates for validity foreach ($roommates as $bed_id => $username) { // Double check the student is valid try { $roommate = StudentFactory::getStudentByUsername($username, $term); } catch (StudentNotFoundException $e) { NQ::simple('hms', hms\NotificationView::ERROR, "{$username} is not a valid student. Please choose a different roommate."); $errorCmd->redirect(); } // Make sure the bed is still empty $bed = new HMS_Bed($bed_id); if ($bed->has_vacancy() != TRUE) { NQ::simple('hms', hms\NotificationView::ERROR, 'One or more of the beds in the room you selected is no longer available. Please try again.'); $errorCmd->redirect(); } // Make sure none of the needed beds are reserved if ($bed->is_lottery_reserved()) { NQ::simple('hms', hms\NotificationView::ERROR, 'One or more of the beds in the room you selected is no longer available. Please try again.'); $errorCmd->redirect(); } // Double check the genders are all the same as the person logged in if ($student->getGender() != $roommate->getGender()) { NQ::simple('hms', hms\NotificationView::ERROR, "{$username} is a different gender. Please choose a roommate of the same gender."); $errorCmd->redirect(); } // Double check the genders are the same as the room (as long as the room isn't AUTO) if ($room->gender_type != AUTO && $roommate->getGender() != $room->gender_type) { NQ::simple('hms', hms\NotificationView::ERROR, "{$username} is a different gender. Please choose a roommate of the same gender."); $errorCmd->redirect(); } // If this student is an RLC-self-selection, then each roommate must be in the same RLC and in the selfselect-invite state too if ($rlcAssignment != null && $rlcAssignment->getStateName() == 'selfselect-invite') { // This student is an RLC-self-select, so check the roommate's RLC status $roommateRlcAssign = RlcMembershipFactory::getMembership($roommate, $term); // Make sure the roommate is a member of the same RLC and is eligible for self-selection if ($roommateRlcAssign == null || $roommateRlcAssign->getStateName() != 'selfselect-invite' || $rlcAssignment->getRlc()->getId() != $roommateRlcAssign->getRlc()->getId()) { NQ::simple('hms', hms\NotificationView::ERROR, "{$roommate} must be a member of the same learning community as you, and must also be eligible for self-selction."); $errorCmd->redirect(); } // Otherwise (if not RLC members), make sure each roommate is eligible } else { if (HMS_Lottery::determineEligibility($username) !== TRUE) { NQ::simple('hms', hms\NotificationView::ERROR, "{$username} is not eligible for assignment."); $errorCmd->redirect(); } } // If this student is a self-select RLC member, then this student must also be a self-select RLC member of the same RLC if ($rlcAssignment != null && $rlcAssignment->getStateName() == 'selfselect-invite') { $roommateRlcAssign = RlcMembershipFactory::getMembership($roommate, $term); if ($roommateRlcAssign == null || $roommateRlcAssign->getStateName() != 'selfselect-invite' || $rlcAssignment->getRlc()->getId() != $roommateRlcAssign->getRlc()->getId()) { NQ::simple('hms', hms\NotificationView::ERROR, "{$username} must be a member of the same learning community as you, and must also be eligible for self-selction."); $errorCmd->redirect(); } } } // If the room's gender is 'AUTO' and no one is assigned to it yet, switch it to the student's gender if ($room->gender_type == AUTO && $room->get_number_of_assignees() == 0) { $room->gender_type = $student->getGender(); $room->save(); } // Assign the student to the requested bed $bed_id = array_search(UserStatus::getUsername(), $roommates); // Find the bed id of the student who's logged in try { $result = HMS_Assignment::assignStudent($student, PHPWS_Settings::get('hms', 'lottery_term'), NULL, $bed_id, $mealPlan, 'Confirmed lottery invite', TRUE, ASSIGN_LOTTERY); } catch (Exception $e) { NQ::simple('hms', hms\NotificationView::ERROR, 'Sorry, there was an error creating your room assignment. Please try again or contact University Housing.'); $errorCmd->redirect(); } // Log the assignment HMS_Activity_Log::log_activity(UserStatus::getUsername(), ACTIVITY_LOTTERY_ROOM_CHOSEN, UserStatus::getUsername(), 'Captcha: ' . $captcha); // Update the student's meal plan in the housing application, just for future reference $app = HousingApplication::getApplicationByUser($student->getUsername(), $term); $app->setMealPlan($mealPlan); $app->save(); // If this student was an RLC self-select, update the RLC memberhsip state if ($rlcAssignment != null && $rlcAssignment->getStateName() == 'selfselect-invite') { $rlcAssignment->changeState(new RlcAssignmentSelfAssignedState($rlcAssignment)); } foreach ($roommates as $bed_id => $username) { // Skip the current user if ($username == $student->getUsername()) { continue; } # Reserve the bed for the roommate $expires_on = time() + INVITE_TTL_HRS * 3600; $bed = new HMS_Bed($bed_id); if (!$bed->lottery_reserve($username, $student->getUsername(), $expires_on)) { NQ::smiple('hms', hms\NotificationView::WARNING, "You were assigned, but there was a problem reserving space for your roommates. Please contact University Housing."); $successCmd->redirect(); } HMS_Activity_Log::log_activity($username, ACTIVITY_LOTTERY_REQUESTED_AS_ROOMMATE, $student->getUsername(), 'Expires: ' . HMS_Util::get_long_date_time($expires_on)); # Invite the selected roommates $roomie = StudentFactory::getStudentByUsername($username, $term); $term = PHPWS_Settings::get('hms', 'lottery_term'); $year = Term::toString($term) . ' - ' . Term::toString(Term::getNextTerm($term)); HMS_Email::send_lottery_roommate_invite($roomie, $student, $expires_on, $room->where_am_i(), $year); } HMS_Email::send_lottery_assignment_confirmation($student, $room->where_am_i(), $term); $successCmd->redirect(); }
public function execute(CommandContext $context) { PHPWS_Core::initModClass('hms', 'HMS_Room.php'); if (!Current_User::allow('hms', 'room_attributes')) { PHPWS_Core::initModClass('hms', 'exception/PermissionException.php'); throw new PermissionException('You do not have permission to edit rooms.'); } $roomId = $context->get('roomId'); $viewCmd = CommandFactory::getCommand('EditRoomView'); $viewCmd->setRoomId($roomId); // Create the room object given the room_id $room = new HMS_Room($roomId); if (!$room) { NQ::simple('hms', hms\NotificationView::ERROR, 'Invalid room.'); $viewCmd->redirect(); } // Check if the user is trying to change a room's gender to co-ed. // If so, make sure the user has the permission to do so. if ($room->getGender() != $context->get('gender_type') && $context->get('gender_type') == COED) { if (!Current_User::allow('hms', 'coed_rooms')) { NQ::simple('hms', hms\NotificationView::ERROR, 'Error: You do not have permission to change the room gender to co-ed. No changes were made.'); $viewCmd->redirect(); } } // Compare the room's gender and the gender the user selected // If they're not equal, call 'can_change_gender' public function if ($room->gender_type != $context->get('gender_type')) { if (!$room->can_change_gender($context->get('gender_type'))) { NQ::simple('hms', hms\NotificationView::ERROR, 'Error: Incompatible genders detected. No changes were made.'); $viewCmd->redirect(); } } // Check the default gender in the same way if ($room->default_gender != $context->get('default_gender')) { if (!$room->can_change_gender($context->get('default_gender'))) { NQ::simple('hms', hms\NotificationView::ERROR, 'Error: Default gender incompatable. No changes were made.'); $viewCmd->redirect(); } } if ($room->get_number_of_assignees() > 0 && $context->get('offline') == 1) { NQ::simple('hms', hms\NotificationView::ERROR, 'Error: Cannot take room offline while students are assigned to the room. No changes were made.'); $viewCmd->redirect(); } // Grab all the input from the form and save the room //Changed from radio buttons to checkboxes, ternary //prevents null since only 1 is defined as a return value //test($_REQUEST['room_number']); $room->room_number = $context->get('room_number'); $room->gender_type = $context->get('gender_type'); $room->default_gender = $context->get('default_gender'); $rlcReserved = $context->get('rlc_reserved'); if ($rlcReserved != 0) { $room->setReservedRlcId($rlcReserved); } else { $room->setReservedRlcId(null); } $room->offline = $context->get('offline') == 1 ? 1 : 0; $room->reserved = $context->get('reserved') == 1 ? 1 : 0; $room->ra = $context->get('ra') == 1 ? 1 : 0; $room->private = $context->get('private') == 1 ? 1 : 0; $room->overflow = $context->get('overflow') == 1 ? 1 : 0; $room->parlor = $context->get('parlor') == 1 ? 1 : 0; $room->ada = $context->get('ada') == 1 ? 1 : 0; $room->hearing_impaired = $context->get('hearing_impaired') == 1 ? 1 : 0; $room->bath_en_suite = $context->get('bath_en_suite') == 1 ? 1 : 0; $reservedReason = $context->get('reserved_reason'); if ($reservedReason == 'none') { $room->setReserved(0); } else { $room->setReserved(1); } $room->setReservedReason($reservedReason); $room->setReservedNotes($context->get('reserved_notes')); $result = $room->save(); if (!$result || PHPWS_Error::logIfError($result)) { NQ::simple('hms', hms\NotificationView::ERROR, 'There was a problem saving the room data. No changes were made.'); $viewCmd->redirect(); } NQ::simple('hms', hms\NotificationView::SUCCESS, 'The room was updated successfully.'); $viewCmd->redirect(); }
public static function deleteRoom($roomId) { if (!Current_User::allow('hms', 'room_structure')) { PHPWS_Core::initModClass('hms', 'exception/PermissionException.php'); throw new PermissionException('You do not have permission to delete a room.'); } // check that we're not about to do something stupid if (!isset($roomId)) { throw new InvalidArgumentException('Invalid room id.'); } $room = new HMS_Room($roomId); // make sure there isn't an assignment if ($room->get_number_of_assignees() != 0) { PHPWS_Core::initModClass('hms', 'exception/HallStructureException.php'); throw new HallStructureException('One or more students are currently assigned to that room and therefore it cannot deleted.'); } // delete any beds try { if ($room->loadBeds()) { if (!empty($room->_beds)) { foreach ($room->_beds as $bed) { HMS_Bed::deleteBed($bed->id); } } } $room->delete(); } catch (Exception $e) { throw $e; } return true; }
public function execute(CommandContext $context) { // Make sure the user has permission to change room attributes if (!Current_User::allow('hms', 'room_attributes')) { echo json_encode(false); die; } // Get the values from the request $id = $context->get('id'); $element = $context->get('field'); $value = $context->get('value'); // Make sure the required values were passed in on the request if (is_null($id) || is_null($element) || is_null($value)) { echo json_encode(false); die; } // Instantiate the room object try { $room = new HMS_Room($id); } catch (Exception $e) { echo json_encode(false); die; } /********** * Gender * */ // If the user is trying to change the gender, make sure no one is assigned if ($element == 'gender_type') { if ($room->get_number_of_assignees() > 0) { echo json_encode(false); die; } } // Check if the user is trying to change a room's gender to co-ed. // If so, make sure the user has the permission to do so. if ($element == 'gender_type' && $room->getGender() != $value && $value == COED) { if (!Current_User::allow('hms', 'coed_rooms')) { echo json_encode(false); die; } } // If the gender field was changed if ($element == 'gender_type' && $room->getGender() != $value) { // Make sure the requested gender is compatiable with the hall/floor if ($room->can_change_gender($value)) { $room->setGender($value); } else { echo json_encode(false); die; } } /****************** * Default Gender * */ // If default gender was changed if ($element == 'default_gender' && $room->getDefaultGender() != $value) { // Make sure the requested default gender is compatiable with the hall/floor if ($room->can_change_gender($value)) { $room->setDefaultGender($value); } else { echo json_encode(false); die; } } /* RLC Reservation */ if ($element == 'rlc_reserved') { if ($value <= 0) { $room->setReservedRlcId(null); } else { $room->setReservedRlcId($value); } } // A switch statement for all the check boxes switch ($element) { case 'ra': $room->setRa($value); break; case 'private': $room->setPrivate($value); break; case 'overflow': $room->setOverflow($value); break; case 'ada': $room->setADA($value); break; case 'reserved': $room->setReserved($value); break; case 'offline': $room->setOffline($value); break; } try { $room->save(); } catch (Exception $e) { echo json_encode(false); die; } echo json_encode($room); die; }