Пример #1
0
 /**
  * Builds a new set of question-answer pairs by taking $questions and using
  * only questions with id in $questionIds. Questions in $questionIds that
  * are not in $questions have an empty answer.
  */
 protected static function pruneQuestionsByIdSet($questions, $questionIds)
 {
     $questionSet = [];
     // Create a hash from _id to the _id-answer pair.
     $questionsHash = arrayToHashByKey($questions, '_id');
     // For all the ids in $questionIds, add an _id-answer pair to questionSet.
     foreach ($questionIds as $questionId) {
         $questionId .= '';
         if (isset($questionsHash[$questionId])) {
             $questionSet[] = $questionsHash[$questionId];
         } else {
             $questionSet[] = ['_id' => $questionId, 'answer' => ''];
         }
     }
     return $questionSet;
 }
 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]);
 }
Пример #3
0
 /**
  * Update a student document's 'answers' field with new saved answers.
  */
 private static function saveStudentAnswers(Application $application)
 {
     $studentId = $application->getStudentId();
     $questions = $application->getQuestions();
     if (count($questions) == 0) {
         return;
     }
     // Retrieves the question-answer pairs.
     $answers = StudentModel::getAnswers($studentId);
     // Loop through $questions, replace answer for the corresponding question
     // in $answers, or append to $answers if it is not in $answers.
     $answersHash = arrayToHashByKey($answers, '_id', 'index');
     foreach ($questions as $question) {
         $questionId = $question['_id'];
         $newAnswer = $question['answer'];
         if (isset($answersHash[$questionId . ''])) {
             $index = $answersHash[$questionId . ''];
             $answers[$index]['answer'] = $newAnswer;
         } else {
             $answers[] = ['_id' => $questionId, 'answer' => $newAnswer];
         }
     }
     StudentModel::replaceAnswers($studentId, $answers);
 }