From 84a3855e47395351d8fbad92449939dff0f8fa8d Mon Sep 17 00:00:00 2001 From: oliver Date: Sat, 30 Jan 2016 20:15:13 +0100 Subject: [PATCH] implement copying of Stations for Seminary copy feature --- app/models/StationtypeModel.inc | 11 ++++ controllers/SeminariesController.inc | 1 + models/CharactergroupsquestsModel.inc | 13 +++- models/CharactergroupsqueststationsModel.inc | 64 ++++++++++++++++++- models/SeminariesModel.inc | 50 ++++++++------- .../keyword/KeywordStationtypeModel.inc | 24 +++++++ .../MultiplechoiceStationtypeModel.inc | 24 +++++++ views/html/seminaries/copy.tpl | 4 ++ 8 files changed, 166 insertions(+), 25 deletions(-) diff --git a/app/models/StationtypeModel.inc b/app/models/StationtypeModel.inc index 4af53507..2121d9ff 100644 --- a/app/models/StationtypeModel.inc +++ b/app/models/StationtypeModel.inc @@ -23,6 +23,17 @@ + /** + * Copy a Station. + * + * @param int $userId ID of creating user + * @param int $sourceStationId ID of Station to copy from + * @param int $targetStationId ID of Station to copy to + * @param int $seminaryMediaIds Mapping of SeminaryMedia-IDs from source Seminary to targetSeminary + */ + public abstract function copyStation($userId, $sourceStationId, $targetStationId, $seminaryMediaIds); + + /** * Load a Model. * diff --git a/controllers/SeminariesController.inc b/controllers/SeminariesController.inc index 95c41cab..874b2d01 100644 --- a/controllers/SeminariesController.inc +++ b/controllers/SeminariesController.inc @@ -452,6 +452,7 @@ array_key_exists('achievements', $elements), array_key_exists('charactergroupsgroups', $elements), array_key_exists('charactergroupsquests', $elements), + array_key_exists('charactergroupsqueststations', $elements), array_key_exists('map', $elements) ); $seminary = $this->Seminaries->getSeminaryById($seminaryId); diff --git a/models/CharactergroupsquestsModel.inc b/models/CharactergroupsquestsModel.inc index 7b21674f..69393322 100644 --- a/models/CharactergroupsquestsModel.inc +++ b/models/CharactergroupsquestsModel.inc @@ -384,9 +384,11 @@ * @param array $groupsgroupIds Mapping of Character groups-group-IDs from source Seminary to target Seminary * @param array $questgroupIds Mapping of Questgroup-IDs from source Seminary to target Seminary * @param array $seminaryMediaIds Mapping of Seminarymedia-IDs from source Seminary to target Seminary (optional) + * @return array Mapping of Quest-IDs from source Seminary to target Seminary */ public function copyQuestsOfSeminary($userId, $groupsgroupIds, $questgroupIds, $seminaryMediaIds=null) { + $questIds = array(); foreach($groupsgroupIds as $sourceGroupsgroupId => $targetGroupsgroupId) { // Get Quests @@ -406,7 +408,7 @@ $userId, $targetGroupsgroupId, $questgroupIds[$quest['questgroups_id']], $quest['id'] ); - $targetQuestId = $this->db->getInsertId(); + $questIds[$quest['id']] = $this->db->getInsertId(); // Copy media if(!is_null($seminaryMediaIds) && !is_null($quest['questsmedia_id'])) @@ -418,11 +420,18 @@ 'WHERE id = ?', 'ii', $seminaryMediaIds[$quest['questsmedia_id']], - $targetQuestId + $questIds[$quest['id']] ); } + + // TODO Copy Stations + //$this->Charactergroupsqueststations->copyStationsOfQuest($quest['id'], $targetQuestId, $seminaryMediaIds); } } + + + // Return target Quest-IDs + return $questIds; } diff --git a/models/CharactergroupsqueststationsModel.inc b/models/CharactergroupsqueststationsModel.inc index 7aee5f95..2ed65ffc 100644 --- a/models/CharactergroupsqueststationsModel.inc +++ b/models/CharactergroupsqueststationsModel.inc @@ -39,6 +39,13 @@ */ const STATUS_SOLVED = 3; + /** + * Required models + * + * @var array + */ + public $models = array('stationtypes'); + @@ -103,6 +110,7 @@ return $data[0]; } + /** * Get all Stations for a Character groups Quest. * @@ -112,7 +120,7 @@ public function getStationsForQuest($questId) { return $this->db->query( - 'SELECT id, title, url, stationpicture_id, latitude, longitude '. + 'SELECT id, stationtype_id, title, url, stationpicture_id, latitude, longitude, rightimage_id, rightav_id, wrongimage_id, wrongav_id '. 'FROM charactergroupsqueststations '. 'WHERE charactergroupsquest_id = ? '. 'ORDER BY pos ASC', @@ -148,6 +156,60 @@ } + /** + * Copy all Character groups Quest Stations of a Seminary. + * + * @param int $userId ID of creating user + * @param array $charactergroupsquestIds Mapping of Character groups Quest-IDs from source Seminary to target Seminary + * @param array $seminaryMediaIds Mapping of Seminary-media-IDs from source Seminary to target Seminary (optional) + */ + public function copyStationsOfSeminary($userId, $charactergroupsquestIds, $seminaryMediaIds) + { + // Go through each Quest + foreach($charactergroupsquestIds as $sourceQuestId => $targetQuestId) + { + // Get Stations + $stations = $this->getStationsForQuest($sourceQuestId); + + // Copy each station + foreach($stations as &$station) + { + // Copy Station + $this->db->query( + 'INSERT INTO charactergroupsqueststations '. + '(charactergroupsquest_id, stationtype_id, title, url, pos, stationpicture_id, prolog, task, latitude, longitude, righttext, wrongtext, rightimage_id, rightav_id, wrongimage_id, wrongav_id) '. + 'SELECT ?, stationtype_id, title, url, pos, ?, prolog, task, latitude, longitude, righttext, wrongtext, ?, ?, ?, ? '. + 'FROM charactergroupsqueststations '. + 'WHERE id = ?', + 'iiiiiii', + $targetQuestId, + (!is_null($station['stationpicture_id'])) ? $seminaryMediaIds[$station['stationpicture_id']] : null, + (!is_null($station['rightimage_id'])) ? $seminaryMediaIds[$station['rightimage_id']] : null, + (!is_null($station['rightav_id'])) ? $seminaryMediaIds[$station['rightav_id']] : null, + (!is_null($station['wrongimage_id'])) ? $seminaryMediaIds[$station['wrongimage_id']] : null, + (!is_null($station['wrongav_id'])) ? $seminaryMediaIds[$station['wrongav_id']] : null, + $station['id'] + ); + $targetStationId = $this->db->getInsertId(); + + // Copy content + $stationtype = $this->Stationtypes->getStationtypeById($station['stationtype_id']); + if(!is_null($stationtype['classname'])) + { + // Load Stationtype Model + \hhu\z\models\StationtypeModel::load($stationtype['classname']); + + // Construct Station Model + $stationtypeModel = \hhu\z\models\StationtypeModel::factory($stationtype['classname']); + + // Copy content + $stationtypeModel->copyStation($userId, $station['id'], $targetStationId, $seminaryMediaIds); + } + } + } + } + + /** * Mark a Station as entered for a Character group. * diff --git a/models/SeminariesModel.inc b/models/SeminariesModel.inc index 20984de6..ec5ee101 100644 --- a/models/SeminariesModel.inc +++ b/models/SeminariesModel.inc @@ -24,7 +24,7 @@ * * @var array */ - public $models = array('questgroupshierarchy', 'questgroups', 'quests', 'questtopics', 'media', 'characters', 'charactertypes', 'xplevels', 'avatars', 'achievements', 'charactergroups', 'charactergroupsquests', 'seminarycharacterfields', 'map', 'uploads'); + public $models = array('questgroupshierarchy', 'questgroups', 'quests', 'questtopics', 'media', 'characters', 'charactertypes', 'xplevels', 'avatars', 'achievements', 'charactergroups', 'charactergroupsquests', 'charactergroupsqueststations', 'seminarycharacterfields', 'map', 'uploads'); @@ -328,27 +328,28 @@ /** * Copy a Seminary and its content. * - * @param int $userId ID of copying user - * @param int $sourceSeminaryId ID of Seminary to copy - * @param string $title Title of new Seminary - * @param string $course Course of now Seminary - * @param string $description Description of new Seminary - * @param boolean $copySeminaryfields Whether to copy Seminary Character fields of not - * @param boolean $copyMedia Whether to copy media or not - * @param boolean $copyQuestgroupshierarchy Whether to copy Questgroupshierarchy or not - * @param boolean $copyQuestgroups Whether to copy Questgroups or not - * @param boolean $copyQuests Whether to copy Quests or not - * @param boolean $copyQuesttopics Whether to copy Quest topics or not - * @param boolean $copyCharactertypes Whether to copy Charactertypes or not - * @param boolean $copyXPlevels Whether to copy XP-levels or not - * @param boolean $copyAvatars Whether to copy Avatars or not - * @param boolean $copyAchievements Whether to copy Achievements or not - * @param boolean $copyCharactergroupsgroups Whether to copy Character groups-groups or not - * @param boolean $copyCharactergroupsquests Whether to copy Character groups Quests or not - * @param boolean $copyMap Whether to copy Map or not - * @return ID of newly created Seminary + * @param int $userId ID of copying user + * @param int $sourceSeminaryId ID of Seminary to copy + * @param string $title Title of new Seminary + * @param string $course Course of now Seminary + * @param string $description Description of new Seminary + * @param boolean $copySeminaryfields Whether to copy Seminary Character fields of not + * @param boolean $copyMedia Whether to copy media or not + * @param boolean $copyQuestgroupshierarchy Whether to copy Questgroupshierarchy or not + * @param boolean $copyQuestgroups Whether to copy Questgroups or not + * @param boolean $copyQuests Whether to copy Quests or not + * @param boolean $copyQuesttopics Whether to copy Quest topics or not + * @param boolean $copyCharactertypes Whether to copy Charactertypes or not + * @param boolean $copyXPlevels Whether to copy XP-levels or not + * @param boolean $copyAvatars Whether to copy Avatars or not + * @param boolean $copyAchievements Whether to copy Achievements or not + * @param boolean $copyCharactergroupsgroups Whether to copy Character groups-groups or not + * @param boolean $copyCharactergroupsquests Whether to copy Character groups Quests or not + * @param boolean $copyCharactergroupsqueststations Whether to copy Character groups Quest Stations or not + * @param boolean $copyMap Whether to copy Map or not + * @return ID of newly created Seminary */ - public function copySeminary($userId, $sourceSeminaryId, $title, $course, $description, $copySeminaryfields, $copyMedia, $copyQuestgroupshierarchy, $copyQuestgroups, $copyQuests, $copyQuesttopics, $copyCharactertypes, $copyXPlevels, $copyAvatars, $copyAchievements, $copyCharactergroupsgroups, $copyCharactergroupsquests, $copyMap) + public function copySeminary($userId, $sourceSeminaryId, $title, $course, $description, $copySeminaryfields, $copyMedia, $copyQuestgroupshierarchy, $copyQuestgroups, $copyQuests, $copyQuesttopics, $copyCharactertypes, $copyXPlevels, $copyAvatars, $copyAchievements, $copyCharactergroupsgroups, $copyCharactergroupsquests, $copyCharactergroupsqueststations, $copyMap) { // Get Seminary $seminary = $this->getSeminaryById($sourceSeminaryId); @@ -437,12 +438,17 @@ // Copy Charactergroups content // Charactergroupsgroups + $characterGroupsgroupIds = null; + $charactergroupsquestIds = null; if($copyCharactergroupsgroups) { $characterGroupsgroupIds = $this->Charactergroups->copyGroupsgroupsOfSeminary($userId, $sourceSeminaryId, $targetSeminaryId); // Copy Charactergroupsquests if($copyCharactergroupsquests &!is_null($questgroupIds)) { - $this->Charactergroupsquests->copyQuestsOfSeminary($userId, $characterGroupsgroupIds, $questgroupIds, $seminaryMediaIds); + $charactergroupsquestIds = $this->Charactergroupsquests->copyQuestsOfSeminary($userId, $characterGroupsgroupIds, $questgroupIds, $seminaryMediaIds); + if($copyCharactergroupsqueststations && !is_null($charactergroupsquestIds)) { + $this->Charactergroupsqueststations->copyStationsOfSeminary($userId, $charactergroupsquestIds, $seminaryMediaIds); + } } } diff --git a/stationtypes/keyword/KeywordStationtypeModel.inc b/stationtypes/keyword/KeywordStationtypeModel.inc index 8ba39749..815b727e 100644 --- a/stationtypes/keyword/KeywordStationtypeModel.inc +++ b/stationtypes/keyword/KeywordStationtypeModel.inc @@ -23,6 +23,30 @@ + /** + * Copy a Station. + * + * @param int $userId ID of creating user + * @param int $sourceStationId ID of Station to copy from + * @param int $targetStationId ID of Station to copy to + * @param int $seminaryMediaIds Mapping of SeminaryMedia-IDs from source Seminary to targetSeminary + */ + public function copyStation($userId, $sourceStationId, $targetStationId, $seminaryMediaIds) + { + // Copy keyword + $this->db->query( + 'INSERT INTO stationtypes_keyword '. + '(station_id, created_user_id, keyword_regex) '. + 'SELECT ?, ?, keyword_regex '. + 'FROM stationtypes_keyword '. + 'WHERE station_id = ?', + 'iii', + $targetStationId, $userId, + $sourceStationId + ); + } + + /** * Get the task of a keyword Station * diff --git a/stationtypes/multiplechoice/MultiplechoiceStationtypeModel.inc b/stationtypes/multiplechoice/MultiplechoiceStationtypeModel.inc index 312f0070..6756bc05 100644 --- a/stationtypes/multiplechoice/MultiplechoiceStationtypeModel.inc +++ b/stationtypes/multiplechoice/MultiplechoiceStationtypeModel.inc @@ -23,6 +23,30 @@ + /** + * Copy a Station. + * + * @param int $userId ID of creating user + * @param int $sourceStationId ID of Station to copy from + * @param int $targetStationId ID of Station to copy to + * @param int $seminaryMediaIds Mapping of SeminaryMedia-IDs from source Seminary to targetSeminary + */ + public function copyStation($userId, $sourceStationId, $targetStationId, $seminaryMediaIds) + { + // Copy answers + $this->db->query( + 'INSERT INTO stationtypes_multiplechoice '. + '(created_user_id, station_id, pos, answer, tick) '. + 'SELECT ?, ?, pos, answer, tick '. + 'FROM stationtypes_multiplechoice '. + 'WHERE station_id = ?', + 'iii', + $userId, $targetStationId, + $sourceStationId + ); + } + + /** * Get all answers for a Station * diff --git a/views/html/seminaries/copy.tpl b/views/html/seminaries/copy.tpl index ea627ff8..74bfc50c 100644 --- a/views/html/seminaries/copy.tpl +++ b/views/html/seminaries/copy.tpl @@ -106,6 +106,10 @@
checked="checked" /> +
+ checked="checked" /> + +
checked="checked" />