public function show()
 {
     $tpl = array();
     PHPWS_Core::initModClass('hms', 'HMS_Roommate.php');
     PHPWS_Core::initModClass('hms', 'HMS_Util.php');
     $roommate = HMS_Roommate::get_confirmed_roommate(UserStatus::getUsername(), $this->term);
     $requests = HMS_Roommate::countPendingRequests(UserStatus::getUsername(), $this->term);
     $tpl['DATES'] = HMS_Util::getPrettyDateRange($this->startDate, $this->endDate);
     $tpl['STATUS'] = "";
     // Roommate has been selected and confirmed
     if (!is_null($roommate)) {
         $name = $roommate->getFullName();
         $tpl['ROOMMATE_MSG'] = "<b>{$name}</b> has confirmed your roommate request. Roommate requests are subject to space availability.";
         $tpl['ICON'] = FEATURE_COMPLETED_ICON;
         if (time() < $this->editDate) {
             $cmd = CommandFactory::GetCommand('ShowRoommateBreak');
             $rm = HMS_Roommate::getByUsernames(UserStatus::getUsername(), $roommate->getUsername(), $this->term);
             $cmd->setRoommateId($rm->id);
             $tpl['ROOMMATE_BREAK'] = $cmd->getLink('Remove roommate request');
         }
     } else {
         if (time() < $this->startDate) {
             $tpl['ROOMMATE_MSG'] = '<b>It is too early to choose a roommate.</b> You can choose a roommate on ' . HMS_Util::getFriendlyDate($this->startDate) . '.';
             $tpl['ICON'] = FEATURE_NOTYET_ICON;
         } else {
             if (time() > $this->endDate) {
                 $tpl['ROOMMATE_MSG'] = '<b>It is too late to choose a roommate.</b> The deadline passed on ' . HMS_Util::getFriendlyDate($this->endDate) . '.';
                 // fade out header
                 $tpl['STATUS'] = "locked";
                 $tpl['ICON'] = FEATURE_LOCKED_ICON;
             } else {
                 if ($requests > 0) {
                     $tpl['ROOMMATE_REQUESTS'] = HMS_Roommate::display_requests(UserStatus::getUsername(), $this->term);
                     if ($requests == 1) {
                         $tpl['ROOMMATE_REQUESTS_MSG'] = "<b style='color: #F00'>You have a roommate request.</b> Please click the name below to confirm or reject the request.";
                     } else {
                         $tpl['ROOMMATE_REQUESTS_MSG'] = "<b style='color: #F00'>You have roommate requests.</b> Please click a name below to confirm or reject a request.";
                     }
                 } else {
                     if (HMS_Roommate::has_roommate_request(UserStatus::getUsername(), $this->term)) {
                         $tpl['ICON'] = FEATURE_COMPLETED_ICON;
                         $tpl['ROOMMATE_MSG'] = "<b>You have selected a roommate</b> and are awaiting their approval.";
                         $requestee = HMS_Roommate::get_unconfirmed_roommate(UserStatus::getUsername(), $this->term);
                         if (time() < $this->editDate) {
                             $rm = HMS_Roommate::getByUsernames(UserStatus::getUsername(), $requestee, $this->term);
                             $cmd = CommandFactory::getCommand('RoommateRequestCancel');
                             $cmd->setRoommateId($rm->id);
                             $tpl['ROOMMATE_BREAK'] = $cmd->getLink('Cancel Request');
                         }
                     } else {
                         $tpl['ICON'] = FEATURE_OPEN_ICON;
                         $tpl['ROOMMATE_MSG'] = 'If you know who you want your roommate to be, <b>you may select your roommate now</b>. You will need to know your roommate\'s ASU user name (their e-mail address). You have until ' . HMS_Util::getFriendlyDate($this->endDate) . ' to choose a roommate. Click the link below to select your roommate.';
                         $cmd = CommandFactory::getCommand('ShowRequestRoommate');
                         $cmd->setTerm($this->term);
                         $tpl['ROOMMATE_LINK'] = $cmd->getLink('Select Your Roommate');
                     }
                 }
             }
         }
     }
     Layout::addPageTitle("Roommate Selection");
     return PHPWS_Template::process($tpl, 'hms', 'student/menuBlocks/roommateMenuBlock.tpl');
 }
 public function execute(CommandContext $context)
 {
     if (!UserStatus::isAdmin() || !Current_User::allow('hms', 'roommate_maintenance')) {
         PHPWS_Core::initModClass('hms', 'exception/PermissionException.php');
         throw new PermissionException('You do not have permission to create/edit roommate groups.');
     }
     PHPWS_Core::initModClass('hms', 'StudentFactory.php');
     PHPWS_Core::initModClass('hms', 'HMS_Roommate.php');
     $term = Term::getSelectedTerm();
     # Check for reasonable input
     $roommate1 = trim($context->get('roommate1'));
     $roommate2 = trim($context->get('roommate2'));
     $viewCmd = CommandFactory::getCommand('CreateRoommateGroupView');
     $viewCmd->setRoommate1($roommate1);
     $viewCmd->setRoommate2($roommate2);
     if (is_null($roommate1) || empty($roommate1) || is_null($roommate2) || empty($roommate2)) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Invalid user names.');
         $viewCmd->redirect();
     }
     try {
         $student1 = StudentFactory::getStudentByUsername($roommate1, $term);
         $student2 = StudentFactory::getStudentByUsername($roommate2, $term);
     } catch (StudentNotFoundException $e) {
         NQ::simple('hms', hms\NotificationView::ERROR, $e->getMessage());
         $viewCmd->redirect();
     }
     try {
         # Check if these two can live together
         HMS_Roommate::canLiveTogetherAdmin($student1, $student2, $term);
     } catch (Exception $e) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Could not create roommate group: ' . $e->getMessage());
         $viewCmd->redirect();
     }
     # Check for pending requests for either roommate and break them
     if (HMS_Roommate::countPendingRequests($roommate1, $term) > 0) {
         NQ::simple('hms', hms\NotificationView::WARNING, "Warning: Pending roommate requests for {$roommate1} were deleted.");
     }
     $result = HMS_Roommate::removeOutstandingRequests($roommate1, $term);
     if (!$result) {
         NQ::simple('hms', hms\NotificationView::ERROR, "Error removing pending requests for {$roommate1}, roommate group was not created.");
         $viewCmd->redirect();
     }
     if (HMS_Roommate::countPendingRequests($roommate2, $term) > 0) {
         NQ::simple('hms', hms\NotificationView::WARNING, "Warning: Pending roommate requests for {$roommate2} were deleted.");
     }
     $result = HMS_Roommate::removeOutstandingRequests($roommate2, $term);
     if (!$result) {
         NQ::simple('hms', hms\NotificationView::ERROR, "Error removing pending requests for {$roommate2}, roommate group was not created.");
         $viewCmd->redirect();
     }
     # Create the roommate group and save it
     $roommate_group = new HMS_Roommate();
     $roommate_group->term = $term;
     $roommate_group->requestor = $roommate1;
     $roommate_group->requestee = $roommate2;
     $roommate_group->confirmed = 1;
     $roommate_group->requested_on = time();
     $roommate_group->confirmed_on = time();
     $result = $roommate_group->save();
     if (!$result) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Error saving roommate group.');
         $viewCmd->redirect();
     } else {
         PHPWS_Core::initModClass('hms', 'HMS_Activity_Log.php');
         HMS_Activity_Log::log_activity($roommate1, ACTIVITY_ADMIN_ASSIGNED_ROOMMATE, UserStatus::getUsername(), $roommate2);
         HMS_Activity_Log::log_activity($roommate2, ACTIVITY_ADMIN_ASSIGNED_ROOMMATE, UserStatus::getUsername(), $roommate1);
         NQ::simple('hms', hms\NotificationView::SUCCESS, 'Roommate group created successfully.');
         $viewCmd->redirect();
     }
 }