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']);
// 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;
}
if($list['questtypes_choiceinput_choice_id'] != $answers[$i]) {
if($list['questtypes_choiceinput_choice_id'] != $answers[$pos]) {
return false;
}
}
// All answer right
// All answers right
return true;
}
@ -109,9 +110,6 @@
// Get Task
$task = $this->Choiceinput->getChoiceinputQuest($quest['id']);
// Process text
$textParts = preg_split('/(\$\$)/', ' '.$task['text'].' ');
// Get lists
$choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']);
foreach($choiceLists as &$list) {
@ -119,8 +117,7 @@
}
// 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) {
$list['answer'] = $this->Choiceinput->getCharacterSubmission($list['id'], $character['id']);
}
@ -128,7 +125,7 @@
// Pass data to view
$this->set('texts', $textParts);
$this->set('task', $task);
$this->set('choiceLists', $choiceLists);
}
@ -148,9 +145,6 @@
// Get Task
$task = $this->Choiceinput->getChoiceinputQuest($quest['id']);
// Process text
$textParts = preg_split('/(\$\$)/', ' '.$task['text'].' ');
// Get lists
$choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']);
foreach($choiceLists as &$list)
@ -162,7 +156,7 @@
// Pass data to view
$this->set('texts', $textParts);
$this->set('task', $task);
$this->set('choiceLists', $choiceLists);
}

View file

@ -1,15 +1,32 @@
<form method="post">
<?php foreach($texts as $i => &$text) : ?>
<?php if($i > 0) : ?>
<select name="answers[<?=$i-1?>]">
<?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>
<?php endforeach ?>
</select>
<?php endif ?>
<?=$t->t($text)?>
<?php endforeach ?>
<br /><br />
<p>
<?php
$text = $t->t($task['text']);
// Insert comboboxes
foreach(array_reverse($choiceLists) as &$list)
{
// Get positions
$posStart = mb_strrpos($text, '[choiceinput]', 0, 'UTF-8');
$posEnd = $posStart + mb_strlen('[choiceinput]', 'UTF-8');
// 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')?>" />
</form>

View file

@ -1,12 +1,29 @@
<form method="post">
<?php foreach($texts as $i => &$text) : ?>
<?php if($i > 0) : ?>
<select name="answers[<?=$i-1?>]" disabled="disabled">
<?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>
<?php endforeach ?>
</select>
<?php endif ?>
<?=$t->t($text)?>
<?php endforeach ?>
</form>
<p>
<?php
$text = $t->t($task['text']);
// Insert comboboxes
foreach(array_reverse($choiceLists) as &$list)
{
// Get positions
$posStart = mb_strrpos($text, '[choiceinput]', 0, 'UTF-8');
$posEnd = $posStart + mb_strlen('[choiceinput]', 'UTF-8');
// 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>