2003-02-16 07:08:57 +00:00
|
|
|
<?PHP // $Id$
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////
|
2003-12-12 07:35:55 +00:00
|
|
|
/// format.php - Default format class for file imports/exports. //
|
2003-02-16 07:08:57 +00:00
|
|
|
/// //
|
|
|
|
/// Doesn't do everything on it's own -- it needs to be extended. //
|
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
|
2003-12-12 07:35:55 +00:00
|
|
|
// Included by import.php
|
2003-02-16 07:08:57 +00:00
|
|
|
|
|
|
|
class quiz_default_format {
|
|
|
|
|
|
|
|
var $displayerrors = true;
|
2003-05-02 15:31:24 +00:00
|
|
|
var $category = NULL;
|
|
|
|
var $questionids = array();
|
2003-02-16 07:08:57 +00:00
|
|
|
|
|
|
|
/// Importing functions
|
|
|
|
|
2003-05-03 04:31:49 +00:00
|
|
|
function importpreprocess($category) {
|
2003-04-10 13:12:40 +00:00
|
|
|
/// Does any pre-processing that may be desired
|
|
|
|
|
2003-05-02 15:31:24 +00:00
|
|
|
$this->category = $category; // Important
|
|
|
|
|
2003-04-10 13:12:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-05-03 04:31:49 +00:00
|
|
|
function importprocess($filename) {
|
2003-05-02 15:31:24 +00:00
|
|
|
/// Processes a given file. There's probably little need to change this
|
|
|
|
|
|
|
|
if (! $lines = $this->readdata($filename)) {
|
|
|
|
notify("File could not be read, or was empty");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $questions = $this->readquestions($lines)) { // Extract all the questions
|
|
|
|
notify("There are no questions in this file!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
notify("Importing ".count($questions)." questions");
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
|
|
|
|
foreach ($questions as $question) { // Process and store each question
|
|
|
|
$count++;
|
|
|
|
|
|
|
|
echo "<hr><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
|
|
|
|
|
|
|
|
$question->category = $this->category->id;
|
2003-08-23 15:26:10 +00:00
|
|
|
$question->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
|
|
|
|
$question->version = 1; // Original version of this question
|
2003-05-02 15:31:24 +00:00
|
|
|
|
|
|
|
if (!$question->id = insert_record("quiz_questions", $question)) {
|
|
|
|
error("Could not insert new question!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->questionids[] = $question->id;
|
|
|
|
|
|
|
|
// Now to save all the answers and type-specific options
|
|
|
|
|
|
|
|
$result = quiz_save_question_options($question);
|
|
|
|
|
|
|
|
if (!empty($result->error)) {
|
|
|
|
notify($result->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($result->notice)) {
|
|
|
|
notify($result->notice);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-16 07:08:57 +00:00
|
|
|
function readdata($filename) {
|
|
|
|
/// Returns complete file with an array, one item per line
|
|
|
|
|
|
|
|
if (is_readable($filename)) {
|
2003-12-09 02:00:48 +00:00
|
|
|
$filearray = file($filename);
|
|
|
|
|
|
|
|
/// Check for Macintosh OS line returns (ie file on one line), and fix
|
|
|
|
if (ereg("\r", $filearray[0]) AND !ereg("\n", $filearray[0])) {
|
|
|
|
return explode("\r", $filearray[0]);
|
|
|
|
} else {
|
|
|
|
return $filearray;
|
|
|
|
}
|
2003-02-16 07:08:57 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function readquestions($lines) {
|
|
|
|
/// Parses an array of lines into an array of questions,
|
|
|
|
/// where each item is a question object as defined by
|
2003-04-10 13:12:40 +00:00
|
|
|
/// readquestion(). Questions are defined as anything
|
2003-02-16 07:08:57 +00:00
|
|
|
/// between blank lines.
|
|
|
|
|
|
|
|
$questions = array();
|
|
|
|
$currentquestion = array();
|
|
|
|
|
|
|
|
foreach ($lines as $line) {
|
2003-05-07 05:46:01 +00:00
|
|
|
$line = trim($line);
|
|
|
|
if (empty($line)) {
|
|
|
|
if (!empty($currentquestion)) {
|
|
|
|
if ($question = $this->readquestion($currentquestion)) {
|
|
|
|
$questions[] = $question;
|
|
|
|
}
|
|
|
|
$currentquestion = array();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$currentquestion[] = $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($currentquestion)) { // There may be a final question
|
|
|
|
if ($question = $this->readquestion($currentquestion)) {
|
|
|
|
$questions[] = $question;
|
|
|
|
}
|
2003-02-16 07:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $questions;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2003-05-02 15:31:24 +00:00
|
|
|
echo "<p>This quiz format has not yet been completed!</p>";
|
2003-02-16 07:08:57 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-03 04:31:49 +00:00
|
|
|
function importpostprocess() {
|
2003-04-10 13:12:40 +00:00
|
|
|
/// Does any post-processing that may be desired
|
|
|
|
/// Argument is a simple array of question ids that
|
|
|
|
/// have just been added.
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-02-16 07:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|