From 1d04ee050872cc4cde4e15606159cf271e5b0232 Mon Sep 17 00:00:00 2001 From: coderkun Date: Sun, 27 Apr 2014 03:50:25 +0200 Subject: [PATCH] search for the first text of a Questgroup recursively (Issue #77) --- controllers/SeminariesController.inc | 5 ++- models/QuestgroupsModel.inc | 46 ++++++++++++++++++++-------- models/QuesttextsModel.inc | 18 +++++++++++ 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/controllers/SeminariesController.inc b/controllers/SeminariesController.inc index 345d4085..f9cb6f90 100644 --- a/controllers/SeminariesController.inc +++ b/controllers/SeminariesController.inc @@ -126,10 +126,9 @@ // Get first Questgroup text $text = $this->Questgroups->getFirstQuestgroupText($questgroup['id']); - if(!empty($text)) + if(!is_null($text)) { - $text = \hhu\z\Utils::shortenString($text['text'], 100, 120).' …'; - $questgroup['text'] = $text; + $questgroup['text'] = \hhu\z\Utils::shortenString($text, 100, 120).' …'; } // Get cumulated data diff --git a/models/QuestgroupsModel.inc b/models/QuestgroupsModel.inc index 21ee1452..d86a2fd8 100644 --- a/models/QuestgroupsModel.inc +++ b/models/QuestgroupsModel.inc @@ -179,27 +179,47 @@ /** - * Get first texts of a Questgroup. + * Get the first text of a Questgroup. * * @param int $questgroupId ID of a Questgroup - * @return array First Text of this Questgroup + * @return string First text of this Questgroup or NULL */ public function getFirstQuestgroupText($questgroupId) { - $data = $this->db->query( - 'SELECT id, pos, text '. - 'FROM questgrouptexts '. - 'WHERE questgroup_id = ? '. - 'ORDER BY pos ASC '. - 'LIMIT 1', - 'i', - $questgroupId - ); - if(!empty($data)) { - return $data[0]; + // Text of Questgroup itself + $questgroupTexts = $this->getQuestgroupTexts($questgroupId); + if(!empty($questgroupTexts)) { + return $questgroupTexts[0]['text']; + } + + // Text of first Quest + $quest = $this->Quests->getFirstQuestOfQuestgroup($questgroupId); + if(!is_null($quest)) + { + $questText = $this->Questtexts->getFirstQuestText($quest['id']); + if(!is_null($questText)) { + return $questText; + } + } + + // Text of ChildQuestgroups + $questgroupHierarchy = $this->Questgroupshierarchy->getHierarchyForQuestgroup($questgroupId); + $childQuestgroupshierarchy = $this->Questgroupshierarchy->getChildQuestgroupshierarchy($questgroupHierarchy['id']); + foreach($childQuestgroupshierarchy as &$hierarchy) + { + // Get Questgroups + $questgroups = $this->getQuestgroupsForHierarchy($hierarchy['id'], $questgroupId); + foreach($questgroups as &$group) + { + $childQuestgroupText = $this->getFirstQuestgroupText($group['id']); + if(!is_null($childQuestgroupText)) { + return $childQuestgroupText; + } + } } + // No text found return null; } diff --git a/models/QuesttextsModel.inc b/models/QuesttextsModel.inc index 7aca28b9..0cd931f1 100644 --- a/models/QuesttextsModel.inc +++ b/models/QuesttextsModel.inc @@ -34,6 +34,24 @@ + /** + * Get the first text of a Quest. + * + * @param int $questId ID of a Quest + * @return string First text of this Quest or NULL + */ + public function getFirstQuestText($questId) + { + $prolog = $this->getQuesttextsOfQuest($questId, 'Prolog'); + if(!empty($prolog)) { + return $prolog[0]['text']; + } + + + return null; + } + + /** * Get all Questtexts for a Quest. *