ShortAnswerResponse.php in Quiz 8.4
The main classes for the short answer response.
Contains \Drupal\short_answer\ShortAnswerResponse.
Namespace
Drupal\short_answerFile
question_types/short_answer/lib/Drupal/short_answer/ShortAnswerResponse.phpView source
<?php
/**
* @file
*
* The main classes for the short answer response.
*
* Contains \Drupal\short_answer\ShortAnswerResponse.
*/
namespace Drupal\short_answer;
use Drupal\quiz_question\QuizQuestionResponse;
use Drupal\short_answer\ShortAnswerQuestion;
/**
* Extension of QuizQuestionResponse
*/
class ShortAnswerResponse extends QuizQuestionResponse {
/**
* Get all quiz scores that haven't been evaluated yet.
*
* @param $count
* Number of items to return (default: 50).
* @param $offset
* Where in the results we should start (default: 0).
*
* @return
* Array of objects describing unanswered questions. Each object will have result_id, question_nid, and question_vid.
*/
public static function fetchAllUnscoredAnswers($count = 50, $offset = 0) {
$user = \Drupal::currentUser();
$query = db_select('quiz_short_answer_user_answers', 'a');
$query
->fields('a', array(
'result_id',
'question_nid',
'question_vid',
'answer_feedback',
));
$query
->fields('nd', array(
'title',
));
$query
->fields('qnr', array(
'time_end',
'time_start',
'uid',
));
$query
->join('quiz_node_results', 'qnr', 'a.result_id = qnr.result_id');
$query
->join('node', 'n', 'qnr.nid = n.nid');
$query
->join('node_field_data', 'nd', 'nd.nid = n.nid AND nd.vid = n.vid');
$query
->condition('a.is_evaluated', 0);
if ($user
->hasPermission('score own quiz') && $user
->hasPermission('score taken quiz answer')) {
$query
->condition(db_or()
->condition('nd.uid', $user
->id())
->condition('qnr.uid', $user
->id()));
}
else {
if ($user
->hasPermission('score own quiz')) {
$query
->condition('nd.uid', $user
->id());
}
else {
if ($user
->hasPermission('score taken quiz answer')) {
$query
->condition('qnr.uid', $user
->id());
}
}
}
$results = $query
->execute();
$unscored = array();
foreach ($results as $row) {
$unscored[] = $row;
}
return $unscored;
}
/**
* Given a question, return a list of all of the unscored answers.
*
* @param $nid
* Node ID for the question to check.
* @param $vid
* Version ID for the question to check.
* @param $count
* Number of items to return (default: 50).
* @param $offset
* Where in the results we should start (default: 0).
*
* @return
* Indexed array of result IDs that need to be scored.
*/
public static function fetchUnscoredAnswersByQuestion($nid, $vid, $count = 50, $offset = 0) {
return db_query('SELECT result_id FROM {quiz_short_answer_user_answers}
WHERE is_evaluated = :is_evaluated AND question_nid = :question_nid AND question_vid = :question_vid', array(
':is_evaluated' => 0,
':question_nid' => $nid,
':question_vid' => $vid,
))
->fetchCol();
}
/**
* ID of the answer.
*/
protected $answer_id = 0;
/**
* Constructor
*/
public function __construct($result_id, \Drupal\node\Entity\Node $question_node, $answer = NULL) {
parent::__construct($result_id, $question_node, $answer);
if (!isset($answer)) {
$r = db_query('SELECT answer_id, answer, is_evaluated, score, question_vid, question_nid, result_id, answer_feedback
FROM {quiz_short_answer_user_answers}
WHERE question_nid = :qnid AND question_vid = :qvid AND result_id = :rid', array(
':qnid' => $question_node
->id(),
':qvid' => $question_node
->getRevisionId(),
':rid' => $result_id,
))
->fetch();
if (!empty($r)) {
$this->answer = $r->answer;
$this->score = $r->score;
$this->evaluated = $r->is_evaluated;
$this->answer_id = $r->answer_id;
$this->answer_feedback = $r->answer_feedback;
}
}
else {
if (is_array($answer)) {
$this->answer = $answer['answer'];
}
else {
$this->answer = $answer;
$this->evaluated = $this->question->correct_answer_evaluation != ShortAnswerQuestion::ANSWER_MANUAL;
}
}
}
/**
* Implementation of isValid
*
* @see QuizQuestionResponse#isValid()
*/
public function isValid() {
if (trim($this->answer) == '') {
return t('You must provide an answer');
}
return TRUE;
}
/**
* Implementation of save
*
* @see QuizQuestionResponse#save()
*/
public function save() {
// We need to set is_evaluated depending on whether the type requires evaluation.
$this->is_evaluated = (int) ($this->question->correct_answer_evaluation != ShortAnswerQuestion::ANSWER_MANUAL);
$this->answer_id = db_insert('quiz_short_answer_user_answers')
->fields(array(
'answer' => $this->answer,
'question_nid' => $this->question
->id(),
'question_vid' => $this->question
->getRevisionId(),
'result_id' => $this->rid,
'score' => $this
->getScore(FALSE),
'is_evaluated' => $this->is_evaluated,
))
->execute();
}
/**
* Implementation of delete
*
* @see QuizQuestionResponse#delete()
*/
public function delete() {
db_delete('quiz_short_answer_user_answers')
->condition('question_nid', $this->question
->id())
->condition('question_vid', $this->question
->getRevisionId())
->condition('result_id', $this->rid)
->execute();
}
/**
* Implementation of score
*
* @see QuizQuestionResponse#score()
*/
public function score() {
// Manual scoring means we go with what is in the DB.
if ($this->question->correct_answer_evaluation == ShortAnswerQuestion::ANSWER_MANUAL) {
$score = db_query('SELECT score FROM {quiz_short_answer_user_answers} WHERE result_id = :result_id AND question_vid = :question_vid', array(
':result_id' => $this->rid,
':question_vid' => $this->question
->getRevisionId(),
))
->fetchField();
if (!$score) {
$score = 0;
}
}
else {
$shortAnswer = new ShortAnswerQuestion($this->question);
$score = $shortAnswer
->evaluateAnswer($this
->getResponse());
}
return $score;
}
/**
* Implementation of getResponse
*
* @see QuizQuestionResponse#getResponse()
*/
public function getResponse() {
return $this->answer;
}
/**
* Implementation of getReportFormResponse
*
* @see QuizQuestionResponse#getReportFormResponse($showpoints, $showfeedback, $allow_scoring)
*/
public function getReportFormResponse($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
$form = array();
$form['#theme'] = 'short_answer_response_form';
if ($this->question && !empty($this->question->answers)) {
$answer = (object) current($this->question->answers);
}
else {
return $form;
}
$form['answer'] = array(
'#markup' => theme('short_answer_user_answer', array(
'answer' => check_plain($answer->answer),
'correct' => check_plain($this->question->correct_answer),
)),
);
if ($answer->is_evaluated == 1) {
// Show feedback, if any.
if ($showfeedback && !empty($answer->feedback)) {
// @todo: Feedback doesn't seem to be in use anymore...
$feedback = check_markup($answer->feedback);
}
}
else {
$feedback = t('This answer has not yet been scored.') . '<br/>' . t('Until the answer is scored, the total score will not be correct.');
}
if (!$allow_scoring && !empty($this->answer_feedback)) {
$form['answer_feedback'] = array(
'#title' => t('Feedback'),
'#type' => 'item',
'#markup' => '<span class="quiz_answer_feedback">' . $this->answer_feedback . '</span>',
);
}
if (!empty($feedback)) {
$form['feedback'] = array(
'#markup' => '<span class="quiz_answer_feedback">' . $feedback . '</span>',
);
}
return $form;
}
/**
* Implementation of getReportFormScore
*
* @see QuizQuestionResponse#getReportFormScore($showpoints, $showfeedback, $allow_scoring)
*/
public function getReportFormScore($showfeedback = TRUE, $showpoints = TRUE, $allow_scoring = FALSE) {
$score = $this
->isEvaluated() ? $this
->getScore() : '?';
if (quiz_access_to_score() && $allow_scoring) {
return array(
'#type' => 'textfield',
'#default_value' => $score,
'#size' => 3,
'#maxlength' => 3,
'#attributes' => array(
'class' => array(
'quiz-report-score',
),
),
);
}
else {
return array(
'#markup' => $score,
);
}
}
public function getReportFormAnswerFeedback($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
if (quiz_access_to_score() && $allow_scoring && $this->question->correct_answer_evaluation == ShortAnswerQuestion::ANSWER_MANUAL) {
return array(
'#title' => t('Enter feedback'),
'#type' => 'textarea',
'#default_value' => $this->answer_feedback,
'#attributes' => array(
'class' => array(
'quiz-report-score',
),
),
);
}
return FALSE;
}
/**
* Implementation of getReportFormSubmit
*
* @see QuizQuestionResponse#getReportFormSubmit($showfeedback, $showpoints, $allow_scoring)
*/
public function getReportFormSubmit($showfeedback = TRUE, $showpoints = TRUE, $allow_scoring = FALSE) {
if (quiz_access_to_score() && $allow_scoring) {
return $allow_scoring ? 'short_answer_report_submit' : FALSE;
}
return FALSE;
}
/**
* Implementation of getReportFormValidate
*
* @see QuizQuestionResponse#getReportFormValidate($showfeedback, $showpoints, $allow_scoring)
*/
public function getReportFormValidate($showfeedback = TRUE, $showpoints = TRUE, $allow_scoring = FALSE) {
if (quiz_access_to_score() && $allow_scoring) {
return $allow_scoring ? 'short_answer_report_validate' : FALSE;
}
return FALSE;
}
}
Classes
Name | Description |
---|---|
ShortAnswerResponse | Extension of QuizQuestionResponse |