diff --git a/question/type/calculated/datasetitems_form.php b/question/type/calculated/datasetitems_form.php index 75abf4ae5d4..a44b5a12caf 100644 --- a/question/type/calculated/datasetitems_form.php +++ b/question/type/calculated/datasetitems_form.php @@ -170,8 +170,8 @@ class question_dataset_dependent_items_form extends moodleform { if ($this->qtypeobj->supports_dataset_item_generation()){ $radiogrp = array(); $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('reuseifpossible', 'qtype_calculated'), 0); - $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('forceregeneration of only non shared wild cards', 'qtype_calculated'), 1); - $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('forceregeneration of all wild cards', 'qtype_calculated'), 2); + $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('forceregenerationshared', 'qtype_calculated'), 1); + $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('forceregenerationall', 'qtype_calculated'), 2); $mform->addGroup($radiogrp, 'forceregenerationgrp', get_string('nextitemtoadd', 'qtype_calculated'), "
", false); } @@ -181,14 +181,14 @@ class question_dataset_dependent_items_form extends moodleform { $addgrp[] =& $mform->createElement('submit', 'addbutton', get_string('add', 'moodle')); $addgrp[] =& $mform->createElement('select', "selectadd", get_string('additem', 'qtype_calculated'), $addremoveoptions); $addgrp[] = & $mform->createElement('static',"stat","Items",get_string('newsetwildcardvalues', 'qtype_calculatedsimple')); - $mform->addGroup($addgrp, 'addgrp', '', ' ', false); + $mform->addGroup($addgrp, 'addgrp', get_string('additem', 'qtype_calculated'), ' ', false); $mform->addElement('static', "divideradd", '', ''); // $mform->closeHeaderBefore('divideradd'); if ($this->noofitems > 0) { $mform->addElement('header', 'additemhdr', get_string('delete', 'moodle')); $deletegrp = array(); $deletegrp[] =& $mform->createElement('submit', 'deletebutton', get_string('delete', 'moodle')); - $deletegrp[] =& $mform->createElement('select', "selectdelete", get_string('deleteitem', 'qtype_calculated')."1", $addremoveoptions); + $deletegrp[] =& $mform->createElement('select', 'selectdelete', get_string('deleteitem', 'qtype_calculated')."1", $addremoveoptions); $deletegrp[] = & $mform->createElement('static',"stat","Items",get_string('setwildcardvalues', 'qtype_calculatedsimple')); $mform->addGroup($deletegrp, 'deletegrp', '', ' ', false); // $mform->addElement('static', "dividerdelete", '', '
'); diff --git a/question/type/calculated/edit_calculated_form.php b/question/type/calculated/edit_calculated_form.php index 505489dd717..a83eab84330 100644 --- a/question/type/calculated/edit_calculated_form.php +++ b/question/type/calculated/edit_calculated_form.php @@ -82,7 +82,7 @@ class question_edit_calculated_form extends question_edit_form { $this->qtypeobj =& $QTYPES[$this->qtype()]; // echo code left for testing period // echo "

question ".optional_param('multichoice', '', PARAM_RAW)." optional

";print_r($this->question);echo "

"; - $label = get_string("sharedwildcards", "qtype_datasetdependent"); + $label = get_string('sharedwildcards', 'qtype_calculated'); $mform->addElement('hidden', 'initialcategory', 1); $mform->setType('initialcategory', PARAM_INT); $html2 = $this->qtypeobj->print_dataset_definitions_category($this->question); @@ -110,33 +110,8 @@ class question_edit_calculated_form extends question_edit_form { $repeated = array(); - $QTYPES['numerical']->edit_numerical_options($mform,$this); - $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}')); - $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz')); - $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz')); - - $mform->setType('unit', PARAM_NOTAGS); - - $mform->setType('multiplier', PARAM_NUMBER); - - if (isset($this->question->options)){ - $countunits = count($this->question->options->units); - } else { - $countunits = 0; - } - if ($this->question->formoptions->repeatelements){ - $repeatsatstart = $countunits + 1; - } else { - $repeatsatstart = $countunits; - } - $this->repeat_elements($repeated, $repeatsatstart, array(), 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}')); - - if ($mform->elementExists('multiplier[0]')){ - $firstunit =& $mform->getElement('multiplier[0]'); - $firstunit->freeze(); - $firstunit->setValue('1.0'); - $firstunit->setPersistantFreeze(true); - } + $QTYPES['numerical']->add_units_options($mform,$this); + $QTYPES['numerical']->add_units_elements($mform,$this); foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) { $mform->addElement('hidden', $feedbackname); $mform->setType($feedbackname, PARAM_RAW); @@ -144,11 +119,6 @@ class question_edit_calculated_form extends question_edit_form { //hidden elements $mform->addElement('hidden', 'synchronize', ''); $mform->setType('synchronize', PARAM_INT); - /* if (isset($this->question->options)&& isset($this->question->options->synchronize) ){ - $mform->setDefault("synchronize", $this->question->options->synchronize); - } else { - $mform->setDefault("synchronize", 0 ); - }*/ $mform->addElement('hidden', 'wizard', 'datasetdefinitions'); $mform->setType('wizard', PARAM_ALPHA); @@ -156,6 +126,7 @@ class question_edit_calculated_form extends question_edit_form { } function set_data($question) { + $default_values = array(); if (isset($question->options)){ $answers = $question->options->answers; if (count($answers)) { @@ -171,37 +142,42 @@ class question_edit_calculated_form extends question_edit_form { $key++; } } + $default_values['synchronize'] = $question->options->synchronize ; $default_values['unitgradingtype'] = $question->options->unitgradingtype ; $default_values['unitpenalty'] = $question->options->unitpenalty ; - $default_values['showunits'] = $question->options->showunits ; - $default_values['unitsleft'] = $question->options->unitsleft ; - $default_values['instructions'] = $question->options->instructions ; - $default_values['synchronize'] = $question->options->synchronize ; + switch ($question->options->showunits){ + case 'O' : + case '1' : + $default_values['showunits0'] = $question->options->showunits ; + $default_values['unitrole'] = 0 ; + break; + case '2' : + case '3' : + $default_values['showunits1'] = $question->options->showunits ; + $default_values['unitrole'] = 1 ; + break; + } + $default_values['unitsleft'] = $question->options->unitsleft ; + $default_values['instructions'] = $question->options->instructions ; if (isset($question->options->units)){ $units = array_values($question->options->units); - // make sure the default unit is at index 0 - usort($units, create_function('$a, $b', - 'if (1.0 === (float)$a->multiplier) { return -1; } else '. - 'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }')); - if (count($units)) { - $key = 0; - foreach ($units as $unit){ + if (!empty($units)) { + foreach ($units as $key => $unit){ $default_values['unit['.$key.']'] = $unit->unit; $default_values['multiplier['.$key.']'] = $unit->multiplier; - $key++; } } } } if (isset($question->options->single)){ - $default_values['single'] = $question->options->single; - $default_values['answernumbering'] = $question->options->answernumbering; - $default_values['shuffleanswers'] = $question->options->shuffleanswers; - $default_values['correctfeedback'] = $question->options->correctfeedback; - $default_values['partiallycorrectfeedback'] = $question->options->partiallycorrectfeedback; - $default_values['incorrectfeedback'] = $question->options->incorrectfeedback; - } + $default_values['single'] = $question->options->single; + $default_values['answernumbering'] = $question->options->answernumbering; + $default_values['shuffleanswers'] = $question->options->shuffleanswers; + $default_values['correctfeedback'] = $question->options->correctfeedback; + $default_values['partiallycorrectfeedback'] = $question->options->partiallycorrectfeedback; + $default_values['incorrectfeedback'] = $question->options->incorrectfeedback; + } $default_values['submitbutton'] = get_string('nextpage', 'qtype_calculated'); $default_values['makecopy'] = get_string('makecopynextpage', 'qtype_calculated'); /* set the wild cards category display given that on loading the category element is @@ -235,6 +211,7 @@ class question_edit_calculated_form extends question_edit_form { } function validation($data, $files) { + global $QTYPES; // echo code left for testing period // echo "

question

";print_r($this->question);echo "

"; @@ -326,7 +303,7 @@ class question_edit_calculated_form extends question_edit_form { $totalfraction = $totalfraction * 100; $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction); } - }*/ + } $units = $data['unit']; if (count($units)) { foreach ($units as $key => $unit){ @@ -344,8 +321,29 @@ class question_edit_calculated_form extends question_edit_form { } } + } + }*/ + $QTYPES['numerical']->validate_numerical_options($data, $errors) ; + /* if ($data['unitrole'] == 0 ){ + $showunits = $data['showunits0']; + }else { + $showunits = $data['showunits1']; + } + + if (($showunits == 0) || ($showunits == 1) || ($showunits == 2)){ + if (trim($units[0]) == ''){ + $errors['unit[0]'] = 'You must set a valid unit name' ; } } + if ($showunits == 3 ){ + if (count($units)) { + foreach ($units as $key => $unit){ + if ($units[$key] != ''){ + $errors["unit[$key]"] = 'You must erase this unit name' ; + } + } + } + }*/ if ($answercount==0){ $errors['answer[0]'] = get_string('atleastoneanswer', 'qtype_calculated'); } diff --git a/question/type/calculated/questiontype.php b/question/type/calculated/questiontype.php index af10e3fcc7f..9f11d513b7d 100644 --- a/question/type/calculated/questiontype.php +++ b/question/type/calculated/questiontype.php @@ -42,13 +42,6 @@ class question_calculated_qtype extends default_questiontype { $question->options->incorrectfeedback = ''; } - // echo "

questionoptions

";print_r($question);echo "

"; - /* $question->options->unitgradingtype = 0; - $question->options->unitpenalty = 0; - $question->options->showunits = 0 ; - $question->options->unitsleft = 0 ; - $question->options->instructions = '' ;*/ - // echo "

questionoptions

";print_r($question);echo "

"; if (!$question->options->answers = $DB->get_records_sql( "SELECT a.*, c.tolerance, c.tolerancetype, c.correctanswerlength, c.correctanswerformat " . @@ -62,8 +55,8 @@ class question_calculated_qtype extends default_questiontype { } if ( $this->get_virtual_qtype() == $QTYPES['numerical']){ - $QTYPES['numerical']->get_numerical_options($question); $QTYPES['numerical']->get_numerical_units($question); + $QTYPES['numerical']->get_numerical_options($question); } if( isset($question->export_process)&&$question->export_process){ diff --git a/question/type/calculatedsimple/edit_calculatedsimple_form.php b/question/type/calculatedsimple/edit_calculatedsimple_form.php index 9df85bba258..e999708d690 100644 --- a/question/type/calculatedsimple/edit_calculatedsimple_form.php +++ b/question/type/calculatedsimple/edit_calculatedsimple_form.php @@ -301,40 +301,9 @@ class question_edit_calculatedsimple_form extends question_edit_form { $this->add_per_answer_fields($mform, get_string('answerhdr', 'qtype_calculated', '{no}'), $creategrades->gradeoptions, 1, 1); - $QTYPES['numerical']->edit_numerical_options($mform,$this); + $QTYPES['numerical']->add_units_options($mform,$this); - $repeated = array(); - $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}')); - - $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz')); - $mform->setType('unit', PARAM_NOTAGS); - - $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz')); - $mform->setType('multiplier', PARAM_NUMBER); - - if (isset($this->question->options)){ - $countunits = count($this->question->options->units); - } else { - $countunits = 0; - } - if ($this->question->formoptions->repeatelements){ - $repeatsatstart = $countunits + 1; - } else { - $repeatsatstart = $countunits; - } - $this->repeat_elements($repeated, $repeatsatstart, array(), 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}')); - - if ($mform->elementExists('multiplier[0]')){ - $firstunit =& $mform->getElement('multiplier[0]'); - $firstunit->freeze(); - $firstunit->setValue('1.0'); - $firstunit->setPersistantFreeze(true); - } - - //hidden elements - // $mform->addElement('hidden', 'wizard', 'datasetdefinitions'); - // $mform->setType('wizard', PARAM_ALPHA); - // $mform->addElement('header', '', ''); + $QTYPES['numerical']->add_units_elements($mform,$this); $label = "
".get_string('wildcardrole', 'qtype_calculatedsimple')."
"; $mform->addElement('html', "
 
"); $mform->addElement('html', $label);// explaining the role of datasets so other strings can be shortened @@ -572,24 +541,15 @@ class question_edit_calculatedsimple_form extends question_edit_form { } } - // $mform->addElement('hidden', 'wizard', 'edit_calculatedsimple'); - // $mform->setType('wizard', PARAM_ALPHA); -/* - $mform->addElement('hidden', 'returnurl'); - $mform->setType('returnurl', PARAM_LOCALURL); - $mform->setDefault('returnurl', 0); - -*/ } function set_data($question) { $answer = $this->answer; - $default_values = array(); + $default_values = array(); if (count($answer)) { $key = 0; foreach ($answer as $answer){ $default_values['answer['.$key.']'] = $answer->answer; - // echo "

$answer->fraction

"; $default_values['fraction['.$key.']'] = $answer->fraction; $default_values['tolerance['.$key.']'] = $answer->tolerance; $default_values['tolerancetype['.$key.']'] = $answer->tolerancetype; @@ -601,23 +561,30 @@ class question_edit_calculatedsimple_form extends question_edit_form { } $default_values['synchronize'] = 0 ; if (isset($question->options)){ - $default_values['unitgradingtype'] = $question->options->unitgradingtype ; - $default_values['unitpenalty'] = $question->options->unitpenalty ; - $default_values['showunits'] = $question->options->showunits ; - $default_values['unitsleft'] = $question->options->unitsleft ; - $default_values['instructions'] = $question->options->instructions ; + $default_values['unitgradingtype'] = $question->options->unitgradingtype ; + $default_values['unitpenalty'] = $question->options->unitpenalty ; + switch ($question->options->showunits){ + case 'O' : + case '1' : + $default_values['showunits0'] = $question->options->showunits ; + $default_values['unitrole'] = 0 ; + break; + case '2' : + case '3' : + $default_values['showunits1'] = $question->options->showunits ; + $default_values['unitrole'] = 1 ; + break; + } + $default_values['unitsleft'] = $question->options->unitsleft ; + $default_values['instructions'] = $question->options->instructions ; - $units = array_values($question->options->units); - // make sure the default unit is at index 0 - usort($units, create_function('$a, $b', - 'if (1.0 === (float)$a->multiplier) { return -1; } else '. - 'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }')); - if (count($units)) { - $key = 0; - foreach ($units as $unit){ - $default_values['unit['.$key.']'] = $unit->unit; - $default_values['multiplier['.$key.']'] = $unit->multiplier; - $key++; + if (isset($question->options->units)){ + $units = array_values($question->options->units); + if (!empty($units)) { + foreach ($units as $key => $unit){ + $default_values['unit['.$key.']'] = $unit->unit; + $default_values['multiplier['.$key.']'] = $unit->multiplier; + } } } } @@ -682,6 +649,7 @@ class question_edit_calculatedsimple_form extends question_edit_form { } function validation($data, $files) { + global $QTYPES; $errors = parent::validation($data, $files); //verifying for errors in {=...} in question text; $qtext = ""; @@ -765,6 +733,7 @@ class question_edit_calculatedsimple_form extends question_edit_form { $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction); } }*/ + $QTYPES['numerical']->validate_numerical_options($data, $errors) ; $units = $data['unit']; if (count($units)) { foreach ($units as $key => $unit){ diff --git a/question/type/calculatedsimple/questiontype.php b/question/type/calculatedsimple/questiontype.php index f7cfb62834b..f528722217f 100644 --- a/question/type/calculatedsimple/questiontype.php +++ b/question/type/calculatedsimple/questiontype.php @@ -407,6 +407,8 @@ class question_calculatedsimple_qtype extends question_calculated_qtype { ////////////////////////////////////////////////////////////////////////// question_register_questiontype(new question_calculatedsimple_qtype()); -define("CALCULATEDSIMPLE", "calculatedsimple"); +if ( ! defined ("CALCULATEDSIMPLE")) { + define("CALCULATEDSIMPLE", "calculatedsimple"); +} diff --git a/question/type/numerical/edit_numerical_form.php b/question/type/numerical/edit_numerical_form.php index 48fb7c1275e..38d570b016a 100644 --- a/question/type/numerical/edit_numerical_form.php +++ b/question/type/numerical/edit_numerical_form.php @@ -38,73 +38,17 @@ class question_edit_numerical_form extends question_edit_form { $this->add_per_answer_fields($mform, get_string('answerno', 'qtype_numerical', '{no}'), $creategrades->gradeoptions); //------------------------------------------------------------------------------------------ - $QTYPES['numerical']->edit_numerical_options($mform,$this); - /* - $mform->addElement('header', 'unithandling', get_string("Units handling", 'qtype_numerical')); - $currentgrp1 = array(); - - $currentgrp1[] =& $mform->createElement('text', 'unitpenalty', get_string('Penalty for bad unit', 'qtype_numerical') , - array('size' => 3)); - $currentgrp1[] =& $mform->createElement('static', 'penalty1','hello', get_string('as decimal fraction (0-1) of', 'qtype_numerical')); - $mform->addGroup($currentgrp1, 'penaltygrp', get_string('Penalty for bad unit', 'qtype_numerical'), null, false); - $mform->setType('unitpenalty', PARAM_NUMBER); - //$mform->addRule('unitpenalty', null, 'required', null, 'client'); - $mform->setDefault('unitpenalty', 0.1); - $currentgrp = array(); - $currentgrp[] =& $mform->createElement('radio', 'unitgradingtype', 'or', get_string('question grade', 'qtype_numerical'),1); - $currentgrp[] =& $mform->createElement('radio', 'unitgradingtype', '', get_string(' response grade', 'qtype_numerical'),2); - $mform->setDefault('unitgradingtype', 1); - $mform->addGroup($currentgrp, 'penaltychoicegrp', '',' or ', false); - $mform->setHelpButton('penaltychoicegrp', array('penaltygrp', get_string('unitpenalty', 'qtype_numerical'), 'qtype_numerical')); - $mform->addElement('radio', 'showunits', 'Edit unit ', get_string('Editable text input element', 'qtype_numerical'),0); - $mform->addElement('radio', 'showunits', 'Select units ', get_string('Choice radio element', 'qtype_numerical'),1); - $mform->addElement('radio', 'showunits', 'Display unit ', get_string('NON editable text of Unit No1', 'qtype_numerical'),2); - $mform->addElement('radio', 'showunits', 'No unit display', get_string("Only numerical answer will be graded leave Unit No1 empty", 'qtype_numerical'),3); - $mform->setDefault('showunits', 0); - $currentgrp = array(); - $leftgrp[] =& $mform->createElement('radio', 'unitsleft', '', get_string('left as $1.00', 'qtype_numerical'),1); - $leftgrp[] =& $mform->createElement('radio', 'unitsleft', '', get_string('rigth as 1.00cm', 'qtype_numerical'),0); - $mform->addGroup($leftgrp, 'unitsleft', 'Unit position',' or ', false); - $mform->setDefault('unitsleft', 0); - $repeated = array();*/ - $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}')); - - $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz')); - $mform->setType('unit', PARAM_NOTAGS); - - $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz')); - $mform->setType('multiplier', PARAM_NUMBER); - - if (isset($this->question->options)){ - $countunits = count($this->question->options->units); - } else { - $countunits = 0; - } - - if ($this->question->formoptions->repeatelements){ - $repeatsatstart = $countunits + 2; - } else { - $repeatsatstart = $countunits; - } - $this->repeat_elements($repeated, $repeatsatstart, array(), 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_numerical')); - - if ($mform->elementExists('multiplier[0]')) { - /// Does not exist when this form is used in 'move to another category' - /// mode with a qusetion that has no units. This was leading to errors. - $firstunit =& $mform->getElement('multiplier[0]'); - $firstunit->freeze(); - $firstunit->setValue('1.0'); - $firstunit->setPersistantFreeze(true); - } + $QTYPES['numerical']->add_units_options($mform,$this); + $QTYPES['numerical']->add_units_elements($mform,$this); } function set_data($question) { if (isset($question->options)){ - $default_values['unitgradingtype'] = $question->options->unitgradingtype ; - $default_values['unitpenalty'] = $question->options->unitpenalty ; - $default_values['showunits'] = $question->options->showunits ; - $default_values['unitsleft'] = $question->options->unitsleft ; - $default_values['instructions'] = $question->options->instructions ; + $default_values['unitgradingtype'] = $question->options->unitgradingtype ; + $default_values['unitpenalty'] = $question->options->unitpenalty ; + $default_values['showunits'] = $question->options->showunits ; + $default_values['unitsleft'] = $question->options->unitsleft ; + $default_values['instructions'] = $question->options->instructions ; $answers = $question->options->answers; if (count($answers)) { @@ -117,11 +61,13 @@ class question_edit_numerical_form extends question_edit_form { $key++; } } - $units = array_values($question->options->units); - if (!empty($units)) { - foreach ($units as $key => $unit){ - $default_values['unit['.$key.']'] = $unit->unit; - $default_values['multiplier['.$key.']'] = $unit->multiplier; + if (isset($question->options->units)){ + $units = array_values($question->options->units); + if (!empty($units)) { + foreach ($units as $key => $unit){ + $default_values['unit['.$key.']'] = $unit->unit; + $default_values['multiplier['.$key.']'] = $unit->multiplier; + } } } $question = (object)((array)$question + $default_values); @@ -129,6 +75,7 @@ class question_edit_numerical_form extends question_edit_form { parent::set_data($question); } function validation($data, $files) { + global $QTYPES; $errors = parent::validation($data, $files); // Check the answers. @@ -156,22 +103,7 @@ class question_edit_numerical_form extends question_edit_form { if ($maxgrade == false) { $errors['fraction[0]'] = get_string('fractionsnomax', 'question'); } - - // Check units. - $alreadyseenunits = array(); - if (isset($data['unit'])) { - foreach ($data['unit'] as $key => $unit) { - $trimmedunit = trim($unit); - if ($trimmedunit!='' && in_array($trimmedunit, $alreadyseenunits)) { - $errors["unit[$key]"] = get_string('errorrepeatedunit', 'qtype_numerical'); - if (trim($data['multiplier'][$key]) == '') { - $errors["multiplier[$key]"] = get_string('errornomultiplier', 'qtype_numerical'); - } - } else { - $alreadyseenunits[] = $trimmedunit; - } - } - } + $QTYPES['numerical']->validate_numerical_options($data, $errors) ; return $errors; } diff --git a/question/type/numerical/questiontype.php b/question/type/numerical/questiontype.php index afe5bc07b44..e8c3b6d63dc 100644 --- a/question/type/numerical/questiontype.php +++ b/question/type/numerical/questiontype.php @@ -44,7 +44,6 @@ class question_numerical_qtype extends question_shortanswer_qtype { // the question table as is usually the case for qtype // specific tables. global $CFG, $DB, $OUTPUT; - $this->get_numerical_options($question); if (!$question->options->answers = $DB->get_records_sql( "SELECT a.*, n.tolerance " . "FROM {question_answers} a, " . @@ -56,6 +55,9 @@ class question_numerical_qtype extends question_shortanswer_qtype { return false; } $this->get_numerical_units($question); + //get_numerical_options() need to know if there are units + // to set correctly default values + $this->get_numerical_options($question); // If units are defined we strip off the default unit from the answer, if // it is present. (Required for compatibility with the old code and DB). @@ -74,20 +76,25 @@ class question_numerical_qtype extends question_shortanswer_qtype { } function get_numerical_options(&$question) { global $DB; - if (!$options = $DB->get_record('question_numerical_options', array('question' => $question->id))) { - // echo $OUTPUT->notification('Error: Missing question options for numerical question'.$question->id.'!'); - // return false; - $question->options->unitgradingtype = 0; - $question->options->unitpenalty = 0; - $question->options->showunits = 0 ; - $question->options->unitsleft = 0 ; - $question->options->instructions = '' ; + if (!$options = $DB->get_record('question_numerical_options', array('question' => $question->id))) { + $question->options->unitgradingtype = 0; + $question->options->unitpenalty = 0; + // the default + if ($defaultunit = $this->get_default_numerical_unit($question)) { + // so units can be graded + $question->options->showunits = 0 ; + }else { + // only numerical will be graded + $question->options->showunits = 3 ; + } + $question->options->unitsleft = 0 ; + $question->options->instructions = '' ; } else { - $question->options->unitgradingtype = $options->unitgradingtype; - $question->options->unitpenalty = $options->unitpenalty; - $question->options->showunits = $options->showunits ; - $question->options->unitsleft = $options->unitsleft ; - $question->options->instructions = $options->instructions ; + $question->options->unitgradingtype = $options->unitgradingtype; + $question->options->unitpenalty = $options->unitpenalty; + $question->options->showunits = $options->showunits ; + $question->options->unitsleft = $options->unitsleft ; + $question->options->instructions = $options->instructions ; } @@ -235,10 +242,25 @@ class question_numerical_qtype extends question_shortanswer_qtype { }else { $options->unitpenalty = 0 ; } - if(isset($question->showunits)){ - $options->showunits = $question->showunits; - }else { - $options->showunits = 0 ; + // if we came from the form then 'unitrole' exists + if(isset($question->unitrole)){ + if ($question->unitrole == 0 ){ + $options->showunits = $question->showunits0; + }else { + $options->showunits = $question->showunits1; + } + }else { + if(isset($question->showunits)){ + $options->showunits = $question->showunits; + }else { + if ($defaultunit = $this->get_default_numerical_unit($question)) { + // so units can be graded + $options->showunits = 0 ; + }else { + // only numerical will be graded + $options->showunits = 3 ; + } + } } if(isset($question->unitsleft)){ $options->unitsleft = $question->unitsleft; @@ -279,9 +301,11 @@ class question_numerical_qtype extends question_shortanswer_qtype { // Save the new units. $units = array(); + $unitalreadyinsert = array(); foreach ($question->multiplier as $i => $multiplier) { // Discard any unit which doesn't specify the unit or the multiplier - if (!empty($question->multiplier[$i]) && !empty($question->unit[$i])) { + if (!empty($question->multiplier[$i]) && !empty($question->unit[$i])&& !array_key_exists(addslashes($question->unit[$i]),$unitalreadyinsert)) { + $unitalreadyinsert[addslashes($question->unit[$i])] = 1 ; $units[$i] = new stdClass; $units[$i]->question = $question->id; $units[$i]->multiplier = $this->apply_unit($question->multiplier[$i], array()); @@ -665,58 +689,157 @@ class question_numerical_qtype extends question_shortanswer_qtype { // Invalid number. Must be wrong. return false; } - function edit_numerical_options(&$mform, &$that){ - $mform->addElement('header', 'unithandling', get_string("Units handling", 'qtype_numerical')); - // $mform->addElement('checkbox', 'usecurrentcat1', get_string('Don\'t use unit','qtype_numerical'), get_string('leave Unit(s) input field empty ','qtype_numerical')); - $mform->addElement('radio', 'showunits', 'No unit display', get_string("Only numerical answer will be graded: leave Unit No1 empty", 'qtype_numerical'),3); - $mform->addElement('radio', 'showunits', 'Display unit ', get_string('NON editable text of Unit No1', 'qtype_numerical'),2); - $mform->addElement('radio', 'showunits', 'Edit unit ', get_string('Editable text input element', 'qtype_numerical'),0); - $mform->addElement('radio', 'showunits', 'Select units ', get_string('Choice radio element of 2 Units minimum', 'qtype_numerical'),1); - $unitslefts = array('0' => get_string('rigth as 1.00cm', 'qtype_numerical'),'1' => get_string('left as $1.00', 'qtype_numerical')); - $mform->addElement('select', 'unitsleft', 'Unit position' , $unitslefts ); +/** + * function used in in function definition_inner() + * of edit_..._form.php for + * numerical, calculated, calculatedsimple + */ + function add_units_options(&$mform, &$that){ + $mform->addElement('header', 'unithandling', get_string('unitshandling', 'qtype_numerical')); + // Units are graded + $mform->addElement('radio', 'unitrole', get_string('unitgraded1', 'qtype_numerical'), get_string('unitgraded', 'qtype_numerical'),0); + $penaltygrp = array(); + $penaltygrp[] =& $mform->createElement('text', 'unitpenalty', get_string('unitpenalty', 'qtype_numerical') , + array('size' => 6)); + $unitgradingtypes = array('1' => get_string('decfractionofquestiongrade', 'qtype_numerical'), '2' => get_string('decfractionofresponsegrade', 'qtype_numerical')); + $penaltygrp[] =& $mform->createElement('select', 'unitgradingtype', '' , $unitgradingtypes ); + $mform->addGroup($penaltygrp, 'penaltygrp', get_string('unitpenalty', 'qtype_numerical'),' ' , false); + $showunits0grp = array(); + $showunits0grp[] =& $mform->createElement('radio', 'showunits0', get_string('unitedit', 'qtype_numerical'), get_string('editableunittext', 'qtype_numerical'),0); + $showunits0grp[] =& $mform->createElement('radio', 'showunits0', get_string('selectunits', 'qtype_numerical') , get_string('unitchoice', 'qtype_numerical'),1); + $mform->addGroup($showunits0grp, 'showunits0grp', get_string('studentunitanswer', 'qtype_numerical'),' OR ' , false); + $mform->addElement('htmleditor', 'instructions', get_string('instructions', 'qtype_numerical'), + array('rows' => 10, 'course' => $that->coursefilesid)); + $mform->addElement('static', 'separator1', '
', '
'); + // Units are not graded + $mform->addElement('radio', 'unitrole', get_string('unitnotgraded', 'qtype_numerical'), get_string('onlynumerical', 'qtype_numerical'),1); + $showunits1grp = array(); + $showunits1grp[] = & $mform->createElement('radio', 'showunits1', '', get_string('no', 'moodle'),3); + $showunits1grp[] = & $mform->createElement('radio', 'showunits1', '', get_string('yes', 'moodle'),2); + $mform->addGroup($showunits1grp, 'showunits1grp', get_string('unitdisplay', 'qtype_numerical'),' ' , false); + $unitslefts = array('0' => get_string('rightexample', 'qtype_numerical'),'1' => get_string('leftexample', 'qtype_numerical')); + $mform->addElement('static', 'separator2', '
', '
'); + $mform->addElement('select', 'unitsleft', get_string('unitposition', 'qtype_numerical') , $unitslefts ); $currentgrp1 = array(); - $currentgrp1[] =& $mform->createElement('text', 'unitpenalty', get_string('Penalty for bad unit', 'qtype_numerical') , - array('size' => 3)); - $currentgrp1[] =& $mform->createElement('static', 'penalty1','', get_string('as decimal fraction (0-1) of', 'qtype_numerical')); - $mform->addGroup($currentgrp1, 'penaltygrp', get_string('Penalty for bad unit', 'qtype_numerical'), null, false); $mform->setType('unitpenalty', PARAM_NUMBER); - //$mform->addRule('unitpenalty', null, 'required', null, 'client'); $mform->setDefault('unitpenalty', 0.1); - $currentgrp = array(); - $unitgradingtypes = array('1' => get_string('question grade', 'qtype_numerical'), '2' => get_string(' response grade', 'qtype_numerical')); - $mform->addElement('select', 'unitgradingtype', '' , $unitgradingtypes ); - /* $currentgrp[] =& $mform->createElement('radio', 'unitgradingtype', 'or', get_string('question grade', 'qtype_numerical'),1); - $currentgrp[] =& $mform->createElement('radio', 'unitgradingtype', '', get_string(' response grade', 'qtype_numerical'),2);;*/ $mform->setDefault('unitgradingtype', 1); - // $mform->addGroup($currentgrp, 'penaltychoicegrp', '',' or ', false); - $mform->setHelpButton('unitgradingtype', array('penaltygrp', get_string('unitpenalty', 'qtype_numerical'), 'qtype_numerical')); - $mform->setDefault('showunits', 0); - $currentgrp = array(); - /* $leftgrp[] =& $mform->createElement('radio', 'unitsleft', '', get_string('left as $1.00', 'qtype_numerical'),1); - $leftgrp[] =& $mform->createElement('radio', 'unitsleft', '', get_string('rigth as 1.00cm', 'qtype_numerical'),0);*/ - // $mform->addGroup($leftgrp, 'unitsleft', 'Unit position',' or ', false); + $mform->setHelpButton('penaltygrp', array('penaltygrp', get_string('unitpenalty', 'qtype_numerical'), 'qtype_numerical')); + $mform->setDefault('showunits0', 0); + $mform->setDefault('showunits1', 3); $mform->setDefault('unitsleft', 0); - $mform->addElement('htmleditor', 'instructions', get_string('instructions', 'quiz'), - array('rows' => 10, 'course' => $that->coursefilesid)); - $mform->setAdvanced('instructions',true); $mform->setType('instructions', PARAM_RAW); - $mform->setHelpButton('instructions', array('instructions', get_string('instructions', 'quiz'), 'quiz')); - // $mform->disabledIf('showunits', 'usecurrentcat1', 'checked'); - $mform->disabledIf('addunits', 'showunits','eq','3'); - $mform->disabledIf('addunits', 'showunits','eq','2'); - $mform->disabledIf('unitpenalty', 'showunits','eq','3'); - $mform->disabledIf('unitpenalty', 'showunits','eq','2'); - $mform->disabledIf('unitgradingtype', 'showunits','eq','3'); - $mform->disabledIf('unitgradingtype', 'showunits','eq','2'); - $mform->disabledIf('instructions', 'showunits','eq','3'); - $mform->disabledIf('instructions', 'showunits','eq','2'); - - // $mform->disabledIf('usecurrentcat1', 'showunits','eq','3'); - // $mform->disabledIf('usecurrentcat1', 'units[0]','eq','3'); + $mform->setHelpButton('instructions', array('instructions', get_string('instructions', 'qtype_numerical'), 'quiz')); + $mform->disabledIf('penaltygrp', 'unitrole','eq','1'); + $mform->disabledIf('unitgradingtype', 'unitrole','eq','1'); + $mform->disabledIf('instructions', 'unitrole','eq','1'); + $mform->disabledIf('unitsleft', 'showunits1','eq','3'); + $mform->disabledIf('showunits1','unitrole','eq','0'); + $mform->disabledIf('showunits0','unitrole','eq','1'); } +/** + * function used in in function definition_inner() + * of edit_..._form.php for + * numerical, calculated, calculatedsimple + */ + function add_units_elements(& $mform,& $that) { + $repeated = array(); + $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}')); + + $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz')); + $mform->setType('unit', PARAM_NOTAGS); + + $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz')); + $mform->setType('multiplier', PARAM_NUMBER); + + if (isset($this->question->options)){ + $countunits = count($that->question->options->units); + } else { + $countunits = 0; + } + if ($that->question->formoptions->repeatelements){ + $repeatsatstart = $countunits + 1; + } else { + $repeatsatstart = $countunits; + } + $that->repeat_elements($repeated, $repeatsatstart, array(), 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}')); + + if ($mform->elementExists('multiplier[0]')){ + $firstunit =& $mform->getElement('multiplier[0]'); + $firstunit->freeze(); + $firstunit->setValue('1.0'); + $firstunit->setPersistantFreeze(true); + } + } +/** + * function use in in function validation() + * of edit_..._form.php for + * numerical, calculated, calculatedsimple + */ + + function validate_numerical_options(& $data, & $errors){ + $units = $data['unit']; + if ($data['unitrole'] == 0 ){ + $showunits = $data['showunits0']; + }else { + $showunits = $data['showunits1']; + } + + if (($showunits == 0) || ($showunits == 1) || ($showunits == 2)){ + if (trim($units[0]) == ''){ + $errors['unit[0]'] = 'You must set a valid unit name' ; + } + } + if ($showunits == 3 ){ + if (count($units)) { + foreach ($units as $key => $unit){ + if ($units[$key] != ''){ + $errors["unit[$key]"] = 'You must erase this unit name' ; + } + } + } + } + + + // Check double units. + $alreadyseenunits = array(); + if (isset($data['unit'])) { + foreach ($data['unit'] as $key => $unit) { + $trimmedunit = trim($unit); + if ($trimmedunit!='' && in_array($trimmedunit, $alreadyseenunits)) { + $errors["unit[$key]"] = get_string('errorrepeatedunit', 'qtype_numerical'); + if (trim($data['multiplier'][$key]) == '') { + $errors["multiplier[$key]"] = get_string('errornomultiplier', 'qtype_numerical'); + } + } elseif($trimmedunit!='') { + $alreadyseenunits[] = $trimmedunit; + } + } + } + $units = $data['unit']; + if (count($units)) { + foreach ($units as $key => $unit){ + if (is_numeric($unit)){ + $errors['unit['.$key.']'] = get_string('mustnotbenumeric', 'qtype_calculated'); + } + $trimmedunit = trim($unit); + $trimmedmultiplier = trim($data['multiplier'][$key]); + if (!empty($trimmedunit)){ + if (empty($trimmedmultiplier)){ + $errors['multiplier['.$key.']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated'); + } + if (!is_numeric($trimmedmultiplier)){ + $errors['multiplier['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated'); + } + + } + } + } + + } function print_question_grading_details(&$question, &$state, $cmoptions, $options) { // echo "

state uestion_grading_details $question->id $question->qtype

";print_r($state);echo "

";