From 7780ac7f2d0843da3de5c8773f60dd9d190c4427 Mon Sep 17 00:00:00 2001 From: coderkun Date: Fri, 21 Mar 2014 12:47:08 +0100 Subject: [PATCH] implement saving of Quest answers in database --- controllers/QuestsController.inc | 9 +-- .../MultiplechoiceQuesttypeController.inc | 34 +++++++--- .../MultiplechoiceQuesttypeModel.inc | 51 ++++++++++++++- questtypes/multiplechoice/html/quest.tpl | 4 +- questtypes/multiplechoice/html/submission.tpl | 9 +++ .../submit/SubmitQuesttypeController.inc | 4 ++ .../TextinputQuesttypeController.inc | 62 +++++++++++++++---- .../textinput/TextinputQuesttypeModel.inc | 49 ++++++++++++++- questtypes/textinput/html/quest.tpl | 4 +- questtypes/textinput/html/submission.tpl | 7 +++ 10 files changed, 201 insertions(+), 32 deletions(-) diff --git a/controllers/QuestsController.inc b/controllers/QuestsController.inc index f3340578..54ffd25e 100644 --- a/controllers/QuestsController.inc +++ b/controllers/QuestsController.inc @@ -191,6 +191,9 @@ } } + // Has Character solved quest? + $solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']); + // Pass data to view $this->set('seminary', $seminary); @@ -204,6 +207,7 @@ $this->set('nextquestgroup', $nextQuestgroup); $this->set('task', $task); $this->set('media', $questmedia); + $this->set('solved', $solved); } @@ -336,14 +340,11 @@ $questtypeAgent = $this->loadQuesttypeAgent($questtypeClassname, $request, $response); // 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 $answers = $this->request->getPostParam('answers'); - // Store answers in session - $_SESSION['answers'][$quest['id']] = $answers; - // Save answers in database $questtypeAgent->saveAnswersOfCharacter($quest['id'], $character['id'], $answers); diff --git a/questtypes/multiplechoice/MultiplechoiceQuesttypeController.inc b/questtypes/multiplechoice/MultiplechoiceQuesttypeController.inc index 77717863..f17998cc 100644 --- a/questtypes/multiplechoice/MultiplechoiceQuesttypeController.inc +++ b/questtypes/multiplechoice/MultiplechoiceQuesttypeController.inc @@ -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,15 @@ */ 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) { - // Answers - if(!array_key_exists('answers', $_SESSION)) { - $_SESSION['answers'] = array(); - } - $answers = array_key_exists($questId, $_SESSION['answers']) ? $_SESSION['answers'][$questId] : array(); - // Get questions $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 $this->set('questions', $questions); - $this->set('answers', $answers); + $this->set('solved', $solved); } /** * Action: submission. - * @TODO submission() * * Show the submission of a Character for a Quest. * @@ -107,6 +114,15 @@ */ 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); } } diff --git a/questtypes/multiplechoice/MultiplechoiceQuesttypeModel.inc b/questtypes/multiplechoice/MultiplechoiceQuesttypeModel.inc index cf170390..fef90bcb 100644 --- a/questtypes/multiplechoice/MultiplechoiceQuesttypeModel.inc +++ b/questtypes/multiplechoice/MultiplechoiceQuesttypeModel.inc @@ -32,7 +32,7 @@ public function getQuestionsOfQuest($questId) { return $this->db->query( - 'SELECT question, tick '. + 'SELECT id, pos, question, tick '. 'FROM questtypes_multiplechoice '. 'WHERE quest_id = ?', 'i', @@ -51,7 +51,7 @@ public function getTickQuestionsOfQuest($questId) { return $this->db->query( - 'SELECT question, tick, pos '. + 'SELECT id, question, tick, pos '. 'FROM questtypes_multiplechoice '. 'WHERE quest_id = ? AND tick = True', 'i', @@ -59,6 +59,53 @@ ); } + + /** + * Save Character’s 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; + } + } ?> diff --git a/questtypes/multiplechoice/html/quest.tpl b/questtypes/multiplechoice/html/quest.tpl index 1e64f3c7..15254ad9 100644 --- a/questtypes/multiplechoice/html/quest.tpl +++ b/questtypes/multiplechoice/html/quest.tpl @@ -2,10 +2,10 @@
    &$question) : ?>
  1. - />
- + /> diff --git a/questtypes/multiplechoice/html/submission.tpl b/questtypes/multiplechoice/html/submission.tpl index e69de29b..0459b59b 100644 --- a/questtypes/multiplechoice/html/submission.tpl +++ b/questtypes/multiplechoice/html/submission.tpl @@ -0,0 +1,9 @@ + diff --git a/questtypes/submit/SubmitQuesttypeController.inc b/questtypes/submit/SubmitQuesttypeController.inc index dcbb81e6..f6a8091a 100644 --- a/questtypes/submit/SubmitQuesttypeController.inc +++ b/questtypes/submit/SubmitQuesttypeController.inc @@ -83,10 +83,14 @@ $wordcount = count(preg_split('/\s+/', $characterSubmission['text'])); } + // Has Character already solved Quest? + $solved = $this->Quests->hasCharacterSolvedQuest($questId, $characterId); + // Pass data to view $this->set('submission', $characterSubmission); $this->set('wordcount', $wordcount); + $this->set('solved', $solved); } diff --git a/questtypes/textinput/TextinputQuesttypeController.inc b/questtypes/textinput/TextinputQuesttypeController.inc index 7963ffd7..50f56b14 100644 --- a/questtypes/textinput/TextinputQuesttypeController.inc +++ b/questtypes/textinput/TextinputQuesttypeController.inc @@ -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); } } diff --git a/questtypes/textinput/TextinputQuesttypeModel.inc b/questtypes/textinput/TextinputQuesttypeModel.inc index d242f251..f8282d14 100644 --- a/questtypes/textinput/TextinputQuesttypeModel.inc +++ b/questtypes/textinput/TextinputQuesttypeModel.inc @@ -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 ''; + } + } ?> diff --git a/questtypes/textinput/html/quest.tpl b/questtypes/textinput/html/quest.tpl index 40f0d213..c61bd50c 100644 --- a/questtypes/textinput/html/quest.tpl +++ b/questtypes/textinput/html/quest.tpl @@ -1,11 +1,11 @@
&$text) : ?> 0) : ?> - + />

- + />
diff --git a/questtypes/textinput/html/submission.tpl b/questtypes/textinput/html/submission.tpl index e69de29b..dbc453c3 100644 --- a/questtypes/textinput/html/submission.tpl +++ b/questtypes/textinput/html/submission.tpl @@ -0,0 +1,7 @@ + &$text) : ?> + 0) : ?> + + + + +