implement saving of Quest answers in database

This commit is contained in:
coderkun 2014-03-21 12:47:08 +01:00
commit 2429bb4c8f
10 changed files with 201 additions and 32 deletions

View file

@ -191,6 +191,9 @@
} }
} }
// Has Character solved quest?
$solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']);
// Pass data to view // Pass data to view
$this->set('seminary', $seminary); $this->set('seminary', $seminary);
@ -204,6 +207,7 @@
$this->set('nextquestgroup', $nextQuestgroup); $this->set('nextquestgroup', $nextQuestgroup);
$this->set('task', $task); $this->set('task', $task);
$this->set('media', $questmedia); $this->set('media', $questmedia);
$this->set('solved', $solved);
} }
@ -336,14 +340,11 @@
$questtypeAgent = $this->loadQuesttypeAgent($questtypeClassname, $request, $response); $questtypeAgent = $this->loadQuesttypeAgent($questtypeClassname, $request, $response);
// Solve Quest // Solve Quest
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('submit'))) if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('submit')) && !$this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']))
{ {
// Get user answers // Get user answers
$answers = $this->request->getPostParam('answers'); $answers = $this->request->getPostParam('answers');
// Store answers in session
$_SESSION['answers'][$quest['id']] = $answers;
// Save answers in database // Save answers in database
$questtypeAgent->saveAnswersOfCharacter($quest['id'], $character['id'], $answers); $questtypeAgent->saveAnswersOfCharacter($quest['id'], $character['id'], $answers);

View file

@ -25,7 +25,6 @@
/** /**
* Save the answers of a Character for a Quest. * Save the answers of a Character for a Quest.
* TODO saveAnswersOfCharacter()
* *
* @param int $questId ID of Quest to save answers for * @param int $questId ID of Quest to save answers for
* @param int $characterId ID of Character to save answers of * @param int $characterId ID of Character to save answers of
@ -33,6 +32,15 @@
*/ */
public function saveAnswersOfCharacter($questId, $characterId, $answers) public function saveAnswersOfCharacter($questId, $characterId, $answers)
{ {
// Get questions
$questions = $this->Multiplechoice->getQuestionsOfQuest($questId);
// Save answers
foreach($questions as &$question)
{
$answer = (array_key_exists(intval($question['pos'])-1, $answers)) ? true : false;
$this->Multiplechoice->setCharacterSubmission($question['id'], $characterId, $answer);
}
} }
@ -80,25 +88,24 @@
*/ */
public function quest($questId, $characterId) 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 questions // Get questions
$questions = $this->Multiplechoice->getQuestionsOfQuest($questId); $questions = $this->Multiplechoice->getQuestionsOfQuest($questId);
foreach($questions as &$question) {
$question['answer'] = $this->Multiplechoice->getCharacterSubmission($question['id'], $characterId);
}
// Has Character already solved Quest?
$solved = $this->Quests->hasCharacterSolvedQuest($questId, $characterId);
// Pass data to view // Pass data to view
$this->set('questions', $questions); $this->set('questions', $questions);
$this->set('answers', $answers); $this->set('solved', $solved);
} }
/** /**
* Action: submission. * Action: submission.
* @TODO submission()
* *
* Show the submission of a Character for a Quest. * Show the submission of a Character for a Quest.
* *
@ -107,6 +114,15 @@
*/ */
public function submission($questId, $characterId) public function submission($questId, $characterId)
{ {
// Get questions
$questions = $this->Multiplechoice->getQuestionsOfQuest($questId);
foreach($questions as &$question) {
$question['answer'] = $this->Multiplechoice->getCharacterSubmission($question['id'], $characterId);
}
// Pass data to view
$this->set('questions', $questions);
} }
} }

View file

