_askedQuestions; } public function setAskedQuestions(array $questions) { $this->_askedQuestions = $questions; } public function getRandomQuestion() { $askedIds = array(); foreach($this->_askedQuestions as $a) $askedIds[] = $a['id']; $q = Doctrine_Query::create() ->from('Question') ->orderBy('RANDOM()'); if(count($askedIds) > 0) $q->whereNotIn('id', $askedIds); return $q->fetchOne(); } public function getAnswers($question, $choice) { $answers = Doctrine_Query::create() ->from('QuestionAnswer') ->where('question_id = ? AND choice = ?', array($question['id'], $choice)) ->execute(); return $answers; } public function getBestAnswer() { if(count($this->_askedQuestions) === 0) throw new App_Questioner_Exception('No asked questions set, cant provide best answer'); $iterations = array(); foreach($this->_askedQuestions as $q) { $q = Doctrine_Query::create() ->select('answer_id') ->from('QuestionAnswer') ->where('question_id = ? AND choice = ?', array($q['id'], $q['choice'])) ->groupBy('answer_id') ->orderBy('COUNT(answer_id)'); $prevIterAnswers = array(); if(count($iterations) > 0) $prevIterAnswers = $iterations[ count($iterations) - 1 ]; if(count($prevIterAnswers) > 0) { $ids = array(); foreach($prevIterAnswers as $a) $ids[] = $a['answer_id']; $q->whereIn('answer_id', $ids); } $iterations[] = $q->execute(); } $iterations = array_reverse($iterations); foreach($iterations as $i) { if(count($i) > 0) return Doctrine::getTable('Answer')->find($i[0]['answer_id']); } return null; } }