From 47de80c701668556aae67e359f6a56ea8e83f622 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Tue, 3 Mar 2009 05:11:55 +0000 Subject: [PATCH] Aiken question import: MDL-18423 Greater robustness when parsing. --- question/format/aiken/format.php | 102 +++++++++++++++++++------------ 1 file changed, 62 insertions(+), 40 deletions(-) diff --git a/question/format/aiken/format.php b/question/format/aiken/format.php index f11b01ad52..cde6211dbe 100644 --- a/question/format/aiken/format.php +++ b/question/format/aiken/format.php @@ -1,76 +1,98 @@ defaultquestion(); $endchar = chr(13); foreach ($lines as $line) { - $stp = strpos($line,$endchar,0); - $newlines = explode($endchar,$line); + $stp = strpos($line, $endchar, 0); + $newlines = explode($endchar, $line); $foundQ = 0; $linescount = count($newlines); for ($i=0; $i < $linescount; $i++) { $nowline = trim($newlines[$i]); - ///Go through the array and build an object called $question - ///When done, add $question to $questions + // Go through the array and build an object called $question + // When done, add $question to $questions if (strlen($nowline) < 2) { continue; } - // This will show everyline when file is being processed - // print("$nowline
"); - $leader = substr($nowline,0,2); - if (preg_match('/[A-Z][).]/',$leader)){ - //trim off the label and space - $question->answer[] = htmlspecialchars(trim(substr($nowline,2)), ENT_NOQUOTES); + if (preg_match('/^[A-Z][).][ \t]/', $nowline)) { + // A choice. Trim off the label and space, then save + $question->answer[] = htmlspecialchars(trim(substr($nowline, 2)), ENT_NOQUOTES); $question->fraction[] = 0; $question->feedback[] = ''; continue; } - if ($leader == "AN"){ - $ans = trim(substr($nowline,strpos($nowline,':') + 1)); - $ans = substr($ans,0,1); - //A becomes 0 since array starts from 0 + if (preg_match('/^ANSWER:/', $nowline)) { + // The line that indicates the correct answer. This question is finised. + $ans = trim(substr($nowline, strpos($nowline, ':') + 1)); + $ans = substr($ans, 0, 1); + // We want to map A to 0, B to 1, etc. $rightans = ord($ans) - ord('A'); $question->fraction[$rightans] = 1; $questions[] = $question; - //clear array for next question set + + // Clear array for next question set $question = $this->defaultquestion(); continue; } else { - //Must be the first line since no leader + // Must be the first line of a new question, since no recognised prefix. $question->qtype = MULTICHOICE; - $question->name = htmlspecialchars(substr($nowline,0,50)); + $question->name = htmlspecialchars(substr($nowline, 0, 50)); $question->questiontext = htmlspecialchars($nowline); $question->single = 1; - $question->feedback[] = ""; + $question->feedback[] = ''; } } } -- 2.39.5