forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.php
550 lines (468 loc) · 16.6 KB
/
format.php
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
<?php // $Id$
/**
* Base class for question import and export formats.
*
* @author Martin Dougiamas, Howard Miller, and many others.
* {@link http://moodle.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package questionbank
* @subpackage importexport
*//** */
/**
* Base class for question import and export formats.
*
* Doesn't do everything on it's own -- it needs to be extended.
*
* @package questionbank
* @subpackage importexport
*/
class qformat_default {
var $displayerrors = true;
var $category = NULL;
var $course = NULL;
var $filename = '';
var $matchgrades = 'error';
var $catfromfile = 0;
var $cattofile = 0;
var $questionids = array();
var $importerrors = 0;
var $stoponerror = true;
// functions to indicate import/export functionality
// override to return true if implemented
function provide_import() {
return false;
}
function provide_export() {
return false;
}
// Accessor methods
/**
* set the category
* @param object category the category object
*/
function setCategory( $category ) {
$this->category = $category;
}
/**
* set the course class variable
* @param course object Moodle course variable
*/
function setCourse( $course ) {
$this->course = $course;
}
/**
* set the filename
* @param string filename name of file to import/export
*/
function setFilename( $filename ) {
$this->filename = $filename;
}
/**
* set matchgrades
* @param string matchgrades error or nearest for grades
*/
function setMatchgrades( $matchgrades ) {
$this->matchgrades = $matchgrades;
}
/**
* set catfromfile
* @param bool catfromfile allow categories embedded in import file
*/
function setCatfromfile( $catfromfile ) {
$this->catfromfile = $catfromfile;
}
/**
* set cattofile
* @param bool cattofile exports categories within export file
*/
function setCattofile( $cattofile ) {
$this->cattofile = $cattofile;
}
/**
* set stoponerror
* @param bool stoponerror stops database write if any errors reported
*/
function setStoponerror( $stoponerror ) {
$this->stoponerror = $stoponerror;
}
/***********************
* IMPORTING FUNCTIONS
***********************/
/**
* Handle parsing error
*/
function error( $message, $text='', $questionname='' ) {
echo "<div class=\"importerror\">\n";
echo "<strong>Error in question $questionname</strong>";
if (!empty($text)) {
$text = s($text);
echo "<blockquote>$text</blockquote>\n";
}
echo "<strong>$message</strong>\n";
echo "</div>";
$this->importerrors++;
}
/**
* Perform any required pre-processing
* @return boolean success
*/
function importpreprocess() {
return true;
}
/**
* Process the file
* This method should not normally be overidden
* @return boolean success
*/
function importprocess() {
// STAGE 1: Parse the file
notify( get_string('parsingquestions','quiz') );
if (! $lines = $this->readdata($this->filename)) {
notify( get_string('cannotread','quiz') );
return false;
}
if (! $questions = $this->readquestions($lines)) { // Extract all the questions
notify( get_string('noquestionsinfile','quiz') );
return false;
}
// STAGE 2: Write data to database
notify( get_string('importingquestions','quiz',count($questions)) );
// check for errors before we continue
if ($this->stoponerror and ($this->importerrors>0)) {
return false;
}
// get list of valid answer grades
$grades = get_grade_options();
$gradeoptionsfull = $grades->gradeoptionsfull;
$count = 0;
foreach ($questions as $question) { // Process and store each question
// check for category modifiers
if ($question->qtype=='category') {
if ($this->catfromfile) {
// find/create category object
$catpath = $question->category;
$newcategory = create_category_path( $catpath, '/', $this->course->id );
if (!empty($newcategory)) {
$this->category = $newcategory;
}
}
continue;
}
$count++;
echo "<hr /><p><b>$count</b>. ".$this->format_question_text($question)."</p>";
// check for answer grades validity (must match fixed list of grades)
if (!empty($question->fraction) and (is_array($question->fraction))) {
$fractions = $question->fraction;
$answersvalid = true; // in case they are!
foreach ($fractions as $key => $fraction) {
$newfraction = match_grade_options($gradeoptionsfull, $fraction, $this->matchgrades);
if ($newfraction===false) {
$answersvalid = false;
}
else {
$fractions[$key] = $newfraction;
}
}
if (!$answersvalid) {
notify( get_string('matcherror','quiz') );
continue;
}
else {
$question->fraction = $fractions;
}
}
$question->category = $this->category->id;
$question->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
if (!$question->id = insert_record("question", $question)) {
error( get_string('cannotinsert','quiz') );
}
$this->questionids[] = $question->id;
// Now to save all the answers and type-specific options
global $QTYPES;
$result = $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;
}
// Give the question a unique version stamp determined by question_hash()
set_field('question', 'version', question_hash($question), 'id', $question->id);
}
return true;
}
/**
* Return complete file within an array, one item per line
* @param string filename name of file
* @return mixed contents array or false on failure
*/
function readdata($filename) {
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;
}
/**
* 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.
*
* If your format does not use blank lines as a delimiter
* then you will need to override this method. Even then
* try to use readquestion for each question
* @param array lines array of lines from readdata
* @return array array of question objects
*/
function readquestions($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;
}
/**
* return an "empty" question
* Somewhere to specify question parameters that are not handled
* by import but are required db fields.
* This should not be overridden.
* @return object default question
*/
function defaultquestion() {
global $CFG;
$question = new stdClass();
$question->shuffleanswers = $CFG->quiz_shuffleanswers;
$question->defaultgrade = 1;
$question->image = "";
$question->usecase = 0;
$question->multiplier = array();
$question->generalfeedback = '';
$question->correctfeedback = '';
$question->partiallycorrectfeedback = '';
$question->incorrectfeedback = '';
return $question;
}
/**
* Given the data known to define a question in
* this format, this function converts it into a question
* object suitable for processing and insertion into Moodle.
*
* If your format does not use blank lines to delimit questions
* (e.g. an XML format) you must override 'readquestions' too
* @param $lines mixed data that represents question
* @return object question object
*/
function readquestion($lines) {
$formatnotimplemented = get_string( 'formatnotimplemented','quiz' );
echo "<p>$formatnotimplemented</p>";
return NULL;
}
/**
* Override if any post-processing is required
* @return boolean success
*/
function importpostprocess() {
return true;
}
/**
* Import an image file encoded in base64 format
* @param string path path (in course data) to store picture
* @param string base64 encoded picture
* @return string filename (nb. collisions are handled)
*/
function importimagefile( $path, $base64 ) {
global $CFG;
// all this to get the destination directory
// and filename!
$fullpath = "{$CFG->dataroot}/{$this->course->id}/$path";
$path_parts = pathinfo( $fullpath );
$destination = $path_parts['dirname'];
$file = clean_filename( $path_parts['basename'] );
// detect and fix any filename collision - get unique filename
$newfiles = resolve_filename_collisions( $destination, array($file) );
$newfile = $newfiles[0];
// convert and save file contents
if (!$content = base64_decode( $base64 )) {
return false;
}
$newfullpath = "$destination/$newfile";
if (!$fh = fopen( $newfullpath, 'w' )) {
return false;
}
if (!fwrite( $fh, $content )) {
return false;
}
fclose( $fh );
// return the (possibly) new filename
return $newfile;
}
/*******************
* EXPORT FUNCTIONS
*******************/
/**
* Return the files extension appropriate for this type
* override if you don't want .txt
* @return string file extension
*/
function export_file_extension() {
return ".txt";
}
/**
* Do any pre-processing that may be required
* @param boolean success
*/
function exportpreprocess() {
return true;
}
/**
* Enable any processing to be done on the content
* just prior to the file being saved
* default is to do nothing
* @param string output text
* @param string processed output text
*/
function presave_process( $content ) {
return $content;
}
/**
* Do the export
* For most types this should not need to be overrided
* @return boolean success
*/
function exportprocess() {
global $CFG;
// create a directory for the exports (if not already existing)
if (! $export_dir = make_upload_directory($this->question_get_export_dir())) {
error( get_string('cannotcreatepath','quiz',$export_dir) );
}
$path = $CFG->dataroot.'/'.$this->question_get_export_dir();
// get the questions (from database) in this category
// only get q's with no parents (no cloze subquestions specifically)
$questions = get_questions_category( $this->category, true );
notify( get_string('exportingquestions','quiz') );
if (!count($questions)) {
notify( get_string('noquestions','quiz') );
return false;
}
$count = 0;
// results are first written into string (and then to a file)
// so create/initialize the string here
$expout = "";
// track which category questions are in
// if it changes we will record the category change in the output
// file if selected. 0 means that it will get printed before the 1st question
$trackcategory = 0;
// iterate through questions
foreach($questions as $question) {
// do not export hidden questions
if (!empty($question->hidden)) {
continue;
}
// do not export random questions
if ($question->qtype==RANDOM) {
continue;
}
// check if we need to record category change
if ($this->cattofile) {
if ($question->category != $trackcategory) {
$trackcategory = $question->category;
$categoryname = get_category_path( $trackcategory );
// create 'dummy' question for category export
$dummyquestion = new object;
$dummyquestion->qtype = 'category';
$dummyquestion->category = $categoryname;
$dummyquestion->name = "switch category to $categoryname";
$dummyquestion->id = 0;
$dummyquestion->questiontextformat = '';
$expout .= $this->writequestion( $dummyquestion ) . "\n";
}
}
// export the question displaying message
$count++;
echo "<hr /><p><b>$count</b>. ".$this->format_question_text($question)."</p>";
$expout .= $this->writequestion( $question ) . "\n";
}
// final pre-process on exported data
$expout = $this->presave_process( $expout );
// write file
$filepath = $path."/".$this->filename . $this->export_file_extension();
if (!$fh=fopen($filepath,"w")) {
error( get_string('cannotopen','quiz',$filepath) );
}
if (!fwrite($fh, $expout, strlen($expout) )) {
error( get_string('cannotwrite','quiz',$filepath) );
}
fclose($fh);
return true;
}
/**
* Do an post-processing that may be required
* @return boolean success
*/
function exportpostprocess() {
return true;
}
/**
* convert a single question object into text output in the given
* format.
* This must be overriden
* @param object question question object
* @return mixed question export text or null if not implemented
*/
function writequestion($question) {
// if not overidden, then this is an error.
$formatnotimplemented = get_string( 'formatnotimplemented','quiz' );
echo "<p>$formatnotimplemented</p>";
return NULL;
}
/**
* get directory into which export is going
* @return string file path
*/
function question_get_export_dir() {
$dirname = get_string("exportfilename","quiz");
$path = $this->course->id.'/backupdata/'.$dirname; // backupdata is protected directory
return $path;
}
/**
* where question specifies a moodle (text) format this
* performs the conversion.
*/
function format_question_text($question) {
$formatoptions = new stdClass;
$formatoptions->noclean = true;
$formatoptions->para = false;
if (empty($question->questiontextformat)) {
$format = FORMAT_MOODLE;
} else {
$format = $question->questiontextformat;
}
return format_text(stripslashes($question->questiontext), $format, $formatoptions);
}
}
?>