Questtype ?Textinput?: add support for field sizes (Issue #252) and general improvements
This commit is contained in:
parent
6cb12a0b7a
commit
b7a7299b45
4 changed files with 88 additions and 58 deletions
|
|
@ -34,15 +34,15 @@
|
|||
*/
|
||||
public function saveAnswersOfCharacter($seminary, $questgroup, $quest, $character, $answers)
|
||||
{
|
||||
// Get regexs
|
||||
$regexs = $this->Textinput->getTextinputRegexs($quest['id']);
|
||||
// Get fields
|
||||
$fields = $this->Textinput->getTextinputFields($quest['id']);
|
||||
|
||||
// Save answers
|
||||
foreach($regexs as &$regex)
|
||||
foreach($fields as &$field)
|
||||
{
|
||||
$pos = intval($regex['number']) - 1;
|
||||
$pos = intval($field['number']) - 1;
|
||||
$answer = (array_key_exists($pos, $answers)) ? $answers[$pos] : '';
|
||||
$this->Textinput->setCharacterSubmission($regex['id'], $character['id'], $answer);
|
||||
$this->Textinput->setCharacterSubmission($field['id'], $character['id'], $answer);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -73,19 +73,20 @@
|
|||
public function matchAnswersOfCharacter($seminary, $questgroup, $quest, $character, $answers)
|
||||
{
|
||||
// Get right answers
|
||||
$regexs = $this->Textinput->getTextinputRegexs($quest['id']);
|
||||
$fields = $this->Textinput->getTextinputFields($quest['id']);
|
||||
|
||||
// Match regexs with user answers
|
||||
$allSolved = true;
|
||||
foreach($regexs as $i => &$regex)
|
||||
foreach($fields as &$field)
|
||||
{
|
||||
if(!array_key_exists($i, $answers))
|
||||
$pos = intval($field['number']) - 1;
|
||||
|
||||
if(!array_key_exists($pos, $answers))
|
||||
{
|
||||
$allSolved = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if(!$this->isMatching($regex['regex'], $answers[$i]))
|
||||
if(!$this->isMatching($field['regex'], $answers[$pos]))
|
||||
{
|
||||
$allSolved = false;
|
||||
break;
|
||||
|
|
@ -115,26 +116,23 @@
|
|||
// Get Task
|
||||
$task = $this->Textinput->getTextinputQuest($quest['id']);
|
||||
|
||||
// Process text
|
||||
$textParts = preg_split('/(\$\$)/', ' '.$task['text'].' ', -1, PREG_SPLIT_NO_EMPTY);
|
||||
// Get fields
|
||||
$fields = $this->Textinput->getTextinputFields($quest['id']);
|
||||
|
||||
// Has Character already solved Quest?
|
||||
$solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']);
|
||||
|
||||
// Get Character answers
|
||||
$regexs = null;
|
||||
if(!$solved || $this->request->getGetParam('show-answer') == 'true' || $this->request->getGetParam('status') == 'solved')
|
||||
{
|
||||
$regexs = $this->Textinput->getTextinputRegexs($quest['id']);
|
||||
foreach($regexs as &$regex) {
|
||||
$regex['answer'] = $this->Textinput->getCharacterSubmission($regex['id'], $character['id']);
|
||||
if(!$solved || $this->request->getGetParam('show-answer') == 'true' || $this->request->getGetParam('status') == 'solved') {
|
||||
foreach($fields as &$field) {
|
||||
$field['answer'] = $this->Textinput->getCharacterSubmission($field['id'], $character['id']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Pass data to view
|
||||
$this->set('texts', $textParts);
|
||||
$this->set('regexs', $regexs);
|
||||
$this->set('task', $task);
|
||||
$this->set('fields', $fields);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -154,19 +152,22 @@
|
|||
$task = $this->Textinput->getTextinputQuest($quest['id']);
|
||||
|
||||
// Process text
|
||||
$textParts = preg_split('/(\$\$)/', $task['text'], -1, PREG_SPLIT_NO_EMPTY);
|
||||
$textParts = preg_split('/(\$\$)/', ' '.$task['text'].' ');
|
||||
|
||||
// Get fields
|
||||
$fields = $this->Textinput->getTextinputFields($quest['id']);
|
||||
|
||||
// Get Character answers
|
||||
$regexs = $this->Textinput->getTextinputRegexs($quest['id']);
|
||||
foreach($regexs as &$regex) {
|
||||
$regex['answer'] = $this->Textinput->getCharacterSubmission($regex['id'], $character['id']);
|
||||
$regex['right'] = $this->isMatching($regex['regex'], $regex['answer']);
|
||||
foreach($fields as &$field)
|
||||
{
|
||||
$field['answer'] = $this->Textinput->getCharacterSubmission($field['id'], $character['id']);
|
||||
$field['right'] = $this->isMatching($field['regex'], $field['answer']);
|
||||
}
|
||||
|
||||
|
||||
// Pass data to view
|
||||
$this->set('texts', $textParts);
|
||||
$this->set('regexs', $regexs);
|
||||
$this->set('task', $task);
|
||||
$this->set('fields', $fields);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -48,18 +48,19 @@
|
|||
|
||||
|
||||
/**
|
||||
* Get regular expressions for a textinput-text.
|
||||
* Get fields for a textinput-text.
|
||||
*
|
||||
* @param int $questId ID of Quest
|
||||
* @return array Regexs
|
||||
* @return array Fields
|
||||
*/
|
||||
public function getTextinputRegexs($questId)
|
||||
public function getTextinputFields($questId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, number, regex '.
|
||||
'FROM questtypes_textinput_regexs '.
|
||||
'WHERE questtypes_textinput_quest_id = ? '.
|
||||
'ORDER BY number ASC',
|
||||
'SELECT fields.id, fields.number, fields.regex, fieldsizes.id AS fieldsize_id, fieldsizes.size '.
|
||||
'FROM questtypes_textinput_fields AS fields '.
|
||||
'LEFT JOIN questtypes_textinput_fieldsizes AS fieldsizes ON fieldsizes.id = fields.questtypes_textinput_fieldsize_id '.
|
||||
'WHERE fields.questtypes_textinput_quest_id = ? '.
|
||||
'ORDER BY fields.number ASC',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
|
|
@ -69,40 +70,40 @@
|
|||
/**
|
||||
* Save Character’s submitted answer for one textinput field.
|
||||
*
|
||||
* @param int $regexId ID of regex
|
||||
* @param int $fieldId ID of field
|
||||
* @param int $characterId ID of Character
|
||||
* @param string $answer Submitted answer for this field
|
||||
*/
|
||||
public function setCharacterSubmission($regexId, $characterId, $answer)
|
||||
public function setCharacterSubmission($fieldId, $characterId, $answer)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO questtypes_textinput_regexs_characters '.
|
||||
'(questtypes_textinput_regex_id, character_id, value) '.
|
||||
'INSERT INTO questtypes_textinput_fields_characters '.
|
||||
'(questtypes_textinput_field_id, character_id, value) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?) '.
|
||||
'ON DUPLICATE KEY UPDATE '.
|
||||
'value = ?',
|
||||
'iiss',
|
||||
$regexId, $characterId, $answer, $answer
|
||||
$fieldId, $characterId, $answer, $answer
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get answer of one regex input field submitted by Character.
|
||||
* Get answer of one input field submitted by Character.
|
||||
*
|
||||
* @param int $regexId ID of regex
|
||||
* @param int $fieldId ID of field
|
||||
* @param int $characterId ID of Character
|
||||
* @return string Submitted answer for this field or empty string
|
||||
*/
|
||||
public function getCharacterSubmission($regexId, $characterId)
|
||||
public function getCharacterSubmission($fieldId, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT value '.
|
||||
'FROM questtypes_textinput_regexs_characters '.
|
||||
'WHERE questtypes_textinput_regex_id = ? AND character_id = ? ',
|
||||
'FROM questtypes_textinput_fields_characters '.
|
||||
'WHERE questtypes_textinput_field_id = ? AND character_id = ? ',
|
||||
'ii',
|
||||
$regexId, $characterId
|
||||
$fieldId, $characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0]['value'];
|
||||
|
|
|
|||
|
|
@ -1,11 +1,25 @@
|
|||
<form method="post" class="textinput">
|
||||
<p>
|
||||
<?php foreach($texts as $i => &$text) : ?>
|
||||
<?php if($i > 0) : ?>
|
||||
<input type="text" name="answers[<?=$i-1?>]" value="<?=$regexs[$i-1]['answer']?>" />
|
||||
<?php endif ?>
|
||||
<?=$t->t($text)?>
|
||||
<?php endforeach ?>
|
||||
<?php
|
||||
$text = $t->t($task['text']);
|
||||
|
||||
// Insert input fields
|
||||
foreach(array_reverse($fields) as &$field)
|
||||
{
|
||||
// Get positions
|
||||
$posStart = mb_strrpos($text, '[textinput]', 0, 'UTF-8');
|
||||
$posEnd = $posStart + mb_strlen('[textinput]', 'UTF-8');
|
||||
|
||||
// Create input field
|
||||
$class = ($field['size'] != 'default') ? $field['size'] : null;
|
||||
$value = (array_key_exists('answer', $field)) ? $field['answer'] : null;
|
||||
$inputField = '<input type="text" name="answers[]" value="'.$value.'" class="'.$class.'" />';
|
||||
|
||||
// Insert input field
|
||||
$text = mb_substr($text, 0, $posStart, 'UTF-8') . $inputField . mb_substr($text, $posEnd, mb_strlen($text)-$posEnd, 'UTF-8');
|
||||
}
|
||||
?>
|
||||
<?=$text?>
|
||||
</p>
|
||||
<input type="submit" name="submit" value="<?=_('solve')?>" />
|
||||
</form>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,21 @@
|
|||
<?php foreach($texts as $i => &$text) : ?>
|
||||
<?php if($i > 0) : ?>
|
||||
<span style="background-color:grey"><?=$regexs[$i-1]['answer']?></span>
|
||||
<?php if($regexs[$i-1]['right']) : ?>✓<?php else: ?>✕<?php endif ?>
|
||||
<?php endif ?>
|
||||
<?=$t->t($text)?>
|
||||
<?php endforeach ?>
|
||||
<p>
|
||||
<?php
|
||||
$text = $t->t($task['text']);
|
||||
|
||||
// Insert Character answers
|
||||
foreach(array_reverse($fields) as &$field)
|
||||
{
|
||||
// Get positions
|
||||
$posStart = mb_strrpos($text, '[textinput]', 0, 'UTF-8');
|
||||
$posEnd = $posStart + mb_strlen('[textinput]', 'UTF-8');
|
||||
|
||||
// Create field for Character answer
|
||||
$answerField = '<span style="background-color:grey">'.$field['answer'].'</span>';
|
||||
$answerField .= ($field['right']) ? '✓' : '✕';
|
||||
|
||||
// Insert input field
|
||||
$text = mb_substr($text, 0, $posStart, 'UTF-8') . $answerField . mb_substr($text, $posEnd, mb_strlen($text)-$posEnd, 'UTF-8');
|
||||
}
|
||||
?>
|
||||
<?=$text?>
|
||||
</p>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue