From: pichetp set data answers outside
');
$j++;
}
+ $mform->addElement('header', 'updateanswershdr', get_string('answerstoleranceparam', 'qtype_datasetdependent'));
+ $mform->addElement('submit', 'updateanswers', get_string('updatetolerancesparam', 'qtype_datasetdependent'));
+ $mform->setAdvanced('updateanswers',true);
+
+ $answers = fullclone($this->question->options->answers);
+ $key1 =1;
+ foreach ($answers as $key => $answer) {
+ if ('' === $answer->answer){
+ }else if ('*' === $answer->answer){
+ $mform->addElement('static', 'answercomment['.($this->noofitems+$key1).']', $answer->answer);
+ $mform->addElement('hidden', 'tolerance['.$key.']', '');
+ $mform->setAdvanced('tolerance['.$key.']',true);
+ $mform->addElement('hidden', 'tolerancetype['.$key.']', '');
+ $mform->setAdvanced('tolerancetype['.$key.']',true);
+ $mform->addElement('hidden', 'correctanswerlength['.$key.']', '');
+ $mform->setAdvanced('correctanswerlength['.$key.']',true);
+ $mform->addElement('hidden', 'correctanswerformat['.$key.']', '');
+ $mform->setAdvanced('correctanswerformat['.$key.']',true);
+ }else {
+ $mform->addElement('static', 'answercomment['.($this->noofitems+$key1).']', $answer->answer);
+ $mform->addElement('text', 'tolerance['.$key.']', get_string('tolerance', 'qtype_calculated'));
+ $mform->setAdvanced('tolerance['.$key.']',true);
+ $mform->addElement('select', 'tolerancetype['.$key.']', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
+ $mform->setAdvanced('tolerancetype['.$key.']',true);
+
+ $mform->addElement('select', 'correctanswerlength['.$key.']', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
+ $mform->setAdvanced('correctanswerlength['.$key.']',true);
+
+ $answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
+ $mform->addElement('select', 'correctanswerformat['.$key.']', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
+ $mform->setAdvanced('correctanswerformat['.$key.']',true);
+ $mform->addElement('static', 'dividertolerance', '', '
');
+ $mform->setAdvanced('dividertolerance',true);
+ }
+ $key1++;
+ }
- if ('' != $strquestionlabel){
- $mform->addElement('static', 'answercomment['.($this->noofitems+1).']', $strquestionlabel);
- }
$addremoveoptions = Array();
$addremoveoptions['1']='1';
for ($i=10; $i<=100 ; $i+=10){
@@ -108,7 +145,7 @@ class question_dataset_dependent_items_form extends moodleform {
$addgrp[] =& $mform->createElement('select', "selectadd", get_string('additem', 'qtype_datasetdependent'), $addremoveoptions);
$addgrp[] = & $mform->createElement('static',"stat","Items",get_string('item(s)', 'qtype_datasetdependent'));
$mform->addGroup($addgrp, 'addgrp', '', ' ', false);
- $mform->addElement('static', "divideradd", '', '');
+ $mform->addElement('static', "divideradd", '', '');
// $mform->closeHeaderBefore('divideradd');
if ($this->noofitems > 0) {
$mform->addElement('header', 'additemhdr', get_string('delete', 'moodle'));
@@ -142,12 +179,15 @@ class question_dataset_dependent_items_form extends moodleform {
$repeated[] =& $mform->addElement('static', "answercomment[$i]", $strquestionlabel);
}
}
-
+ // if ($this->outsidelimit){
+ $mform->addElement('static','outsidelimit','','');
+ // }
//------------------------------------------------------------------------------------------------------------------------------
//non standard name for button element needed so not using add_action_buttons
+ if ( !($this->noofitems==0) ){
$mform->addElement('submit', 'backtoquiz', get_string('savechanges'));
$mform->closeHeaderBefore('backtoquiz');
-
+ }
//hidden elements
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
@@ -170,7 +210,25 @@ class question_dataset_dependent_items_form extends moodleform {
function set_data($question){
$formdata = array();
-
+ if (isset($question->options)){
+ $answers = $question->options->answers;
+ if (count($answers)) {
+ // $key = 0;
+ // echo""; print_r($answers);echo"
set data answers $key
"; print_r($answer);echo""; + + $formdata['tolerance['.$key.']'] = $answer->tolerance; + $formdata['tolerancetype['.$key.']'] = $answer->tolerancetype; + $formdata['correctanswerlength['.$key.']'] = $answer->correctanswerlength; + $formdata['correctanswerformat['.$key.']'] = $answer->correctanswerformat; + // $default_values['feedback['.$key.']'] = $answer->feedback; + // $key++; + } + } +} //fill out all data sets and also the fields for the next item to add. $j = $this->noofitems * count($this->datasetdefs); for ($itemnumber = $this->noofitems; $itemnumber >= 1; $itemnumber--){ @@ -184,7 +242,18 @@ class question_dataset_dependent_items_form extends moodleform { } $j--; } - $formdata['answercomment['.$itemnumber.']'] = $this->qtypeobj->comment_on_datasetitems($this->question, $data, $itemnumber); + $comment = $this->qtypeobj->comment_on_datasetitems($question->id,$question->options->answers, $data, $itemnumber); + // echo"
comment outside
"; print_r($comment);echo""; + if ($comment->outsidelimit) { + $this->outsidelimit=$comment->outsidelimit ; + // echo"
comment outside $comment->outsidelimit
"; + } + $totalcomment=''; + foreach ($question->options->answers as $key => $answer) { + $totalcomment .= $comment->stranswers[$key].'comment outside $comment->outsidelimit
"; + } + $key1 = 1; + foreach ($question->options->answers as $key => $answer) { + $formdata['answercomment['.($this->noofitems+$key1).']'] = $comment->stranswers[$key]; + $key1++; + } + if ($this->outsidelimit){ + $formdata['outsidelimit']= ''.get_string('oneanswertrueansweroutsidelimits', 'qtype_datasetdependent').''; + } $formdata = $this->qtypeobj->custom_generator_set_data($this->datasetdefs, $formdata); parent::set_data((object)($formdata + (array)$question)); @@ -231,8 +312,12 @@ class question_dataset_dependent_items_form extends moodleform { function validation($data, $files) { $errors = array(); - if (isset($data['backtoquiz']) && ($this->noofitems==0)){ + if (isset($data['backtoquiz']) && ($this->noofitems==0) ){ $errors['warning'] = get_string('warning', 'mnet'); + } + if ($this->outsidelimit){ + // if(!isset($errors['warning'])) $errors['warning']=' '; + // $errors['outsidelimits'] = get_string('oneanswertrueansweroutsidelimits','qtype_calculated'); } return $errors; } diff --git a/question/type/calculated/questiontype.php b/question/type/calculated/questiontype.php index bc10782411..60b627e902 100644 --- a/question/type/calculated/questiontype.php +++ b/question/type/calculated/questiontype.php @@ -602,6 +602,7 @@ class question_calculated_qtype extends default_questiontype { break; case 'datasetitems': $this->save_dataset_items($question, $form); + $this->save_question_calculated($question, $form); break; default: print_error('invalidwizardpage', 'question'); @@ -849,6 +850,25 @@ class question_calculated_qtype extends default_questiontype { return $datasetdefs; } + function save_question_calculated($question, $fromform){ + global $DB; + + foreach ($question->options->answers as $key => $answer) { + if ($options = $DB->get_record('question_calculated', array('answer' => $key))) { + $options->tolerance = trim($fromform->tolerance[$key]); + $options->tolerancetype = trim($fromform->tolerancetype[$key]); + $options->correctanswerlength = trim($fromform->correctanswerlength[$key]); + $options->correctanswerformat = trim($fromform->correctanswerformat[$key]); + if (! $DB->update_record('question_calculated', $options)) { + $mess->name = $this->name(); + $mess->id = $options->id ; + notify ( get_string('errorupdatingoptions','question',$mess)); + } + } + } + } + + function save_dataset_items($question, $fromform){ global $CFG, $DB; // max datasets = 100 items @@ -907,7 +927,7 @@ class question_calculated_qtype extends default_questiontype { $i++; } - if ($maxnumber < $addeditem->itemnumber){ + if (isset($addeditem->itemnumber) && $maxnumber < $addeditem->itemnumber){ $maxnumber = $addeditem->itemnumber; foreach ($datasetdefs as $key => $newdef) { if (isset($newdef->id) && $newdef->itemcount <= $maxnumber) { @@ -999,12 +1019,12 @@ class question_calculated_qtype extends default_questiontype { function comment_header($question) { //$this->get_question_options($question); - $strheader = ''; + $strheader = array(); $delimiter = ''; $answers = $question->options->answers; - foreach ($answers as $answer) { + foreach ($answers as $key => $answer) { if (is_string($answer)) { $strheader .= $delimiter.$answer; } else { @@ -1015,57 +1035,61 @@ class question_calculated_qtype extends default_questiontype { return $strheader; } - function comment_on_datasetitems($question, $data, $number) { + function comment_on_datasetitems($questionid, $answers,$data, $number) { global $DB; + $comment = new stdClass; + $comment->stranswers = array(); + $comment->outsidelimit = false ; + $comment->answers = array(); /// Find a default unit: - if (!empty($question->id) && $unit = $DB->get_record('question_numerical_units', array('question'=> $question->id, 'multiplier' => 1.0))) { + if (!empty($questionid) && $unit = $DB->get_record('question_numerical_units', array('question'=> $questionid, 'multiplier' => 1.0))) { $unit = $unit->unit; } else { $unit = ''; } - $answers = fullclone($question->options->answers); - $stranswers = ''; + $answers = fullclone($answers); $strmin = get_string('min', 'quiz'); $strmax = get_string('max', 'quiz'); $errors = ''; $delimiter = ': '; $virtualqtype = $this->get_virtual_qtype(); - foreach ($answers as $answer) { - // $formula = $answer->answer; + foreach ($answers as $key => $answer) { $formula = $this->substitute_variables($answer->answer,$data); - /* foreach ($data as $name => $value) { - $formula = str_replace('{'.$name.'}', $value, $formula); - }*/ $formattedanswer = qtype_calculated_calculate_answer( $answer->answer, $data, $answer->tolerance, $answer->tolerancetype, $answer->correctanswerlength, $answer->correctanswerformat, $unit); if ( $formula === '*'){ - $answer->min = '' ; + $answer->min = ' '; + $formattedanswer->answer = $answer->answer ; }else { eval('$answer->answer = '.$formula.';') ; $virtualqtype->get_tolerance_interval($answer); } if ($answer->min === '') { // This should mean that something is wrong - $stranswers .= " -$formattedanswer->answer".'