예제 #1
0
 public function executeSubmit()
 {
     header('Cache-Control: no-cache, must-revalidate');
     header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
     header('Content-type: application/json');
     $this->jsonwrapper = new jsonwrapper();
     /* Check sudah login atau belum */
     if (!$this->getUser()->isAuthenticated2('bo')) {
         $this->jsonwrapper->show_json_error('not login', 'Sorry, You must logged in to access this page.');
     }
     $user_id = $this->getContext()->getUser()->getAttribute('user_id', null, 'bo');
     /* Check apakah user terdaftar */
     $applicant = TestApplicantPeer::retrieveByPk($user_id);
     if (!$applicant) {
         $this->jsonwrapper->show_json_error('not register', 'Sorry, Please register first.');
     }
     /* check ada parameter question atau tidak */
     $data = $this->getRequestParameter('question');
     if (empty($data)) {
         $this->jsonwrapper->show_json_error('allQuestionError', 'Please choose an option.');
     }
     $errorAnswer = array();
     /* Cari Soal */
     $c = new Criteria();
     $c->addAscendingOrderByColumn(AngketpmbSoalPeer::ORDERING);
     $soal = AngketpmbSoalPeer::doSelect($c);
     if (!$soal) {
         $this->jsonwrapper->show_json_error('allQuestionError', 'Question is no longer exist. Please refresh the page.');
     }
     foreach ($soal as $s) {
         if (!array_key_exists($s->getId(), $data)) {
             $errorAnswer[] = array('id' => $s->getId(), 'message' => 'Please choose an option.');
         }
     }
     /* Start Transaction */
     $connection = Propel::getConnection('propel');
     $connection->begin();
     /* Getting or Creating Angket Session */
     $c = new Criteria();
     $c->add(AngketpmbSessionPeer::TEST_APPLICANT_ID, $applicant->getId(), Criteria::EQUAL);
     $angketSession = AngketpmbSessionPeer::doSelectOne($c);
     if (!$angketSession) {
         try {
             $angketSession = new AngketpmbSession();
             $angketSession->setTestApplicantId($applicant->getId());
             $angketSession->save();
         } catch (Exception $e) {
             $connection->rollback();
             $this->jsonwrapper->print_json(array('success' => 0, 'code' => 'processError', 'message' => 'Sorry, there is an error while saving your answer. Please trye again later', 'hideMessage' => 'Error while create applicant session'));
             die;
         }
     }
     /* check apakah ada pertanyaan yang belum dijawab */
     foreach ($data as $key => $value) {
         $soal = AngketpmbSoalPeer::retrieveByPk($key);
         /* Jika soal tidak ketemu */
         if (!$soal) {
             $errorAnswer[] = array('id' => $key, 'message' => 'Question is no longer exist. Please refresh the page.');
             continue;
         }
         /* Jika belum dijawab */
         if (empty($value)) {
             $errorAnswer[] = array('id' => $key, 'message' => 'Please choose an option.');
             continue;
         }
         /* Jika jawaban tidak ketemu */
         $option = AngketpmbOptionPeer::retrieveByPk($value);
         if (!$option) {
             $errorAnswer[] = array('id' => $key, 'message' => 'Option you choosed is no longer exist. Please refresh the page.');
             continue;
         }
         /* Jika jawaban bukan untuk soal ini */
         if ($option->getSoalId() != $soal->getId()) {
             $errorAnswer[] = array('id' => $key, 'message' => 'Option you choosed is not valid. Please refresh the page.');
             continue;
         }
         /* Mencari jawaban yang sudah pernah dijawab */
         $c = new Criteria();
         $c->add(AngketpmbSessionAnswerPeer::SESSION_ID, $angketSession->getId(), Criteria::EQUAL);
         $c->add(AngketpmbSessionAnswerPeer::SOAL_ID, $soal->getId(), Criteria::EQUAL);
         $angketAnswer = AngketpmbSessionAnswerPeer::doSelectOne($c);
         if ($angketAnswer) {
             /* Update */
             if ($angketAnswer->getOptionId() != $option->getId()) {
                 try {
                     $angketAnswer->setOptionId($option->getId());
                     $angketAnswer->save();
                 } catch (Exception $e) {
                     $connection->rollback();
                     $this->jsonwrapper->print_json(array('success' => 0, 'code' => 'processError', 'message' => 'Sorry, there is an error while saving your answer. Please trye again later', 'hideMessage' => 'Error while update applicant answer Id = ' . $angketAnswer->getId()));
                     die;
                 }
             }
         } else {
             /* Insert */
             try {
                 $angketAnswer = new AngketpmbSessionAnswer();
                 $angketAnswer->setSessionId($angketSession->getId());
                 $angketAnswer->setSoalId($soal->getId());
                 $angketAnswer->setOptionId($option->getId());
                 $angketAnswer->save();
             } catch (Exception $e) {
                 $connection->rollback();
                 $this->jsonwrapper->print_json(array('success' => 0, 'code' => 'processError', 'message' => 'Sorry, there is an error while saving your answer. Please trye again later', 'hideMessage' => 'Error while insert applicant answer SoalId = ' . $soal->getId()));
                 die;
             }
         }
     }
     if (count($errorAnswer) > 0) {
         $connection->rollback();
         $this->jsonwrapper->print_json(array('success' => 0, 'code' => 'someQuestionError', 'data' => $errorAnswer));
         die;
     }
     $connection->commit();
     $this->jsonwrapper->print_json(array('success' => 1, 'message' => 'Berhasil'));
     die;
 }