save answers for Questtype ?Textinput?
This commit is contained in:
parent
d22618d4b9
commit
b94af71a2b
2 changed files with 13 additions and 1 deletions
|
|
@ -31,10 +31,17 @@
|
||||||
*/
|
*/
|
||||||
public function index($questId)
|
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
|
// 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 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
|
||||||
|
|
@ -42,12 +49,16 @@
|
||||||
for($i=0; $i<count($regexs); $i++)
|
for($i=0; $i<count($regexs); $i++)
|
||||||
{
|
{
|
||||||
$answer = $this->request->getPostParam('answer-'.strval($i+1));
|
$answer = $this->request->getPostParam('answer-'.strval($i+1));
|
||||||
|
$answers[] = $answer;
|
||||||
$score = preg_match($regexs[$i]['regex'], $answer);
|
$score = preg_match($regexs[$i]['regex'], $answer);
|
||||||
if($score === 0 || $score === false) {
|
if($score === 0 || $score === false) {
|
||||||
$allSolved = false;
|
$allSolved = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Store answers in session
|
||||||
|
$_SESSION['answers'][$questId] = $answers;
|
||||||
|
|
||||||
// Set status
|
// Set status
|
||||||
if($allSolved) {
|
if($allSolved) {
|
||||||
$this->setQuestSolved();
|
$this->setQuestSolved();
|
||||||
|
|
@ -67,6 +78,7 @@
|
||||||
|
|
||||||
// Pass data to view
|
// Pass data to view
|
||||||
$this->set('text', $textParts);
|
$this->set('text', $textParts);
|
||||||
|
$this->set('answers', $answers);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<?php for($i=0; $i<count($text); $i++) : ?>
|
<?php for($i=0; $i<count($text); $i++) : ?>
|
||||||
<?php if($i > 0) : ?>
|
<?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 ?>
|
<?php endif ?>
|
||||||
<?=\hhu\z\Utils::t($text[$i])?>
|
<?=\hhu\z\Utils::t($text[$i])?>
|
||||||
<?php endfor ?>
|
<?php endfor ?>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue