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
|
<?PHP // $Id: format.php,v 1.6 2004/08/09 13:56:47 thepurpleblob Exp $
////////////////////////////////////////////////////////////////////
/// format.php - Default format class for file imports/exports. //
/// //
/// Doesn't do everything on it's own -- it needs to be extended. //
////////////////////////////////////////////////////////////////////
// Included by import.php and export.php
class quiz_default_format {
var $displayerrors = true;
var $category = NULL;
var $course = NULL;
var $questionids = array();
/// Importing functions
function importpreprocess($category) {
/// Does any pre-processing that may be desired
$this->category = $category; // Important
return true;
}
function importprocess($filename) {
/// 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;
$question->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
$question->version = 1; // Original version of this question
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
global $QUIZ_QTYPES;
$result = $QUIZ_QTYPES[$question->qtype]
->save_question_options($question);
if (!empty($result->error)) {
notify($result->error);
return false;
}
if (!empty($result->notice)) {
notify($result->notice);
return true;
}
}
return true;
}
function readdata($filename) {
/// Returns complete file with an array, one item per line
if (is_readable($filename)) {
$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;
}
}
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
/// readquestion(). Questions are defined as anything
/// between blank lines.
$questions = array();
$currentquestion = array();
foreach ($lines as $line) {
$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;
}
}
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.
echo "<p>This quiz format has not yet been completed!</p>";
return NULL;
}
function importpostprocess() {
/// Does any post-processing that may be desired
/// Argument is a simple array of question ids that
/// have just been added.
return true;
}
// Export functions
function export_file_extension() {
/// return the files extension appropriate for this type
/// override if you don't want .txt
return ".txt";
}
function exportpreprocess($category, $course) {
/// Does any pre-processing that may be desired
$this->category = $category; // Important
$this->course = $course; // As is this!
return true;
}
function presave_process( $content ) {
/// enables any processing to be done on the content
/// just prior to the file being saved
/// default is to do nothing
return $content;
}
function exportprocess($filename) {
/// Exports a given category. There's probably little need to change this
global $CFG;
// create a directory for the exports (if not already existing)
$dirname = get_string("exportfilename","quiz");
$courseid = $this->course->id;
$path = $CFG->dataroot.'/'.$courseid.'/'.$dirname;
if (!is_dir($path)) {
if (!mkdir($path, $CFG->directorypermissions)) {
error("Cannot create path: $path");
}
}
// get the questions (from database) in this category
// $questions = get_records("quiz_questions","category",$this->category->id);
$questions = get_questions_category( $this->category );
notify("Exporting ".count($questions)." questions.");
$count = 0;
// results are first written into string (and then to a file)
// so create/initialize the string here
$expout = "";
// iterate through questions
foreach($questions as $question) {
$count++;
echo "<hr><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
$expout .= $this->writequestion( $question ) . "\n";
}
// final pre-process on exported data
$expout = $this->presave_process( $expout );
// write file
$filepath = $path."/".$filename . $this->export_file_extension();
if (!$fh=fopen($filepath,"w")) {
error("Cannot open for writing: $filepath");
}
if (!fwrite($fh, $expout)) {
error("Cannot write exported questions to $filepath");
}
fclose($fh);
return true;
}
function exportpostprocess() {
/// Does any post-processing that may be desired
return true;
}
function writequestion($question) {
/// Turns a question object into textual output in the given format
/// must be overidden
echo "<p>This quiz format has not yet been completed!</p>";
return NULL;
}
}
?>
|