Questtype ?textinput?: store number of correct answers between redirects (fixes #90)

This commit is contained in:
oliver 2015-04-20 21:14:20 +02:00
parent 989a0019c3
commit f5a8e51cbd
4 changed files with 35 additions and 18 deletions

View file

@ -2,7 +2,7 @@ msgid ""
msgstr ""
"Project-Id-Version: The Legend of Z\n"
"POT-Creation-Date: 2015-04-19 14:15+0100\n"
"PO-Revision-Date: 2015-04-19 14:16+0100\n"
"PO-Revision-Date: 2015-04-20 21:06+0100\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: de_DE\n"
@ -342,7 +342,7 @@ msgstr "Felder"
#: questtypes/textinput/html/quest.tpl:13
#, php-format
msgid "You filled %d of %d fields correctly"
msgstr "Du hast %d von %d Feldern korrekt ausgefüllt"
msgstr "Du hast %d von %d Felder korrekt ausgefüllt"
#: views/ajax/quests/index.tpl:9 views/html/quests/create.tpl:79
#: views/html/quests/edit.tpl:82 views/html/quests/index.tpl:27

View file

@ -24,7 +24,15 @@
*
* @var array
*/
public $components = array('validation');
public $components = array('validation', 'questtypedata');
/**
* Count of correct answers
*
* @var int
*/
const KEY_QUESTTYPEDATA_N_CORRECT_ANSWRES = 'nCorrectAnswers';
private $nCorrectAnswers = null;
@ -81,22 +89,31 @@
{
// Get right answers
$fields = $this->Textinput->getTextinputFields($quest['id']);
// Match regexs with user answers
$nCorrectAnswers = 0;
foreach($fields as &$field)
{
$pos = intval($field['number']) - 1;
if(!array_key_exists($pos, $answers)) {
return false;
continue;
}
if(!$this->isMatching($field['regex'], $answers[$pos])) {
return false;
continue;
}
$nCorrectAnswers++;
}
// Save count of correct answers
$this->Questtypedata->set(
$quest['id'],
self::KEY_QUESTTYPEDATA_N_CORRECT_ANSWRES,
$nCorrectAnswers
);
// All answers right
return true;
return ($nCorrectAnswers == count($fields));
}
@ -124,26 +141,26 @@
$solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']);
// Get Character answers
$correctAnswersCount = null;
if(!$solved || $this->request->getGetParam('show-answer') == 'true' || $this->request->getGetParam('status') == 'solved')
{
$correctAnswersCount = 0;
foreach($fields as &$field)
{
foreach($fields as &$field) {
$field['answer'] = $this->Textinput->getCharacterSubmission($field['id'], $character['id']);
$correctAnswersCount += $this->isMatching($field['regex'], $field['answer']);
}
}
// Show count
$showcount = (count($this->request->getGetParams()) > 1);
// Get count of last correct answers
$nCorrectAnswers = $this->Questtypedata->get($quest['id'], self::KEY_QUESTTYPEDATA_N_CORRECT_ANSWRES);
$this->Questtypedata->set(
$quest['id'],
self::KEY_QUESTTYPEDATA_N_CORRECT_ANSWRES,
null
);
// Pass data to view
$this->set('task', $task);
$this->set('fields', $fields);
$this->set('count', $correctAnswersCount);
$this->set('showcount', $showcount);
$this->set('nCorrectAnswers', $nCorrectAnswers);
}

View file

@ -9,8 +9,8 @@
<?php endforeach ?>
<?=$t->t(mb_substr($task['text'], $posStart, mb_strlen($task['text'], 'UTF-8')-$posStart, 'UTF-8'))?>
</p>
<?php if($showcount) : ?>
<p><?=sprintf(_('You filled %d of %d fields correctly'), $count, count($fields))?>.</p>
<?php if(!is_null($nCorrectAnswers)) : ?>
<p><?=sprintf(_('You filled %d of %d fields correctly'), $nCorrectAnswers, count($fields))?>.</p>
<?php endif ?>
<input type="submit" name="submit" value="<?=_('solve')?>" />
</form>