From d3274412ca9d71b603c200483a866989ec89f2aa Mon Sep 17 00:00:00 2001 From: oliver Date: Fri, 25 Dec 2015 16:30:06 +0100 Subject: [PATCH] add Stationtype ?multiplechoice? --- db/create.sql | 43 ++++++ .../MultiplechoiceStationtypeAgent.inc | 24 ++++ .../MultiplechoiceStationtypeController.inc | 127 ++++++++++++++++++ .../MultiplechoiceStationtypeModel.inc | 68 ++++++++++ stationtypes/multiplechoice/html/quest.tpl | 11 ++ 5 files changed, 273 insertions(+) create mode 100644 stationtypes/multiplechoice/MultiplechoiceStationtypeAgent.inc create mode 100644 stationtypes/multiplechoice/MultiplechoiceStationtypeController.inc create mode 100644 stationtypes/multiplechoice/MultiplechoiceStationtypeModel.inc create mode 100644 stationtypes/multiplechoice/html/quest.tpl diff --git a/db/create.sql b/db/create.sql index 2a241fe4..c3fbee7d 100644 --- a/db/create.sql +++ b/db/create.sql @@ -1971,6 +1971,49 @@ CREATE TABLE `stationtypes_keyword_charactergroups` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `stationtypes_multiplechoice` +-- + +DROP TABLE IF EXISTS `stationtypes_multiplechoice`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `stationtypes_multiplechoice` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `created_user_id` int(11) NOT NULL, + `station_id` int(11) NOT NULL, + `pos` int(11) NOT NULL, + `answer` text COLLATE utf8mb4_unicode_ci NOT NULL, + `tick` tinyint(1) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `questtypes_multiplechoice_id_2` (`station_id`,`pos`), + KEY `created_user_id` (`created_user_id`), + KEY `questtypes_multiplechoice_id` (`station_id`), + CONSTRAINT `stationtypes_multiplechoice_ibfk_1` FOREIGN KEY (`created_user_id`) REFERENCES `users` (`id`), + CONSTRAINT `stationtypes_multiplechoice_ibfk_2` FOREIGN KEY (`station_id`) REFERENCES `charactergroupsqueststations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `stationtypes_multiplechoice_charactergroups` +-- + +DROP TABLE IF EXISTS `stationtypes_multiplechoice_charactergroups`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `stationtypes_multiplechoice_charactergroups` ( + `stationtypes_multiplechoice_id` int(11) NOT NULL, + `charactergroup_id` int(11) NOT NULL, + `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `ticked` tinyint(1) NOT NULL, + PRIMARY KEY (`stationtypes_multiplechoice_id`,`charactergroup_id`), + KEY `character_id` (`charactergroup_id`), + CONSTRAINT `stationtypes_multiplechoice_charactergroups_ibfk_1` FOREIGN KEY (`stationtypes_multiplechoice_id`) REFERENCES `stationtypes_multiplechoice` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `stationtypes_multiplechoice_charactergroups_ibfk_2` FOREIGN KEY (`charactergroup_id`) REFERENCES `charactergroups` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + -- -- Table structure for table `userroles` -- diff --git a/stationtypes/multiplechoice/MultiplechoiceStationtypeAgent.inc b/stationtypes/multiplechoice/MultiplechoiceStationtypeAgent.inc new file mode 100644 index 00000000..e2b59fa1 --- /dev/null +++ b/stationtypes/multiplechoice/MultiplechoiceStationtypeAgent.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\stationtypes; + + + /** + * StationtypeAgent for a multiple choice task. + * + * @author Oliver Hanraths + */ + class MultiplechoiceStationtypeAgent extends \hhu\z\agents\StationtypeAgent + { + } + +?> diff --git a/stationtypes/multiplechoice/MultiplechoiceStationtypeController.inc b/stationtypes/multiplechoice/MultiplechoiceStationtypeController.inc new file mode 100644 index 00000000..b2f5d7d7 --- /dev/null +++ b/stationtypes/multiplechoice/MultiplechoiceStationtypeController.inc @@ -0,0 +1,127 @@ + + * @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\stationtypes; + + + /** + * Controller of the StationtypeAgent for a multiple choice task. + * + * @author Oliver Hanraths + */ + class MultiplechoiceStationtypeController extends \hhu\z\controllers\StationtypeController + { + + + + + /** + * Save the answer of a Character group for a Station. + * + * @param array $seminary Current Seminary data + * @param array $questgroup Current Questgroup data + * @param array $quest Current Quest data + * @param array $station Current Station data + * @param array $charactergroup Current Character group data + * @param array $answer Character group answer for the Station + */ + public function saveAnswer($seminary, $groupsgroup, $quest, $station, $charactergroup, $answer) + { + $answers = (!is_array($answer)) ? array() : $answer; + $solutions = $this->Multiplechoice->getAnswers($station['id']); + foreach($solutions as &$solution) + { + $answer = (array_key_exists($solution['pos']-1, $answers)) ? true : false; + $this->Multiplechoice->setCharactergroupSubmission($solution['id'], $charactergroup['id'], $answer); + } + } + + + /** + * Check if answer of a Character group for a Station matches the correct one. + * + * @param array $seminary Current Seminary data + * @param array $questgroup Current Questgroup data + * @param array $quest Current Quest data + * @param array $station Current Station data + * @param array $charactergroup Current Character group data + * @param array $answer Character group answer for the Station + * @return boolean True/false for a right/wrong answer + */ + public function matchAnswer($seminary, $groupsgroup, $quest, $station, $charactergroup, $answer) + { + $answers = (!is_array($answer)) ? array() : $answer; + $solutions = $this->Multiplechoice->getAnswers($station['id']); + foreach($solutions as &$solution) + { + if(is_null($solution['tick'])) { + continue; + } + if($solution['tick']) { + if(!array_key_exists($solution['pos']-1, $answers)) { + return false; + } + } + else { + if(array_key_exists($solution['pos']-1, $answers)) { + return false; + } + } + } + + + // All questions correct answerd + return true; + } + + + /** + * Action: quest. + * + * Show the task of a Station. + * + * @param array $seminary Current Seminary data + * @param array $questgroup Current Questgroup data + * @param array $quest Current Quest data + * @param array $station Current Station data + * @param array $charactergroup Current Character group data + */ + public function quest($seminary, $groupsgroup, $quest, $station, $charactergroup) + { + // Get answers + $answers = $this->Multiplechoice->getAnswers($station['id']); + + + // Pass data to view + $this->set('answers', $answers); + } + + + + + /** + * Check if an Character answer matches a Regex. + * + * @param string $regex Regex to match against + * @param string $answer Character answer to match + * @return boolean Whether answer matches Regex or not + */ + private function isMatching($regex, $answer) + { + $score = preg_match($regex, trim($answer)); + + + return ($score !== false && $score > 0); + } + + } + +?> diff --git a/stationtypes/multiplechoice/MultiplechoiceStationtypeModel.inc b/stationtypes/multiplechoice/MultiplechoiceStationtypeModel.inc new file mode 100644 index 00000000..d066919d --- /dev/null +++ b/stationtypes/multiplechoice/MultiplechoiceStationtypeModel.inc @@ -0,0 +1,68 @@ + + * @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\stationtypes; + + + /** + * Model of the StationtypeAgent for a multiple choice task. + * + * @author Oliver Hanraths + */ + class MultiplechoiceStationtypeModel extends \hhu\z\models\StationtypeModel + { + + + + + /** + * Get all answers for a Station + * + * @param int $stationId ID of Station + * @return array List of answers + */ + public function getAnswers($stationId) + { + return $this->db->query( + 'SELECT id, pos, answer, tick '. + 'FROM stationtypes_multiplechoice '. + 'WHERE station_id = ? '. + 'ORDER BY pos', + 'i', + $stationId + ); + } + + + /** + * Save Character group’s submitted answer for a station. + * + * @param int $stationId ID of Station + * @param int $charactergroupId ID of Character group + * @param string $answer Submitted answer + */ + public function setCharactergroupSubmission($answerId, $charactergroupId, $answer) + { + $this->db->query( + 'INSERT INTO stationtypes_multiplechoice_charactergroups '. + '(stationtypes_multiplechoice_id, charactergroup_id, ticked) '. + 'VALUES '. + '(?, ?, ?) '. + 'ON DUPLICATE KEY UPDATE '. + 'ticked = ?', + 'iiii', + $answerId, $charactergroupId, $answer, $answer + ); + } + + } + +?> diff --git a/stationtypes/multiplechoice/html/quest.tpl b/stationtypes/multiplechoice/html/quest.tpl new file mode 100644 index 00000000..79872785 --- /dev/null +++ b/stationtypes/multiplechoice/html/quest.tpl @@ -0,0 +1,11 @@ +
+
    + &$answer) : ?> +
  1. + + +
  2. + +
+ +