From 725ba2a0621f901e80a3eed10bd33488d5ac3756 Mon Sep 17 00:00:00 2001 From: pichetp Date: Wed, 28 Mar 2007 05:00:36 +0000 Subject: [PATCH] first version of calculated question import, MDL-8673, somme echos left to diagnostics possible bugs --- question/format/xml/format.php | 158 +++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) diff --git a/question/format/xml/format.php b/question/format/xml/format.php index 2b0b662802..5b98c489b2 100755 --- a/question/format/xml/format.php +++ b/question/format/xml/format.php @@ -446,6 +446,96 @@ class qformat_xml extends qformat_default { return $qo; } + function import_calculated( $question ) { + // import numerical question + + // get common parts + $qo = $this->import_headers( $question ); + + // header parts particular to numerical + $qo->qtype = CALCULATED ;//CALCULATED; + + // get answers array + // echo "
 question";print_r($question);echo "
"; + $answers = $question['#']['answer']; + $qo->answers = array(); + $qo->feedback = array(); + $qo->fraction = array(); + $qo->tolerance = array(); + $qo->tolerancetype = array(); + $qo->correctanswerformat = array(); + $qo->correctanswerlength = array(); + $qo->feedback = array(); + foreach ($answers as $answer) { + // answer outside of is deprecated + if (!empty( $answer['#']['text'] )) { + $answertext = $this->import_text( $answer['#']['text'] ); + } + else { + $answertext = trim($answer['#'][0]); + } + if ($answertext == '') { + $qo->answers[] = '*'; + } else { + $qo->answers[] = $answertext; + } + $qo->feedback[] = $this->import_text( $answer['#']['feedback'][0]['#']['text'] ); + $qo->tolerance[] = $answer['#']['tolerance'][0]['#']; + // fraction as a tag is deprecated + if (!empty($answer['#']['fraction'][0]['#'])) { + $qo->fraction[] = $answer['#']['fraction'][0]['#']; + } + else { + $qo->fraction[] = $answer['@']['fraction'] / 100; + } + $qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#']; + $qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#']; + $qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#']; + } + // get units array + $qo->unit = array(); + if (isset($question['#']['units'][0]['#']['unit'])) { + $units = $question['#']['units'][0]['#']['unit']; + $qo->multiplier = array(); + foreach ($units as $unit) { + $qo->multiplier[] = $unit['#']['multiplier'][0]['#']; + $qo->unit[] = $unit['#']['unit_name'][0]['#']; + } + } + $datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition']; + $qo->dataset = array(); + $qo->datasetindex= 0 ; + foreach ($datasets as $dataset) { + $qo->datasetindex++; + $qo->dataset[$qo->datasetindex] = new stdClass(); + $qo->dataset[$qo->datasetindex]->status = $this->import_text( $dataset['#']['status'][0]['#']['text']); + $qo->dataset[$qo->datasetindex]->name = $this->import_text( $dataset['#']['name'][0]['#']['text']); + $qo->dataset[$qo->datasetindex]->type = $dataset['#']['type'][0]['#']; + $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']); + $qo->dataset[$qo->datasetindex]->max = $this->import_text( $dataset['#']['maximum'][0]['#']['text']); + $qo->dataset[$qo->datasetindex]->min = $this->import_text( $dataset['#']['minimum'][0]['#']['text']); + $qo->dataset[$qo->datasetindex]->length = $this->import_text( $dataset['#']['decimals'][0]['#']['text']); + $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']); + $qo->dataset[$qo->datasetindex]->itemcount = $dataset['#']['itemcount'][0]['#']; + $qo->dataset[$qo->datasetindex]->datasetitem = array(); + $qo->dataset[$qo->datasetindex]->itemindex = 0; + $qo->dataset[$qo->datasetindex]->number_of_items=$dataset['#']['number_of_items'][0]['#']; + $datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item']; + foreach ($datasetitems as $datasetitem) { + $qo->dataset[$qo->datasetindex]->itemindex++; + $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex] = new stdClass(); + $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->itemnumber = $datasetitem['#']['number'][0]['#']; //[0]['#']['number'][0]['#'] ; // [0]['numberitems'] ;//['#']['number'][0]['#'];// $datasetitems['#']['number'][0]['#']; + $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->value = $datasetitem['#']['value'][0]['#'] ;//$datasetitem['#']['value'][0]['#']; + } + } + + echo "
loaded qo";print_r($qo);echo "
"; + + $qo->import_process = true ; // flag for question_save_options() + + return $qo; + } + /** * this is not a real question type. It's a dummy type used * to specify the import category @@ -513,6 +603,9 @@ class qformat_xml extends qformat_default { elseif ($question_type=='essay') { $qo = $this->import_essay( $question ); } + elseif ($question_type=='calculated') { + $qo = $this->import_calculated( $question ); + } elseif ($question_type=='category') { $qo = $this->import_category( $question ); } @@ -576,6 +669,9 @@ class qformat_xml extends qformat_default { case ESSAY: $name = 'essay'; break; + case CALCULATED: + $name = 'calculated'; + break; default: $name = 'unknown'; } @@ -692,6 +788,7 @@ class qformat_xml extends qformat_default { * @return string xml segment */ function writequestion( $question ) { + global $CFG,$QTYPES; // initial string; $expout = ""; @@ -865,6 +962,67 @@ class qformat_xml extends qformat_default { $expout .= "\n"; } + break; + case CALCULATED: + foreach ($question->options->answers as $answer) { + $tolerance = $answer->tolerance; + $tolerancetype = $answer->tolerancetype; + $correctanswerlength= $answer->correctanswerlength ; + $correctanswerformat= $answer->correctanswerformat; + $percent = 100 * $answer->fraction; + $expout .= "\n"; + // "" tags are an added feature, old files won't have them + $expout .= " {$answer->answer}\n"; + $expout .= " $tolerance\n"; + $expout .= " $tolerancetype\n"; + $expout .= " $correctanswerformat\n"; + $expout .= " $correctanswerformat\n"; + $expout .= " ".$this->writetext( $answer->feedback )."\n"; + $expout .= "\n"; + } + $units = $question->options->units; + if (count($units)) { + $expout .= "\n"; + foreach ($units as $unit) { + $expout .= " \n"; + $expout .= " {$unit->multiplier}\n"; + $expout .= " {$unit->unit}\n"; + $expout .= " \n"; + } + $expout .= "\n"; + } + //echo "
 question calc";print_r($question);echo "
"; + //First, we a new function to get all the data itmes in the database + $question_datasetdefs =$QTYPES['calculated']->get_datasets_for_export ($question->id); + // echo "
 question defs";print_r($question_datasetdefs);echo "
"; + //If there are question_datasets + if (count($question_datasetdefs)) {// there should be + $expout .= "\n"; + foreach ($question_datasetdefs as $def) { + $expout .= "\n"; + $expout .= " ".$this->writetext($def->status)."\n"; + $expout .= " ".$this->writetext($def->name)."\n"; + $expout .= " calculated\n"; + $expout .= " ".$this->writetext($def->distribution)."\n"; + $expout .= " ".$this->writetext($def->minimum)."\n"; + $expout .= " ".$this->writetext($def->maximum)."\n"; + $expout .= " ".$this->writetext($def->decimals)."\n"; + $expout .= " $def->itemcount\n"; + if ($def->itemcount > 0 ) { + $expout .= " \n"; + foreach ($def->items as $item ){ + $expout .= " \n"; + $expout .= " ".$item->itemnumber."\n"; + $expout .= " ".$item->value."\n"; + $expout .= " \n"; + } + $expout .= " \n"; + $expout .= " ".$def-> number_of_items."\n"; + } + $expout .= "\n"; + } + $expout .= "\n"; + } break; default: $expout .= "\n"; -- 2.39.5