/** * @test */ public function setQuestionsFromArrayWorks() { $data = array(array('text' => 'Question text 1', 'hint' => 'Question hint 1', 'possibleAnswers' => array(array('text' => 'Answer 1', 'correctAnswer' => true), array('text' => 'Answer 2', 'correctAnswer' => false))), array('text' => 'Question text 2', 'hint' => 'Question hint 2', 'possibleAnswers' => array(array('text' => 'Answer 1', 'correctAnswer' => false), array('text' => 'Answer 2', 'correctAnswer' => true))), array('text' => 'Question text 3', 'hint' => 'Question hint 3', 'possibleAnswers' => array(array('text' => 'Answer 1', 'correctAnswer' => false), array('text' => 'Answer 2', 'correctAnswer' => true)))); $exercise = new MultipleChoiceSameAnswerExercise(); $exercise->setQuestionsFromArray($data); $this->assertEquals(3, $exercise->getMaxScore()); }
/** * @param \_OurBrand_\Quiz\Domain\Model\Exercises\MultipleChoiceSameAnswerExercise $exercise */ public function updateAction($exercise) { $possibleAnswers = $correctAnswers = $toSetQuestions = array(); if ($this->request->hasArgument('possibleAnswers')) { $possibleAnswers = $this->request->getArgument('possibleAnswers'); } if ($this->request->hasArgument('questions')) { $newQuestions = $this->request->getArgument('questions'); if (!is_array($newQuestions)) { $newQuestions = array($newQuestions); } if ($this->request->hasArgument('correctAnswer')) { $correctAnswers = $this->request->getArgument('correctAnswer'); } foreach ($newQuestions as $i => $newQuestionText) { $question = array('text' => $newQuestionText, 'hint' => ''); foreach ($possibleAnswers as $k => $possibleAnswerText) { $possibleAnswer = array('text' => $possibleAnswerText, 'correctAnswer' => 0); if (isset($correctAnswers[$i]) && $correctAnswers[$i] == $k) { $possibleAnswer['correctAnswer'] = 1; } $question['possibleAnswers'][] = $possibleAnswer; } $toSetQuestions[] = $question; } $exercise->setQuestionsFromArray($toSetQuestions); } // The possible answers are not saved individually, but only with questions. // So if no questions have been saved, we would like to // save the possible answers anyways. $identifier = $this->persistenceManager->getIdentifierByObject($exercise); $this->loginSession->setData('possibleAnswers-' . $identifier, $possibleAnswers); if ($this->request->hasArgument('quiz')) { $this->updateQuizBeforeUpdatingExercise($exercise->getQuiz(), $this->request->getArgument('quiz')); } if (!$this->request->hasArgument('json')) { $this->forward('update', 'exercise', null, array('exercise' => $exercise)); } $this->forward('updateSilent', 'exercise', null, array('exercise' => $exercise, 'json' => $this->request->getArgument('json'))); }