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)
|
public function saveAnswersOfCharacter($seminary, $questgroup, $quest, $character, $answers)
|
||||||
{
|
{
|
||||||
// Get regexs
|
// Get fields
|
||||||
$regexs = $this->Textinput->getTextinputRegexs($quest['id']);
|
$fields = $this->Textinput->getTextinputFields($quest['id']);
|
||||||
|
|
||||||
// Save answers
|
// 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] : '';
|
$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)
|
public function matchAnswersOfCharacter($seminary, $questgroup, $quest, $character, $answers)
|
||||||
{
|
{
|
||||||
// Get right answers
|
// Get right answers
|
||||||
$regexs = $this->Textinput->getTextinputRegexs($quest['id']);
|
$fields = $this->Textinput->getTextinputFields($quest['id']);
|
||||||
|
|
||||||
// Match regexs with user answers
|
// Match regexs with user answers
|
||||||
$allSolved = true;
|
$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;
|
$allSolved = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if(!$this->isMatching($field['regex'], $answers[$pos]))
|
||||||
if(!$this->isMatching($regex['regex'], $answers[$i]))
|
|
||||||
{
|
{
|
||||||
$allSolved = false;
|
$allSolved = false;
|
||||||
break;
|
break;
|
||||||
|
|
@ -115,26 +116,23 @@
|
||||||
// Get Task
|
// Get Task
|
||||||
$task = $this->Textinput->getTextinputQuest($quest['id']);
|
$task = $this->Textinput->getTextinputQuest($quest['id']);
|
||||||
|
|
||||||
// Process text
|
// Get fields
|
||||||
$textParts = preg_split('/(\$\$)/', ' '.$task['text'].' ', -1, PREG_SPLIT_NO_EMPTY);
|
$fields = $this->Textinput->getTextinputFields($quest['id']);
|
||||||
|
|
||||||
// Has Character already solved Quest?
|
// Has Character already solved Quest?
|
||||||
$solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']);
|
$solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']);
|
||||||
|
|
||||||
// Get Character answers
|
// Get Character answers
|
||||||
$regexs = null;
|
if(!$solved || $this->request->getGetParam('show-answer') == 'true' || $this->request->getGetParam('status') == 'solved') {
|
||||||
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']);
|
||||||
$regexs = $this->Textinput->getTextinputRegexs($quest['id']);
|
|
||||||
foreach($regexs as &$regex) {
|
|
||||||
$regex['answer'] = $this->Textinput->getCharacterSubmission($regex['id'], $character['id']);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Pass data to view
|
// Pass data to view
|
||||||
$this->set('texts', $textParts);
|
$this->set('task', $task);
|
||||||
$this->set('regexs', $regexs);
|
$this->set('fields', $fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -154,19 +152,22 @@
|
||||||
$task = $this->Textinput->getTextinputQuest($quest['id']);
|
$task = $this->Textinput->getTextinputQuest($quest['id']);
|
||||||
|
|
||||||
// Process text
|
// 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
|
// Get Character answers
|
||||||
$regexs = $this->Textinput->getTextinputRegexs($quest['id']);
|
foreach($fields as &$field)
|
||||||
foreach($regexs as &$regex) {
|
{
|
||||||
$regex['answer'] = $this->Textinput->getCharacterSubmission($regex['id'], $character['id']);
|
$field['answer'] = $this->Textinput->getCharacterSubmission($field['id'], $character['id']);
|
||||||
$regex['right'] = $this->isMatching($regex['regex'], $regex['answer']);
|
$field['right'] = $this->isMatching($field['regex'], $field['answer']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Pass data to view
|
// Pass data to view
|
||||||
$this->set('texts', $textParts);
|
$this->set('task', $task);
|
||||||
$this->set('regexs', $regexs);
|
$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
|
* @param int $questId ID of Quest
|
||||||
* @return array Regexs
|
* @return array Fields
|
||||||
*/
|
*/
|
||||||
public function getTextinputRegexs($questId)
|
public function getTextinputFields($questId)
|
||||||
{
|
{
|
||||||
return $this->db->query(
|
return $this->db->query(
|
||||||
'SELECT id, number, regex '.
|
'SELECT fields.id, fields.number, fields.regex, fieldsizes.id AS fieldsize_id, fieldsizes.size '.
|
||||||
'FROM questtypes_textinput_regexs '.
|
'FROM questtypes_textinput_fields AS fields '.
|
||||||
'WHERE questtypes_textinput_quest_id = ? '.
|
'LEFT JOIN questtypes_textinput_fieldsizes AS fieldsizes ON fieldsizes.id = fields.questtypes_textinput_fieldsize_id '.
|
||||||
'ORDER BY number ASC',
|
'WHERE fields.questtypes_textinput_quest_id = ? '.
|
||||||
|
'ORDER BY fields.number ASC',
|
||||||
'i',
|
'i',
|
||||||
$questId
|
$questId
|
||||||
);
|
);
|
||||||
|
|
@ -69,40 +70,40 @@
|
||||||
/**
|
/**
|
||||||
* Save Character’s submitted answer for one textinput field.
|
* 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 int $characterId ID of Character
|
||||||
* @param string $answer Submitted answer for this field
|
* @param string $answer Submitted answer for this field
|
||||||
*/
|
*/
|
||||||
public function setCharacterSubmission($regexId, $characterId, $answer)
|
public function setCharacterSubmission($fieldId, $characterId, $answer)
|
||||||
{
|
{
|
||||||
$this->db->query(
|
$this->db->query(
|
||||||
'INSERT INTO questtypes_textinput_regexs_characters '.
|
'INSERT INTO questtypes_textinput_fields_characters '.
|
||||||
'(questtypes_textinput_regex_id, character_id, value) '.
|
'(questtypes_textinput_field_id, character_id, value) '.
|
||||||
'VALUES '.
|
'VALUES '.
|
||||||
'(?, ?, ?) '.
|
'(?, ?, ?) '.
|
||||||
'ON DUPLICATE KEY UPDATE '.
|
'ON DUPLICATE KEY UPDATE '.
|
||||||
'value = ?',
|
'value = ?',
|
||||||
'iiss',
|
'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
|
* @param int $characterId ID of Character
|
||||||
* @return string Submitted answer for this field or empty string
|
* @return string Submitted answer for this field or empty string
|
||||||
*/
|
*/
|
||||||
public function getCharacterSubmission($regexId, $characterId)
|
public function getCharacterSubmission($fieldId, $characterId)
|
||||||
{
|
{
|
||||||
$data = $this->db->query(
|
$data = $this->db->query(
|
||||||
'SELECT value '.
|
'SELECT value '.
|
||||||
'FROM questtypes_textinput_regexs_characters '.
|
'FROM questtypes_textinput_fields_characters '.
|
||||||
'WHERE questtypes_textinput_regex_id = ? AND character_id = ? ',
|
'WHERE questtypes_textinput_field_id = ? AND character_id = ? ',
|
||||||
'ii',
|
'ii',
|
||||||
$regexId, $characterId
|
$fieldId, $characterId
|
||||||
);
|
);
|
||||||
if(!empty($data)) {
|
if(!empty($data)) {
|
||||||
return $data[0]['value'];
|
return $data[0]['value'];
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,25 @@
|
||||||
<form method="post" class="textinput">
|
<form method="post" class="textinput">
|
||||||
<p>
|
<p>
|
||||||
<?php foreach($texts as $i => &$text) : ?>
|
<?php
|
||||||
<?php if($i > 0) : ?>
|
$text = $t->t($task['text']);
|
||||||
<input type="text" name="answers[<?=$i-1?>]" value="<?=$regexs[$i-1]['answer']?>" />
|
|
||||||
<?php endif ?>
|
// Insert input fields
|
||||||
<?=$t->t($text)?>
|
foreach(array_reverse($fields) as &$field)
|
||||||
<?php endforeach ?>
|
{
|
||||||
|
// 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>
|
</p>
|
||||||
<input type="submit" name="submit" value="<?=_('solve')?>" />
|
<input type="submit" name="submit" value="<?=_('solve')?>" />
|
||||||
</form>
|
</form>
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,21 @@
|
||||||
<?php foreach($texts as $i => &$text) : ?>
|
<p>
|
||||||
<?php if($i > 0) : ?>
|
<?php
|
||||||
<span style="background-color:grey"><?=$regexs[$i-1]['answer']?></span>
|
$text = $t->t($task['text']);
|
||||||
<?php if($regexs[$i-1]['right']) : ?>✓<?php else: ?>✕<?php endif ?>
|
|
||||||
<?php endif ?>
|
// Insert Character answers
|
||||||
<?=$t->t($text)?>
|
foreach(array_reverse($fields) as &$field)
|
||||||
<?php endforeach ?>
|
{
|
||||||
|
// 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