Questtype ?Drag&Drop?: implement multiple valid Drag-items for each Drop-item
This commit is contained in:
parent
d015ac5ebc
commit
5988e2176a
3 changed files with 47 additions and 9 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
<div id="dropZone" style="width:<?=$field['width']?>px; height:<?=$field['height']?>px; background-image:url('<?=$linker->link(array('media','seminary',$seminary['url'],$field['media']['url']))?>')">
|
||||
<?php foreach($drops as &$drop) : ?>
|
||||
<div id="drop<?=$drop['id']?>" ondragenter="onDragEnter(event)" ondragover="onDragOver(event)" ondragleave="onDragLeave(event)" ondrop="onDrop(event)" style="position:absolute; width:<?=$drop['width']?>px; height:<?=$drop['height']?>px; margin:<?=$drop['top']?>px 0 0 <?=$drop['left']?>px;"><?php if(array_key_exists('answer', $drop) && !is_null($drop['answer'])) : ?><img id="drag<?=$drop['answer']['id']?>" draggable="true" ondragstart="onDragStart(event)" ondragend="onDragEnd(event)" src="<?=$linker->link(array('media','seminary',$seminary['url'],$drop['answer']['media']['url']))?>" /><?php endif ?></div>
|
||||
<input type="hidden" id="dnd_drop<?=$drop['id']?>" name="answers[<?=$drop['id']?>]" value="<?=(!is_null($drop['answer'])) ? 'drag'.$drop['answer']['id'] : null ?>" />
|
||||
<input type="hidden" id="dnd_drop<?=$drop['id']?>" name="answers[<?=$drop['id']?>]" value="<?=(array_key_exists('answer', $drop)) ? 'drag'.$drop['answer']['id'] : null ?>" />
|
||||
<?php endforeach ?>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue