public function execute($request)
 {
     $this->questions = Doctrine_Query::create()->select('q.*, qa.*')->from('Questions q')->leftJoin('q.QuestionsAnswers qa')->where('q.poll_id =? ', $this->poll['id'])->execute();
     $optionQuizzCollection = array('questions' => $this->questions, 'multi' => $this->poll['type'], 'points' => $this->poll['type']);
     if ($this->poll['self_answers']) {
         for ($i = 0; $i < count($this->questions); $i++) {
             $optionQuizzCollection['self' . $i] = "self";
         }
     }
     if ($this->poll['demographic']) {
         $this->demographicForm = new demogrphicForm();
         if ($request->hasParameter('demographic')) {
             $this->demographicForm->bind($request->getParameter('demographic'));
             if ($this->demographicForm->isValid()) {
                 $demographic = new DemographicStat();
                 $demographic->fromArray(array('poll_id' => $this->poll->id, 'user_id' => $this->user->id, 'city' => $this->demographicForm->getValue('city'), 'age' => $this->demographicForm->getValue('age'), 'sex' => $this->demographicForm->getValue('sex')));
                 $demographic->save();
             }
         }
     }
     $this->quizzCollectionForm = new quizzCollectionForm(null, $optionQuizzCollection);
     if ($request->hasParameter('quizzCollection')) {
         $this->quizzCollectionForm->bind($request->getParameter('quizzCollection'));
         if ($this->quizzCollectionForm->isValid()) {
             if ($this->user->UsersAnswers->count() == 0) {
                 $requestQuizzCollection = $request->getParameter('quizzCollection');
                 for ($i = 0; $i < count($requestQuizzCollection) - 1; $i++) {
                     foreach ($requestQuizzCollection[$i]['answers'] as $answers) {
                         $userAnswer = explode('___', $answers);
                         $last = count($userAnswer) - 1;
                         $answer = new UsersAnswers();
                         $answer->fromArray(array('user_id' => $this->user['id'], 'question_id' => $userAnswer[0], 'points' => $userAnswer[$last]));
                         if ($userAnswer[1] != 'null') {
                             $answer['answer_id'] = $userAnswer[1];
                         } else {
                             $answer['answer_text'] = $request->getParameter('self' . $i);
                         }
                         $answer->save();
                     }
                 }
                 //					$this->getUser()->setFlash('answers', $answers);
                 //					$this->redirect(url_for2('default_index', array('module' => 'quizResult'), true));
             }
         }
     }
 }
Beispiel #2
0
    public static  function setUserQuestionsAnswers ($user_id,$answers_array) {

        $error = 0;

        foreach ($answers_array as $key=>$value ) {

        try{
            $answer = new UsersAnswers();
            $answer->Question_ID = $key;
            $answer->User_ID = $user_id;
            $answer->Hashed_Answer = sha1($value);
            $answer->Hint = UsersAnswers::makeHint($value);
            $answer->save();

        } catch (Exception $e) {
            $error = 1;
        }

        }
        return $error;
    }