_store = new Zend_Session_Namespace(__CLASS__); } public function startAction() { $this->_store->answers = array(); $this->_store->questions = array(); } public function winAction() { } public function endAction() { if($this->_request->isPost()) { $yes = $this->_request->getPost('yes', false); if(!$yes) { $this->_helper->redirector->gotoRoute(array(), 'new-answer'); return; } $this->_saveAnswers($this->_store->guess); $this->_forward('win'); return; } $totalAnswers = count($this->_store->answers); if($totalAnswers == 0) { $this->_helper->redirector->gotoRoute(array(), 'new-answer'); return; } $id = $this->_store->answers[ rand(0, $totalAnswers - 1) ]; $answer = Doctrine_Query::create() ->select('id, answer') ->from('Answer') ->where('id = ?', $id) ->fetchOne(); $this->view->answer = $answer->answer; $this->_store->guess = $answer->id; } protected function _saveAnswers($answerId) { foreach($this->_store->questions as $q) { $qa = new QuestionAnswer(); $qa->question_id = $q['id']; $qa->choice = $q['choice']; $qa->answer_id = $answerId; $qa->save(); } } public function newAnswerAction() { if($this->_request->isPost()) { try { $answer = new Answer(); $answer->answer = $this->_request->getPost('answer'); $answer->save(); } catch(Doctrine_Connection_Exception $e) { $answer = Doctrine::getTable('Answer')->findOneByAnswer($this->_request->getPost('answer')); } try { $question = new Question(); $question->question = $this->_request->getPost('question'); $question->save(); } catch(Doctrine_Connection_Exception $e) { $question = Doctrine::getTable('Question')->findOneByQuestion($this->_request->getPost('question')); } $qa = new QuestionAnswer(); $qa->question_id = $question->id; $qa->answer_id = $answer->id; $qa->choice = (string)$this->_request->getPost('choice'); $qa->save(); $this->_saveAnswers($answer->id); $this->_helper->redirector->gotoRoute(array(), 'start'); return; } } public function questionAction() { if($this->_request->isPost()) { $yes = $this->_request->getPost('yes', false); if($yes !== false) $this->_filterAnswers(QuestionAnswer::CHOICE_YES); else $this->_filterAnswers(QuestionAnswer::CHOICE_NO); } $questionIds = array(); foreach($this->_store->questions as $q) $questionIds[] = $q['id']; $question = Doctrine_Query::create() ->from('Question') ->whereNotIn('id', $questionIds) ->orderBy('RANDOM()') ->fetchOne(); //We probably ran out of questions... $questionsAsked = count($this->_store->questions); $totalAnswers = count($this->_store->answers); if(!$question || $questionsAsked == 10) { $this->_helper->redirector->gotoRoute(array(), 'end'); return; } $this->_store->questions[] = array( 'id' => $question['id'], 'choice' => null ); $this->view->question = $question; } /** * Filter the answers * @param bool $yes */ protected function _filterAnswers($yes) { $question = $this->_store->questions[ count($this->_store->questions) - 1]; $question['choice'] = $yes; $q = Doctrine_Query::create() ->select('answer_id') ->distinct() ->from('QuestionAnswer') ->where('question_id = ?', $question['id']) ->addWhere('choice = ?', (int)$yes); if(count($this->_store->answers) == 0) { $possibleAnswers = $q->execute(); foreach($possibleAnswers as $a) $this->_store->answers[] = $a['answer_id']; return; } $q->whereIn('answer_id', $this->_store->answers); $possibleAnswers = $q->execute(); if(count($possibleAnswers) == 0) return; $this->_store->answers = array(); foreach($possibleAnswers as $a) $this->_store->answers[] = $a['answer_id']; } }