public function execute(CommandContext $context)
 {
     $term = $context->get('term');
     $student = StudentFactory::getStudentByUsername(UserStatus::getUsername(), Term::getCurrentTerm());
     $errorCmd = CommandFactory::getCommand('ShowRlcApplicationView');
     $errorCmd->setTerm($term);
     $choice1 = new HMS_Learning_Community($context->get('rlc_first_choice'));
     $choice2 = new HMS_Learning_Community($context->get('rlc_second_choice'));
     $choice3 = new HMS_Learning_Community($context->get('rlc_third_choice'));
     if (!$choice1->allowStudentType($student->getType()) || $choice2->id != -1 && !$choice2->allowStudentType($student->getType()) || $choice3->id != -1 && !$choice3->allowStudentType($student->getType())) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Sorry, you cannot apply for the selected RLC. Please contact University Housing if you believe this to be in error.');
         $errorCmd->redirect();
     }
     // Check the lengths of the responses to the short answer questions
     $question0 = $context->get('rlc_question_0');
     $question1 = $context->get('rlc_question_1');
     $question2 = $context->get('rlc_question_2');
     $whySpecific = $context->get('why_specific_communities');
     $strengthsWeaknesses = $context->get('strengths_weaknesses');
     if (str_word_count($whySpecific) > HMS_RLC_Application::RLC_RESPONSE_WORD_LIMIT) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Your respose to the question is too long. Please limit your response to ' . HMS_RLC_Application::RLC_RESPONSE_WORD_LIMIT . ' words.');
         $errorCmd->redirect();
     }
     if (str_word_count($strengthsWeaknesses) > HMS_RLC_Application::RLC_RESPONSE_WORD_LIMIT) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Your respose to the question is too long. Please limit your response to ' . HMS_RLC_Application::RLC_RESPONSE_WORD_LIMIT . ' words.');
         $errorCmd->redirect();
     }
     if (str_word_count($question0) > HMS_RLC_Application::RLC_RESPONSE_WORD_LIMIT) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Your respose to the first question is too long. Please limit your response to ' . HMS_RLC_Application::RLC_RESPONSE_WORD_LIMIT . ' words.');
         $errorCmd->redirect();
     }
     if (str_word_count($question1) > HMS_RLC_Application::RLC_RESPONSE_WORD_LIMIT) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Your respose to the second question is too long. Please limit your response to ' . HMS_RLC_Application::RLC_RESPONSE_WORD_LIMIT . ' words.');
         $errorCmd->redirect();
     }
     if (str_word_count($question2) > HMS_RLC_Application::RLC_RESPONSE_WORD_LIMIT) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Your respose to the third question is too long. Please limit your response to ' . HMS_RLC_Application::RLC_RESPONSE_WORD_LIMIT . ' words.');
         $errorCmd->redirect();
     }
     // Check for an existing application and delete it
     $oldApp = HMS_RLC_Application::getApplicationByUsername($student->getUsername(), $term);
     if (isset($oldApp) && $oldApp->id != NULL) {
         //TODO check if the student has already been assigned to an RLC via the old application
         // Delete the old application to make way for this one
         try {
             $oldApp->delete();
         } catch (Exception $e) {
             NQ::simple('hms', hms\NotificationView::ERROR, 'Sorry, an error occured while attempting to replace your existing Residential Learning Community Application.  If this problem persists please contact University Housing.');
             $errorCmd->redirect();
         }
     }
     // Setup the new application
     $application = new HMS_RLC_Application();
     $application->setUsername($student->getUsername());
     $application->setDateSubmitted(time());
     $application->setFirstChoice($context->get('rlc_first_choice'));
     $application->setSecondChoice($choice2->id > 0 ? $choice2->id : NULL);
     $application->setThirdChoice($choice3->id > 0 ? $choice3->id : NULL);
     $application->setWhySpecificCommunities($context->get('why_specific_communities'));
     $application->setStrengthsWeaknesses($context->get('strengths_weaknesses'));
     $application->setRLCQuestion0($context->get('rlc_question_0'));
     $application->setRLCQuestion1(is_null($context->get('rlc_question_1')) ? '' : $context->get('rlc_question_1'));
     $application->setRLCQuestion2(is_null($context->get('rlc_question_2')) ? '' : $context->get('rlc_question_2'));
     $application->setEntryTerm($context->get('term'));
     $application->setApplicationType(RLC_APP_FRESHMEN);
     try {
         $application->save();
     } catch (Exception $e) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Sorry, an error occured while attempting to submit your application.  If this problem persists please contact University Housing.');
         $errorCmd->redirect();
     }
     # Log that this happened
     PHPWS_Core::initModClass('hms', 'HMS_Activity_Log.php');
     HMS_Activity_Log::log_activity($student->getUsername(), ACTIVITY_SUBMITTED_RLC_APPLICATION, $student->getUsername());
     # Send the notification email
     PHPWS_Core::initModClass('hms', 'HMS_Email.php');
     HMS_Email::send_rlc_application_confirmation($student);
     # Show a success message and redirect
     NQ::simple('hms', hms\NotificationView::SUCCESS, 'Your Residential Learning Community (RLC) application has been successfully submitted. You should receive a confirmation email (sent to your Appalachian State email account) soon. Notification of your acceptance into an RLC will also be sent to your Appalachian State email account.  Please continue to check your ASU email account regularly.  For more information on the RLC acceptance timeline or frequently asked questions, please visit <a href="http://housing.appstate.edu/rlc" target="_blank">housing.appstate.edu/rlc</a>.');
     $cmd = CommandFactory::getCommand('ShowStudentMenu');
     $cmd->redirect();
 }
 public function execute(CommandContext $context)
 {
     PHPWS_Core::initModClass('hms', 'StudentFactory.php');
     PHPWS_Core::initModClass('hms', 'HMS_Learning_Community.php');
     PHPWS_Core::initModClass('hms', 'HMS_RLC_Application.php');
     PHPWS_Core::initModClass('hms', 'HMS_RLC_Assignment.php');
     PHPWS_Core::initModClass('hms', 'HousingApplication.php');
     $term = $context->get('term');
     $student = StudentFactory::getStudentByUsername(UserStatus::getUsername(), $term);
     // Commands for re-directing later
     $formCmd = CommandFactory::getCommand('ShowRlcReapplication');
     $formCmd->setTerm($term);
     // $menuCmd = CommandFactory::getCommand('ShowStudentMenu');
     // Pull in data for local use
     $rlcOpt = $context->get('rlc_opt');
     $rlcChoice1 = $context->get('rlc_choice_1');
     $rlcChoice2 = $context->get('rlc_choice_2');
     $rlcChoice3 = $context->get('rlc_choice_3');
     $why = $context->get('why_this_rlc');
     $contribute = $context->get('contribute_gain');
     // Change any 'none's into null
     if ($rlcChoice2 == 'none') {
         $rlcChoice2 = null;
     }
     if ($rlcChoice3 == 'none') {
         $rlcChoice3 = null;
     }
     # Get the list of RLCs that the student is eligible for
     # Note: hard coded to 'C' because we know they're continuing at this point.
     # This accounts for freshmen addmitted in the spring, who will still have the 'F' type.
     $communities = HMS_Learning_Community::getRlcListReapplication(false, 'C');
     # Look up any existing RLC assignment (for the current term, should be the Spring term)
     $rlcAssignment = HMS_RLC_Assignment::getAssignmentByUsername($student->getUsername(), Term::getPrevTerm(Term::getCurrentTerm()));
     // Sanity checking on user-supplied data
     // If the student is already in an RLC, and the student is eligible to reapply for that RLC (RLC always takes returners,
     // or the RLC is in the list of communities this student is eligible for), then check to make the user chose something for the re-apply option.
     if (!is_null($rlcAssignment) && (array_key_exists($rlcAssignment->getRlcId(), $communities) || $rlcAssignment->getRlc()->getMembersReapply() == 1) && is_null($rlcOpt)) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Please choose whether you would like to continue in your currnet RLC, or apply for a different community.');
         $formCmd->redirect();
     }
     // If the user is 'contining' in his/her current RLC, then figure that out and set it
     if (!is_null($rlcOpt) && $rlcOpt == 'continue') {
         $rlcChoice1 = $rlcAssignment->getRLC()->get_id();
         $rlcChoice2 = NULL;
         $rlcChoice3 = NULL;
     } else {
         // User either can't 'continue' or didn't want to. Check that the user supplied rankings isstead.
         // Make sure a first choice was made
         if ($rlcChoice1 == 'select') {
             NQ::simple('hms', hms\NotificationView::ERROR, 'You must choose a community as your "first choice".');
             $formCmd->redirect();
         }
         if (isset($rlcChoice2) && $rlcChoice1 == $rlcChoice2 || isset($rlcChoice2) && isset($rlcChoice3) && $rlcChoice2 == $rlcChoice3 || isset($rlcChoice3) && $rlcChoice1 == $rlcChoice3) {
             NQ::simple('hms', hms\NotificationView::ERROR, 'You cannot choose the same community twice.');
             $formCmd->redirect();
         }
     }
     // Check the short answer questions
     if (empty($why) || empty($contribute)) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Please respond to both of the short answer questions.');
         $formCmd->redirect();
     }
     $wordLimit = 500;
     if (str_word_count($why) > $wordLimit) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Your answer to question number one is too long. Please limit your response to 500 words or less.');
         $formCmd->redirect();
     }
     $wordLimit = 500;
     if (str_word_count($contribute) > $wordLimit) {
         NQ::simple('hms', hms\NotificationView::ERROR, 'Your answer to question number two is too long. Please limit your response to 500 words or less.');
         $formCmd->redirect();
     }
     $app = new HMS_RLC_Application();
     $app->setUsername($student->getUsername());
     $app->setFirstChoice($rlcChoice1);
     $app->setSecondChoice($rlcChoice2);
     $app->setThirdChoice($rlcChoice3);
     $app->setWhySpecificCommunities($why);
     $app->setStrengthsWeaknesses($contribute);
     $_SESSION['RLC_REAPP'] = $app;
     // Redirect to the page 2 view command
     $page2cmd = CommandFactory::getCommand('ShowRlcReapplicationPageTwo');
     $page2cmd->setTerm($term);
     $page2cmd->redirect();
 }
 public function execute(CommandContext $context)
 {
     if (!Current_User::allow('hms', 'add_rlc_members')) {
         PHPWS_Core::initModClass('hms', 'exception/PermissionException.php');
         throw new PermissionException('You do not have permission to view RLC members.');
     }
     PHPWS_Core::initModClass('hms', 'HMS_Learning_Community.php');
     PHPWS_Core::initModClass('hms', 'StudentFactory.php');
     PHPWS_Core::initModClass('hms', 'HousingApplicationFactory.php');
     PHPWS_Core::initModClass('hms', 'RlcApplicationFactory.php');
     PHPWS_Core::initModClass('hms', 'RlcMembershipFactory.php');
     // Get the selected term
     $term = Term::getSelectedTerm();
     // Get the request community
     $communityId = $context->get('communityId');
     if (!isset($communityId) || $communityId == '') {
         throw new InvalidArgumentException('Missing community id.');
     }
     $community = new HMS_Learning_Community($communityId);
     // Get banner ID list and make sure it has content
     $bannerIds = $context->get('banner_id_list');
     if (!isset($bannerIds) || $bannerIds == '') {
         $errorCmd = CommandFactory::getCommand('ShowAdminAddRlcMember');
         $errorCmd->setCommunity($community);
         $errorCmd->redirect();
     }
     // Break up string into an array of individual ids
     $bannerIds = explode("\n", $bannerIds);
     foreach ($bannerIds as $banner) {
         // Clean up the banner id
         $banner = trim($banner);
         // Skip blank lines
         if ($banner == '') {
             continue;
         }
         // Get the student
         try {
             $student = StudentFactory::getStudentByBannerId($banner, $term);
         } catch (StudentNotFoundException $e) {
             NQ::simple('hms', hms\NotificationView::ERROR, "Couldn't find a student with ID: {$e->getRequestedId()}");
             continue;
         } catch (InvalidArgumentException $e) {
             NQ::simple('hms', hms\NotificationView::ERROR, "This doesn't look like a banner ID: {$banner}");
             continue;
         }
         // Check for an existing housing application
         $housingApp = HousingApplicationFactory::getAppByStudent($student, $term);
         // If no housing app, show a warning
         if (is_null($housingApp)) {
             NQ::simple('hms', hms\NotificationView::WARNING, "No housing application found for: {$student->getName()}({$student->getBannerID()})");
         }
         // Check for an existing learning community application
         $rlcApp = RlcApplicationFactory::getApplication($student, $term);
         if ($rlcApp == null) {
             // Create a new learning community application
             $rlcApp = new HMS_RLC_Application();
             $rlcApp->setUsername($student->getUsername());
             $rlcApp->setDateSubmitted(time());
             $rlcApp->setFirstChoice($community->getId());
             $rlcApp->setSecondChoice(null);
             $rlcApp->setThirdChoice(null);
             $rlcApp->setWhySpecificCommunities('Application created administratively.');
             $rlcApp->setStrengthsWeaknesses('');
             $rlcApp->setRLCQuestion0(null);
             $rlcApp->setRLCQuestion1(null);
             $rlcApp->setRLCQuestion2(null);
             $rlcApp->setEntryTerm($term);
             if ($student->getType() == TYPE_CONTINUING) {
                 $rlcApp->setApplicationType(RLC_APP_RETURNING);
             } else {
                 $rlcApp->setApplicationType(RLC_APP_FRESHMEN);
             }
             $rlcApp->save();
         } else {
             // Reset the application's denial flag, see #1026
             $rlcApp->setDenied(0);
             $rlcApp->save();
             // RLC application already exists
             NQ::simple('hms', hms\NotificationView::WARNING, "RLC application already exists for {$student->getName()}({$student->getBannerID()})");
         }
         // Check for RLC membership
         $membership = RlcMembershipFactory::getMembership($student, $term);
         if ($membership !== false) {
             NQ::simple('hms', hms\NotificationView::ERROR, "RLC membership already exists for {$student->getName()}({$student->getBannerID()})");
             continue;
         }
         // Check Student's Eligibility
         $eligibility = HMS_Lottery::determineEligibility($student->getUsername());
         if ($eligibility == false) {
             NQ::simple('hms', hms\NotificationView::ERROR, "{$student->getName()} ({$student->getBannerID()}) is not currently eligible for housing");
             continue;
         }
         // Create RLC Membership
         $membership = new HMS_RLC_Assignment();
         $membership->rlc_id = $community->getId();
         $membership->gender = $student->getGender();
         $membership->assigned_by = UserStatus::getUsername();
         $membership->application_id = $rlcApp->id;
         $membership->state = 'new';
         $membership->save();
     }
     $successCmd = CommandFactory::getCommand('ShowViewByRlc');
     $successCmd->setRlcId($community->getId());
     $successCmd->redirect();
 }