From 8566369f630510f6453ff7edbe0fc2a40691b0e9 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 28 Jun 2011 16:33:01 +0100 Subject: [PATCH] MDL-27430 qtype numerical put multiple-choice units back to radio buttons It seems that people want to be able to use and here, which makes sense, so we need to keep radio buttons for questions upgraded from 2.0. The dropdown menu, however, is also nice, so keep that option too for new questions. --- .../type/numerical/edit_numerical_form.php | 3 +- .../numerical/lang/en/qtype_numerical.php | 7 ++-- question/type/numerical/question.php | 25 +++++++----- question/type/numerical/questiontype.php | 3 +- question/type/numerical/renderer.php | 39 ++++++++++++++----- 5 files changed, 53 insertions(+), 24 deletions(-) diff --git a/question/type/numerical/edit_numerical_form.php b/question/type/numerical/edit_numerical_form.php index 72cf9941e73..f0e1e334fe4 100644 --- a/question/type/numerical/edit_numerical_form.php +++ b/question/type/numerical/edit_numerical_form.php @@ -98,7 +98,8 @@ class qtype_numerical_edit_form extends question_edit_form { $unitinputoptions = array( qtype_numerical::UNITINPUT => get_string('editableunittext', 'qtype_numerical'), - qtype_numerical::UNITSELECT => get_string('unitchoice', 'qtype_numerical'), + qtype_numerical::UNITRADIO => get_string('unitchoice', 'qtype_numerical'), + qtype_numerical::UNITSELECT => get_string('unitselect', 'qtype_numerical'), ); $mform->addElement('select', 'multichoicedisplay', get_string('studentunitanswer', 'qtype_numerical'), $unitinputoptions); diff --git a/question/type/numerical/lang/en/qtype_numerical.php b/question/type/numerical/lang/en/qtype_numerical.php index 94426bc3e73..9008651a5b3 100644 --- a/question/type/numerical/lang/en/qtype_numerical.php +++ b/question/type/numerical/lang/en/qtype_numerical.php @@ -71,9 +71,11 @@ $string['selectunit'] = 'Select one unit'; $string['studentunitanswer'] = 'Units are input using'; $string['tolerancetype'] = 'Tolerance type'; $string['unit'] = 'Unit'; +$string['unitappliedpenalty'] = 'These marks include a penalty of {$a} for bad unit.'; $string['unitchoice'] = 'a multiple choice selection'; $string['unitedit'] = 'Edit unit'; $string['unitgraded'] = 'The unit must be given, and will be graded.'; +$string['unithandling'] = 'Unit handling'; $string['unithdr'] = 'Unit {$a}'; $string['unitincorrect'] = 'You did not give the correct unit.'; $string['unitmandatory'] = 'Mandatory'; @@ -84,6 +86,7 @@ $string['unitmandatory_help'] = ' * The unit penalty will be applied if the unit field is empty '; +$string['unitnotselected'] = 'You must select a unit.'; $string['unitonerequired'] = 'You must enter at least one unit'; $string['unitoptional'] = 'Optional unit'; $string['unitoptional_help'] = ' @@ -91,15 +94,13 @@ $string['unitoptional_help'] = ' * If the unit is badly written or unknown, the response will be considered as non valid. '; -$string['unitnotselected'] = 'You must select a unit.'; $string['unitpenalty'] = 'Unit penalty'; $string['unitpenalty_help'] = 'The penalty is applied if * the wrong unit name is entered into the unit input, or * a unit is entered into the value input box'; -$string['unitappliedpenalty'] = 'These marks include a penalty of {$a} for bad unit.'; $string['unitposition'] = 'Units go'; -$string['unithandling'] = 'Unit handling'; +$string['unitselect'] = 'a drop-down menu'; $string['validnumberformats'] = 'Valid number formats'; $string['validnumberformats_help'] = ' * regular numbers 13500.67, 13 500.67, 13500,67 or 13 500,67 diff --git a/question/type/numerical/question.php b/question/type/numerical/question.php index 764552834f1..90d2d7df0c4 100644 --- a/question/type/numerical/question.php +++ b/question/type/numerical/question.php @@ -39,7 +39,7 @@ class qtype_numerical_question extends question_graded_automatically { /** @var array of question_answer. */ public $answers = array(); - /** @var int one of the constants UNITNONE, UNITSELECT or UNITINPUT. */ + /** @var int one of the constants UNITNONE, UNITRADIO, UNITSELECT or UNITINPUT. */ public $unitdisplay; /** @var int one of the constants UNITGRADEDOUTOFMARK or UNITGRADEDOUTOFMAX. */ public $unitgradingtype; @@ -51,12 +51,17 @@ class qtype_numerical_question extends question_graded_automatically { public function get_expected_data() { $expected = array('answer' => PARAM_RAW_TRIMMED); - if ($this->unitdisplay == qtype_numerical::UNITSELECT) { + if ($this->has_separate_unit_field()) { $expected['unit'] = PARAM_RAW_TRIMMED; } return $expected; } + public function has_separate_unit_field() { + return $this->unitdisplay == qtype_numerical::UNITRADIO || + $this->unitdisplay == qtype_numerical::UNITSELECT; + } + public function start_attempt(question_attempt_step $step, $variant) { $step->set_qt_var('_separators', $this->ap->get_point() . '$' . $this->ap->get_separator()); @@ -74,7 +79,7 @@ class qtype_numerical_question extends question_graded_automatically { $resp = null; } - if ($this->unitdisplay == qtype_numerical::UNITSELECT && !empty($response['unit'])) { + if ($this->has_separate_unit_field() && !empty($response['unit'])) { $resp = $this->ap->add_unit($resp, $response['unit']); } @@ -100,7 +105,7 @@ class qtype_numerical_question extends question_graded_automatically { return false; } - if ($this->unitdisplay == qtype_numerical::UNITSELECT && empty($response['unit'])) { + if ($this->has_separate_unit_field() && empty($response['unit'])) { return false; } @@ -121,7 +126,7 @@ class qtype_numerical_question extends question_graded_automatically { return get_string('invalidnumbernounit', 'qtype_numerical'); } - if ($this->unitdisplay == qtype_numerical::UNITSELECT && empty($response['unit'])) { + if ($this->has_separate_unit_field() && empty($response['unit'])) { return get_string('unitnotselected', 'qtype_numerical'); } @@ -134,7 +139,7 @@ class qtype_numerical_question extends question_graded_automatically { return false; } - if ($this->unitdisplay == qtype_numerical::UNITSELECT) { + if ($this->has_separate_unit_field()) { return question_utils::arrays_same_at_key_missing_is_blank( $prevresponse, $newresponse, 'unit'); } @@ -150,7 +155,7 @@ class qtype_numerical_question extends question_graded_automatically { $response = array('answer' => $answer->answer); - if ($this->unitdisplay == qtype_numerical::UNITSELECT) { + if ($this->has_separate_unit_field()) { $response['unit'] = $this->ap->get_default_unit(); } else if ($this->unitdisplay == qtype_numerical::UNITINPUT) { $response['answer'] = $this->ap->add_unit($answer->answer); @@ -199,7 +204,7 @@ class qtype_numerical_question extends question_graded_automatically { } public function grade_response(array $response) { - if ($this->unitdisplay == qtype_numerical::UNITSELECT) { + if ($this->has_separate_unit_field()) { $selectedunit = $response['unit']; } else { $selectedunit = null; @@ -219,7 +224,7 @@ class qtype_numerical_question extends question_graded_automatically { return array($this->id => question_classified_response::no_response()); } - if ($this->unitdisplay == qtype_numerical::UNITSELECT) { + if ($this->has_separate_unit_field()) { $selectedunit = $response['unit']; } else { $selectedunit = null; @@ -231,7 +236,7 @@ class qtype_numerical_question extends question_graded_automatically { } $resp = $response['answer']; - if ($this->unitdisplay == qtype_numerical::UNITSELECT) { + if ($this->has_separate_unit_field()) { $resp = $this->ap->add_unit($resp, $unit); } diff --git a/question/type/numerical/questiontype.php b/question/type/numerical/questiontype.php index b8c8b3863a8..fb643e4026d 100644 --- a/question/type/numerical/questiontype.php +++ b/question/type/numerical/questiontype.php @@ -41,7 +41,8 @@ require_once($CFG->dirroot . '/question/type/numerical/question.php'); */ class qtype_numerical extends question_type { const UNITINPUT = 0; - const UNITSELECT = 1; + const UNITRADIO = 1; + const UNITSELECT = 2; const UNITNONE = 3; const UNITGRADED = 1; diff --git a/question/type/numerical/renderer.php b/question/type/numerical/renderer.php index b70747995f6..d60f09661fc 100644 --- a/question/type/numerical/renderer.php +++ b/question/type/numerical/renderer.php @@ -36,7 +36,7 @@ class qtype_numerical_renderer extends qtype_renderer { $question = $qa->get_question(); $currentanswer = $qa->get_last_qt_var('answer'); - if ($question->unitdisplay == qtype_numerical::UNITSELECT) { + if ($question->has_separate_unit_field()) { $selectedunit = $qa->get_last_qt_var('unit'); } else { $selectedunit = null; @@ -77,14 +77,35 @@ class qtype_numerical_renderer extends qtype_renderer { $input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg; - if ($question->unitdisplay == qtype_numerical::UNITSELECT) { - $unitselect = html_writer::select($question->ap->get_unit_options(), - $qa->get_qt_field_name('unit'), $selectedunit, array(''=>'choosedots'), - array('disabled' => $options->readonly)); + if ($question->has_separate_unit_field()) { + if ($question->unitdisplay == qtype_numerical::UNITRADIO) { + $choices = array(); + $i = 1; + foreach ($question->ap->get_unit_options() as $unit) { + $id = $qa->get_qt_field_name('unit') . '_' . $i++; + $radioattrs = array('type' => 'radio', 'id' => $id, 'value' => $unit, + 'name' => $qa->get_qt_field_name('unit')); + if ($unit == $selectedunit) { + $radioattrs['checked'] = 'checked'; + } + $choices[] = html_writer::tag('label', + html_writer::empty_tag('input', $radioattrs) . $unit, + array('for' => $id, 'class' => 'unitchoice')); + } + + $unitchoice = html_writer::tag('span', implode(' ', $choices), + array('class' => 'unitchoices')); + + } else if ($question->unitdisplay == qtype_numerical::UNITSELECT) { + $unitchoice = html_writer::select($question->ap->get_unit_options(), + $qa->get_qt_field_name('unit'), $selectedunit, array(''=>'choosedots'), + array('disabled' => $options->readonly)); + } + if ($question->ap->are_units_before()) { - $input = $unitselect . ' ' . $input; + $input = $unitchoice . ' ' . $input; } else { - $input = $input . ' ' . $unitselect; + $input = $input . ' ' . $unitchoice; } } @@ -114,7 +135,7 @@ class qtype_numerical_renderer extends qtype_renderer { public function specific_feedback(question_attempt $qa) { $question = $qa->get_question(); - if ($question->unitdisplay == qtype_numerical::UNITSELECT) { + if ($question->has_separate_unit_field()) { $selectedunit = $qa->get_last_qt_var('unit'); } else { $selectedunit = null; @@ -149,6 +170,6 @@ class qtype_numerical_renderer extends qtype_renderer { $response = $question->ap->add_unit($response); } - return get_string('correctansweris', 'qtype_shortanswer', s($response)); + return get_string('correctansweris', 'qtype_shortanswer', $response); } }