From b903a1df77613d36016e4eec6e354bc1b34ade10 Mon Sep 17 00:00:00 2001 From: coderkun Date: Fri, 18 Apr 2014 18:44:38 +0200 Subject: [PATCH] update form for creating Quests and add various data import helpers --- controllers/QuestsController.inc | 74 +++++++++++++++++++++++++++++++ models/MediaModel.inc | 56 +++++++++++++++++++++++ models/QuestsModel.inc | 19 ++++++++ models/QuesttextsModel.inc | 65 +++++++++++++++++++++++++++ views/html/quests/create.tpl | 13 ++++-- views/html/quests/createmedia.tpl | 19 ++++++++ 6 files changed, 242 insertions(+), 4 deletions(-) create mode 100644 views/html/quests/createmedia.tpl diff --git a/controllers/QuestsController.inc b/controllers/QuestsController.inc index 2ebdb257..4beb62ee 100644 --- a/controllers/QuestsController.inc +++ b/controllers/QuestsController.inc @@ -448,6 +448,7 @@ // TODO Validation $name = $this->request->getPostParam('name'); $xps = $this->request->getPostParam('xps'); + $prolog = $this->request->getPostParam('prolog'); $entrytext = $this->request->getPostParam('entrytext'); $wrongtext = $this->request->getPostParam('wrongtext'); $task = $this->request->getPostParam('task'); @@ -478,6 +479,17 @@ throw new \nre\exceptions\ParamsNotValidException($questtype); } + // Questmedia + $questmedia = null; + if(array_key_exists('questmedia', $_FILES) && !empty($_FILES['questmedia']) && $_FILES['questmedia']['error'] == 0) { + $questmedia = $_FILES['questmedia']; + } + + // Process Prolog + if(!empty($prolog)) { + $prolog = preg_split('/\s*(_|=){5,}\s*/', $prolog, -1, PREG_SPLIT_NO_EMPTY); + } + // Create new Quest if($validation === true) { @@ -492,6 +504,28 @@ $task ); + // Create Questmedia + if(!is_null($questmedia)) + { + $questsmediaId = $this->Quests->createQuestMedia( + $this->Auth->getUserId(), + $seminary['id'], + $questmedia['name'], + $name, + $questmedia['type'], + $questmedia['tmp_name'] + ); + if($questsmediaId > 0) { + $this->Quests->setQuestmedia($questId, $questsmediaId); + } + } + + // Add Prolog-texts + if(!empty($prolog)) { + $this->Questtexts->addQuesttextsToQuest($this->Auth->getUserId(), $questId, 'Prolog', $prolog); + } + + // Redirect $this->redirect($this->linker->link(array('quests', 'index', $seminary['url']))); } @@ -505,6 +539,46 @@ } + /** + * Action: createmedia. + * TODO only temporary for easier data import. + * + * Display a form for creating new Seminary media. + * + * @param string $seminaryUrl URL-title of Seminary + */ + public function createmedia($seminaryUrl) + { + // Get seminary + $seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl); + + // Create media + $mediaId = null; + $error = null; + if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('submit'))) + { + $file = $_FILES['file']; + $error = $file['error']; + if(empty($error)) + { + $mediaId = $this->Media->createQuestMedia( + $this->Auth->getUserId(), + $seminary['id'], + $file['name'], + $this->request->getPostParam('description'), + $file['type'], + $file['tmp_name'] + ); + } + } + + + // Pass data to view + $this->set('seminary', $seminary); + $this->set('mediaid', $mediaId); + } + + /** diff --git a/models/MediaModel.inc b/models/MediaModel.inc index 2c2d5eae..031df753 100644 --- a/models/MediaModel.inc +++ b/models/MediaModel.inc @@ -134,6 +134,62 @@ return $data[0]; } + + /** + * Create a new Questsmedia by creating a new Seminarymedia and + * adding it to the list of Questsmedia. + * TODO Currently only temporary for easier data import. + */ + public function createQuestMedia($userId, $seminaryId, $filename, $description, $mimetype, $tmpFilename) + { + $uploadId = false; + $this->db->setAutocommit(false); + + try { + // Create database record + $this->db->query( + 'INSERT INTO seminarymedia '. + '(created_user_id, seminary_id, name, url, description, mimetype) '. + 'VALUES '. + '(?, ? ,? ,?, ?, ?)', + 'iissss', + $userId, + $seminaryId, + $filename, + \nre\core\Linker::createLinkParam($filename), + $description, + $mimetype + ); + $uploadId = $this->db->getInsertId(); + + $this->db->query( + 'INSERT INTO questsmedia '. + '(media_id, created_user_id) '. + 'VALUES '. + '(?, ?)', + 'ii', + $uploadId, + $userId + ); + + // Create filename + $filename = ROOT.DS.'seminarymedia'.DS.$uploadId; + if(!move_uploaded_file($tmpFilename, $filename)) + { + $this->db->rollback(); + $uploadId = false; + } + } + catch(\nre\exceptions\DatamodelException $e) { + $this->db->rollback(); + $this->db->setAutocommit(true); + } + + + $this->db->setAutocommit(true); + return $uploadId; + } + } ?> diff --git a/models/QuestsModel.inc b/models/QuestsModel.inc index ff77498a..d840a39a 100644 --- a/models/QuestsModel.inc +++ b/models/QuestsModel.inc @@ -436,6 +436,25 @@ return $this->db->getInsertId(); } + + /** + * Set the media for a Quest. + * + * @param int $questId ID of Quest to set media for + * @param int $questmediaId ID of Questsmedia to set + */ + public function setQuestmedia($questId, $questsmediaId) + { + $this->db->query( + 'UPDATE quests '. + 'SET questsmedia_id = ? '. + 'WHERE id = ?', + 'ii', + $questsmediaId, + $questId + ); + } + } ?> diff --git a/models/QuesttextsModel.inc b/models/QuesttextsModel.inc index 53430ec9..7aca28b9 100644 --- a/models/QuesttextsModel.inc +++ b/models/QuesttextsModel.inc @@ -150,6 +150,71 @@ ); } + + /** + * Get a Questtexttype by its URL. + * + * @param string $questtexttypeUrl URL-type of Questtexttype + * @return array Questtexttype data + */ + public function getQuesttexttypeByUrl($questtexttypeUrl) + { + $data = $this->db->query( + 'SELECT id, type, url '. + 'FROM questtexttypes '. + 'WHERE url = ?', + 's', + $questtexttypeUrl + ); + if(!empty($data)) { + return $data[0]; + } + + + return null; + } + + + /** + * Add a list of Questtexts to a Quest. + * + * @param int $userId ID of user + * @param int $questId ID of Quest to add texts to + * @param string $questtexttypeUrl URL-type of Questtexttype of texts + * @param array $texts List of texts to add. + */ + public function addQuesttextsToQuest($userId, $questId, $questtexttypeUrl, $texts) + { + $questtexttype = $this->getQuesttexttypeByUrl($questtexttypeUrl); + if(is_null($questtexttype)) { + return; + } + foreach($texts as &$text) + { + $pos = $this->db->query( + 'SELECT COALESCE(MAX(pos),0)+1 AS pos '. + 'FROM questtexts '. + 'WHERE quest_id = ?', + 'i', + $questId + ); + $pos = $pos[0]['pos']; + + $this->db->query( + 'INSERT INTO questtexts '. + '(created_user_id, quest_id, questtexttype_id, pos, text) '. + 'VALUES '. + '(?, ?, ?, ?, ?)', + 'iiiis', + $userId, $questId, $questtexttype['id'], $pos, + $text + ); + } + } + + + + } ?> diff --git a/views/html/quests/create.tpl b/views/html/quests/create.tpl index 31089ccb..94bc47b3 100644 --- a/views/html/quests/create.tpl +++ b/views/html/quests/create.tpl @@ -7,7 +7,7 @@

-
+

@@ -25,12 +25,17 @@

+
+
+
+
+

-
+

-
+

-
+
diff --git a/views/html/quests/createmedia.tpl b/views/html/quests/createmedia.tpl new file mode 100644 index 00000000..003ea042 --- /dev/null +++ b/views/html/quests/createmedia.tpl @@ -0,0 +1,19 @@ + +
+ +
+ +

+

Create Questsmedia

+ + +

New mediaId:

+ + +

Error:

+ +
+
+
+ +