fix labels and text editors for Quest creation

This commit is contained in:
oliver 2016-01-15 12:33:07 +01:00
commit 476c18b6a9
4278 changed files with 1196345 additions and 0 deletions

View file

@ -0,0 +1,197 @@
<?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;
/**
* Abstract class for implementing a Controller of an IntermediateAgent.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
abstract class IntermediateController extends \hhu\z\Controller
{
/**
* Required models
*
* @var array
*/
public $models = array('users', 'userroles', 'seminaries', 'characters');
/**
* Current user
*
* @var array
*/
public static $user = null;
/**
* Title information
*
* @var array
*/
private $title = array();
/**
* Construct a new IntermediateController.
*
* @throws \nre\exceptions\DriverNotFoundException
* @throws \nre\exceptions\DriverNotValidException
* @throws \nre\exceptions\ModelNotValidException
* @throws \nre\exceptions\ModelNotFoundException
* @throws \nre\exceptions\ViewNotFoundException
* @param string $layoutName Name of the current Layout
* @param string $action Current Action
* @param \nre\core\Agent $agent Corresponding Agent
*/
public function __construct($layoutName, $action, $agent)
{
parent::__construct($layoutName, $action, $agent);
}
/**
* Prefilter that is executed before running the Controller.
*
* @param \nre\core\Request $request Current request
* @param \nre\core\Response $response Current response
*/
public function preFilter(\nre\core\Request $request, \nre\core\Response $response)
{
parent::preFilter($request, $response);
// Get userdata
try {
self::$user = $this->Users->getUserById($this->Auth->getUserId());
self::$user['roles'] = array_map(function($r) { return $r['name']; }, $this->Userroles->getUserrolesForUserById(self::$user['id']));
}
catch(\nre\exceptions\IdNotFoundException $e) {
}
// Check permissions
$this->checkPermission($request, $response);
// Set userdata
$this->set('loggedUser', self::$user);
}
/**
* Postfilter that is executed after running the Controller.
*
* @param \nre\core\Request $request Current request
* @param \nre\core\Response $response Current response
*/
public function postFilter(\nre\core\Request $request, \nre\core\Response $response)
{
parent::postFilter($request, $response);
}
/**
* Return current title information.
*
* @return string Title information
*/
public function getTitle()
{
return $this->title;
}
/**
* Add a piece of information to the current title.
*
* @param string $title Title information
*/
protected function addTitle($title)
{
$this->title[] = $title;
}
/**
* Add a piece of information to the current title and localize
* it.
*
* @param string $title Title information
*/
protected function addTitleLocalized($title)
{
$title = gettext($title);
$args = func_get_args();
if(count($args) > 0) {
$title = call_user_func_array(
'sprintf',
array_merge(
array($title),
array_slice($args, 1)
)
);
}
$this->title[] = $title;
}
/**
* Check user permissions.
*
* @throws \nre\exceptions\AccessDeniedException
* @param \nre\core\Request $request Current request
* @param \nre\core\Response $response Current response
*/
private function checkPermission(\nre\core\Request $request, \nre\core\Response $response)
{
// Determine user
$userRoles = array('guest');
if(!is_null(self::$user)) {
$userRoles = self::$user['roles'];
}
// Do not check error pages
if($response->getParam(0, 'toplevel') == \nre\core\Config::getDefault('toplevel-error')) {
return;
}
if($response->getParam(1, 'intermediate') == \nre\core\Config::getDefault('intermediate-error')) {
return;
}
// Determine permissions of Intermediate Controller for current action
$controller = $this->agent->controller;
$action = $this->request->getParam(2, 'action');
if(!property_exists($controller, 'permissions')) {
return; // Allow if nothing is specified
}
if(!array_key_exists($action, $controller->permissions)) {
return; // Allow if Action is not specified
}
$permissions = $controller->permissions[$action];
// Check permissions
if(count(array_intersect($userRoles, $permissions)) == 0) {
throw new \nre\exceptions\AccessDeniedException();
}
}
}
?>

View file

