questlab/controllers/CharactergroupsqueststationsController.inc
2016-05-28 12:30:53 +02:00

1234 lines
52 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 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');
/**
* Required components
*
* @var array
*/
public $components = array('validation');
/**
* User permissions
*
* @var array
*/
public $permissions = array(
'index' => array('admin', 'moderator', 'user'),
'station' => array('admin', 'moderator', 'user'),
'create' => array('admin', 'moderator', 'user'),
'edit' => array('admin', 'moderator', 'user'),
'edittask' => array('admin', 'moderator', 'user'),
'delete' => array('admin', 'moderator', 'user')
);
/**
* User seminary permissions
*
* @var array
*/
public $seminaryPermissions = array(
'index' => array('admin', 'moderator', 'user'),
'station' => array('admin', 'moderator', 'user'),
'create' => array('admin', 'moderator'),
'edit' => array('admin', 'moderator'),
'edittask' => array('admin', 'moderator'),
'delete' => array('admin', 'moderator')
);
/**
* 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
$charactergroups = null;
$charactergroup = null;
if(count(array_intersect(array('admin', 'moderator'), \hhu\z\controllers\SeminaryController::$character['characterroles'])) > 0) {
$charactergroups = $this->Charactergroups->getGroupsForGroupsgroup($groupsgroup['id']);
}
else {
$character = $this->Characters->getCharacterForUserAndSeminary($this->Auth->getUserId(), $seminary['id']);
$charactergroups = $this->Charactergroups->getGroupsForCharacter($character['id']);
if(!empty($charactergroups)) {
$charactergroup = $charactergroups[0];
}
}
// Select group by parameter
$selectedStationGroupId = $this->request->getGetParam('stationgroup');
if(!is_null($selectedStationGroupId))
{
$selectedStationGroupId = intval($selectedStationGroupId);
foreach($charactergroups as &$group) {
if($group['id'] == $selectedStationGroupId) {
$charactergroup = $group;
break;
}
}
}
// Get Stations
$stations = array();
if(is_null($charactergroup)) {
if(count(array_intersect(array('admin', 'moderator'), \hhu\z\controllers\SeminaryController::$character['characterroles'])) > 0) {
$stations = $this->Charactergroupsqueststations->getStationsForQuest($quest['id']);
}
}
else {
$stations = $this->Charactergroupsqueststations->getEnteredStationsForQuestAndGroup($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);
// Get media
if(!is_null($station['rightimage_id'])) {
$station['rightimage'] = $this->Media->getSeminaryMediaById($station['rightimage_id']);
}
if(!is_null($station['rightav_id'])) {
$station['rightav'] = $this->Media->getSeminaryMediaById($station['rightav_id']);
}
if(!is_null($station['wrongimage_id'])) {
$station['wrongimage'] = $this->Media->getSeminaryMediaById($station['wrongimage_id']);
}
if(!is_null($station['wrongav_id'])) {
$station['wrongav'] = $this->Media->getSeminaryMediaById($station['wrongav_id']);
}
// Get Character group(s)
$stationgroups = null;
$stationgroup = null;
$charactergroup = null;
if(count(array_intersect(array('admin', 'moderator'), \hhu\z\controllers\SeminaryController::$character['characterroles'])) > 0) {
$stationgroups = $this->Charactergroups->getGroupsForGroupsgroup($groupsgroup['id']);
}
else
{
$character = $this->Characters->getCharacterForUserAndSeminary($this->Auth->getUserId(), $seminary['id']);
$stationgroups = $this->Charactergroups->getGroupsForCharacter($character['id']);
if(!empty($stationgroups)) {
$stationgroup = $stationgroups[0];
$charactergroup = $stationgroup;
}
}
// Select group by parameter
$selectedStationGroupId = $this->request->getGetParam('stationgroup');
if(!is_null($selectedStationGroupId))
{
$selectedStationGroupId = intval($selectedStationGroupId);
foreach($stationgroups as &$group) {
if($group['id'] == $selectedStationGroupId) {
$stationgroup = $group;
break;
}
}
}
// 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['tried'] = $this->Charactergroupsqueststations->hasCharactergroupTriedStation(
$station['id'],
$group['id']
);
if($group['tried']) {
$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, $stationgroup);
}
else
{
// Mark Station as solved
if(!is_null($charactergroup)) {
$this->Charactergroupsqueststations->setStationSolved(
$station['id'],
$charactergroup['id']
);
}
}
// Status
$entered = false;
$tried = false;
$solved = false;
if(!is_null($stationgroup)) {
$entered = $this->Charactergroupsqueststations->hasCharactergroupEnteredStation(
$station['id'],
$stationgroup['id']
);
if($entered) {
$tried = $this->Charactergroupsqueststations->hasCharactergroupTriedStation(
$station['id'],
$stationgroup['id']
);
if($tried) {
$solved = $this->Charactergroupsqueststations->hasCharactergroupSolvedStation(
$station['id'],
$stationgroup['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('stationgroups', $stationgroups);
$this->set('stationgroup', $stationgroup);
$this->set('entered', $entered);
$this->set('tried', $tried);
$this->set('solved', $solved);
}
/**
* Action: create.
*
* Create a new Character groups Quest Station for a Character
* groups Quest.
*
* @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 create($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 Quest types
$stationtypes = $this->Stationtypes->getStationtypes();
foreach($stationtypes as &$stationtype) {
$stationtype['selected'] = false;
}
// Get allowed mimetypes
$iconMimetypes = \nre\configs\AppConfig::$mimetypes['icons'];
$imageMimetypes = \nre\configs\AppConfig::$mimetypes['stationimages'];
$avMimetypes = \nre\configs\AppConfig::$mimetypes['stationavs'];
// Values
$title = '';
$prolog = '';
$task = '';
$longitude = null;
$latitude = null;
$rightText = '';
$wrongText = '';
$textMedia = array(
'rightimage' => null,
'rightav' => null,
'wrongimage' => null,
'wrongav' => null
);
$fields = array('title');
$validation = array();
// Create a new Station
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('create')))
{
// Get params and validate them
$validation = $this->Validation->validateParams($this->request->getPostParams(), $fields);
$title = $this->request->getPostParam('title');
if($this->Charactergroupsqueststations->stationTitleExists($quest['id'], $title)) {
$validation = $this->Validation->addValidationResult($validation, 'title', 'exist', true);
}
$prolog = $this->request->getPostParam('prolog');
$task = $this->request->getPostParam('task');
$longitude = $this->request->getPostParam('longitude');
$latitude = $this->request->getPostParam('latitude');
$rightText = $this->request->getPostParam('rightText');
$wrongText = $this->request->getPostParam('wrongText');
// Validate Stationtype
$stationtypeIndex = null;
foreach($stationtypes as $index => &$stationtype)
{
$stationtype['selected'] = ($stationtype['url'] == $this->request->getPostParam('stationtype'));
if($stationtype['selected']) {
$stationtypeIndex = $index;
}
}
if(is_null($stationtypeIndex)) {
throw new \nre\exceptions\ParamsNotValidException($stationtype);
}
// Validate icon
$icon = null;
if(!empty($_FILES) && array_key_exists('icon', $_FILES) && $_FILES['icon']['error'] != UPLOAD_ERR_NO_FILE)
{
$icon = $_FILES['icon'];
// Check error
if($icon['error'] !== UPLOAD_ERR_OK) {
$validation = $this->Validation->addValidationResult($validation, 'icon', 'error', $icon['error']);
}
// Check mimetype
$mediaMimetype = null;
$icon['mimetype'] = \hhu\z\Utils::getMimetype($icon['tmp_name'], $icon['type']);
foreach($iconMimetypes as &$mimetype) {
if($mimetype['mimetype'] == $icon['mimetype']) {
$mediaMimetype = $mimetype;
break;
}
}
if(is_null($mediaMimetype)) {
$validation = $this->Validation->addValidationResult($validation, 'icon', 'mimetype', $icon['mimetype']);
}
elseif($icon['size'] > $mediaMimetype['size']) {
$validation = $this->Validation->addValidationResult($validation, 'icon', 'size', $mediaMimetype['size']);
}
}
// Validate media
if(!empty($_FILES))
{
foreach(array_keys($textMedia) as $mediumKey)
{
if(array_key_exists($mediumKey, $_FILES) && $_FILES[$mediumKey]['error'] != UPLOAD_ERR_NO_FILE)
{
$mediumValidation = true;
$medium = $_FILES[$mediumKey];
// Check error
if($medium['error'] !== UPLOAD_ERR_OK) {
$mediumValidation = $this->Validation->addValidationResults($mediumValidation, 'error', $medium['error']);
}
else
{
// Check mimetype
$mediaMimetype = null;
$medium['mimetype'] = \hhu\z\Utils::getMimetype($medium['tmp_name'], $medium['type']);
$mediaMimetypes = (strpos($mediumKey, 'image') !== false) ? $imageMimetypes : $avMimetypes;
foreach($mediaMimetypes as &$mimetype) {
if($mimetype['mimetype'] == $medium['mimetype']) {
$mediaMimetype = $mimetype;
break;
}
}
if(is_null($mediaMimetype)) {
$mediumValidation = $this->Validation->addValidationResults($mediumValidation, 'mimetype', $medium['mimetype']);
}
elseif($medium['size'] > $mediaMimetype['size']) {
$mediumValidation = $this->Validation->addValidationResults($mediumValidation, 'size', $mediaMimetype['size']);
}
if($validation == true) {
$textMedia[$mediumKey] = $medium;
}
}
if($mediumValidation !== true) {
$validation = $this->Validation->addValidationResults($validation, $mediumKey, $mediumValidation);
}
}
}
}
// Create new Station
if($validation === true)
{
$stationId = $this->Charactergroupsqueststations->createStation(
$quest['id'],
$stationtypes[$stationtypeIndex]['id'],
$title,
$prolog,
$task,
$latitude,
$longitude,
$rightText,
$wrongText
);
$station = $this->Charactergroupsqueststations->getStationById($stationId);
// Upload icon
if(!is_null($icon))
{
$mediaId = $this->Media->createQuestMedia(
$this->Auth->getUserId(),
$seminary['id'],
sprintf('charactergroupsqueststation-%s', $station['url']),
'',
$icon['mimetype'],
$icon['tmp_name']
);
if($mediaId !== false) {
$this->Charactergroupsqueststations->setPictureForStation($station['id'], $mediaId);
}
}
// Upload media
$textMediaIds = array();
foreach($textMedia as $mediumKey => $medium)
{
if(!is_null($medium))
{
$mediaId = $this->Media->createStationMedia(
$this->Auth->getUserId(),
$seminary['id'],
sprintf('station-%d-%s', $station['id'], $mediumKey),
$medium['name'],
$medium['type'],
$medium['tmp_name']
);
$textMediaIds[$mediumKey] = $mediaId;
}
}
// Set media
if(array_key_exists('rightimage', $textMediaIds)) {
$this->Charactergroupsqueststations->setRightImageForStation(
$station['id'],
$textMediaIds['rightimage']
);
}
if(array_key_exists('rightav', $textMediaIds)) {
$this->Charactergroupsqueststations->setRightAvForStation(
$station['id'],
$textMediaIds['rightav']
);
}
if(array_key_exists('wrongimage', $textMediaIds)) {
$this->Charactergroupsqueststations->setWrongImageForStation(
$station['id'],
$textMediaIds['wrongimage']
);
}
if(array_key_exists('wrongav', $textMediaIds)) {
$this->Charactergroupsqueststations->setWrongAvForStation(
$station['id'],
$textMediaIds['wrongav']
);
}
// Redirect to Station page
$this->redirect(
$this->linker->link(
array(
'station',
$seminary['url'],
$groupsgroup['url'],
$quest['url'],
$station['url']
),
1
)
);
}
}
// Set title
$this->addTitleLocalized('Create Station');
$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('title', $title);
$this->set('prolog', $prolog);
$this->set('task', $task);
$this->set('longitude', $longitude);
$this->set('latitude', $latitude);
$this->set('righttext', $rightText);
$this->set('wrongtext', $wrongText);
$this->set('iconMimetypes', $iconMimetypes);
$this->set('imageMimetypes', $imageMimetypes);
$this->set('avMimetypes', $avMimetypes);
$this->set('stationtypes', $stationtypes);
$this->set('validation', $validation);
}
/**
* Action: edit.
*
* Edit 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 edit($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);
// Get media
if(!is_null($station['rightimage_id'])) {
$station['rightimage'] = $this->Media->getSeminaryMediaById($station['rightimage_id']);
}
if(!is_null($station['rightav_id'])) {
$station['rightav'] = $this->Media->getSeminaryMediaById($station['rightav_id']);
}
if(!is_null($station['wrongimage_id'])) {
$station['wrongimage'] = $this->Media->getSeminaryMediaById($station['wrongimage_id']);
}
if(!is_null($station['wrongav_id'])) {
$station['wrongav'] = $this->Media->getSeminaryMediaById($station['wrongav_id']);
}
// Get Quest types
$stationtypes = $this->Stationtypes->getStationtypes();
foreach($stationtypes as &$stationtype) {
$stationtype['selected'] = ($stationtype['id'] == $station['stationtype_id']);
}
// Get allowed mimetypes
$iconMimetypes = \nre\configs\AppConfig::$mimetypes['icons'];
$imageMimetypes = \nre\configs\AppConfig::$mimetypes['stationimages'];
$avMimetypes = \nre\configs\AppConfig::$mimetypes['stationavs'];
// Values
$title = $station['title'];
$prolog = $station['prolog'];
$task = $station['task'];
$longitude = $station['longitude'];
$latitude = $station['latitude'];
$rightText = $station['righttext'];
$wrongText = $station['wrongtext'];
$textMedia = array(
'rightimage' => null,
'rightav' => null,
'wrongimage' => null,
'wrongav' => null
);
$fields = array('title');
$validation = array();
// Check request method
if($this->request->getRequestMethod() == 'POST' && (!is_null($this->request->getPostParam('edit')) || !is_null($this->request->getPostParam('edit-task'))))
{
// Get params and validate them
$validation = $this->Validation->validateParams($this->request->getPostParams(), $fields);
$title = $this->request->getPostParam('title');
if($this->Charactergroupsqueststations->stationTitleExists($quest['id'], $title, $station['id'])) {
$validation = $this->Validation->addValidationResult($validation, 'title', 'exist', true);
}
$prolog = $this->request->getPostParam('prolog');
$task = $this->request->getPostParam('task');
$longitude = $this->request->getPostParam('longitude');
$latitude = $this->request->getPostParam('latitude');
$rightText = $this->request->getPostParam('rightText');
$wrongText = $this->request->getPostParam('wrongText');
// Validate Stationtype
$stationtypeIndex = null;
foreach($stationtypes as $index => &$stationtype)
{
$stationtype['selected'] = ($stationtype['url'] == $this->request->getPostParam('stationtype'));
if($stationtype['selected']) {
$stationtypeIndex = $index;
}
}
if(is_null($stationtypeIndex)) {
throw new \nre\exceptions\ParamsNotValidException($stationtype);
}
// Validate icon
$icon = null;
if(!empty($_FILES) && array_key_exists('icon', $_FILES) && $_FILES['icon']['error'] != UPLOAD_ERR_NO_FILE)
{
$icon = $_FILES['icon'];
// Check error
if($icon['error'] !== UPLOAD_ERR_OK) {
$validation = $this->Validation->addValidationResult($validation, 'icon', 'error', $icon['error']);
}
// Check mimetype
$mediaMimetype = null;
$icon['mimetype'] = \hhu\z\Utils::getMimetype($icon['tmp_name'], $icon['type']);
foreach($iconMimetypes as &$mimetype) {
if($mimetype['mimetype'] == $icon['mimetype']) {
$mediaMimetype = $mimetype;
break;
}
}
if(is_null($mediaMimetype)) {
$validation = $this->Validation->addValidationResult($validation, 'icon', 'mimetype', $icon['mimetype']);
}
elseif($icon['size'] > $mediaMimetype['size']) {
$validation = $this->Validation->addValidationResult($validation, 'icon', 'size', $mediaMimetype['size']);
}
}
// Validate media
if(!empty($_FILES))
{
foreach(array_keys($textMedia) as $mediumKey)
{
if(array_key_exists($mediumKey, $_FILES) && $_FILES[$mediumKey]['error'] != UPLOAD_ERR_NO_FILE)
{
$mediumValidation = true;
$medium = $_FILES[$mediumKey];
// Check error
if($medium['error'] !== UPLOAD_ERR_OK) {
$mediumValidation = $this->Validation->addValidationResults($mediumValidation, 'error', $medium['error']);
}
else
{
// Check mimetype
$mediaMimetype = null;
$medium['mimetype'] = \hhu\z\Utils::getMimetype($medium['tmp_name'], $medium['type']);
$mediaMimetypes = (strpos($mediumKey, 'image') !== false) ? $imageMimetypes : $avMimetypes;
foreach($mediaMimetypes as &$mimetype) {
if($mimetype['mimetype'] == $medium['mimetype']) {
$mediaMimetype = $mimetype;
break;
}
}
if(is_null($mediaMimetype)) {
$mediumValidation = $this->Validation->addValidationResults($mediumValidation, 'mimetype', $medium['mimetype']);
}
elseif($medium['size'] > $mediaMimetype['size']) {
$mediumValidation = $this->Validation->addValidationResults($mediumValidation, 'size', $mediaMimetype['size']);
}
if($validation == true) {
$textMedia[$mediumKey] = $medium;
}
}
if($mediumValidation !== true) {
$validation = $this->Validation->addValidationResults($validation, $mediumKey, $mediumValidation);
}
}
}
}
// Edit Station
if($validation === true)
{
$this->Charactergroupsqueststations->editStation(
$station['id'],
$stationtypes[$stationtypeIndex]['id'],
$title,
$prolog,
$task,
$latitude,
$longitude,
$rightText,
$wrongText
);
$station = $this->Charactergroupsqueststations->getStationById($station['id']);
// Upload icon
if(!is_null($icon))
{
$mediaId = $this->Media->createQuestMedia(
$this->Auth->getUserId(),
$seminary['id'],
sprintf('charactergroupsqueststation-%s', $station['url']),
'',
$icon['mimetype'],
$icon['tmp_name']
);
if($mediaId !== false) {
$this->Charactergroupsqueststations->setPictureForStation($station['id'], $mediaId);
}
}
// Upload media
$textMediaIds = array();
foreach($textMedia as $mediumKey => $medium)
{
if(!is_null($medium))
{
$mediaId = $this->Media->createStationMedia(
$this->Auth->getUserId(),
$seminary['id'],
sprintf('station-%d-%s', $station['id'], $mediumKey),
$medium['name'],
$medium['type'],
$medium['tmp_name']
);
$textMediaIds[$mediumKey] = $mediaId;
}
}
// Set media
if(array_key_exists('rightimage', $textMediaIds)) {
$this->Charactergroupsqueststations->setRightImageForStation(
$station['id'],
$textMediaIds['rightimage']
);
}
if(array_key_exists('rightav', $textMediaIds)) {
$this->Charactergroupsqueststations->setRightAvForStation(
$station['id'],
$textMediaIds['rightav']
);
}
if(array_key_exists('wrongimage', $textMediaIds)) {
$this->Charactergroupsqueststations->setWrongImageForStation(
$station['id'],
$textMediaIds['wrongimage']
);
}
if(array_key_exists('wrongav', $textMediaIds)) {
$this->Charactergroupsqueststations->setWrongAvForStation(
$station['id'],
$textMediaIds['wrongav']
);
}
// Redirect
if(!is_null($this->request->getPostParam('edit-task'))) {
// To task editing
$this->redirect(
$this->linker->link(
array(
'edittask',
$seminary['url'],
$groupsgroup['url'],
$quest['url'],
$station['url']
),
1
)
);
}
else {
// To Station page
$this->redirect(
$this->linker->link(
array(
'station',
$seminary['url'],
$groupsgroup['url'],
$quest['url'],
$station['url']
),
1
)
);
}
}
}
// Set title
$this->addTitleLocalized('Edit Station');
$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('title', $title);
$this->set('prolog', $prolog);
$this->set('task', $task);
$this->set('longitude', $longitude);
$this->set('latitude', $latitude);
$this->set('righttext', $rightText);
$this->set('wrongtext', $wrongText);
$this->set('iconMimetypes', $iconMimetypes);
$this->set('imageMimetypes', $imageMimetypes);
$this->set('avMimetypes', $avMimetypes);
$this->set('stationtypes', $stationtypes);
$this->set('validation', $validation);
}
/**
* Action: edittask.
*
* Edit the task of a Character groups Quest Station.
*
* @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 edittask($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);
// Render editing task
$task = null;
$stationtype = $this->Stationtypes->getStationtypeById($station['stationtype_id']);
if(!is_null($stationtype['classname'])) {
$task = $this->renderTaskEditing($stationtype['classname'], $seminary, $groupsgroup, $quest, $station);
}
// Set title
$this->addTitleLocalized('Edit Station task');
$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('task', $task);
}
/**
* Action: delete.
*
* Delete 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 delete($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);
// Check request method
if($this->request->getRequestMethod() == 'POST')
{
// Check confirmation
if(!is_null($this->request->getPostParam('delete')))
{
// Delete seminary
$this->Charactergroupsqueststations->deleteStation(
$station['id']
);
// Redirect to overview
$this->redirect(
$this->linker->link(
array(
'charactergroupsquests',
'quest',
$seminary['url'],
$groupsgroup['url'],
$quest['url']
),
0, true, null, true, 'stations'
)
);
}
// Redirect to entry
$this->redirect(
$this->linker->link(
array(
'station',
$seminary['url'],
$groupsgroup['url'],
$quest['url'],
$station['url']
),
1
)
);
}
// Set title
$this->addTitleLocalized('Delete Station');
$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);
}
/**
* 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 $groupsgroup Groups-group 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;
}
/**
* Render editing of a Station task.
*
* @param string $stationtypeClassname Name of the class for the Stationtype of a Station
* @param array $seminary Seminary data
* @param array $groupsgroup Groupsgroup data
* @param array $quest Quest data
* @param array $station Station data
* @return string Rendered output
*/
private function renderTaskEditing($stationtypeClassname, $seminary, $groupsgroup, $quest, $station)
{
$task = null;
try {
// Generate request and response
$request = clone $this->request;
$response = $this->createStationtypeResponse('edittask', $seminary, $groupsgroup, $quest, $station);
// Load Stationtype Agent
$stationtypeAgent = $this->loadStationtypeAgent($stationtypeClassname, $request, $response);
// 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 $param1 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();
}
}
?>