From 091d014815a611efcac376590200156c95de059a Mon Sep 17 00:00:00 2001 From: oliver Date: Sun, 24 Jan 2016 19:36:05 +0100 Subject: [PATCH] add media to right/wrong text of Character group Quest Stations --- configs/AppConfig.inc | 32 +++ ...CharactergroupsqueststationsController.inc | 241 +++++++++++++++++- db/create.sql | 14 +- models/CharactergroupsqueststationsModel.inc | 79 +++++- models/MediaModel.inc | 40 +++ .../charactergroupsqueststations/create.tpl | 53 +++- .../charactergroupsqueststations/edit.tpl | 86 ++++++- .../charactergroupsqueststations/station.tpl | 24 ++ 8 files changed, 547 insertions(+), 22 deletions(-) diff --git a/configs/AppConfig.inc b/configs/AppConfig.inc index fc5233b3..a412bf7b 100644 --- a/configs/AppConfig.inc +++ b/configs/AppConfig.inc @@ -164,6 +164,38 @@ 'mimetype' => 'image/png', 'size' => 10485760 ) + ), + 'stationimages' => array( + array( + 'mimetype' => 'image/jpeg', + 'size' => 1048576 + ), + array( + 'mimetype' => 'image/png', + 'size' => 2097152 + ) + ), + 'stationavs' => array( + array( + 'mimetype' => 'audio/mpeg', + 'size' => 2097152 + ), + array( + 'mimetype' => 'audio/ogg', + 'size' => 2097152 + ), + array( + 'mimetype' => 'audio/opus', + 'size' => 2097152 + ), + array( + 'mimetype' => 'video/mp4', + 'size' => 2097152 + ), + array( + 'mimetype' => 'video/webm', + 'size' => 2097152 + ) ) ); diff --git a/controllers/CharactergroupsqueststationsController.inc b/controllers/CharactergroupsqueststationsController.inc index ba182e85..db71f747 100644 --- a/controllers/CharactergroupsqueststationsController.inc +++ b/controllers/CharactergroupsqueststationsController.inc @@ -133,6 +133,20 @@ // 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 $charactergroup = null; $character = $this->Characters->getCharacterForUserAndSeminary($this->Auth->getUserId(), $seminary['id']); @@ -233,7 +247,9 @@ } // Get allowed mimetypes - $mimetypes = \nre\configs\AppConfig::$mimetypes['icons']; + $iconMimetypes = \nre\configs\AppConfig::$mimetypes['icons']; + $imageMimetypes = \nre\configs\AppConfig::$mimetypes['stationimages']; + $avMimetypes = \nre\configs\AppConfig::$mimetypes['stationavs']; // Values $title = ''; @@ -243,6 +259,12 @@ $latitude = null; $rightText = ''; $wrongText = ''; + $textMedia = array( + 'rightimage' => null, + 'rightav' => null, + 'wrongimage' => null, + 'wrongav' => null + ); $fields = array('title'); $validation = array(); @@ -289,7 +311,7 @@ // Check mimetype $mediaMimetype = null; $icon['mimetype'] = \hhu\z\Utils::getMimetype($icon['tmp_name'], $icon['type']); - foreach($mimetypes as &$mimetype) { + foreach($iconMimetypes as &$mimetype) { if($mimetype['mimetype'] == $icon['mimetype']) { $mediaMimetype = $mimetype; break; @@ -303,6 +325,51 @@ } } + // 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) { @@ -335,6 +402,50 @@ } } + // 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( @@ -369,7 +480,9 @@ $this->set('latitude', $latitude); $this->set('righttext', $rightText); $this->set('wrongtext', $wrongText); - $this->set('mimetypes', $mimetypes); + $this->set('iconMimetypes', $iconMimetypes); + $this->set('imageMimetypes', $imageMimetypes); + $this->set('avMimetypes', $avMimetypes); $this->set('stationtypes', $stationtypes); $this->set('validation', $validation); } @@ -400,6 +513,20 @@ // 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) { @@ -407,7 +534,9 @@ } // Get allowed mimetypes - $mimetypes = \nre\configs\AppConfig::$mimetypes['icons']; + $iconMimetypes = \nre\configs\AppConfig::$mimetypes['icons']; + $imageMimetypes = \nre\configs\AppConfig::$mimetypes['stationimages']; + $avMimetypes = \nre\configs\AppConfig::$mimetypes['stationavs']; // Values $title = $station['title']; @@ -417,6 +546,12 @@ $latitude = $station['latitude']; $rightText = $station['righttext']; $wrongText = $station['wrongtext']; + $textMedia = array( + 'rightimage' => null, + 'rightav' => null, + 'wrongimage' => null, + 'wrongav' => null + ); $fields = array('title'); $validation = array(); @@ -463,7 +598,7 @@ // Check mimetype $mediaMimetype = null; $icon['mimetype'] = \hhu\z\Utils::getMimetype($icon['tmp_name'], $icon['type']); - foreach($mimetypes as &$mimetype) { + foreach($iconMimetypes as &$mimetype) { if($mimetype['mimetype'] == $icon['mimetype']) { $mediaMimetype = $mimetype; break; @@ -477,6 +612,51 @@ } } + // 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) { @@ -509,6 +689,50 @@ } } + // 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 @@ -555,6 +779,7 @@ $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); @@ -562,14 +787,16 @@ $this->set('latitude', $latitude); $this->set('righttext', $rightText); $this->set('wrongtext', $wrongText); - $this->set('mimetypes', $mimetypes); + $this->set('iconMimetypes', $iconMimetypes); + $this->set('imageMimetypes', $imageMimetypes); + $this->set('avMimetypes', $avMimetypes); $this->set('stationtypes', $stationtypes); $this->set('validation', $validation); } /** - * TODO Action: edittask. + * Action: edittask. * * Edit the task of a Character groups Quest Station. * diff --git a/db/create.sql b/db/create.sql index cb2f4b87..1fe09b2e 100644 --- a/db/create.sql +++ b/db/create.sql @@ -590,15 +590,27 @@ CREATE TABLE `charactergroupsqueststations` ( `longitude` decimal(10,6) DEFAULT NULL, `righttext` text COLLATE utf8mb4_unicode_ci NOT NULL, `wrongtext` text COLLATE utf8mb4_unicode_ci NOT NULL, + `rightimage_id` int(11) DEFAULT NULL, + `rightav_id` int(11) DEFAULT NULL, + `wrongimage_id` int(11) DEFAULT NULL, + `wrongav_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `charactergroupsquest_id_2` (`charactergroupsquest_id`,`url`), UNIQUE KEY `charactergroupsquest_id_3` (`charactergroupsquest_id`,`pos`), KEY `charactergroupsquest_id` (`charactergroupsquest_id`), KEY `charactergroupsqueststationtype_id` (`stationtype_id`), KEY `stationpicture_id` (`stationpicture_id`) USING BTREE, + KEY `rightimage_id` (`rightimage_id`), + KEY `rightav_id` (`rightav_id`), + KEY `wrongimage_id` (`wrongimage_id`), + KEY `wrongav_id` (`wrongav_id`), CONSTRAINT `charactergroupsqueststations_ibfk_1` FOREIGN KEY (`charactergroupsquest_id`) REFERENCES `charactergroupsquests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `charactergroupsqueststations_ibfk_2` FOREIGN KEY (`stationpicture_id`) REFERENCES `seminarymedia` (`id`) ON DELETE SET NULL ON UPDATE SET NULL, - CONSTRAINT `charactergroupsqueststations_ibfk_3` FOREIGN KEY (`stationtype_id`) REFERENCES `stationtypes` (`id`) + CONSTRAINT `charactergroupsqueststations_ibfk_3` FOREIGN KEY (`stationtype_id`) REFERENCES `stationtypes` (`id`), + CONSTRAINT `charactergroupsqueststations_ibfk_4` FOREIGN KEY (`rightimage_id`) REFERENCES `seminarymedia` (`id`) ON DELETE SET NULL ON UPDATE SET NULL, + CONSTRAINT `charactergroupsqueststations_ibfk_5` FOREIGN KEY (`rightav_id`) REFERENCES `seminarymedia` (`id`) ON DELETE SET NULL ON UPDATE SET NULL, + CONSTRAINT `charactergroupsqueststations_ibfk_6` FOREIGN KEY (`wrongimage_id`) REFERENCES `seminarymedia` (`id`) ON DELETE SET NULL ON UPDATE SET NULL, + CONSTRAINT `charactergroupsqueststations_ibfk_7` FOREIGN KEY (`wrongav_id`) REFERENCES `seminarymedia` (`id`) ON DELETE SET NULL ON UPDATE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; diff --git a/models/CharactergroupsqueststationsModel.inc b/models/CharactergroupsqueststationsModel.inc index 8c28865a..7aee5f95 100644 --- a/models/CharactergroupsqueststationsModel.inc +++ b/models/CharactergroupsqueststationsModel.inc @@ -63,7 +63,7 @@ public function getStationById($stationId) { $data = $this->db->query( - 'SELECT id, charactergroupsquest_id, stationtype_id, title, url, stationpicture_id, prolog, task, latitude, longitude, righttext, wrongtext '. + 'SELECT id, charactergroupsquest_id, stationtype_id, title, url, stationpicture_id, prolog, task, latitude, longitude, righttext, wrongtext, rightimage_id, rightav_id, wrongimage_id, wrongav_id '. 'FROM charactergroupsqueststations '. 'WHERE id = ?', 'i', @@ -88,7 +88,7 @@ public function getStationByUrl($groupsquestId, $stationUrl) { $data = $this->db->query( - 'SELECT id, charactergroupsquest_id, stationtype_id, title, url, stationpicture_id, prolog, task, latitude, longitude, righttext, wrongtext '. + 'SELECT id, charactergroupsquest_id, stationtype_id, title, url, stationpicture_id, prolog, task, latitude, longitude, righttext, wrongtext, rightimage_id, rightav_id, wrongimage_id, wrongav_id '. 'FROM charactergroupsqueststations '. 'WHERE charactergroupsquest_id = ? AND url = ?', 'is', @@ -296,7 +296,7 @@ * @param int $stationId ID of Station to upload media for * @param int $mediaId ID of Station media */ - public function setPictureForStation($station, $mediaId) + public function setPictureForStation($stationId, $mediaId) { $this->db->query( 'UPDATE charactergroupsqueststations '. @@ -304,11 +304,59 @@ 'WHERE id = ?', 'ii', $mediaId, - $station + $stationId ); } + /** + * Set the image for the right-text. + * + * @param int $stationId ID of Station to set image for + * @param int $mediaId ID of Seminary media to set + */ + public function setRightImageForStation($stationId, $mediaId) + { + $this->setMediaForStation($stationId, $mediaId, 'rightimage_id'); + } + + + /** + * Set the audio/video for the right-text. + * + * @param int $stationId ID of Station to set audio/video for + * @param int $mediaId ID of Seminary media to set + */ + public function setRightAVForStation($stationId, $mediaId) + { + $this->setMediaForStation($stationId, $mediaId, 'rightav_id'); + } + + + /** + * Set the image for the wrong-text. + * + * @param int $stationId ID of Station to set image for + * @param int $mediaId ID of Seminary media to set + */ + public function setWrongImageForStation($stationId, $mediaId) + { + $this->setMediaForStation($stationId, $mediaId, 'wrongimage_id'); + } + + + /** + * Set the audio/video for the wrong-text. + * + * @param int $stationId ID of Station to set audio/video for + * @param int $mediaId ID of Seminary media to set + */ + public function setWrongAvForStation($stationId, $mediaId) + { + $this->setMediaForStation($stationId, $mediaId, 'wrongav_id'); + } + + /** * Create a new Character groups Quest Station. * @@ -437,6 +485,29 @@ ); } + + /** + * Add a medium for a Character groups Quest Station. + * + * @param int $stationId ID of Station to upload media for + * @param int $mediaId ID of Seminary media + * @param string $field Field name to set media to + */ + private function setMediaForStation($stationId, $mediaId, $field) + { + $this->db->query( + sprintf( + 'UPDATE charactergroupsqueststations '. + 'SET %s = ? '. + 'WHERE id = ?', + $field + ), + 'ii', + $mediaId, + $stationId + ); + } + } ?> diff --git a/models/MediaModel.inc b/models/MediaModel.inc index 61125d36..b4ae0bec 100644 --- a/models/MediaModel.inc +++ b/models/MediaModel.inc @@ -557,6 +557,46 @@ } + /** + * Create a new Character groups Quest Station media by creating a new + * Seminarymedia . + * + * @param int $userId ID of user that does the upload + * @param int $seminaryId ID of Seminary + * @param string $filename Filename of uploading media + * @param string $description Description for media + * @param string $mimetype Mimetype of media + * @param string $tmpFilename Name of temporary uploaded file + * @return mixed ID of media record or false if upload failed + */ + public function createStationMedia($userId, $seminaryId, $filename, $description, $mimetype, $tmpFilename) + { + $mediaId = false; + $this->db->setAutocommit(false); + + try { + // Create Seminary media record + $mediaId = $this->createSeminaryMedia($userId, $seminaryId, $filename, $description, $mimetype); + + // Create filename + $filename = ROOT.DS.\nre\configs\AppConfig::$dirs['seminarymedia'].DS.$mediaId; + if(!move_uploaded_file($tmpFilename, $filename)) + { + $this->db->rollback(); + $mediaId = false; + } + } + catch(\nre\exceptions\DatamodelException $e) { + $this->db->rollback(); + $this->db->setAutocommit(true); + } + + + $this->db->setAutocommit(true); + return $mediaId; + } + + /** * Create a new Achievement media by creating a new Seminarymedia and * adding it to the list of media for Achievements. diff --git a/views/html/charactergroupsqueststations/create.tpl b/views/html/charactergroupsqueststations/create.tpl index c46c3374..cdca96b8 100644 --- a/views/html/charactergroupsqueststations/create.tpl +++ b/views/html/charactergroupsqueststations/create.tpl @@ -39,6 +39,20 @@ default: echo _('Title invalid'); } break; + case 'rightimage': + case 'rightav': + case 'wrongimage': + case 'wrongav': + switch($setting) { + case 'error': printf(_('Error during file upload: %s'), $value); + break; + case 'mimetype': printf(_('File has wrong type “%s”'), $value); + break; + case 'size': echo _('File exceeds size maximum'); + break; + default: echo _('File invalid'); + } + break; } ?> @@ -53,7 +67,7 @@

