closedquestion-8.x-3.x-dev/src/Question/CqQuestionHotspot.php

src/Question/CqQuestionHotspot.php
<?php

namespace Drupal\closedquestion\Question;

use Drupal\closedquestion\Entity\ClosedQuestionInterface;
use Drupal\closedquestion\Question\Mapping\CqDraggable;
use Drupal\closedquestion\Question\Mapping\CqFeedback;
use Drupal\closedquestion\Question\Mapping\CqMapping;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class CqQuestionHotspot.
 *
 * Implementation of the Hotspot question type.
 * A hotspot question presents the student with an image and the assignment
 * to point out one or more things on the image.
 *
 * @package Drupal\closedquestion\Question
 */
class CqQuestionHotspot extends CqQuestionAbstract {

  /**
   * HTML containing the question-text.
   *
   * @var string
   */
  private $text;

  /**
   * The base-name used for form elements that need to be accessed by js.
   *
   * @var string
   */
  private $formElementName;

  /**
   * The url of the image to use as background for the hotspot question.
   *
   * @var string
   */
  private $matchImgUrl;

  /**
   * The width of the background image.
   *
   * @var int
   */
  private $matchImgWidth;

  /**
   * The height of the background image.
   *
   * @var int
   */
  private $matchImgHeight;

  /**
   * Maximum number of items the student is allowed to point out.
   *
   * Defaults to 1;
   *
   * @var int
   */
  private $maxChoices = 1;

  /**
   * The list of hotspots in the image.
   *
   * @var \Drupal\closedquestion\Question\Mapping\CqHotspotInterface[]
   */
  private $hotspots = array();

  /**
   * The list of draggables.
   *
   * Each time the student clicks on the target image one draggable is added,
   * untill the maximum $maxChoices is reached.
   *
   * @var \Drupal\closedquestion\Question\Mapping\CqDraggable[]
   */
  private $draggables = array();

  /**
   * The mappings to check the student's answer against.
   *
   * @var \Drupal\closedquestion\Question\Mapping\CqMapping[]
   */
  private $mappings = array();

  /**
   * The mappings that have the correct flag set and matched the current answer.
   *
   * @var \Drupal\closedquestion\Question\Mapping\CqMapping[]
   */
  private $matchedCorrectMappings = array();

  /**
   * The mappings that matched the current answer.
   *
   * @var \Drupal\closedquestion\Question\Mapping\CqMapping[]
   */
  private $matchedMappings = array();

  /**
   * List of feedback items to use as general hints.
   *
   * @var \Drupal\closedquestion\Question\Mapping\CqFeedback[]
   */
  private $hints = array();

  /**
   * Flag determining whether click order is considered when evaluation answer.
   *
   * @var bool
   */
  private $clickOrder = FALSE;

  /**
   * Constructs a Hotspot question object.
   *
   * @param CqUserAnswerInterface $userAnswer
   *   The CqUserAnswerInterface to use for storing the student's answer.
   * @param \Drupal\closedquestion\Entity\ClosedQuestionInterface $closedQuestion
   *   Closed question entity.
   */
  public function __construct(CqUserAnswerInterface $userAnswer, ClosedQuestionInterface $closedQuestion) {
    parent::__construct();
    $this->userAnswer = $userAnswer;
    $this->closedQuestion = $closedQuestion;
    $this->formElementName = 'cq_dragdrop_question' . $this->closedQuestion->id() . '_';
  }

  /**
   * Implements CqQuestionAbstract::getOutput()
   */
  public function getOutput() {
    $this->initialise();
    $retval = $this->formBuilder->getForm('\Drupal\closedquestion\Form\QuestionForm', $this->closedQuestion);
    $retval['#prefix'] = $this->prefix;
    $retval['#suffix'] = $this->postfix;
    return $retval;
  }

