diff --git a/controllers/QuestgroupsController.inc b/controllers/QuestgroupsController.inc index ca06cbd1..687887a6 100644 --- a/controllers/QuestgroupsController.inc +++ b/controllers/QuestgroupsController.inc @@ -81,6 +81,9 @@ } } + // Set status “entered” + $this->Questgroups->setQuestgroupEntered($questgroup['id'], $character['id']); + // Get child Questgroupshierarchy $childQuestgroupshierarchy = null; if(!empty($questgroup['hierarchy'])) @@ -160,10 +163,8 @@ $relatedQuestgroups = $this->Questgroups->getRelatedQuestsgroupsOfQuest($currentQuest['id']); foreach($relatedQuestgroups as &$relatedQuestgroup) { - $relatedQuestgroup = $this->Questgroups->getQuestgroupById($relatedQuestgroup['id']); - $firstQuest = $this->Quests->getFirstQuestOfQuestgroup($relatedQuestgroup['id']); - if(!empty($firstQuest) && $this->Quests->hasCharacterEnteredQuest($firstQuest['id'], $character['id'])) { - $currentQuest['relatedQuestgroups'][] = $relatedQuestgroup; + if($this->Questgroups->hasCharacterEnteredQuestgroup($relatedQuestgroup['id'], $character['id'])) { + $currentQuest['relatedQuestgroups'][] = $this->Questgroups->getQuestgroupById($relatedQuestgroup['id']); } } diff --git a/models/QuestgroupsModel.inc b/models/QuestgroupsModel.inc index f3cd0949..1534251d 100644 --- a/models/QuestgroupsModel.inc +++ b/models/QuestgroupsModel.inc @@ -19,6 +19,13 @@ */ class QuestgroupsModel extends \hhu\z\Model { + /** + * Questgroup-status: Entered + * + * @var int; + */ + const QUESTGROUP_STATUS_ENTERED = 0; + /** * Required models * @@ -254,6 +261,42 @@ } + /** + * Mark a Questgroup as entered for a Character. + * + * @param int $questId ID of Quest to mark as entered + * @param int $characterId ID of Character that entered the Quest + */ + public function setQuestgroupEntered($questgroupId, $characterId) + { + $this->setQuestgroupStatus($questgroupId, $characterId, self::QUESTGROUP_STATUS_ENTERED, false); + } + + + /** + * Determine if the given Character has entered a Questgroup. + * + * @param int $questgroupId ID of Questgroup to check + * @param int $characterId ID of Character to check + * @result boolean Whether Character has entered the Questgroup or not + */ + public function hasCharacterEnteredQuestgroup($questgroupId, $characterId) + { + $count = $this->db->query( + 'SELECT count(id) AS c '. + 'FROM questgroups_characters '. + 'WHERE questgroup_id = ? AND character_id = ? AND status IN (?)', + 'iii', + $questgroupId, + $characterId, + self::QUESTGROUP_STATUS_ENTERED + ); + + + return (!empty($count) && intval($count[0]['c']) > 0); + } + + /** * Determine if the given Character has solved the Quests form * this Questgroup. @@ -657,6 +700,47 @@ return $data[0]; } + + /** + * Mark a Questgroup for a Character. + * + * @param int $questgroupId ID of Questgroup to mark + * @param int $characterId ID of Character to mark the Questgroup for + * @param int $status Questgroup status to mark + * @param boolean $repeated Insert although status is already set for this Questgroup and Character + */ + private function setQuestgroupStatus($questgroupId, $characterId, $status, $repeated=true) + { + // Check if status is already set + if(!$repeated) + { + $count = $this->db->query( + 'SELECT count(*) AS c '. + 'FROM questgroups_characters '. + 'WHERE questgroup_id = ? AND character_id = ? AND status = ?', + 'iii', + $questgroupId, + $characterId, + $status + ); + if(!empty($count) && intval($count[0]['c']) > 0) { + return; + } + } + + // Set status + $this->db->query( + 'INSERT INTO questgroups_characters '. + '(questgroup_id, character_id, status) '. + 'VALUES '. + '(?, ?, ?) ', + 'iii', + $questgroupId, + $characterId, + $status + ); + } + } ?>