class MultichoiceResponse in Quiz 7.6
Same name and namespace in other branches
- 6.4 question_types/multichoice/multichoice.classes.inc \MultichoiceResponse
- 7 question_types/multichoice/multichoice.classes.inc \MultichoiceResponse
- 7.4 question_types/multichoice/multichoice.classes.inc \MultichoiceResponse
- 7.5 question_types/multichoice/multichoice.classes.inc \MultichoiceResponse
Extension of QuizQuestionResponse
Hierarchy
- class \QuizQuestionResponse
- class \MultichoiceResponse
Expanded class hierarchy of MultichoiceResponse
1 string reference to 'MultichoiceResponse'
- multichoice_quiz_question_info in question_types/
multichoice/ multichoice.module - Implements hook_quiz_question_info().
File
- question_types/
multichoice/ multichoice.classes.inc, line 799 - The main classes for the multichoice question type.
View source
class MultichoiceResponse extends QuizQuestionResponse {
/**
* ID of the answers.
*/
protected $user_answer_ids;
protected $choice_order;
/**
* Constructor.
*
* @param type $result_id
* @param stdClass $question_node
* @param type $values
* Form state values.
*/
public function __construct($result_id, stdClass $question_node, $values = NULL) {
parent::__construct($result_id, $question_node, $values['user_answer']);
$this->user_answer_ids = array();
// tries is the tries part of the post data
if (isset($values['user_answer'])) {
if (!is_array($values['user_answer'])) {
// Account for single-select
$values['user_answer'] = array(
$values['user_answer'],
);
}
if (isset($values['choice_order'])) {
$this->choice_order = $values['choice_order'];
}
unset($values['choice_order']);
if (isset($values['user_answer']) && is_array($values['user_answer'])) {
foreach ($values['user_answer'] as $answer_id) {
$this->user_answer_ids[] = $answer_id;
$this->answer = $this->user_answer_ids;
//@todo: Stop using user_answer_ids and only use answer instead...
}
}
elseif (isset($values['user_answer'])) {
$this->user_answer_ids[] = $values['user_answer'];
}
}
else {
$res = db_query('SELECT answer_id FROM {quiz_multichoice_user_answers} ua
LEFT OUTER JOIN {quiz_multichoice_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->nid,
':question_vid' => $this->question->vid,
));
while ($res_o = $res
->fetch()) {
$this->user_answer_ids[] = $res_o->answer_id;
}
}
}
/**
* Implementation of save
*
* @see QuizQuestionResponse#save()
*/
public function save() {
$user_answer_id = db_insert('quiz_multichoice_user_answers')
->fields(array(
'result_id' => $this->result_id,
'question_vid' => $this->question->vid,
'question_nid' => $this->question->nid,
'choice_order' => $this->choice_order,
))
->execute();
$query = db_insert('quiz_multichoice_user_answer_multi')
->fields(array(
'user_answer_id',
'answer_id',
));
for ($i = 0; $i < count($this->user_answer_ids); $i++) {
if ($this->user_answer_ids[$i]) {
$query
->values(array(
$user_answer_id,
$this->user_answer_ids[$i],
));
}
}
$query
->execute();
}
/**
* Implementation of delete
*
* @see QuizQuestionResponse#delete()
*/
public function delete() {
$user_answer_id = array();
$query = db_query('SELECT id FROM {quiz_multichoice_user_answers} WHERE question_nid = :nid AND question_vid = :vid AND result_id = :result_id', array(
':nid' => $this->question->nid,
':vid' => $this->question->vid,
':result_id' => $this->result_id,
));
while ($user_answer = $query
->fetch()) {
$user_answer_id[] = $user_answer->id;
}
if (!empty($user_answer_id)) {
db_delete('quiz_multichoice_user_answer_multi')
->condition('user_answer_id', $user_answer_id, 'IN')
->execute();
}
db_delete('quiz_multichoice_user_answers')
->condition('result_id', $this->result_id)
->condition('question_nid', $this->question->nid)
->condition('question_vid', $this->question->vid)
->execute();
}
/**
* Implementation of score
*
* @return uint
*
* @see QuizQuestionResponse#score()
*/
public function score() {
if ($this->question->choice_boolean || $this
->isAllWrong()) {
$score = $this
->getMaxScore();
foreach ($this->question->alternatives as $key => $alt) {
if (in_array($alt['id'], $this->user_answer_ids)) {
if ($alt['score_if_chosen'] <= $alt['score_if_not_chosen']) {
$score = 0;
}
}
else {
if ($alt['score_if_chosen'] > $alt['score_if_not_chosen']) {
$score = 0;
}
}
}
}
else {
$score = 0;
foreach ($this->question->alternatives as $key => $alt) {
if (in_array($alt['id'], $this->user_answer_ids)) {
$score += $alt['score_if_chosen'];
}
else {
$score += $alt['score_if_not_chosen'];
}
}
}
return $score;
}
/**
* If all answers in a question is wrong
*
* @return boolean
* TRUE if all answers are wrong. False otherwise.
*/
public function isAllWrong() {
foreach ($this->question->alternatives as $key => $alt) {
if ($alt['score_if_chosen'] > 0 || $alt['score_if_not_chosen'] > 0) {
return FALSE;
}
}
return TRUE;
}
/**
* Implementation of getResponse
*
* @return answer
*
* @see QuizQuestionResponse#getResponse()
*/
public function getResponse() {
return $this->user_answer_ids;
}
/**
* Implementation of getReportFormResponse
*/
public function getFeedbackValues() {
$this
->orderAlternatives($this->question->alternatives);
$simple_scoring = $this->question->choice_boolean;
$data = array();
foreach ($this->question->alternatives as $alternative) {
$chosen = in_array($alternative['id'], $this->user_answer_ids);
$not = $chosen ? '' : 'not_';
$data[] = array(
'choice' => check_markup($alternative['answer']['value'], $alternative['answer']['format']),
'attempt' => $chosen ? quiz_icon('selected') : '',
'correct' => $chosen ? $alternative['score_if_chosen'] > 0 ? quiz_icon('correct') : quiz_icon('incorrect') : '',
'score' => $alternative["score_if_{$not}chosen"],
'answer_feedback' => check_markup($alternative["feedback_if_{$not}chosen"]['value'], $alternative["feedback_if_{$not}chosen"]['format'], FALSE),
'question_feedback' => 'Question feedback',
'solution' => $alternative['score_if_chosen'] > 0 ? quiz_icon('should') : ($simple_scoring ? quiz_icon('should-not') : ''),
'quiz_feedback' => "Quiz feedback",
);
}
return $data;
}
/**
* Order the alternatives according to the choice order stored in the database
*
* @param array $alternatives
* The alternatives to be ordered
*/
protected function orderAlternatives(array &$alternatives) {
if (!$this->question->choice_random) {
return;
}
$result = db_query('SELECT choice_order FROM {quiz_multichoice_user_answers}
WHERE result_id = :result_id AND question_nid = :question_nid AND question_vid = :question_vid', array(
':result_id' => $this->result_id,
':question_nid' => $this->question->nid,
':question_vid' => $this->question->vid,
))
->fetchField();
if (!$result) {
return;
}
$order = explode(',', $result);
$newAlternatives = array();
foreach ($order as $value) {
foreach ($alternatives as $alternative) {
if ($alternative['id'] == $value) {
$newAlternatives[] = $alternative;
break;
}
}
}
$alternatives = $newAlternatives;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MultichoiceResponse:: |
protected | property | ||
MultichoiceResponse:: |
protected | property | ID of the answers. | |
MultichoiceResponse:: |
public | function |
Implementation of delete Overrides QuizQuestionResponse:: |
|
MultichoiceResponse:: |
public | function |
Implementation of getReportFormResponse Overrides QuizQuestionResponse:: |
|
MultichoiceResponse:: |
public | function |
Implementation of getResponse Overrides QuizQuestionResponse:: |
|
MultichoiceResponse:: |
public | function | If all answers in a question is wrong | |
MultichoiceResponse:: |
protected | function | Order the alternatives according to the choice order stored in the database | |
MultichoiceResponse:: |
public | function |
Implementation of save Overrides QuizQuestionResponse:: |
|
MultichoiceResponse:: |
public | function |
Implementation of score Overrides QuizQuestionResponse:: |
|
MultichoiceResponse:: |
public | function |
Constructor. Overrides QuizQuestionResponse:: |
|
QuizQuestionResponse:: |
protected | property | ||
QuizQuestionResponse:: |
protected | property | ||
QuizQuestionResponse:: |
protected | property | ||
QuizQuestionResponse:: |
public | property | ||
QuizQuestionResponse:: |
public | property | ||
QuizQuestionResponse:: |
public | property | ||
QuizQuestionResponse:: |
public | property | ||
QuizQuestionResponse:: |
protected | property | ||
QuizQuestionResponse:: |
protected | property | 8 | |
QuizQuestionResponse:: |
public | function | Can the quiz taker view the requested review? | |
QuizQuestionResponse:: |
public | function | Returns a renderable array of question feedback. | |
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:: |
function | |||
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. | 2 |
QuizQuestionResponse:: |
public | function | 2 | |
QuizQuestionResponse:: |
public | function | Implementation of getReportFormScore | |
QuizQuestionResponse:: |
public | function | Get the submit function for the reportForm | 2 |
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. | 2 | |
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 | Validates response from a quiz taker. If the response isn't valid the quiz taker won't be allowed to proceed. | |
QuizQuestionResponse:: |
public | function | Used to refresh this instances question node in case drupal has changed it. | |
QuizQuestionResponse:: |
public | function | Set the target result ID for this Question response. | |
QuizQuestionResponse:: |
function | Represent the response as a stdClass object. |