From faa5a6923e0cd0ade2521d051f0528fe5873df83 Mon Sep 17 00:00:00 2001 From: oliver Date: Fri, 25 Dec 2015 16:26:34 +0100 Subject: [PATCH] add Stationtype ?keyword? --- db/create.sql | 38 +++++++ .../keyword/KeywordStationtypeAgent.inc | 24 +++++ .../keyword/KeywordStationtypeController.inc | 98 +++++++++++++++++++ .../keyword/KeywordStationtypeModel.inc | 74 ++++++++++++++ stationtypes/keyword/html/quest.tpl | 4 + 5 files changed, 238 insertions(+) create mode 100644 stationtypes/keyword/KeywordStationtypeAgent.inc create mode 100644 stationtypes/keyword/KeywordStationtypeController.inc create mode 100644 stationtypes/keyword/KeywordStationtypeModel.inc create mode 100644 stationtypes/keyword/html/quest.tpl diff --git a/db/create.sql b/db/create.sql index c9d20739..2a241fe4 100644 --- a/db/create.sql +++ b/db/create.sql @@ -1933,6 +1933,44 @@ CREATE TABLE `stationtypes` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `stationtypes_keyword` +-- + +DROP TABLE IF EXISTS `stationtypes_keyword`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `stationtypes_keyword` ( + `station_id` int(11) NOT NULL, + `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `created_user_id` int(11) NOT NULL, + `keyword_regex` varchar(256) COLLATE utf8mb4_unicode_ci NOT NULL, + PRIMARY KEY (`station_id`), + KEY `created_user_id` (`created_user_id`), + CONSTRAINT `stationtypes_keyword_ibfk_1` FOREIGN KEY (`station_id`) REFERENCES `charactergroupsqueststations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `stationtypes_keyword_ibfk_2` FOREIGN KEY (`created_user_id`) REFERENCES `users` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `stationtypes_keyword_charactergroups` +-- + +DROP TABLE IF EXISTS `stationtypes_keyword_charactergroups`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `stationtypes_keyword_charactergroups` ( + `station_id` int(11) NOT NULL, + `charactergroup_id` int(11) NOT NULL, + `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `keyword` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL, + PRIMARY KEY (`station_id`,`charactergroup_id`), + KEY `charactergroup_id` (`charactergroup_id`), + CONSTRAINT `stationtypes_keyword_charactergroups_ibfk_1` FOREIGN KEY (`station_id`) REFERENCES `charactergroupsqueststations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `stationtypes_keyword_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/keyword/KeywordStationtypeAgent.inc b/stationtypes/keyword/KeywordStationtypeAgent.inc new file mode 100644 index 00000000..f96c2ea7 --- /dev/null +++ b/stationtypes/keyword/KeywordStationtypeAgent.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 keyword access. + * + * @author Oliver Hanraths + */ + class KeywordStationtypeAgent extends \hhu\z\agents\StationtypeAgent + { + } + +?> diff --git a/stationtypes/keyword/KeywordStationtypeController.inc b/stationtypes/keyword/KeywordStationtypeController.inc new file mode 100644 index 00000000..c2c0736a --- /dev/null +++ b/stationtypes/keyword/KeywordStationtypeController.inc @@ -0,0 +1,98 @@ + + * @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 keyword access. + * + * @author Oliver Hanraths + */ + class KeywordStationtypeController 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) + { + $this->Keyword->setCharactergroupSubmission($station['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) + { + // Get right answers + $task = $this->Keyword->getKeywordTask($station['id']); + + // Match regex with user answers + return $this->isMatching($task['keyword_regex'], $answer); + } + + + /** + * 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) + { + } + + + + + /** + * 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/keyword/KeywordStationtypeModel.inc b/stationtypes/keyword/KeywordStationtypeModel.inc new file mode 100644 index 00000000..5038c80c --- /dev/null +++ b/stationtypes/keyword/KeywordStationtypeModel.inc @@ -0,0 +1,74 @@ + + * @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 keyword access. + * + * @author Oliver Hanraths + */ + class KeywordStationtypeModel extends \hhu\z\models\StationtypeModel + { + + + + + /** + * Get the task of a keyword Station + * + * @param int $stationId ID of Station + * @return array Task data + */ + public function getKeywordTask($stationId) + { + $data = $this->db->query( + 'SELECT keyword_regex '. + 'FROM stationtypes_keyword '. + 'WHERE station_id = ?', + 'i', + $stationId + ); + if(!empty($data)) { + return $data[0]; + } + + + return null; + } + + + /** + * 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($stationId, $charactergroupId, $answer) + { + $this->db->query( + 'INSERT INTO stationtypes_keyword_charactergroups '. + '(station_id, charactergroup_id, keyword) '. + 'VALUES '. + '(?, ?, ?) '. + 'ON DUPLICATE KEY UPDATE '. + 'keyword = ?', + 'iiss', + $stationId, $charactergroupId, $answer, $answer + ); + var_dump("saved"); + } + + } + +?> diff --git a/stationtypes/keyword/html/quest.tpl b/stationtypes/keyword/html/quest.tpl new file mode 100644 index 00000000..20ee3c20 --- /dev/null +++ b/stationtypes/keyword/html/quest.tpl @@ -0,0 +1,4 @@ +
+ + +