@ -0,0 +1,320 @@
<?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;
/**
* Abstract class for implementing a QuesttypeController.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
abstract class QuesttypeController extends \hhu\z\Controller
{
/**
* Required models
*
* @var array
*/
public $models = array('seminaries', 'questgroups', 'quests', 'characters');
/**
* Save the answers of a Character for a Quest.
*
* @param array $seminary Current Seminary data
* @param array $questgroup Current Questgroup data
* @param array $quest Current Quest data
* @param array $character Current Character data
* @param array $answers Character answers for the Quest
*/
public abstract function saveAnswersOfCharacter($seminary, $questgroup, $quest, $character, $answers);
/**
* Save additional data for the answers of a Character for a Quest.
*
* @param array $seminary Current Seminary data
* @param array $questgroup Current Questgroup data
* @param array $quest Current Quest data
* @param array $character Current Character data
* @param array $data Additional (POST-) data
*/
public abstract function saveDataForCharacterAnswers($seminary, $questgroup, $quest, $character, $data);
/**
* Check if answers of a Character for a Quest match the correct ones.
*
* @param array $seminary Current Seminary data
* @param array $questgroup Current Questgroup data
* @param array $quest Current Quest data
* @param array $character Current Character data
* @param array $answers Character answers for the Quest
* @return boolean True/false for a right/wrong answer or null for moderator evaluation
*/
public abstract function matchAnswersOfCharacter($seminary, $questgroup, $quest, $character, $answers);
/**
* Action: quest.
*
* Show the task of a Quest.
*
* @param array $seminary Current Seminary data
* @param array $questgroup Current Questgroup data
* @param array $quest Current Quest data
* @param array $character Current Character data
* @param \Exception $exception Character submission exception
*/
public abstract function quest($seminary, $questgroup, $quest, $character, $exception);
/**
* Action: submission.
*
* Show the submission of a Character for a Quest.
*
* @param array $seminary Current Seminary data
* @param array $questgroup Current Questgroup data
* @param array $quest Current Quest data
* @param array $character Current Character data
*/
public abstract function submission($seminary, $questgroup, $quest, $character);
/**
* Action: edittask.
*
* Edit the task of a Quest.
*
* @param array $seminary Current Seminary data
* @param array $questgroup Current Questgroup data
* @param array $quest Current Quest data
*/
public abstract function edittask($seminary, $questgroup, $quest);
/**
* Load a QuesttypeController.
*
* @static
* @throws \hhu\z\exceptions\QuesttypeControllerNotFoundException
* @throws \hhu\z\exceptions\QuesttypeControllerNotValidException
* @param string $controllerName Name of the QuesttypeController to load
*/
public static function load($controllerName)
{
// Determine full classname
$className = self::getClassName($controllerName);
try {
// Load class
static::loadClass($controllerName, $className);
// Validate class
static::checkClass($className, get_class());
}
catch(\nre\exceptions\ClassNotValidException $e) {
throw new \hhu\z\exceptions\QuesttypeControllerNotValidException($e->getClassName());
}
catch(\nre\exceptions\ClassNotFoundException $e) {
throw new \hhu\z\exceptions\QuesttypeControllerNotFoundException($e->getClassName());
}
}
/**
* Instantiate a QuesttypeController (Factory Pattern).
*
* @static
* @throws \nre\exceptions\DatamodelException
* @throws \nre\exceptions\DriverNotFoundException
* @throws \nre\exceptions\DriverNotValidException
* @throws \nre\exceptions\ModelNotValidException
* @throws \nre\exceptions\ModelNotFoundException
* @throws \nre\exceptions\ViewNotFoundException
* @throws \hhu\z\exceptions\QuesttypeModelNotValidException
* @throws \hhu\z\exceptions\QuesttypeModelNotFoundException
* @param string $controllerName Name of the QuesttypeController to instantiate
* @param string $layoutName Name of the current Layout
* @param string $action Current Action
* @param \nre\core\Agent $agent Corresponding Agent
*/
public static function factory($controllerName, $layoutName, $action, $agent)
{
// Determine full classname
$className = self::getClassName($controllerName);
// Construct and return Controller
return new $className($layoutName, $action, $agent);
}
/**
* Determine the Controller-classname for the given Questtype-name.
*
* @static
* @param string $questtypeName Questtype-name to get Controller-classname of
* @return string Classname for the Questtype-name
*/
private static function getClassName($questtypeName)
{
$className = \nre\core\ClassLoader::concatClassNames($questtypeName, \nre\core\ClassLoader::stripClassType(\nre\core\ClassLoader::stripNamespace(get_class())), 'controller');
return \nre\configs\AppConfig::$app['namespace']."questtypes\\$className";
}
/**
* Load the class of a QuesttypeController
*
* @static
* @throws \nre\exceptions\ClassNotFoundException
* @param string $questtypeName Name of the QuesttypeController to load
* @param string $fullClassName Name of the class to load
*/
private static function loadClass($questtypeName, $fullClassName)
{
// Determine folder to look in
$className = explode('\\', $fullClassName);
$className = array_pop($className);
// Determine filename
$fileName = ROOT.DS.\nre\configs\AppConfig::$dirs['questtypes'].DS.strtolower($questtypeName).DS.$className.\nre\configs\CoreConfig::getFileExt('includes');
// Check file
if(!file_exists($fileName))
{
throw new \nre\exceptions\ClassNotFoundException(
$fullClassName
);
}
// Include file
include_once($fileName);
}
/**
* Check inheritance of the QuesttypeController-class.
*
* @static
* @throws \nre\exceptions\ClassNotValidException
* @param string $className Name of the class to check
* @param string $parentClassName Name of the parent class
*/
public static function checkClass($className, $parentClassName)
{
// Check if class is subclass of parent class
if(!is_subclass_of($className, $parentClassName)) {
throw new \nre\exceptions\ClassNotValidException(
$className
);
}
}
/**
* Construct a new application Controller.
*
* @throws \nre\exceptions\DriverNotFoundException
* @throws \nre\exceptions\DriverNotValidException
* @throws \nre\exceptions\ModelNotValidException
* @throws \nre\exceptions\ModelNotFoundException
* @throws \nre\exceptions\ViewNotFoundException
* @throws \hhu\z\exceptions\QuesttypeModelNotValidException
* @throws \hhu\z\exceptions\QuesttypeModelNotFoundException
* @param string $layoutName Name of the current Layout
* @param string $action Current Action
* @param Agent $agent Corresponding Agent
*/
public function __construct($layoutName, $action, $agent)
{
parent::__construct($layoutName, $action, $agent);
}
/**
* Load the Models of this Controller.
*
* @throws \nre\exceptions\DatamodelException
* @throws \nre\exceptions\DriverNotFoundException
* @throws \nre\exceptions\DriverNotValidException
* @throws \nre\exceptions\ModelNotValidException
* @throws \nre\exceptions\ModelNotFoundException
* @throws \hhu\z\exceptions\QuesttypeModelNotValidException
* @throws \hhu\z\exceptions\QuesttypeModelNotFoundException
*/
protected function loadModels()
{
// Load default models
parent::loadModels();
// Load QuesttypeModel
$this->loadModel();
}
/**
* Load the Model of the Questtype.
*
* @throws \hhu\z\exceptions\QuesttypeModelNotValidException
* @throws \hhu\z\exceptions\QuesttypeModelNotFoundException
*/
private function loadModel()
{
// Determine Model
$model = \nre\core\ClassLoader::stripClassType(\nre\core\ClassLoader::stripClassType(\nre\core\ClassLoader::stripNamespace(get_class($this))));
// Load class
\hhu\z\models\QuesttypeModel::load($model);
// Construct Model
$modelName = ucfirst(strtolower($model));
$this->$modelName = \hhu\z\models\QuesttypeModel::factory($model);
}
/**
* Load the View of this QuesttypeController.
*
* @throws \nre\exceptions\ViewNotFoundException
* @param string $layoutName Name of the current Layout
* @param string $action Current Action
*/
protected function loadView($layoutName, $action)
{
// Check Layout name
if(is_null($layoutName)) {
return;
}
// Determine controller name
$controllerName = \nre\core\ClassLoader::stripClassType(\nre\core\ClassLoader::getClassName(get_class($this)));
// Load view
$this->view = \hhu\z\views\QuesttypeView::loadAndFactory($layoutName, $controllerName, $action);
}
}
?>