  /**
   * Implements CqQuestionAbstract::getFeedbackItems()
   */
  public function getFeedbackItems() {
    $tries = $this->userAnswer->getTries();
    $answer = $this->userAnswer->getAnswer();
    $feedback = array();
    if ($answer == NULL) {
      // If there is no answer, don't check any further..
      return $feedback;
    }

    if ($this->isCorrect()) {
      foreach ($this->matchedCorrectMappings as $mapping) {
        $feedback = array_merge($feedback, $mapping->getFeedbackItems($tries));
      }
    }
    else {
      foreach ($this->hints as $fb) {
        if ($fb->inRange($tries)) {
          $feedback[] = $fb;
        }
      }
      foreach ($this->matchedMappings as $mapping) {
        $feedback = array_merge($feedback, $mapping->getFeedbackItems($tries));
      }
    }

    // Finally, ask external systems if they want to add extra feedback.
    $feedback = array_merge($feedback, $this->fireGetExtraFeedbackItems($this, $tries));
    return $feedback;
  }

  /**
   * Overrides CqQuestionAbstract::getDraggables()
   */
  public function getDraggables() {
    return $this->draggables;
  }

  /**
   * Overrides CqQuestionAbstract::getHotspots()
   */
  public function getHotspots() {
    return $this->hotspots;
  }

  /**
   * Overrides CqQuestionAbstract::loadXml()
   */
  public function loadXml(\DOMNode $dom) {
    parent::loadXml($dom);

    $this->hotspots = array();
    $this->mappings = array();
    $this->hints = array();
    $this->draggables = array();

    foreach ($dom->childNodes as $node) {
      $name = mb_strtolower($node->nodeName);
      switch ($name) {
        case 'text':
          $this->text = $this->xmlLib->getTextContent($node, $this);
          break;

        case 'matchimg':
          $this->matchImgUrl = $this->getUrlFromMediaTag($node->getAttribute('src'));
          $this->matchImgHeight = $node->getAttribute('height');
          $this->matchImgWidth = $node->getAttribute('width');
          $this->maxChoices = $node->getAttribute('maxChoices');
          foreach ($node->childNodes as $child) {
            switch (mb_strtolower($child->nodeName)) {
              case 'hotspot':
                $hotspot = cq_Hotspot_from_xml($child, $this);
                if (is_object($hotspot)) {
                  if (isset($this->hotspots[$hotspot->getIdentifier()])) {
                    $this->messenger->addMessage(t('Hotspot identifier %identifier used more than once!', array('%identifier' => $hotspot->getIdentifier())), 'warning');
                  }
                  $this->hotspots[$hotspot->getIdentifier()] = $hotspot;
                }
                break;
            }
          }
          break;

        case 'mapping':
          $map = new CqMapping();
          $map->generateFromNode($node, $this);
          $this->mappings[] = $map;
          break;

        case 'hint':
          $this->hints[] = CqFeedback::newCqFeedback($node, $this);
          break;

        default:
          if (!in_array($name, $this->knownElements)) {
            $this->messenger->addMessage(t('Unknown node: @nodename', array('@nodename' => $node->nodeName)));
          }
          break;
      }
    }

    $item = $dom->attributes->getNamedItem('clickorder');
    if ($item !== NULL && $this->clickOrder !== NULL) {
      $this->clickOrder = $item->value;
    }
  }

  /**
   * Returns the the answer in string form.
   *
   * @param string $identifier
   *   Unused in this question type.
   *
   * @return string
   *   The answer in string form.
   */
  public function getAnswerForChoice($identifier) {
    $hotspots = $this->getHotspots();
    $draggables = $this->getDraggables();

    $this->matchOrder = '';

    // Do the matching.
    foreach ($draggables as $draggable) {
      foreach ($hotspots as $hotspot) {
        if ($hotspot->doMatch($draggable->getLocation())) {
          $this->matchOrder .= $hotspot->getIdentifier();
        }
      }
    }

    return $this->matchOrder;
  }

  /**
   * Parses the answer.
   *
   * Parse the answer string and put the coordinates of the different draggables
   * into the corresponding draggable objects. Creates new draggables objects
   * if required.
   */
  private function parseAnswer() {
    $this->draggables = array();
    $answer = $this->userAnswer->getAnswer();
    $parts = explode(';', $answer);
    foreach ($parts as $part) {
      $partArr = explode(',', $part);
      if (count($partArr) == 3) {
        $draggable = new CqDraggable(new \DOMElement('null'), $this);
        $draggable->setLocationXY($partArr[1], $partArr[2]);
        $this->draggables[] = $draggable;
      }
    }
  }

