1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
|
<?PHP // $Id: format.php,v 1.11 2004/08/23 13:48:09 moodler Exp $
///////////////////////////////////////////////////////////////////////////
// //
// WebCT FORMAT //
// //
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Part of Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 2004 ASP Consulting http://www.asp-consulting.net //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
// Based on format.php, included by ../../import.php
function unhtmlentities($string){
$search = array ("'<script[?>]*?>.*?</script>'si", // remove javascript
"'<[\/\!]*?[^<?>]*?>'si", // remove HTML tags
"'([\r\n])[\s]+'", // remove spaces
"'&(quot|#34);'i", // remove HTML entites
"'&(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"); // Evaluate like PHP
$replace = array ("",
"",
"\\1",
"\"",
"&",
"<",
"?>",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\\1)");
return preg_replace ($search, $replace, $string);
}
class quiz_format_webct_modified_calculated_qtype extends quiz_calculated_qtype {
// We need to make a modification of this qtype so that
// it will be able to save webct style calculated questions
// The difference between webct and Moodle is that webct
// pass the individual data items as part of the question
// while Moodle core treat them separately
function save_question_options($question, $options = false) {
if (false !== $options) {
// function is called from save_question...
return parent::save_question_options($question, $options);
}
// function is called from format.php...
$datasetdatas = $question->datasets;
// Set dataset
$form->dataset = array();
foreach ($datasetdatas as $datasetname => $notimportant) {
// Literal - question local - name
$form->dataset[] = "1-0-$datasetname";
}
$subtypeoptions->answers = $question->answers;
$subtypeoptions->units = $question->units;
unset($question->datasets);
unset($question->answers);
unset($question->units);
$this->save_question($question, $form, 'not used', $subtypeoptions);
// Save dataset options and items...
// Get datasetdefinitions
global $CFG;
$datasetdefs = get_records_sql(
"SELECT a.*
FROM {$CFG->prefix}quiz_dataset_definitions a,
{$CFG->prefix}quiz_question_datasets b
WHERE a.id = b.datasetdefinition
AND b.question = '$question->id' ");
foreach ($datasetdefs as $datasetdef) {
$datasetdata = $datasetdatas[$datasetdef->name];
// Set items and retrieve ->itemcout
$item->definition = $datasetdef->id;
for ($item->number=1 ; isset($datasetdata->items["$item->number"]) ; ++$item->number) {
$item->value = $datasetdata->items["$item->number"];
if (!insert_record('quiz_dataset_items', $item)) {
error("Unable to insert dataset item $item->number with $item->value for $datasetdef->name");
}
}
$datasetdef->itemcount = $item->number - 1;
// Retrieve ->options
if (is_numeric($datasetdata->min) && is_numeric($datasetdata->max)
&& $datasetdata->min <= $datasetdata->max) {
if (is_numeric($datasetdata->dec)) {
$dec = max(0, ceil($datasetdata->dec));
} else {
$dec = 1; // A try
}
$datasetdef->options = "uniform:$datasetdata->min:$datasetdata->max:$dec";
} else {
$datasetdef->options = '';
}
// Save definition
if ($datasetdef->itemcount || $datasetdef->options) {
if (!update_record('quiz_dataset_definitions', $datasetdef)) {
error("Unable to update dataset definition $datasetdef->name on question $question->id");
}
}
}
// Done
return true;
}
}
$QUIZ_QTYPES[CALCULATED] = new quiz_format_webct_modified_calculated_qtype();
function quiz_format_webct_convert_formula($formula) {
// Remove empty space, as it would cause problems otherwise:
$formula = str_replace(' ', '', $formula);
// Remove paranthesis after e,E and *10**:
while (ereg('[0-9.](e|E|\\*10\\*\\*)\\([+-]?[0-9]+\\)', $formula, $regs)) {
$formula = str_replace(
$regs[0], ereg_replace('[)(]', '', $regs[0]), $formula);
}
// Replace *10** with e where possible
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);
}
// Replace other 10** with 1e where possible
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);
}
// Replace all other base**exp with the PHP equivalent function pow(base,exp)
// (Pretty tricky to exchange an operator with a function)
while (2 == count($splits = explode('**', $formula, 2))) {
// Find $base
if (ereg('^(.*[^0-9.eE])?(([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][+-]?[0-9]+)?|\\{[^}]*\\})$',
$splits[0], $regs)) {
// The simple cases
$base = $regs[2];
$splits[0] = $regs[1];
} else if (ereg('\\)$', $splits[0])) {
// Find the start of this parenthesis
$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]**");
}
// Find $exp (similar to above but a little easier)
if (ereg('^([+-]?(\\{[^}]\\}|([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][+-]?[0-9]+)?))(.*)',
$splits[1], $regs)) {
// The simple case
$exp = $regs[1];
$splits[1] = $regs[6];
} else if (ereg('^[+-]?[[:alnum:]_]*\\(', $splits[1])) {
// Find the end of the parenthesis
$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];
}
// Replace it!
$formula = "$splits[0]pow($base,$exp)$splits[1]";
}
// Nothing more is known to need to be converted
return $formula;
}
class quiz_file_format extends quiz_default_format {
function readquestions ($lines) {
$qtypecalculated = new quiz_format_webct_modified_calculated_qtype();
$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:"; // for an easiest processing of the last line
foreach ($lines as $line) {
$nLineCounter++;
// Processing multiples lines strings
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;
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;
}
}
$line = trim($line);
if (eregi("^:(TYPE|EOF):",$line)) {
// New Question or End of File
if (isset($question)) { // if previous question exists, complete, check and save it
// Setup default value of missing fields
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 = "";
}
// Perform sanity checks
$QuestionOK = TRUE;
if (strlen($question->questiontext) == 0) {
$warnings[] = get_string("missingquestion", "quiz", $nQuestionStartLine);
$QuestionOK = FALSE;
}
if (sizeof($question->answer) < 1) { // a question must have at least 1 answer
$errors[] = get_string("missinganswer", "quiz", $nQuestionStartLine);
$QuestionOK = FALSE;
}
else {
// Perform string length check
foreach ($question->answer as $key => $dataanswer) {
if (strlen($dataanswer) > 255) {
$question->answer[$key] = substr($dataanswer,0,250)."...";
$warnings[] = get_string("answertoolong", "quiz", $nQuestionStartLine);
}
}
$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) {
echo "<p>$totalfraction</p>";
$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 = quiz_qtype_calculated_find_formula_errors($answer->answer)) {
$warnings[] = "'$question->name': ". $formulaerror;
$QuestionOK = FALSE;
}
}
break;
default:
// No problemo
}
}
if ($QuestionOK) {
$questions[] = $question; // store it
unset($question); // and prepare a new one
}
}
$nQuestionStartLine = $nLineCounter;
}
// Processing Question Header
if (eregi("^:TYPE:MC:1(.*)",$line,$webct_options)) {
// Multiple Choice Question with only one good answer
$question->qtype = MULTICHOICE;
$question->single = 1; // Only one answer is allowed
$ignore_rest_of_question = FALSE;
continue;
}
if (eregi("^:TYPE:MC:N(.*)",$line,$webct_options)) {
// Multiple Choice Question with several good answers
$question->qtype = MULTICHOICE;
$question->single = 0; // Many answers allowed
$ignore_rest_of_question = FALSE;
continue;
}
if (eregi("^:TYPE:S",$line)) {
// Short Answer Question
$question->qtype = SHORTANSWER;
$question->usecase = 0; // Ignore case
$ignore_rest_of_question = FALSE;
continue;
}
if (eregi("^:TYPE:C",$line)) {
// Calculated Question
$question->qtype = CALCULATED;
$question->answers = array(); // No problem as they go as :FORMULA: from webct
$question->units = array();
$question->datasets = array();
// To make us pass the end-of-question sanity checks
$question->answer = array('dummy');
$question->fraction = array('1.0');
$currentchoice = -1;
$ignore_rest_of_question = FALSE;
continue;
}
if (eregi("^:TYPE:M",$line)) {
// Match Question
$question->qtype = MATCH;
$ignore_rest_of_question = TRUE; // match question processing is not debugged
continue;
}
if (eregi("^:TYPE:P",$line)) {
// Paragraph Question
$warnings[] = get_string("paragraphquestion", "quiz", $nLineCounter);
$ignore_rest_of_question = TRUE; // Question Type not handled by Moodle
continue;
}
if (eregi("^:TYPE:",$line)) {
// Unknow Question
$warnings[] = get_string("unknowntype", "quiz", $nLineCounter);
$ignore_rest_of_question = TRUE; // Question Type not handled by Moodle
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;
}
// Need to put the parsing of calculated items here to avoid ambitiuosness:
if (CALCULATED == $question->qtype && ereg(
"^:([[:lower:]].*|::.*)-(MIN|MAX|DEC|VAL([0-9]+))::?:?($webctnumberregex)", $line, $webct_options)) {
$datasetname = ereg_replace('^::', '', $webct_options[1]);
$datasetvalue = quiz_format_webct_convert_formula($webct_options[4]);
switch ($webct_options[2]) {
case 'MIN':
$question->datasets[$datasetname]->min = $datasetvalue;
break;
case 'MAX':
$question->datasets[$datasetname]->max = $datasetvalue;
break;
case 'DEC':
$datasetvalue = floor($datasetvalue); // int only!
$question->datasets[$datasetname]->dec = max(0, $datasetvalue);
break;
default:
// The VAL case:
$question->datasets[$datasetname]->items[$webct_options[3]] = $datasetvalue;
break;
}
continue;
}
$bIsHTMLText = eregi(":H$",$line); // True if next lines are coded in HTML
if (eregi("^:QUESTION",$line)) {
$questiontext=""; // Start gathering next lines
continue;
}
if (eregi("^:ANSWER([0-9]+):([^:]+):([0-9\.]+):(.*)",$line,$webct_options)) { /// SHORTANSWER
$currentchoice=$webct_options[1];
$answertext=$webct_options[2]; // Start gathering next lines
$question->fraction[$currentchoice]=($webct_options[3]/100);
continue;
}
if (eregi("^:ANSWER([0-9]+):([0-9\.]+)",$line,$webct_options)) {
$answertext=""; // Start gathering next lines
$currentchoice=$webct_options[1];
$question->fraction[$currentchoice]=($webct_options[2]/100);
continue;
}
if (eregi('^:FORMULA:(.*)', $line, $webct_options)) {
// Answer for a CALCULATED question
++$currentchoice;
$question->answers[$currentchoice]->answer =
quiz_format_webct_convert_formula($webct_options[1]);
// Default settings:
$question->answers[$currentchoice]->fraction = 1.0;
$question->answers[$currentchoice]->tolerance = 0.0;
$question->answers[$currentchoice]->tolerancetype = 2; // nominal (units in webct)
$question->answers[$currentchoice]->feedback = '';
$question->answers[$currentchoice]->correctanswerlength = 4;
$datasetnames = $qtypecalculated->find_dataset_names($webct_options[1]);
foreach ($datasetnames as $datasetname) {
$question->datasets[$datasetname]->items = array();
}
continue;
}
if (eregi("^:L([0-9]+)",$line,$webct_options)) {
$answertext=""; // Start gathering next lines
$currentchoice=$webct_options[1];
continue;
}
if (eregi("^:R[0-9]+)",$line,$webct_options)) {
$responsetext=""; // Start gathering next lines
$currentchoice=$webct_options[1];
continue;
}
if (eregi("^:REASON([0-9]+):?",$line,$webct_options)) {
$feedbacktext=""; // Start gathering next lines
$currentchoice=$webct_options[1];
continue;
}
if (CALCULATED == $question->qtype && eregi('^:ANS-DEC:([1-9][0-9]*)', $line, $webct_options)) {
// We can but hope that this always appear before the ANSTYPE property
$question->answers[$currentchoice]->correctanswerlength = $webct_options[1];
continue;
}
if (CALCULATED == $question->qtype && eregi("^:TOL:($webctnumberregex)", $line, $webct_options)) {
// We can but hope that this always appear before the TOL property
$question->answers[$currentchoice]->tolerance =
quiz_format_webct_convert_formula($webct_options[1]);
continue;
}
if (CALCULATED == $question->qtype && eregi('^:TOLTYPE:percent', $line)) {
// Percentage case is handled as relative in Moodle:
$question->answers[$currentchoice]->tolerance /= 100;
$question->answers[$currentchoice]->tolerancetype = 1; // Relative
continue;
}
if (eregi('^:UNITS:(.+)', $line, $webct_options)
and $webctunits = trim($webct_options[1])) {
// This is a guess - I really do not know how different webct units are separated...
$webctunits = explode(':', $webctunits);
$unitrec->multiplier = 1.0; // Webct does not seem to support this
foreach ($webctunits as $webctunit) {
$unitrec->unit = trim($webctunit);
$question->units[] = $unitrec;
}
continue;
}
if (!empty($question->units) && eregi('^:UNITREQ:(.*)', $line, $webct_options)
&& !$webct_options[1]) {
// There are units but units are not required so add the no unit alternative
// We can but hope that the UNITS property always appear before this property
$unitrec->unit = '';
$unitrec->multiplier = 1.0;
$question->units[] = $unitrec;
continue;
}
if (!empty($question->units) && eregi('^:UNITCASE:', $line)) {
// This could be important but I was not able to figure out how
// it works so I ignore it for now
continue;
}
if (CALCULATED == $question->qtype && eregi('^:ANSTYPE:dec', $line)) {
// Houston - we have a problem
// Moodle does not support this - we try something defensively by
// setting the correct answer length to 5, it shoud be enough for
// most cases
$question->answers[$currentchoice]->correctanswerlength = 5;
continue;
}
}
if (sizeof($errors) > 0) {
echo "<p>".get_string("errorsdetected", "quiz", sizeof($errors))."</p><ul>";
foreach($errors as $error) {
echo "<li>$error</li>";
}
echo "</ul>";
unset($questions); // no questions imported
}
if (sizeof($warnings) > 0) {
echo "<p>".get_string("warningsdetected", "quiz", sizeof($warnings))."</p><ul>";
foreach($warnings as $warning) {
echo "<li>$warning</li>";
}
echo "</ul>";
}
return $questions;
}
}
?>
|