@ -32,7 +32,7 @@
public function getQuestionsOfQuest($questId) public function getQuestionsOfQuest($questId)
{ {
return $this->db->query( return $this->db->query(
'SELECT question, tick '. 'SELECT id, pos, question, tick '.
'FROM questtypes_multiplechoice '. 'FROM questtypes_multiplechoice '.
'WHERE quest_id = ?', 'WHERE quest_id = ?',
'i', 'i',
@ -51,7 +51,7 @@
public function getTickQuestionsOfQuest($questId) public function getTickQuestionsOfQuest($questId)
{ {
return $this->db->query( return $this->db->query(
'SELECT question, tick, pos '. 'SELECT id, question, tick, pos '.
'FROM questtypes_multiplechoice '. 'FROM questtypes_multiplechoice '.
'WHERE quest_id = ? AND tick = True', 'WHERE quest_id = ? AND tick = True',
'i', 'i',
@ -59,6 +59,53 @@
); );
} }
/**
* Save Characters submitted answer for one option.
*
* @param int $multipleChoiceId ID of multiple choice option
* @param int $characterId ID of Character
* @param boolean $answer Submitted answer for this option
*/
public function setCharacterSubmission($multipleChoiceId, $characterId, $answer)
{
$this->db->query(
'INSERT INTO questtypes_multiplechoice_characters '.
'(questtypes_multiplechoice_id, character_id, ticked) '.
'VALUES '.
'(?, ?, ?) '.
'ON DUPLICATE KEY UPDATE '.
'ticked = ?',
'iiii',
$multipleChoiceId, $characterId, $answer, $answer
);
}
/**
* Get answer of one option submitted by Character.
*
* @param int $multipleChoiceId ID of multiple choice option
* @param int $characterId ID of Character
* @return boolean Submitted answer of Character or false
*/
public function getCharacterSubmission($multipleChoiscId, $characterId)
{
$data = $this->db->query(
'SELECT ticked '.
'FROM questtypes_multiplechoice_characters '.
'WHERE questtypes_multiplechoice_id = ? AND character_id = ? ',
'ii',
$multipleChoiscId, $characterId
);
if(!empty($data)) {
return $data[0]['ticked'];
}
return false;
}
} }
?> ?>

View file

@ -2,10 +2,10 @@
<ol> <ol>
<?php foreach($questions as $i => &$question) : ?> <?php foreach($questions as $i => &$question) : ?>
<li> <li>
<input type="checkbox" id="answers[<?=$i?>]" name="answers[<?=$i?>]" value="true" <?=(array_key_exists($i, $answers)) ? 'checked="checked' : '' ?> /> <input type="checkbox" id="answers[<?=$i?>]" name="answers[<?=$i?>]" value="true" <?=($question['answer']) ? 'checked="checked"' : '' ?> <?=($solved) ? 'disabled="disabled"' : '' ?>/>
<label for="answers[<?=$i?>]"><?=\hhu\z\Utils::t($question['question'])?></label> <label for="answers[<?=$i?>]"><?=\hhu\z\Utils::t($question['question'])?></label>
</li> </li>
<?php endforeach ?> <?php endforeach ?>
</ol> </ol>
<input type="submit" name="submit" value="<?=_('solve')?>" /> <input type="submit" name="submit" value="<?=_('solve')?>" <?=($solved) ? 'disabled="disabled"' : '' ?> />
</form> </form>

View file

@ -0,0 +1,9 @@
<ul>
<?php foreach($questions as &$question) : ?>
<li>
<?php if($question['answer']) : ?>☑<?php else : ?>☐<?php endif ?>
<?php if($question['answer'] == $question['tick']) : ?>✓<?php else : ?>×<?php endif ?>
<?=$question['question']?>
</li>
<?php endforeach ?>
</ul>

View file

@ -83,10 +83,14 @@
$wordcount = count(preg_split('/\s+/', $characterSubmission['text'])); $wordcount = count(preg_split('/\s+/', $characterSubmission['text']));
} }
// Has Character already solved Quest?
$solved = $this->Quests->hasCharacterSolvedQuest($questId, $characterId);
// Pass data to view // Pass data to view
$this->set('submission', $characterSubmission); $this->set('submission', $characterSubmission);
$this->set('wordcount', $wordcount); $this->set('wordcount', $wordcount);
$this->set('solved', $solved);
} }

View file

@ -25,7 +25,6 @@
/** /**
* Save the answers of a Character for a Quest. * Save the answers of a Character for a Quest.
* TODO saveAnswersOfCharacter()
* *
* @param int $questId ID of Quest to save answers for * @param int $questId ID of Quest to save answers for
* @param int $characterId ID of Character to save answers of * @param int $characterId ID of Character to save answers of
@ -33,6 +32,16 @@
*/ */
public function saveAnswersOfCharacter($questId, $characterId, $answers) 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; break;
} }
$score = preg_match($regex['regex'], $answers[$i]); if(!$this->isMatching($regex['regex'], $answers[$i]))
if($score === 0 || $score === false)
{ {
$allSolved = false; $allSolved = false;
break; break;
@ -83,29 +91,31 @@
*/ */
public function quest($questId, $characterId) 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 // Get Task
$task = $this->Textinput->getTextinputQuest($questId); $task = $this->Textinput->getTextinputQuest($questId);
// Process text // Process text
$textParts = preg_split('/(\$\$)/', $task['text'], -1, PREG_SPLIT_NO_EMPTY); $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 // Pass data to view
$this->set('texts', $textParts); $this->set('texts', $textParts);
$this->set('answers', $answers); $this->set('regexs', $regexs);
$this->set('solved', $solved);
} }
/** /**
* Action: submission. * Action: submission.
* @TODO submission()
* *
* Show the submission of a Character for a Quest. * Show the submission of a Character for a Quest.
* *
@ -114,6 +124,34 @@
*/ */
public function submission($questId, $characterId) 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);
} }
} }

View file

@ -53,7 +53,7 @@
public function getTextinputRegexs($questId) public function getTextinputRegexs($questId)
{ {
return $this->db->query( return $this->db->query(
'SELECT number, regex '. 'SELECT id, number, regex '.
'FROM questtypes_textinput_regexs '. 'FROM questtypes_textinput_regexs '.
'WHERE questtypes_textinput_quest_id = ? '. 'WHERE questtypes_textinput_quest_id = ? '.
'ORDER BY number ASC', 'ORDER BY number ASC',
@ -62,6 +62,53 @@
); );
} }
/**
* Save Characters 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 '';
}
} }
?> ?>

View file

@ -1,11 +1,11 @@
<form method="post"> <form method="post">
<?php foreach($texts as $i => &$text) : ?> <?php foreach($texts as $i => &$text) : ?>
<?php if($i > 0) : ?> <?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 ?> <?php endif ?>
<?=\hhu\z\Utils::t($text)?> <?=\hhu\z\Utils::t($text)?>
<?php endforeach ?> <?php endforeach ?>
<br /><br /> <br /><br />
<input type="submit" name="submit" value="<?=_('solve')?>" /> <input type="submit" name="submit" value="<?=_('solve')?>" <?=($solved) ? 'disabled="disabled"' : '' ?> />
</form> </form>

View file

@ -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 ?>