closedquestion-8.x-3.x-dev/src/Question/Mapping/CqDraggable.php
src/Question/Mapping/CqDraggable.php
<?php
namespace Drupal\closedquestion\Question\Mapping;
use Drupal\closedquestion\Question\CqQuestionInterface;
/**
* Class CqDraggable.
*
* CqDraggable describes a draggable in Drag&Drop and Hotspot questions.
*
* @package Drupal\closedquestion\Question\Mapping
*/
class CqDraggable {
/**
* The identifier of this draggable.
*
* @var string
*/
private $identifier = 'no_identifier';
/**
* The location of this draggable.
*
* @var array
* - 0: x coordinate
* - 1: y coordinate
*/
private $location;
/**
* The time of this draggable.
*
* @var float
*/
private $time;
/**
* If this draggable is of the image kind, the source url of the image.
*
* @var string
*/
private $imgSrc;
/**
* The (html) content of the draggable.
*
* @var string
*/
private $text;
/**
* The css class to use for the draggable.
*
* @var string
*/
private $cssClass;
/**
* Initialises this item using data from an XML node.
*
* @param \DOMElement $node
* The node to use for initialisation.
* @param \Drupal\closedquestion\Question\CqQuestionInterface $context
* The question or other object that the mapping can query for things
* like the current answer, draggables and hotspots.
*/
public function __construct(\DOMElement $node, CqQuestionInterface $context) {
if ($node) {
$attribs = $node->attributes;
if ($attribs) {
$item = $attribs->getNamedItem('identifier');
if ($item === NULL) {
$item = $attribs->getNamedItem('id');
}
if ($item === NULL) {
$item = $attribs->getNamedItem('name');
}
if ($item !== NULL) {
$this->identifier = $item->nodeValue;
}
$item = $attribs->getNamedItem('src');
if ($item !== NULL) {
$this->imgSrc = $item->nodeValue;
}
$item = $attribs->getNamedItem('class');
if ($item !== NULL) {
$this->cssClass = $item->nodeValue;
}
}
$this->text = \Drupal::service('closedquestion.utility.xml_lib')->getTextContent($node, $context);
}
}
/**
* Gets the identifier for this draggable.
*
* @return string
* The identifier.
*/
public function getIdentifier() {
return $this->identifier;
}
/**
* Sets the location of this draggable from an Array($x, $y)
*
* @param array $location
* The array containing the X and Y coordinates.
*/
public function setLocation(array $location) {
$this->location = $location;
}
/**
* Sets the location of this draggable from an individual x and y coordinate.
*
* @param int $x
* The X coordinate of the draggable.
* @param int $y
* The Y coordinate of the draggable.
*/
public function setLocationXY($x, $y) {
$this->location = array($x, $y);
}
/**
* Gets the time of this draggable.
*
* @return float
* The time of this draggable.
*/
public function getTime() {
return $this->time;
}
/**
* Sets the time of this draggable.
*
* @param float $time
* The time of this draggable.
*/
public function setTime($time) {
$this->time = $time;
}
/**
* Gets the location of this draggable as an Array($x, $y)
*
* @return array
* The location of this draggable.
*/
public function getLocation() {
return $this->location;
}
/**
* Returns HTML representation of the draggable.
*
* @return string
* The (html) content of this draggable.
*/
public function getText() {
if ($this->imgSrc != NULL) {
return '<img src="' . $this->imgSrc . '">';
}
else {
return $this->text;
}
}
/**
* The CSS class name to use for this draggable.
*
* @return string
* The CSS class name.
*/
public function getClass() {
return $this->cssClass;
}
/**
* Checks that draggable only contains an image and no other html.
*
* @return bool
* True if the draggable only contains an image and no other html.
*/
public function imageOnly() {
return ($this->imgSrc != NULL);
}
}
