diff --git a/controllers/QuestgroupsController.inc b/controllers/QuestgroupsController.inc index d7b8acf1..b5e7e509 100644 --- a/controllers/QuestgroupsController.inc +++ b/controllers/QuestgroupsController.inc @@ -31,7 +31,8 @@ * @var array */ public $permissions = array( - 'questgroup' => array('admin', 'moderator', 'user') + 'questgroup' => array('admin', 'moderator', 'user'), + 'create' => array('admin', 'moderator', 'user') ); /** * User seminary permissions @@ -39,7 +40,8 @@ * @var array */ public $seminaryPermissions = array( - 'questgroup' => array('admin', 'moderator', 'user') + 'questgroup' => array('admin', 'moderator', 'user'), + 'create' => array('admin', 'moderator') ); @@ -174,6 +176,45 @@ $this->set('quests', $quests); } + + /** + * Action: create. + * + * Create a new Questgroup. + * + * @param string $seminaryUrl URL-Title of a Seminary + */ + public function create($seminaryUrl) + { + // Get seminary + $seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl); + + // Create Questgroup + $validation = true; + if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('create'))) + { + // TODO Validation + $title = $this->request->getPostParam('title'); + + // Create new Questgroup + if($validation === true) + { + $questgroupId = $this->Questgroups->createQuestgroup( + $this->Auth->getUserId(), + $seminary['id'], + $title + ); + + // Redirect + $this->redirect($this->linker->link(array('seminaries', 'seminary', $seminary['url']))); + } + } + + + // Pass data to view + $this->set('seminary', $seminary); + } + } ?> diff --git a/controllers/QuestsController.inc b/controllers/QuestsController.inc index ee8a3c67..d35e4fd2 100644 --- a/controllers/QuestsController.inc +++ b/controllers/QuestsController.inc @@ -34,7 +34,8 @@ 'index' => array('admin', 'moderator', 'user'), 'quest' => array('admin', 'moderator', 'user'), 'submissions' => array('admin', 'moderator', 'user'), - 'submission' => array('admin', 'moderator', 'user') + 'submission' => array('admin', 'moderator', 'user'), + 'create' => array('admin', 'moderator', 'user') ); /** * User seminary permissions @@ -42,10 +43,11 @@ * @var array */ public $seminaryPermissions = array( - 'index' => array('admin', 'moderator', 'user'), - 'quest' => array('admin', 'moderator', 'user'), - 'submissions' => array('admin', 'moderator'), - 'submission' => array('admin', 'moderator') + 'index' => array('admin', 'moderator', 'user'), + 'quest' => array('admin', 'moderator', 'user'), + 'submissions' => array('admin', 'moderator'), + 'submission' => array('admin', 'moderator'), + 'create' => array('admin', 'moderator') ); @@ -418,6 +420,86 @@ } + /** + * Action: create. + * + * Create a new Quest. + * + * @param string $seminaryUrl URL-Title of a Seminary + */ + public function create($seminaryUrl) + { + // Get seminary + $seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl); + + // Quest groups + $questgroups = $this->Questgroups->getQuestgroupsForSeminary($seminary['id']); + + // Quest types + $questtypes = $this->Questtypes->getQuesttypes(); + + // Create Quest + $validation = true; + if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('create'))) + { + // TODO Validation + $name = $this->request->getPostParam('name'); + $xps = $this->request->getPostParam('xps'); + $wrongtext = $this->request->getPostParam('wrongtext'); + $task = $this->request->getPostParam('task'); + + // Validate Questgroup + $questgroupIndex = null; + foreach($questgroups as $index => &$questgroup) + { + $questgroup['selected'] = ($questgroup['url'] == $this->request->getPostParam('questgroup')); + if($questgroup['selected']) { + $questgroupIndex = $index; + } + } + if(is_null($questgroupIndex)) { + throw new \nre\exceptions\ParamsNotValidException($questgroup); + } + + // Validate Questtype + $questtypeIndex = null; + foreach($questtypes as $index => &$questtype) + { + $questtype['selected'] = ($questtype['url'] == $this->request->getPostParam('questtype')); + if($questtype['selected']) { + $questtypeIndex = $index; + } + } + if(is_null($questtypeIndex)) { + throw new \nre\exceptions\ParamsNotValidException($questtype); + } + + // Create new Quest + if($validation === true) + { + $questId = $this->Quests->createQuest( + $this->Auth->getUserId(), + $name, + $questgroups[$questgroupIndex]['id'], + $questtypes[$questtypeIndex]['id'], + $xps, + $wrongtext, + $task + ); + + // Redirect + $this->redirect($this->linker->link(array('quests', 'index', $seminary['url']))); + } + } + + + // Pass data to view + $this->set('seminary', $seminary); + $this->set('questgroups', $questgroups); + $this->set('questtypes', $questtypes); + } + + /** diff --git a/models/QuestgroupsModel.inc b/models/QuestgroupsModel.inc index 3e2ced21..26ae9a10 100644 --- a/models/QuestgroupsModel.inc +++ b/models/QuestgroupsModel.inc @@ -89,6 +89,25 @@ } + /** + * Get all Questgroups for a Seminary. + * + * @param int $seminaryId ID of Seminary + * @return array List of Questgroups + */ + public function getQuestgroupsForSeminary($seminaryId) + { + return $this->db->query( + 'SELECT id, title, url '. + 'FROM questgroups '. + 'WHERE seminary_id = ? '. + 'ORDER BY title ASC', + 'i', + $seminaryId + ); + } + + /** * Get a Questgroup by its ID. * @@ -503,6 +522,33 @@ } + /** + * Create a new Questgroup. + * + * @param int $userId User-ID that creates the new character + * @param int $seminaryId ID of Seminary + * @param string $title Title for new Questgroup + * @return int ID of new Questgroup + */ + public function createQuestgroup($userId, $seminaryId, $title) + { + $this->db->query( + 'INSERT INTO questgroups '. + '(created_user_id, seminary_id, title, url) '. + 'VALUES '. + '(?, ?, ?, ?)', + 'iiss', + $userId, + $seminaryId, + $title, + \nre\core\Linker::createLinkParam($title) + ); + + + return $this->db->getInsertId(); + } + + /** diff --git a/models/QuestsModel.inc b/models/QuestsModel.inc index 5b7eda91..13d113bc 100644 --- a/models/QuestsModel.inc +++ b/models/QuestsModel.inc @@ -405,6 +405,36 @@ ); } + + /** + * Create a new Quest. + * + * @param int $userId User-ID that creates the new character + * @param string $name Name for new Quest + * @param int $questgroupId ID of Questgroup + * @param int $questtypeId ID of Questtype + * @param int $xps XPs for new Quest + * @param string $wrongtext Wrongtext for new Quest + * @param string $task Task for new Quest + * @return int ID of new Quest + */ + public function createQuest($userId, $name, $questgroupId, $questtypeId, $xps, $wrongtext, $task) + { + $this->db->query( + 'INSERT INTO quests '. + '(created_user_id, questgroup_id, questtype_id, title, url, xps, wrong_text, task) '. + 'VALUES '. + '(?, ?, ?, ?, ?, ?, ?, ?)', + 'iiississ', + $userId, $questgroupId, $questtypeId, + $name, \nre\core\Linker::createLinkParam($name), + $xps, $wrongtext, $task + ); + + + return $this->db->getInsertId(); + } + } ?> diff --git a/models/QuesttypesModel.inc b/models/QuesttypesModel.inc index 3e0ef265..58b36648 100644 --- a/models/QuesttypesModel.inc +++ b/models/QuesttypesModel.inc @@ -34,6 +34,21 @@ + /** + * Get all registered Questtypes. + * + * @return array List of registered Questtypes + */ + public function getQuesttypes() + { + return $this->db->query( + 'SELECT id, title, url, classname '. + 'FROM questtypes '. + 'ORDER BY title ASC' + ); + } + + /** * Get a Questtype by its ID * diff --git a/views/html/questgroups/create.tpl b/views/html/questgroups/create.tpl new file mode 100644 index 00000000..233713cc --- /dev/null +++ b/views/html/questgroups/create.tpl @@ -0,0 +1,16 @@ + +