save answers for Questtype ?Textinput?

This commit is contained in:
coderkun 2014-03-11 00:49:26 +01:00
commit ad1da59748
2 changed files with 13 additions and 1 deletions

View file

@ -31,10 +31,17 @@
*/
public function index($questId)
{
// Answers
if(!array_key_exists('answers', $_SESSION)) {
$_SESSION['answers'] = array();
}
$answers = array_key_exists($questId, $_SESSION['answers']) ? $_SESSION['answers'][$questId] : array();
// Check for submission
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('submit')))
{
// Get right answers
$answers = array();
$regexs = $this->Textinput->getTextinputRegexs($questId);
// Match regexs with user answers
@ -42,12 +49,16 @@
for($i=0; $i<count($regexs); $i++)
{
$answer = $this->request->getPostParam('answer-'.strval($i+1));
$answers[] = $answer;
$score = preg_match($regexs[$i]['regex'], $answer);
if($score === 0 || $score === false) {
$allSolved = false;
}
}
// Store answers in session
$_SESSION['answers'][$questId] = $answers;
// Set status
if($allSolved) {
$this->setQuestSolved();
@ -67,6 +78,7 @@
// Pass data to view
$this->set('text', $textParts);
$this->set('answers', $answers);
}
}

View file

@ -1,7 +1,7 @@
<form method="post">
<?php for($i=0; $i<count($text); $i++) : ?>
<?php if($i > 0) : ?>
<input type="text" name="answer-<?=$i?>" value="" />
<input type="text" name="answer-<?=$i?>" value="<?=(count($answers) > $i-1) ? $answers[$i-1] : '' ?>" />
<?php endif ?>
<?=\hhu\z\Utils::t($text[$i])?>
<?php endfor ?>