  /**
   * Implements CqQuestionAbstract::getForm()
   */
  public function getForm($formState) {
    $nextlink = '';
    $answer = $this->userAnswer->getAnswer();
    $correct = $this->isCorrect();
    $mapName = $this->formElementName . 'map';

    // The question part is themed.
    $form['question']['#theme'] = 'closedquestion_question_hotspot';
    $form['question']['questionText'] = array(
      '#type' => 'item',
      '#markup' => $this->text,
    );

    // The data needed by the theme function.
    $data = array();
    $data['elementname'] = $this->formElementName;
    $data['clickorder'] = $this->clickOrder;
    $data['mapname'] = $mapName;
    $data['image'] = array(
      "height" => (int) $this->matchImgHeight,
      "width" => (int) $this->matchImgWidth,
      "url" => $this->matchImgUrl,
    );
    $data['maxchoices'] = $this->maxChoices;


    // Handle the draggables.
    $counter = 0;
    foreach ($this->draggables as $draggable) {
      $counter++;
      $dragClass = 'cqDdDraggable cqDdDraggable_minimal';
      $loc = $draggable->getLocation();
      $data['draggables']['d' . $counter] = array(
        'class' => $dragClass,
        'cqvalue' => 'd' . $counter,
        'x' => (int) $loc[0],
        'y' => (int) $loc[1],
      );
    }

    // Handle the hotspots.
    foreach ($this->hotspots as $hotspot) {
      $description = $hotspot->getDescription();
      if (!empty($description) || !empty($hotspot->getImages())) {
        $termId = $this->formElementName . 'term_' . $hotspot->getIdentifier();
        $data['hotspots'][$hotspot->getIdentifier()] = array(
          'termid' => $termId,
          'maphtml' => $hotspot->getMapHtml(),
          'description' => $description,
          'images' => $hotspot->getImages()
        );
      }
    }
    $form['question']['data'] = array('#type' => 'value', '#value' => $data);


    // Other elements are not themed by default.
    // This element will be filled by Javascript to contain the answer.
    $form[$this->formElementName . 'answer'] = array(
      '#type' => 'hidden',
      '#default_value' => $answer,
      '#input' => TRUE,
    );

    // Insert standard feedback and submit elements.
    $wrapper_id = 'cq-feedback-wrapper_' . $this->formElementName;
    $this->insertFeedback($form, $wrapper_id);
    $this->insertSubmit($form, $wrapper_id);
    return $form;
  }

  /**
   * Implements CqQuestionAbstract::checkCorrect()
   */
  public function checkCorrect() {
    $this->parseAnswer();
    $this->matchedCorrectMappings = array();
    $this->matchedMappings = array();
    $correct = FALSE;

    foreach ($this->mappings as $id => $mapping) {
      if ($mapping->evaluate()) {
        if ($mapping->getCorrect() != 0) {
          $correct = TRUE;
          $this->matchedCorrectMappings[] = $mapping;
        }
        else {
          $this->matchedMappings[] = $mapping;
        }
        if ($mapping->stopIfMatch()) {
          break;
        }
      }
      unset($mapping);
    }
    return $correct;
  }

  /**
   * Implements CqQuestionAbstract::submitAnswer()
   */
  public function submitAnswer($form, FormStateInterface $form_state) {
    $newAnswer = $form_state->getValue($this->formElementName . 'answer');
    $this->userAnswer->setAnswer($newAnswer);
    $correct = $this->isCorrect(TRUE);
    if ($this->userAnswer->answerHasChanged()) {
      if (!$correct) {
        $this->userAnswer->increaseTries();
      }
      $this->userAnswer->store();
    }
  }

  /**
   * Implements CqQuestionAbstract::getAllText()
   */
  public function getAllText() {
    $this->initialise();
    $retval = array();
    $retval['text']['#markup'] = $this->text;

    // Hints.
    $retval['hints'] = array(
      '#theme' => 'closedquestion_feedback_list',
      '#extended' => TRUE,
    );
    foreach ($this->hints as $fbitem) {
      $retval['hints']['items'][] = $fbitem->getAllText();
    }

    // Mappings.
    $retval['mappings'] = array(
      '#theme' => 'closedquestion_mapping_list',
      'items' => array(),
    );
    foreach ($this->mappings as $mapping) {
      $retval['mappings']['items'][] = $mapping->getAllText();
    }

    $retval['#theme'] = 'closedquestion_question_general_text';
    return $retval;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc