Merge branch 'MDL-27430' of git://github.com/timhunt/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2011-06-29 23:33:01 +02:00
commit 0f744d2ca4
5 changed files with 53 additions and 24 deletions

View File

@ -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);

View File

@ -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

View File

@ -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);
}

View File

@ -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;

View File

@ -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);
}
}