question export MDL-25088 this is the first part. I think question export is now working for all types.

Import to follow soon.
This commit is contained in:
Tim Hunt
2010-11-12 12:06:48 +00:00
parent e7ff48d284
commit 13bb604ed5
3 changed files with 358 additions and 292 deletions
+57 -55
View File
@@ -10,22 +10,22 @@
*/
class qformat_default {
var $displayerrors = true;
var $category = NULL;
var $questions = array();
var $course = NULL;
var $filename = '';
var $realfilename = '';
var $matchgrades = 'error';
var $catfromfile = 0;
var $contextfromfile = 0;
var $cattofile = 0;
var $contexttofile = 0;
var $questionids = array();
var $importerrors = 0;
var $stoponerror = true;
var $translator = null;
var $canaccessbackupdata = true;
public $displayerrors = true;
public $category = NULL;
public $questions = array();
public $course = NULL;
public $filename = '';
public $realfilename = '';
public $matchgrades = 'error';
public $catfromfile = 0;
public $contextfromfile = 0;
public $cattofile = 0;
public $contexttofile = 0;
public $questionids = array();
public $importerrors = 0;
public $stoponerror = true;
public $translator = null;
public $canaccessbackupdata = true;
protected $importcontext = null;
@@ -61,8 +61,8 @@ class qformat_default {
* set the category
* @param object category the category object
*/
function setCategory( $category ) {
if (count($this->questions)){
function setCategory($category) {
if (count($this->questions)) {
debugging('You shouldn\'t call setCategory after setQuestions');
}
$this->category = $category;
@@ -74,8 +74,8 @@ class qformat_default {
* Only used for question export.
* @param array of question objects
*/
function setQuestions( $questions ) {
if ($this->category !== null){
function setQuestions($questions) {
if ($this->category !== null) {
debugging('You shouldn\'t call setQuestions after setCategory');
}
$this->questions = $questions;
@@ -85,9 +85,10 @@ class qformat_default {
* set the course class variable
* @param course object Moodle course variable
*/
function setCourse( $course ) {
function setCourse($course) {
$this->course = $course;
}
/**
* set an array of contexts.
* @param array $contexts Moodle course variable
@@ -101,7 +102,7 @@ class qformat_default {
* set the filename
* @param string filename name of file to import/export
*/
function setFilename( $filename ) {
function setFilename($filename) {
$this->filename = $filename;
}
@@ -110,7 +111,7 @@ class qformat_default {
* (this is what the user typed, regardless of wha happened next)
* @param string realfilename name of file as typed by user
*/
function setRealfilename( $realfilename ) {
function setRealfilename($realfilename) {
$this->realfilename = $realfilename;
}
@@ -118,7 +119,7 @@ class qformat_default {
* set matchgrades
* @param string matchgrades error or nearest for grades
*/
function setMatchgrades( $matchgrades ) {
function setMatchgrades($matchgrades) {
$this->matchgrades = $matchgrades;
}
@@ -126,7 +127,7 @@ class qformat_default {
* set catfromfile
* @param bool catfromfile allow categories embedded in import file
*/
function setCatfromfile( $catfromfile ) {
function setCatfromfile($catfromfile) {
$this->catfromfile = $catfromfile;
}
@@ -142,9 +143,10 @@ class qformat_default {
* set cattofile
* @param bool cattofile exports categories within export file
*/
function setCattofile( $cattofile ) {
function setCattofile($cattofile) {
$this->cattofile = $cattofile;
}
/**
* set contexttofile
* @param bool cattofile exports categories within export file
@@ -157,7 +159,7 @@ class qformat_default {
* set stoponerror
* @param bool stoponerror stops database write if any errors reported
*/
function setStoponerror( $stoponerror ) {
function setStoponerror($stoponerror) {
$this->stoponerror = $stoponerror;
}
@@ -176,7 +178,7 @@ class qformat_default {
/**
* Handle parsing error
*/
function error( $message, $text='', $questionname='' ) {
function error($message, $text='', $questionname='') {
$importerrorquestion = get_string('importerrorquestion','quiz');
echo "<div class=\"importerror\">\n";
@@ -200,7 +202,7 @@ class qformat_default {
* @param qtypehint hint about a question type from format
* @return object question object suitable for save_options() or false if cannot handle
*/
function try_importing_using_qtypes( $data, $question=null, $extra=null, $qtypehint='') {
function try_importing_using_qtypes($data, $question=null, $extra=null, $qtypehint='') {
global $QTYPES;
// work out what format we are using
@@ -221,8 +223,8 @@ class qformat_default {
// loop through installed questiontypes checking for
// function to handle this question
foreach ($QTYPES as $qtype) {
if (method_exists( $qtype, $methodname)) {
if ($question = $qtype->$methodname( $data, $question, $this, $extra )) {
if (method_exists($qtype, $methodname)) {
if ($question = $qtype->$methodname($data, $question, $this, $extra)) {
return $question;
}
}
@@ -254,24 +256,24 @@ class qformat_default {
set_time_limit(0);
// STAGE 1: Parse the file
echo $OUTPUT->notification( get_string('parsingquestions','quiz') );
echo $OUTPUT->notification(get_string('parsingquestions','quiz'));
if (! $lines = $this->readdata($this->filename)) {
echo $OUTPUT->notification( get_string('cannotread','quiz') );
echo $OUTPUT->notification(get_string('cannotread','quiz'));
return false;
}
if (!$questions = $this->readquestions($lines)) { // Extract all the questions
echo $OUTPUT->notification( get_string('noquestionsinfile','quiz') );
if (!$questions = $this->readquestions($lines, $context)) { // Extract all the questions
echo $OUTPUT->notification(get_string('noquestionsinfile','quiz'));
return false;
}
// STAGE 2: Write data to database
echo $OUTPUT->notification( get_string('importingquestions','quiz',$this->count_questions($questions)) );
echo $OUTPUT->notification(get_string('importingquestions','quiz',$this->count_questions($questions)));
// check for errors before we continue
if ($this->stoponerror and ($this->importerrors>0)) {
echo $OUTPUT->notification( get_string('importparseerror','quiz') );
echo $OUTPUT->notification(get_string('importparseerror','quiz'));
return true;
}
@@ -380,6 +382,7 @@ class qformat_default {
}
return true;
}
/**
* Count all non-category questions in the questions array.
*
@@ -436,7 +439,7 @@ class qformat_default {
// Now create any categories that need to be created.
foreach ($catnames as $catname) {
if ($category = $DB->get_record( 'question_categories', array('name' => $catname, 'contextid' => $context->id, 'parent' => $parent))) {
if ($category = $DB->get_record('question_categories', array('name' => $catname, 'contextid' => $context->id, 'parent' => $parent))) {
$parent = $category->id;
} else {
require_capability('moodle/question:managecategory', $context);
@@ -516,7 +519,6 @@ class qformat_default {
return $questions;
}
/**
* return an "empty" question
* Somewhere to specify question parameters that are not handled
@@ -565,7 +567,7 @@ class qformat_default {
*/
function readquestion($lines) {
$formatnotimplemented = get_string( 'formatnotimplemented','quiz' );
$formatnotimplemented = get_string('formatnotimplemented','quiz');
echo "<p>$formatnotimplemented</p>";
return NULL;
@@ -592,17 +594,17 @@ class qformat_default {
* @param extra mixed any addition format specific data needed
* @return string the data to append to export or false if error (or unhandled)
*/
function try_exporting_using_qtypes( $name, $question, $extra=null ) {
function try_exporting_using_qtypes($name, $question, $extra=null) {
global $QTYPES;
// work out the name of format in use
$formatname = substr( get_class( $this ), strlen( 'qformat_' ));
$formatname = substr(get_class($this), strlen('qformat_'));
$methodname = "export_to_$formatname";
if (array_key_exists( $name, $QTYPES )) {
if (array_key_exists($name, $QTYPES)) {
$qtype = $QTYPES[ $name ];
if (method_exists( $qtype, $methodname )) {
if ($data = $qtype->$methodname( $question, $this, $extra )) {
if (method_exists($qtype, $methodname)) {
if ($data = $qtype->$methodname($question, $this, $extra)) {
return $data;
}
}
@@ -625,7 +627,7 @@ class qformat_default {
* @param string output text
* @param string processed output text
*/
function presave_process( $content ) {
function presave_process($content) {
return $content;
}
@@ -640,12 +642,12 @@ class qformat_default {
// get the questions (from database) in this category
// only get q's with no parents (no cloze subquestions specifically)
if ($this->category) {
$questions = get_questions_category( $this->category, true );
$questions = get_questions_category($this->category, true);
} else {
$questions = $this->questions;
}
//echo $OUTPUT->notification( get_string('exportingquestions','quiz') );
//echo $OUTPUT->notification(get_string('exportingquestions','quiz'));
$count = 0;
// results are first written into string (and then to a file)
@@ -726,7 +728,7 @@ class qformat_default {
// did we actually process anything
if ($count==0) {
print_error( 'noquestions','quiz',$continuepath );
print_error('noquestions','quiz',$continuepath);
}
// final pre-process on exported data
@@ -751,9 +753,9 @@ class qformat_default {
do {
$pathsections[] = $category->name;
$id = $category->parent;
} while ($category = $DB->get_record( 'question_categories', array('id' => $id )));
} while ($category = $DB->get_record('question_categories', array('id' => $id)));
if ($includecontext){
if ($includecontext) {
$pathsections[] = '$' . $contextstring . '$';
}
@@ -826,7 +828,7 @@ class qformat_default {
*/
function writequestion($question) {
// if not overidden, then this is an error.
$formatnotimplemented = get_string( 'formatnotimplemented','quiz' );
$formatnotimplemented = get_string('formatnotimplemented','quiz');
echo "<p>$formatnotimplemented</p>";
return NULL;
}
@@ -847,8 +849,8 @@ class qformat_default {
}
/**
* where question specifies a moodle (text) format this
* performs the conversion.
* Convert the question text to plain text, so it can safely be displayed
* during import to let the user see roughly what is going on.
*/
function format_question_text($question) {
global $DB;
@@ -861,7 +863,7 @@ class qformat_default {
$format = $question->questiontextformat;
}
$text = $question->questiontext;
return format_text(html_to_text($text), $format, $formatoptions);
return format_text(html_to_text($text, 0, false), $format, $formatoptions);
}
/**