From 5461a4d93711d4c0c8b124717970cfda56723a8e Mon Sep 17 00:00:00 2001 From: coderkun Date: Wed, 12 Mar 2014 01:29:11 +0100 Subject: [PATCH] implement Questtype ?Multiple choice? --- .../MultiplechoiceQuesttypeAgent.inc | 24 +++++ .../MultiplechoiceQuesttypeController.inc | 88 +++++++++++++++++++ .../MultiplechoiceQuesttypeModel.inc | 64 ++++++++++++++ questtypes/multiplechoice/html/index.tpl | 11 +++ 4 files changed, 187 insertions(+) create mode 100644 questtypes/multiplechoice/MultiplechoiceQuesttypeAgent.inc create mode 100644 questtypes/multiplechoice/MultiplechoiceQuesttypeController.inc create mode 100644 questtypes/multiplechoice/MultiplechoiceQuesttypeModel.inc create mode 100644 questtypes/multiplechoice/html/index.tpl diff --git a/questtypes/multiplechoice/MultiplechoiceQuesttypeAgent.inc b/questtypes/multiplechoice/MultiplechoiceQuesttypeAgent.inc new file mode 100644 index 00000000..2789c5f6 --- /dev/null +++ b/questtypes/multiplechoice/MultiplechoiceQuesttypeAgent.inc @@ -0,0 +1,24 @@ + + * @copyright 2014 Heinrich-Heine-Universität Düsseldorf + * @license http://www.gnu.org/licenses/gpl.html + * @link https://bitbucket.org/coderkun/the-legend-of-z + */ + + namespace hhu\z\questtypes; + + + /** + * QuesttypeAgent for multiple choice. + * + * @author Oliver Hanraths + */ + class MultiplechoiceQuesttypeAgent extends \hhu\z\QuesttypeAgent + { + } + +?> diff --git a/questtypes/multiplechoice/MultiplechoiceQuesttypeController.inc b/questtypes/multiplechoice/MultiplechoiceQuesttypeController.inc new file mode 100644 index 00000000..44c99e3b --- /dev/null +++ b/questtypes/multiplechoice/MultiplechoiceQuesttypeController.inc @@ -0,0 +1,88 @@ + + * @copyright 2014 Heinrich-Heine-Universität Düsseldorf + * @license http://www.gnu.org/licenses/gpl.html + * @link https://bitbucket.org/coderkun/the-legend-of-z + */ + + namespace hhu\z\questtypes; + + + /** + * Controller of the MultiplechoiceQuesttypeAgent multiple choice. + * + * @author Oliver Hanraths + */ + class MultiplechoiceQuesttypeController extends \hhu\z\QuesttypeController + { + + + + + /** + * Action: index. + * + * Display a text with input fields and evaluate if user input + * matches with stored regular expressions. + */ + public function index($questId) + { + // Answers + if(!array_key_exists('answers', $_SESSION)) { + $_SESSION['answers'] = array(); + } + $answers = array_key_exists($questId, $_SESSION['answers']) ? $_SESSION['answers'][$questId] : array(); + + // Check for submission + if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('submit'))) + { + // Get answers + $answers = $this->request->getPostParam('answers'); + + // Store answers in session + $_SESSION['answers'][$questId] = $answers; + + // Get right answers + $tickQuestions = $this->Multiplechoice->getTickQuestionsOfQuest($questId); + + // Match tick questions with user answers + $allSolved = true; + foreach($tickQuestions as &$tickQuestion) + { + $pos = intval($tickQuestion['pos'])-1; + if(!array_key_exists($pos, $answers) && $answers[$pos] == 'true') + { + $allSolved = false; + break; + } + else { + unset($answers[$pos]); + } + } + + // Set status + if($allSolved && count($answers) == 0) { + $this->setQuestSolved(); + } + else { + $this->setQuestUnsolved(); + } + } + + + // Get questions + $questions = $this->Multiplechoice->getQuestionsOfQuest($questId); + + + // Pass data to view + $this->set('questions', $questions); + $this->set('answers', $answers); + } + + } + +?> diff --git a/questtypes/multiplechoice/MultiplechoiceQuesttypeModel.inc b/questtypes/multiplechoice/MultiplechoiceQuesttypeModel.inc new file mode 100644 index 00000000..cf170390 --- /dev/null +++ b/questtypes/multiplechoice/MultiplechoiceQuesttypeModel.inc @@ -0,0 +1,64 @@ + + * @copyright 2014 Heinrich-Heine-Universität Düsseldorf + * @license http://www.gnu.org/licenses/gpl.html + * @link https://bitbucket.org/coderkun/the-legend-of-z + */ + + namespace hhu\z\questtypes; + + + /** + * Model of the MultiplechoiceQuesttypeAgent for multiple choice. + * + * @author Oliver Hanraths + */ + class MultiplechoiceQuesttypeModel extends \hhu\z\QuesttypeModel + { + + + + + /** + * Get all multiple choice questions of a Quest. + * + * @param int $questId ID of Quest + * @return array Multiple choice questions + */ + public function getQuestionsOfQuest($questId) + { + return $this->db->query( + 'SELECT question, tick '. + 'FROM questtypes_multiplechoice '. + 'WHERE quest_id = ?', + 'i', + $questId + ); + } + + + /** + * Get all multiple choice questions of a Quest that should be + * ticked. + * + * @param int $questId ID of Quest + * @return array Multiple choice questions that should be ticked + */ + public function getTickQuestionsOfQuest($questId) + { + return $this->db->query( + 'SELECT question, tick, pos '. + 'FROM questtypes_multiplechoice '. + 'WHERE quest_id = ? AND tick = True', + 'i', + $questId + ); + } + + } + +?> diff --git a/questtypes/multiplechoice/html/index.tpl b/questtypes/multiplechoice/html/index.tpl new file mode 100644 index 00000000..1e64f3c7 --- /dev/null +++ b/questtypes/multiplechoice/html/index.tpl @@ -0,0 +1,11 @@ +
+
    + &$question) : ?> +
  1. + ]"> +
  2. + +
+ +