Actualización
This commit is contained in:
471
main/exercise/export/qti2/qti2_classes.php
Normal file
471
main/exercise/export/qti2/qti2_classes.php
Normal file
@@ -0,0 +1,471 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* Interface ImsAnswerInterface.
|
||||
*/
|
||||
interface ImsAnswerInterface
|
||||
{
|
||||
/**
|
||||
* @param string $questionIdent
|
||||
* @param string $questionStatment
|
||||
* @param string $questionDesc
|
||||
* @param string $questionMedia
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function imsExportResponses($questionIdent, $questionStatment, $questionDesc = '', $questionMedia = '');
|
||||
|
||||
/**
|
||||
* @param $questionIdent
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function imsExportResponsesDeclaration($questionIdent, Question $question = null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Claro Team <cvs@claroline.net>
|
||||
* @author Yannick Warnier <yannick.warnier@beeznest.com> -
|
||||
* updated ImsAnswerHotspot to match QTI norms
|
||||
*/
|
||||
class Ims2Question extends Question
|
||||
{
|
||||
/**
|
||||
* Include the correct answer class and create answer.
|
||||
*
|
||||
* @return Answer
|
||||
*/
|
||||
public function setAnswer()
|
||||
{
|
||||
switch ($this->type) {
|
||||
case MCUA:
|
||||
$answer = new ImsAnswerMultipleChoice($this->iid);
|
||||
|
||||
return $answer;
|
||||
case MCMA:
|
||||
case MULTIPLE_ANSWER_DROPDOWN:
|
||||
case MULTIPLE_ANSWER_DROPDOWN_COMBINATION:
|
||||
$answer = new ImsAnswerMultipleChoice($this->iid);
|
||||
|
||||
return $answer;
|
||||
case TF:
|
||||
$answer = new ImsAnswerMultipleChoice($this->iid);
|
||||
|
||||
return $answer;
|
||||
case FIB:
|
||||
$answer = new ImsAnswerFillInBlanks($this->iid);
|
||||
|
||||
return $answer;
|
||||
case MATCHING:
|
||||
case MATCHING_DRAGGABLE:
|
||||
$answer = new ImsAnswerMatching($this->iid);
|
||||
|
||||
return $answer;
|
||||
case FREE_ANSWER:
|
||||
$answer = new ImsAnswerFree($this->iid);
|
||||
|
||||
return $answer;
|
||||
case HOT_SPOT:
|
||||
case HOT_SPOT_COMBINATION:
|
||||
$answer = new ImsAnswerHotspot($this->iid);
|
||||
|
||||
return $answer;
|
||||
default:
|
||||
$answer = null;
|
||||
break;
|
||||
}
|
||||
|
||||
return $answer;
|
||||
}
|
||||
|
||||
public function createAnswersForm($form)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function processAnswersCreation($form, $exercise)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class.
|
||||
*/
|
||||
class ImsAnswerMultipleChoice extends Answer implements ImsAnswerInterface
|
||||
{
|
||||
/**
|
||||
* Return the XML flow for the possible answers.
|
||||
*
|
||||
* @param string $questionIdent
|
||||
* @param string $questionStatment
|
||||
* @param string $questionDesc
|
||||
* @param string $questionMedia
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function imsExportResponses($questionIdent, $questionStatment, $questionDesc = '', $questionMedia = '')
|
||||
{
|
||||
// @todo getAnswersList() converts the answers using api_html_entity_decode()
|
||||
$this->answerList = $this->getAnswersList(true);
|
||||
$out = ' <choiceInteraction responseIdentifier="'.$questionIdent.'" >'."\n";
|
||||
$out .= ' <prompt><![CDATA['.formatExerciseQtiText($questionDesc).']]></prompt>'."\n";
|
||||
if (is_array($this->answerList)) {
|
||||
foreach ($this->answerList as $current_answer) {
|
||||
$out .= '<simpleChoice identifier="answer_'.$current_answer['iid'].'" fixed="false">
|
||||
<![CDATA['.formatExerciseQtiText($current_answer['answer']).']]>';
|
||||
if (isset($current_answer['comment']) && $current_answer['comment'] != '') {
|
||||
$out .= '<feedbackInline identifier="answer_'.$current_answer['iid'].'">
|
||||
<![CDATA['.formatExerciseQtiText($current_answer['comment']).']]>
|
||||
</feedbackInline>';
|
||||
}
|
||||
$out .= '</simpleChoice>'."\n";
|
||||
}
|
||||
}
|
||||
$out .= ' </choiceInteraction>'."\n";
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the XML flow of answer ResponsesDeclaration.
|
||||
*/
|
||||
public function imsExportResponsesDeclaration($questionIdent, Question $question = null)
|
||||
{
|
||||
$this->answerList = $this->getAnswersList(true);
|
||||
$type = $this->getQuestionType();
|
||||
if (in_array($type, [MCMA, MULTIPLE_ANSWER_DROPDOWN, MULTIPLE_ANSWER_DROPDOWN_COMBINATION])) {
|
||||
$cardinality = 'multiple';
|
||||
} else {
|
||||
$cardinality = 'single';
|
||||
}
|
||||
|
||||
$out = ' <responseDeclaration identifier="'.$questionIdent.'" cardinality="'.$cardinality.'" baseType="identifier">'."\n";
|
||||
|
||||
// Match the correct answers.
|
||||
if (is_array($this->answerList)) {
|
||||
$out .= ' <correctResponse>'."\n";
|
||||
foreach ($this->answerList as $current_answer) {
|
||||
if ($current_answer['correct']) {
|
||||
$out .= ' <value>answer_'.$current_answer['iid'].'</value>'."\n";
|
||||
}
|
||||
}
|
||||
$out .= ' </correctResponse>'."\n";
|
||||
}
|
||||
|
||||
// Add the grading
|
||||
if (is_array($this->answerList)) {
|
||||
$out .= ' <mapping';
|
||||
|
||||
if (MULTIPLE_ANSWER_DROPDOWN_COMBINATION == $this->getQuestionType()) {
|
||||
$out .= ' defaultValue="'.$question->selectWeighting().'"';
|
||||
}
|
||||
|
||||
$out .= '>'."\n";
|
||||
|
||||
foreach ($this->answerList as $current_answer) {
|
||||
if (isset($current_answer['grade'])) {
|
||||
$out .= ' <mapEntry mapKey="answer_'.$current_answer['iid'].'" mappedValue="'.$current_answer['grade'].'" />'."\n";
|
||||
}
|
||||
}
|
||||
$out .= ' </mapping>'."\n";
|
||||
}
|
||||
|
||||
$out .= ' </responseDeclaration>'."\n";
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class.
|
||||
*
|
||||
* @package chamilo.exercise
|
||||
*/
|
||||
class ImsAnswerFillInBlanks extends Answer implements ImsAnswerInterface
|
||||
{
|
||||
private $answerList = [];
|
||||
private $gradeList = [];
|
||||
|
||||
/**
|
||||
* Export the text with missing words.
|
||||
*
|
||||
* @param string $questionIdent
|
||||
* @param string $questionStatment
|
||||
* @param string $questionDesc
|
||||
* @param string $questionMedia
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function imsExportResponses($questionIdent, $questionStatment, $questionDesc = '', $questionMedia = '')
|
||||
{
|
||||
$this->answerList = $this->getAnswersList(true);
|
||||
$text = isset($this->answerText) ? $this->answerText : '';
|
||||
if (is_array($this->answerList)) {
|
||||
foreach ($this->answerList as $key => $answer) {
|
||||
$key = $answer['iid'];
|
||||
$answer = $answer['answer'];
|
||||
$len = api_strlen($answer);
|
||||
$text = str_replace('['.$answer.']', '<textEntryInteraction responseIdentifier="fill_'.$key.'" expectedLength="'.api_strlen($answer).'"/>', $text);
|
||||
}
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
public function imsExportResponsesDeclaration($questionIdent, Question $question = null)
|
||||
{
|
||||
$this->answerList = $this->getAnswersList(true);
|
||||
$this->gradeList = $this->getGradesList();
|
||||
$out = '';
|
||||
if (is_array($this->answerList)) {
|
||||
foreach ($this->answerList as $answer) {
|
||||
$answerKey = $answer['iid'];
|
||||
$answer = $answer['answer'];
|
||||
$out .= ' <responseDeclaration identifier="fill_'.$answerKey.'" cardinality="single" baseType="identifier">'."\n";
|
||||
$out .= ' <correctResponse>'."\n";
|
||||
$out .= ' <value><![CDATA['.formatExerciseQtiText($answer).']]></value>'."\n";
|
||||
$out .= ' </correctResponse>'."\n";
|
||||
if (isset($this->gradeList[$answerKey])) {
|
||||
$out .= ' <mapping>'."\n";
|
||||
$out .= ' <mapEntry mapKey="'.$answer.'" mappedValue="'.$this->gradeList[$answerKey].'"/>'."\n";
|
||||
$out .= ' </mapping>'."\n";
|
||||
}
|
||||
|
||||
$out .= ' </responseDeclaration>'."\n";
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class.
|
||||
*
|
||||
* @package chamilo.exercise
|
||||
*/
|
||||
class ImsAnswerMatching extends Answer implements ImsAnswerInterface
|
||||
{
|
||||
public $leftList = [];
|
||||
public $rightList = [];
|
||||
private $answerList = [];
|
||||
|
||||
/**
|
||||
* Export the question part as a matrix-choice, with only one possible answer per line.
|
||||
*
|
||||
* @param string $questionIdent
|
||||
* @param string $questionStatment
|
||||
* @param string $questionDesc
|
||||
* @param string $questionMedia
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function imsExportResponses($questionIdent, $questionStatment, $questionDesc = '', $questionMedia = '')
|
||||
{
|
||||
$this->answerList = $this->getAnswersList(true);
|
||||
$maxAssociation = max(count($this->leftList), count($this->rightList));
|
||||
|
||||
$out = '<matchInteraction responseIdentifier="'.$questionIdent.'" maxAssociations="'.$maxAssociation.'">'."\n";
|
||||
$out .= $questionStatment;
|
||||
|
||||
//add left column
|
||||
$out .= ' <simpleMatchSet>'."\n";
|
||||
if (is_array($this->leftList)) {
|
||||
foreach ($this->leftList as $leftKey => $leftElement) {
|
||||
$out .= '
|
||||
<simpleAssociableChoice identifier="left_'.$leftKey.'" >
|
||||
<![CDATA['.formatExerciseQtiText($leftElement['answer']).']]>
|
||||
</simpleAssociableChoice>'."\n";
|
||||
}
|
||||
}
|
||||
|
||||
$out .= ' </simpleMatchSet>'."\n";
|
||||
|
||||
//add right column
|
||||
$out .= ' <simpleMatchSet>'."\n";
|
||||
$i = 0;
|
||||
|
||||
if (is_array($this->rightList)) {
|
||||
foreach ($this->rightList as $rightKey => $rightElement) {
|
||||
$out .= '<simpleAssociableChoice identifier="right_'.$i.'" >
|
||||
<![CDATA['.formatExerciseQtiText($rightElement['answer']).']]>
|
||||
</simpleAssociableChoice>'."\n";
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
$out .= ' </simpleMatchSet>'."\n";
|
||||
$out .= '</matchInteraction>'."\n";
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
public function imsExportResponsesDeclaration($questionIdent, Question $question = null)
|
||||
{
|
||||
$this->answerList = $this->getAnswersList(true);
|
||||
$out = ' <responseDeclaration identifier="'.$questionIdent.'" cardinality="single" baseType="identifier">'."\n";
|
||||
$out .= ' <correctResponse>'."\n";
|
||||
|
||||
$gradeArray = [];
|
||||
if (isset($this->leftList) && is_array($this->leftList)) {
|
||||
foreach ($this->leftList as $leftKey => $leftElement) {
|
||||
$i = 0;
|
||||
foreach ($this->rightList as $rightKey => $rightElement) {
|
||||
if (($leftElement['match'] == $rightElement['code'])) {
|
||||
$out .= ' <value>left_'.$leftKey.' right_'.$i.'</value>'."\n";
|
||||
$gradeArray['left_'.$leftKey.' right_'.$i] = $leftElement['grade'];
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
$out .= ' </correctResponse>'."\n";
|
||||
|
||||
if (is_array($gradeArray)) {
|
||||
$out .= ' <mapping>'."\n";
|
||||
foreach ($gradeArray as $gradeKey => $grade) {
|
||||
$out .= ' <mapEntry mapKey="'.$gradeKey.'" mappedValue="'.$grade.'"/>'."\n";
|
||||
}
|
||||
$out .= ' </mapping>'."\n";
|
||||
}
|
||||
|
||||
$out .= ' </responseDeclaration>'."\n";
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class.
|
||||
*
|
||||
* @package chamilo.exercise
|
||||
*/
|
||||
class ImsAnswerHotspot extends Answer implements ImsAnswerInterface
|
||||
{
|
||||
private $answerList = [];
|
||||
private $gradeList = [];
|
||||
|
||||
/**
|
||||
* @todo update this to match hot spots instead of copying matching
|
||||
* Export the question part as a matrix-choice, with only one possible answer per line.
|
||||
*
|
||||
* @param string $questionIdent
|
||||
* @param string $questionStatment
|
||||
* @param string $questionDesc
|
||||
* @param string $questionMedia
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function imsExportResponses($questionIdent, $questionStatment, $questionDesc = '', $questionMedia = '')
|
||||
{
|
||||
$this->answerList = $this->getAnswersList(true);
|
||||
$mediaFilePath = api_get_course_path().'/document/images/'.$questionMedia;
|
||||
$sysQuestionMediaPath = api_get_path(SYS_COURSE_PATH).$mediaFilePath;
|
||||
$questionMedia = api_get_path(WEB_COURSE_PATH).$mediaFilePath;
|
||||
$mimetype = mime_content_type($sysQuestionMediaPath);
|
||||
if (empty($mimetype)) {
|
||||
$mimetype = 'image/jpeg';
|
||||
}
|
||||
|
||||
$text = ' <p>'.$questionStatment.'</p>'."\n";
|
||||
$text .= ' <graphicOrderInteraction responseIdentifier="hotspot_'.$questionIdent.'">'."\n";
|
||||
$text .= ' <prompt>'.$questionDesc.'</prompt>'."\n";
|
||||
$text .= ' <object type="'.$mimetype.'" width="250" height="230" data="'.$questionMedia.'">-</object>'."\n";
|
||||
if (is_array($this->answerList)) {
|
||||
foreach ($this->answerList as $key => $answer) {
|
||||
$key = $answer['iid'];
|
||||
$answerTxt = $answer['answer'];
|
||||
$len = api_strlen($answerTxt);
|
||||
//coords are transformed according to QTIv2 rules here: http://www.imsproject.org/question/qtiv2p1pd/imsqti_infov2p1pd.html#element10663
|
||||
$coords = '';
|
||||
$type = 'default';
|
||||
switch ($answer['hotspot_type']) {
|
||||
case 'square':
|
||||
$type = 'rect';
|
||||
$res = [];
|
||||
$coords = preg_match('/^\s*(\d+);(\d+)\|(\d+)\|(\d+)\s*$/', $answer['hotspot_coord'], $res);
|
||||
$coords = $res[1].','.$res[2].','.((int) $res[1] + (int) $res[3]).",".((int) $res[2] + (int) $res[4]);
|
||||
break;
|
||||
case 'circle':
|
||||
$type = 'circle';
|
||||
$res = [];
|
||||
$coords = preg_match('/^\s*(\d+);(\d+)\|(\d+)\|(\d+)\s*$/', $answer['hotspot_coord'], $res);
|
||||
$coords = $res[1].','.$res[2].','.sqrt(pow(($res[1] - $res[3]), 2) + pow(($res[2] - $res[4])));
|
||||
break;
|
||||
case 'poly':
|
||||
$type = 'poly';
|
||||
$coords = str_replace([';', '|'], [',', ','], $answer['hotspot_coord']);
|
||||
break;
|
||||
case 'delineation':
|
||||
$type = 'delineation';
|
||||
$coords = str_replace([';', '|'], [',', ','], $answer['hotspot_coord']);
|
||||
break;
|
||||
}
|
||||
$text .= ' <hotspotChoice shape="'.$type.'" coords="'.$coords.'" identifier="'.$key.'"/>'."\n";
|
||||
}
|
||||
}
|
||||
$text .= ' </graphicOrderInteraction>'."\n";
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
public function imsExportResponsesDeclaration($questionIdent, Question $question = null)
|
||||
{
|
||||
$this->answerList = $this->getAnswersList(true);
|
||||
$this->gradeList = $this->getGradesList();
|
||||
$out = ' <responseDeclaration identifier="hotspot_'.$questionIdent.'" cardinality="ordered" baseType="identifier">'."\n";
|
||||
if (is_array($this->answerList)) {
|
||||
$out .= ' <correctResponse>'."\n";
|
||||
foreach ($this->answerList as $answerKey => $answer) {
|
||||
$answerKey = $answer['iid'];
|
||||
$answer = $answer['answer'];
|
||||
$out .= '<value><![CDATA['.formatExerciseQtiText($answerKey).']]></value>';
|
||||
}
|
||||
$out .= ' </correctResponse>'."\n";
|
||||
}
|
||||
$out .= ' </responseDeclaration>'."\n";
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class.
|
||||
*
|
||||
* @package chamilo.exercise
|
||||
*/
|
||||
class ImsAnswerFree extends Answer implements ImsAnswerInterface
|
||||
{
|
||||
/**
|
||||
* @todo implement
|
||||
* Export the question part as a matrix-choice, with only one possible answer per line.
|
||||
*
|
||||
* @param string $questionIdent
|
||||
* @param string $questionStatment
|
||||
* @param string $questionDesc
|
||||
* @param string $questionMedia
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function imsExportResponses($questionIdent, $questionStatment, $questionDesc = '', $questionMedia = '')
|
||||
{
|
||||
$questionDesc = formatExerciseQtiText($questionDesc);
|
||||
|
||||
return '<extendedTextInteraction responseIdentifier="'.$questionIdent.'" >
|
||||
<prompt>
|
||||
'.$questionDesc.'
|
||||
</prompt>
|
||||
</extendedTextInteraction>';
|
||||
}
|
||||
|
||||
public function imsExportResponsesDeclaration($questionIdent, Question $question = null)
|
||||
{
|
||||
$out = ' <responseDeclaration identifier="'.$questionIdent.'" cardinality="single" baseType="string">';
|
||||
$out .= '<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">
|
||||
<defaultValue><value>'.$question->weighting.'</value></defaultValue></outcomeDeclaration>';
|
||||
$out .= ' </responseDeclaration>'."\n";
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
515
main/exercise/export/qti2/qti2_export.php
Normal file
515
main/exercise/export/qti2/qti2_export.php
Normal file
@@ -0,0 +1,515 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* @author Claro Team <cvs@claroline.net>
|
||||
* @author Yannick Warnier <yannick.warnier@beeznest.com>
|
||||
*/
|
||||
require __DIR__.'/qti2_classes.php';
|
||||
|
||||
/**
|
||||
* An IMS/QTI item. It corresponds to a single question.
|
||||
* This class allows export from Claroline to IMS/QTI2.0 XML format of a single question.
|
||||
* It is not usable as-is, but must be subclassed, to support different kinds of questions.
|
||||
*
|
||||
* Every start_*() and corresponding end_*(), as well as export_*() methods return a string.
|
||||
*
|
||||
* note: Attached files are NOT exported.
|
||||
*/
|
||||
class ImsAssessmentItem
|
||||
{
|
||||
/**
|
||||
* @var Ims2Question
|
||||
*/
|
||||
public $question;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $questionIdent;
|
||||
/**
|
||||
* @var ImsAnswerInterface
|
||||
*/
|
||||
public $answer;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Ims2Question $question ims2Question object we want to export
|
||||
*/
|
||||
public function __construct($question)
|
||||
{
|
||||
$this->question = $question;
|
||||
$this->answer = $this->question->setAnswer();
|
||||
$this->questionIdent = 'QST_'.$question->iid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the XML flow.
|
||||
*
|
||||
* This opens the <item> block, with correct attributes.
|
||||
*/
|
||||
public function start_item()
|
||||
{
|
||||
$categoryTitle = '';
|
||||
if (!empty($this->question->category)) {
|
||||
$category = new TestCategory();
|
||||
$category = $category->getCategory($this->question->category);
|
||||
if ($category) {
|
||||
$categoryTitle = htmlspecialchars(formatExerciseQtiText($category->name));
|
||||
}
|
||||
}
|
||||
|
||||
return '<assessmentItem xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqti_v2p1 imsqti_v2p1.xsd"
|
||||
identifier="'.$this->questionIdent.'"
|
||||
title = "'.htmlspecialchars(formatExerciseQtiText($this->question->selectTitle())).'"
|
||||
category = "'.$categoryTitle.'"
|
||||
>'."\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* End the XML flow, closing the </item> tag.
|
||||
*/
|
||||
public function end_item()
|
||||
{
|
||||
return "</assessmentItem>\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the itemBody.
|
||||
*/
|
||||
public function start_item_body()
|
||||
{
|
||||
return ' <itemBody>'."\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* End the itemBody part.
|
||||
*/
|
||||
public function end_item_body()
|
||||
{
|
||||
return " </itemBody>\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* add the response processing template used.
|
||||
*/
|
||||
public function add_response_processing()
|
||||
{
|
||||
return ' <responseProcessing template="http://www.imsglobal.org/question/qti_v2p1/rptemplates/map_correct"/>'."\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the question as an IMS/QTI Item.
|
||||
*
|
||||
* This is a default behaviour, some classes may want to override this.
|
||||
*
|
||||
* @return string string, the XML flow for an Item
|
||||
*/
|
||||
public function export($standalone = false)
|
||||
{
|
||||
$head = $foot = '';
|
||||
if ($standalone) {
|
||||
$head = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'."\n";
|
||||
}
|
||||
|
||||
//TODO understand why answer might be a non-object sometimes
|
||||
if (!is_object($this->answer)) {
|
||||
return $head;
|
||||
}
|
||||
|
||||
return $head
|
||||
.$this->start_item()
|
||||
.$this->answer->imsExportResponsesDeclaration($this->questionIdent, $this->question)
|
||||
.$this->start_item_body()
|
||||
.$this->answer->imsExportResponses(
|
||||
$this->questionIdent,
|
||||
$this->question->question,
|
||||
$this->question->description,
|
||||
$this->question->getPictureFilename()
|
||||
)
|
||||
.$this->end_item_body()
|
||||
.$this->add_response_processing()
|
||||
.$this->end_item()
|
||||
.$foot;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class represents an entire exercise to be exported in IMS/QTI.
|
||||
* It will be represented by a single <section> containing several <item>.
|
||||
*
|
||||
* Some properties cannot be exported, as IMS does not support them :
|
||||
* - type (one page or multiple pages)
|
||||
* - start_date and end_date
|
||||
* - max_attempts
|
||||
* - show_answer
|
||||
* - anonymous_attempts
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
class ImsSection
|
||||
{
|
||||
public $exercise;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Exercise $exe The Exercise instance to export
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
public function __construct($exe)
|
||||
{
|
||||
$this->exercise = $exe;
|
||||
}
|
||||
|
||||
public function start_section()
|
||||
{
|
||||
return '<section
|
||||
ident = "EXO_'.$this->exercise->selectId().'"
|
||||
title = "'.cleanAttribute(formatExerciseQtiDescription($this->exercise->selectTitle())).'"
|
||||
>'."\n";
|
||||
}
|
||||
|
||||
public function end_section()
|
||||
{
|
||||
return "</section>\n";
|
||||
}
|
||||
|
||||
public function export_duration()
|
||||
{
|
||||
if ($max_time = $this->exercise->selectTimeLimit()) {
|
||||
// return exercise duration in ISO8601 format.
|
||||
$minutes = floor($max_time / 60);
|
||||
$seconds = $max_time % 60;
|
||||
|
||||
return '<duration>PT'.$minutes.'M'.$seconds."S</duration>\n";
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the presentation (Exercise's description).
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
public function export_presentation()
|
||||
{
|
||||
return "<presentation_material><flow_mat><material>\n"
|
||||
.' <mattext><![CDATA['.formatExerciseQtiDescription($this->exercise->selectDescription())."]]></mattext>\n"
|
||||
."</material></flow_mat></presentation_material>\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the ordering information.
|
||||
* Either sequential, through all questions, or random, with a selected number of questions.
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
public function export_ordering()
|
||||
{
|
||||
$out = '';
|
||||
if ($n = $this->exercise->getShuffle()) {
|
||||
$out .= "<selection_ordering>"
|
||||
." <selection>\n"
|
||||
." <selection_number>".$n."</selection_number>\n"
|
||||
." </selection>\n"
|
||||
.' <order order_type="Random" />'
|
||||
."\n</selection_ordering>\n";
|
||||
} else {
|
||||
$out .= '<selection_ordering sequence_type="Normal">'."\n"
|
||||
." <selection />\n"
|
||||
."</selection_ordering>\n";
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the questions, as a succession of <items>.
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
public function exportQuestions()
|
||||
{
|
||||
$out = '';
|
||||
foreach ($this->exercise->selectQuestionList() as $q) {
|
||||
$out .= export_question_qti($q, false);
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the exercise in IMS/QTI.
|
||||
*
|
||||
* @param bool $standalone wether it should include XML tag and DTD line
|
||||
*
|
||||
* @return string string containing the XML flow
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
public function export($standalone)
|
||||
{
|
||||
$head = $foot = '';
|
||||
if ($standalone) {
|
||||
$head = '<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>'."\n"
|
||||
.'<!DOCTYPE questestinterop SYSTEM "ims_qtiasiv2p1.dtd">'."\n"
|
||||
."<questestinterop>\n";
|
||||
$foot = "</questestinterop>\n";
|
||||
}
|
||||
|
||||
return $head
|
||||
.$this->start_section()
|
||||
.$this->export_duration()
|
||||
.$this->export_presentation()
|
||||
.$this->export_ordering()
|
||||
.$this->exportQuestions()
|
||||
.$this->end_section()
|
||||
.$foot;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Some quick notes on identifiers generation.
|
||||
The IMS format requires some blocks, like items, responses, feedbacks, to be uniquely
|
||||
identified.
|
||||
The unicity is mandatory in a single XML, of course, but it's prefered that the identifier stays
|
||||
coherent for an entire site.
|
||||
|
||||
Here's the method used to generate those identifiers.
|
||||
Question identifier :: "QST_" + <Question Id from the DB> + "_" + <Question numeric type>
|
||||
Response identifier :: <Question identifier> + "_A_" + <Response Id from the DB>
|
||||
Condition identifier :: <Question identifier> + "_C_" + <Response Id from the DB>
|
||||
Feedback identifier :: <Question identifier> + "_F_" + <Response Id from the DB>
|
||||
*/
|
||||
/**
|
||||
* Class ImsItem.
|
||||
*
|
||||
* An IMS/QTI item. It corresponds to a single question.
|
||||
* This class allows export from Claroline to IMS/QTI XML format.
|
||||
* It is not usable as-is, but must be subclassed, to support different kinds of questions.
|
||||
*
|
||||
* Every start_*() and corresponding end_*(), as well as export_*() methods return a string.
|
||||
*
|
||||
* warning: Attached files are NOT exported.
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
class ImsItem
|
||||
{
|
||||
public $question;
|
||||
public $questionIdent;
|
||||
public $answer;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Question $question the Question object we want to export
|
||||
*
|
||||
* @author Anamd Tihon
|
||||
*/
|
||||
public function __construct($question)
|
||||
{
|
||||
$this->question = $question;
|
||||
$this->answer = $question->answer;
|
||||
$this->questionIdent = 'QST_'.$question->selectId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the XML flow.
|
||||
*
|
||||
* This opens the <item> block, with correct attributes.
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
public function start_item()
|
||||
{
|
||||
return '<item title="'.cleanAttribute(formatExerciseQtiDescription($this->question->selectTitle())).'" ident="'.$this->questionIdent.'">'."\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* End the XML flow, closing the </item> tag.
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
public function end_item()
|
||||
{
|
||||
return "</item>\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the opening, with the question itself.
|
||||
*
|
||||
* This means it opens the <presentation> but doesn't close it, as this is the role of end_presentation().
|
||||
* In between, the export_responses from the subclass should have been called.
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
public function start_presentation()
|
||||
{
|
||||
return '<presentation label="'.$this->questionIdent.'"><flow>'."\n"
|
||||
.'<material><mattext>'.formatExerciseQtiDescription($this->question->selectDescription())."</mattext></material>\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* End the </presentation> part, opened by export_header.
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
public function end_presentation()
|
||||
{
|
||||
return "</flow></presentation>\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the response processing, and declare the default variable, SCORE, at 0 in the outcomes.
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
public function start_processing()
|
||||
{
|
||||
return '<resprocessing><outcomes><decvar vartype="Integer" defaultval="0" /></outcomes>'."\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* End the response processing part.
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
public function end_processing()
|
||||
{
|
||||
return "</resprocessing>\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the question as an IMS/QTI Item.
|
||||
*
|
||||
* This is a default behaviour, some classes may want to override this.
|
||||
*
|
||||
* @param bool $standalone Boolean stating if it should be exported as a stand-alone question
|
||||
*
|
||||
* @return string string, the XML flow for an Item
|
||||
*
|
||||
* @author Amand Tihon <amand@alrj.org>
|
||||
*/
|
||||
public function export($standalone = false)
|
||||
{
|
||||
global $charset;
|
||||
$head = $foot = '';
|
||||
|
||||
if ($standalone) {
|
||||
$head = '<?xml version = "1.0" encoding = "'.$charset.'" standalone = "no"?>'."\n"
|
||||
.'<!DOCTYPE questestinterop SYSTEM "ims_qtiasiv2p1.dtd">'."\n"
|
||||
."<questestinterop>\n";
|
||||
$foot = "</questestinterop>\n";
|
||||
}
|
||||
|
||||
return $head
|
||||
.$this->start_item()
|
||||
.$this->start_presentation()
|
||||
.$this->answer->imsExportResponses($this->questionIdent)
|
||||
.$this->end_presentation()
|
||||
.$this->start_processing()
|
||||
.$this->answer->imsExportProcessing($this->questionIdent)
|
||||
.$this->end_processing()
|
||||
.$this->answer->imsExportFeedback($this->questionIdent)
|
||||
.$this->end_item()
|
||||
.$foot;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a complete exercise in IMS/QTI format, from its ID.
|
||||
*
|
||||
* @param int $exerciseId The exercise to export
|
||||
* @param bool $standalone wether it should include XML tag and DTD line
|
||||
*
|
||||
* @return string XML as a string, or an empty string if there's no exercise with given ID
|
||||
*/
|
||||
function export_exercise_to_qti($exerciseId, $standalone = true)
|
||||
{
|
||||
$exercise = new Exercise();
|
||||
if (!$exercise->read($exerciseId)) {
|
||||
return '';
|
||||
}
|
||||
$ims = new ImsSection($exercise);
|
||||
|
||||
return $ims->export($standalone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the XML flow corresponding to one question.
|
||||
*
|
||||
* @param int $questionId
|
||||
* @param bool $standalone (ie including XML tag, DTD declaration, etc)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function export_question_qti($questionId, $standalone = true)
|
||||
{
|
||||
$question = new Ims2Question();
|
||||
$qst = $question->read($questionId);
|
||||
if (!$qst) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$isValid = $qst instanceof UniqueAnswer
|
||||
|| $qst instanceof MultipleAnswer
|
||||
|| $qst instanceof FreeAnswer
|
||||
|| $qst instanceof MultipleAnswerDropdown
|
||||
|| $qst instanceof MultipleAnswerDropdownCombination
|
||||
;
|
||||
|
||||
if (!$isValid) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$question->iid = $qst->iid;
|
||||
$question->type = $qst->type;
|
||||
$question->question = $qst->question;
|
||||
$question->description = $qst->description;
|
||||
$question->weighting = $qst->weighting;
|
||||
$question->position = $qst->position;
|
||||
$question->picture = $qst->picture;
|
||||
$question->category = $qst->category;
|
||||
$ims = new ImsAssessmentItem($question);
|
||||
|
||||
return $ims->export($standalone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean text like a description.
|
||||
*/
|
||||
function formatExerciseQtiDescription($text)
|
||||
{
|
||||
$entities = api_html_entity_decode($text);
|
||||
|
||||
return htmlspecialchars($entities);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean titles.
|
||||
*
|
||||
* @param $text
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function formatExerciseQtiText($text)
|
||||
{
|
||||
return htmlspecialchars($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function cleanAttribute($text)
|
||||
{
|
||||
return $text;
|
||||
}
|
||||
Reference in New Issue
Block a user