add CharactergroupsqueststationsAgent

This commit is contained in:
oliver 2015-12-25 16:39:26 +01:00
commit f779b22574
7 changed files with 1019 additions and 0 deletions

View file

@ -0,0 +1,383 @@
<?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 CharactergroupsqueststationAgent to display Character
* groups Quest stations.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class CharactergroupsqueststationsController extends \hhu\z\controllers\SeminaryController
{
/**
* Required models
*
* @var array
*/
public $models = array('seminaries', 'charactergroups', 'charactergroupsquests', 'charactergroupsqueststations', 'stationtypes', 'media');
/**
* User permissions
*
* @var array
*/
public $permissions = array(
'index' => array('admin', 'moderator', 'user'),
'station' => array('admin', 'moderator', 'user')
);
/**
* User seminary permissions
*
* @var array
*/
public $seminaryPermissions = array(
'index' => array('admin', 'moderator', 'user'),
'station' => array('admin', 'moderator', 'user')
);
/**
* Action: index.
*
* Show all stations of a Character groups Quest.
*
* @throws \nre\exceptions\IdNotFoundException
* @param string $seminaryUrl URL-Title of a Seminary
* @param string $groupsgroupUrl URL-Title of a Character groups-group
* @param string $questUrl URL-Title of a Character groups Quest
*/
public function index($seminaryUrl, $groupsgroupUrl, $questUrl)
{
// Get seminary
$seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl);
// Get Character groups-group
$groupsgroup = $this->Charactergroups->getGroupsgroupByUrl($seminary['id'], $groupsgroupUrl);
// Get Character groups-group Quests
$quest = $this->Charactergroupsquests->getQuestByUrl($groupsgroup['id'], $questUrl);
// Get Character group
$charactergroup = null;
$character = $this->Characters->getCharacterForUserAndSeminary($this->Auth->getUserId(), $seminary['id']);
$charactergroups = $this->Charactergroups->getGroupsForCharacter($character['id']);
if(!empty($charactergroups)) {
// TODO Multiple Character groups
$charactergroup = $charactergroups[0];
}
// Get Stations
$stations = null;
if(count(array_intersect(array('admin', 'moderator'), \hhu\z\controllers\SeminaryController::$character['characterroles'])) > 0) {
$stations = $this->Charactergroupsqueststations->getStationsForQuest($quest['id']);
}
elseif(!is_null($charactergroup)) {
$stations = $this->Charactergroupsqueststations->getStationsForQuestAndGroup($quest['id'], $charactergroup['id']);
}
// Pass data to view
$this->set('stations', $stations);
$this->set('hasgroup', !is_null($charactergroup));
}
/**
* Action: station.
*
* Show a station of a Character groups Quest.
*
* @throws \nre\exceptions\IdNotFoundException
* @param string $seminaryUrl URL-Title of a Seminary
* @param string $groupsgroupUrl URL-Title of a Character groups-group
* @param string $questUrl URL-Title of a Character groups Quest
* @param string $stationUrl URL of station
*/
public function station($seminaryUrl, $groupsgroupUrl, $questUrl, $stationUrl)
{
// Get seminary
$seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl);
// Get Character groups-group
$groupsgroup = $this->Charactergroups->getGroupsgroupByUrl($seminary['id'], $groupsgroupUrl);
// Get Character groups-group Quests
$quest = $this->Charactergroupsquests->getQuestByUrl($groupsgroup['id'], $questUrl);
// Get Station
$station = $this->Charactergroupsqueststations->getStationByUrl($quest['id'], $stationUrl);
if(!is_null($station['stationpicture_id'])) {
$station['picture'] = $this->Media->getSeminaryMediaById($station['stationpicture_id']);
}
// Get Character group
$charactergroup = null;
$character = $this->Characters->getCharacterForUserAndSeminary($this->Auth->getUserId(), $seminary['id']);
$charactergroups = $this->Charactergroups->getGroupsForCharacter($character['id']);
if(!empty($charactergroups)) {
// TODO Multiple Character groups
$charactergroup = $charactergroups[0];
}
// TODO Check permissions
// Set status “entered”
if(!is_null($charactergroup)) {
$this->Charactergroupsqueststations->setStationEntered($station['id'], $charactergroup['id']);
}
// Get Character groups-groups
$groups = null;
if(count(array_intersect(array('admin', 'moderator'), \hhu\z\controllers\SeminaryController::$character['characterroles'])) > 0)
{
$groups = $this->Charactergroups->getGroupsForQueststation($station['id']);
foreach($groups as &$group) {
$group['solved'] = $this->Charactergroupsqueststations->hasCharactergroupSolvedStation(
$station['id'],
$group['id']
);
}
}
// Task
$task = null;
$stationtype = $this->Stationtypes->getStationtypeById($station['stationtype_id']);
if(!is_null($stationtype['classname'])) {
$task = $this->renderTask($stationtype['classname'], $seminary, $groupsgroup, $quest, $station, $charactergroup);
}
else
{
// Mark Station as solved
if(!is_null($charactergroup)) {
$this->Charactergroupsqueststations->setStationSolved($station['id'], $charactergroup['id']);
}
}
// Status
$solved = false;
$tried = false;
if(!is_null($charactergroup)) {
$solved = $this->Charactergroupsqueststations->hasCharactergroupSolvedStation($station['id'], $charactergroup['id']);
if(!$solved) {
$tried = $this->Charactergroupsqueststations->hasCharactergroupTriedStation($station['id'], $charactergroup['id']);
}
}
// Set title
$this->addTitle($station['title']);
$this->addTitle($quest['title']);
$this->addTitle($groupsgroup['name']);
$this->addTitle($seminary['title']);
// Pass data to view
$this->set('seminary', $seminary);
$this->set('groupsgroup', $groupsgroup);
$this->set('quest', $quest);
$this->set('station', $station);
$this->set('task', $task);
$this->set('groups', $groups);
$this->set('solved', $solved);
$this->set('tried', $tried);
}
/**
* Render and handle the task of a Station.
*
* @param string $stationtypeClassname Name of the class for the Stationtype of a Station
* @param array $seminary Seminary data
* @param array $questgroup Questgroup data
* @param array $quest Quest data
* @param array $station Station data
* @param array $charactergroup Charactergroup data
* @return string Rendered output
*/
private function renderTask($stationtypeClassname, $seminary, $groupsgroup, $quest, $station, $charactergroup)
{
$task = null;
try {
// Generate request and response
$request = clone $this->request;
$response = $this->createStationtypeResponse('quest', $seminary, $groupsgroup, $quest, $station, $charactergroup);
// Load Stationtype Agent
$stationtypeAgent = $this->loadStationtypeAgent($stationtypeClassname, $request, $response);
// Solve Quest
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('submit')))
{
// Get user answers
$answer = $this->request->getPostParam('answer');
// Save answers in database
try {
// save answer
if(!is_null($charactergroup)) {
if(!$this->Charactergroupsqueststations->hasCharactergroupSolvedStation($station['id'], $charactergroup['id'])) {
$stationtypeAgent->saveAnswer($seminary, $groupsgroup, $quest, $station, $charactergroup, $answer);
}
}
// Match answer with correct one
$status = $stationtypeAgent->matchAnswer(
$seminary,
$groupsgroup,
$quest,
$station,
$charactergroup,
$answer
);
if($status === true)
{
if(!is_null($charactergroup))
{
// Mark Station as solved
$this->Charactergroupsqueststations->setStationSolved($station['id'], $charactergroup['id']);
}
// Redirect
$this->redirect($this->linker->link(array(), 6, true, null, false, 'task'));
}
elseif($status === false)
{
if(!is_null($charactergroup)) {
// Mark Station as unsolved
$this->Charactergroupsqueststations->setStationUnsolved(
$station['id'],
$charactergroup['id']
);
}
// Redirect
$this->redirect($this->linker->link(array(), 6, true, null, false, 'task'));
}
else {
// Redirect
$this->redirect($this->linker->link(array(), 6, true, null, false, 'task'));
}
}
catch(\hhu\z\exceptions\SubmissionNotValidException $e) {
$response->addParam($e);
}
}
// Render Task
$task = $this->runStationtypeAgent($stationtypeAgent, $request, $response);
}
catch(\nre\exceptions\ViewNotFoundException $e) {
$task = $e->getMessage();
}
catch(\nre\exceptions\ActionNotFoundException $e) {
$task = $e->getMessage();
}
catch(\hhu\z\exceptions\StationtypeModelNotValidException $e) {
$task = $e->getMessage();
}
catch(\hhu\z\exceptions\StationtypeModelNotFoundException $e) {
$task = $e->getMessage();
}
catch(\hhu\z\exceptions\StationtypeControllerNotValidException $e) {
$task = $e->getMessage();
}
catch(\hhu\z\exceptions\StationtypeControllerNotFoundException $e) {
$task = $e->getMessage();
}
catch(\hhu\z\exceptions\StationtypeAgentNotValidException $e) {
$task = $e->getMessage();
}
catch(\hhu\z\exceptions\stationtypeAgentNotFoundException $e) {
$task = $e->getMessage();
}
// Return rendered output
return $task;
}
/**
* Create a response for the Stationtype rendering.
*
* @param string $action Action to run
* @param mixed $param Additional parameters to add to the response
* @return \nre\core\Response Generated response
*/
private function createStationtypeResponse($action, $param1)
{
// Clone current response
$response = clone $this->response;
// Clear parameters
$response->clearParams(1);
// Add Action
$response->addParams(
null,
$action
);
// Add additional parameters
foreach(array_slice(func_get_args(), 1) as $param) {
$response->addParam($param);
}
// Return response
return $response;
}
/**
* Load and construct the StationtypeAgent for a Stationtype.
*
* @param string $stationtypeClassname Name of the class for the Stationtype of a Station
* @param \nre\core\Request $request Request
* @param \nre\core\Response $response Response
* @return \hhu\z\agents\StationtypeAgent Stationtype Agent
*/
private function loadStationtypeAgent($stationtypeClassname, $request, $response)
{
// Load Agent
\hhu\z\agents\StationtypeAgent::load($stationtypeClassname);
// Construct and return Agent
return \hhu\z\agents\StationtypeAgent::factory($stationtypeClassname, $request, $response);
}
/**
* Run and render the Agent for a StationtypeAgent and return ist output.
*
* @param \nre\core\Agent $stationtypeAgent StationtypeAgent to run and render
* @param \nre\core\Request $request Request
* @param \nre\core\Response $response Response
* @return string Rendered output
*/
private function runStationtypeAgent($stationtypeAgent, $request, $response)
{
// Run Agent
$stationtypeAgent->run($request, $response);
// Render and return output
return $stationtypeAgent->render();
}
}
?>