class MatchingResponse in Quiz 6.4
Same name and namespace in other branches
- 6.6 question_types/matching/matching.classes.inc \MatchingResponse
- 6.3 question_types/matching/matching.classes.inc \MatchingResponse
- 6.5 question_types/matching/matching.classes.inc \MatchingResponse
- 7.6 question_types/matching/matching.classes.inc \MatchingResponse
- 7 question_types/matching/matching.classes.inc \MatchingResponse
- 7.4 question_types/matching/matching.classes.inc \MatchingResponse
- 7.5 question_types/matching/matching.classes.inc \MatchingResponse
Extension of QuizQuestionResponse
Hierarchy
- class \QuizQuestionResponse
- class \MatchingResponse
Expanded class hierarchy of MatchingResponse
1 string reference to 'MatchingResponse'
- matching_quiz_question_info in question_types/
matching/ matching.module - Implementation of hook_quiz_question_info().
File
- question_types/
matching/ matching.classes.inc, line 345 - matching.classes
View source
class MatchingResponse extends QuizQuestionResponse {
/**
* Constructor
*/
public function __construct($result_id, stdClass $question_node, $answer = NULL) {
parent::__construct($result_id, $question_node, $answer);
if (!isset($answer)) {
$sql = '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 = %d AND n.vid = %d AND ua.result_id = %d';
$res = db_query($sql, $question_node->nid, $question_node->vid, $result_id);
$this->answer = array();
while ($obj = db_fetch_object($res)) {
$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() {
$user_answers = isset($this->answer) ? $this->answer : array();
// to prevent warning : Invalid argument supplied for foreach()
foreach ((array) $user_answers as $key => $value) {
$score = $key == $value ? 1 : 0;
$sql = 'INSERT INTO {quiz_matching_user_answers} (match_id, result_id, answer, score) VALUES (%d, %d, %d, %d)';
db_query($sql, $key, $this->rid, (int) $value, $score);
}
}
/**
* Implementation of delete
*
* @see QuizQuestionResponse#delete()
*/
public function delete() {
$sql = 'DELETE FROM {quiz_matching_user_answers}
WHERE match_id IN (
SELECT match_id
FROM {quiz_matching_node}
WHERE nid = %d AND vid = %d
)
AND result_id = %d';
db_query($sql, $this->question->nid, $this->question->vid, $this->rid);
}
/**
* 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');
if ($showfeedback) {
$metadata[] = t('Feedback');
}
$MatchingQuestion = new MatchingQuestion($this->question);
$correct_answers = $MatchingQuestion
->getCorrectAnswer();
$user_answers = isset($this->answer['answer']) ? $this->answer['answer'] : $this->answer;
foreach ($correct_answers as $correct_answer) {
$answer_data = array();
$id = $user_answers[$correct_answer['match_id']];
$correct = isset($correct_answers[$id]) && $correct_answer['answer'] == $correct_answers[$id]['answer'];
$answer_data['question'] = check_markup($correct_answer['question'], $this->question->format, FALSE);
if ($showpoints) {
$answer_data['correct_answer'] = check_markup($correct_answer['answer'], $this->question->format, FALSE);
}
$answer_data['user_answer'] = isset($correct_answers[$id]) ? check_markup($correct_answers[$id]['answer'], $this->question->format, FALSE) : NULL;
if ($showfeedback) {
$answer_data['feedback'] = check_markup($correct_answer['feedback'], $this->question->format, FALSE);
$answer_data['is_correct'] = $correct;
}
$data[] = $answer_data;
}
return array(
'#type' => 'markup',
'#value' => theme('matching_response', $metadata, $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:: |
protected | property | ||
QuizQuestionResponse:: |
protected | property | 7 | |
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 | 2 |
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. | 1 | |
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. |