From 460135234cf7d350f109882bed5fd4824e860562 Mon Sep 17 00:00:00 2001 From: thepurpleblob Date: Tue, 8 May 2007 14:24:13 +0000 Subject: [PATCH] MDL-9700: Added more comprehensive checks for missing fields not exported in previous versions. More could be done, but this covers the basics and works for 1.6 (and probably 1.5) exports. --- question/format.php | 7 +-- question/format/xml/format.php | 87 ++++++++++++++++++++++------------ 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/question/format.php b/question/format.php index 842de08811..a579cafe11 100644 --- a/question/format.php +++ b/question/format.php @@ -307,6 +307,7 @@ class qformat_default { $question->partiallycorrectfeedback = ''; $question->incorrectfeedback = ''; $question->answernumbering = 'abc'; + $question->penalty = 0.1; // this option in case the questiontypes class wants // to know where the data came from @@ -363,14 +364,14 @@ class qformat_default { // convert and save file contents if (!$content = base64_decode( $base64 )) { - return false; + return ''; } $newfullpath = "$destination/$newfile"; if (!$fh = fopen( $newfullpath, 'w' )) { - return false; + return ''; } if (!fwrite( $fh, $content )) { - return false; + return ''; } fclose( $fh ); diff --git a/question/format/xml/format.php b/question/format/xml/format.php index 07b84c69be..805c3ee591 100755 --- a/question/format/xml/format.php +++ b/question/format/xml/format.php @@ -97,39 +97,61 @@ class qformat_xml extends qformat_default { } } + /** + * return the value of a node, given a path to the node + * if it doesn't exist return the default value + * @param array xml data to read + * @param array path path to node expressed as array + * @param mixed default + * @param bool istext process as text + * @param string error if set value must exist, return false and issue message if not + * @return mixed value + */ + function getpath( $xml, $path, $default, $istext=false, $error='' ) { + foreach ($path as $index) { + if (empty($xml[$index])) { + if (!empty($error)) { + $this->error( $error ); + return false; + } else { + return $default; + } + } + else $xml = $xml[$index]; + } + if ($istext) { + $xml = addslashes( trim( $xml ) ); + } + + return $xml; + } + + /** * import parts of question common to all types * @param array question question array from xml tree * @return object question object */ function import_headers( $question ) { + // get some error strings + $error_noname = get_string( 'xmlimportnoname','quiz' ); + $error_noquestion = get_string( 'xmlimportnoquestion','quiz' ); + // this routine initialises the question object $qo = $this->defaultquestion(); - $name = $this->import_text( $question['#']['name'][0]['#']['text'] ); - $qtext = $this->import_text( $question['#']['questiontext'][0]['#']['text'] ); - $qformat = $question['#']['questiontext'][0]['@']['format']; - $image = $question['#']['image'][0]['#']; - if (!empty($question['#']['image_base64'][0]['#'])) { - $image_base64 = stripslashes( trim( $question['#']['image_base64'][0]['#'] ) ); - $image = $this->importimagefile( $image, $image_base64 ); - } - if (array_key_exists('generalfeedback', $question['#'])) { - $generalfeedback = $this->import_text( $question['#']['generalfeedback'][0]['#']['text'] ); - } else { - $generalfeedback = ''; - } - if (!empty($question['#']['defaultgrade'][0]['#'])) { - $qo->defaultgrade = $question['#']['defaultgrade'][0]['#']; - } - - $penalty = $question['#']['penalty'][0]['#']; - $qo->name = $name; - $qo->questiontext = $qtext; - $qo->questiontextformat = $this->trans_format( $qformat ); - $qo->image = ((!empty($image)) ? $image : ''); - $qo->generalfeedback = $generalfeedback; - $qo->penalty = $penalty; + // question name + $qo->name = $this->getpath( $question, array('#','name',0,'#','text',0,'#'), '', true, $error_noname ); + $qo->questiontext = $this->getpath( $question, array('#','questiontext',0,'#','text',0,'#'), '', true, $error_noquestion ); + $qo->questiontextformat = $this->getpath( $question, array('#','questiontext',0,'@','format'), '' ); + $image = $this->getpath( $question, array('#','image',0,'#'), $qo->image ); + $image_base64 = $this->getpath( $question, array('#','image_base64','0','#'),'' ); + if (!empty($image_base64)) { + $qo->image = $this->importimagefile( $image, stripslashes(image_base64) ); + } + $qo->generalfeedback = $this->getpath( $question, array('#','generalfeedback',0,'#','text',0,'#'), $qo->generalfeedback, true ); + $qo->defaultgrade = $this->getpath( $question, array('#','defaultgrade',0,'#'), $qo->defaultgrade ); + $qo->penalty = $this->getpath( $question, array('#','penalty',0,'#'), $qo->penalty ); return $qo; } @@ -271,7 +293,7 @@ class qformat_xml extends qformat_default { $qo->qtype = SHORTANSWER; // get usecase - $qo->usecase = $question['#']['usecase'][0]['#']; + $qo->usecase = $this->getpath($question, array('#','usecase',0,'#'), $qo->usecase ); // run through the answers $answers = $question['#']['answer']; @@ -609,7 +631,7 @@ class qformat_xml extends qformat_default { } else { $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type ); - $this->error( $notsupportted ); + $this->error( $notsupported ); $qo = null; } @@ -750,7 +772,7 @@ class qformat_xml extends qformat_default { function presave_process( $content ) { // override method to allow us to add xml headers and footers - $content = "\n" . + $content = "\n" . "\n" . $content . "\n" . ""; @@ -793,6 +815,12 @@ class qformat_xml extends qformat_default { // add comment $expout .= "\n\n\n"; + // check question type - make sure valid + $question_type = $this->get_qtype( $question->qtype ); + if ($question_type=='unknown') { + $expout .= "\n\n"; + } + // add opening tag // generates specific header for Cloze and category type question if ($question->qtype == 'category') { @@ -805,7 +833,6 @@ class qformat_xml extends qformat_default { } elseif ($question->qtype != MULTIANSWER) { // for all question types except Close - $question_type = $this->get_qtype( $question->qtype ); $name_text = $this->writetext( $question->name ); $qtformat = $this->get_format($question->questiontextformat); $question_text = $this->writetext( $question->questiontext ); @@ -826,7 +853,6 @@ class qformat_xml extends qformat_default { } else { // for Cloze type only - $question_type = $this->get_qtype( $question->qtype ); $name_text = $this->writetext( $question->name ); $question_text = $this->writetext( $question->questiontext ); $expout .= " \n"; @@ -1023,7 +1049,8 @@ class qformat_xml extends qformat_default { } break; default: - $expout .= "\n"; + // should not get here + error( 'Unsupported question type detected in strange circumstances!' ); } // close the question tag -- 2.39.5