View source
<?php
namespace Drupal\cloze;
use Drupal\quiz_question\QuizQuestionResponse;
class ClozeResponse extends QuizQuestionResponse {
protected $answer_id = 0;
public function __construct($result_id, stdClass $question_node, $answer = NULL) {
parent::__construct($result_id, $question_node, $answer);
if (!isset($answer)) {
$r = db_query("SELECT answer_id, answer, score, question_vid, question_nid, result_id FROM {quiz_cloze_user_answers} WHERE question_nid = :question_nid AND question_vid = :question_vid AND result_id = :result_id", array(
':question_nid' => $question_node
->id(),
':question_vid' => $question_node
->getRevisionId(),
':result_id' => $result_id,
))
->fetch();
if (!empty($r)) {
$this->answer = unserialize($r->answer);
$this->score = $r->score;
$this->answer_id = $r->answer_id;
}
}
else {
$this->answer = $answer;
}
}
public function isValid() {
return TRUE;
}
public function save() {
$this->answer_id = db_insert('quiz_cloze_user_answers')
->fields(array(
'answer' => serialize($this->answer),
'question_nid' => $this->question
->id(),
'question_vid' => $this->question
->getRevisionId(),
'result_id' => $this->rid,
'score' => $this
->getScore(FALSE),
))
->execute();
}
public function delete() {
db_delete('quiz_cloze_user_answers')
->condition('question_nid', $this->question
->id())
->condition('question_vid', $this->question
->getRevisionId())
->condition('result_id', $this->rid)
->execute();
}
public function score() {
$shortAnswer = new ClozeQuestion($this->question);
$score = $shortAnswer
->evaluateAnswer($this->answer);
return $score;
}
public function getResponse() {
return $this->answer;
}
public function getReportForm($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
$form = parent::getReportForm($showpoints, $showfeedback, $allow_scoring);
$question = strip_tags($form['question']['#markup']);
$question_form['open_wrapper'] = array(
'#markup' => '<div class="cloze-question">',
);
foreach (_cloze_get_question_chunks($question) as $position => $chunk) {
if (strpos($chunk, '[') === FALSE) {
$question_form['tries[' . $position . ']'] = array(
'#markup' => str_replace("\n", "<br/>", $chunk),
'#prefix' => '<div class="form-item">',
'#suffix' => '</div>',
);
}
else {
$chunk = str_replace(array(
'[',
']',
), '', $chunk);
$choices = explode(',', $chunk);
if (count($choices) > 1) {
$question_form['tries[' . $position . ']'] = array(
'#type' => 'select',
'#title' => '',
'#options' => _cloze_shuffle_choices(drupal_map_assoc($choices)),
'#required' => FALSE,
);
}
else {
$question_form['tries[' . $position . ']'] = array(
'#type' => 'textfield',
'#title' => '',
'#size' => 32,
'#required' => FALSE,
'#attributes' => array(
'autocomplete' => 'off',
),
);
}
}
}
$question_form['close_wrapper'] = array(
'#markup' => '</div>',
);
$form['question']['#markup'] = drupal_render($question_form);
return $form;
}
public function getReportFormResponse($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
$form = array();
$form['#theme'] = 'cloze_response_form';
$form['#attached']['css'] = array(
drupal_get_path('module', 'cloze') . '/css/cloze.css',
);
if ($this->question && !empty($this->question->answers)) {
$answer = (object) current($this->question->answers);
}
else {
return $form;
}
$body = $this->question->body
->getValue();
$this->question = node_load($this->question
->id());
$question = $body[0]['value'];
$correct_answer = _cloze_get_correct_answer($question);
$user_answer = _cloze_get_user_answer($question, $this->answer);
$form['answer'] = array(
'#markup' => theme('cloze_user_answer', array(
'answer' => $user_answer,
'correct' => $correct_answer,
)),
);
return $form;
}
public function getReportFormScore($showfeedback = TRUE, $showpoints = TRUE, $allow_scoring = FALSE) {
return array(
'#markup' => $this
->getScore(),
);
}
}