179 lines
5.3 KiB
PHP
179 lines
5.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* The Legend of Z
|
|
*
|
|
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
|
* @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\controllers;
|
|
|
|
|
|
/**
|
|
* Controller of the QuestgroupsAgent to display Questgroups.
|
|
*
|
|
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
|
*/
|
|
class QuestgroupsController extends \hhu\z\controllers\SeminaryRoleController
|
|
{
|
|
/**
|
|
* Required models
|
|
*
|
|
* @var array
|
|
*/
|
|
public $models = array('seminaries', 'questgroupshierarchy', 'questgroups', 'quests', 'questtexts', 'media');
|
|
/**
|
|
* User permissions
|
|
*
|
|
* @var array
|
|
*/
|
|
public $permissions = array(
|
|
'questgroup' => array('admin', 'moderator', 'user')
|
|
);
|
|
/**
|
|
* User seminary permissions
|
|
*
|
|
* @var array
|
|
*/
|
|
public $seminaryPermissions = array(
|
|
'questgroup' => array('admin', 'moderator', 'user')
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
* Action: questgroup.
|
|
*
|
|
* Display a Questgroup and its data.
|
|
*
|
|
* @throws IdNotFoundException
|
|
* @param string $seminaryUrl URL-Title of a Seminary
|
|
* @param string $questgroupUrl URL-Title of a Questgroup
|
|
*/
|
|
public function questgroup($seminaryUrl, $questgroupUrl)
|
|
{
|
|
// Get Seminary
|
|
$seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl);
|
|
|
|
// Get Questgroup
|
|
$questgroup = $this->Questgroups->getQuestgroupByUrl($seminary['id'], $questgroupUrl);
|
|
|
|
// Get Questgrouphierarchy
|
|
$questgroup['hierarchy'] = $this->Questgroupshierarchy->getHierarchyForQuestgroup($questgroup['id']);
|
|
|
|
// Get Character
|
|
$character = $this->Characters->getCharacterForUserAndSeminary($this->Auth->getUserId(), $seminary['id']);
|
|
|
|
// Check permission
|
|
$previousQuestgroup = $this->Questgroups->getPreviousQuestgroup($questgroup['id']);
|
|
if(!is_null($previousQuestgroup)) {
|
|
if(!$this->Questgroups->hasCharacterSolvedQuestgroup($previousQuestgroup['id'], $character['id'])) {
|
|
throw new \nre\exceptions\AccessDeniedException();
|
|
}
|
|
}
|
|
|
|
// Get child Questgroupshierarchy
|
|
$childQuestgroupshierarchy = null;
|
|
if(!empty($questgroup['hierarchy']))
|
|
{
|
|
$childQuestgroupshierarchy = $this->Questgroupshierarchy->getChildQuestgroupshierarchy($questgroup['hierarchy']['id']);
|
|
foreach($childQuestgroupshierarchy as &$hierarchy)
|
|
{
|
|
// Get Questgroups
|
|
$hierarchy['questgroups'] = $this->Questgroups->getQuestgroupsForHierarchy($hierarchy['id'], $questgroup['id']);
|
|
|
|
// Get additional data
|
|
foreach($hierarchy['questgroups'] as $i => &$group)
|
|
{
|
|
// Check permission of Questgroups
|
|
if($i >= 1)
|
|
{
|
|
if(!$this->Questgroups->hasCharacterSolvedQuestgroup($hierarchy['questgroups'][$i-1]['id'], $character['id']))
|
|
{
|
|
$hierarchy['questgroups'] = array_slice($hierarchy['questgroups'], 0, $i);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Get Character XPs
|
|
$group['character_xps'] = $this->Questgroups->getAchievedXPsForQuestgroup($group['id'], $character['id']);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Get texts
|
|
$questgroupTexts = $this->Questgroups->getQuestgroupTexts($questgroup['id']);
|
|
|
|
// Get Character XPs
|
|
$questgroup['character_xps'] = $this->Questgroups->getAchievedXPsForQuestgroup($questgroup['id'], $character['id']);
|
|
|
|
// Media
|
|
$picture = null;
|
|
if(!is_null($questgroup['questgroupspicture_id']))
|
|
{
|
|
$picture = $this->Media->getMediaById($questgroup['questgroupspicture_id']);
|
|
}
|
|
|
|
|
|
// Get Quests
|
|
$quests = array();
|
|
if(count($childQuestgroupshierarchy) == 0)
|
|
{
|
|
$currentQuest = null;
|
|
do {
|
|
// Get next Quest
|
|
if(is_null($currentQuest)) {
|
|
$currentQuest = $this->Quests->getFirstQuestOfQuestgroup($questgroup['id']);
|
|
}
|
|
else {
|
|
$nextQuests = $this->Quests->getNextQuests($currentQuest['id']);
|
|
$currentQuest = null;
|
|
foreach($nextQuests as &$nextQuest) {
|
|
if($this->Quests->hasCharacterEnteredQuest($nextQuest['id'], $character['id'])) {
|
|
$currentQuest = $nextQuest;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Add additional data
|
|
if(!is_null($currentQuest))
|
|
{
|
|
// Set status
|
|
$currentQuest['solved'] = $this->Quests->hasCharacterSolvedQuest($currentQuest['id'], $character['id']);
|
|
|
|
// Attach related Questgroups
|
|
$currentQuest['relatedQuestgroups'] = array();
|
|
$relatedQuestgroups = $this->Questgroups->getRelatedQuestsgroupsOfQuest($currentQuest['id']);
|
|
foreach($relatedQuestgroups as &$relatedQuestgroup)
|
|
{
|
|
$firstQuest = $this->Quests->getFirstQuestOfQuestgroup($relatedQuestgroup['id']);
|
|
if(!empty($firstQuest) && $this->Quests->hasCharacterEnteredQuest($firstQuest['id'], $character['id'])) {
|
|
$currentQuest['relatedQuestgroups'][] = $relatedQuestgroup;
|
|
}
|
|
}
|
|
|
|
// Add Quest to Quests
|
|
$quests[] = $currentQuest;
|
|
}
|
|
}
|
|
while(!is_null($currentQuest) && $currentQuest['solved']);
|
|
}
|
|
|
|
|
|
// Pass data to view
|
|
$this->set('seminary', $seminary);
|
|
$this->set('questgroup', $questgroup);
|
|
$this->set('childquestgroupshierarchy', $childQuestgroupshierarchy);
|
|
$this->set('texts', $questgroupTexts);
|
|
$this->set('picture', $picture);
|
|
$this->set('quests', $quests);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|