View source
<?php
function unhtmlentities($string) {
$search = array(
"'<script[?>]*?>.*?</script>'si",
"'<[\\/\\!]*?[^<?>]*?>'si",
"'([\r\n])[\\s]+'",
"'&(quot|#34);'i",
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(\\d+);'e",
);
$replace = array(
"",
"",
"\\1",
"\"",
"&",
"<",
"?>",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\\1)",
);
return preg_replace($search, $replace, $string);
}
function qformat_webct_convert_formula($formula) {
$formula = str_replace(' ', '', $formula);
while (ereg('[0-9.](e|E|\\*10\\*\\*)\\([+-]?[0-9]+\\)', $formula, $regs)) {
$formula = str_replace($regs[0], ereg_replace('[)(]', '', $regs[0]), $formula);
}
while (ereg('(^[+-]?|[^eE][+-]|[^0-9eE+-])[0-9.]+\\*10\\*\\*[+-]?[0-9]+([^0-9.eE]|$)', $formula, $regs)) {
$formula = str_replace($regs[0], str_replace('*10**', 'e', $regs[0]), $formula);
}
while (ereg('(^|[^0-9.eE])10\\*\\*[+-]?[0-9]+([^0-9.eE]|$)', $formula, $regs)) {
$formula = str_replace($regs[0], str_replace('10**', '1e', $regs[0]), $formula);
}
while (2 == count($splits = explode('**', $formula, 2))) {
if (ereg('^(.*[^0-9.eE])?(([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][+-]?[0-9]+)?|\\{[^}]*\\})$', $splits[0], $regs)) {
$base = $regs[2];
$splits[0] = $regs[1];
}
else {
if (ereg('\\)$', $splits[0])) {
$deep = 1;
for ($i = 1; $deep; ++$i) {
if (!ereg('^(.*[^[:alnum:]_])?([[:alnum:]_]*([)(])([^)(]*[)(]){' . $i . '})$', $splits[0], $regs)) {
error("Parenthesis before ** is not properly started in {$splits[0]}**");
}
if ('(' == $regs[3]) {
--$deep;
}
else {
if (')' == $regs[3]) {
++$deep;
}
else {
error("Impossible character {$regs[3]} detected as parenthesis character");
}
}
}
$base = $regs[2];
$splits[0] = $regs[1];
}
else {
error("Bad base before **: {$splits[0]}**");
}
}
if (ereg('^([+-]?(\\{[^}]\\}|([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][+-]?[0-9]+)?))(.*)', $splits[1], $regs)) {
$exp = $regs[1];
$splits[1] = $regs[6];
}
else {
if (ereg('^[+-]?[[:alnum:]_]*\\(', $splits[1])) {
$deep = 1;
for ($i = 1; $deep; ++$i) {
if (!ereg('^([+-]?[[:alnum:]_]*([)(][^)(]*){' . $i . '}([)(]))(.*)', $splits[1], $regs)) {
error("Parenthesis after ** is not properly closed in **{$splits[1]}");
}
if (')' == $regs[3]) {
--$deep;
}
else {
if ('(' == $regs[3]) {
++$deep;
}
else {
error("Impossible character {$regs[3]} detected as parenthesis character");
}
}
}
$exp = $regs[1];
$splits[1] = $regs[4];
}
}
$formula = "{$splits[0]}pow({$base},{$exp}){$splits[1]}";
}
return $formula;
}
class qformat_webct extends qformat_default {
function provide_import() {
return true;
}
function readquestions($lines) {
global $QTYPES;
$webctnumberregex = '[+-]?([0-9]+(\\.[0-9]*)?|\\.[0-9]+)((e|E|\\*10\\*\\*)([+-]?[0-9]+|\\([+-]?[0-9]+\\)))?';
$questions = array();
$errors = array();
$warnings = array();
$webct_options = array();
$ignore_rest_of_question = FALSE;
$nLineCounter = 0;
$nQuestionStartLine = 0;
$bIsHTMLText = FALSE;
$lines[] = ":EOF:";
foreach ($lines as $line) {
$nLineCounter++;
$line = iconv("Windows-1252", "UTF-8", $line);
if (isset($questiontext) and is_string($questiontext)) {
if (ereg("^:", $line)) {
$question->questiontext = addslashes(trim($questiontext));
unset($questiontext);
}
else {
$questiontext .= str_replace('\\:', ':', $line);
continue;
}
}
if (isset($answertext) and is_string($answertext)) {
if (ereg("^:", $line)) {
$answertext = addslashes(trim($answertext));
$question->answer[$currentchoice] = $answertext;
$question->subanswers[$currentchoice] = $answertext;
unset($answertext);
}
else {
$answertext .= str_replace('\\:', ':', $line);
continue;
}
}
if (isset($responsetext) and is_string($responsetext)) {
if (ereg("^:", $line)) {
$question->subquestions[$currentchoice] = addslashes(trim($responsetext));
unset($responsetext);
}
else {
$responsetext .= str_replace('\\:', ':', $line);
continue;
}
}
if (isset($feedbacktext) and is_string($feedbacktext)) {
if (ereg("^:", $line)) {
$question->feedback[$currentchoice] = addslashes(trim($feedbacktext));
unset($feedbacktext);
}
else {
$feedbacktext .= str_replace('\\:', ':', $line);
continue;
}
}
if (isset($generalfeedbacktext) and is_string($generalfeedbacktext)) {
if (ereg("^:", $line)) {
$question->tempgeneralfeedback = addslashes(trim($generalfeedbacktext));
unset($generalfeedbacktext);
}
else {
$generalfeedbacktext .= str_replace('\\:', ':', $line);
continue;
}
}
$line = trim($line);
if (eregi("^:(TYPE|EOF):", $line)) {
if (isset($question)) {
if (!isset($question->name)) {
$question->name = $question->questiontext;
}
if (strlen($question->name) > 255) {
$question->name = substr($question->name, 0, 250) . "...";
$warnings[] = get_string("questionnametoolong", "quiz", $nQuestionStartLine);
}
if (!isset($question->defaultgrade)) {
$question->defaultgrade = 1;
}
if (!isset($question->image)) {
$question->image = "";
}
$QuestionOK = TRUE;
if (strlen($question->questiontext) == 0) {
$warnings[] = get_string("missingquestion", "quiz", $nQuestionStartLine);
$QuestionOK = FALSE;
}
if (sizeof($question->answer) < 1) {
$errors[] = get_string("missinganswer", "quiz", $nQuestionStartLine);
$QuestionOK = FALSE;
}
else {
foreach ($question->answer as $key => $dataanswer) {
if (!isset($question->feedback[$key])) {
$question->feedback[$key] = '';
}
}
if (isset($question->tempgeneralfeedback)) {
if (isset($question->generalfeedback)) {
$question->generalfeedback = $question->tempgeneralfeedback;
}
else {
foreach ($question->answer as $key => $dataanswer) {
if ($question->tempgeneralfeedback != '') {
$question->feedback[$key] = $question->tempgeneralfeedback . '<br/>' . $question->feedback[$key];
}
}
}
unset($question->tempgeneralfeedback);
}
$maxfraction = -1;
$totalfraction = 0;
foreach ($question->fraction as $fraction) {
if ($fraction > 0) {
$totalfraction += $fraction;
}
if ($fraction > $maxfraction) {
$maxfraction = $fraction;
}
}
switch ($question->qtype) {
case SHORTANSWER:
if ($maxfraction != 1) {
$maxfraction = $maxfraction * 100;
$errors[] = "'{$question->name}': " . get_string("wronggrade", "quiz", $nLineCounter) . ' ' . get_string("fractionsnomax", "quiz", $maxfraction);
$QuestionOK = FALSE;
}
break;
case MULTICHOICE:
if ($question->single) {
if ($maxfraction != 1) {
$maxfraction = $maxfraction * 100;
$errors[] = "'{$question->name}': " . get_string("wronggrade", "quiz", $nLineCounter) . ' ' . get_string("fractionsnomax", "quiz", $maxfraction);
$QuestionOK = FALSE;
}
}
else {
$totalfraction = round($totalfraction, 2);
if ($totalfraction != 1) {
$totalfraction = $totalfraction * 100;
$errors[] = "'{$question->name}': " . get_string("wronggrade", "quiz", $nLineCounter) . ' ' . get_string("fractionsaddwrong", "quiz", $totalfraction);
$QuestionOK = FALSE;
}
}
break;
case CALCULATED:
foreach ($question->answers as $answer) {
if ($formulaerror = qtype_calculated_find_formula_errors($answer)) {
$warnings[] = "'{$question->name}': " . $formulaerror;
$QuestionOK = FALSE;
}
}
foreach ($question->dataset as $dataset) {
$dataset->itemcount = count($dataset->datasetitem);
}
$question->import_process = TRUE;
unset($question->answer);
break;
case MATCH:
foreach ($question->subquestions as $id => $subquestion) {
$temp = $question->subquestions[$id];
$question->subquestions[$id] = $question->subanswers[$id];
$question->subanswers[$id] = $temp;
}
if (count($question->answer) < 3) {
$question->name = 'Dummy question added ' . $question->name;
$question->answer[] = 'dummy';
$question->subanswers[] = 'dummy';
$question->subquestions[] = 'dummy';
$question->fraction[] = '0.0';
$question->feedback[] = '';
}
break;
default:
}
}
if ($QuestionOK) {
$questions[] = $question;
unset($question);
$question = $this
->defaultquestion();
}
}
$nQuestionStartLine = $nLineCounter;
}
if (eregi("^:TYPE:MC:1(.*)", $line, $webct_options)) {
$question = $this
->defaultquestion();
$question->feedback = array();
$question->qtype = MULTICHOICE;
$question->single = 1;
$ignore_rest_of_question = FALSE;
continue;
}
if (eregi("^:TYPE:MC:N(.*)", $line, $webct_options)) {
$question = $this
->defaultquestion();
$question->feedback = array();
$question->qtype = MULTICHOICE;
$question->single = 0;
$ignore_rest_of_question = FALSE;
continue;
}
if (eregi("^:TYPE:S", $line)) {
$question = $this
->defaultquestion();
$question->feedback = array();
$question->qtype = SHORTANSWER;
$question->usecase = 0;
$ignore_rest_of_question = FALSE;
continue;
}
if (eregi("^:TYPE:C", $line)) {
$question = $this
->defaultquestion();
$question->qtype = CALCULATED;
$question->answers = array();
$question->units = array();
$question->dataset = array();
$question->answer = array(
'dummy',
);
$question->fraction = array(
'1.0',
);
$question->feedback = array();
$currentchoice = -1;
$ignore_rest_of_question = FALSE;
continue;
}
if (eregi("^:TYPE:M", $line)) {
$question = $this
->defaultquestion();
$question->qtype = MATCH;
$question->feedback = array();
$ignore_rest_of_question = FALSE;
continue;
}
if (eregi("^:TYPE:P", $line)) {
$warnings[] = get_string("paragraphquestion", "quiz", $nLineCounter);
unset($question);
$ignore_rest_of_question = TRUE;
continue;
}
if (eregi("^:TYPE:", $line)) {
$warnings[] = get_string("unknowntype", "quiz", $nLineCounter);
unset($question);
$ignore_rest_of_question = TRUE;
continue;
}
if ($ignore_rest_of_question) {
continue;
}
if (eregi("^:TITLE:(.*)", $line, $webct_options)) {
$name = trim($webct_options[1]);
if (strlen($name) > 255) {
$name = substr($name, 0, 250) . "...";
$warnings[] = get_string("questionnametoolong", "quiz", $nLineCounter);
}
$question->name = addslashes($name);
continue;
}
if (eregi("^:IMAGE:(.*)", $line, $webct_options)) {
$filename = trim($webct_options[1]);
if (eregi("^http://", $filename)) {
$question->image = $filename;
}
continue;
}
if (!isset($question)) {
continue;
}
if (isset($question->qtype) && CALCULATED == $question->qtype && ereg("^:([[:lower:]].*|::.*)-(MIN|MAX|DEC|VAL([0-9]+))::?:?({$webctnumberregex})", $line, $webct_options)) {
$datasetname = ereg_replace('^::', '', $webct_options[1]);
$datasetvalue = qformat_webct_convert_formula($webct_options[4]);
switch ($webct_options[2]) {
case 'MIN':
$question->dataset[$datasetname]->min = $datasetvalue;
break;
case 'MAX':
$question->dataset[$datasetname]->max = $datasetvalue;
break;
case 'DEC':
$datasetvalue = floor($datasetvalue);
$question->dataset[$datasetname]->length = max(0, $datasetvalue);
break;
default:
$question->dataset[$datasetname]->datasetitem[$webct_options[3]] = new stdClass();
$question->dataset[$datasetname]->datasetitem[$webct_options[3]]->itemnumber = $webct_options[3];
$question->dataset[$datasetname]->datasetitem[$webct_options[3]]->value = $datasetvalue;
break;
}
continue;
}
$bIsHTMLText = eregi(":H\$", $line);
if (eregi("^:QUESTION", $line)) {
$questiontext = "";
continue;
}
if (eregi("^:ANSWER([0-9]+):([^:]+):([0-9\\.\\-]+):(.*)", $line, $webct_options)) {
$currentchoice = $webct_options[1];
$answertext = $webct_options[2];
$question->fraction[$currentchoice] = $webct_options[3] / 100;
continue;
}
if (eregi("^:ANSWER([0-9]+):([0-9\\.\\-]+)", $line, $webct_options)) {
$answertext = "";
$currentchoice = $webct_options[1];
$question->fraction[$currentchoice] = $webct_options[2] / 100;
continue;
}
if (eregi('^:FORMULA:(.*)', $line, $webct_options)) {
continue;
++$currentchoice;
$question->answers[$currentchoice] = qformat_webct_convert_formula($webct_options[1]);
$question->fraction[$currentchoice] = 1.0;
$question->tolerance[$currentchoice] = 0.0;
$question->tolerancetype[$currentchoice] = 2;
$question->feedback[$currentchoice] = '';
$question->correctanswerlength[$currentchoice] = 4;
$datasetnames = $QTYPES[CALCULATED]
->find_dataset_names($webct_options[1]);
foreach ($datasetnames as $datasetname) {
$question->dataset[$datasetname] = new stdClass();
$question->dataset[$datasetname]->datasetitem = array();
$question->dataset[$datasetname]->name = $datasetname;
$question->dataset[$datasetname]->distribution = 'uniform';
$question->dataset[$datasetname]->status = 'private';
}
continue;
}
if (eregi("^:L([0-9]+)", $line, $webct_options)) {
$answertext = "";
$currentchoice = $webct_options[1];
$question->fraction[$currentchoice] = 1;
continue;
}
if (eregi("^:R([0-9]+)", $line, $webct_options)) {
$responsetext = "";
$currentchoice = $webct_options[1];
continue;
}
if (eregi("^:REASON([0-9]+):?", $line, $webct_options)) {
$feedbacktext = "";
$currentchoice = $webct_options[1];
continue;
}
if (eregi("^:FEEDBACK([0-9]+):?", $line, $webct_options)) {
$generalfeedbacktext = "";
$currentchoice = $webct_options[1];
continue;
}
if (eregi('^:FEEDBACK:(.*)', $line, $webct_options)) {
$generalfeedbacktext = "";
continue;
}
if (eregi('^:LAYOUT:(.*)', $line, $webct_options)) {
continue;
}
if (isset($question->qtype) && CALCULATED == $question->qtype && eregi('^:ANS-DEC:([1-9][0-9]*)', $line, $webct_options)) {
$question->correctanswerlength[$currentchoice] = $webct_options[1];
continue;
}
if (isset($question->qtype) && CALCULATED == $question->qtype && eregi("^:TOL:({$webctnumberregex})", $line, $webct_options)) {
$question->tolerance[$currentchoice] = qformat_webct_convert_formula($webct_options[1]);
continue;
}
if (isset($question->qtype) && CALCULATED == $question->qtype && eregi('^:TOLTYPE:percent', $line)) {
$question->tolerance[$currentchoice] /= 100;
$question->tolerancetype[$currentchoice] = 1;
continue;
}
if (eregi('^:UNITS:(.+)', $line, $webct_options) and $webctunits = trim($webct_options[1])) {
$webctunits = explode(':', $webctunits);
$unitrec->multiplier = 1.0;
foreach ($webctunits as $webctunit) {
$unitrec->unit = trim($webctunit);
$question->units[] = $unitrec;
}
continue;
}
if (!empty($question->units) && eregi('^:UNITREQ:(.*)', $line, $webct_options) && !$webct_options[1]) {
$unitrec->unit = '';
$unitrec->multiplier = 1.0;
$question->units[] = $unitrec;
continue;
}
if (!empty($question->units) && eregi('^:UNITCASE:', $line)) {
continue;
}
if (isset($question->qtype) && CALCULATED == $question->qtype && eregi('^:ANSTYPE:dec', $line)) {
$question->correctanswerformat[$currentchoice] = '1';
continue;
}
if (isset($question->qtype) && CALCULATED == $question->qtype && eregi('^:ANSTYPE:sig', $line)) {
$question->correctanswerformat[$currentchoice] = '2';
continue;
}
}
return $questions;
}
}