:

@@ -65,6 +79,9 @@
+ + + /> -
-
- - />



+
+
+ + + + +

+
+
+ + + + +

diff --git a/views/html/charactergroupsqueststations/edit.tpl b/views/html/charactergroupsqueststations/edit.tpl index 08c96a0e..e7466776 100644 --- a/views/html/charactergroupsqueststations/edit.tpl +++ b/views/html/charactergroupsqueststations/edit.tpl @@ -39,6 +39,20 @@ default: echo _('Title invalid'); } break; + case 'rightimage': + case 'rightav': + case 'wrongimage': + case 'wrongav': + switch($setting) { + case 'error': printf(_('Error during file upload: %s'), $value); + break; + case 'mimetype': printf(_('File has wrong type “%s”'), $value); + break; + case 'size': echo _('File exceeds size maximum'); + break; + default: echo _('File invalid'); + } + break; } ?> @@ -53,7 +67,7 @@

:

@@ -65,6 +79,9 @@
+ + + /> -
-
- - />



+
+ + + + + + +
+ +
+ +
    + +
  • 0) : ?>(  MiB)
  • + +
+ + + + + + + +
+ + +
    + +
  • 0) : ?>(  MiB)
  • + +


+
+
+ + + + + + +
+ +
+ +
    + +
  • 0) : ?>(  MiB)
  • + +
+ + + + + + + +
+ + +
    + +
  • 0) : ?>(  MiB)
  • + +


diff --git a/views/html/charactergroupsqueststations/station.tpl b/views/html/charactergroupsqueststations/station.tpl index 03bf37db..d773a8ff 100644 --- a/views/html/charactergroupsqueststations/station.tpl +++ b/views/html/charactergroupsqueststations/station.tpl @@ -122,10 +122,34 @@
+ + + +
+ + + + + + + + t($station['righttext'])?>
+ + + +
+ + + + + + + + t($station['wrongtext'])?>