From 63336df7023c8342cadd32863fa1a811c8a8ec54 Mon Sep 17 00:00:00 2001 From: pichetp Date: Tue, 15 May 2007 00:34:41 +0000 Subject: [PATCH] adding calculated question import and export functions for xml formats merged from head --- question/format/xml/format.php | 162 ++++++++++++++++++++++++++++++++- 1 file changed, 160 insertions(+), 2 deletions(-) diff --git a/question/format/xml/format.php b/question/format/xml/format.php index 92c3417ada4..f7b337d891f 100755 --- a/question/format/xml/format.php +++ b/question/format/xml/format.php @@ -5,7 +5,10 @@ // ////////////////////////////////////////////////////////////////////////// // Based on default.php, included by ../import.php - +/** + * @package questionbank + * @subpackage importexport + */ require_once( "$CFG->libdir/xmlize.php" ); class qformat_xml extends qformat_default { @@ -465,6 +468,94 @@ 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 "
"; + + return $qo; + } + /** * this is not a real question type. It's a dummy type used * to specify the import category @@ -532,6 +623,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 ); } @@ -543,7 +637,6 @@ class qformat_xml extends qformat_default { // stick the result in the $questions array if ($qo) { - $qo->generalfeedback = ''; $questions[] = $qo; } } @@ -595,6 +688,9 @@ class qformat_xml extends qformat_default { case ESSAY: $name = 'essay'; break; + case CALCULATED: + $name = 'calculated'; + break; default: $name = 'unknown'; } @@ -711,6 +807,7 @@ class qformat_xml extends qformat_default { * @return string xml segment */ function writequestion( $question ) { + global $CFG,$QTYPES; // initial string; $expout = ""; @@ -888,6 +985,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); + // echo "
 question defs";print_r($question_datasetdefs);echo "
"; + //If there are question_datasets + if( isset($question->options->datasets)&&count($question->options->datasets)){// there should be + $expout .= "\n"; + foreach ($question->options->datasets 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: // should not get here