Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-38388 qformat_blackboard_six: fix question image import

This works on the expectation that the returned array from readdata()
is opaque and will be fed directly into readquestions() since
filebase is different per file and needs to be set up appropriately
before each parse.
  • Loading branch information...
commit ffbc0d6f2f5b545153809c4bc4ac0e49188f8dcd 1 parent a812924
@jonof jonof authored
View
79 question/format/blackboard_six/format.php
@@ -29,13 +29,20 @@
require_once($CFG->dirroot . '/question/format/blackboard_six/formatqti.php');
require_once($CFG->dirroot . '/question/format/blackboard_six/formatpool.php');
+class qformat_blackboard_six_file {
+ /** @var int type of file being imported, one of the constants FILETYPE_QTI or FILETYPE_POOL. */
+ public $filetype;
+ /** @var string the xml text */
+ public $text;
+ /** @var string path to path to root of image tree in unzipped archive. */
+ public $filebase = '';
+}
+
class qformat_blackboard_six extends qformat_blackboard_six_base {
/** @var int Blackboard assessment qti files were always imported by the blackboard_six plugin. */
const FILETYPE_QTI = 1;
/** @var int Blackboard question pool files were previously handled by the blackboard plugin. */
const FILETYPE_POOL = 2;
- /** @var int type of file being imported, one of the constants FILETYPE_QTI or FILETYPE_POOL. */
- public $filetype;
public function get_filecontent($path) {
$fullpath = $this->tempdir . '/' . $path;
@@ -46,14 +53,6 @@ public function get_filecontent($path) {
}
/**
- * Set the file type being imported
- * @param int $type the imported file's type
- */
- public function set_filetype($type) {
- $this->filetype = $type;
- }
-
- /**
* Return content of all files containing questions,
* as an array one element for each file found,
* For each file, the corresponding element is an array of lines.
@@ -69,23 +68,26 @@ public function readdata($filename) {
$this->error(get_string('filenotreadable', 'error'));
return false;
}
+
+ $fileobj = new qformat_blackboard_six_file();
+
// As we are not importing a .zip file,
// there is no imsmanifest, and it is not possible
// to parse it to find the file type.
// So we need to guess the file type by looking at the content.
// For now we will do that searching for a required tag.
// This is certainly not bullet-proof but works for all usual files.
- $text = file_get_contents($filename);
+ $fileobj->text = file_get_contents($filename);
if (strpos($text, '<questestinterop>')) {
- $this->set_filetype(self::FILETYPE_QTI);
+ $fileobj->filetype = self::FILETYPE_QTI;
}
if (strpos($text, '<POOL>')) {
- $this->set_filetype(self::FILETYPE_POOL);
+ $fileobj->filetype = self::FILETYPE_POOL;
}
// In all other cases we are not able to handle this question file.
// Readquestions is now expecting an array of strings.
- return array($text);
+ return array($fileobj);
}
// We are importing a zip file.
// Create name for temporary directory.
@@ -110,26 +112,30 @@ public function readdata($filename) {
// We starts from the root element.
$query = '//resources/resource';
- $this->filebase = $this->tempdir;
$q_file = array();
$examfiles = $xpath->query($query);
foreach ($examfiles as $examfile) {
+ $fileobj = new qformat_blackboard_six_file();
+
if ($examfile->getAttribute('type') == 'assessment/x-bb-qti-test'
|| $examfile->getAttribute('type') == 'assessment/x-bb-qti-pool') {
if ($content = $this->get_filecontent($examfile->getAttribute('bb:file'))) {
- $this->set_filetype(self::FILETYPE_QTI);
- $q_file[] = $content;
+ $fileobj->filetype = self::FILETYPE_QTI;
+ $fileobj->filebase = $this->tempdir;
+ $fileobj->text = $content;
+ $q_file[] = $fileobj;
}
}
if ($examfile->getAttribute('type') == 'assessment/x-bb-pool') {
if ($examfile->getAttribute('baseurl')) {
- $this->filebase = $this->tempdir. '/' . $examfile->getAttribute('baseurl');
+ $fileobj->filebase = $this->tempdir. '/' . $examfile->getAttribute('baseurl');
}
if ($content = $this->get_filecontent($examfile->getAttribute('file'))) {
- $this->set_filetype(self::FILETYPE_POOL);
- $q_file[] = $content;
+ $fileobj->filetype = self::FILETYPE_POOL;
+ $fileobj->text = $content;
+ $q_file[] = $fileobj;
}
}
}
@@ -152,32 +158,33 @@ public function readdata($filename) {
}
/**
- * Parse the array of strings into an array of questions.
- * Each string is the content of a .dat questions file.
+ * Parse the array of objects into an array of questions.
+ * Each object is the content of a .dat questions file.
* This *could* burn memory - but it won't happen that much
* so fingers crossed!
- * @param array of strings from the input file.
- * @param stdClass $context
+ * @param array $lines array of qformat_blackboard_six_file objects for each input file.
* @return array (of objects) question objects.
*/
public function readquestions($lines) {
// Set up array to hold all our questions.
$questions = array();
- if ($this->filetype == self::FILETYPE_QTI) {
- $importer = new qformat_blackboard_six_qti();
- } else if ($this->filetype == self::FILETYPE_POOL) {
- $importer = new qformat_blackboard_six_pool();
- } else {
- // In all other cases we are not able to import the file.
- return false;
- }
- $importer->set_filebase($this->filebase);
- // Each element of $lines is a string containing a complete xml document.
- foreach ($lines as $text) {
- $questions = array_merge($questions, $importer->readquestions($text));
+ // Each element of $lines is a qformat_blackboard_six_file object.
+ foreach ($lines as $fileobj) {
+ if ($fileobj->filetype == self::FILETYPE_QTI) {
+ $importer = new qformat_blackboard_six_qti();
+ } else if ($fileobj->filetype == self::FILETYPE_POOL) {
+ $importer = new qformat_blackboard_six_pool();
+ } else {
+ // In all other cases we are not able to import the file.
+ debugging('fileobj type not recognised', DEBUG_DEVELOPER);
+ continue;
+ }
+ $importer->set_filebase($fileobj->filebase);
+ $questions = array_merge($questions, $importer->readquestions($fileobj->text));
}
+
return $questions;
}
}
View
12 question/format/blackboard_six/tests/blackboardformatpool_test.php
@@ -41,8 +41,10 @@
class qformat_blackboard_six_pool_test extends question_testcase {
public function make_test_xml() {
- $xml = file_get_contents(__DIR__ . '/fixtures/sample_blackboard_pool.dat');
- return array(0=>$xml);
+ $xmlfile = new qformat_blackboard_six_file();
+ $xmlfile->filetype = 2;
+ $xmlfile->text = file_get_contents(__DIR__ . '/fixtures/sample_blackboard_pool.dat');
+ return array(0=>$xmlfile);
}
public function test_import_match() {
@@ -50,7 +52,6 @@ public function test_import_match() {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
- $importer->set_filetype(2);
$questions = $importer->readquestions($xml);
$q = $questions[4];
@@ -87,7 +88,6 @@ public function test_import_multichoice_single() {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
- $importer->set_filetype(2);
$questions = $importer->readquestions($xml);
$q = $questions[1];
@@ -147,7 +147,6 @@ public function test_import_multichoice_multi() {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
- $importer->set_filetype(2);
$questions = $importer->readquestions($xml);
$q = $questions[2];
@@ -221,7 +220,6 @@ public function test_import_truefalse() {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
- $importer->set_filetype(2);
$questions = $importer->readquestions($xml);
$q = $questions[0];
@@ -251,7 +249,6 @@ public function test_import_fill_in_the_blank() {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
- $importer->set_filetype(2);
$questions = $importer->readquestions($xml);
$q = $questions[3];
@@ -286,7 +283,6 @@ public function test_import_essay() {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
- $importer->set_filetype(2);
$questions = $importer->readquestions($xml);
$q = $questions[5];
View
12 question/format/blackboard_six/tests/blackboardsixformatqti_test.php
@@ -41,14 +41,15 @@
class qformat_blackboard_six_qti_test extends question_testcase {
public function make_test_xml() {
- $xml = file_get_contents(__DIR__ . '/fixtures/sample_blackboard_qti.dat');
- return array(0=>$xml);
+ $xmlfile = new qformat_blackboard_six_file();
+ $xmlfile->filetype = 1;
+ $xmlfile->text = file_get_contents(__DIR__ . '/fixtures/sample_blackboard_qti.dat');
+ return array(0=>$xmlfile);
}
public function test_import_match() {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
- $importer->set_filetype(1);
$questions = $importer->readquestions($xml);
$q = $questions[3];
@@ -83,7 +84,6 @@ public function test_import_multichoice_single() {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
- $importer->set_filetype(1);
$questions = $importer->readquestions($xml);
$q = $questions[1];
@@ -143,7 +143,6 @@ public function test_import_multichoice_multi() {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
- $importer->set_filetype(1);
$questions = $importer->readquestions($xml);
$q = $questions[2];
@@ -220,7 +219,6 @@ public function test_import_truefalse() {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
- $importer->set_filetype(1);
$questions = $importer->readquestions($xml);
$q = $questions[0];
@@ -251,7 +249,6 @@ public function test_import_fill_in_the_blank() {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
- $importer->set_filetype(1);
$questions = $importer->readquestions($xml);
$q = $questions[4];
@@ -286,7 +283,6 @@ public function test_import_essay() {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
- $importer->set_filetype(1);
$questions = $importer->readquestions($xml);
$q = $questions[5];
Please sign in to comment.
Something went wrong with that request. Please try again.