View file

@ -0,0 +1,193 @@
<?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;
/**
* Abstract class for implementing a Controller for a Seminary and its
* concepts.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
abstract class SeminaryController extends \hhu\z\controllers\IntermediateController
{
/**
* Required components
*
* @var array
*/
public $components = array('achievement', 'auth', 'notification');
/**
* Required models
*
* @var array
*/
public $models = array('seminaries', 'characters', 'characterroles', 'xplevels', 'avatars', 'achievements');
/**
* Current Seminary
*
* var array
*/
public static $seminary = null;
/**
* Character of current user and Seminary
*
* @var array
*/
public static $character = null;
/**
* Construct a new Seminary Controller.
*
* @throws \nre\exceptions\DriverNotFoundException
* @throws \nre\exceptions\DriverNotValidException
* @throws \nre\exceptions\ModelNotValidException
* @throws \nre\exceptions\ModelNotFoundException
* @throws \nre\exceptions\ViewNotFoundException
* @param string $layoutName Name of the current Layout
* @param string $action Current Action
* @param \nre\core\Agent $agent Corresponding Agent
*/
public function __construct($layoutName, $action, $agent)
{
parent::__construct($layoutName, $action, $agent);
}
/**
* Prefilter that is executed before running the Controller.
*
* @param \nre\core\Request $request Current request
* @param \nre\core\Response $response Current response
*/
public function preFilter(\nre\core\Request $request, \nre\core\Response $response)
{
parent::preFilter($request, $response);
// Get Seminary and Character data
try {
self::$seminary = $this->Seminaries->getSeminaryByUrl($this->request->getParam(3));
if(!is_null(self::$user))
{
self::$character = $this->Characters->getCharacterForUserAndSeminary(self::$user['id'], self::$seminary['id']);
self::$character['characterroles'] = array_map(function($r) { return $r['name']; }, $this->Characterroles->getCharacterrolesForCharacterById(self::$character['id']));
try {
self::$character['xplevel'] = $this->Xplevels->getXPLevelById(self::$character['xplevel_id']);
self::$character['avatar'] = $this->Avatars->getAvatarByTypeAndLevel(self::$seminary['id'], self::$character['charactertype_url'], self::$character['xplevel']['level']);
}
catch(\nre\exceptions\IdNotFoundException $e) {
// No Avatar available
}
}
}
catch(\nre\exceptions\IdNotFoundException $e) {
}
// Check permissions
$this->checkPermission($request, $response);
// Check achievements
$this->checkAchievements($request, 'date');
$this->checkAchievements($request, 'achievement');
// Set Seminary and Character data
$this->set('loggedSeminary', self::$seminary);
$this->set('loggedCharacter', self::$character);
}
/**
* Postfilter that is executed after running the Controller.
*
* @param \nre\core\Request $request Current request
* @param \nre\core\Response $response Current response
*/
public function postFilter(\nre\core\Request $request, \nre\core\Response $response)
{
parent::postFilter($request, $response);
}
/**
* Check user permissions.
*
* @throws AccessDeniedException
* @param \nre\core\Request $request Current request
* @param \nre\core\Response $response Current response
*/
private function checkPermission(\nre\core\Request $request, \nre\core\Response $response)
{
// Do not check index page
if(is_null($request->getParam(3))) {
return;
}
// Determine permissions for current action
$action = $this->request->getParam(2, 'action');
if(!property_exists($this, 'seminaryPermissions')) {
return; // Allow if nothing is specified
}
if(!array_key_exists($action, $this->seminaryPermissions)) {
return; // Allow if Action is not specified
}
$permissions = $this->seminaryPermissions[$action];
// Check permissions
if(is_null(self::$character) || !array_key_exists('characterroles', self::$character) || count(array_intersect(self::$character['characterroles'], $permissions)) == 0) {
throw new \nre\exceptions\AccessDeniedException();
}
}
/**
* Check for newly achieved Achievements.
*
* @param \nre\core\Request $request Current request
* @param array $checkConditions Conditions to check
*/
protected function checkAchievements(\nre\core\Request $request, $checkConditions=null)
{
// Do not check MediaController
if($request->getParam(0, 'toplevel') != \nre\configs\AppConfig::$defaults['toplevel']) {
return;
}
// Check if Character is present
if(is_null(self::$character)) {
return;
}
// Check Achievements
$achievements = $this->Achievement->checkAchievements(self::$seminary['id'], self::$character['id'], $checkConditions);
// Add notifications
foreach($achievements as &$achievement)
{
$this->Notification->addNotification(
\hhu\z\controllers\components\NotificationComponent::TYPE_ACHIEVEMENT,
$achievement['title'],
$this->linker->link(array('achievements', 'index', self::$seminary['url']), 0, true, null, true, $achievement['url']),
(!is_null($achievement['achieved_achievementsmedia_id']) ? $this->linker->link(array('media','achievement',self::$seminary['url'],$achievement['url'])) : null)
);
}
}
}
?>