MDL-27413 qtype_multianswer more coding style cleanup.

This commit is contained in:
Tim Hunt 2011-05-26 11:25:26 +01:00
parent df4be1222e
commit 12c6e0087e
4 changed files with 153 additions and 149 deletions

View file

@ -1,5 +1,4 @@
<?php <?php
// This file is part of Moodle - http://moodle.org/ // This file is part of Moodle - http://moodle.org/
// //
// Moodle is free software: you can redistribute it and/or modify // Moodle is free software: you can redistribute it and/or modify
@ -69,14 +68,12 @@ class qtype_multianswer_question extends question_graded_automatically {
public function start_attempt(question_attempt_step $step) { public function start_attempt(question_attempt_step $step) {
foreach ($this->subquestions as $i => $subq) { foreach ($this->subquestions as $i => $subq) {
$subq->start_attempt($this->get_substep($step, $i)); $subq->start_attempt($this->get_substep($step, $i));
} }
} }
public function apply_attempt_state(question_attempt_step $step) { public function apply_attempt_state(question_attempt_step $step) {
foreach ($this->subquestions as $i => $subq) { foreach ($this->subquestions as $i => $subq) {
$subq->apply_attempt_state($this->get_substep($step, $i)); $subq->apply_attempt_state($this->get_substep($step, $i));
} }
} }

View file

@ -92,7 +92,8 @@ class qtype_multianswer extends question_type {
array('question' => $question->id))) { array('question' => $question->id))) {
$oldwrappedquestions = array(); $oldwrappedquestions = array();
} else { } else {
$oldwrappedquestions = $DB->get_records_list('question', 'id', explode(',', $oldwrappedids), 'id ASC'); $oldwrappedquestions = $DB->get_records_list('question', 'id',
explode(',', $oldwrappedids), 'id ASC');
} }
$sequence = array(); $sequence = array();
@ -100,7 +101,8 @@ class qtype_multianswer extends question_type {
if (!empty($wrapped)) { if (!empty($wrapped)) {
// if we still have some old wrapped question ids, reuse the next of them // if we still have some old wrapped question ids, reuse the next of them
if (is_array($oldwrappedquestions) && $oldwrappedquestion = array_shift($oldwrappedquestions)) { if (is_array($oldwrappedquestions) &&
$oldwrappedquestion = array_shift($oldwrappedquestions)) {
$wrapped->id = $oldwrappedquestion->id; $wrapped->id = $oldwrappedquestion->id;
if ($oldwrappedquestion->qtype != $wrapped->qtype) { if ($oldwrappedquestion->qtype != $wrapped->qtype) {
switch ($oldwrappedquestion->qtype) { switch ($oldwrappedquestion->qtype) {
@ -152,7 +154,8 @@ class qtype_multianswer extends question_type {
$multianswer = new stdClass(); $multianswer = new stdClass();
$multianswer->question = $question->id; $multianswer->question = $question->id;
$multianswer->sequence = implode(',', $sequence); $multianswer->sequence = implode(',', $sequence);
if ($oldid = $DB->get_field('question_multianswer', 'id', array('question' => $question->id))) { if ($oldid = $DB->get_field('question_multianswer', 'id',
array('question' => $question->id))) {
$multianswer->id = $oldid; $multianswer->id = $oldid;
$DB->update_record('question_multianswer', $multianswer); $DB->update_record('question_multianswer', $multianswer);
} else { } else {
@ -248,7 +251,8 @@ define('NUMERICAL_ABS_ERROR_MARGIN', 6);
// Remaining ANSWER regexes // Remaining ANSWER regexes
define('ANSWER_TYPE_DEF_REGEX', define('ANSWER_TYPE_DEF_REGEX',
'(NUMERICAL|NM)|(MULTICHOICE|MC)|(MULTICHOICE_V|MCV)|(MULTICHOICE_H|MCH)|(SHORTANSWER|SA|MW)|(SHORTANSWER_C|SAC|MWC)'); '(NUMERICAL|NM)|(MULTICHOICE|MC)|(MULTICHOICE_V|MCV)|(MULTICHOICE_H|MCH)|' .
'(SHORTANSWER|SA|MW)|(SHORTANSWER_C|SAC|MWC)');
define('ANSWER_START_REGEX', define('ANSWER_START_REGEX',
'\{([0-9]*):(' . ANSWER_TYPE_DEF_REGEX . '):'); '\{([0-9]*):(' . ANSWER_TYPE_DEF_REGEX . '):');

View file

@ -1,5 +1,4 @@
<?php <?php
// This file is part of Moodle - http://moodle.org/ // This file is part of Moodle - http://moodle.org/
// //
// Moodle is free software: you can redistribute it and/or modify // Moodle is free software: you can redistribute it and/or modify
@ -156,13 +155,15 @@ class qtype_multianswer_textfield_renderer extends qtype_renderer {
if ($options->rightanswer) { if ($options->rightanswer) {
$correct = $subq->get_matching_answer($subq->get_correct_response()); $correct = $subq->get_matching_answer($subq->get_correct_response());
$feedback[] = get_string('correctansweris', 'qtype_shortanswer', s($correct->answer)); $feedback[] = get_string('correctansweris', 'qtype_shortanswer',
s($correct->answer));
} }
$subfraction = ''; $subfraction = '';
if ($options->marks >= question_display_options::MARK_AND_MAX && $subq->maxmark > 0) { if ($options->marks >= question_display_options::MARK_AND_MAX && $subq->maxmark > 0) {
$a = new stdClass(); $a = new stdClass();
$a->mark = format_float($matchinganswer->fraction * $subq->maxmark, $options->markdp); $a->mark = format_float($matchinganswer->fraction * $subq->maxmark,
$options->markdp);
$a->max = format_float($subq->maxmark, $options->markdp); $a->max = format_float($subq->maxmark, $options->markdp);
$feedback[] = get_string('markoutofmax', 'question', $a); $feedback[] = get_string('markoutofmax', 'question', $a);
} }
@ -182,11 +183,14 @@ class qtype_multianswer_textfield_renderer extends qtype_renderer {
} }
} }
/** /**
* As multianswer have specific display requirements for multichoice display * As multianswer have specific display requirements for multichoice display
* a new class was defined although largely following the multichoice one * a new class was defined although largely following the multichoice one
*
* @copyright 2010 Pierre Pichet
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
abstract class qtype_multianswer_multichoice_renderer_base extends qtype_renderer { abstract class qtype_multianswer_multichoice_renderer_base extends qtype_renderer {
abstract protected function get_input_type(); abstract protected function get_input_type();
@ -202,8 +206,6 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
abstract protected function get_response(question_attempt $qa); abstract protected function get_response(question_attempt $qa);
public function specific_feedback(question_attempt $qa) { public function specific_feedback(question_attempt $qa) {
return ''; return '';
} }
@ -213,7 +215,7 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
$questiontot = $qa->get_question(); $questiontot = $qa->get_question();
$subquestion = $questiontot->subquestions[$qa->subquestionindex]; $subquestion = $questiontot->subquestions[$qa->subquestionindex];
$order = $subquestion->get_order($qa); //array_keys($question->answers); // $order = $subquestion->get_order($qa);
$response = $this->get_response($qa); $response = $this->get_response($qa);
$inputattributes = array( $inputattributes = array(
'type' => $this->get_input_type(), 'type' => $this->get_input_type(),
@ -231,13 +233,12 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
foreach ($order as $value => $ansid) { foreach ($order as $value => $ansid) {
$ans = $subquestion->answers[$ansid]; $ans = $subquestion->answers[$ansid];
$inputattributes['name'] = $this->get_input_name($qa, $value); $inputattributes['name'] = $this->get_input_name($qa, $value);
// echo "<p>name $value name".$inputattributes['name']." </p>";
$inputattributes['value'] = $this->get_input_value($value); $inputattributes['value'] = $this->get_input_value($value);
$inputattributes['id'] = $this->get_input_id($qa, $value); $inputattributes['id'] = $this->get_input_id($qa, $value);
if ($subquestion->single) { if ($subquestion->single) {
$isselected = $this->is_choice_selected($response, $value); $isselected = $this->is_choice_selected($response, $value);
} else { } else {
$isselected = $this->is_choice_selected($response,$value) ; //$subquestion->field( $value)); $isselected = $this->is_choice_selected($response, $value);
} }
if ($isselected) { if ($isselected) {
$inputattributes['checked'] = 'checked'; $inputattributes['checked'] = 'checked';
@ -298,8 +299,8 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
$result .= html_writer::end_tag('table'); // answer $result .= html_writer::end_tag('table'); // answer
$result .= html_writer::end_tag('div'); // ablock $result .= html_writer::end_tag('div'); // ablock
} }
if ($options->feedback) { if ($options->feedback) {
$result .= html_writer::start_tag('div', array('class' => 'outcome')); $result .= html_writer::start_tag('div', array('class' => 'outcome'));
@ -312,8 +313,7 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
array('class' => 'validationerror')); array('class' => 'validationerror'));
$result1 = ($subquestion->single) ? get_string('singleanswer', 'quiz') : get_string('multipleanswers', 'quiz'); $result1 = ($subquestion->single) ? get_string('singleanswer', 'quiz') : get_string('multipleanswers', 'quiz');
$result .= html_writer::nonempty_tag('div', $result1, $result .= html_writer::nonempty_tag('div', $result1,
array('class' => 'validationerror')) array('class' => 'validationerror'));
;
} else { } else {
$state = $qa->get_state(); $state = $qa->get_state();
$state = question_state::graded_state_for_fraction($totfraction); $state = question_state::graded_state_for_fraction($totfraction);
@ -323,11 +323,9 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
} }
} }
if ($options->correctresponse) { if ($options->correctresponse) {
$result1 = $this->correct_response($qa); $result1 = $this->correct_response($qa);
$result .= html_writer::nonempty_tag('div',$result1, array('class' => 'outcome')) $result .= html_writer::nonempty_tag('div', $result1, array('class' => 'outcome'));
;
} }
if ($options->marks) { if ($options->marks) {
$subgrade= $totfraction * $subquestion->defaultmark; $subgrade= $totfraction * $subquestion->defaultmark;
@ -356,15 +354,18 @@ class qtype_multianswer_multichoice_single_renderer extends qtype_multianswer_mu
protected function is_choice_selected($response, $value) { protected function is_choice_selected($response, $value) {
return $response == $value; return $response == $value;
} }
protected function is_right(question_answer $ans) { protected function is_right(question_answer $ans) {
return $ans->fraction > 0.9999999; return $ans->fraction > 0.9999999;
} }
protected function get_input_name(question_attempt $qa, $value) { protected function get_input_name(question_attempt $qa, $value) {
$questiontot = $qa->get_question(); $questiontot = $qa->get_question();
$subquestion = $questiontot->subquestions[$qa->subquestionindex]; $subquestion = $questiontot->subquestions[$qa->subquestionindex];
$answername = $subquestion->fieldid.'answer'; $answername = $subquestion->fieldid.'answer';
return $qa->get_qt_field_name($answername); return $qa->get_qt_field_name($answername);
} }
protected function get_input_value($value) { protected function get_input_value($value) {
return $value; return $value;
} }
@ -382,6 +383,7 @@ class qtype_multianswer_multichoice_single_renderer extends qtype_multianswer_mu
return $qa->get_last_qt_var($subquestion->fieldid.'answer', -1); return $qa->get_last_qt_var($subquestion->fieldid.'answer', -1);
} }
public function correct_response(question_attempt $qa) { public function correct_response(question_attempt $qa) {
$questiontot = $qa->get_question(); $questiontot = $qa->get_question();
$subquestion = $questiontot->subquestions[$qa->subquestionindex]; $subquestion = $questiontot->subquestions[$qa->subquestionindex];
@ -395,8 +397,9 @@ class qtype_multianswer_multichoice_single_renderer extends qtype_multianswer_mu
return ''; return '';
} }
} }
class qtype_multianswer_multichoice_single_inline_renderer extends qtype_multianswer_multichoice_single_renderer { class qtype_multianswer_multichoice_single_inline_renderer extends qtype_multianswer_multichoice_single_renderer {
protected function get_input_type() { protected function get_input_type() {
return 'select'; return 'select';
@ -483,7 +486,6 @@ class qtype_multianswer_multichoice_single_inline_renderer extends qtype_multian
} }
} }
if ($options->correctresponse) { if ($options->correctresponse) {
$feedback .= $this->correct_response($qa)."<br />"; $feedback .= $this->correct_response($qa)."<br />";
} }
@ -513,13 +515,11 @@ class qtype_multianswer_multichoice_single_inline_renderer extends qtype_multian
$result .= "<span $popup >"; $result .= "<span $popup >";
$result .= html_writer::start_tag('span', array('class' => $classes), ''); $result .= html_writer::start_tag('span', array('class' => $classes), '');
$result .= $result .= choose_from_menu($choices, $inputname, $chosen,
choose_from_menu($choices, $inputname, $chosen,
' ', '', '', true, $options->readonly) . $feedbackimage; ' ', '', '', true, $options->readonly) . $feedbackimage;
$result .= html_writer::end_tag('span'); $result .= html_writer::end_tag('span');
$result .= html_writer::end_tag('span'); $result .= html_writer::end_tag('span');
return $result; return $result;
} }

View file

@ -90,9 +90,12 @@ class qtype_multianswer_test_helper extends question_test_helper {
$mc->layout = 1; $mc->layout = 1;
$mc->answers = array( $mc->answers = array(
13 => new question_answer(13, 'Bow-wow', 0, 'You seem to have a dog obsessions!', FORMAT_HTML), 13 => new question_answer(13, 'Bow-wow', 0,
14 => new question_answer(14, 'Wiggly worm', 0, 'Now you are just being rediculous!', FORMAT_HTML), 'You seem to have a dog obsessions!', FORMAT_HTML),
15 => new question_answer(15, 'Pussy-cat', 1, 'Well done!', FORMAT_HTML), 14 => new question_answer(14, 'Wiggly worm', 0,
'Now you are just being rediculous!', FORMAT_HTML),
15 => new question_answer(15, 'Pussy-cat', 1,
'Well done!', FORMAT_HTML),
); );
$mc->qtype = question_bank::get_qtype('multichoice'); $mc->qtype = question_bank::get_qtype('multichoice');
$mc->maxmark = 1; $mc->maxmark = 1;