dirroot/question/format/qti2/qt_common.php");
////////////////////////////////////////////////////////////////////////////
/// IMS QTI 2.0 FORMAT
///
/// HISTORY: created 28.01.2005 brian@mediagonal.ch
////////////////////////////////////////////////////////////////////////////
// Based on format.php, included by ../../import.php
/**
* @package questionbank
* @subpackage importexport
*/
define('CLOZE_TRAILING_TEXT_ID', 9999999);
class qformat_qti2 extends qformat_default {
var $lang;
function provide_export() {
return true;
}
function indent_xhtml($source, $indenter = ' ') {
// xml tidier-upper
// (c) Ari Koivula http://ventionline.com
// Remove all pre-existing formatting.
// Remove all newlines.
$source = str_replace("\n", '', $source);
$source = str_replace("\r", '', $source);
// Remove all tabs.
$source = str_replace("\t", '', $source);
// Remove all space after ">" and before "<".
$source = ereg_replace(">( )*", ">", $source);
$source = ereg_replace("( )*<", "<", $source);
// Iterate through the source.
$level = 0;
$source_len = strlen($source);
$pt = 0;
while ($pt < $source_len) {
if ($source{$pt} === '<') {
// We have entered a tag.
// Remember the point where the tag starts.
$started_at = $pt;
$tag_level = 1;
// If the second letter of the tag is "/", assume its an ending tag.
if ($source{$pt+1} === '/') {
$tag_level = -1;
}
// If the second letter of the tag is "!", assume its an "invisible" tag.
if ($source{$pt+1} === '!') {
$tag_level = 0;
}
// Iterate throught the source until the end of tag.
while ($source{$pt} !== '>') {
$pt++;
}
// If the second last letter is "/", assume its a self ending tag.
if ($source{$pt-1} === '/') {
$tag_level = 0;
}
$tag_lenght = $pt+1-$started_at;
// Decide the level of indention for this tag.
// If this was an ending tag, decrease indent level for this tag..
if ($tag_level === -1) {
$level--;
}
// Place the tag in an array with proper indention.
$array[] = str_repeat($indenter, $level).substr($source, $started_at, $tag_lenght);
// If this was a starting tag, increase the indent level after this tag.
if ($tag_level === 1) {
$level++;
}
// if it was a self closing tag, dont do anything.
}
// Were out of the tag.
// If next letter exists...
if (($pt+1) < $source_len) {
// ... and its not an "<".
if ($source{$pt+1} !== '<') {
$started_at = $pt+1;
// Iterate through the source until the start of new tag or until we reach the end of file.
while ($source{$pt} !== '<' && $pt < $source_len) {
$pt++;
}
// If we found a "<" (we didnt find the end of file)
if ($source{$pt} === '<') {
$tag_lenght = $pt-$started_at;
// Place the stuff in an array with proper indention.
$array[] = str_repeat($indenter, $level).substr($source, $started_at, $tag_lenght);
}
// If the next tag is "<", just advance pointer and let the tag indenter take care of it.
} else {
$pt++;
}
// If the next letter doesnt exist... Were done... well, almost..
} else {
break;
}
}
// Replace old source with the new one we just collected into our array.
$source = implode($array, "\n");
return $source;
}
function importpreprocess() {
global $CFG;
error("Sorry, importing this format is not yet implemented!",
"$CFG->wwwroot/mod/quiz/import.php?category=$category->id");
}
function exportpreprocess() {
global $CFG;
require_once("{$CFG->libdir}/smarty/Smarty.class.php");
// assign the language for the export: by parameter, SESSION, USER, or the default of 'en'
$lang = current_language();
$this->lang = $lang;
return parent::exportpreprocess();
}
function export_file_extension() {
// override default type so extension is .xml
return ".zip";
}
function get_qtype( $type_id ) {
// translates question type code number into actual name
switch( $type_id ) {
case TRUEFALSE:
$name = 'truefalse';
break;
case MULTICHOICE:
$name = 'multichoice';
break;
case SHORTANSWER:
$name = 'shortanswer';
break;
case NUMERICAL:
$name = 'numerical';
break;
case MATCH:
$name = 'matching';
break;
case DESCRIPTION:
$name = 'description';
break;
case MULTIANSWER:
$name = 'multianswer';
break;
default:
$name = 'Unknown';
}
return $name;
}
function writetext( $raw ) {
// generates
", $result));
}
$manifestquestions = $this->objects_to_array($questions);
$manifestid = str_replace(array(':', '/'), array('-','_'), "question_category_{$this->category->id}---{$CFG->wwwroot}");
$smarty->assign('externalfiles', 1);
$smarty->assign('manifestidentifier', $manifestid);
$smarty->assign('quiztitle', "question_category_{$this->category->id}");
$smarty->assign('quizinfo', "All questions in category {$this->category->id}");
$smarty->assign('questions', $manifestquestions);
$smarty->assign('lang', $this->lang);
$smarty->error_reporting = 99;
$expout = $smarty->fetch('imsmanifest.tpl');
$filepath = $path.'/imsmanifest.xml';
if (empty($expout)) {
error("Unkown error - empty imsmanifest.xml");
}
if (!$fh=fopen($filepath,"w")) {
error("Cannot open for writing: $filepath");
}
if (!fwrite($fh, $expout)) {
error("Cannot write exported questions to $filepath");
}
fclose($fh);
// iterate through questions
foreach($questions as $question) {
// results are first written into string (and then to a file)
$count++;
echo "
$count. ".stripslashes($question->questiontext)."
"; $expout = $this->writequestion( $question , null, true, $path) . "\n"; $expout = $this->presave_process( $expout ); $filepath = $path.'/'.$this->get_assesment_item_id($question) . ".xml"; if (!$fh=fopen($filepath,"w")) { error("Cannot open for writing: $filepath"); } if (!fwrite($fh, $expout)) { error("Cannot write exported questions to $filepath"); } fclose($fh); } // zip files into single export file zip_files( array($path), "$path.zip" ); // remove the temporary directory remove_dir( $path ); return true; } /** * exports a quiz (as opposed to exporting a category of questions) * * The parent class method was overridden because the IMS export consists of multiple files * * @param object $quiz * @param array $questions - an array of question objects * @param object $result - if set, contains result of calling quiz_grade_responses() * @param string $redirect - a URL to redirect to in case of failure * @param string $submiturl - the URL for the qti player to send the results to (e.g. attempt.php) * @todo use $result in the ouput */ function export_quiz($course, $quiz, $questions, $result, $redirect, $submiturl = null) { $this->xml_entitize($course); $this->xml_entitize($quiz); $this->xml_entitize($questions); $this->xml_entitize($result); $this->xml_entitize($submiturl); if (! $this->exportpreprocess(0, $course)) { // Do anything before that we need to error("Error occurred during pre-processing!", $redirect); } if (! $this->exportprocess_quiz($quiz, $questions, $result, $submiturl, $course)) { // Process the export data error("Error occurred during processing!", $redirect); } if (! $this->exportpostprocess()) { // In case anything needs to be done after error("Error occurred during post-processing!", $redirect); } } /** * This function is called to export a quiz (as opposed to exporting a category of questions) * * @uses $USER * @param object $quiz * @param array $questions - an array of question objects * @param object $result - if set, contains result of calling quiz_grade_responses() * @todo use $result in the ouput */ function exportprocess_quiz($quiz, $questions, $result, $submiturl, $course) { global $USER; global $CFG; $gradingmethod = array (1 => 'GRADEHIGHEST', 2 => 'GRADEAVERAGE', 3 => 'ATTEMPTFIRST' , 4 => 'ATTEMPTLAST'); $questions = $this->quiz_export_prepare_questions($questions, $quiz->id, $course->id, $quiz->shuffleanswers); $smarty =& $this->init_smarty(); $smarty->assign('questions', $questions); // quiz level smarty variables $manifestid = str_replace(array(':', '/'), array('-','_'), "quiz{$quiz->id}-{$CFG->wwwroot}"); $smarty->assign('manifestidentifier', $manifestid); $smarty->assign('submiturl', $submiturl); $smarty->assign('userid', $USER->id); $smarty->assign('username', htmlspecialchars($USER->username, ENT_COMPAT, 'UTF-8')); $smarty->assign('quiz_level_export', 1); $smarty->assign('quiztitle', format_string($quiz->name,true)); //assigned specifically so as not to cause problems with category-level export $smarty->assign('quiztimeopen', date('Y-m-d\TH:i:s', $quiz->timeopen)); // ditto $smarty->assign('quiztimeclose', date('Y-m-d\TH:i:s', $quiz->timeclose)); // ditto $smarty->assign('grademethod', $gradingmethod[$quiz->grademethod]); $smarty->assign('quiz', $quiz); $smarty->assign('course', $course); $smarty->assign('lang', $this->lang); $expout = $smarty->fetch('imsmanifest.tpl'); echo $expout; return true; } /** * Prepares questions for quiz export * * The questions are changed as follows: * - the question answers atached to the questions * - image set to an http reference instead of a file path * - qti specific info added * - exporttext added, which contains an xml-formatted qti assesmentItem * * @param array $questions - an array of question objects * @param int $quizid * @return an array of question arrays */ function quiz_export_prepare_questions($questions, $quizid, $courseid, $shuffleanswers = null) { global $CFG; // add the answers to the questions and format the image property foreach ($questions as $key=>$question) { $questions[$key] = get_question_data($question); $questions[$key]->courseid = $courseid; $questions[$key]->quizid = $quizid; if ($question->image) { if (empty($question->mediamimetype)) { $questions[$key]->mediamimetype = mimeinfo('type',$question->image); } $localfile = (substr(strtolower($question->image), 0, 7) == 'http://') ? false : true; if ($localfile) { // create the http url that the player will need to access the file if ($CFG->slasharguments) { // Use this method if possible for better caching $questions[$key]->mediaurl = "$CFG->wwwroot/file.php/$question->image"; } else { $questions[$key]->mediaurl = "$CFG->wwwroot/file.php?file=$question->image"; } } else { $questions[$key]->mediaurl = $question->image; } } } $this->add_qti_info($questions); $questions = $this->questions_with_export_info($questions, $shuffleanswers); $questions = $this->objects_to_array($questions); return $questions; } /** * calls htmlspecialchars for each string field, to convert, for example, & to & * * collections are processed recursively * * @param array $collection - an array or object or string */ function xml_entitize(&$collection) { if (is_array($collection)) { foreach ($collection as $key=>$var) { if (is_string($var)) { $collection[$key]= htmlspecialchars($var, ENT_COMPAT, 'UTF-8'); } else if (is_array($var) || is_object($var)) { $this->xml_entitize($collection[$key]); } } } else if (is_object($collection)) { $vars = get_object_vars($collection); foreach ($vars as $key=>$var) { if (is_string($var)) { $collection->$key = htmlspecialchars($var, ENT_COMPAT, 'UTF-8'); } else if (is_array($var) || is_object($var)) { $this->xml_entitize($collection->$key); } } } else if (is_string($collection)) { $collection = htmlspecialchars($collection, ENT_COMPAT, 'UTF-8'); } } /** * adds exporttext property to the questions * * Adds the qti export text to the questions * * @param array $questions - an array of question objects * @return an array of question objects */ function questions_with_export_info($questions, $shuffleanswers = null) { $exportquestions = array(); foreach($questions as $key=>$question) { $expout = $this->writequestion( $question , $shuffleanswers) . "\n"; $expout = $this->presave_process( $expout ); $key = $this->get_assesment_item_id($question); $exportquestions[$key] = $question; $exportquestions[$key]->exporttext = $expout; } return $exportquestions; } /** * Creates the export text for a question * * @todo handle in-line media (specified in the question/subquestion/answer text) for course-level exports * @param object $question * @param boolean $shuffleanswers whether or not to shuffle the answers * @param boolean $courselevel whether or not this is a course-level export * @param string $path provide the path to copy question media files to, if $courselevel == true * @return string containing export text */ function writequestion($question, $shuffleanswers = null, $courselevel = false, $path = '') { // turns question into string // question reflects database fields for general question and specific to type global $CFG; $expout = ''; //need to unencode the html entities in the questiontext field. // the whole question object was earlier run throught htmlspecialchars in xml_entitize(). $question->questiontext = html_entity_decode($question->questiontext, ENT_COMPAT); $hasimage = empty($question->image) ? 0 : 1; $hassize = empty($question->mediax) ? 0 : 1; $allowedtags = '