View source
<?php
namespace Drupal\quiz_ddlines;
use Drupal\quiz_question\QuizQuestionResponse;
class DDLinesResponse extends QuizQuestionResponse {
protected $user_answers = array();
public function __construct($result_id, $question_node, $tries = NULL) {
parent::__construct($result_id, $question_node, $tries);
if (isset($tries)) {
$decoded = json_decode($tries);
if (is_array($decoded)) {
foreach ($decoded as $answer) {
$this->user_answers[$answer->label_id] = $answer->hotspot_id;
}
}
}
else {
$res = db_query('SELECT label_id, hotspot_id FROM {quiz_ddlines_user_answers} ua
LEFT OUTER JOIN {quiz_ddlines_user_answer_multi} uam ON(uam.user_answer_id = ua.id)
WHERE ua.result_id = :result_id AND ua.question_nid = :question_nid AND ua.question_vid = :question_vid', array(
':result_id' => $result_id,
':question_nid' => $this->question
->id(),
':question_vid' => $this->question
->getRevisionId(),
));
while ($row = $res
->fetch()) {
$this->user_answers[$row->label_id] = $row->hotspot_id;
}
}
}
public function save() {
$user_answer_id = db_insert('quiz_ddlines_user_answers')
->fields(array(
'question_nid' => $this->question
->id(),
'question_vid' => $this->question
->getRevisionId(),
'result_id' => $this->rid,
))
->execute();
$query = db_insert('quiz_ddlines_user_answer_multi')
->fields(array(
'user_answer_id',
'label_id',
'hotspot_id',
));
foreach ($this->user_answers as $key => $value) {
$query
->values(array(
$user_answer_id,
$key,
$value,
));
}
$query
->execute();
}
public function delete() {
$user_answer_ids = array();
$query = db_query('SELECT id FROM {quiz_ddlines_user_answers} WHERE question_nid = :nid AND question_vid = :vid AND result_id = :result_id', array(
':nid' => $this->question
->id(),
':vid' => $this->question
->getRevisionId(),
':result_id' => $this->rid,
));
while ($answer = $query
->fetch()) {
$user_answer_ids[] = $answer->id;
}
if (!empty($user_answer_ids)) {
db_delete('quiz_ddlines_user_answer_multi')
->condition('user_answer_id', $user_answer_ids, 'IN')
->execute();
}
db_delete('quiz_ddlines_user_answers')
->condition('result_id', $this->rid)
->condition('question_nid', $this->question
->id())
->condition('question_vid', $this->question
->getRevisionId())
->execute();
}
public function score() {
$results = $this
->getDragDropResults();
$correct_count = 0;
foreach ($results as $result) {
$correct_count += $result == AnswerStatus::CORRECT ? 1 : 0;
}
return $correct_count;
}
public function getResponse() {
return $this->user_answers;
}
public function getReportFormResponse($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
$image_path = base_path() . drupal_get_path('module', 'quiz_ddlines') . '/theme/images/';
$html = '<h3>' . t('Your answers') . '</h3>';
$html .= '<div class="icon-descriptions"><div><img src="' . $image_path . 'icon_ok.gif">' . t('Means alternative is placed on the correct spot') . '</div>';
$html .= '<div><img src="' . $image_path . 'icon_wrong.gif">' . t('Means alternative is placed on the wrong spot, or not placed at all') . '</div></div>';
$html .= '<div class="quiz-ddlines-user-answers" id="' . $this->question
->id() . '">';
$html .= '</div>';
$html .= '<h3>' . t('Correct answers') . '</h3>';
$html .= '<div class="quiz-ddlines-correct-answers" id="' . $this->question
->id() . '">';
$html .= '</div>';
$settings = array();
$correct_id = "correct-{$this->question->id()}";
$settings[$correct_id] = json_decode($this->question->ddlines_elements);
$elements = $settings[$correct_id]->elements;
$answers = clone $settings[$correct_id];
$answers->elements = array();
$elements_answered = array();
foreach ($this->user_answers as $label_id => $hotspot_id) {
if (!isset($hotspot_id)) {
continue;
}
$element = array(
'feedback_wrong' => '',
'feedback_correct' => '',
'color' => $this
->getElementColor($elements, $label_id),
);
$label = $this
->getLabel($elements, $label_id);
$hotspot = $this
->getHotspot($elements, $hotspot_id);
if (isset($hotspot)) {
$elements_answered[] = $hotspot->id;
$element['hotspot'] = $hotspot;
}
if (isset($label)) {
$elements_answered[] = $label->id;
$element['label'] = $label;
}
$element['correct'] = $this
->isAnswerCorrect($elements, $label_id, $hotspot_id);
$answers->elements[] = $element;
}
foreach ($elements as $el) {
if (!in_array($el->label->id, $elements_answered)) {
$element = array(
'feedback_wrong' => '',
'feedback_correct' => '',
'color' => $el->color,
'label' => $el->label,
);
$answers->elements[] = $element;
}
if (!in_array($el->hotspot->id, $elements_answered)) {
$element = array(
'feedback_wrong' => '',
'feedback_correct' => '',
'color' => $el->color,
'hotspot' => $el->hotspot,
);
$answers->elements[] = $element;
}
}
$settings["answers-{$this->question->id()}"] = $answers;
$settings['mode'] = 'result';
$settings['execution_mode'] = $this->question->execution_mode;
$settings['hotspot']['radius'] = $this->question->hotspot_radius;
$settings['quiz_imagepath'] = base_path() . drupal_get_path('module', 'quiz_ddlines') . '/theme/images/';
drupal_add_js(array(
'quiz_ddlines' => $settings,
), 'setting');
_quiz_ddlines_add_js_and_css();
return array(
'#markup' => $html,
);
}
private function getElementColor($list, $id) {
foreach ($list as $element) {
if ($element->label->id == $id) {
return $element->color;
}
}
}
private function getHotspot($list, $id) {
foreach ($list as $element) {
if ($element->hotspot->id == $id) {
return $element->hotspot;
}
}
}
private function getLabel($list, $id) {
foreach ($list as $element) {
if ($element->label->id == $id) {
return $element->label;
}
}
}
private function isAnswerCorrect($list, $label_id, $hotspot_id) {
foreach ($list as $element) {
if ($element->label->id == $label_id) {
return $element->hotspot->id == $hotspot_id;
}
}
return false;
}
private function getDragDropResults() {
$results = array();
foreach (json_decode($this->question->ddlines_elements)->elements as $element) {
$source_id = $element->label->id;
if (isset($this->user_answers[$source_id])) {
$results[$element->label->id] = $this->user_answers[$source_id] == $element->hotspot->id ? AnswerStatus::CORRECT : AnswerStatus::WRONG;
}
else {
$results[$element->label->id] = AnswerStatus::NO_ANSWER;
}
}
return $results;
}
}