diff --git a/configs/AppConfig.inc b/configs/AppConfig.inc index 9e18237b..c7ba2a0e 100644 --- a/configs/AppConfig.inc +++ b/configs/AppConfig.inc @@ -135,6 +135,16 @@ 'mimetype' => 'image/jpeg', 'size' => 1048576 ) + ), + 'questtypes' => array( + array( + 'mimetype' => 'image/jpeg', + 'size' => 524288 + ), + array( + 'mimetype' => 'image/png', + 'size' => 1048576 + ) ) ); diff --git a/locale/de_DE/LC_MESSAGES/The Legend of Z.mo b/locale/de_DE/LC_MESSAGES/The Legend of Z.mo index 0c8ddb68..6d7cf414 100644 Binary files a/locale/de_DE/LC_MESSAGES/The Legend of Z.mo and b/locale/de_DE/LC_MESSAGES/The Legend of Z.mo differ diff --git a/locale/de_DE/LC_MESSAGES/The Legend of Z.po b/locale/de_DE/LC_MESSAGES/The Legend of Z.po index 96ddc4a2..0e9da04f 100644 --- a/locale/de_DE/LC_MESSAGES/The Legend of Z.po +++ b/locale/de_DE/LC_MESSAGES/The Legend of Z.po @@ -1,8 +1,8 @@ msgid "" msgstr "" "Project-Id-Version: The Legend of Z\n" -"POT-Creation-Date: 2015-04-19 14:15+0100\n" -"PO-Revision-Date: 2015-04-20 21:06+0100\n" +"POT-Creation-Date: 2015-04-30 18:44+0100\n" +"PO-Revision-Date: 2015-04-30 18:45+0100\n" "Last-Translator: \n" "Language-Team: \n" "Language: de_DE\n" @@ -18,31 +18,31 @@ msgstr "" "X-Poedit-SearchPath-2: controllers\n" #: controllers/CharactersController.inc:402 -#: controllers/UsersController.inc:333 views/ajax/characters/index.tpl:10 +#: controllers/UsersController.inc:339 views/ajax/characters/index.tpl:10 #: views/ajax/users/index.tpl:10 views/html/characters/index.tpl:48 #: views/html/characters/manage.tpl:35 views/html/characters/manage.tpl:50 #: views/html/characters/manage.tpl:58 views/html/userroles/user.tpl:5 -#: views/html/users/index.tpl:29 views/html/users/manage.tpl:28 +#: views/html/users/index.tpl:33 views/html/users/manage.tpl:28 #: views/html/users/manage.tpl:41 views/html/users/manage.tpl:49 msgid "Admin" msgstr "Administrator" #: controllers/CharactersController.inc:408 -#: controllers/UsersController.inc:339 views/ajax/characters/index.tpl:12 +#: controllers/UsersController.inc:345 views/ajax/characters/index.tpl:12 #: views/ajax/users/index.tpl:12 views/html/characters/index.tpl:49 #: views/html/characters/manage.tpl:36 views/html/characters/manage.tpl:51 #: views/html/characters/manage.tpl:59 views/html/userroles/user.tpl:7 -#: views/html/users/index.tpl:30 views/html/users/manage.tpl:29 +#: views/html/users/index.tpl:34 views/html/users/manage.tpl:29 #: views/html/users/manage.tpl:42 views/html/users/manage.tpl:50 msgid "Moderator" msgstr "Moderator" #: controllers/CharactersController.inc:414 -#: controllers/UsersController.inc:345 views/ajax/characters/index.tpl:14 +#: controllers/UsersController.inc:351 views/ajax/characters/index.tpl:14 #: views/ajax/users/index.tpl:14 views/html/characters/index.tpl:50 #: views/html/characters/manage.tpl:37 views/html/characters/manage.tpl:53 #: views/html/characters/manage.tpl:61 views/html/userroles/user.tpl:9 -#: views/html/users/index.tpl:31 views/html/users/manage.tpl:30 +#: views/html/users/index.tpl:35 views/html/users/manage.tpl:30 #: views/html/users/manage.tpl:44 views/html/users/manage.tpl:52 msgid "User" msgstr "Benutzer" @@ -101,6 +101,7 @@ msgstr "" #: questtypes/bossfight/html/edittask.tpl:17 #: questtypes/choiceinput/html/edittask.tpl:83 +#: questtypes/dragndrop/html/edittask.tpl:144 #: questtypes/multiplechoice/html/edittask.tpl:79 #: questtypes/textinput/html/edittask.tpl:44 #: views/html/achievements/conditions.tpl:151 @@ -190,6 +191,137 @@ msgstr "vertikal" msgid "horizontal" msgstr "horizontal" +#: questtypes/dragndrop/html/edittask.tpl:11 +#: questtypes/dragndrop/html/edittask.tpl:104 +#: questtypes/submit/html/quest.tpl:8 +#: views/html/charactergroupsquests/manage.tpl:37 +#, php-format +msgid "Error during file upload: %s" +msgstr "Fehler beim Dateiupload: %s" + +#: questtypes/dragndrop/html/edittask.tpl:13 +#: questtypes/dragndrop/html/edittask.tpl:106 +#: questtypes/submit/html/quest.tpl:4 +#: views/html/charactergroupsquests/manage.tpl:39 +#, php-format +msgid "File has wrong type “%s”" +msgstr "Der Dateityp „%s“ ist nicht erlaubt" + +#: questtypes/dragndrop/html/edittask.tpl:15 +#: questtypes/dragndrop/html/edittask.tpl:108 +#: questtypes/submit/html/quest.tpl:6 +#: views/html/charactergroupsquests/manage.tpl:41 +msgid "File exceeds size maximum" +msgstr "Die Datei ist zu groß" + +#: questtypes/dragndrop/html/edittask.tpl:17 +#: questtypes/dragndrop/html/edittask.tpl:110 +#: views/html/charactergroupsquests/manage.tpl:43 +msgid "File invalid" +msgstr "Die Datei ist ungültig" + +#: questtypes/dragndrop/html/edittask.tpl:31 +#, php-format +msgid "Step %d" +msgstr "Schritt %d" + +#: questtypes/dragndrop/html/edittask.tpl:31 +#: views/html/achievements/conditions.tpl:22 +#: views/html/achievements/conditions.tpl:31 +#: views/html/achievements/conditions.tpl:59 +#: views/html/achievements/conditions.tpl:89 +#: views/html/achievements/conditions.tpl:98 +#: views/html/achievements/conditions.tpl:126 +msgid "Field" +msgstr "Feld" + +#: questtypes/dragndrop/html/edittask.tpl:36 +msgid "Upload background image" +msgstr "Hintergrundbild hochladen" + +#: questtypes/dragndrop/html/edittask.tpl:38 +#: questtypes/dragndrop/html/edittask.tpl:136 +#: questtypes/submit/html/quest.tpl:17 views/html/achievements/create.tpl:66 +#: views/html/achievements/edit.tpl:68 +#: views/html/charactergroups/creategroup.tpl:64 +#: views/html/charactergroups/editgroup.tpl:65 +#: views/html/charactergroupsquests/create.tpl:66 +#: views/html/charactergroupsquests/edit.tpl:67 +#: views/html/charactergroupsquests/manage.tpl:72 +#: views/html/charactertypes/edit.tpl:135 views/html/questgroups/create.tpl:65 +#: views/html/questgroups/edit.tpl:53 views/html/quests/create.tpl:62 +#: views/html/quests/edit.tpl:65 views/html/seminaries/create.tpl:60 +#: views/html/seminaries/edit.tpl:62 +msgid "Allowed file types" +msgstr "Erlaubte Dateiformate" + +#: questtypes/dragndrop/html/edittask.tpl:41 +#: questtypes/dragndrop/html/edittask.tpl:139 +#: questtypes/submit/html/quest.tpl:20 views/html/achievements/create.tpl:69 +#: views/html/achievements/edit.tpl:71 +#: views/html/charactergroups/creategroup.tpl:67 +#: views/html/charactergroups/editgroup.tpl:68 +#: views/html/charactergroupsquests/create.tpl:69 +#: views/html/charactergroupsquests/edit.tpl:70 +#: views/html/charactergroupsquests/manage.tpl:72 +#: views/html/charactertypes/edit.tpl:138 views/html/questgroups/create.tpl:68 +#: views/html/questgroups/edit.tpl:56 views/html/quests/create.tpl:65 +#: views/html/quests/edit.tpl:68 views/html/seminaries/create.tpl:63 +#: views/html/seminaries/edit.tpl:65 +#, php-format +msgid "%s-files" +msgstr "%s-Dateien" + +#: questtypes/dragndrop/html/edittask.tpl:41 +#: questtypes/dragndrop/html/edittask.tpl:139 +#: questtypes/submit/html/quest.tpl:20 views/html/achievements/create.tpl:69 +#: views/html/achievements/edit.tpl:71 +#: views/html/charactergroups/creategroup.tpl:67 +#: views/html/charactergroups/editgroup.tpl:68 +#: views/html/charactergroupsquests/create.tpl:69 +#: views/html/charactergroupsquests/edit.tpl:70 +#: views/html/charactergroupsquests/manage.tpl:72 +#: views/html/charactertypes/edit.tpl:138 views/html/questgroups/create.tpl:68 +#: views/html/questgroups/edit.tpl:56 views/html/quests/create.tpl:65 +#: views/html/quests/edit.tpl:68 views/html/seminaries/create.tpl:63 +#: views/html/seminaries/edit.tpl:65 +msgid "max." +msgstr "max." + +#: questtypes/dragndrop/html/edittask.tpl:45 +#: questtypes/dragndrop/html/edittask.tpl:80 +msgid "next step" +msgstr "nächster Schritt" + +#: questtypes/dragndrop/html/edittask.tpl:48 +#: questtypes/dragndrop/html/edittask.tpl:83 +#, php-format +msgid "Step %d:" +msgstr "Schritt %d" + +#: questtypes/dragndrop/html/edittask.tpl:48 +msgid "Drop-items" +msgstr "Drop-Elemente" + +#: questtypes/dragndrop/html/edittask.tpl:63 +#: questtypes/dragndrop/html/edittask.tpl:184 +msgid "Size" +msgstr "Größe" + +#: questtypes/dragndrop/html/edittask.tpl:67 +#: questtypes/dragndrop/html/edittask.tpl:188 +msgid "Position" +msgstr "Position" + +#: questtypes/dragndrop/html/edittask.tpl:79 +#: questtypes/dragndrop/html/edittask.tpl:143 +msgid "previous step" +msgstr "vorheriger Schritt" + +#: questtypes/dragndrop/html/edittask.tpl:83 +msgid "Drag-items" +msgstr "Drag-Elemente" + #: questtypes/multiplechoice/html/edittask.tpl:12 #: questtypes/multiplechoice/html/edittask.tpl:44 #, php-format @@ -234,66 +366,6 @@ msgstr "Frage %d von %d" msgid "solve Question" msgstr "Frage lösen" -#: questtypes/submit/html/quest.tpl:4 -#: views/html/charactergroupsquests/manage.tpl:39 -#, php-format -msgid "File has wrong type “%s”" -msgstr "Der Dateityp „%s“ ist nicht erlaubt" - -#: questtypes/submit/html/quest.tpl:6 -#: views/html/charactergroupsquests/manage.tpl:41 -msgid "File exceeds size maximum" -msgstr "Die Datei ist zu groß" - -#: questtypes/submit/html/quest.tpl:8 -#: views/html/charactergroupsquests/manage.tpl:37 -#, php-format -msgid "Error during file upload: %s" -msgstr "Fehler beim Dateiupload: %s" - -#: questtypes/submit/html/quest.tpl:17 views/html/achievements/create.tpl:66 -#: views/html/achievements/edit.tpl:68 -#: views/html/charactergroups/creategroup.tpl:64 -#: views/html/charactergroups/editgroup.tpl:65 -#: views/html/charactergroupsquests/create.tpl:66 -#: views/html/charactergroupsquests/edit.tpl:67 -#: views/html/charactergroupsquests/manage.tpl:72 -#: views/html/charactertypes/edit.tpl:135 views/html/questgroups/create.tpl:65 -#: views/html/questgroups/edit.tpl:53 views/html/quests/create.tpl:62 -#: views/html/quests/edit.tpl:65 views/html/seminaries/create.tpl:60 -#: views/html/seminaries/edit.tpl:62 -msgid "Allowed file types" -msgstr "Erlaubte Dateiformate" - -#: questtypes/submit/html/quest.tpl:20 views/html/achievements/create.tpl:69 -#: views/html/achievements/edit.tpl:71 -#: views/html/charactergroups/creategroup.tpl:67 -#: views/html/charactergroups/editgroup.tpl:68 -#: views/html/charactergroupsquests/create.tpl:69 -#: views/html/charactergroupsquests/edit.tpl:70 -#: views/html/charactergroupsquests/manage.tpl:72 -#: views/html/charactertypes/edit.tpl:138 views/html/questgroups/create.tpl:68 -#: views/html/questgroups/edit.tpl:56 views/html/quests/create.tpl:65 -#: views/html/quests/edit.tpl:68 views/html/seminaries/create.tpl:63 -#: views/html/seminaries/edit.tpl:65 -#, php-format -msgid "%s-files" -msgstr "%s-Dateien" - -#: questtypes/submit/html/quest.tpl:20 views/html/achievements/create.tpl:69 -#: views/html/achievements/edit.tpl:71 -#: views/html/charactergroups/creategroup.tpl:67 -#: views/html/charactergroups/editgroup.tpl:68 -#: views/html/charactergroupsquests/create.tpl:69 -#: views/html/charactergroupsquests/edit.tpl:70 -#: views/html/charactergroupsquests/manage.tpl:72 -#: views/html/charactertypes/edit.tpl:138 views/html/questgroups/create.tpl:68 -#: views/html/questgroups/edit.tpl:56 views/html/quests/create.tpl:65 -#: views/html/quests/edit.tpl:68 views/html/seminaries/create.tpl:63 -#: views/html/seminaries/edit.tpl:65 -msgid "max." -msgstr "max." - #: questtypes/submit/html/quest.tpl:28 msgid "Your submissions" msgstr "Deine Lösungsvorschläge" @@ -411,7 +483,7 @@ msgstr "Texteingabe" msgid "%d XPs" msgstr "%d XP" -#: views/ajax/users/index.tpl:20 views/html/users/index.tpl:28 +#: views/ajax/users/index.tpl:20 views/html/users/index.tpl:32 #: views/html/users/manage.tpl:31 views/html/users/user.tpl:22 #, php-format msgid "registered on %s" @@ -443,15 +515,6 @@ msgstr "Bedingungen" msgid "Date" msgstr "Datum" -#: views/html/achievements/conditions.tpl:22 -#: views/html/achievements/conditions.tpl:31 -#: views/html/achievements/conditions.tpl:59 -#: views/html/achievements/conditions.tpl:89 -#: views/html/achievements/conditions.tpl:98 -#: views/html/achievements/conditions.tpl:126 -msgid "Field" -msgstr "Feld" - #: views/html/achievements/conditions.tpl:28 #: views/html/achievements/conditions.tpl:29 #: views/html/achievements/conditions.tpl:39 @@ -973,6 +1036,7 @@ msgstr "Icon" #: views/html/charactertypes/create.tpl:41 #: views/html/charactertypes/create.tpl:42 #: views/html/charactertypes/edit.tpl:41 views/html/charactertypes/edit.tpl:42 +#: views/html/users/index.tpl:21 views/html/users/index.tpl:22 #: views/html/users/user.tpl:23 msgid "Name" msgstr "Name" @@ -1193,10 +1257,6 @@ msgstr "%s-Quest bearbeiten" msgid "Media" msgstr "Medien" -#: views/html/charactergroupsquests/manage.tpl:43 -msgid "File invalid" -msgstr "Die Datei ist ungültig" - #: views/html/charactergroupsquests/manage.tpl:68 msgid "delete selected" msgstr "markierte löschen" @@ -1345,17 +1405,17 @@ msgid "Date of registration" msgstr "Registrierungsdatum" #: views/html/characters/index.tpl:37 views/html/quests/index.tpl:50 -#: views/html/users/index.tpl:22 +#: views/html/users/index.tpl:26 msgid "Apply filters" msgstr "Filter anwenden" #: views/html/characters/index.tpl:61 views/html/quests/index.tpl:82 -#: views/html/users/index.tpl:39 +#: views/html/users/index.tpl:48 msgid "Show more" msgstr "Weitere anzeigen" #: views/html/characters/index.tpl:62 views/html/quests/index.tpl:83 -#: views/html/users/index.tpl:40 +#: views/html/users/index.tpl:49 msgid "Show all" msgstr "Alle anzeigen" @@ -2112,6 +2172,7 @@ msgstr "Nachname" #: views/html/users/create.tpl:92 views/html/users/create.tpl:93 #: views/html/users/edit.tpl:100 views/html/users/edit.tpl:101 +#: views/html/users/index.tpl:23 views/html/users/index.tpl:24 #: views/html/users/register.tpl:93 views/html/users/register.tpl:94 #: views/html/users/user.tpl:24 msgid "E‑mail address" diff --git a/models/MediaModel.inc b/models/MediaModel.inc index 826641c7..cb0e4004 100644 --- a/models/MediaModel.inc +++ b/models/MediaModel.inc @@ -580,7 +580,6 @@ } - /** * Copy Achievement media. * @@ -602,6 +601,32 @@ $userId ); } + + + /** + * Gather some information about a Seminary medium. + * + * The infos are organized in an associative array and contain the + * following keys: width and height + * + * @param int $seminarymediaId ID of Seminary media to get infos for + * @return array List of infos for Seminary medium + */ + public function getSeminarymediaInfos($seminarymediaId) + { + // Create filename + $filename = ROOT.DS.\nre\configs\AppConfig::$dirs['seminarymedia'].DS.$seminarymediaId; + + // Get infos + $infos = array(); + $im = new \Imagick($filename); + $infos['width'] = $im->getImageWidth(); + $infos['height'] = $im->getImageHeight(); + + + // Return infos + return $infos; + } diff --git a/questtypes/dragndrop/DragndropQuesttypeController.inc b/questtypes/dragndrop/DragndropQuesttypeController.inc index b2569237..0bf08a41 100644 --- a/questtypes/dragndrop/DragndropQuesttypeController.inc +++ b/questtypes/dragndrop/DragndropQuesttypeController.inc @@ -25,6 +25,12 @@ * @var array */ public $models = array('media'); + /** + * Required components + * + * @var array + */ + public $components = array('validation'); @@ -222,19 +228,304 @@ } - /** - * TODO 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 function edittask($seminary, $questgroup, $quest) - { - } - - } + /** + * 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 function edittask($seminary, $questgroup, $quest) + { + // Get Drag&Drop field + $dndField = $this->Dragndrop->getDragndrop($quest['id']); + if(!is_null($dndField)) { + $dndField['media'] = $this->Media->getSeminaryMediaById($dndField['questmedia_id']); + } + + // Get Drop-items + $drops = array(); + if(!is_null($dndField)) { + $drops = $this->Dragndrop->getDrops($dndField['quest_id']); + } + + // Get Drag-items + $drags = array(); + if(!is_null($dndField)) + { + $drags = $this->Dragndrop->getDrags($dndField['quest_id']); + foreach($drags as &$drag) + { + $drag['media'] = $this->Media->getSeminaryMediaById($drag['questmedia_id']); + $drag['drops'] = array_map(function($d) { return $d['id']; }, $this->Dragndrop->getDropsForDrag($drag['id'])); + } + } + + // Get allowed mimetypes + $mimetypes = \nre\configs\AppConfig::$mimetypes['questtypes']; + + // Values + $step = 0; + $steps = 2; + $validation = true; + $dragsValidation = true; + + // Save submitted data + if($this->request->getRequestMethod() == 'POST') + { + // Get current step + $step = max(0, min(2, intval($this->request->getPostParam('step')))); + + // Drag&Drop-field (background image) + if($step == 0) + { + // Validate zone + $zone = null; + if(!empty($_FILES) && array_key_exists('zone', $_FILES) && $_FILES['zone']['error'] != UPLOAD_ERR_NO_FILE) + { + $zone = $_FILES['zone']; + + // Check error + if($zone['error'] !== UPLOAD_ERR_OK) { + $validation = $this->Validation->addValidationResult($validation, 'zone', 'error', $zone['error']); + } + + // Check mimetype + $mediaMimetype = null; + $zone['mimetype'] = \hhu\z\Utils::getMimetype($zone['tmp_name'], $zone['type']); + foreach($mimetypes as &$mimetype) { + if($mimetype['mimetype'] == $zone['mimetype']) { + $mediaMimetype = $mimetype; + break; + } + } + if(is_null($mediaMimetype)) { + $validation = $this->Validation->addValidationResult($validation, 'zone', 'mimetype', $zone['mimetype']); + } + elseif($zone['size'] > $mediaMimetype['size']) { + $validation = $this->Validation->addValidationResult($validation, 'zone', 'size', $mediaMimetype['size']); + } + } + + // Set zone + if($validation === true) + { + // Upload background image + $mediaId = $this->Media->createQuestMedia( + $this->Auth->getUserId(), + $seminary['id'], + sprintf('quest-dnd-%s', $quest['url']), + '', + $zone['mimetype'], + $zone['tmp_name'] + ); + if($mediaId !== false) + { + // Create Drag&Drop zone + $this->Dragndrop->createDragndrop( + $this->Auth->getUserId(), + $quest['id'], + $mediaId + ); + } + + // Reload Drag&Drop field + $dndField = $this->Dragndrop->getDragndrop($quest['id']); + $dndField['media'] = $this->Media->getSeminaryMediaById($dndField['questmedia_id']); + } + } + // Drop-items + elseif($step == 1) + { + // Get new Drop-items and organize them + $dropsNew = array(); + $dropsUpdate = array(); + foreach($this->request->getPostParam('drops') as $drop) + { + if(array_key_exists('id', $drop)) { + $dropsUpdate[$drop['id']] = $drop; + } + else { + $dropsNew[] = $drop; + } + } + + // Update Drop-items + foreach($drops as $drop) + { + if(array_key_exists($drop['id'], $dropsUpdate)) + { + $drop = $dropsUpdate[$drop['id']]; + $this->Dragndrop->editDrop($drop['id'], $drop['width'], $drop['height'], $drop['x'], $drop['y']); + } + else { + $this->Dragndrop->deleteDrop($drop['id']); + } + } + // Add new Drop-items + foreach($dropsNew as $drop) { + $this->Dragndrop->addDrop($dndField['quest_id'], $drop['width'], $drop['height'], $drop['x'], $drop['y']); + } + + // Reload Drop-items + $drops = $this->Dragndrop->getDrops($dndField['quest_id']); + } + // Drag-items + elseif($step == 2) + { + // Get new Drop-items and organize them + $dragsNew = array(); + $dragsUpdate = array(); + foreach($this->request->getPostParam('drags') as $index => $drag) + { + // Get file + if(array_key_exists($index, $_FILES['drags']['error']) && $_FILES['drags']['error'][$index] !== UPLOAD_ERR_NO_FILE) + { + $drag['file'] = array( + 'name' => $_FILES['drags']['name'][$index], + 'type' => $_FILES['drags']['type'][$index], + 'tmp_name' => $_FILES['drags']['tmp_name'][$index], + 'error' => $_FILES['drags']['error'][$index], + 'size' => $_FILES['drags']['size'][$index] + ); + + // Validate file + $dragValidation = true; + + // Check error + if($drag['file']['error'] !== UPLOAD_ERR_OK) { + $dragValidation = $this->Validation->addValidationResult($dragValidation, 'file', 'error', $drag['file']['error']); + } + + // Check mimetype + $dragMimetype = null; + $drag['file']['mimetype'] = \hhu\z\Utils::getMimetype($drag['file']['tmp_name'], $drag['file']['type']); + foreach($mimetypes as &$mimetype) { + if($mimetype['mimetype'] == $drag['file']['mimetype']) { + $dragMimetype = $mimetype; + break; + } + } + if(is_null($dragMimetype)) { + $dragValidation = $this->Validation->addValidationResult($dragValidation, 'file', 'mimetype', $drag['file']['mimetype']); + } + elseif($drag['file']['size'] > $dragMimetype['size']) { + $dragValidation = $this->Validation->addValidationResult($dragValidation, 'file', 'size', $dragMimetype['size']); + } + + // Add validation result + if($dragValidation !== true) + { + if(!is_array($dragsValidation)) { + $dragsValidation = array(); + } + $dragsValidation[$index] = $dragValidation; + } + + // Upload Drag-item file + if($dragValidation === true) + { + $drag['file']['media_id'] = $this->Media->createQuestMedia( + $this->Auth->getUserId(), + $seminary['id'], + sprintf('quest-dnd-%s-%s', substr($quest['url'], 0, 25), substr($drag['file']['name'], 0, 25)), + '', + $drag['file']['mimetype'], + $drag['file']['tmp_name'] + ); + } + } + + // Add to array + if(array_key_exists('id', $drag)) { + $dragsUpdate[$drag['id']] = $drag; + } + else { + $dragsNew[] = $drag; + } + } + + // Update Drag-items + foreach($drags as $drag) + { + if(array_key_exists($drag['id'], $dragsUpdate)) + { + $drag = $dragsUpdate[$drag['id']]; + // Edit Drag-items + if(array_key_exists('file', $drag) && array_key_exists('media_id', $drag['file'])) { + $this->Dragndrop->editDrag($drag['id'], $drag['file']['media_id']); + } + // Set correct Drop-items for Drag-item + $this->Dragndrop->setDropsForDrag( + $this->Auth->getUserId(), + $drag['id'], + array_key_exists('drops', $drag) ? array_keys($drag['drops']) : array() + ); + } + else { + $this->Dragndrop->deleteDrag($drag['id']); + } + } + // Add new Drag-items + foreach($dragsNew as $drag) + { + if(array_key_exists('file', $drag) && array_key_exists('media_id', $drag['file'])) + { + // Create Drag-item + $dragId = $this->Dragndrop->addDrag($dndField['quest_id'], $drag['file']['media_id']); + + // Set Drop-items for Drag-item + $this->Dragndrop->setDropsForDrag( + $this->Auth->getUserId(), + $dragId, + array_key_exists('drops', $drag) ? array_keys($drag['drops']) : array() + ); + } + } + + // Reload Drag-items + $drags = $this->Dragndrop->getDrags($dndField['quest_id']); + foreach($drags as &$drag) + { + $drag['media'] = $this->Media->getSeminaryMediaById($drag['questmedia_id']); + $drag['drops'] = array_map(function($d) { return $d['id']; }, $this->Dragndrop->getDropsForDrag($drag['id'])); + } + } + + // Go to next/previous step + if(!is_null($this->request->getPostParam('prev'))) { + $step--; + } + else { + $step++; + } + + // Redirect after last step + if($step > $steps) + { + if($validation === true && $dragsValidation === true) { + $this->redirect($this->linker->link(array('quest', $seminary['url'], $questgroup['url'], $quest['url']), 1)); + } + else { + $step--; + } + } + } + + + // Pass data to view + $this->set('seminary', $seminary); + $this->set('zone', $dndField); + $this->set('drops', $drops); + $this->set('drags', $drags); + $this->set('mimetypes', $mimetypes); + $this->set('step', $step); + $this->set('validation', $validation); + $this->set('dragsValidation', $dragsValidation); + } + + } ?> diff --git a/questtypes/dragndrop/DragndropQuesttypeModel.inc b/questtypes/dragndrop/DragndropQuesttypeModel.inc index cc3db7e0..9806cce8 100644 --- a/questtypes/dragndrop/DragndropQuesttypeModel.inc +++ b/questtypes/dragndrop/DragndropQuesttypeModel.inc @@ -128,6 +128,33 @@ + /** + * Create a new Drag&Drop field for a Quest. + * + * @param int $userId ID of creating user + * @param int $questId ID of Quest to create Drag&Drop field for + * @param int $questmediaId ID of Questmedia to use for the field + */ + public function createDragndrop($userId, $questId, $questmediaId) + { + // Get image measurements + $infos = $this->Media->getSeminarymediaInfos($questmediaId); + + // Create field + $this->db->query( + 'INSERT INTO questtypes_dragndrop '. + '(quest_id, created_user_id, questmedia_id, width, height) '. + 'VALUES '. + '(?, ?, ?, ?, ?) '. + 'ON DUPLICATE KEY UPDATE '. + 'questmedia_id = ?, width = ?, height = ?', + 'iiiiiiii', + $questId, $userId, $questmediaId, $infos['width'], $infos['height'], + $questmediaId, $infos['width'], $infos['height'] + ); + } + + /** * Get Drag&Drop-field. * @@ -161,13 +188,151 @@ public function getDrops($dragndropId) { return $this->db->query( - 'SELECT id, top, `left`, width, height '. //, questtypes_dragndrop_drag_id '. + 'SELECT id, top, `left`, width, height '. 'FROM questtypes_dragndrop_drops '. 'WHERE questtypes_dragndrop_id = ?', 'i', $dragndropId ); } + + + /** + * Get Drop-items for a Drag-item + * + * @param int $dragId ID of Drag-item to get Drop-items for + * @return array List of Drop-items + */ + public function getDropsForDrag($dragId) + { + return $this->db->query( + 'SELECT drops.id, top, `left`, width, height '. + 'FROM questtypes_dragndrop_drops_drags AS drops_drags '. + 'INNER JOIN questtypes_dragndrop_drops AS drops ON drops.id = drops_drags.questtypes_dragndrop_drop_id '. + 'WHERE drops_drags.questtypes_dragndrop_drag_id = ?', + 'i', + $dragId + ); + } + + + /** + * Set correct Drop-items for a Drag-item. + * + * @param int $userId ID of creating user + * @param int $dragId ID of Drag-item to set Drop-items for + * @param array $dropIds List of Drop-items to set for Drag-item + */ + public function setDropsForDrag($userId, $dragId, $dropIds) + { + // Set new Drop-items + if(!empty($dropIds)) + { + $this->db->query( + sprintf( + 'INSERT INTO questtypes_dragndrop_drops_drags '. + '(questtypes_dragndrop_drop_id, questtypes_dragndrop_drag_id, created_user_id) '. + 'SELECT questtypes_dragndrop_drops.id, ?, ? '. + 'FROM questtypes_dragndrop_drops '. + 'WHERE questtypes_dragndrop_drops.questtypes_dragndrop_id = ('. + 'SELECT questtypes_dragndrop_drags.questtypes_dragndrop_id '. + 'FROM questtypes_dragndrop_drags '. + 'WHERE questtypes_dragndrop_drags.id = ?'. + ') AND questtypes_dragndrop_drops.id IN (%s) '. + 'ON DUPLICATE KEY UPDATE '. + 'questtypes_dragndrop_drops_drags.created = questtypes_dragndrop_drops_drags.created', + implode(',', array_map(function($id) { return intval($id); }, $dropIds)) + ), + 'iii', + $dragId, $userId, + $dragId + ); + + // Remove old Drop-items + $this->db->query( + sprintf( + 'DELETE FROM questtypes_dragndrop_drops_drags '. + 'WHERE questtypes_dragndrop_drag_id = ? AND questtypes_dragndrop_drop_id NOT IN (%s)', + implode(',', array_map(function($id) { return intval($id); }, $dropIds)) + ), + 'i', + $dragId + ); + } + else + { + // Remove all Drop-items + $this->db->query( + 'DELETE FROM questtypes_dragndrop_drops_drags '. + 'WHERE questtypes_dragndrop_drag_id = ?', + 'i', + $dragId + ); + } + } + + + /** + * Create a new Drop-item for a Drag&Drop-field. + * + * @param int $dragndropId ID of Drag&Drop-field to create Drop-item for + * @param int $width Width of Drop-item + * @param int $height Height of Drop-item + * @param int $x X-coordinate of Drop-item + * @param int $y Y-coordinate of Drop-item + * @return int ID of newly created Drop-item + */ + public function addDrop($dragndropId, $width, $height, $x, $y) + { + $this->db->query( + 'INSERT INTO questtypes_dragndrop_drops '. + '(questtypes_dragndrop_id, top, `left`, width, height) '. + 'VALUES '. + '(?, ?, ?, ?, ?)', + 'iiiii', + $dragndropId, $y, $x, $width, $height + ); + + return $this->db->getInsertId(); + } + + + /** + * Edit Drop-item. + * + * @param int $dropId ID of Drop-item to edit + * @param int $width New width of Drop-item + * @param int $height New height of Drop-item + * @param int $x New X-coordinate of Drop-item + * @param int $y New Y-coordinate of Drop-item + */ + public function editDrop($dropId, $width, $height, $x, $y) + { + $this->db->query( + 'UPDATE questtypes_dragndrop_drops '. + 'SET top = ?, `left` = ?, width = ?, height = ? '. + 'WHERE id = ?', + 'iiiii', + $y, $x, $width, $height, + $dropId + ); + } + + + /** + * Delete a Drop-item. + * + * @param int $dropId ID of Drop-item to delete + */ + public function deleteDrop($dropId) + { + $this->db->query( + 'DELETE FROM questtypes_dragndrop_drops '. + 'WHERE id = ?', + 'i', + $dropId + ); + } /** @@ -195,6 +360,63 @@ $dragndropId ); } + + + /** + * Create a new Drag-item. + * + * @param int $dragndropId ID of Drag&Drop-field to add Drag-item for + * @param int $questmediaId ID of Questmedia to use for this Drag-item + * @return int ID of newly created Drag-item + */ + public function addDrag($dragndropId, $questmediaId) + { + $this->db->query( + 'INSERT INTO questtypes_dragndrop_drags '. + '(questtypes_dragndrop_id, questmedia_id) '. + 'VALUES '. + '(?, ?) ', + 'ii', + $dragndropId, $questmediaId + ); + + return $this->db->getInsertId(); + } + + + /** + * Edit Drag-item. + * + * @param int $dragId ID of Drag-item to edit + * @param int $questmediaId ID of new Questmedia to use for this Drag-item + */ + public function editDrag($dragId, $questmediaId) + { + $this->db->query( + 'UPDATE questtypes_dragndrop_drags '. + 'SET questmedia_id = ? '. + 'WHERE id = ?', + 'ii', + $questmediaId, + $dragId + ); + } + + + /** + * Delete a Drag-item. + * + * @param int $dragId ID of Drag-item to delete + */ + public function deleteDrag($dragId) + { + $this->db->query( + 'DELETE FROM questtypes_dragndrop_drags '. + 'WHERE id = ?', + 'i', + $dragId + ); + } /** diff --git a/questtypes/dragndrop/html/edittask.tpl b/questtypes/dragndrop/html/edittask.tpl index c65158e7..1bbc76e1 100644 --- a/questtypes/dragndrop/html/edittask.tpl +++ b/questtypes/dragndrop/html/edittask.tpl @@ -1 +1,254 @@ -
TODO
+ +