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
// This file is part of Moodle - http://moodle.org/
//
// 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) {
foreach ($this->subquestions as $i => $subq) {
$subq->start_attempt($this->get_substep($step, $i));
}
}
public function apply_attempt_state(question_attempt_step $step) {
foreach ($this->subquestions as $i => $subq) {
$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))) {
$oldwrappedquestions = array();
} 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();
@ -100,7 +101,8 @@ class qtype_multianswer extends question_type {
if (!empty($wrapped)) {
// 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;
if ($oldwrappedquestion->qtype != $wrapped->qtype) {
switch ($oldwrappedquestion->qtype) {
@ -152,7 +154,8 @@ class qtype_multianswer extends question_type {
$multianswer = new stdClass();
$multianswer->question = $question->id;
$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;
$DB->update_record('question_multianswer', $multianswer);
} else {
@ -248,7 +251,8 @@ define('NUMERICAL_ABS_ERROR_MARGIN', 6);
// Remaining ANSWER regexes
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',
'\{([0-9]*):(' . ANSWER_TYPE_DEF_REGEX . '):');

View file

@ -1,5 +1,4 @@
<?php
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@ -39,7 +38,7 @@ require_once($CFG->dirroot . '/question/type/shortanswer/renderer.php');
* @copyright 2010 Pierre Pichet
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_multianswer_renderer extends qtype_renderer {
class qtype_multianswer_renderer extends qtype_renderer {
public function formulation_and_controls(question_attempt $qa,
question_display_options $options) {
@ -156,13 +155,15 @@ class qtype_multianswer_textfield_renderer extends qtype_renderer {
if ($options->rightanswer) {
$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 = '';
if ($options->marks >= question_display_options::MARK_AND_MAX && $subq->maxmark > 0) {
$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);
$feedback[] = get_string('markoutofmax', 'question', $a);
}
@ -182,12 +183,15 @@ class qtype_multianswer_textfield_renderer extends qtype_renderer {
}
}
/**
* As multianswer have specific display requirements for multichoice display
* 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_name(question_attempt $qa, $value);
@ -202,10 +206,8 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
abstract protected function get_response(question_attempt $qa);
public function specific_feedback(question_attempt $qa) {
return '';
return '';
}
public function formulation_and_controls(question_attempt $qa,
@ -213,11 +215,11 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
$questiontot = $qa->get_question();
$subquestion = $questiontot->subquestions[$qa->subquestionindex];
$order = $subquestion->get_order($qa); //array_keys($question->answers); //
$order = $subquestion->get_order($qa);
$response = $this->get_response($qa);
$inputattributes = array(
'type' => $this->get_input_type(),
);
);
if ($options->readonly) {
$inputattributes['disabled'] = 'disabled';
@ -226,23 +228,22 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
$feedbackimg = array();
$feedback = array();
$classes = array();
$totfraction = 0 ;
$nullresponse = true ;
$totfraction = 0;
$nullresponse = true;
foreach ($order as $value => $ansid) {
$ans = $subquestion->answers[$ansid];
$inputattributes['name'] = $this->get_input_name($qa, $value);
// echo "<p>name $value name".$inputattributes['name']." </p>";
$inputattributes['value'] = $this->get_input_value($value);
$inputattributes['id'] = $this->get_input_id($qa, $value);
if ($subquestion->single) {
$isselected = $this->is_choice_selected($response, $value);
} else {
$isselected = $this->is_choice_selected($response,$value) ; //$subquestion->field( $value));
} else {
$isselected = $this->is_choice_selected($response, $value);
}
if ($isselected) {
$inputattributes['checked'] = 'checked';
$totfraction += $ans->fraction ;
$nullresponse = false ;
$totfraction += $ans->fraction;
$nullresponse = false;
} else {
unset($inputattributes['checked']);
}
@ -266,17 +267,17 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
$classes[] = $class;
}
$result = '' ;
$result = '';
$answername = 'answer' ;
if ($subquestion->layout == 1 ){
$answername = 'answer';
if ($subquestion->layout == 1) {
$result .= html_writer::start_tag('div', array('class' => 'ablock'));
$result .= html_writer::start_tag('table', array('class' => $answername));
foreach ($radiobuttons as $key => $radio) {
$result .= html_writer::start_tag('tr', array('class' => $answername));
$result .= html_writer::start_tag('td', array('class' => $answername));
$result .= html_writer::tag('span',$radio . $feedbackimg[$key] . $feedback[$key], array('class' => $classes[$key])) . "\n";
$result .= html_writer::tag('span', $radio . $feedbackimg[$key] . $feedback[$key], array('class' => $classes[$key])) . "\n";
$result .= html_writer::end_tag('td');
$result .= html_writer::end_tag('tr');
}
@ -284,13 +285,13 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
$result .= html_writer::end_tag('div'); // ablock
}
if ($subquestion->layout == 2 ){
if ($subquestion->layout == 2) {
$result .= html_writer::start_tag('div', array('class' => 'ablock'));
$result .= html_writer::start_tag('table', array('class' => $answername));
$result .= html_writer::start_tag('tr', array('class' => $answername));
foreach ($radiobuttons as $key => $radio) {
$result .= html_writer::start_tag('td', array('class' => $answername));
$result .= html_writer::tag('span',$radio . $feedbackimg[$key] . $feedback[$key]
$result .= html_writer::tag('span', $radio . $feedbackimg[$key] . $feedback[$key]
, array('class' => $classes[$key])) . "\n";
$result .= html_writer::end_tag('td');
}
@ -298,23 +299,22 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
$result .= html_writer::end_tag('table'); // answer
$result .= html_writer::end_tag('div'); // ablock
}
}
if ($options->feedback ) {
if ($options->feedback) {
$result .= html_writer::start_tag('div', array('class' => 'outcome'));
if ($options->correctness ) {
if ( $nullresponse ){
if ($options->correctness) {
if ( $nullresponse) {
$state = $qa->get_state();
$state = question_state::$invalid;
$result1 = $state->default_string();
$result .= html_writer::nonempty_tag('div',$result1,
$result .= html_writer::nonempty_tag('div', $result1,
array('class' => 'validationerror'));
$result1 = ($subquestion->single) ? get_string('singleanswer', 'quiz') : get_string('multipleanswers', 'quiz');
$result .= html_writer::nonempty_tag('div', $result1,
array('class' => 'validationerror'))
;
}else {
array('class' => 'validationerror'));
} else {
$state = $qa->get_state();
$state = question_state::graded_state_for_fraction($totfraction);
$result1 = $state->default_string();
@ -323,24 +323,22 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
}
}
if ($options->correctresponse) {
$result1 = $this->correct_response($qa);
$result .= html_writer::nonempty_tag('div', $result1, array('class' => 'outcome'));
}
if ($options->marks) {
$subgrade= $totfraction * $subquestion->defaultmark;
$result .= $questiontot->mark_summary($options, $subquestion->defaultmark , $subgrade);
}
if ($qa->get_state() == question_state::$invalid) {
$result .= html_writer::nonempty_tag('div', array('class' => 'validationerror'),
$subquestion->get_validation_error($qa->get_last_qt_data()));
}
$result .= html_writer::end_tag('div');
if ($options->correctresponse ) {
$result1 = $this->correct_response($qa);
$result .= html_writer::nonempty_tag('div',$result1, array('class' => 'outcome'))
;
}
if ($options->marks ) {
$subgrade= $totfraction * $subquestion->defaultmark ;
$result .= $questiontot->mark_summary($options, $subquestion->defaultmark , $subgrade );
}
if ($qa->get_state() == question_state::$invalid) {
$result .= html_writer::nonempty_tag('div', array('class' => 'validationerror'),
$subquestion->get_validation_error($qa->get_last_qt_data()));
}
$result .= html_writer::end_tag('div');
}
return $result;
}
@ -349,27 +347,30 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
class qtype_multianswer_multichoice_single_renderer extends qtype_multianswer_multichoice_renderer_base {
protected function get_input_type() {
protected function get_input_type() {
return 'radio';
}
}
protected function is_choice_selected($response, $value) {
return $response == $value ;
}
protected function is_right(question_answer $ans) {
return $ans->fraction > 0.9999999;
}
protected function get_input_name(question_attempt $qa, $value) {
$questiontot = $qa->get_question();
$subquestion = $questiontot->subquestions[$qa->subquestionindex];
$answername = $subquestion->fieldid.'answer';
return $qa->get_qt_field_name($answername);
}
protected function get_input_value($value) {
return $value;
}
protected function is_choice_selected($response, $value) {
return $response == $value;
}
protected function get_input_id(question_attempt $qa, $value) {
protected function is_right(question_answer $ans) {
return $ans->fraction > 0.9999999;
}
protected function get_input_name(question_attempt $qa, $value) {
$questiontot = $qa->get_question();
$subquestion = $questiontot->subquestions[$qa->subquestionindex];
$answername = $subquestion->fieldid.'answer';
return $qa->get_qt_field_name($answername);
}
protected function get_input_value($value) {
return $value;
}
protected function get_input_id(question_attempt $qa, $value) {
$questiontot = $qa->get_question();
$subquestion = $questiontot->subquestions[$qa->subquestionindex];
$answername = $subquestion->fieldid.'answer';
@ -382,6 +383,7 @@ class qtype_multianswer_multichoice_single_renderer extends qtype_multianswer_mu
return $qa->get_last_qt_var($subquestion->fieldid.'answer', -1);
}
public function correct_response(question_attempt $qa) {
$questiontot = $qa->get_question();
$subquestion = $questiontot->subquestions[$qa->subquestionindex];
@ -393,10 +395,11 @@ 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 {
protected function get_input_type() {
return 'select';
@ -408,12 +411,12 @@ class qtype_multianswer_multichoice_single_inline_renderer extends qtype_multian
$subquestion = $questiontot->subquestions[$qa->subquestionindex];
$answers = $subquestion->answers;
$correctanswers = $subquestion->get_correct_response();
foreach($correctanswers as $key=> $value){
$correct = $value ;
foreach ($correctanswers as $key => $value) {
$correct = $value;
}
$order = $subquestion->get_order($qa);
$response = $this->get_response($qa);
$currentanswer = $response ;
$currentanswer = $response;
$answername = $subquestion->fieldid.'answer';
$inputname = $qa->get_qt_field_name($answername);
$inputattributes = array(
@ -427,25 +430,25 @@ class qtype_multianswer_multichoice_single_inline_renderer extends qtype_multian
}
$choices = array();
$popup = '';
$feedback = '' ;
$answer = '' ;
$feedback = '';
$answer = '';
$classes = 'control';
$feedbackimage = '';
$fraction = 0 ;
$chosen = 0 ;
$fraction = 0;
$chosen = 0;
foreach ($order as $value => $ansid) {
$mcanswer = $subquestion->answers[$ansid];
$choices[$value] = strip_tags($mcanswer->answer);
$selected = '';
$isselected = false ;
if( $response != ''){
$isselected = false;
if ( $response != '') {
$isselected = $this->is_choice_selected($response, $value);
}
if ($isselected) {
$chosen = $value ;
$answer = $mcanswer ;
$fraction = $mcanswer->fraction ;
$chosen = $value;
$answer = $mcanswer;
$fraction = $mcanswer->fraction;
$selected = ' selected="selected"';
}
}
@ -466,59 +469,56 @@ class qtype_multianswer_multichoice_single_inline_renderer extends qtype_multian
// subquestion status correctness or Finished validator if correctness
// Correct response
// marks
$strfeedbackwrapped = 'Response Status';
if ($options->feedback ) {
$feedback = get_string('feedback', 'quiz').":".$feedback."<br />";
$strfeedbackwrapped = 'Response Status';
if ($options->feedback) {
$feedback = get_string('feedback', 'quiz').":".$feedback."<br />";
if ($options->correctness ) {
if ( ! $answer ){
if ($options->correctness) {
if (!$answer) {
$state = $qa->get_state();
$state = question_state::$invalid;
$strfeedbackwrapped .= ":<font color=red >".$state->default_string()."</font>" ;
$strfeedbackwrapped .= ":<font color=red >".$state->default_string()."</font>";
$feedback = "<font color=red >".get_string('singleanswer', 'quiz') ."</font><br />";
}else {
} else {
$state = $qa->get_state();
$state = question_state::graded_state_for_fraction($fraction);
$strfeedbackwrapped .= ":".$state->default_string();
}
}
if ($options->correctresponse ) {
if ($options->correctresponse) {
$feedback .= $this->correct_response($qa)."<br />";
}
if ($options->marks ) {
$subgrade= $fraction * $subquestion->defaultmark ;
$feedback .= $questiontot->mark_summary($options, $subquestion->defaultmark , $subgrade );
if ($options->marks) {
$subgrade= $fraction * $subquestion->defaultmark;
$feedback .= $questiontot->mark_summary($options, $subquestion->defaultmark , $subgrade);
}
$feedback .= '</div>';
}
if ($options->feedback ) {
// need to replace ' and " as they could break the popup string
// as the text comes from database, slashes have been removed
// addslashes will not work as it keeps the "
// HTML &#039; for ' does not work
$feedback = str_replace("'","\'",$feedback);
$feedback = str_replace('"',"\'",$feedback);
$strfeedbackwrapped = str_replace("'","\'",$strfeedbackwrapped);
$strfeedbackwrapped = str_replace('"',"\'",$strfeedbackwrapped);
if ($options->feedback) {
// need to replace ' and " as they could break the popup string
// as the text comes from database, slashes have been removed
// addslashes will not work as it keeps the "
// HTML &#039; for ' does not work
$feedback = str_replace("'", "\'", $feedback);
$feedback = str_replace('"', "\'", $feedback);
$strfeedbackwrapped = str_replace("'", "\'", $strfeedbackwrapped);
$strfeedbackwrapped = str_replace('"', "\'", $strfeedbackwrapped);
$popup = " onmouseover=\"return overlib('$feedback', STICKY, MOUSEOFF, CAPTION, '$strfeedbackwrapped', FGCOLOR, '#FFFFFF');\" ".
$popup = " onmouseover=\"return overlib('$feedback', STICKY, MOUSEOFF, CAPTION, '$strfeedbackwrapped', FGCOLOR, '#FFFFFF');\" ".
" onmouseout=\"return nd();\" ";
}
}
$result = '';
$result .= "<span $popup >";
$result .= html_writer::start_tag('span', array('class' => $classes), '');
$result .=
choose_from_menu($choices, $inputname, $chosen,
' ', '', '', true, $options->readonly) . $feedbackimage ;
$result .= html_writer::end_tag('span');
$result .= html_writer::end_tag('span');
$result .= "<span $popup >";
$result .= html_writer::start_tag('span', array('class' => $classes), '');
$result .= choose_from_menu($choices, $inputname, $chosen,
' ', '', '', true, $options->readonly) . $feedbackimage;
$result .= html_writer::end_tag('span');
$result .= html_writer::end_tag('span');
return $result;
}
@ -556,9 +556,9 @@ class qtype_multianswer_multichoice_multi_renderer extends qtype_multianswer_mul
$responses = $qa->get_last_qt_data();
$questiontot = $qa->get_question();
$subresponses =$questiontot->decode_subquestion_responses($responses);
if( isset($subresponses[$qa->subquestionindex])) {
return $subresponses[$qa->subquestionindex] ;
}else{
if ( isset($subresponses[$qa->subquestionindex])) {
return $subresponses[$qa->subquestionindex];
} else {
return '';
}
}

View file

@ -90,9 +90,12 @@ class qtype_multianswer_test_helper extends question_test_helper {
$mc->layout = 1;
$mc->answers = array(
13 => new question_answer(13, 'Bow-wow', 0, 'You seem to have a dog obsessions!', 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),
13 => new question_answer(13, 'Bow-wow', 0,
'You seem to have a dog obsessions!', 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->maxmark = 1;