4 ////////////////////////////////////////////////////////////////////
5 /// format.php - Default format class for file imports/exports. //
7 /// Doesn't do everything on it's own -- it needs to be extended. //
8 ////////////////////////////////////////////////////////////////////
10 // Included by import.php and export.php
12 class qformat_default
{
14 // var $displayerrors = true;
18 var $matchgrades = 'error';
20 var $questionids = array();
22 // functions to indicate import/export functionality
23 // override to return true if implemented
25 function provide_import() {
29 function provide_export() {
37 * @param object category the category object
39 function setCategory( $category ) {
40 $this->category
= $category;
44 * set the course class variable
45 * @param course object Moodle course variable
47 function setCourse( $course ) {
48 $this->course
= $course;
53 * @param string filename name of file to import/export
55 function setFilename( $filename ) {
56 $this->filename
= $filename;
61 * @param string matchgrades error or nearest for grades
63 function setMatchgrades( $matchgrades ) {
64 $this->matchgrades
= $matchgrades;
69 * @param bool catfromfile allow categories embedded in import file
71 function setCatfromfile( $catfromfile ) {
72 $this->catfromfile
= $catfromfile;
75 /// Importing functions
78 * Perform any required pre-processing
80 function importpreprocess() {
86 * This method should not normally be overidden
88 function importprocess() {
89 if (! $lines = $this->readdata($this->filename
)) {
90 notify( get_string('cannotread','quiz') );
94 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
95 notify( get_string('noquestionsinfile','quiz') );
99 notify( get_string('importingquestions','quiz',count($questions)) );
101 // get list of valid answer grades
102 $grades = get_grade_options();
103 $gradeoptionsfull = $grades->gradeoptionsfull
;
107 foreach ($questions as $question) { // Process and store each question
109 // check for category modifiers
110 if ($question->qtype
=='category') {
111 if ($this->catfromfile
) {
112 // find/create category object
113 $catpath = $question->category
;
114 $newcategory = create_category_path( $catpath, '/', $this->course
->id
);
115 if (!empty($newcategory)) {
116 $this->category
= $newcategory;
124 echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext
)."</p>";
126 // check for answer grades validity (must match fixed list of grades)
127 if (!empty($question->fraction
) and (is_array($question->fraction
))) {
128 $fractions = $question->fraction
;
129 $answersvalid = true; // in case they are!
130 foreach ($fractions as $key => $fraction) {
131 $newfraction = match_grade_options($gradeoptionsfull, $fraction, $this->matchgrades
);
132 if ($newfraction===false) {
133 $answersvalid = false;
136 $fractions[$key] = $newfraction;
139 if (!$answersvalid) {
140 notify( get_string('matcherror','quiz') );
144 $question->fraction
= $fractions;
148 $question->category
= $this->category
->id
;
149 $question->stamp
= make_unique_id_code(); // Set the unique code (not to be changed)
151 if (!$question->id
= insert_record("question", $question)) {
152 error( get_string('cannotinsert','quiz') );
155 $this->questionids
[] = $question->id
;
157 // Now to save all the answers and type-specific options
160 $result = $QTYPES[$question->qtype
]
161 ->save_question_options($question);
163 if (!empty($result->error
)) {
164 notify($result->error
);
168 if (!empty($result->notice
)) {
169 notify($result->notice
);
173 // Give the question a unique version stamp determined by question_hash()
174 set_field('question', 'version', question_hash($question), 'id', $question->id
);
180 function readdata($filename) {
181 /// Returns complete file with an array, one item per line
183 if (is_readable($filename)) {
184 $filearray = file($filename);
186 /// Check for Macintosh OS line returns (ie file on one line), and fix
187 if (ereg("\r", $filearray[0]) AND !ereg("\n", $filearray[0])) {
188 return explode("\r", $filearray[0]);
196 function readquestions($lines) {
197 /// Parses an array of lines into an array of questions,
198 /// where each item is a question object as defined by
199 /// readquestion(). Questions are defined as anything
200 /// between blank lines.
202 $questions = array();
203 $currentquestion = array();
205 foreach ($lines as $line) {
208 if (!empty($currentquestion)) {
209 if ($question = $this->readquestion($currentquestion)) {
210 $questions[] = $question;
212 $currentquestion = array();
215 $currentquestion[] = $line;
219 if (!empty($currentquestion)) { // There may be a final question
220 if ($question = $this->readquestion($currentquestion)) {
221 $questions[] = $question;
229 function defaultquestion() {
230 // returns an "empty" question
231 // Somewhere to specify question parameters that are not handled
232 // by import but are required db fields.
233 // This should not be overridden.
236 $question = new stdClass();
237 $question->shuffleanswers
= $CFG->quiz_shuffleanswers
;
238 $question->defaultgrade
= 1;
239 $question->image
= "";
240 $question->usecase
= 0;
241 $question->multiplier
= array();
242 $question->generalfeedback
= '';
243 $question->correctfeedback
= '';
244 $question->partiallycorrectfeedback
= '';
245 $question->incorrectfeedback
= '';
250 function readquestion($lines) {
251 /// Given an array of lines known to define a question in
252 /// this format, this function converts it into a question
253 /// object suitable for processing and insertion into Moodle.
255 $formatnotimplemented = get_string( 'formatnotimplemented','quiz' );
256 echo "<p>$formatnotimplemented</p>";
262 function importpostprocess() {
263 /// Does any post-processing that may be desired
264 /// Argument is a simple array of question ids that
265 /// have just been added.
270 function importimagefile( $path, $base64 ) {
271 /// imports an image file encoded in base64 format
272 /// This should not be overridden.
275 // all this to get the destination directory
277 $fullpath = "{$CFG->dataroot}/{$this->course->id}/$path";
278 $path_parts = pathinfo( $fullpath );
279 $destination = $path_parts['dirname'];
280 $file = clean_filename( $path_parts['basename'] );
282 // detect and fix any filename collision - get unique filename
283 $newfiles = resolve_filename_collisions( $destination, array($file) );
284 $newfile = $newfiles[0];
286 // convert and save file contents
287 if (!$content = base64_decode( $base64 )) {
290 $newfullpath = "$destination/$newfile";
291 if (!$fh = fopen( $newfullpath, 'w' )) {
294 if (!fwrite( $fh, $content )) {
299 // return the (possibly) new filename
307 function export_file_extension() {
308 /// return the files extension appropriate for this type
309 /// override if you don't want .txt
314 function exportpreprocess() {
315 /// Does any pre-processing that may be desired
320 function presave_process( $content ) {
321 /// enables any processing to be done on the content
322 /// just prior to the file being saved
323 /// default is to do nothing
328 function exportprocess() {
329 /// Exports a given category. There's probably little need to change this
333 // create a directory for the exports (if not already existing)
334 if (! $export_dir = make_upload_directory($this->question_get_export_dir())) {
335 error( get_string('cannotcreatepath','quiz',$export_dir) );
337 $path = $CFG->dataroot
.'/'.$this->question_get_export_dir();
339 // get the questions (from database) in this category
340 // only get q's with no parents (no cloze subquestions specifically)
341 $questions = get_questions_category( $this->category
, true );
343 notify( get_string('exportingquestions','quiz') );
344 if (!count($questions)) {
345 notify( get_string('noquestions','quiz') );
350 // results are first written into string (and then to a file)
351 // so create/initialize the string here
354 // iterate through questions
355 foreach($questions as $question) {
356 // do not export hidden questions
357 if (!empty($question->hidden
)) {
361 // do not export random questions
362 if ($question->qtype
==RANDOM
) {
366 // export the question displaying message
368 echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext
)."</p>";
369 $expout .= $this->writequestion( $question ) . "\n";
372 // final pre-process on exported data
373 $expout = $this->presave_process( $expout );
376 $filepath = $path."/".$this->filename
. $this->export_file_extension();
377 if (!$fh=fopen($filepath,"w")) {
378 error( get_string('cannotopen','quiz',$filepath) );
380 if (!fwrite($fh, $expout)) {
381 error( get_string('cannotwrite','quiz',$filepath) );
388 function exportpostprocess() {
389 /// Does any post-processing that may be desired
394 function writequestion($question) {
395 /// Turns a question object into textual output in the given format
396 /// must be overidden
398 // if not overidden, then this is an error.
399 $formatnotimplemented = get_string( 'formatnotimplemented','quiz' );
400 echo "<p>$formatnotimplemented</p>";
405 function question_get_export_dir() {
406 $dirname = get_string("exportfilename","quiz");
407 $path = $this->course
->id
.'/backupdata/'.$dirname; // backupdata is protected directory