public static function apply(array $restOfRoute)
 {
     global $params;
     StudentController::requireLogin();
     $jobId = self::getIdFromRoute($restOfRoute);
     if (is_null($jobId)) {
         return;
     }
     $studentId = $_SESSION['_id'];
     $application = ApplicationJob::get($jobId);
     // Make sure job exists.
     if (!self::checkJobExists($jobId)) {
         return;
     }
     // Saving of application.
     if (isset($params['questions'])) {
         ApplicationStudent::save($jobId, $studentId, $params['questions']);
         return;
     }
     $submitted = false;
     // Submitting of application.
     if ($params) {
         self::submit($jobId, $studentId, $params);
     }
     $entry = JobModel::getById($jobId);
     $companyId = $entry['company'];
     $company = CompanyModel::getById($companyId);
     $questions = array();
     if (ApplicationModel::applicationExists($jobId, $studentId)) {
         $applicationData = ApplicationModel::getApplication($jobId, $studentId);
         $application = new ApplicationStudent($applicationData);
         $applicationId = $application->getId();
         $submitted = ApplicationModel::checkSubmitted($application->getId());
         if ($submitted) {
             //student succesfully submitted application
             self::redirect("../application/{$applicationId}");
         }
         foreach ($application->getQuestions() as $question) {
             $_id = $question['_id'];
             $questions[] = ['_id' => $_id, 'text' => Question::getTextById($_id), 'answer' => $question['answer']];
         }
     } else {
         JobModel::incrementApply($jobId);
         foreach ($application->getQuestions() as $questionId) {
             $answer = '';
             $answers = StudentModel::getAnswers($studentId);
             $answers = arrayToHashByKey($answers, '_id');
             if (isset($answers[$questionId . ''])) {
                 $answer = $answers[$questionId . '']['answer'];
             } else {
                 $answer = '';
             }
             $questions[] = ['_id' => $questionId, 'text' => Question::getTextById($questionId), 'answer' => $answer];
         }
     }
     self::render('jobs/applications/apply', ['questions' => $questions, 'jobtitle' => $entry['title'], 'companytitle' => $company['name'], 'jobId' => $jobId, 'submitted' => $submitted]);
 }