qti2_export.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @author Claro Team <cvs@claroline.net>
  5. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  6. * @package chamilo.exercise
  7. */
  8. /**
  9. * Code
  10. */
  11. require dirname(__FILE__) . '/qti2_classes.php';
  12. /**
  13. * Classes
  14. */
  15. /**
  16. * An IMS/QTI item. It corresponds to a single question.
  17. * This class allows export from Claroline to IMS/QTI2.0 XML format of a single question.
  18. * It is not usable as-is, but must be subclassed, to support different kinds of questions.
  19. *
  20. * Every start_*() and corresponding end_*(), as well as export_*() methods return a string.
  21. *
  22. * note: Attached files are NOT exported.
  23. * @package chamilo.exercise
  24. */
  25. class ImsAssessmentItem
  26. {
  27. public $question;
  28. public $question_ident;
  29. public $answer;
  30. /**
  31. * Constructor.
  32. *
  33. * @param Ims2Question $question Ims2Question object we want to export.
  34. */
  35. function __construct($question)
  36. {
  37. $this->question = $question;
  38. $this->answer = $this->question->setAnswer();
  39. $this->questionIdent = "QST_" . $question->id ;
  40. }
  41. /**
  42. * Start the XML flow.
  43. *
  44. * This opens the <item> block, with correct attributes.
  45. *
  46. */
  47. function start_item()
  48. {
  49. $string = '<assessmentItem xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1"
  50. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  51. xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqti_v2p1 imsqti_v2p1.xsd"
  52. identifier="'.$this->questionIdent.'"
  53. title="'.htmlspecialchars(formatExerciseQtiTitle($this->question->selectTitle())).'">'."\n";
  54. return $string;
  55. }
  56. /**
  57. * End the XML flow, closing the </item> tag.
  58. *
  59. */
  60. function end_item()
  61. {
  62. return "</assessmentItem>\n";
  63. }
  64. /**
  65. * Start the itemBody
  66. *
  67. */
  68. function start_item_body()
  69. {
  70. return ' <itemBody>' . "\n";
  71. }
  72. /**
  73. * End the itemBody part.
  74. *
  75. */
  76. function end_item_body()
  77. {
  78. return " </itemBody>\n";
  79. }
  80. /**
  81. * add the response processing template used.
  82. *
  83. */
  84. function add_response_processing()
  85. {
  86. return ' <responseProcessing template="http://www.imsglobal.org/question/qti_v2p1/rptemplates/map_correct"/>' . "\n";
  87. }
  88. /**
  89. * Export the question as an IMS/QTI Item.
  90. *
  91. * This is a default behaviour, some classes may want to override this.
  92. *
  93. * @param $standalone: Boolean stating if it should be exported as a stand-alone question
  94. * @return string string, the XML flow for an Item.
  95. */
  96. function export($standalone = false)
  97. {
  98. $head = $foot = "";
  99. if ($standalone) {
  100. $head = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n";
  101. }
  102. //TODO understand why answer might be a non-object sometimes
  103. if (!is_object($this->answer)) {
  104. return $head;
  105. }
  106. $res = $head
  107. .$this->start_item()
  108. .$this->answer->imsExportResponsesDeclaration($this->questionIdent)
  109. .$this->start_item_body()
  110. .$this->answer->imsExportResponses(
  111. $this->questionIdent,
  112. $this->question->question,
  113. $this->question->description,
  114. $this->question->picture
  115. )
  116. .$this->end_item_body()
  117. .$this->add_response_processing()
  118. .$this->end_item()
  119. .$foot;
  120. return $res;
  121. }
  122. }
  123. /**
  124. * This class represents an entire exercise to be exported in IMS/QTI.
  125. * It will be represented by a single <section> containing several <item>.
  126. *
  127. * Some properties cannot be exported, as IMS does not support them :
  128. * - type (one page or multiple pages)
  129. * - start_date and end_date
  130. * - max_attempts
  131. * - show_answer
  132. * - anonymous_attempts
  133. *
  134. * @author Amand Tihon <amand@alrj.org>
  135. * @package chamilo.exercise
  136. */
  137. class ImsSection
  138. {
  139. public $exercise;
  140. /**
  141. * Constructor.
  142. * @param Exercise $exe The Exercise instance to export
  143. * @return ImsSection
  144. * @author Amand Tihon <amand@alrj.org>
  145. */
  146. function __construct($exe)
  147. {
  148. $this->exercise = $exe;
  149. }
  150. function start_section()
  151. {
  152. $out = '<section ident="EXO_' . $this->exercise->selectId() . '" title="' .cleanAttribute(formatExerciseQtiDescription($this->exercise->selectTitle())) . '">' . "\n";
  153. return $out;
  154. }
  155. function end_section()
  156. {
  157. return "</section>\n";
  158. }
  159. function export_duration()
  160. {
  161. if ($max_time = $this->exercise->selectTimeLimit())
  162. {
  163. // return exercise duration in ISO8601 format.
  164. $minutes = floor($max_time / 60);
  165. $seconds = $max_time % 60;
  166. return '<duration>PT' . $minutes . 'M' . $seconds . "S</duration>\n";
  167. } else {
  168. return '';
  169. }
  170. }
  171. /**
  172. * Export the presentation (Exercise's description)
  173. * @author Amand Tihon <amand@alrj.org>
  174. */
  175. function export_presentation()
  176. {
  177. $out = "<presentation_material><flow_mat><material>\n"
  178. . " <mattext><![CDATA[" . formatExerciseQtiDescription($this->exercise->selectDescription()) . "]]></mattext>\n"
  179. . "</material></flow_mat></presentation_material>\n";
  180. return $out;
  181. }
  182. /**
  183. * Export the ordering information.
  184. * Either sequential, through all questions, or random, with a selected number of questions.
  185. * @author Amand Tihon <amand@alrj.org>
  186. */
  187. function export_ordering()
  188. {
  189. $out = '';
  190. if ($n = $this->exercise->getShuffle()) {
  191. $out.= "<selection_ordering>"
  192. . " <selection>\n"
  193. . " <selection_number>" . $n . "</selection_number>\n"
  194. . " </selection>\n"
  195. . ' <order order_type="Random" />'
  196. . "\n</selection_ordering>\n";
  197. } else {
  198. $out.= '<selection_ordering sequence_type="Normal">' . "\n"
  199. . " <selection />\n"
  200. . "</selection_ordering>\n";
  201. }
  202. return $out;
  203. }
  204. /**
  205. * Export the questions, as a succession of <items>
  206. * @author Amand Tihon <amand@alrj.org>
  207. */
  208. function export_questions()
  209. {
  210. $out = "";
  211. foreach ($this->exercise->selectQuestionList() as $q) {
  212. $out .= export_question_qti($q, false);
  213. }
  214. return $out;
  215. }
  216. /**
  217. * Export the exercise in IMS/QTI.
  218. *
  219. * @param bool $standalone Wether it should include XML tag and DTD line.
  220. * @return string string containing the XML flow
  221. * @author Amand Tihon <amand@alrj.org>
  222. */
  223. function export($standalone)
  224. {
  225. $head = $foot = "";
  226. if ($standalone) {
  227. $head = '<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>' . "\n"
  228. . '<!DOCTYPE questestinterop SYSTEM "ims_qtiasiv2p1.dtd">' . "\n"
  229. . "<questestinterop>\n";
  230. $foot = "</questestinterop>\n";
  231. }
  232. $out = $head
  233. . $this->start_section()
  234. . $this->export_duration()
  235. . $this->export_presentation()
  236. . $this->export_ordering()
  237. . $this->export_questions()
  238. . $this->end_section()
  239. . $foot;
  240. return $out;
  241. }
  242. }
  243. /*
  244. Some quick notes on identifiers generation.
  245. The IMS format requires some blocks, like items, responses, feedbacks, to be uniquely
  246. identified.
  247. The unicity is mandatory in a single XML, of course, but it's prefered that the identifier stays
  248. coherent for an entire site.
  249. Here's the method used to generate those identifiers.
  250. Question identifier :: "QST_" + <Question Id from the DB> + "_" + <Question numeric type>
  251. Response identifier :: <Question identifier> + "_A_" + <Response Id from the DB>
  252. Condition identifier :: <Question identifier> + "_C_" + <Response Id from the DB>
  253. Feedback identifier :: <Question identifier> + "_F_" + <Response Id from the DB>
  254. */
  255. /**
  256. * Class ImsItem
  257. *
  258. * An IMS/QTI item. It corresponds to a single question.
  259. * This class allows export from Claroline to IMS/QTI XML format.
  260. * It is not usable as-is, but must be subclassed, to support different kinds of questions.
  261. *
  262. * Every start_*() and corresponding end_*(), as well as export_*() methods return a string.
  263. *
  264. * warning: Attached files are NOT exported.
  265. * @author Amand Tihon <amand@alrj.org>
  266. *
  267. * @package chamilo.exercise
  268. */
  269. class ImsItem
  270. {
  271. public $question;
  272. public $question_ident;
  273. public $answer;
  274. /**
  275. * Constructor.
  276. *
  277. * @param $question The Question object we want to export.
  278. * @return ImsItem
  279. * @author Anamd Tihon
  280. */
  281. function __construct($question)
  282. {
  283. $this->question = $question;
  284. $this->answer = $question->answer;
  285. $this->questionIdent = "QST_" . $question->selectId() ;
  286. }
  287. /**
  288. * Start the XML flow.
  289. *
  290. * This opens the <item> block, with correct attributes.
  291. *
  292. * @author Amand Tihon <amand@alrj.org>
  293. */
  294. function start_item()
  295. {
  296. return '<item title="' . cleanAttribute(formatExerciseQtiDescription($this->question->selectTitle())) . '" ident="' . $this->questionIdent . '">' . "\n";
  297. }
  298. /**
  299. * End the XML flow, closing the </item> tag.
  300. *
  301. * @author Amand Tihon <amand@alrj.org>
  302. */
  303. function end_item()
  304. {
  305. return "</item>\n";
  306. }
  307. /**
  308. * Create the opening, with the question itself.
  309. *
  310. * This means it opens the <presentation> but doesn't close it, as this is the role of end_presentation().
  311. * In between, the export_responses from the subclass should have been called.
  312. *
  313. * @author Amand Tihon <amand@alrj.org>
  314. */
  315. function start_presentation()
  316. {
  317. return '<presentation label="' . $this->questionIdent . '"><flow>' . "\n"
  318. . '<material><mattext>' . formatExerciseQtiDescription($this->question->selectDescription()) . "</mattext></material>\n";
  319. }
  320. /**
  321. * End the </presentation> part, opened by export_header.
  322. *
  323. * @author Amand Tihon <amand@alrj.org>
  324. */
  325. function end_presentation()
  326. {
  327. return "</flow></presentation>\n";
  328. }
  329. /**
  330. * Start the response processing, and declare the default variable, SCORE, at 0 in the outcomes.
  331. *
  332. * @author Amand Tihon <amand@alrj.org>
  333. */
  334. function start_processing()
  335. {
  336. return '<resprocessing><outcomes><decvar vartype="Integer" defaultval="0" /></outcomes>' . "\n";
  337. }
  338. /**
  339. * End the response processing part.
  340. *
  341. * @author Amand Tihon <amand@alrj.org>
  342. */
  343. function end_processing()
  344. {
  345. return "</resprocessing>\n";
  346. }
  347. /**
  348. * Export the question as an IMS/QTI Item.
  349. *
  350. * This is a default behaviour, some classes may want to override this.
  351. *
  352. * @param $standalone: Boolean stating if it should be exported as a stand-alone question
  353. * @return string string, the XML flow for an Item.
  354. * @author Amand Tihon <amand@alrj.org>
  355. */
  356. function export($standalone = False)
  357. {
  358. global $charset;
  359. $head = $foot = "";
  360. if ($standalone) {
  361. $head = '<?xml version = "1.0" encoding = "'.$charset.'" standalone = "no"?>' . "\n"
  362. . '<!DOCTYPE questestinterop SYSTEM "ims_qtiasiv2p1.dtd">' . "\n"
  363. . "<questestinterop>\n";
  364. $foot = "</questestinterop>\n";
  365. }
  366. return $head
  367. . $this->start_item()
  368. . $this->start_presentation()
  369. . $this->answer->imsExportResponses($this->questionIdent)
  370. . $this->end_presentation()
  371. . $this->start_processing()
  372. . $this->answer->imsExportProcessing($this->questionIdent)
  373. . $this->end_processing()
  374. . $this->answer->imsExportFeedback($this->questionIdent)
  375. . $this->end_item()
  376. . $foot;
  377. }
  378. }
  379. /**
  380. * Send a complete exercise in IMS/QTI format, from its ID
  381. *
  382. * @param int $exerciseId The exercise to export
  383. * @param boolean $standalone Wether it should include XML tag and DTD line.
  384. * @return string XML as a string, or an empty string if there's no exercise with given ID.
  385. */
  386. function export_exercise_to_qti($exerciseId, $standalone = true)
  387. {
  388. $exercise = new Exercise();
  389. if (!$exercise->read($exerciseId)) {
  390. return '';
  391. }
  392. $ims = new ImsSection($exercise);
  393. $xml = $ims->export($standalone);
  394. return $xml;
  395. }
  396. /**
  397. * Returns the XML flow corresponding to one question
  398. *
  399. * @param int $questionId
  400. * @param bool $standalone (ie including XML tag, DTD declaration, etc)
  401. * @return string
  402. */
  403. function export_question_qti($questionId, $standalone = true)
  404. {
  405. $question = new Ims2Question();
  406. $qst = $question->read($questionId);
  407. if (!$qst or $qst->type == FREE_ANSWER) {
  408. return '';
  409. }
  410. $question->id = $qst->id;
  411. $question->type = $qst->type;
  412. $question->question = $qst->question;
  413. $question->description = $qst->description;
  414. $question->weighting=$qst->weighting;
  415. $question->position=$qst->position;
  416. $question->picture=$qst->picture;
  417. $ims = new ImsAssessmentItem($question);
  418. return $ims->export($standalone);
  419. }
  420. /**
  421. * Clean text like a description
  422. **/
  423. function formatExerciseQtiDescription($text)
  424. {
  425. $entities = api_html_entity_decode($text);
  426. return htmlspecialchars($entities);
  427. }
  428. /**
  429. * Clean titles
  430. * @param $text
  431. * @return string
  432. */
  433. function formatExerciseQtiTitle($text)
  434. {
  435. return htmlspecialchars($text);
  436. }
  437. /**
  438. * @param string $text
  439. */
  440. function cleanAttribute($text)
  441. {
  442. return $text;
  443. }