From eb9ea3b0ca4d3083aabc9fb640e29c2c03ad266d Mon Sep 17 00:00:00 2001 From: coderkun Date: Fri, 4 Apr 2014 00:57:05 +0200 Subject: [PATCH] implement functionality for Questtype ?Drag&Drop? --- .../DragndropQuesttypeController.inc | 77 +++++++++++++++---- .../dragndrop/DragndropQuesttypeModel.inc | 57 +++++++++++--- questtypes/dragndrop/html/quest.tpl | 10 +-- questtypes/dragndrop/html/submission.tpl | 22 ++++-- views/html/html.tpl | 1 + www/js/dnd.js | 54 +++++++++++++ 6 files changed, 183 insertions(+), 38 deletions(-) create mode 100644 www/js/dnd.js diff --git a/questtypes/dragndrop/DragndropQuesttypeController.inc b/questtypes/dragndrop/DragndropQuesttypeController.inc index fa801be2..1007a1f8 100644 --- a/questtypes/dragndrop/DragndropQuesttypeController.inc +++ b/questtypes/dragndrop/DragndropQuesttypeController.inc @@ -49,9 +49,18 @@ // Save user answers foreach($drops as &$drop) { - if(!array_key_exists($drop['id'], $answers)) { - $this->Dragndrop->setCharacterSubmission($drop['id'], $character['id'], $answers[$drop['id']]); + // Determine user answer + $answer = null; + if(array_key_exists($drop['id'], $answers) && !empty($answers[$drop['id']])) + { + $a = intval(substr($answers[$drop['id']], 4)); + if($a !== false && $a > 0) { + $answer = $a; + } } + + // Update database record + $this->Dragndrop->setCharacterSubmission($drop['id'], $character['id'], $answer); } } @@ -74,19 +83,16 @@ $drops = $this->Dragndrop->getDrops($dndField['quest_id']); // Match drops with user answers - $allSolved = true; foreach($drops as &$drop) { - if(!array_key_exists($drop['id'], $answers) || $answer[$drop['id']] != $drop['questtypes_dragndrop_drag_id']) - { - $allSolved = false; - break; + if(!array_key_exists($drop['id'], $answers) || intval(substr($answers[$drop['id']], 4)) !== $drop['questtypes_dragndrop_drag_id']) { + return false; } } // Set status - return $allSolved; + return true; } @@ -107,17 +113,26 @@ $dndField = $this->Dragndrop->getDragndrop($quest['id']); $dndField['media'] = $this->Media->getMediaById($dndField['questmedia_id']); - // Get Drops - $drops = $this->Dragndrop->getDrops($dndField['quest_id']); - // Get Drags - $drags = $this->Dragndrop->getDrags($dndField['quest_id']); - foreach($drags as &$drag) { + $drags = array(); + $dragsByIndex = $this->Dragndrop->getDrags($dndField['quest_id']); + foreach($dragsByIndex as &$drag) { $drag['media'] = $this->Media->getMediaById($drag['questmedia_id']); + $drags[$drag['id']] = $drag; } - // Has Character already solved Quest? - $solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']); + // Get Drops + $drops = $this->Dragndrop->getDrops($dndField['quest_id']); + foreach($drops as &$drop) + { + // Get saved user answer + $drop['useranswer'] = $this->Dragndrop->getCharacterSubmission($drop['id'], $character['id']); + if(!is_null($drop['useranswer'])) + { + $drop['useranswer'] = $drags[$drop['useranswer']]; + unset($drags[$drop['useranswer']['id']]); + } + } // Pass data to view @@ -130,7 +145,6 @@ /** * Action: submission. - * TODO submission() * * Show the submission of a Character for a Quest. * @@ -141,6 +155,37 @@ */ public function submission($seminary, $questgroup, $quest, $character) { + // Get Drag&Drop field + $dndField = $this->Dragndrop->getDragndrop($quest['id']); + $dndField['media'] = $this->Media->getMediaById($dndField['questmedia_id']); + + // Get Drags + $drags = array(); + $dragsByIndex = $this->Dragndrop->getDrags($dndField['quest_id']); + foreach($dragsByIndex as &$drag) { + $drag['media'] = $this->Media->getMediaById($drag['questmedia_id']); + $drags[$drag['id']] = $drag; + } + + // Get Drops + $drops = $this->Dragndrop->getDrops($dndField['quest_id']); + foreach($drops as &$drop) + { + // Get saved user answer + $drop['useranswer'] = $this->Dragndrop->getCharacterSubmission($drop['id'], $character['id']); + if(!is_null($drop['useranswer'])) + { + $drop['useranswer'] = $drags[$drop['useranswer']]; + unset($drags[$drop['useranswer']['id']]); + } + } + + + // Pass data to view + $this->set('seminary', $seminary); + $this->set('field', $dndField); + $this->set('drops', $drops); + $this->set('drags', $drags); } } diff --git a/questtypes/dragndrop/DragndropQuesttypeModel.inc b/questtypes/dragndrop/DragndropQuesttypeModel.inc index 676e502f..1e772edf 100644 --- a/questtypes/dragndrop/DragndropQuesttypeModel.inc +++ b/questtypes/dragndrop/DragndropQuesttypeModel.inc @@ -84,7 +84,7 @@ /** - * Save Character’s submitted answer for one Drag&Drop-field. + * Save Character’s submitted answer for one Drop-field. * * @param int $dropId ID of Drop-field * @param int $characterId ID of Character @@ -92,16 +92,53 @@ */ public function setCharacterSubmission($dropId, $characterId, $answer) { - $this->db->query( - 'INSERT INTO questtypes_dragndrop_drops_characters '. - '(questtypes_dragndrop_drops_id, character_id, questtypes_dragndrop_drag_id) '. - 'VALUES '. - '(?, ?, ?) '. - 'ON DUPLICATE KEY UPDATE '. - 'questtypes_dragndrop_drag_id = ?', - 'iiii', - $dropId, $characterId, $answer, $answer + if(is_null($answer)) + { + $this->db->query( + 'DELETE FROM questtypes_dragndrop_drops_characters '. + 'WHERE questtypes_dragndrop_drop_id = ? AND character_id = ?', + 'ii', + $dropId, $characterId + ); + } + else + { + $this->db->query( + 'INSERT INTO questtypes_dragndrop_drops_characters '. + '(questtypes_dragndrop_drop_id, character_id, questtypes_dragndrop_drag_id) '. + 'VALUES '. + '(?, ?, ?) '. + 'ON DUPLICATE KEY UPDATE '. + 'questtypes_dragndrop_drag_id = ?', + 'iiii', + $dropId, $characterId, $answer, $answer + ); + } + } + + + /** + * Get Character’s saved answer for one Drop-field. + * + * @param int $dropId ID of Drop-field + * @param int $characterId ID of Character + * @return int ID of Drag-field or null + */ + public function getCharacterSubmission($dropId, $characterId) + { + $data = $this->db->query( + 'SELECT questtypes_dragndrop_drag_id '. + 'FROM questtypes_dragndrop_drops_characters '. + 'WHERE questtypes_dragndrop_drop_id = ? AND character_id = ?', + 'ii', + $dropId, $characterId ); + if(!empty($data)) { + return $data[0]['questtypes_dragndrop_drag_id']; + } + + + return null; } } diff --git a/questtypes/dragndrop/html/quest.tpl b/questtypes/dragndrop/html/quest.tpl index 74e497c7..28d208b2 100644 --- a/questtypes/dragndrop/html/quest.tpl +++ b/questtypes/dragndrop/html/quest.tpl @@ -1,14 +1,14 @@
-
+
-
- +
+
-
+
- +
diff --git a/questtypes/dragndrop/html/submission.tpl b/questtypes/dragndrop/html/submission.tpl index dbc453c3..90148212 100644 --- a/questtypes/dragndrop/html/submission.tpl +++ b/questtypes/dragndrop/html/submission.tpl @@ -1,7 +1,15 @@ - &$text) : ?> - 0) : ?> - - - - - +
+ +
+ + + +
+ +
+ +
+ + + +
diff --git a/views/html/html.tpl b/views/html/html.tpl index e36da721..da2c90b1 100644 --- a/views/html/html.tpl +++ b/views/html/html.tpl @@ -21,6 +21,7 @@ + diff --git a/www/js/dnd.js b/www/js/dnd.js new file mode 100644 index 00000000..ff1340c7 --- /dev/null +++ b/www/js/dnd.js @@ -0,0 +1,54 @@ +/** + * Drag&Drop-functions + */ + +function onDragStart(event) +{ + jQuery(event.currentTarget).addClass("drag"); + event.dataTransfer.setData("Text", event.target.id); +} + +function onDragEnter(event) +{ + if(event.target.nodeName == "DIV" && !(event.target.parentNode.id == "dropZone" && event.target.hasChildNodes())) { + jQuery(event.target).addClass('drop'); + } +} + +function onDragOver(event) +{ + if(event.target.nodeName == "DIV" && !(event.target.parentNode.id == "dropZone" && event.target.hasChildNodes())) { + event.preventDefault(); + } +} + +function onDragLeave(event) +{ + jQuery(event.target).removeClass('drop'); +} + +function onDragEnd(event) +{ + jQuery(event.currentTarget).removeClass("drag"); +} + +function onDrop(event, setId) +{ + setId = (typeof setId == 'undefined') ? true : setId; + jQuery(event.currentTarget).removeClass('drag'); + jQuery(event.target).removeClass('drop'); + event.preventDefault(); + + var data = event.dataTransfer.getData("Text"); + var dataElement = $('#'+data); + if(dataElement.parent() && $('#dnd_'+dataElement.parent().attr('id'))) { + $('#dnd_'+dataElement.parent().attr('id')).attr('value', "null"); + } + jQuery(event.target).append(dataElement); + + if(setId) { + console.log(data); + $('#dnd_' + jQuery(event.target).attr('id')).attr('value', data); + } + +}