Skip to content

Commit

Permalink
MDL-20636 Fix some minor editing problems, and a bunch of coding style.
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Apr 28, 2011
1 parent 1fa3936 commit fdd015b
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 69 deletions.
24 changes: 21 additions & 3 deletions question/type/edit_question_form.php
Expand Up @@ -484,14 +484,21 @@ public function set_data($question) {
}

/**
* Any preprocessing needed for the settings form for the question type
*
* @param array $question - array to fill in with the default values
* Perform an preprocessing needed on the data passed to {@link set_data()}
* before it is used to initialise the form.
* @param object $question the data being passed to the form.
* @return object $question the modified data.
*/
protected function data_preprocessing($question) {
return $question;
}

/**
* Perform the necessary preprocessing for the fields added by
* {@link add_per_answer_fields()}.
* @param object $question the data being passed to the form.
* @return object $question the modified data.
*/
protected function data_preprocessing_answers($question) {
if (empty($question->options->answers)) {
return $question;
Expand Down Expand Up @@ -532,6 +539,12 @@ protected function data_preprocessing_answers($question) {
return $question;
}

/**
* Perform the necessary preprocessing for the fields added by
* {@link add_combined_feedback_fields()}.
* @param object $question the data being passed to the form.
* @return object $question the modified data.
*/
protected function data_preprocessing_combined_feedback($question,
$withshownumcorrect = false) {
if (empty($question->options)) {
Expand Down Expand Up @@ -565,6 +578,11 @@ protected function data_preprocessing_combined_feedback($question,
return $question;
}

/**
* Perform the necessary preprocessing for the hint fields.
* @param object $question the data being passed to the form.
* @return object $question the modified data.
*/
protected function data_preprocessing_hints($question, $withclearwrong = false,
$withshownumpartscorrect = false) {
if (empty($question->hints)) {
Expand Down
Expand Up @@ -68,7 +68,8 @@ protected function define_question_plugin_structure() {
$numericalrecords->add_child($numericalrecord);

// set source to populate the data
$numericalrecord->set_source_table('question_numerical', array('question' => backup::VAR_PARENTID));
$numericalrecord->set_source_table('question_numerical',
array('question' => backup::VAR_PARENTID));

// don't need to annotate ids nor files

Expand Down
Expand Up @@ -51,7 +51,7 @@ protected function define_question_plugin_structure() {

// Add own qtype stuff
$elename = 'numerical';
$elepath = $this->get_pathfor('/numerical_records/numerical_record'); // we used get_recommended_name() so this works
$elepath = $this->get_pathfor('/numerical_records/numerical_record');
$paths[] = new restore_path_element($elename, $elepath);


Expand All @@ -72,7 +72,8 @@ public function process_numerical($data) {
$newquestionid = $this->get_new_parentid('question');
$questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;

// If the question has been created by restore, we need to create its question_numerical too
// If the question has been created by restore, we need to create its
// question_numerical too.
if ($questioncreated) {
// Adjust some columns
$data->question = $newquestionid;
Expand Down
2 changes: 0 additions & 2 deletions question/type/numerical/db/upgrade.php
Expand Up @@ -98,5 +98,3 @@ function xmldb_qtype_numerical_upgrade($oldversion) {

return true;
}


86 changes: 54 additions & 32 deletions question/type/numerical/edit_numerical_form.php
Expand Up @@ -41,15 +41,18 @@ protected function definition_inner($mform) {
$this->add_per_answer_fields($mform, get_string('answerno', 'qtype_numerical', '{no}'),
$creategrades->gradeoptions);

$this->add_units_options($mform);
$this->add_units_elements($mform);
$this->add_unit_options($mform);
$this->add_unit_fields($mform);
$this->add_interactive_settings();
}

protected function get_per_answer_fields($mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
$repeated = parent::get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
protected function get_per_answer_fields($mform, $label, $gradeoptions,
&$repeatedoptions, &$answersoption) {
$repeated = parent::get_per_answer_fields($mform, $label, $gradeoptions,
$repeatedoptions, $answersoption);

$tolerance = $mform->createElement('text', 'tolerance', get_string('acceptederror', 'qtype_numerical'));
$tolerance = $mform->createElement('text', 'tolerance',
get_string('acceptederror', 'qtype_numerical'));
$repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
array_splice($repeated, 3, 0, array($tolerance));
$repeated[1]->setSize(10);
Expand All @@ -61,48 +64,56 @@ protected function get_per_answer_fields($mform, $label, $gradeoptions, &$repeat
* Add the unit handling options to the form.
* @param object $mform the form being built.
*/
protected function add_units_options($mform) {
protected function add_unit_options($mform) {

$mform->addElement('header', 'unithandling', get_string('unithandling', 'qtype_numerical'));
$mform->addElement('header', 'unithandling',
get_string('unithandling', 'qtype_numerical'));

$unitoptions = array(
qtype_numerical::UNITNONE => get_string('onlynumerical', 'qtype_numerical'),
qtype_numerical::UNITDISPLAY => get_string('oneunitshown', 'qtype_numerical'),
qtype_numerical::UNITOPTIONAL => get_string('manynumerical', 'qtype_numerical'),
qtype_numerical::UNITGRADED => get_string('unitgraded', 'qtype_numerical'),
);
$mform->addElement('select', 'unitrole', get_string('unithandling', 'qtype_numerical'), $unitoptions);
$mform->addElement('select', 'unitrole',
get_string('unithandling', 'qtype_numerical'), $unitoptions);

$penaltygrp = array();
$penaltygrp[] = $mform->createElement('text', 'unitpenalty', get_string('unitpenalty', 'qtype_numerical') ,
array('size' => 6));
$penaltygrp[] = $mform->createElement('text', 'unitpenalty',
get_string('unitpenalty', 'qtype_numerical'), array('size' => 6));
$mform->setType('unitpenalty', PARAM_NUMBER);
$mform->setDefault('unitpenalty', 0.1000000);

$unitgradingtypes = array(
qtype_numerical::UNITGRADEDOUTOFMARK => get_string('decfractionofresponsegrade', 'qtype_numerical'),
qtype_numerical::UNITGRADEDOUTOFMAX => get_string('decfractionofquestiongrade', 'qtype_numerical'),
qtype_numerical::UNITGRADEDOUTOFMARK =>
get_string('decfractionofresponsegrade', 'qtype_numerical'),
qtype_numerical::UNITGRADEDOUTOFMAX =>
get_string('decfractionofquestiongrade', 'qtype_numerical'),
);
$penaltygrp[] = $mform->createElement('select', 'unitgradingtypes', '' , $unitgradingtypes );
$penaltygrp[] = $mform->createElement('select', 'unitgradingtypes', '', $unitgradingtypes);
$mform->setDefault('unitgradingtypes', 1);

$mform->addGroup($penaltygrp, 'penaltygrp', get_string('unitpenalty', 'qtype_numerical'),' ' , false);
$mform->addGroup($penaltygrp, 'penaltygrp',
get_string('unitpenalty', 'qtype_numerical'), ' ', false);
$mform->addHelpButton('penaltygrp', 'unitpenalty', 'qtype_numerical');

$unitinputoptions = array(
qtype_numerical::UNITINPUT => get_string('editableunittext', 'qtype_numerical'),
qtype_numerical::UNITSELECT => get_string('unitchoice', 'qtype_numerical'),
);
$mform->addElement('select', 'multichoicedisplay', get_string('studentunitanswer', 'qtype_numerical'), $unitinputoptions);
$mform->addElement('select', 'multichoicedisplay',
get_string('studentunitanswer', 'qtype_numerical'), $unitinputoptions);

$unitslefts = array(
0 => get_string('rightexample', 'qtype_numerical'),
1 => get_string('leftexample', 'qtype_numerical')
);
$mform->addElement('select', 'unitsleft', get_string('unitposition', 'qtype_numerical') , $unitslefts );
$mform->addElement('select', 'unitsleft',
get_string('unitposition', 'qtype_numerical'), $unitslefts);
$mform->setDefault('unitsleft', 0);

$mform->addElement('editor', 'instructions', get_string('instructions', 'qtype_numerical'), null, $this->editoroptions);
$mform->addElement('editor', 'instructions',
get_string('instructions', 'qtype_numerical'), null, $this->editoroptions);
$mform->setType('instructions', PARAM_RAW);
$mform->addHelpButton('instructions', 'numericalinstructions', 'qtype_numerical');

Expand All @@ -121,17 +132,20 @@ protected function add_units_options($mform) {
* Add the input areas for each unit.
* @param object $mform the form being built.
*/
protected function add_units_elements($mform) {
protected function add_unit_fields($mform) {
$repeated = array(
$mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}')),
$mform->createElement('header', 'unithdr',
get_string('unithdr', 'qtype_numerical', '{no}')),
$mform->createElement('text', 'unit', get_string('unit', 'quiz')),
$mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz')),
);

$repeatedoptions['unit']['type'] = PARAM_NOTAGS;
$repeatedoptions['multiplier']['type'] = PARAM_NUMBER;
$repeatedoptions['unit']['disabledif'] = array('unitrole', 'eq', qtype_numerical::UNITNONE);
$repeatedoptions['multiplier']['disabledif'] = array('unitrole', 'eq', qtype_numerical::UNITNONE);
$repeatedoptions['unit']['disabledif'] =
array('unitrole', 'eq', qtype_numerical::UNITNONE);
$repeatedoptions['multiplier']['disabledif'] =
array('unitrole', 'eq', qtype_numerical::UNITNONE);

if (isset($this->question->options->units)) {
$countunits = count($this->question->options->units);
Expand All @@ -143,8 +157,8 @@ protected function add_units_elements($mform) {
} else {
$repeatsatstart = $countunits;
}
$this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}'));
$this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'nounits',
'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}'));

if ($mform->elementExists('multiplier[0]')) {
$firstunit = $mform->getElement('multiplier[0]');
Expand All @@ -160,7 +174,7 @@ protected function data_preprocessing($question) {
$question = $this->data_preprocessing_answers($question);
$question = $this->data_preprocessing_hints($question);
$question = $this->data_preprocessing_units($question);
$question = $this->data_preprocessing_numerical_options($question);
$question = $this->data_preprocessing_unit_options($question);
return $question;
}

Expand All @@ -180,9 +194,12 @@ protected function data_preprocessing_answers($question) {
}

/**
* TODO
* Perform the necessary preprocessing for the fields added by
* {@link add_unit_fields()}.
* @param object $question the data being passed to the form.
* @return object $question the modified data.
*/
function data_preprocessing_units($question) {
protected function data_preprocessing_units($question) {
if (empty($question->options->units)) {
return $question;
}
Expand All @@ -196,9 +213,12 @@ function data_preprocessing_units($question) {
}

/**
* TODO
* Perform the necessary preprocessing for the fields added by
* {@link add_unit_options()}.
* @param object $question the data being passed to the form.
* @return object $question the modified data.
*/
function data_preprocessing_numerical_options($question) {
protected function data_preprocessing_unit_options($question) {
if (empty($question->options)) {
return $question;
}
Expand Down Expand Up @@ -243,13 +263,16 @@ public function validation($data, $files) {
if ($trimmedanswer != '') {
$answercount++;
if (!(is_numeric($trimmedanswer) || $trimmedanswer == '*')) {
$errors["answer[$key]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
$errors['answer[' . $key . ']'] =
get_string('answermustbenumberorstar', 'qtype_numerical');
}
if ($data['fraction'][$key] == 1) {
$maxgrade = true;
}
} else if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key]['text'])) {
$errors["answer[$key]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
} else if ($data['fraction'][$key] != 0 ||
!html_is_blank($data['feedback'][$key]['text'])) {
$errors['answer[' . $key . ']'] =
get_string('answermustbenumberorstar', 'qtype_numerical');
$answercount++;
}
}
Expand All @@ -269,7 +292,6 @@ public function validation($data, $files) {
* Validate the unit options.
*/
function validate_numerical_options($data, $errors) {
print_object($data); // DONOTCOMMIT
if ($data['unitrole'] != qtype_numerical::UNITNONE && trim($data['unit'][0]) == '') {
$errors['unit[0]'] = get_string('unitonerequired', 'qtype_numerical');
}
Expand Down
4 changes: 2 additions & 2 deletions question/type/numerical/lang/en/qtype_numerical.php
Expand Up @@ -105,12 +105,12 @@
$string['unithandling'] = 'Unit handling';
$string['validnumberformats'] = 'Valid number formats';
$string['validnumberformats_help'] = '
* regular numbers 13500.67 : 13 500.67 : 13500,67: 13 500,67
* regular numbers 13500.67, 13 500.67, 13500,67 or 13 500,67
* if you use , as thousand separator *always* put the decimal . as in
13,500.67 : 13,500.
* for exponent form, say 1.350067 * 10<sup>4</sup>, use
1.350067 E4 : 1.350067 E04 ';

$string['validnumbers'] = ' 13500.67 : 13 500.67 : 13,500.67 : 13500,67: 13 500,67 : 1.350067 E4 : 1.350067 E04 ';
$string['validnumbers'] = '13500.67, 13 500.67, 13,500.67, 13500,67, 13 500,67, 1.350067 E4 or 1.350067 E04';

0 comments on commit fdd015b

Please sign in to comment.