first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,46 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy Subsystem implementation for qformat_blackboard_six.
*
* @package qformat_blackboard_six
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace qformat_blackboard_six\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for qformat_blackboard_six implementing null_provider.
*
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
+223
View File
@@ -0,0 +1,223 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Blackboard V5 and V6 question importer.
*
* @package qformat_blackboard_six
* @copyright 2005 Michael Penney
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/xmlize.php');
require_once($CFG->dirroot . '/question/format/blackboard_six/formatbase.php');
require_once($CFG->dirroot . '/question/format/blackboard_six/formatqti.php');
require_once($CFG->dirroot . '/question/format/blackboard_six/formatpool.php');
/**
* Class to represent a Blackboard file.
*
* @package qformat_blackboard_six
* @copyright 2005 Michael Penney
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
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 = '';
}
/**
* Blackboard Six QTI file importer class.
*
* @package qformat_blackboard_six
* @copyright 2005 Michael Penney
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
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 string temporary directory/folder. */
public $temp_dir;
/**
* Return the content of a file given by its path in the tempdir directory.
*
* @param string $path path to the file inside tempdir
* @return mixed contents array or false on failure
*/
public function get_filecontent($path) {
$fullpath = $this->tempdir . '/' . clean_param($path, PARAM_PATH);
if (is_file($fullpath) && is_readable($fullpath)) {
return file_get_contents($fullpath);
}
return false;
}
/**
* 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.
*
* @param string $filename name of file
* @return mixed contents array or false on failure
*/
public function readdata($filename) {
global $CFG;
// Find if we are importing a .dat file.
if (strtolower(pathinfo($filename, PATHINFO_EXTENSION)) == 'dat') {
if (!is_readable($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.
$fileobj->text = file_get_contents($filename);
if (strpos($fileobj->text, '<questestinterop>')) {
$fileobj->filetype = self::FILETYPE_QTI;
}
if (strpos($fileobj->text, '<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($fileobj);
}
// We are importing a zip file.
// Create name for temporary directory.
$uniquecode = time();
$this->tempdir = make_temp_directory('bbquiz_import/' . $uniquecode);
if (is_readable($filename)) {
if (!copy($filename, $this->tempdir . '/bboard.zip')) {
$this->error(get_string('cannotcopybackup', 'question'));
fulldelete($this->tempdir);
return false;
}
$packer = get_file_packer('application/zip');
if ($packer->extract_to_pathname($this->tempdir . '/bboard.zip', $this->tempdir)) {
$dom = new DomDocument();
if (!$dom->load($this->tempdir . '/imsmanifest.xml')) {
$this->error(get_string('errormanifest', 'qformat_blackboard_six'));
fulldelete($this->tempdir);
return false;
}
$xpath = new DOMXPath($dom);
// We starts from the root element.
$query = '//resources/resource';
$qfile = 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'))) {
$fileobj->filetype = self::FILETYPE_QTI;
$fileobj->filebase = $this->tempdir;
$fileobj->text = $content;
$qfile[] = $fileobj;
}
}
if ($examfile->getAttribute('type') == 'assessment/x-bb-pool') {
if ($examfile->getAttribute('baseurl')) {
$fileobj->filebase = $this->tempdir. '/' . clean_param($examfile->getAttribute('baseurl'), PARAM_PATH);
}
if ($content = $this->get_filecontent($examfile->getAttribute('file'))) {
$fileobj->filetype = self::FILETYPE_POOL;
$fileobj->text = $content;
$qfile[] = $fileobj;
}
}
}
if ($qfile) {
return $qfile;
} else {
$this->error(get_string('cannotfindquestionfile', 'question'));
fulldelete($this->tempdir);
}
} else {
$this->error(get_string('cannotunzip', 'question'));
fulldelete($this->temp_dir);
}
} else {
$this->error(get_string('cannotreaduploadfile', 'error'));
fulldelete($this->tempdir);
}
return false;
}
/**
* 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 $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();
// 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));
}
// Give any unnamed categories generated names.
$unnamedcount = 0;
foreach ($questions as $question) {
if ($question->qtype == 'category' && $question->category == '') {
$question->category = get_string('importedcategory', 'qformat_blackboard_six', ++$unnamedcount);
}
}
return $questions;
}
}
@@ -0,0 +1,155 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Blackboard V5 and V6 question importer.
*
* @package qformat_blackboard_six
* @copyright 2012 Jean-Michel Vedrine
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Base class question import format for zip files with images
*
* @package qformat_blackboard_six
* @copyright 2012 Jean-Michel Vedrine
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qformat_blackboard_six_base extends qformat_based_on_xml {
/** @var string path to path to root of image tree in unzipped archive. */
public $filebase = '';
/** @var string path to the temporary directory. */
public $tempdir = '';
/**
* This plugin provide import
* @return bool true
*/
public function provide_import() {
return true;
}
/**
* Check if the given file is capable of being imported by this plugin.
* As {@link file_storage::mimetype()} may use finfo PHP extension if available,
* the value returned by $file->get_mimetype for a .dat file is not the same on all servers.
* So we must made 2 checks to verify if the plugin can import the file.
* @param stored_file $file the file to check
* @return bool whether this plugin can import the file
*/
public function can_import_file($file) {
$mimetypes = array(
mimeinfo('type', '.dat'),
mimeinfo('type', '.zip')
);
return in_array($file->get_mimetype(), $mimetypes) || in_array(mimeinfo('type', $file->get_filename()), $mimetypes);
}
public function mime_type() {
return mimeinfo('type', '.zip');
}
/**
* Does any post-processing that may be desired
* Clean the temporary directory if a zip file was imported
* @return bool success
*/
public function importpostprocess() {
if ($this->tempdir != '') {
fulldelete($this->tempdir);
}
return true;
}
/**
* Set the path to the root of images tree
* @param string $path path to images root
*/
public function set_filebase($path) {
$this->filebase = $path;
}
/**
* Store an image file in a draft filearea
* @param array $text, if itemid element don't exists it will be created
* @param string $tempdir path to root of image tree
* @param string $filepathinsidetempdir path to image in the tree
* @param string $filename image's name
* @return string new name of the image as it was stored
*/
protected function store_file_for_text_field(&$text, $tempdir, $filepathinsidetempdir, $filename) {
global $USER;
$fs = get_file_storage();
if (empty($text['itemid'])) {
$text['itemid'] = file_get_unused_draft_itemid();
}
// As question file areas don't support subdirs,
// convert path to filename.
// So that images with same name can be imported.
$newfilename = clean_param(str_replace('/', '__', $filepathinsidetempdir . '__' . $filename), PARAM_FILE);
$filerecord = array(
'contextid' => context_user::instance($USER->id)->id,
'component' => 'user',
'filearea' => 'draft',
'itemid' => $text['itemid'],
'filepath' => '/',
'filename' => $newfilename,
);
$fs->create_file_from_pathname($filerecord, $tempdir . '/' . $filepathinsidetempdir . '/' . $filename);
return $newfilename;
}
/**
* Given an HTML text with references to images files,
* store all images in a draft filearea,
* and return an array with all urls in text recoded,
* format set to FORMAT_HTML, and itemid set to filearea itemid
* @param string $text text to parse and recode
* @return array with keys text, format, itemid.
*/
public function text_field($text) {
$data = array();
// Step one, find all file refs then add to array.
preg_match_all('|<img[^>]+src="([^"]*)"|i', $text, $out); // Find all src refs.
$filepaths = array();
foreach ($out[1] as $path) {
$fullpath = $this->filebase . '/' . $path;
if (is_readable($fullpath) && !in_array($path, $filepaths)) {
$dirpath = dirname($path);
$filename = basename($path);
$newfilename = $this->store_file_for_text_field($data, $this->filebase, $dirpath, $filename);
$text = preg_replace("|{$path}|", "@@PLUGINFILE@@/" . $newfilename, $text);
$filepaths[] = $path;
}
}
$data['text'] = $text;
$data['format'] = FORMAT_HTML;
return $data;
}
/**
* Same as text_field but text is cleaned.
* @param string $text text to parse and recode
* @return array with keys text, format, itemid.
*/
public function cleaned_text_field($text) {
return $this->text_field($this->cleaninput($text));
}
}
@@ -0,0 +1,492 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Blackboard V5 and V6 question importer.
*
* @package qformat_blackboard_six
* @copyright 2003 Scott Elliott
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/xmlize.php');
/**
* Blackboard pool question importer class.
*
* @package qformat_blackboard_six
* @copyright 2003 Scott Elliott
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qformat_blackboard_six_pool extends qformat_blackboard_six_base {
/**
* @var bool Is the current question's question text escaped HTML
* (true for most if not all Blackboard files).
*/
public $ishtml = true;
/**
* Parse the xml document into an array of questions
*
* This *could* burn memory - but it won't happen that much
* so fingers crossed!
*
* @param array $text array of lines from the input file.
* @return array (of objects) questions objects.
*/
protected function readquestions($text) {
// This converts xml to big nasty data structure,
// the 0 means keep white space as it is.
try {
$xml = xmlize($text, 0, 'UTF-8', true);
} catch (xml_format_exception $e) {
$this->error($e->getMessage(), '');
return false;
}
$questions = array();
$this->process_category($xml, $questions);
$this->process_tf($xml, $questions);
$this->process_mc($xml, $questions);
$this->process_ma($xml, $questions);
$this->process_fib($xml, $questions);
$this->process_matching($xml, $questions);
$this->process_essay($xml, $questions);
return $questions;
}
/**
* Do question import processing common to every qtype.
*
* @param array $questiondata the xml tree related to the current question
* @return object initialized question object.
*/
public function process_common($questiondata) {
// This routine initialises the question object.
$question = $this->defaultquestion();
// Determine if the question is already escaped html.
$this->ishtml = $this->getpath($questiondata,
array('#', 'BODY', 0, '#', 'FLAGS', 0, '#', 'ISHTML', 0, '@', 'value'),
false, false);
// Put questiontext in question object.
$text = $this->getpath($questiondata,
array('#', 'BODY', 0, '#', 'TEXT', 0, '#'),
'', true, get_string('importnotext', 'qformat_blackboard_six'));
$questiontext = $this->cleaned_text_field($text);
$question->questiontext = $questiontext['text'];
$question->questiontextformat = $questiontext['format']; // Needed because add_blank_combined_feedback uses it.
if (isset($questiontext['itemid'])) {
$question->questiontextitemid = $questiontext['itemid'];
}
// Put name in question object. We must ensure it is not empty and it is less than 250 chars.
$id = $this->getpath($questiondata, array('@', 'id'), '', true);
$question->name = $this->create_default_question_name($question->questiontext,
get_string('defaultname', 'qformat_blackboard_six' , $id));
$question->generalfeedback = '';
$question->generalfeedbackformat = FORMAT_HTML;
$question->generalfeedbackfiles = array();
// TODO : read the mark from the POOL TITLE QUESTIONLIST section.
$question->defaultmark = 1;
return $question;
}
/**
* Add a category question entry based on the pool file title
* @param array $xml the xml tree
* @param array $questions the questions already parsed
*/
public function process_category($xml, &$questions) {
$title = $this->getpath($xml, array('POOL', '#', 'TITLE', 0, '@', 'value'), '', true);
$dummyquestion = new stdClass();
$dummyquestion->qtype = 'category';
$dummyquestion->category = $this->cleaninput($this->clean_question_name($title));
$questions[] = $dummyquestion;
}
/**
* Process Essay Questions
* @param array $xml the xml tree
* @param array $questions the questions already parsed
*/
public function process_essay($xml, &$questions) {
if ($this->getpath($xml, array('POOL', '#', 'QUESTION_ESSAY'), false, false)) {
$essayquestions = $this->getpath($xml,
array('POOL', '#', 'QUESTION_ESSAY'), false, false);
} else {
return;
}
foreach ($essayquestions as $thisquestion) {
$question = $this->process_common($thisquestion);
$question->qtype = 'essay';
$question->answer = '';
$answer = $this->getpath($thisquestion,
array('#', 'ANSWER', 0, '#', 'TEXT', 0, '#'), '', true);
$question->graderinfo = $this->cleaned_text_field($answer);
$question->responsetemplate = $this->text_field('');
$question->feedback = '';
$question->responseformat = 'editor';
$question->responserequired = 1;
$question->responsefieldlines = 15;
$question->attachments = 0;
$question->attachmentsrequired = 0;
$question->fraction = 0;
$questions[] = $question;
}
}
/**
* Process True / False Questions
* @param array $xml the xml tree
* @param array $questions the questions already parsed
*/
public function process_tf($xml, &$questions) {
if ($this->getpath($xml, array('POOL', '#', 'QUESTION_TRUEFALSE'), false, false)) {
$tfquestions = $this->getpath($xml,
array('POOL', '#', 'QUESTION_TRUEFALSE'), false, false);
} else {
return;
}
foreach ($tfquestions as $thisquestion) {
$question = $this->process_common($thisquestion);
$question->qtype = 'truefalse';
$question->single = 1; // Only one answer is allowed.
$choices = $this->getpath($thisquestion, array('#', 'ANSWER'), array(), false);
$correctanswer = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'CORRECTANSWER', 0, '@', 'answer_id'),
'', true);
// First choice is true, second is false.
$id = $this->getpath($choices[0], array('@', 'id'), '', true);
$correctfeedback = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_CORRECT', 0, '#'),
'', true);
$incorrectfeedback = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_INCORRECT', 0, '#'),
'', true);
if (strcmp($id, $correctanswer) == 0) { // True is correct.
$question->answer = 1;
$question->feedbacktrue = $this->cleaned_text_field($correctfeedback);
$question->feedbackfalse = $this->cleaned_text_field($incorrectfeedback);
} else { // False is correct.
$question->answer = 0;
$question->feedbacktrue = $this->cleaned_text_field($incorrectfeedback);
$question->feedbackfalse = $this->cleaned_text_field($correctfeedback);
}
$question->correctanswer = $question->answer;
$questions[] = $question;
}
}
/**
* Process Multiple Choice Questions with single answer
* @param array $xml the xml tree
* @param array $questions the questions already parsed
*/
public function process_mc($xml, &$questions) {
if ($this->getpath($xml, array('POOL', '#', 'QUESTION_MULTIPLECHOICE'), false, false)) {
$mcquestions = $this->getpath($xml,
array('POOL', '#', 'QUESTION_MULTIPLECHOICE'), false, false);
} else {
return;
}
foreach ($mcquestions as $thisquestion) {
$question = $this->process_common($thisquestion);
$correctfeedback = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_CORRECT', 0, '#'),
'', true);
$incorrectfeedback = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_INCORRECT', 0, '#'),
'', true);
$question->correctfeedback = $this->cleaned_text_field($correctfeedback);
$question->partiallycorrectfeedback = $this->text_field('');
$question->incorrectfeedback = $this->cleaned_text_field($incorrectfeedback);
$question->qtype = 'multichoice';
$question->single = 1; // Only one answer is allowed.
$choices = $this->getpath($thisquestion, array('#', 'ANSWER'), false, false);
$correctanswerid = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'CORRECTANSWER', 0, '@', 'answer_id'),
'', true);
foreach ($choices as $choice) {
$choicetext = $this->getpath($choice, array('#', 'TEXT', 0, '#'), '', true);
// Put this choice in the question object.
$question->answer[] = $this->cleaned_text_field($choicetext);
$choiceid = $this->getpath($choice, array('@', 'id'), '', true);
// If choice is the right answer, give 100% mark, otherwise give 0%.
if (strcmp ($choiceid, $correctanswerid) == 0) {
$question->fraction[] = 1;
} else {
$question->fraction[] = 0;
}
// There is never feedback specific to each choice.
$question->feedback[] = $this->text_field('');
}
$questions[] = $question;
}
}
/**
* Process Multiple Choice Questions With Multiple Answers
* @param array $xml the xml tree
* @param array $questions the questions already parsed
*/
public function process_ma($xml, &$questions) {
if ($this->getpath($xml, array('POOL', '#', 'QUESTION_MULTIPLEANSWER'), false, false)) {
$maquestions = $this->getpath($xml,
array('POOL', '#', 'QUESTION_MULTIPLEANSWER'), false, false);
} else {
return;
}
foreach ($maquestions as $thisquestion) {
$question = $this->process_common($thisquestion);
$correctfeedback = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_CORRECT', 0, '#'),
'', true);
$incorrectfeedback = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_INCORRECT', 0, '#'),
'', true);
$question->correctfeedback = $this->cleaned_text_field($correctfeedback);
// As there is no partially correct feedback we use incorrect one.
$question->partiallycorrectfeedback = $this->cleaned_text_field($incorrectfeedback);
$question->incorrectfeedback = $this->cleaned_text_field($incorrectfeedback);
$question->qtype = 'multichoice';
$question->defaultmark = 1;
$question->single = 0; // More than one answers allowed.
$choices = $this->getpath($thisquestion, array('#', 'ANSWER'), false, false);
$correctanswerids = array();
foreach ($this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'CORRECTANSWER'), false, false) as $correctanswer) {
if ($correctanswer) {
$correctanswerids[] = $this->getpath($correctanswer,
array('@', 'answer_id'),
'', true);
}
}
$fraction = 1 / count($correctanswerids);
foreach ($choices as $choice) {
$choicetext = $this->getpath($choice, array('#', 'TEXT', 0, '#'), '', true);
// Put this choice in the question object.
$question->answer[] = $this->cleaned_text_field($choicetext);
$choiceid = $this->getpath($choice, array('@', 'id'), '', true);
$iscorrect = in_array($choiceid, $correctanswerids);
if ($iscorrect) {
$question->fraction[] = $fraction;
} else {
$question->fraction[] = 0;
}
// There is never feedback specific to each choice.
$question->feedback[] = $this->text_field('');
}
$questions[] = $question;
}
}
/**
* Process Fill in the Blank Questions
* @param array $xml the xml tree
* @param array $questions the questions already parsed
*/
public function process_fib($xml, &$questions) {
if ($this->getpath($xml, array('POOL', '#', 'QUESTION_FILLINBLANK'), false, false)) {
$fibquestions = $this->getpath($xml,
array('POOL', '#', 'QUESTION_FILLINBLANK'), false, false);
} else {
return;
}
foreach ($fibquestions as $thisquestion) {
$question = $this->process_common($thisquestion);
$question->qtype = 'shortanswer';
$question->usecase = 0; // Ignore case.
$correctfeedback = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_CORRECT', 0, '#'),
'', true);
$incorrectfeedback = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_INCORRECT', 0, '#'),
'', true);
$answers = $this->getpath($thisquestion, array('#', 'ANSWER'), false, false);
foreach ($answers as $answer) {
$question->answer[] = $this->getpath($answer,
array('#', 'TEXT', 0, '#'), '', true);
$question->fraction[] = 1;
$question->feedback[] = $this->cleaned_text_field($correctfeedback);
}
$question->answer[] = '*';
$question->fraction[] = 0;
$question->feedback[] = $this->cleaned_text_field($incorrectfeedback);
$questions[] = $question;
}
}
/**
* Process Matching Questions
* @param array $xml the xml tree
* @param array $questions the questions already parsed
*/
public function process_matching($xml, &$questions) {
if ($this->getpath($xml, array('POOL', '#', 'QUESTION_MATCH'), false, false)) {
$matchquestions = $this->getpath($xml,
array('POOL', '#', 'QUESTION_MATCH'), false, false);
} else {
return;
}
// Blackboard questions can't be imported in core Moodle without a loss in data,
// as core match question don't allow HTML in subanswers. The contributed ddmatch
// question type support HTML in subanswers.
// The ddmatch question type is not part of core, so we need to check if it is defined.
$ddmatchisinstalled = question_bank::is_qtype_installed('ddmatch');
foreach ($matchquestions as $thisquestion) {
$question = $this->process_common($thisquestion);
if ($ddmatchisinstalled) {
$question->qtype = 'ddmatch';
} else {
$question->qtype = 'match';
}
$correctfeedback = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_CORRECT', 0, '#'),
'', true);
$incorrectfeedback = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_INCORRECT', 0, '#'),
'', true);
$question->correctfeedback = $this->cleaned_text_field($correctfeedback);
// As there is no partially correct feedback we use incorrect one.
$question->partiallycorrectfeedback = $this->cleaned_text_field($incorrectfeedback);
$question->incorrectfeedback = $this->cleaned_text_field($incorrectfeedback);
$choices = $this->getpath($thisquestion,
array('#', 'CHOICE'), false, false); // Blackboard "choices" are Moodle subanswers.
$answers = $this->getpath($thisquestion,
array('#', 'ANSWER'), false, false); // Blackboard "answers" are Moodle subquestions.
$correctanswers = $this->getpath($thisquestion,
array('#', 'GRADABLE', 0, '#', 'CORRECTANSWER'), false, false); // Mapping between choices and answers.
$mappings = array();
foreach ($correctanswers as $correctanswer) {
if ($correctanswer) {
$correctchoiceid = $this->getpath($correctanswer,
array('@', 'choice_id'), '', true);
$correctanswerid = $this->getpath($correctanswer,
array('@', 'answer_id'),
'', true);
$mappings[$correctanswerid] = $correctchoiceid;
}
}
foreach ($choices as $choice) {
if ($ddmatchisinstalled) {
$choicetext = $this->cleaned_text_field($this->getpath($choice,
array('#', 'TEXT', 0, '#'), '', true));
} else {
$choicetext = trim(strip_tags($this->getpath($choice,
array('#', 'TEXT', 0, '#'), '', true)));
}
if ($choicetext != '') { // Only import non empty subanswers.
$subquestion = '';
$choiceid = $this->getpath($choice,
array('@', 'id'), '', true);
$fiber = array_search($choiceid, $mappings);
$fiber = moodle_array_keys_filter($mappings, $choiceid);
foreach ($fiber as $correctanswerid) {
// We have found a correspondance for this choice so we need to take the associated answer.
foreach ($answers as $answer) {
$currentanswerid = $this->getpath($answer,
array('@', 'id'), '', true);
if (strcmp ($currentanswerid, $correctanswerid) == 0) {
$subquestion = $this->getpath($answer,
array('#', 'TEXT', 0, '#'), '', true);
break;
}
}
$question->subquestions[] = $this->cleaned_text_field($subquestion);
$question->subanswers[] = $choicetext;
}
if ($subquestion == '') { // Then in this case, $choice is a distractor.
$question->subquestions[] = $this->text_field('');
$question->subanswers[] = $choicetext;
}
}
}
// Verify that this matching question has enough subquestions and subanswers.
$subquestioncount = 0;
$subanswercount = 0;
$subanswers = $question->subanswers;
foreach ($question->subquestions as $key => $subquestion) {
$subquestion = $subquestion['text'];
$subanswer = $subanswers[$key];
if ($subquestion != '') {
$subquestioncount++;
}
$subanswercount++;
}
if ($subquestioncount < 2 || $subanswercount < 3) {
$this->error(get_string('notenoughtsubans', 'qformat_blackboard_six', $question->questiontext));
} else {
$questions[] = $question;
}
}
}
}
@@ -0,0 +1,914 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Blackboard V5 and V6 question importer.
*
* @package qformat_blackboard_six
* @copyright 2005 Michael Penney
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/xmlize.php');
/**
* Blackboard 6.0 question importer.
*
* @copyright 2005 Michael Penney
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qformat_blackboard_six_qti extends qformat_blackboard_six_base {
/**
* Parse the xml document into an array of questions
* this *could* burn memory - but it won't happen that much
* so fingers crossed!
* @param array $text array of lines from the input file.
* @return array (of objects) questions objects.
*/
protected function readquestions($text) {
// This converts xml to big nasty data structure,
// the 0 means keep white space as it is.
try {
$xml = xmlize($text, 0, 'UTF-8', true);
} catch (xml_format_exception $e) {
$this->error($e->getMessage(), '');
return false;
}
$questions = array();
// Treat the assessment title as a category title.
$this->process_category($xml, $questions);
// First step : we are only interested in the <item> tags.
$rawquestions = $this->getpath($xml,
array('questestinterop', '#', 'assessment', 0, '#', 'section', 0, '#', 'item'),
array(), false);
// Each <item> tag contains data related to a single question.
foreach ($rawquestions as $quest) {
// Second step : parse each question data into the intermediate
// rawquestion structure array.
// Warning : rawquestions are not Moodle questions.
$question = $this->create_raw_question($quest);
// Third step : convert a rawquestion into a Moodle question.
switch($question->qtype) {
case "Matching":
$this->process_matching($question, $questions);
break;
case "Multiple Choice":
$this->process_mc($question, $questions);
break;
case "Essay":
$this->process_essay($question, $questions);
break;
case "Multiple Answer":
$this->process_ma($question, $questions);
break;
case "True/False":
$this->process_tf($question, $questions);
break;
case 'Fill in the Blank':
$this->process_fblank($question, $questions);
break;
case 'Short Response':
$this->process_essay($question, $questions);
break;
default:
$this->error(get_string('unknownorunhandledtype', 'question', $question->qtype));
break;
}
}
return $questions;
}
/**
* Creates a cleaner object to deal with for processing into Moodle.
* The object returned is NOT a moodle question object.
* @param array $quest XML <item> question data
* @return object rawquestion
*/
public function create_raw_question($quest) {
$rawquestion = new stdClass();
$rawquestion->qtype = $this->getpath($quest,
array('#', 'itemmetadata', 0, '#', 'bbmd_questiontype', 0, '#'),
'', true);
$rawquestion->id = $this->getpath($quest,
array('#', 'itemmetadata', 0, '#', 'bbmd_asi_object_id', 0, '#'),
'', true);
$presentation = new stdClass();
$presentation->blocks = $this->getpath($quest,
array('#', 'presentation', 0, '#', 'flow', 0, '#', 'flow'),
array(), false);
foreach ($presentation->blocks as $pblock) {
$block = new stdClass();
$block->type = $this->getpath($pblock,
array('@', 'class'),
'', true);
switch($block->type) {
case 'QUESTION_BLOCK':
$subblocks = $this->getpath($pblock,
array('#', 'flow'),
array(), false);
foreach ($subblocks as $sblock) {
$this->process_block($sblock, $block);
}
break;
case 'RESPONSE_BLOCK':
$choices = null;
switch($rawquestion->qtype) {
case 'Matching':
$bbsubquestions = $this->getpath($pblock,
array('#', 'flow'),
array(), false);
foreach ($bbsubquestions as $bbsubquestion) {
$subquestion = new stdClass();
$subquestion->ident = $this->getpath($bbsubquestion,
array('#', 'response_lid', 0, '@', 'ident'),
'', true);
$this->process_block($this->getpath($bbsubquestion,
array('#', 'flow', 0),
false, false), $subquestion);
$bbchoices = $this->getpath($bbsubquestion,
array('#', 'response_lid', 0, '#', 'render_choice', 0,
'#', 'flow_label', 0, '#', 'response_label'),
array(), false);
$choices = array();
$this->process_choices($bbchoices, $choices);
$subquestion->choices = $choices;
if (!isset($block->subquestions)) {
$block->subquestions = array();
}
$block->subquestions[] = $subquestion;
}
break;
case 'Multiple Answer':
$bbchoices = $this->getpath($pblock,
array('#', 'response_lid', 0, '#', 'render_choice', 0, '#', 'flow_label'),
array(), false);
$choices = array();
$this->process_choices($bbchoices, $choices);
$block->choices = $choices;
break;
case 'Essay':
// Doesn't apply since the user responds with text input.
break;
case 'Multiple Choice':
$mcchoices = $this->getpath($pblock,
array('#', 'response_lid', 0, '#', 'render_choice', 0, '#', 'flow_label'),
array(), false);
foreach ($mcchoices as $mcchoice) {
$choices = new stdClass();
$choices = $this->process_block($mcchoice, $choices);
$block->choices[] = $choices;
}
break;
case 'Short Response':
// Do nothing?
break;
case 'Fill in the Blank':
// Do nothing?
break;
default:
$bbchoices = $this->getpath($pblock,
array('#', 'response_lid', 0, '#', 'render_choice', 0, '#',
'flow_label', 0, '#', 'response_label'),
array(), false);
$choices = array();
$this->process_choices($bbchoices, $choices);
$block->choices = $choices;
}
break;
case 'RIGHT_MATCH_BLOCK':
$matchinganswerset = $this->getpath($pblock,
array('#', 'flow'),
false, false);
$answerset = array();
foreach ($matchinganswerset as $answer) {
$bbanswer = new stdClass;
$bbanswer->text = $this->getpath($answer,
array('#', 'flow', 0, '#', 'material', 0, '#', 'mat_extension',
0, '#', 'mat_formattedtext', 0, '#'),
false, false);
$answerset[] = $bbanswer;
}
$block->matchinganswerset = $answerset;
break;
default:
$this->error(get_string('unhandledpresblock', 'qformat_blackboard_six'));
break;
}
$rawquestion->{$block->type} = $block;
}
// Determine response processing.
// There is a section called 'outcomes' that I don't know what to do with.
$resprocessing = $this->getpath($quest,
array('#', 'resprocessing'),
array(), false);
$respconditions = $this->getpath($resprocessing[0],
array('#', 'respcondition'),
array(), false);
$responses = array();
if ($rawquestion->qtype == 'Matching') {
$this->process_matching_responses($respconditions, $responses);
} else {
$this->process_responses($respconditions, $responses);
}
$rawquestion->responses = $responses;
$feedbackset = $this->getpath($quest,
array('#', 'itemfeedback'),
array(), false);
$feedbacks = array();
$this->process_feedback($feedbackset, $feedbacks);
$rawquestion->feedback = $feedbacks;
return $rawquestion;
}
/**
* Helper function to process an XML block into an object.
* Can call himself recursively if necessary to parse this branch of the XML tree.
* @param array $curblock XML block to parse
* @param object $block block already parsed so far
* @return object $block parsed
*/
public function process_block($curblock, $block) {
$curtype = $this->getpath($curblock,
array('@', 'class'),
'', true);
switch($curtype) {
case 'FORMATTED_TEXT_BLOCK':
$text = $this->getpath($curblock,
array('#', 'material', 0, '#', 'mat_extension', 0, '#', 'mat_formattedtext', 0, '#'),
'', true);
$block->text = $this->strip_applet_tags_get_mathml($text);
break;
case 'FILE_BLOCK':
$block->filename = $this->getpath($curblock,
array('#', 'material', 0, '#'),
'', true);
if ($block->filename != '') {
// TODO : determine what to do with the file's content.
$this->error(get_string('filenothandled', 'qformat_blackboard_six', $block->filename));
}
break;
case 'Block':
if ($this->getpath($curblock,
array('#', 'material', 0, '#', 'mattext'),
false, false)) {
$block->text = $this->getpath($curblock,
array('#', 'material', 0, '#', 'mattext', 0, '#'),
'', true);
} else if ($this->getpath($curblock,
array('#', 'material', 0, '#', 'mat_extension', 0, '#', 'mat_formattedtext'),
false, false)) {
$block->text = $this->getpath($curblock,
array('#', 'material', 0, '#', 'mat_extension', 0, '#', 'mat_formattedtext', 0, '#'),
'', true);
} else if ($this->getpath($curblock,
array('#', 'response_label'),
false, false)) {
// This is a response label block.
$subblocks = $this->getpath($curblock,
array('#', 'response_label', 0),
array(), false);
if (!isset($block->ident)) {
if ($this->getpath($subblocks,
array('@', 'ident'), '', true)) {
$block->ident = $this->getpath($subblocks,
array('@', 'ident'), '', true);
}
}
foreach ($this->getpath($subblocks,
array('#', 'flow_mat'), array(), false) as $subblock) {
$this->process_block($subblock, $block);
}
} else {
if ($this->getpath($curblock,
array('#', 'flow_mat'), false, false)
|| $this->getpath($curblock,
array('#', 'flow'), false, false)) {
if ($this->getpath($curblock,
array('#', 'flow_mat'), false, false)) {
$subblocks = $this->getpath($curblock,
array('#', 'flow_mat'), array(), false);
} else if ($this->getpath($curblock,
array('#', 'flow'), false, false)) {
$subblocks = $this->getpath($curblock,
array('#', 'flow'), array(), false);
}
foreach ($subblocks as $sblock) {
// This will recursively grab the sub blocks which should be of one of the other types.
$this->process_block($sblock, $block);
}
}
}
break;
case 'LINK_BLOCK':
// Not sure how this should be included?
$link = $this->getpath($curblock,
array('#', 'material', 0, '#', 'mattext', 0, '@', 'uri'), '', true);
if (!empty($link)) {
$block->link = $link;
} else {
$block->link = '';
}
break;
}
return $block;
}
/**
* Preprocess XML blocks containing data for questions' choices.
* Called by {@link create_raw_question()}
* for matching, multichoice and fill in the blank questions.
* @param array $bbchoices XML block to parse
* @param array $choices array of choices suitable for a rawquestion.
*/
protected function process_choices($bbchoices, &$choices) {
foreach ($bbchoices as $choice) {
if ($this->getpath($choice,
array('@', 'ident'), '', true)) {
$curchoice = $this->getpath($choice,
array('@', 'ident'), '', true);
} else { // For multiple answers.
$curchoice = $this->getpath($choice,
array('#', 'response_label', 0), array(), false);
}
if ($this->getpath($choice,
array('#', 'flow_mat', 0), false, false)) { // For multiple answers.
$curblock = $this->getpath($choice,
array('#', 'flow_mat', 0), false, false);
// Reset $curchoice to new stdClass because process_block is expecting an object
// for the second argument and not a string,
// which is what is was set as originally - CT 8/7/06.
$curchoice = new stdClass();
$this->process_block($curblock, $curchoice);
} else if ($this->getpath($choice,
array('#', 'response_label'), false, false)) {
// Reset $curchoice to new stdClass because process_block is expecting an object
// for the second argument and not a string,
// which is what is was set as originally - CT 8/7/06.
$curchoice = new stdClass();
$this->process_block($choice, $curchoice);
}
$choices[] = $curchoice;
}
}
/**
* Preprocess XML blocks containing data for subanswers
* Called by {@link create_raw_question()}
* for matching questions only.
* @param array $bbresponses XML block to parse
* @param array $responses array of responses suitable for a matching rawquestion.
*/
protected function process_matching_responses($bbresponses, &$responses) {
foreach ($bbresponses as $bbresponse) {
$response = new stdClass;
if ($this->getpath($bbresponse,
array('#', 'conditionvar', 0, '#', 'varequal'), false, false)) {
$response->correct = $this->getpath($bbresponse,
array('#', 'conditionvar', 0, '#', 'varequal', 0, '#'), '', true);
$response->ident = $this->getpath($bbresponse,
array('#', 'conditionvar', 0, '#', 'varequal', 0, '@', 'respident'), '', true);
}
// Suppressed an else block because if the above if condition is false,
// the question is not necessary a broken one, most of the time it's an <other> tag.
$response->feedback = $this->getpath($bbresponse,
array('#', 'displayfeedback', 0, '@', 'linkrefid'), '', true);
$responses[] = $response;
}
}
/**
* Preprocess XML blocks containing data for responses processing.
* Called by {@link create_raw_question()}
* for all questions types.
* @param array $bbresponses XML block to parse
* @param array $responses array of responses suitable for a rawquestion.
*/
protected function process_responses($bbresponses, &$responses) {
foreach ($bbresponses as $bbresponse) {
$response = new stdClass();
if ($this->getpath($bbresponse,
array('@', 'title'), '', true)) {
$response->title = $this->getpath($bbresponse,
array('@', 'title'), '', true);
} else {
$response->title = $this->getpath($bbresponse,
array('#', 'displayfeedback', 0, '@', 'linkrefid'), '', true);
}
$response->ident = array();
if ($this->getpath($bbresponse,
array('#', 'conditionvar', 0, '#'), false, false)) {
$response->ident[0] = $this->getpath($bbresponse,
array('#', 'conditionvar', 0, '#'), array(), false);
} else if ($this->getpath($bbresponse,
array('#', 'conditionvar', 0, '#', 'other', 0, '#'), false, false)) {
$response->ident[0] = $this->getpath($bbresponse,
array('#', 'conditionvar', 0, '#', 'other', 0, '#'), array(), false);
}
if ($this->getpath($bbresponse,
array('#', 'conditionvar', 0, '#', 'and'), false, false)) {
$responseset = $this->getpath($bbresponse,
array('#', 'conditionvar', 0, '#', 'and'), array(), false);
foreach ($responseset as $rs) {
$response->ident[] = $this->getpath($rs, array('#'), array(), false);
if (!isset($response->feedback) and $this->getpath($rs, array('@'), false, false)) {
$response->feedback = $this->getpath($rs,
array('@', 'respident'), '', true);
}
}
} else {
$response->feedback = $this->getpath($bbresponse,
array('#', 'displayfeedback', 0, '@', 'linkrefid'), '', true);
}
// Determine what fraction to give response.
if ($this->getpath($bbresponse,
array('#', 'setvar'), false, false)) {
switch ($this->getpath($bbresponse,
array('#', 'setvar', 0, '#'), false, false)) {
case "SCORE.max":
$response->fraction = 1;
break;
default:
// I have only seen this being 0 or unset.
// There are probably fractional values of SCORE.max, but I'm not sure what they look like.
$response->fraction = 0;
break;
}
} else {
// Just going to assume this is the case this is probably not correct.
$response->fraction = 0;
}
$responses[] = $response;
}
}
/**
* Preprocess XML blocks containing data for responses feedbacks.
* Called by {@link create_raw_question()}
* for all questions types.
* @param array $feedbackset XML block to parse
* @param array $feedbacks array of feedbacks suitable for a rawquestion.
*/
public function process_feedback($feedbackset, &$feedbacks) {
foreach ($feedbackset as $bbfeedback) {
$feedback = new stdClass();
$feedback->ident = $this->getpath($bbfeedback,
array('@', 'ident'), '', true);
$feedback->text = '';
if ($this->getpath($bbfeedback,
array('#', 'flow_mat', 0), false, false)) {
$this->process_block($this->getpath($bbfeedback,
array('#', 'flow_mat', 0), false, false), $feedback);
} else if ($this->getpath($bbfeedback,
array('#', 'solution', 0, '#', 'solutionmaterial', 0, '#', 'flow_mat', 0), false, false)) {
$this->process_block($this->getpath($bbfeedback,
array('#', 'solution', 0, '#', 'solutionmaterial', 0, '#', 'flow_mat', 0), false, false), $feedback);
}
$feedbacks[$feedback->ident] = $feedback;
}
}
/**
* Create common parts of question
* @param object $quest rawquestion
* @return object Moodle question.
*/
public function process_common($quest) {
$question = $this->defaultquestion();
$text = $quest->QUESTION_BLOCK->text;
$questiontext = $this->cleaned_text_field($text);
$question->questiontext = $questiontext['text'];
$question->questiontextformat = $questiontext['format']; // Needed because add_blank_combined_feedback uses it.
if (isset($questiontext['itemid'])) {
$question->questiontextitemid = $questiontext['itemid'];
}
$question->name = $this->create_default_question_name($question->questiontext,
get_string('defaultname', 'qformat_blackboard_six' , $quest->id));
$question->generalfeedback = '';
$question->generalfeedbackformat = FORMAT_HTML;
$question->generalfeedbackfiles = array();
return $question;
}
/**
* Process True / False Questions
* Parse a truefalse rawquestion and add the result
* to the array of questions already parsed.
* @param object $quest rawquestion
* @param array $questions array of Moodle questions already done
*/
protected function process_tf($quest, &$questions) {
$question = $this->process_common($quest);
$question->qtype = 'truefalse';
$question->single = 1; // Only one answer is allowed.
$question->penalty = 1; // Penalty = 1 for truefalse questions.
// 0th [response] is the correct answer.
$responses = $quest->responses;
$correctresponse = $this->getpath($responses[0]->ident[0],
array('varequal', 0, '#'), '', true);
if ($correctresponse != 'false') {
$correct = true;
} else {
$correct = false;
}
$fback = new stdClass();
foreach ($quest->feedback as $fb) {
$fback->{$fb->ident} = $fb->text;
}
if ($correct) { // True is correct.
$question->answer = 1;
$question->feedbacktrue = $this->cleaned_text_field($fback->correct);
$question->feedbackfalse = $this->cleaned_text_field($fback->incorrect);
} else { // False is correct.
$question->answer = 0;
$question->feedbacktrue = $this->cleaned_text_field($fback->incorrect);
$question->feedbackfalse = $this->cleaned_text_field($fback->correct);
}
$question->correctanswer = $question->answer;
$questions[] = $question;
}
/**
* Process Fill in the Blank Questions
* Parse a fillintheblank rawquestion and add the result
* to the array of questions already parsed.
* @param object $quest rawquestion
* @param array $questions array of Moodle questions already done.
*/
protected function process_fblank($quest, &$questions) {
$question = $this->process_common($quest);
$question->qtype = 'shortanswer';
$question->usecase = 0; // Ignore case.
$answers = array();
$fractions = array();
$feedbacks = array();
// Extract the feedback.
$feedback = array();
foreach ($quest->feedback as $fback) {
if (isset($fback->ident)) {
if ($fback->ident == 'correct' || $fback->ident == 'incorrect') {
$feedback[$fback->ident] = $fback->text;
}
}
}
foreach ($quest->responses as $response) {
if (isset($response->title)) {
if ($this->getpath($response->ident[0],
array('varequal', 0, '#'), false, false)) {
// For BB Fill in the Blank, only interested in correct answers.
if ($response->feedback = 'correct') {
$answers[] = $this->getpath($response->ident[0],
array('varequal', 0, '#'), '', true);
$fractions[] = 1;
if (isset($feedback['correct'])) {
$feedbacks[] = $this->cleaned_text_field($feedback['correct']);
} else {
$feedbacks[] = $this->text_field('');
}
}
}
}
}
// Adding catchall to so that students can see feedback for incorrect answers when they enter something,
// the instructor did not enter.
$answers[] = '*';
$fractions[] = 0;
if (isset($feedback['incorrect'])) {
$feedbacks[] = $this->cleaned_text_field($feedback['incorrect']);
} else {
$feedbacks[] = $this->text_field('');
}
$question->answer = $answers;
$question->fraction = $fractions;
$question->feedback = $feedbacks; // Changed to assign $feedbacks to $question->feedback instead of.
if (!empty($question)) {
$questions[] = $question;
}
}
/**
* Process Multichoice Questions
* Parse a multichoice single answer rawquestion and add the result
* to the array of questions already parsed.
* @param object $quest rawquestion
* @param array $questions array of Moodle questions already done.
*/
protected function process_mc($quest, &$questions) {
$question = $this->process_common($quest);
$question->qtype = 'multichoice';
$question = $this->add_blank_combined_feedback($question);
$question->single = 1;
$feedback = array();
foreach ($quest->feedback as $fback) {
$feedback[$fback->ident] = $fback->text;
}
foreach ($quest->responses as $response) {
if (isset($response->title)) {
if ($response->title == 'correct') {
// Only one answer possible for this qtype so first index is correct answer.
$correct = $this->getpath($response->ident[0],
array('varequal', 0, '#'), '', true);
}
} else {
// Fallback method for when the title is not set.
if ($response->feedback == 'correct') {
// Only one answer possible for this qtype so first index is correct answer.
$correct = $this->getpath($response->ident[0],
array('varequal', 0, '#'), '', true);
}
}
}
$i = 0;
foreach ($quest->RESPONSE_BLOCK->choices as $response) {
$question->answer[$i] = $this->cleaned_text_field($response->text);
if ($correct == $response->ident) {
$question->fraction[$i] = 1;
// This is a bit of a hack to catch the feedback... first we see if a 'specific'
// feedback for this response exists, then if a 'correct' feedback exists.
if (!empty($feedback[$response->ident]) ) {
$question->feedback[$i] = $this->cleaned_text_field($feedback[$response->ident]);
} else if (!empty($feedback['correct'])) {
$question->feedback[$i] = $this->cleaned_text_field($feedback['correct']);
} else if (!empty($feedback[$i])) {
$question->feedback[$i] = $this->cleaned_text_field($feedback[$i]);
} else {
$question->feedback[$i] = $this->cleaned_text_field(get_string('correct', 'question'));
}
} else {
$question->fraction[$i] = 0;
if (!empty($feedback[$response->ident]) ) {
$question->feedback[$i] = $this->cleaned_text_field($feedback[$response->ident]);
} else if (!empty($feedback['incorrect'])) {
$question->feedback[$i] = $this->cleaned_text_field($feedback['incorrect']);
} else if (!empty($feedback[$i])) {
$question->feedback[$i] = $this->cleaned_text_field($feedback[$i]);
} else {
$question->feedback[$i] = $this->cleaned_text_field(get_string('incorrect', 'question'));
}
}
$i++;
}
if (!empty($question)) {
$questions[] = $question;
}
}
/**
* Process Multiple Choice Questions With Multiple Answers.
* Parse a multichoice multianswer rawquestion and add the result
* to the array of questions already parsed.
* @param object $quest rawquestion
* @param array $questions array of Moodle questions already done.
*/
public function process_ma($quest, &$questions) {
$question = $this->process_common($quest);
$question->qtype = 'multichoice';
$question = $this->add_blank_combined_feedback($question);
$question->single = 0; // More than one answer allowed.
$answers = $quest->responses;
$correctanswers = array();
foreach ($answers as $answer) {
if ($answer->title == 'correct') {
$answerset = $this->getpath($answer->ident[0],
array('and', 0, '#', 'varequal'), array(), false);
foreach ($answerset as $ans) {
$correctanswers[] = $ans['#'];
}
}
}
$feedback = new stdClass();
foreach ($quest->feedback as $fb) {
$feedback->{$fb->ident} = trim($fb->text);
}
$correctanswercount = count($correctanswers);
$fraction = 1 / $correctanswercount;
$choiceset = $quest->RESPONSE_BLOCK->choices;
$i = 0;
foreach ($choiceset as $choice) {
$question->answer[$i] = $this->cleaned_text_field(trim($choice->text));
if (in_array($choice->ident, $correctanswers)) {
// Correct answer.
$question->fraction[$i] = $fraction;
$question->feedback[$i] = $this->cleaned_text_field($feedback->correct);
} else {
// Wrong answer.
$question->fraction[$i] = 0;
$question->feedback[$i] = $this->cleaned_text_field($feedback->incorrect);
}
$i++;
}
$questions[] = $question;
}
/**
* Process Essay Questions
* Parse an essay rawquestion and add the result
* to the array of questions already parsed.
* @param object $quest rawquestion
* @param array $questions array of Moodle questions already done.
*/
public function process_essay($quest, &$questions) {
$question = $this->process_common($quest);
$question->qtype = 'essay';
$question->feedback = array();
// Not sure where to get the correct answer from?
foreach ($quest->feedback as $feedback) {
// Added this code to put the possible solution that the
// instructor gives as the Moodle answer for an essay question.
if ($feedback->ident == 'solution') {
$question->graderinfo = $this->cleaned_text_field($feedback->text);
}
}
// Added because essay/questiontype.php:save_question_option is expecting a
// fraction property - CT 8/10/06.
$question->fraction[] = 1;
$question->defaultmark = 1;
$question->responseformat = 'editor';
$question->responserequired = 1;
$question->responsefieldlines = 15;
$question->attachments = 0;
$question->attachmentsrequired = 0;
$question->responsetemplate = $this->text_field('');
$questions[] = $question;
}
/**
* Process Matching Questions
* Parse a matching rawquestion and add the result
* to the array of questions already parsed.
* @param object $quest rawquestion
* @param array $questions array of Moodle questions already done.
*/
public function process_matching($quest, &$questions) {
// Blackboard matching questions can't be imported in core Moodle without a loss in data,
// as core match question don't allow HTML in subanswers. The contributed ddmatch
// question type support HTML in subanswers.
// The ddmatch question type is not part of core, so we need to check if it is defined.
$ddmatchisinstalled = question_bank::is_qtype_installed('ddmatch');
$question = $this->process_common($quest);
$question = $this->add_blank_combined_feedback($question);
$question->valid = true;
if ($ddmatchisinstalled) {
$question->qtype = 'ddmatch';
} else {
$question->qtype = 'match';
}
// Construction of the array holding mappings between subanswers and subquestions.
foreach ($quest->RESPONSE_BLOCK->subquestions as $qid => $subq) {
foreach ($quest->responses as $rid => $resp) {
if (isset($resp->ident) && $resp->ident == $subq->ident) {
$correct = $resp->correct;
}
}
foreach ($subq->choices as $cid => $choice) {
if ($choice == $correct) {
$mappings[$subq->ident] = $cid;
}
}
}
foreach ($subq->choices as $choiceid => $choice) {
$subanswertext = $quest->RIGHT_MATCH_BLOCK->matchinganswerset[$choiceid]->text;
if ($ddmatchisinstalled) {
$subanswer = $this->cleaned_text_field($subanswertext);
} else {
$subanswertext = html_to_text($this->cleaninput($subanswertext), 0);
$subanswer = $subanswertext;
}
if ($subanswertext != '') { // Only import non empty subanswers.
$subquestion = '';
$fiber = moodle_array_keys_filter($mappings, $choiceid);
foreach ($fiber as $correctanswerid) {
// We have found a correspondance for this subanswer so we need to take the associated subquestion.
foreach ($quest->RESPONSE_BLOCK->subquestions as $qid => $subq) {
$currentsubqid = $subq->ident;
if (strcmp ($currentsubqid, $correctanswerid) == 0) {
$subquestion = $subq->text;
break;
}
}
$question->subquestions[] = $this->cleaned_text_field($subquestion);
$question->subanswers[] = $subanswer;
}
if ($subquestion == '') { // Then in this case, $choice is a distractor.
$question->subquestions[] = $this->text_field('');
$question->subanswers[] = $subanswer;
}
}
}
// Verify that this matching question has enough subquestions and subanswers.
$subquestioncount = 0;
$subanswercount = 0;
$subanswers = $question->subanswers;
foreach ($question->subquestions as $key => $subquestion) {
$subquestion = $subquestion['text'];
$subanswer = $subanswers[$key];
if ($subquestion != '') {
$subquestioncount++;
}
$subanswercount++;
}
if ($subquestioncount < 2 || $subanswercount < 3) {
$this->error(get_string('notenoughtsubans', 'qformat_blackboard_six', $question->questiontext));
} else {
$questions[] = $question;
}
}
/**
* Add a category question entry based on the assessment title
* @param array $xml the xml tree
* @param array $questions the questions already parsed
*/
public function process_category($xml, &$questions) {
$title = $this->getpath($xml, array('questestinterop', '#', 'assessment', 0, '@', 'title'), '', true);
$dummyquestion = new stdClass();
$dummyquestion->qtype = 'category';
$dummyquestion->category = $this->cleaninput($this->clean_question_name($title));
$questions[] = $dummyquestion;
}
/**
* Strip the applet tag used by Blackboard to render mathml formulas,
* keeping the mathml tag.
* @param string $string
* @return string
*/
public function strip_applet_tags_get_mathml($string) {
if (stristr($string, '</APPLET>') === false) {
return $string;
} else {
// Strip all applet tags keeping stuff before/after and inbetween (if mathml) them.
while (stristr($string, '</APPLET>') !== false) {
preg_match("/(.*)\<applet.*value=\"(\<math\>.*\<\/math\>)\".*\<\/applet\>(.*)/i", $string, $mathmls);
$string = $mathmls[1].$mathmls[2].$mathmls[3];
}
return $string;
}
}
}
@@ -0,0 +1,35 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for component 'qformat_blackboard_six', language 'en', branch 'MOODLE_20_STABLE'
*
* @package qformat_blackboard_six
* @copyright 2010 Helen Foster
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['defaultname'] = 'Imported question {$a}';
$string['errormanifest'] = 'Error while parsing the IMS manifest document';
$string['importnotext'] = 'Missing question text in XML file';
$string['filenothandled'] = 'This archive contains reference to a file material {$a} which is not currently handled by import';
$string['imagenotfound'] = 'Image file with path {$a} was not found in the import.';
$string['importedcategory'] = 'Imported category {$a}';
$string['notenoughtsubans'] = 'Unable to import matching question \'{$a}\' because a matching question must comprise at least two questions and three answers.';
$string['pluginname'] = 'Blackboard';
$string['pluginname_help'] = 'Blackboard format enables questions saved in all Blackboard export formats to be imported via a dat or zip file. For zip files, images import is supported.';
$string['privacy:metadata'] = 'The Blackbard question format plugin does not store any personal data.';
$string['unhandledpresblock'] = 'Unhandled presentation block';
@@ -0,0 +1,334 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace qformat_blackboard_six;
use qformat_blackboard_six;
use qformat_blackboard_six_file;
use question_bank;
use question_check_specified_fields_expectation;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/question/format.php');
require_once($CFG->dirroot . '/question/format/blackboard_six/format.php');
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
/**
* Unit tests for the blackboard v6+ question import format.
*
* @package qformat_blackboard_six
* @copyright 2012 Jean-Michel Vedrine
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class blackboardformatpool_test extends \question_testcase {
public function make_test_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(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[5];
// If qtype_ddmatch is installed, the formatter produces ddmatch
// qtypes, not match ones.
$ddmatchisinstalled = question_bank::is_qtype_installed('ddmatch');
$expectedq = new \stdClass();
$expectedq->qtype = $ddmatchisinstalled ? 'ddmatch' : 'match';
$expectedq->name = 'Classify the animals.';
$expectedq->questiontext = '<i>Classify the animals.</i>';
$expectedq->questiontextformat = FORMAT_HTML;
$expectedq->correctfeedback = array('text' => '',
'format' => FORMAT_HTML);
$expectedq->partiallycorrectfeedback = array('text' => '',
'format' => FORMAT_HTML);
$expectedq->incorrectfeedback = array('text' => '',
'format' => FORMAT_HTML);
$expectedq->generalfeedback = '';
$expectedq->generalfeedbackformat = FORMAT_HTML;
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->penalty = 0.3333333;
$expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
$expectedq->subquestions = array(
array('text' => 'cat', 'format' => FORMAT_HTML),
array('text' => '', 'format' => FORMAT_HTML),
array('text' => 'frog', 'format' => FORMAT_HTML),
array('text' => 'newt', 'format' => FORMAT_HTML));
if ($ddmatchisinstalled) {
$expectedq->subanswers = array(
array('text' => 'mammal', 'format' => FORMAT_HTML),
array('text' => 'insect', 'format' => FORMAT_HTML),
array('text' => 'amphibian', 'format' => FORMAT_HTML),
array('text' => 'amphibian', 'format' => FORMAT_HTML),
);
} else {
$expectedq->subanswers = array('mammal', 'insect', 'amphibian', 'amphibian');
}
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
public function test_import_multichoice_single(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[2];
$expectedq = new \stdClass();
$expectedq->qtype = 'multichoice';
$expectedq->single = 1;
$expectedq->name = 'What\'s between orange and green in the spectrum?';
$expectedq->questiontext = '<span style="font-size:12pt">What\'s between orange and green in the spectrum?</span>';
$expectedq->questiontextformat = FORMAT_HTML;
$expectedq->correctfeedback = array('text' => 'You gave the right answer.',
'format' => FORMAT_HTML);
$expectedq->partiallycorrectfeedback = array('text' => '',
'format' => FORMAT_HTML);
$expectedq->incorrectfeedback = array('text' => 'Only yellow is between orange and green in the spectrum.',
'format' => FORMAT_HTML);
$expectedq->generalfeedback = '';
$expectedq->generalfeedbackformat = FORMAT_HTML;
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->penalty = 0.3333333;
$expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
$expectedq->answer = array(
0 => array(
'text' => '<span style="font-size:12pt">red</span>',
'format' => FORMAT_HTML,
),
1 => array(
'text' => '<span style="font-size:12pt">yellow</span>',
'format' => FORMAT_HTML,
),
2 => array(
'text' => '<span style="font-size:12pt">blue</span>',
'format' => FORMAT_HTML,
)
);
$expectedq->fraction = array(0, 1, 0);
$expectedq->feedback = array(
0 => array(
'text' => '',
'format' => FORMAT_HTML,
),
1 => array(
'text' => '',
'format' => FORMAT_HTML,
),
2 => array(
'text' => '',
'format' => FORMAT_HTML,
)
);
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
public function test_import_multichoice_multi(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[3];
$expectedq = new \stdClass();
$expectedq->qtype = 'multichoice';
$expectedq->single = 0;
$expectedq->name = 'What\'s between orange and green in the spectrum?';
$expectedq->questiontext = '<span style="font-size:12pt">What\'s between orange and green in the spectrum?</span>';
$expectedq->questiontextformat = FORMAT_HTML;
$expectedq->correctfeedback = array(
'text' => 'You gave the right answer.',
'format' => FORMAT_HTML,
);
$expectedq->partiallycorrectfeedback = array(
'text' => 'Only yellow and off-beige are between orange and green in the spectrum.',
'format' => FORMAT_HTML,
);
$expectedq->incorrectfeedback = array(
'text' => 'Only yellow and off-beige are between orange and green in the spectrum.',
'format' => FORMAT_HTML,
);
$expectedq->generalfeedback = '';
$expectedq->generalfeedbackformat = FORMAT_HTML;
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->penalty = 0.3333333;
$expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
$expectedq->answer = array(
0 => array(
'text' => '<span style="font-size:12pt">yellow</span>',
'format' => FORMAT_HTML,
),
1 => array(
'text' => '<span style="font-size:12pt">red</span>',
'format' => FORMAT_HTML,
),
2 => array(
'text' => '<span style="font-size:12pt">off-beige</span>',
'format' => FORMAT_HTML,
),
3 => array(
'text' => '<span style="font-size:12pt">blue</span>',
'format' => FORMAT_HTML,
)
);
$expectedq->fraction = array(0.5, 0, 0.5, 0);
$expectedq->feedback = array(
0 => array(
'text' => '',
'format' => FORMAT_HTML,
),
1 => array(
'text' => '',
'format' => FORMAT_HTML,
),
2 => array(
'text' => '',
'format' => FORMAT_HTML,
),
3 => array(
'text' => '',
'format' => FORMAT_HTML,
)
);
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
public function test_import_truefalse(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[1];
$expectedq = new \stdClass();
$expectedq->qtype = 'truefalse';
$expectedq->name = '42 is the Absolute Answer to everything.';
$expectedq->questiontext = '<span style="font-size:12pt">42 is the Absolute Answer to everything.</span>';
$expectedq->questiontextformat = FORMAT_HTML;
$expectedq->generalfeedback = '';
$expectedq->generalfeedbackformat = FORMAT_HTML;
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->correctanswer = 0;
$expectedq->feedbacktrue = array(
'text' => '42 is the Ultimate Answer.',
'format' => FORMAT_HTML,
);
$expectedq->feedbackfalse = array(
'text' => 'You gave the right answer.',
'format' => FORMAT_HTML,
);
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
public function test_import_fill_in_the_blank(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[4];
$expectedq = new \stdClass();
$expectedq->qtype = 'shortanswer';
$expectedq->name = 'Name an amphibian: __________.';
$expectedq->questiontext = '<span style="font-size:12pt">Name an amphibian: __________.</span>';
$expectedq->questiontextformat = FORMAT_HTML;
$expectedq->generalfeedback = '';
$expectedq->generalfeedbackformat = FORMAT_HTML;
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->usecase = 0;
$expectedq->answer = array('frog', '*');
$expectedq->fraction = array(1, 0);
$expectedq->feedback = array(
0 => array(
'text' => '',
'format' => FORMAT_HTML,
),
1 => array(
'text' => '',
'format' => FORMAT_HTML,
)
);
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
public function test_import_essay(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[6];
$expectedq = new \stdClass();
$expectedq->qtype = 'essay';
$expectedq->name = 'How are you?';
$expectedq->questiontext = 'How are you?';
$expectedq->questiontextformat = FORMAT_HTML;
$expectedq->generalfeedback = '';
$expectedq->generalfeedbackformat = FORMAT_HTML;
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->responseformat = 'editor';
$expectedq->responsefieldlines = 15;
$expectedq->attachments = 0;
$expectedq->graderinfo = array(
'text' => 'Blackboard answer for essay questions will be imported as informations for graders.',
'format' => FORMAT_HTML,
);
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
public function test_import_category(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[0];
$expectedq = new \stdClass();
$expectedq->qtype = 'category';
$expectedq->category = 'exam 3 2008-9';
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
}
@@ -0,0 +1,334 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace qformat_blackboard_six;
use qformat_blackboard_six;
use qformat_blackboard_six_file;
use question_bank;
use question_check_specified_fields_expectation;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/question/format.php');
require_once($CFG->dirroot . '/question/format/blackboard_six/format.php');
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
/**
* Unit tests for the blackboard v6+ question import format.
*
* @package qformat_blackboard_six
* @copyright 2012 Jean-Michel Vedrine
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class blackboardsixformatqti_test extends \question_testcase {
public function make_test_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(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[4];
// If qtype_ddmatch is installed, the formatter produces ddmatch
// qtypes, not match ones.
$ddmatchisinstalled = question_bank::is_qtype_installed('ddmatch');
$expectedq = new \stdClass();
$expectedq->qtype = $ddmatchisinstalled ? 'ddmatch' : 'match';
$expectedq->name = 'Classify the animals.';
$expectedq->questiontext = 'Classify the animals.';
$expectedq->questiontextformat = FORMAT_HTML;
$expectedq->correctfeedback = array('text' => '',
'format' => FORMAT_HTML, 'files' => array());
$expectedq->partiallycorrectfeedback = array('text' => '',
'format' => FORMAT_HTML, 'files' => array());
$expectedq->incorrectfeedback = array('text' => '',
'format' => FORMAT_HTML, 'files' => array());
$expectedq->generalfeedback = '';
$expectedq->generalfeedbackformat = FORMAT_HTML;
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->penalty = 0.3333333;
$expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
$expectedq->subquestions = array(
array('text' => '', 'format' => FORMAT_HTML),
array('text' => 'cat', 'format' => FORMAT_HTML),
array('text' => 'frog', 'format' => FORMAT_HTML),
array('text' => 'newt', 'format' => FORMAT_HTML));
if ($ddmatchisinstalled) {
$expectedq->subanswers = array(
array('text' => 'insect', 'format' => FORMAT_HTML),
array('text' => 'mammal', 'format' => FORMAT_HTML),
array('text' => 'amphibian', 'format' => FORMAT_HTML),
array('text' => 'amphibian', 'format' => FORMAT_HTML),
);
} else {
$expectedq->subanswers = array('insect', 'mammal', 'amphibian', 'amphibian');
}
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
public function test_import_multichoice_single(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[2];
$expectedq = new \stdClass();
$expectedq->qtype = 'multichoice';
$expectedq->single = 1;
$expectedq->name = 'What\'s between orange and green in the spectrum?';
$expectedq->questiontext = '<span style="font-size:12pt">What\'s between orange and green in the spectrum?</span>';
$expectedq->questiontextformat = FORMAT_HTML;
$expectedq->correctfeedback = array('text' => '',
'format' => FORMAT_HTML, 'files' => array());
$expectedq->partiallycorrectfeedback = array('text' => '',
'format' => FORMAT_HTML, 'files' => array());
$expectedq->incorrectfeedback = array('text' => '',
'format' => FORMAT_HTML, 'files' => array());
$expectedq->generalfeedback = '';
$expectedq->generalfeedbackformat = FORMAT_HTML;
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->penalty = 0.3333333;
$expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
$expectedq->answer = array(
0 => array(
'text' => '<span style="font-size:12pt">red</span>',
'format' => FORMAT_HTML,
),
1 => array(
'text' => '<span style="font-size:12pt">yellow</span>',
'format' => FORMAT_HTML,
),
2 => array(
'text' => '<span style="font-size:12pt">blue</span>',
'format' => FORMAT_HTML,
)
);
$expectedq->fraction = array(0, 1, 0);
$expectedq->feedback = array(
0 => array(
'text' => 'Red is not between orange and green in the spectrum but yellow is.',
'format' => FORMAT_HTML,
),
1 => array(
'text' => 'You gave the right answer.',
'format' => FORMAT_HTML,
),
2 => array(
'text' => 'Blue is not between orange and green in the spectrum but yellow is.',
'format' => FORMAT_HTML,
)
);
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
public function test_import_multichoice_multi(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[3];
$expectedq = new \stdClass();
$expectedq->qtype = 'multichoice';
$expectedq->single = 0;
$expectedq->name = 'What\'s between orange and green in the spectrum?';
$expectedq->questiontext = '<i>What\'s between orange and green in the spectrum?</i>';
$expectedq->questiontextformat = FORMAT_HTML;
$expectedq->correctfeedback = array(
'text' => '',
'format' => FORMAT_HTML,
'files' => array(),
);
$expectedq->partiallycorrectfeedback = array(
'text' => '',
'format' => FORMAT_HTML,
'files' => array(),
);
$expectedq->incorrectfeedback = array(
'text' => '',
'format' => FORMAT_HTML,
'files' => array(),
);
$expectedq->generalfeedback = '';
$expectedq->generalfeedbackformat = FORMAT_HTML;
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->penalty = 0.3333333;
$expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
$expectedq->answer = array(
0 => array(
'text' => '<span style="font-size:12pt">yellow</span>',
'format' => FORMAT_HTML,
),
1 => array(
'text' => '<span style="font-size:12pt">red</span>',
'format' => FORMAT_HTML,
),
2 => array(
'text' => '<span style="font-size:12pt">off-beige</span>',
'format' => FORMAT_HTML,
),
3 => array(
'text' => '<span style="font-size:12pt">blue</span>',
'format' => FORMAT_HTML,
)
);
$expectedq->fraction = array(0.5, 0, 0.5, 0);
$expectedq->feedback = array(
0 => array(
'text' => '',
'format' => FORMAT_HTML,
),
1 => array(
'text' => '',
'format' => FORMAT_HTML,
),
2 => array(
'text' => '',
'format' => FORMAT_HTML,
),
3 => array(
'text' => '',
'format' => FORMAT_HTML,
)
);
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
public function test_import_truefalse(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[1];
$expectedq = new \stdClass();
$expectedq->qtype = 'truefalse';
$expectedq->name = '42 is the Absolute Answer to everything.';
$expectedq->questiontext = '<span style="font-size:12pt">42 is the Absolute Answer to everything.</span>';
$expectedq->questiontextformat = FORMAT_HTML;
$expectedq->generalfeedback = '';
$expectedq->generalfeedbackformat = FORMAT_HTML;
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->correctanswer = 0;
$expectedq->feedbacktrue = array(
'text' => '42 is the <b>Ultimate</b> Answer.',
'format' => FORMAT_HTML,
);
$expectedq->feedbackfalse = array(
'text' => 'You gave the right answer.',
'format' => FORMAT_HTML,
);
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
public function test_import_fill_in_the_blank(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[5];
$expectedq = new \stdClass();
$expectedq->qtype = 'shortanswer';
$expectedq->name = 'Name an amphibian: __________.';
$expectedq->questiontext = '<span style="font-size:12pt">Name an amphibian: __________.</span>';
$expectedq->questiontextformat = FORMAT_HTML;
$expectedq->generalfeedback = '';
$expectedq->generalfeedbackformat = FORMAT_HTML;
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->usecase = 0;
$expectedq->answer = array('frog', '*');
$expectedq->fraction = array(1, 0);
$expectedq->feedback = array(
0 => array(
'text' => 'A frog is an amphibian.',
'format' => FORMAT_HTML,
),
1 => array(
'text' => 'A frog is an amphibian.',
'format' => FORMAT_HTML,
)
);
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
public function test_import_essay(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[6];
$expectedq = new \stdClass();
$expectedq->qtype = 'essay';
$expectedq->name = 'How are you?';
$expectedq->questiontext = 'How are you?';
$expectedq->questiontextformat = FORMAT_HTML;
$expectedq->generalfeedback = '';
$expectedq->generalfeedbackformat = FORMAT_HTML;
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->responseformat = 'editor';
$expectedq->responsefieldlines = 15;
$expectedq->attachments = 0;
$expectedq->graderinfo = array(
'text' => 'Blackboard answer for essay questions will be imported as informations for graders.',
'format' => FORMAT_HTML,
);
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
public function test_import_category(): void {
$xml = $this->make_test_xml();
$importer = new qformat_blackboard_six();
$questions = $importer->readquestions($xml);
$q = $questions[0];
$expectedq = new \stdClass();
$expectedq->qtype = 'category';
$expectedq->category = 'sample_blackboard_six';
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}
}
@@ -0,0 +1,142 @@
<?xml version='1.0' encoding='utf-8'?>
<POOL>
<TITLE value='exam 3 2008-9'/>
<QUESTIONLIST>
<QUESTION id='q1' class='QUESTION_TRUEFALSE' points='1'/>
<QUESTION id='q7' class='QUESTION_MULTIPLECHOICE' points='1'/>
<QUESTION id='q8' class='QUESTION_MULTIPLEANSWER' points='1'/>
<QUESTION id='q39-44' class='QUESTION_MATCH' points='1'/>
<QUESTION id='q9' class='QUESTION_ESSAY' points='1'/>
<QUESTION id='q27' class='QUESTION_FILLINBLANK' points='1'/>
</QUESTIONLIST>
<QUESTION_TRUEFALSE id='q1'>
<BODY>
<TEXT><![CDATA[<span style="font-size:12pt">42 is the Absolute Answer to everything.</span>]]></TEXT>
<FLAGS>
<ISHTML value='true'/>
<ISNEWLINELITERAL value='false'/>
</FLAGS>
</BODY>
<ANSWER id='q1_a1'>
<TEXT>False</TEXT>
</ANSWER>
<ANSWER id='q1_a2'>
<TEXT>True</TEXT>
</ANSWER>
<GRADABLE>
<CORRECTANSWER answer_id='q1_a2'/>
<FEEDBACK_WHEN_CORRECT><![CDATA[You gave the right answer.]]></FEEDBACK_WHEN_CORRECT>
<FEEDBACK_WHEN_INCORRECT><![CDATA[42 is the Ultimate Answer.]]></FEEDBACK_WHEN_INCORRECT>
</GRADABLE>
</QUESTION_TRUEFALSE>
<QUESTION_MULTIPLECHOICE id='q7'>
<BODY>
<TEXT><![CDATA[<span style="font-size:12pt">What's between orange and green in the spectrum?</span>]]></TEXT>
<FLAGS>
<ISHTML value='true'/>
<ISNEWLINELITERAL value='false'/>
</FLAGS>
</BODY>
<ANSWER id='q7_a1' position='1'>
<TEXT><![CDATA[<span style="font-size:12pt">red</span>]]></TEXT>
</ANSWER>
<ANSWER id='q7_a2' position='2'>
<TEXT><![CDATA[<span style="font-size:12pt">yellow</span>]]></TEXT>
</ANSWER>
<ANSWER id='q7_a3' position='3'>
<TEXT><![CDATA[<span style="font-size:12pt">blue</span>]]></TEXT>
</ANSWER>
<GRADABLE>
<CORRECTANSWER answer_id='q7_a2'/>
<FEEDBACK_WHEN_CORRECT><![CDATA[You gave the right answer.]]></FEEDBACK_WHEN_CORRECT>
<FEEDBACK_WHEN_INCORRECT><![CDATA[Only yellow is between orange and green in the spectrum.]]></FEEDBACK_WHEN_INCORRECT>
</GRADABLE>
</QUESTION_MULTIPLECHOICE>
<QUESTION_MULTIPLEANSWER id='q8'>
<BODY>
<TEXT><![CDATA[<span style="font-size:12pt">What's between orange and green in the spectrum?</span>]]></TEXT>
<FLAGS>
<ISHTML value='true'/>
<ISNEWLINELITERAL value='false'/>
</FLAGS>
</BODY>
<ANSWER id='q8_a1' position='1'>
<TEXT><![CDATA[<span style="font-size:12pt">yellow</span>]]></TEXT>
</ANSWER>
<ANSWER id='q8_a2' position='2'>
<TEXT><![CDATA[<span style="font-size:12pt">red</span>]]></TEXT>
</ANSWER>
<ANSWER id='q8_a3' position='3'>
<TEXT><![CDATA[<span style="font-size:12pt">off-beige</span>]]></TEXT>
</ANSWER>
<ANSWER id='q8_a4' position='4'>
<TEXT><![CDATA[<span style="font-size:12pt">blue</span>]]></TEXT>
</ANSWER>
<GRADABLE>
<CORRECTANSWER answer_id='q8_a1'/>
<CORRECTANSWER answer_id='q8_a3'/>
<FEEDBACK_WHEN_CORRECT><![CDATA[You gave the right answer.]]></FEEDBACK_WHEN_CORRECT>
<FEEDBACK_WHEN_INCORRECT><![CDATA[Only yellow and off-beige are between orange and green in the spectrum.]]></FEEDBACK_WHEN_INCORRECT>
</GRADABLE>
</QUESTION_MULTIPLEANSWER>
<QUESTION_MATCH id='q39-44'>
<BODY>
<TEXT><![CDATA[<i>Classify the animals.</i>]]></TEXT>
<FLAGS>
<ISHTML value='true'/>
<ISNEWLINELITERAL value='false'/>
</FLAGS>
</BODY>
<ANSWER id='q39-44_a1' position='1'>
<TEXT><![CDATA[frog]]></TEXT>
</ANSWER>
<ANSWER id='q39-44_a2' position='2'>
<TEXT><![CDATA[cat]]></TEXT>
</ANSWER>
<ANSWER id='q39-44_a3' position='3'>
<TEXT><![CDATA[newt]]></TEXT>
</ANSWER>
<CHOICE id='q39-44_c1' position='1'>
<TEXT><![CDATA[mammal]]></TEXT>
</CHOICE>
<CHOICE id='q39-44_c2' position='2'>
<TEXT><![CDATA[insect]]></TEXT>
</CHOICE>
<CHOICE id='q39-44_c3' position='3'>
<TEXT><![CDATA[amphibian]]></TEXT>
</CHOICE>
<GRADABLE>
<CORRECTANSWER answer_id='q39-44_a1' choice_id='q39-44_c3'/>
<CORRECTANSWER answer_id='q39-44_a2' choice_id='q39-44_c1'/>
<CORRECTANSWER answer_id='q39-44_a3' choice_id='q39-44_c3'/>
</GRADABLE>
</QUESTION_MATCH>
<QUESTION_ESSAY id='q9'>
<BODY>
<TEXT><![CDATA[How are you?]]></TEXT>
<FLAGS>
<ISHTML value='true'/>
<ISNEWLINELITERAL value='false'/>
</FLAGS>
</BODY>
<ANSWER id='q9_a1'>
<TEXT><![CDATA[Blackboard answer for essay questions will be imported as informations for graders.]]></TEXT>
</ANSWER>
<GRADABLE>
</GRADABLE>
</QUESTION_ESSAY>
<QUESTION_FILLINBLANK id='q27'>
<BODY>
<TEXT><![CDATA[<span style="font-size:12pt">Name an amphibian: __________.</span>]]></TEXT>
<FLAGS>
<ISHTML value='true'/>
<ISNEWLINELITERAL value='false'/>
</FLAGS>
</BODY>
<ANSWER id='q27_a1' position='1'>
<TEXT>frog</TEXT>
</ANSWER>
<GRADABLE>
</GRADABLE>
</QUESTION_FILLINBLANK>
</POOL>
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,32 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Version information for the blackboard_six question import format.
*
* @package qformat_blackboard_six
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'qformat_blackboard_six';
$plugin->version = 2024042200;
$plugin->requires = 2024041600;
$plugin->maturity = MATURITY_STABLE;