correct for-loops and answers-array for Questtype ?Textinput?
This commit is contained in:
parent
ad1da59748
commit
6f91287afc
3 changed files with 23 additions and 15 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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')?>" />
|
||||
|
|
|
|||
|
|
@ -76,6 +76,6 @@
|
|||
<section>
|
||||
<h1><?=_('Task')?></h1>
|
||||
<p><?=\hhu\z\Utils::t($quest['task'])?></p>
|
||||
<p><?=$task?></p>
|
||||
<?=$task?>
|
||||
</section>
|
||||
<?php endif ?>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue