correct for-loops and answers-array for Questtype ?Textinput?

This commit is contained in:
coderkun 2014-03-12 01:28:26 +01:00
commit 6f91287afc
3 changed files with 23 additions and 15 deletions

View file

@ -40,25 +40,33 @@
// Check for submission
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('submit')))
{
// Get answers
$answers = $this->request->getPostParam('answers');
// Store answers in session
$_SESSION['answers'][$questId] = $answers;
// Get right answers
$answers = array();
$regexs = $this->Textinput->getTextinputRegexs($questId);
// Match regexs with user answers
$allSolved = true;
for($i=0; $i<count($regexs); $i++)
foreach($regexs as $i => &$regex)
{
$answer = $this->request->getPostParam('answer-'.strval($i+1));
$answers[] = $answer;
$score = preg_match($regexs[$i]['regex'], $answer);
if($score === 0 || $score === false) {
if(!array_key_exists($i, $answers))
{
$allSolved = false;
break;
}
$score = preg_match($regex['regex'], $answers[$i]);
if($score === 0 || $score === false)
{
$allSolved = false;
break;
}
}
// Store answers in session
$_SESSION['answers'][$questId] = $answers;
// Set status
if($allSolved) {
$this->setQuestSolved();
@ -77,7 +85,7 @@
// Pass data to view
$this->set('text', $textParts);
$this->set('texts', $textParts);
$this->set('answers', $answers);
}

View file

@ -1,10 +1,10 @@
<form method="post">
<?php for($i=0; $i<count($text); $i++) : ?>
<?php foreach($texts as $i => &$text) : ?>
<?php if($i > 0) : ?>
<input type="text" name="answer-<?=$i?>" value="<?=(count($answers) > $i-1) ? $answers[$i-1] : '' ?>" />
<input type="text" name="answers[<?=$i-1?>]" value="<?=(array_key_exists($i-1, $answers)) ? $answers[$i-1] : '' ?>" />
<?php endif ?>
<?=\hhu\z\Utils::t($text[$i])?>
<?php endfor ?>
<?=\hhu\z\Utils::t($text)?>
<?php endforeach ?>
<br /><br />
<input type="submit" name="submit" value="<?=_('solve')?>" />

View file

@ -76,6 +76,6 @@
<section>
<h1><?=_('Task')?></h1>
<p><?=\hhu\z\Utils::t($quest['task'])?></p>
<p><?=$task?></p>
<?=$task?>
</section>
<?php endif ?>