implement saving of Quest answers in database
This commit is contained in:
parent
a914aac3c7
commit
7780ac7f2d
10 changed files with 201 additions and 32 deletions
|
|
@ -25,7 +25,6 @@
|
|||
|
||||
/**
|
||||
* Save the answers of a Character for a Quest.
|
||||
* TODO saveAnswersOfCharacter()
|
||||
*
|
||||
* @param int $questId ID of Quest to save answers for
|
||||
* @param int $characterId ID of Character to save answers of
|
||||
|
|
@ -33,6 +32,16 @@
|
|||
*/
|
||||
public function saveAnswersOfCharacter($questId, $characterId, $answers)
|
||||
{
|
||||
// Get regexs
|
||||
$regexs = $this->Textinput->getTextinputRegexs($questId);
|
||||
|
||||
// Save answers
|
||||
foreach($regexs as &$regex)
|
||||
{
|
||||
$pos = intval($regex['number']) - 1;
|
||||
$answer = (array_key_exists($pos, $answers)) ? $answers[$pos] : '';
|
||||
$this->Textinput->setCharacterSubmission($regex['id'], $characterId, $answer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -58,8 +67,7 @@
|
|||
break;
|
||||
}
|
||||
|
||||
$score = preg_match($regex['regex'], $answers[$i]);
|
||||
if($score === 0 || $score === false)
|
||||
if(!$this->isMatching($regex['regex'], $answers[$i]))
|
||||
{
|
||||
$allSolved = false;
|
||||
break;
|
||||
|
|
@ -83,29 +91,31 @@
|
|||
*/
|
||||
public function quest($questId, $characterId)
|
||||
{
|
||||
// Answers
|
||||
if(!array_key_exists('answers', $_SESSION)) {
|
||||
$_SESSION['answers'] = array();
|
||||
}
|
||||
$answers = array_key_exists($questId, $_SESSION['answers']) ? $_SESSION['answers'][$questId] : array();
|
||||
|
||||
|
||||
// Get Task
|
||||
$task = $this->Textinput->getTextinputQuest($questId);
|
||||
|
||||
// Process text
|
||||
$textParts = preg_split('/(\$\$)/', $task['text'], -1, PREG_SPLIT_NO_EMPTY);
|
||||
|
||||
// Get Character answers
|
||||
$regexs = $this->Textinput->getTextinputRegexs($questId);
|
||||
foreach($regexs as &$regex) {
|
||||
$regex['answer'] = $this->Textinput->getCharacterSubmission($regex['id'], $characterId);
|
||||
}
|
||||
|
||||
// Has Character already solved Quest?
|
||||
$solved = $this->Quests->hasCharacterSolvedQuest($questId, $characterId);
|
||||
|
||||
|
||||
// Pass data to view
|
||||
$this->set('texts', $textParts);
|
||||
$this->set('answers', $answers);
|
||||
$this->set('regexs', $regexs);
|
||||
$this->set('solved', $solved);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Action: submission.
|
||||
* @TODO submission()
|
||||
*
|
||||
* Show the submission of a Character for a Quest.
|
||||
*
|
||||
|
|
@ -114,6 +124,34 @@
|
|||
*/
|
||||
public function submission($questId, $characterId)
|
||||
{
|
||||
// Get Task
|
||||
$task = $this->Textinput->getTextinputQuest($questId);
|
||||
|
||||
// Process text
|
||||
$textParts = preg_split('/(\$\$)/', $task['text'], -1, PREG_SPLIT_NO_EMPTY);
|
||||
|
||||
// Get Character answers
|
||||
$regexs = $this->Textinput->getTextinputRegexs($questId);
|
||||
foreach($regexs as &$regex) {
|
||||
$regex['answer'] = $this->Textinput->getCharacterSubmission($regex['id'], $characterId);
|
||||
$regex['right'] = $this->isMatching($regex['regex'], $regex['answer']);
|
||||
}
|
||||
|
||||
|
||||
// Pass data to view
|
||||
$this->set('texts', $textParts);
|
||||
$this->set('regexs', $regexs);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private function isMatching($regex, $answer)
|
||||
{
|
||||
$score = preg_match($regex, $answer);
|
||||
|
||||
|
||||
return ($score !== false && $score > 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@
|
|||
public function getTextinputRegexs($questId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT number, regex '.
|
||||
'SELECT id, number, regex '.
|
||||
'FROM questtypes_textinput_regexs '.
|
||||
'WHERE questtypes_textinput_quest_id = ? '.
|
||||
'ORDER BY number ASC',
|
||||
|
|
@ -62,6 +62,53 @@
|
|||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save Character’s submitted answer for one textinput field.
|
||||
*
|
||||
* @param int $regexId ID of regex
|
||||
* @param int $characterId ID of Character
|
||||
* @param string $answer Submitted answer for this field
|
||||
*/
|
||||
public function setCharacterSubmission($regexId, $characterId, $answer)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO questtypes_textinput_regexs_characters '.
|
||||
'(questtypes_textinput_regex_id, character_id, value) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?) '.
|
||||
'ON DUPLICATE KEY UPDATE '.
|
||||
'value = ?',
|
||||
'iiss',
|
||||
$regexId, $characterId, $answer, $answer
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get answer of one regex input field submitted by Character.
|
||||
*
|
||||
* @param int $regexId ID of regex
|
||||
* @param int $characterId ID of Character
|
||||
* @return string Submitted answer for this field or empty string
|
||||
*/
|
||||
public function getCharacterSubmission($regexId, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT value '.
|
||||
'FROM questtypes_textinput_regexs_characters '.
|
||||
'WHERE questtypes_textinput_regex_id = ? AND character_id = ? ',
|
||||
'ii',
|
||||
$regexId, $characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0]['value'];
|
||||
}
|
||||
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
<form method="post">
|
||||
<?php foreach($texts as $i => &$text) : ?>
|
||||
<?php if($i > 0) : ?>
|
||||
<input type="text" name="answers[<?=$i-1?>]" value="<?=(array_key_exists($i-1, $answers)) ? $answers[$i-1] : '' ?>" />
|
||||
<input type="text" name="answers[<?=$i-1?>]" value="<?=$regexs[$i-1]['answer']?>" <?=($solved) ? 'disabled="disabled"' : '' ?>/>
|
||||
<?php endif ?>
|
||||
<?=\hhu\z\Utils::t($text)?>
|
||||
<?php endforeach ?>
|
||||
|
||||
<br /><br />
|
||||
<input type="submit" name="submit" value="<?=_('solve')?>" />
|
||||
<input type="submit" name="submit" value="<?=_('solve')?>" <?=($solved) ? 'disabled="disabled"' : '' ?> />
|
||||
</form>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
<?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 ?>
|
||||
<?=\hhu\z\Utils::t($text)?>
|
||||
<?php endforeach ?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue