diff --git a/questtypes/dragndrop/DragndropQuesttypeController.inc b/questtypes/dragndrop/DragndropQuesttypeController.inc index 90843218..5fbf6328 100644 --- a/questtypes/dragndrop/DragndropQuesttypeController.inc +++ b/questtypes/dragndrop/DragndropQuesttypeController.inc @@ -79,13 +79,17 @@ // Get Drag&Drop field $dndField = $this->Dragndrop->getDragndrop($quest['id']); - // Get Drops - $drops = $this->Dragndrop->getDrops($dndField['quest_id']); + // Get Drags + $drags = $this->Dragndrop->getDrags($dndField['quest_id'], true); - // Match drops with user answers - foreach($drops as &$drop) + // Match drags with user answers + foreach($drags as &$drag) { - if(!array_key_exists($drop['id'], $answers) || intval(substr($answers[$drop['id']], 4)) !== $drop['questtypes_dragndrop_drag_id']) { + $founds = array_keys($answers, 'drag'.$drag['id']); + if(count($founds) != 1) { + return false; + } + if(!$this->Dragndrop->dragMatchesDrop($drag['id'], $founds[0])) { return false; } } diff --git a/questtypes/dragndrop/DragndropQuesttypeModel.inc b/questtypes/dragndrop/DragndropQuesttypeModel.inc index 1e772edf..2aadb335 100644 --- a/questtypes/dragndrop/DragndropQuesttypeModel.inc +++ b/questtypes/dragndrop/DragndropQuesttypeModel.inc @@ -56,7 +56,7 @@ public function getDrops($dragndropId) { return $this->db->query( - 'SELECT id, top, `left`, width, height, questtypes_dragndrop_drag_id '. + 'SELECT id, top, `left`, width, height '. //, questtypes_dragndrop_drag_id '. 'FROM questtypes_dragndrop_drops '. 'WHERE questtypes_dragndrop_id = ?', 'i', @@ -69,20 +69,54 @@ * Get Drag-items. * * @param int $dragndropId ID of Drag&Drop-field + * @param boolean $onlyUsed Only Drag-items that are used for a Drop-item * @return array Drag-items */ - public function getDrags($dragndropId) + public function getDrags($dragndropId, $onlyUsed=false) { return $this->db->query( 'SELECT id, questmedia_id '. 'FROM questtypes_dragndrop_drags '. - 'WHERE questtypes_dragndrop_id = ?', + 'WHERE questtypes_dragndrop_id = ?'. + ($onlyUsed + ? ' AND EXISTS ('. + 'SELECT questtypes_dragndrop_drag_id '. + 'FROM questtypes_dragndrop_drops_drags '. + 'WHERE questtypes_dragndrop_drag_id = questtypes_dragndrop_drags.id'. + ')' + : null + ), 'i', $dragndropId ); } + /** + * Check if a Drag-item mathes a Drop-item. + * + * @param int $dragId ID of Drag-field + * @param int $dropId ID of Drop-field + * @return boolean Drag-item is valid for Drop-item + */ + public function dragMatchesDrop($dragId, $dropId) + { + $data = $this->db->query( + 'SELECT count(*) AS c '. + 'FROM questtypes_dragndrop_drops_drags '. + 'WHERE questtypes_dragndrop_drop_id = ? AND questtypes_dragndrop_drag_id = ?', + 'ii', + $dropId, $dragId + ); + if(!empty($data)) { + return ($data[0]['c'] > 0); + } + + + return false; + } + + /** * Save Character’s submitted answer for one Drop-field. * diff --git a/questtypes/dragndrop/html/quest.tpl b/questtypes/dragndrop/html/quest.tpl index 5a5d3717..4cb10b60 100644 --- a/questtypes/dragndrop/html/quest.tpl +++ b/questtypes/dragndrop/html/quest.tpl @@ -2,7 +2,7 @@