From: pichetp Date: Wed, 7 Oct 2009 20:45:45 +0000 (+0000) Subject: MDL-20296 Units handling for numerical and calculatedquestions X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=b003b818176d81eab0b64dbd1644b5740331cb50;p=moodle.git MDL-20296 Units handling for numerical and calculatedquestions --- diff --git a/question/type/calculated/edit_calculated_form.php b/question/type/calculated/edit_calculated_form.php index 8286f4130d..80537cc33f 100644 --- a/question/type/calculated/edit_calculated_form.php +++ b/question/type/calculated/edit_calculated_form.php @@ -193,8 +193,14 @@ class question_edit_calculated_form extends question_edit_form { $mform->addElement('hidden', 'multiplier'."[$i]", optional_param('multiplier'."[$i]", '', PARAM_NUMBER)); $mform->setType('multiplier'."[$i]", PARAM_NUMBER); } + $mform->addElement('hidden','unitgradingtype',optional_param('unitgradingtype', '', PARAM_INT)) ; + $mform->addElement('hidden','unitpenalty',optional_param('unitpenalty', '', PARAM_NUMBER)) ; + $mform->addElement('hidden','showunits',optional_param('showunits', '', PARAM_INT)) ; + $mform->addElement('hidden','unitsleft',optional_param('unitsleft', '', PARAM_INT)) ; + $mform->addElement('hidden','instructions',optional_param('instructions', '', PARAM_RAW)) ; }else { + $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')); @@ -253,7 +259,7 @@ class question_edit_calculated_form extends question_edit_form { function set_data($question) { if (isset($this->editasmultichoice)){ - $default_values['multichoice']= 1 ; //$this->editasmultichoice ; + $default_values['multichoice']= $this->editasmultichoice ; //$this->editasmultichoice ; } if (isset($question->options)){ $answers = $question->options->answers; @@ -270,6 +276,12 @@ class question_edit_calculated_form extends question_edit_form { $key++; } } + $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 ; + if (isset($question->options->units)){ $units = array_values($question->options->units); // make sure the default unit is at index 0 diff --git a/question/type/calculatedsimple/edit_calculatedsimple_form.php b/question/type/calculatedsimple/edit_calculatedsimple_form.php index 2ce33499e7..0ef397a17f 100644 --- a/question/type/calculatedsimple/edit_calculatedsimple_form.php +++ b/question/type/calculatedsimple/edit_calculatedsimple_form.php @@ -52,6 +52,8 @@ class question_edit_calculatedsimple_form extends question_edit_form { global $QTYPES, $SESSION, $CFG, $DB; $this->regenerate = true; $this->question = $question; + echo "

question

";print_r($question);echo "

"; + $this->qtypeobj =& $QTYPES[$this->question->qtype]; //get the dataset definitions for this question //coming here everytime even when using a NoSubmitButton @@ -136,6 +138,7 @@ class question_edit_calculatedsimple_form extends question_edit_form { if(trim($answer) != ''){ // just look for non-empty $this->answer[$key]=new stdClass(); $this->answer[$key]->answer = $answer; + $this->answer[$key]->fraction = $fraction[$key]; $this->answer[$key]->tolerance = $tolerance[$key]; $this->answer[$key]->tolerancetype = $tolerancetype[$key]; $this->answer[$key]->correctanswerlength = $correctanswerlength[$key]; @@ -150,7 +153,8 @@ class question_edit_calculatedsimple_form extends question_edit_form { if ($olddef = optional_param('datasetdef', '', PARAM_RAW)){ $calcmin = optional_param('calcmin', '', PARAM_NUMBER); $calclength = optional_param('calclength', '', PARAM_INT); - $calcmax = optional_param('calcmax', '', PARAM_NUMBER); + $calcmax = optional_param('calcmax', '', PARAM_NUMBER); + $oldoptions = optional_param('defoptions', '', PARAM_RAW); $newdatasetvalues = false ; for($key = 1 ; $key <= sizeof($olddef) ; $key++) { $def = $olddef[$key] ; @@ -158,7 +162,7 @@ class question_edit_calculatedsimple_form extends question_edit_form { $this->datasetdefs[$def]->type = 1; $this->datasetdefs[$def]->category = 0; // $this->datasets[$key]->name = $datasetname; - // $this->datasetdefs[$def]->options = $oldoptions[$key] ; + $this->datasetdefs[$def]->options = $oldoptions[$key] ; $this->datasetdefs[$def]->calcmin = $calcmin[$key] ; $this->datasetdefs[$def]->calcmax = $calcmax[$key] ; $this->datasetdefs[$def]->calclength = $calclength[$key] ; @@ -283,6 +287,8 @@ class question_edit_calculatedsimple_form extends question_edit_form { $this->qtypeobj =& $QTYPES[$this->qtype()]; $strquestionlabel = $this->qtypeobj->comment_header($this->nonemptyanswer); $label = get_string("sharedwildcards", "qtype_datasetdependent"); + $mform->addElement('hidden', 'multichoice', 0); + $mform->addElement('hidden', 'synchronize', 0); $mform->addElement('hidden', 'initialcategory', 1); $mform->setType('initialcategory', PARAM_INT); $mform->addElement('hidden', 'reload', 1); @@ -297,6 +303,8 @@ 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); + $repeated = array(); $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}')); @@ -324,6 +332,7 @@ class question_edit_calculatedsimple_form extends question_edit_form { $firstunit->setValue('1.0'); $firstunit->setPersistantFreeze(true); } + //hidden elements // $mform->addElement('hidden', 'wizard', 'datasetdefinitions'); // $mform->setType('wizard', PARAM_ALPHA); @@ -359,6 +368,8 @@ class question_edit_calculatedsimple_form extends question_edit_form { $this->qtypeobj->custom_generator_tools_part($mform, $idx, $j); $mform->addElement('hidden', "datasetdef[$idx]"); $mform->setType("datasetdef[$idx]", PARAM_RAW); + $mform->addElement('hidden', "defoptions[$idx]"); + $mform->setType("defoptions[$idx]", PARAM_RAW); $idx++; $mform->addElement('static', "divider[$j]", '', '
'); $j++; @@ -538,6 +549,7 @@ class question_edit_calculatedsimple_form extends question_edit_form { $mform->closeHeaderBefore('warningnowildcards'); } + //------------------------------------------------------------------------------------------------------------------------------ //non standard name for button element needed so not using add_action_buttons //hidden elements @@ -575,10 +587,11 @@ class question_edit_calculatedsimple_form extends question_edit_form { function set_data($question) { $answer = $this->answer; $default_values = array(); - /* if (count($answer)) { + 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; @@ -587,8 +600,16 @@ class question_edit_calculatedsimple_form extends question_edit_form { $default_values['feedback['.$key.']'] = $answer->feedback; $key++; } - }*/ + } + $default_values['multichoice'] = 0 ; + $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 ; + $units = array_values($question->options->units); // make sure the default unit is at index 0 usort($units, create_function('$a, $b', @@ -781,7 +802,7 @@ class question_edit_calculatedsimple_form extends question_edit_form { } /*Here we use the already done the error analysis so that * we could force all wild cards values display if there is an error in values. - * as using a , in a number */ + * as using a , in a number *//* $numbers = $data['number']; foreach ($numbers as $key => $number){ if(! is_numeric($number)){ @@ -796,7 +817,7 @@ class question_edit_calculatedsimple_form extends question_edit_form { $errors['number['.$key.']'] = get_string('notvalidnumber', 'qtype_datasetdependent'); } } - + */ if ( $this->noofitems==0 ){ $errors['warning'] = get_string('warning', 'mnet'); } diff --git a/question/type/calculatedsimple/questiontype.php b/question/type/calculatedsimple/questiontype.php index 787dba5fdc..3009c28f14 100644 --- a/question/type/calculatedsimple/questiontype.php +++ b/question/type/calculatedsimple/questiontype.php @@ -24,7 +24,7 @@ class question_calculatedsimple_qtype extends question_calculated_qtype { function save_question_options($question) { //$options = $question->subtypeoptions; // Get old answers: - global $CFG, $DB; + global $CFG, $DB , $QTYPES; if (isset($question->answer) && !isset($question->answers)) { $question->answers = $question->answer; @@ -40,7 +40,7 @@ class question_calculatedsimple_qtype extends question_calculated_qtype { } // Save the units. - $virtualqtype = $this->get_virtual_qtype(); + $virtualqtype = $this->get_virtual_qtype($question); $result = $virtualqtype->save_numerical_units($question); if (isset($result->error)) { return $result; @@ -180,6 +180,11 @@ class question_calculatedsimple_qtype extends question_calculated_qtype { if(!empty($question->makecopy) && !empty($question->convert)) { $DB->set_field('question', 'qtype', 'calculated', array('id'=> $question->id)); } + $result = $QTYPES['numerical']->save_numerical_options($question); + if (isset($result->error)) { + return $result; + } + if (!empty($result->notice)) { return $result; } @@ -261,7 +266,7 @@ class question_calculatedsimple_qtype extends question_calculated_qtype { } function comment_on_datasetitems($questionid, $answers,$data, $number) { - global $DB; + global $DB,$QTYPES; $comment = new stdClass; $comment->stranswers = array(); $comment->outsidelimit = false ; @@ -278,7 +283,7 @@ class question_calculatedsimple_qtype extends question_calculated_qtype { $strmax = get_string('max', 'quiz'); $errors = ''; $delimiter = ': '; - $virtualqtype = $this->get_virtual_qtype(); + $virtualqtype = & $QTYPES['numerical'] ; //$this->get_virtual_qtype($question); foreach ($answers as $key => $answer) { $formula = $this->substitute_variables($answer->answer,$data); $formattedanswer = qtype_calculated_calculate_answer( diff --git a/question/type/numerical/db/install.xml b/question/type/numerical/db/install.xml index 731cfe9a1d..735fc4b466 100644 --- a/question/type/numerical/db/install.xml +++ b/question/type/numerical/db/install.xml @@ -1,10 +1,10 @@ - - +
@@ -19,7 +19,22 @@
- +
+ + + + + + + + + + + + + +
+ diff --git a/question/type/numerical/db/upgrade.php b/question/type/numerical/db/upgrade.php index 57813f207f..930f91cfb1 100644 --- a/question/type/numerical/db/upgrade.php +++ b/question/type/numerical/db/upgrade.php @@ -27,6 +27,30 @@ function xmldb_qtype_numerical_upgrade($oldversion) { $result = true; //===== 1.9.0 upgrade line ======// + if ($result && $oldversion < 2009100100 ) { //New version in version.php + + /// Define table question_numerical_options to be created + $table = new xmldb_table('question_numerical_options'); + + /// Adding fields to table question_numerical_options + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('question', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('instructions', XMLDB_TYPE_TEXT, 'small', null, null, null, null); + $table->add_field('showunits', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('unitsleft', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('unitgradingtype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('unitpenalty', XMLDB_TYPE_NUMBER, '12, 7', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0.1'); + + /// Adding keys to table question_numerical_options + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('question', XMLDB_KEY_FOREIGN, array('question'), 'question', array('id')); + /// Conditionally launch create table for question_calculated_options + if (!$dbman->table_exists($table)) { + // $dbman->create_table doesnt return a result, we just have to trust it + $dbman->create_table($table); + }//else + upgrade_plugin_savepoint($result, 2009100100, 'qtype', 'numerical'); + } return $result; } diff --git a/question/type/numerical/display.html b/question/type/numerical/display.html new file mode 100644 index 0000000000..daea9e6715 --- /dev/null +++ b/question/type/numerical/display.html @@ -0,0 +1,226 @@ +name_prefix."unit\""; + $nameanswer = "name=\"".$question->name_prefix."answer\""; +//if left unit is placed before answer so we need to answer field text and a center pice for the show units options +// split the respnse in 2 element numerical and units +/*if (!empty($question->options->showunits)){ + if(isset($question->options->units) && count($question->options->units)){ + $length = 0; + $goodkey = 0 ; + foreach ($question->options->units as $key => $unit){ + if (false === stripos($value, $unit->unit)){ + }else{ + + if(strlen($unit->unit)> $length) {$length =strlen($unit->unit); + $goodkey = $key ; + } + } + } + if($length){ + $value = str_replace($question->options->units[$goodkey]->unit, '' , $value); + } + $state->responses['unit']=$goodkey ;
+ } + + }*/ + if (isset($state->responses['answer']) && $state->responses['answer']!='') { + $valueanswer = ' value="'.s($state->responses['answer']).'" '; + } else { + $valueanswer = ' value="" '; + } + if (isset($state->responses['unit']) && $state->responses['unit']!='') { + $valueunit = ' value="'.s($state->responses['unit']).'" '; + } else { + + $valueunit = ' value="" '; + if ($question->options->showunits == 2 ){ + $valueunit = ' value="'.s($question->options->units[0]->unit).'" '; + } + } + // put a div to enclose the answer and unit elements
+ $textlength = 20 ; +?> + +options->unitsleft == 0 ){ ?> + +
+
+ + + + size="" /> + +
+
+ + +feedback){ + $classunit = question_get_feedback_class(1-$this->raw_unitpenalty); + $feedbackimgunit = question_get_feedback_image(1-$this->raw_unitpenalty, $options->feedback); + } + if ($question->options->showunits == 3 || (isset($question->options->units) && count($question->options->units) == 0 )) {// no unit display or tested + +?> +
+ + value ="" /> +
+options->showunits == 0 ) {// display unit text input +?> +
+
+ + + + size=""/> + +
+
+options->showunits == 2 ) {// display text + +?> +
+
+ + options->units[0]->unit ; + ?> + + + /> + options->units[0]->unit ; + + echo '  ' ; + ?> +
+
+ + options->showunits == 1){?> + + + +options->showunits == 1 && isset($question->options->units)){?> +
+
+ +options->units as $key => $unit) { + $checked = ''; + $chosen = false; + + $type = 'type="radio"'; + // $nameunit = "name=\"".$question->name_prefix;//."unit\""; + if (isset($state->responses['unit'])){ + if ($state->responses['unit'] == $unit->unit) { + $checked = 'checked="checked"'; + $chosen = true; + } + }else if ($key == 0) { + $checked = 'checked="checked"'; + $chosen = true; + } + + $aid = $question->id ; + $a = new stdClass; + $a->id = $question->name_prefix."unit" ;//. "2" + $a->class = ''; + $a->feedbackimg = ''; + + // Print the control + $a->control = ""; + + if ($options->correct_responses && $answer->fraction > 0) { + $a->class = question_get_feedback_class(1); + } + if (($options->feedback && $chosen) || $options->correct_responses) { + if ($type == ' type="checkbox" ') { + $a->feedbackimg = question_get_feedback_image($answer->fraction > 0 ? 1 : 0, $chosen && $options->feedback); + } else { + $a->feedbackimg = question_get_feedback_image($answer->fraction, $chosen && $options->feedback); + } + } + + // Print the answer text + $a->text = format_text($unit->unit, FORMAT_MOODLE, $formatoptions, $cmoptions->course); + $row = 0 ; + +?> + + + + + + +
+ control; ?> + + +
+ + + options->unitsleft == 1 ){?> + +
+
+ + + + size=""/> + +
+
+ put a div to enclose the answer and unit elements +?> + + + + + + + diff --git a/question/type/numerical/edit_numerical_form.php b/question/type/numerical/edit_numerical_form.php index 9bb9676aa7..2933439f64 100644 --- a/question/type/numerical/edit_numerical_form.php +++ b/question/type/numerical/edit_numerical_form.php @@ -31,13 +31,42 @@ class question_edit_numerical_form extends question_edit_form { * @param MoodleQuickForm $mform the form being built. */ function definition_inner(&$mform) { + global $QTYPES ; //------------------------------------------------------------------------------------------ $creategrades = get_grade_options(); $this->add_per_answer_fields($mform, get_string('answerno', 'qtype_numerical', '{no}'), $creategrades->gradeoptions); //------------------------------------------------------------------------------------------ - $repeated = array(); + $QTYPES['numerical']->edit_numerical_options($mform); + /* + $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')); @@ -71,6 +100,12 @@ class question_edit_numerical_form extends question_edit_form { 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 ; + $answers = $question->options->answers; if (count($answers)) { $key = 0; diff --git a/question/type/numerical/version.php b/question/type/numerical/version.php index e1b7f3eee1..c033bef16b 100644 --- a/question/type/numerical/version.php +++ b/question/type/numerical/version.php @@ -1,6 +1,6 @@ version = 2006121500; +$plugin->version = 2009100100; $plugin->requires = 2007101000; ?> diff --git a/question/type/shortanswer/display.html b/question/type/shortanswer/display.html index 6f1bbb478b..375a20b428 100644 --- a/question/type/shortanswer/display.html +++ b/question/type/shortanswer/display.html @@ -10,11 +10,14 @@
- + qtype == 'numerical' || $question->qtype == 'calculated' || $question->qtype == 'calculatedsimple' ){ + include("$CFG->dirroot/question/type/numerical/display.html");?> +
size="80"/>
+