class MatchingResponse in Quiz 8.4
Extension of QuizQuestionResponse
Hierarchy
- class \Drupal\quiz_question\QuizQuestionResponse
- class \Drupal\matching\MatchingResponse
Expanded class hierarchy of MatchingResponse
File
- question_types/
matching/ lib/ Drupal/ matching/ MatchingResponse.php, line 14
Namespace
Drupal\matchingView source
class MatchingResponse extends QuizQuestionResponse {
/**
* Constructor
*/
public function __construct($result_id, $question_node, $answer = NULL) {
parent::__construct($result_id, $question_node, $answer);
if (!isset($answer)) {
$res = db_query('SELECT ua.answer, score, ua.match_id FROM {quiz_matching_user_answers} ua
JOIN {quiz_matching_node} n ON n.match_id = ua.match_id
WHERE n.nid = :nid AND n.vid = :vid AND ua.result_id = :result_id', array(
':nid' => $question_node
->id(),
':vid' => $question_node
->getRevisionId(),
':result_id' => $result_id,
));
$this->answer = array();
while ($obj = $res
->fetch()) {
$this->answer[$obj->match_id] = $obj->answer;
}
}
$this->is_correct = $this
->isCorrect();
}
/**
* Implementation of isValid
*
* @see QuizQuestionResponse#isValid()
*/
public function isValid() {
foreach ($this->answer as $value) {
if ($value != 'def') {
return TRUE;
}
}
return t('You need to match at least one of the items.');
}
/**
* Implementation of save
*
* @see QuizQuestionResponse#save()
*/
public function save() {
if (!isset($this->answer) || !is_array($this->answer)) {
return;
}
$insert = db_insert('quiz_matching_user_answers')
->fields(array(
'match_id',
'result_id',
'answer',
'score',
));
foreach ($this->answer as $key => $value) {
$insert
->values(array(
'match_id' => $key,
'result_id' => $this->rid,
'answer' => (int) $value,
'score' => $key == $value ? 1 : 0,
));
}
$insert
->execute();
}
/**
* Implementation of delete
*
* @see QuizQuestionResponse#delete()
*/
public function delete() {
$match_id = db_query('SELECT match_id FROM {quiz_matching_node} WHERE nid = :nid AND vid = :vid', array(
':nid' => $this->question
->id(),
':vid' => $this->question
->getRevisionId(),
))
->fetchCol();
db_delete('quiz_matching_user_answers')
->condition('match_id', is_array($match_id) ? $match_id : array(
0,
), 'IN')
->condition('result_id', $this->rid)
->execute();
}
/**
* Implementation of score
*
* @see QuizQuestionResponse#score()
*/
public function score() {
$wrong_answer = 0;
$correct_answer = 0;
$user_answers = isset($this->answer['answer']) ? $this->answer['answer'] : $this->answer;
foreach ((array) $user_answers as $key => $value) {
if ($key == $value) {
$correct_answer++;
}
elseif ($value == 0 || $value == 'def') {
}
else {
$wrong_answer++;
}
}
$score = $correct_answer - $wrong_answer;
return $score < 0 ? 0 : $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) {
$data = $metadata = array();
// Build the question answers header (add blank space for IE).
$metadata[] = t('Match');
if ($showpoints) {
$metadata[] = t('Correct Answer');
}
$metadata[] = t('User answer');
$MatchingQuestion = new MatchingQuestion($this->question);
$correct_answers = $MatchingQuestion
->getCorrectAnswer();
$user_answers = isset($this->answer['answer']) ? $this->answer['answer'] : $this->answer;
$has_feedback = TRUE;
foreach ($correct_answers as $correct_answer) {
$answer_data = array();
$correct = FALSE;
$id = NULL;
$answer_data['question'] = check_markup($correct_answer['question'], $this
->getFormat());
if (isset($user_answers[$correct_answer['match_id']])) {
$id = $user_answers[$correct_answer['match_id']];
$correct = isset($correct_answers[$id]) && $correct_answer['answer'] == $correct_answers[$id]['answer'];
$answer_data['user_answer'] = isset($correct_answers[$id]) ? check_markup($correct_answers[$id]['answer'], $this
->getFormat()) : NULL;
}
if ($showpoints) {
$answer_data['correct_answer'] = $correct_answer['answer'];
}
if ($showfeedback && !empty($correct_answer['feedback'])) {
$answer_data['feedback'] = check_markup($correct_answer['feedback'], $this
->getFormat());
}
else {
//$has_feedback = FALSE;
if ($showfeedback) {
$answer_data['feedback'] = '';
}
}
$answer_data['is_correct'] = $correct;
$data[] = $answer_data;
}
if ($showfeedback && $has_feedback) {
$metadata[] = t('Feedback');
}
return array(
'#markup' => theme('matching_response', array(
'metadata' => $metadata,
'data' => $data,
)),
);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MatchingResponse:: |
public | function |
Implementation of delete Overrides QuizQuestionResponse:: |
|
MatchingResponse:: |
public | function |
Implementation of getReportFormResponse Overrides QuizQuestionResponse:: |
|
MatchingResponse:: |
public | function |
Implementation of getResponse Overrides QuizQuestionResponse:: |
|
MatchingResponse:: |
public | function |
Implementation of isValid Overrides QuizQuestionResponse:: |
|
MatchingResponse:: |
public | function |
Implementation of save Overrides QuizQuestionResponse:: |
|
MatchingResponse:: |
public | function |
Implementation of score Overrides QuizQuestionResponse:: |
|
MatchingResponse:: |
public | function |
Constructor Overrides QuizQuestionResponse:: |
|
QuizQuestionResponse:: |
protected | property | ||
QuizQuestionResponse:: |
protected | property | ||
QuizQuestionResponse:: |
protected | property | ||
QuizQuestionResponse:: |
public | property | ||
QuizQuestionResponse:: |
public | property | ||
QuizQuestionResponse:: |
public | property | ||
QuizQuestionResponse:: |
protected | property | ||
QuizQuestionResponse:: |
protected | property | 8 | |
QuizQuestionResponse:: |
protected | function | Utility function that returns the format of the node body | |
QuizQuestionResponse:: |
public | function | Returns stored max score if it exists, if not the max score is calculated and returned. | |
QuizQuestionResponse:: |
public | function | Get data suitable for reporting a user's score on the question. This expects an object with the following attributes: | |
QuizQuestionResponse:: |
public | function | Creates the report form for the admin pages, and for when a user gets feedback after answering questions. | 1 |
QuizQuestionResponse:: |
public | function | 2 | |
QuizQuestionResponse:: |
public | function | get the question part of the reportForm | |
QuizQuestionResponse:: |
public | function | Get the score part of the report form | 3 |
QuizQuestionResponse:: |
public | function | Get the submit function for the reportForm | 2 |
QuizQuestionResponse:: |
public | function | Get the theme key for the reportForm | |
QuizQuestionResponse:: |
public | function | Get the validate function for the reportForm | 2 |
QuizQuestionResponse:: |
function | Returns stored score if it exists, if not the score is calculated and returned. | ||
QuizQuestionResponse:: |
function | Check to see if the answer is marked as correct. | ||
QuizQuestionResponse:: |
public | function | Indicate whether the response has been evaluated (scored) yet. Questions that require human scoring (e.g. essays) may need to manually toggle this. | |
QuizQuestionResponse:: |
public | function | Used to refresh this instances question node in case drupal has changed it. | |
QuizQuestionResponse:: |
public | function | Saves the quiz result. This is not used when a question is skipped! | |
QuizQuestionResponse:: |
function | Represent the response as a stdClass object. |