correct for-loops and answers-array for Questtype ?Textinput?
This commit is contained in:
parent
b94af71a2b
commit
1f41ed6be8
3 changed files with 23 additions and 15 deletions
|
|
@ -40,25 +40,33 @@
|
||||||
// Check for submission
|
// Check for submission
|
||||||
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('submit')))
|
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
|
// Get right answers
|
||||||
$answers = array();
|
|
||||||
$regexs = $this->Textinput->getTextinputRegexs($questId);
|
$regexs = $this->Textinput->getTextinputRegexs($questId);
|
||||||
|
|
||||||
// Match regexs with user answers
|
// Match regexs with user answers
|
||||||
$allSolved = true;
|
$allSolved = true;
|
||||||
for($i=0; $i<count($regexs); $i++)
|
foreach($regexs as $i => &$regex)
|
||||||
{
|
{
|
||||||
$answer = $this->request->getPostParam('answer-'.strval($i+1));
|
if(!array_key_exists($i, $answers))
|
||||||
$answers[] = $answer;
|
{
|
||||||
$score = preg_match($regexs[$i]['regex'], $answer);
|
|
||||||
if($score === 0 || $score === false) {
|
|
||||||
$allSolved = false;
|
$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
|
// Set status
|
||||||
if($allSolved) {
|
if($allSolved) {
|
||||||
$this->setQuestSolved();
|
$this->setQuestSolved();
|
||||||
|
|
@ -77,7 +85,7 @@
|
||||||
|
|
||||||
|
|
||||||
// Pass data to view
|
// Pass data to view
|
||||||
$this->set('text', $textParts);
|
$this->set('texts', $textParts);
|
||||||
$this->set('answers', $answers);
|
$this->set('answers', $answers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<?php for($i=0; $i<count($text); $i++) : ?>
|
<?php foreach($texts as $i => &$text) : ?>
|
||||||
<?php if($i > 0) : ?>
|
<?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 ?>
|
<?php endif ?>
|
||||||
<?=\hhu\z\Utils::t($text[$i])?>
|
<?=\hhu\z\Utils::t($text)?>
|
||||||
<?php endfor ?>
|
<?php endforeach ?>
|
||||||
|
|
||||||
<br /><br />
|
<br /><br />
|
||||||
<input type="submit" name="submit" value="<?=_('solve')?>" />
|
<input type="submit" name="submit" value="<?=_('solve')?>" />
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,6 @@
|
||||||
<section>
|
<section>
|
||||||
<h1><?=_('Task')?></h1>
|
<h1><?=_('Task')?></h1>
|
||||||
<p><?=\hhu\z\Utils::t($quest['task'])?></p>
|
<p><?=\hhu\z\Utils::t($quest['task'])?></p>
|
||||||
<p><?=$task?></p>
|
<?=$task?>
|
||||||
</section>
|
</section>
|
||||||
<?php endif ?>
|
<?php endif ?>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue