Questtype ?Choiceinput?: general improvements learned from Questtype ?Textinput?

This commit is contained in:
coderkun 2014-05-19 12:05:25 +02:00
commit 930601586c
3 changed files with 66 additions and 38 deletions

View file

@ -77,18 +77,19 @@
$choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']); $choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']);
// Match lists with user answers // Match lists with user answers
foreach($choiceLists as $i => &$list) foreach($choiceLists as &$list)
{ {
if(!array_key_exists($i, $answers)) { $pos = intval($list['number']) - 1;
if(!array_key_exists($pos, $answers)) {
return false; return false;
} }
if($list['questtypes_choiceinput_choice_id'] != $answers[$i]) { if($list['questtypes_choiceinput_choice_id'] != $answers[$pos]) {
return false; return false;
} }
} }
// All answer right // All answers right
return true; return true;
} }
@ -109,9 +110,6 @@
// Get Task // Get Task
$task = $this->Choiceinput->getChoiceinputQuest($quest['id']); $task = $this->Choiceinput->getChoiceinputQuest($quest['id']);
// Process text
$textParts = preg_split('/(\$\$)/', ' '.$task['text'].' ');
// Get lists // Get lists
$choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']); $choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']);
foreach($choiceLists as &$list) { foreach($choiceLists as &$list) {
@ -119,8 +117,7 @@
} }
// Get Character answers // Get Character answers
if($this->request->getGetParam('show-answer') == 'true' || !$this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']) || $this->request->getGetParam('status') == 'solved') if($this->request->getGetParam('show-answer') == 'true' || !$this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']) || $this->request->getGetParam('status') == 'solved') {
{
foreach($choiceLists as &$list) { foreach($choiceLists as &$list) {
$list['answer'] = $this->Choiceinput->getCharacterSubmission($list['id'], $character['id']); $list['answer'] = $this->Choiceinput->getCharacterSubmission($list['id'], $character['id']);
} }
@ -128,7 +125,7 @@
// Pass data to view // Pass data to view
$this->set('texts', $textParts); $this->set('task', $task);
$this->set('choiceLists', $choiceLists); $this->set('choiceLists', $choiceLists);
} }
@ -148,9 +145,6 @@
// Get Task // Get Task
$task = $this->Choiceinput->getChoiceinputQuest($quest['id']); $task = $this->Choiceinput->getChoiceinputQuest($quest['id']);
// Process text
$textParts = preg_split('/(\$\$)/', ' '.$task['text'].' ');
// Get lists // Get lists
$choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']); $choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']);
foreach($choiceLists as &$list) foreach($choiceLists as &$list)
@ -162,7 +156,7 @@
// Pass data to view // Pass data to view
$this->set('texts', $textParts); $this->set('task', $task);
$this->set('choiceLists', $choiceLists); $this->set('choiceLists', $choiceLists);
} }

View file

@ -1,15 +1,32 @@
<form method="post"> <form method="post">
<?php foreach($texts as $i => &$text) : ?> <p>
<?php if($i > 0) : ?> <?php
<select name="answers[<?=$i-1?>]"> $text = $t->t($task['text']);
<?php foreach($choiceLists[$i-1]['values'] as &$choice) : ?>
<option value="<?=$choice['id']?>" <?php if(array_key_exists('answer', $choiceLists[$i-1]) && $choiceLists[$i-1]['answer'] == $choice['id']) : ?>selected="selected"<?php endif ?>><?=$choice['text']?></option> // Insert comboboxes
<?php endforeach ?> foreach(array_reverse($choiceLists) as &$list)
</select> {
<?php endif ?> // Get positions
<?=$t->t($text)?> $posStart = mb_strrpos($text, '[choiceinput]', 0, 'UTF-8');
<?php endforeach ?> $posEnd = $posStart + mb_strlen('[choiceinput]', 'UTF-8');
<br /><br /> // Create combobox
$combobox = '<select name="answers[]">';
foreach($list['values'] as &$choice) {
$combobox .= sprintf(
'<option value="%s" %s>%s</option>',
$choice['id'],
(array_key_exists('answer', $list) && $list['answer'] == $choice['id']) ? 'selected="selected"' : null,
$choice['text']
);
}
$combobox .= '</select>';
// Insert input field
$text = mb_substr($text, 0, $posStart, 'UTF-8') . $combobox . mb_substr($text, $posEnd, mb_strlen($text)-$posEnd, 'UTF-8');
}
?>
<?=$text?>
</p>
<input type="submit" name="submit" value="<?=_('solve')?>" /> <input type="submit" name="submit" value="<?=_('solve')?>" />
</form> </form>

View file

@ -1,12 +1,29 @@
<form method="post"> <p>
<?php foreach($texts as $i => &$text) : ?> <?php
<?php if($i > 0) : ?> $text = $t->t($task['text']);
<select name="answers[<?=$i-1?>]" disabled="disabled">
<?php foreach($choiceLists[$i-1]['values'] as &$choice) : ?> // Insert comboboxes
<option value="<?=$choice['id']?>" <?php if(array_key_exists('answer', $choiceLists[$i-1]) && $choiceLists[$i-1]['answer'] == $choice['id']) : ?>selected="selected"<?php endif ?>><?=$choice['text']?></option> foreach(array_reverse($choiceLists) as &$list)
<?php endforeach ?> {
</select> // Get positions
<?php endif ?> $posStart = mb_strrpos($text, '[choiceinput]', 0, 'UTF-8');
<?=$t->t($text)?> $posEnd = $posStart + mb_strlen('[choiceinput]', 'UTF-8');
<?php endforeach ?>
</form> // Create combobox
$combobox = '<select name="answers[]" disabled="disabled">';
foreach($list['values'] as &$choice) {
$combobox .= sprintf(
'<option value="%s" %s>%s</option>',
$choice['id'],
(array_key_exists('answer', $list) && $list['answer'] == $choice['id']) ? 'selected="selected"' : null,
$choice['text']
);
}
$combobox .= '</select>';
// Insert input field
$text = mb_substr($text, 0, $posStart, 'UTF-8') . $combobox . mb_substr($text, $posEnd, mb_strlen($text)-$posEnd, 'UTF-8');
}
?>
<?=$text?>
</p>