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 function execute(CommandContext $context) { if (!UserStatus::isAdmin() || !Current_User::allow('hms', 'assignment_maintenance')) { PHPWS_Core::initModClass('hms', 'exception/PermissionException.php'); throw new PermissionException('You do not have permission to assign students.'); } PHPWS_Core::initModClass('hms', 'HousingApplication.php'); PHPWS_Core::initModClass('hms', 'FallApplication.php'); PHPWS_Core::initModClass('hms', 'HMS_Assignment.php'); PHPWS_Core::initModClass('hms', 'StudentFactory.php'); PHPWS_Core::initModClass('hms', 'HMS_Room.php'); PHPWS_Core::initModClass('hms', 'HMS_Activity_Log.php'); PHPWS_Core::initModClass('hms', 'BannerQueue.php'); // NB: Username must be all lowercase $username = strtolower(trim($context->get('username'))); $term = Term::getSelectedTerm(); // Setup command to redirect to in case of error $errorCmd = CommandFactory::getCommand('ShowAssignStudent'); $errorCmd->setUsername($username); /*** * Input Sanity Checking */ // Must supply a user name if (is_null($username)) { NQ::simple('hms', hms\NotificationView::ERROR, 'Invalid or missing username.'); $errorCmd->redirect(); } // Must supply at least a room ID $roomId = $context->get('room'); if (is_null($roomId) || $roomId == 0) { NQ::simple('hms', hms\NotificationView::ERROR, 'You must select a room.'); $errorCmd->redirect(); } // Must choose an assignment type $assignmentType = $context->get('assignment_type'); if (!isset($assignmentType) || is_null($assignmentType) || $assignmentType < 0) { NQ::simple('hms', hms\NotificationView::ERROR, 'You must choose an assignment type.'); $errorCmd->redirect(); } // Check to make sure the student has an application on file $applicationStatus = HousingApplication::checkForApplication($username, $term); if ($applicationStatus == FALSE) { NQ::simple('hms', hms\NotificationView::WARNING, 'Warning: No housing application found for this student in this term.'); } // If the student is already assigned, redirect to the confirmation screen. If the student is already assigned // and the confirmation flag is true, then set a flag and proceed. $moveNeeded = FALSE; if (HMS_Assignment::checkForAssignment($username, $term)) { if ($context->get('moveConfirmed') == 'true') { // Move has been confirmed $moveNeeded = true; } else { // Redirect to the move confirmation interface $moveConfirmCmd = CommandFactory::getCommand('ShowAssignmentMoveConfirmation'); $moveConfirmCmd->setUsername($username); $moveConfirmCmd->setRoom($context->get('room')); $moveConfirmCmd->setBed($context->get('bed')); $moveConfirmCmd->setMealPlan($context->get('meal_plan')); $moveConfirmCmd->setAssignmentType($assignmentType); $moveConfirmCmd->setNotes($context->get('note')); $moveConfirmCmd->redirect(); } } try { $student = StudentFactory::getStudentByUsername($username, $term); } catch (StudentNotFoundException $e) { NQ::simple('hms', hms\NotificationView::ERROR, 'Invalid user name, no such student found.'); $errorCmd->redirect(); } // Check age, issue a warning for over 25 if (strtotime($student->getDOB()) < strtotime("-25 years")) { NQ::simple('hms', hms\NotificationView::WARNING, 'Student is 25 years old or older!'); } $gender = $student->getGender(); if (!isset($gender) || is_null($gender)) { throw new InvalidArgumentException('Missing student gender.'); } // Create the room object so we can check gender $room = new HMS_Room($roomId); if (!$room) { NQ::simple('hms', hms\NotificationView::ERROR, 'Error creating the room object.'); $errorCmd->redirect(); } // Create the hall object for later $floor = $room->get_parent(); $hall = $floor->get_parent(); // If the room is Co-ed, make sure the user has permission to assign to co-ed rooms if ($room->getGender() == COED && !Current_User::allow('hms', 'coed_assignment')) { NQ::simple('hms', hms\NotificationView::ERROR, 'Error: You do not have permission to assign students to co-ed rooms.'); $errorCmd->redirect(); } // Make sure the student's gender matches the gender of the room, unless the room is co-ed. if ($room->getGender() != $gender && $room->getGender() != COED) { // Room gender does not match student's gender, so check if we can change it if ($room->can_change_gender($gender) && Current_User::allow('hms', 'room_attributes')) { $room->setGender($gender); $room->save(); NQ::simple('hms', hms\NotificationView::WARNING, 'Warning: Changing room gender.'); } else { NQ::simple('hms', hms\NotificationView::ERROR, 'Error: The student\'s gender and the room\'s gender do not match and the room could not be changed.'); $errorCmd->redirect(); } } // If the user is attempting to re-assign and has confirmed the move, // then unassign the student first. if ($moveNeeded) { try { //TODO don't hard-code refund percentage to 100% HMS_Assignment::unassignStudent($student, $term, '(re-assign)', UNASSIGN_REASSIGN, 100); } catch (Exception $e) { NQ::simple('hms', hms\NotificationView::ERROR, "Error deleting current assignment. {$username} was not removed."); $errorCmd->redirect(); } } // Actually try to make the assignment, decide whether to use the room id or the bed id $bed = $context->get('bed'); try { if (isset($bed) && $bed != 0) { HMS_Assignment::assignStudent($student, $term, NULL, $bed, $context->get('meal_plan'), $context->get('note'), false, $context->get('assignment_type')); } else { HMS_Assignment::assignStudent($student, $term, $context->get('room'), NULL, $context->get('meal_plan'), $context->get('note'), false, $context->get('assignment_type')); } } catch (AssignmentException $e) { NQ::simple('hms', hms\NotificationView::ERROR, 'Assignment error: ' . $e->getMessage()); $errorCmd->redirect(); } // Show a success message if ($context->get('moveConfirmed') == 'true') { NQ::simple('hms', hms\NotificationView::SUCCESS, 'Successfully moved ' . $username . ' to ' . $hall->hall_name . ' room ' . $room->room_number); } else { NQ::simple('hms', hms\NotificationView::SUCCESS, 'Successfully assigned ' . $username . ' to ' . $hall->hall_name . ' room ' . $room->room_number); } $successCmd = CommandFactory::getCommand('ShowAssignStudent'); $successCmd->redirect(); }
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; }