moodle/mod/quiz/format/gift/format.php

1 line
15 KiB
PHP
Raw Normal View History

2004-01-10 09:39:28 +00:00
<?php // $Id$ // /////////////////////////////////////////////////////////////// // GIFT // // The GIFT import filter is an easy to use method for teachers // writing questions as a text file. It supports true-false, // short answer, multiple-choice and numerical questions, as well // as insertion of a blank line for the missing word format. // // Multiple Choice / Missing Word // Who's buried in Grant's tomb?{~Grant ~Jefferson =no one} // Grant is {~buried =entombed ~living} in Grant's tomb. // True-False: // Grant is buried in Grant's tomb.{FALSE} // Short-Answer. // Who's buried in Grant's tomb?{=no one =nobody} // Numerical // When was Ulysses S. Grant born?{#1822:5} // // Comment lines start with a double backslash (//). // Optional question names are enclosed in double colon(::). // Answer feedback is indicated with hash mark (#). // Percentage answer weights immediately follow the tilde (for // multiple choice) or equal sign (for short answer and numerical), // and are enclosed in percent signs (% %). Below are more // complicated examples with various options and formatting styles. // // ::Grant's Tomb::Grant is { // ~buried#No one is buried there. // =entombed#Right answer! // ~living#We hope not! // } in Grant's tomb. // // Difficult multiple choice question.{ // ~wrong answer #comment on wrong answer // ~%50%half credit answer #comment on answer // =full credit answer #well done!} // // ::Jesus' hometown (Short answer ex.):: Jesus Christ was from { // =Nazareth#Yes! That's right! // =%75%Nazereth#Right, but misspelled. // =%25%Bethlehem#He was born here, but not raised here. // }. // // //this inline comment will be ignored by the filter // ::Numerical example:: // When was Ulysses S. Grant born? {# // =1822:0 #Correct! 100% credit // =%50%1982:2 #He was born in 1822. // You get 50% credit for being close. // } // // This filter was written through the collaboration of numerous // members of the Moodle community. It was originally based on // the missingword format, which included code from Thomas Robb // and others. Paul Tsuchido Shew wrote this filter in December 2003. ////////////////////////////////////////////////////////////////////////// // Based on default.php, included by ../import.php class quiz_file_format extends quiz_default_format { function answerweightparser(&$answer) { $answer = substr($answer, 1); // removes initial % $end_position = strpos($answer, "%"); $answer_weight = substr($answer, 0, $end_position); // gets weight as integer $answer_weight = $answer_weight/100; // converts to percent $answer = substr($answer, $end_position+1); // removes comment from answer return $answer_weight; } function commentparser(&$answer) { if (strpos($answer,"#") > 0){ $hashpos = strpos($answer,"#"); $comment = addslashes(substr($answer, $hashpos+1)); $answer = substr($answer, 0, $hashpos); } else { $comment = " "; } return $comment; } function readquestion($lines) { // Given an array of lines known to define a question in this format, this function // converts it into a question object suitable for processing and insertion into Moodle. $question = NULL; $comment = NULL; define("GIFT_ANSWERWEIGHT_REGEX", "^%\-*([0-9]{1,2})\.?([0-9]*)%"); // REMOVED COMMENTED LINES and IMPLODE foreach ($lines as $key => $line) { $line = trim($line); if (substr($line, 0, 2) == "//") { // echo "Commented line removed.<br />"; $lines[$key] = " "; } } $text = trim(implode(" ", $lines)); if ($text == "") { // echo "<p>Empty line.</p>"; return false; }