qti2_export.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 $question Ims2Question object we want to export.
  34. */
  35. function ImsAssessmentItem($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 A 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. var $exercise;
  140. /**
  141. * Constructor.
  142. * @param $exe The Exercise instance to export
  143. * @author Amand Tihon <amand@alrj.org>
  144. */
  145. function ImsSection($exe)
  146. {
  147. $this->exercise = $exe;
  148. }
  149. function start_section()
  150. {
  151. $out = '<section ident="EXO_' . $this->exercise->selectId() . '" title="' .cleanAttribute(formatExerciseQtiDescription($this->exercise->selectTitle())) . '">' . "\n";
  152. return $out;
  153. }
  154. function end_section()
  155. {
  156. return "</section>\n";
  157. }
  158. function export_duration()
  159. {
  160. if ($max_time = $this->exercise->selectTimeLimit())
  161. {
  162. // return exercise duration in ISO8601 format.
  163. $minutes = floor($max_time / 60);
  164. $seconds = $max_time % 60;
  165. return '<duration>PT' . $minutes . 'M' . $seconds . "S</duration>\n";
  166. } else {
  167. return '';
  168. }
  169. }
  170. /**
  171. * Export the presentation (Exercise's description)
  172. * @author Amand Tihon <amand@alrj.org>
  173. */
  174. function export_presentation()
  175. {
  176. $out = "<presentation_material><flow_mat><material>\n"
  177. . " <mattext><![CDATA[" . formatExerciseQtiDescription($this->exercise->selectDescription()) . "]]></mattext>\n"
  178. . "</material></flow_mat></presentation_material>\n";
  179. return $out;
  180. }
  181. /**
  182. * Export the ordering information.
  183. * Either sequential, through all questions, or random, with a selected number of questions.
  184. * @author Amand Tihon <amand@alrj.org>
  185. */
  186. function export_ordering()
  187. {
  188. $out = '';
  189. if ($n = $this->exercise->getShuffle()) {
  190. $out.= "<selection_ordering>"
  191. . " <selection>\n"
  192. . " <selection_number>" . $n . "</selection_number>\n"
  193. . " </selection>\n"
  194. . ' <order order_type="Random" />'
  195. . "\n</selection_ordering>\n";
  196. } else {
  197. $out.= '<selection_ordering sequence_type="Normal">' . "\n"
  198. . " <selection />\n"
  199. . "</selection_ordering>\n";
  200. }
  201. return $out;
  202. }
  203. /**
  204. * Export the questions, as a succession of <items>
  205. * @author Amand Tihon <amand@alrj.org>
  206. */
  207. function export_questions()
  208. {
  209. $out = "";
  210. foreach ($this->exercise->selectQuestionList() as $q) {
  211. $out .= export_question_qti($q, false);
  212. }
  213. return $out;
  214. }
  215. /**
  216. * Export the exercise in IMS/QTI.
  217. *
  218. * @param bool $standalone Wether it should include XML tag and DTD line.
  219. * @return a string containing the XML flow
  220. * @author Amand Tihon <amand@alrj.org>
  221. */
  222. function export($standalone)
  223. {
  224. $head = $foot = "";
  225. if ($standalone) {
  226. $head = '<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>' . "\n"
  227. . '<!DOCTYPE questestinterop SYSTEM "ims_qtiasiv2p1.dtd">' . "\n"
  228. . "<questestinterop>\n";
  229. $foot = "</questestinterop>\n";
  230. }
  231. $out = $head
  232. . $this->start_section()
  233. . $this->export_duration()
  234. . $this->export_presentation()
  235. . $this->export_ordering()
  236. . $this->export_questions()
  237. . $this->end_section()
  238. . $foot;
  239. return $out;
  240. }
  241. }
  242. /*
  243. Some quick notes on identifiers generation.
  244. The IMS format requires some blocks, like items, responses, feedbacks, to be uniquely
  245. identified.
  246. The unicity is mandatory in a single XML, of course, but it's prefered that the identifier stays
  247. coherent for an entire site.
  248. Here's the method used to generate those identifiers.
  249. Question identifier :: "QST_" + <Question Id from the DB> + "_" + <Question numeric type>
  250. Response identifier :: <Question identifier> + "_A_" + <Response Id from the DB>
  251. Condition identifier :: <Question identifier> + "_C_" + <Response Id from the DB>
  252. Feedback identifier :: <Question identifier> + "_F_" + <Response Id from the DB>
  253. */
  254. /**
  255. * Class ImsItem
  256. *
  257. * An IMS/QTI item. It corresponds to a single question.
  258. * This class allows export from Claroline to IMS/QTI XML format.
  259. * It is not usable as-is, but must be subclassed, to support different kinds of questions.
  260. *
  261. * Every start_*() and corresponding end_*(), as well as export_*() methods return a string.
  262. *
  263. * warning: Attached files are NOT exported.
  264. * @author Amand Tihon <amand@alrj.org>
  265. *
  266. * @package chamilo.exercise
  267. */
  268. class ImsItem
  269. {
  270. public $question;
  271. public $question_ident;
  272. public $answer;
  273. /**
  274. * Constructor.
  275. *
  276. * @param $question The Question object we want to export.
  277. * @author Anamd Tihon
  278. */
  279. function ImsItem($question)
  280. {
  281. $this->question = $question;
  282. $this->answer = $question->answer;
  283. $this->questionIdent = "QST_" . $question->selectId() ;
  284. }
  285. /**
  286. * Start the XML flow.
  287. *
  288. * This opens the <item> block, with correct attributes.
  289. *
  290. * @author Amand Tihon <amand@alrj.org>
  291. */
  292. function start_item()
  293. {
  294. return '<item title="' . cleanAttribute(formatExerciseQtiDescription($this->question->selectTitle())) . '" ident="' . $this->questionIdent . '">' . "\n";
  295. }
  296. /**
  297. * End the XML flow, closing the </item> tag.
  298. *
  299. * @author Amand Tihon <amand@alrj.org>
  300. */
  301. function end_item()
  302. {
  303. return "</item>\n";
  304. }
  305. /**
  306. * Create the opening, with the question itself.
  307. *
  308. * This means it opens the <presentation> but doesn't close it, as this is the role of end_presentation().
  309. * In between, the export_responses from the subclass should have been called.
  310. *
  311. * @author Amand Tihon <amand@alrj.org>
  312. */
  313. function start_presentation()
  314. {
  315. return '<presentation label="' . $this->questionIdent . '"><flow>' . "\n"
  316. . '<material><mattext>' . formatExerciseQtiDescription($this->question->selectDescription()) . "</mattext></material>\n";
  317. }
  318. /**
  319. * End the </presentation> part, opened by export_header.
  320. *
  321. * @author Amand Tihon <amand@alrj.org>
  322. */
  323. function end_presentation()
  324. {
  325. return "</flow></presentation>\n";
  326. }
  327. /**
  328. * Start the response processing, and declare the default variable, SCORE, at 0 in the outcomes.
  329. *
  330. * @author Amand Tihon <amand@alrj.org>
  331. */
  332. function start_processing()
  333. {
  334. return '<resprocessing><outcomes><decvar vartype="Integer" defaultval="0" /></outcomes>' . "\n";
  335. }
  336. /**
  337. * End the response processing part.
  338. *
  339. * @author Amand Tihon <amand@alrj.org>
  340. */
  341. function end_processing()
  342. {
  343. return "</resprocessing>\n";
  344. }
  345. /**
  346. * Export the question as an IMS/QTI Item.
  347. *
  348. * This is a default behaviour, some classes may want to override this.
  349. *
  350. * @param $standalone: Boolean stating if it should be exported as a stand-alone question
  351. * @return A string, the XML flow for an Item.
  352. * @author Amand Tihon <amand@alrj.org>
  353. */
  354. function export($standalone = False)
  355. {
  356. global $charset;
  357. $head = $foot = "";
  358. if ($standalone) {
  359. $head = '<?xml version = "1.0" encoding = "'.$charset.'" standalone = "no"?>' . "\n"
  360. . '<!DOCTYPE questestinterop SYSTEM "ims_qtiasiv2p1.dtd">' . "\n"
  361. . "<questestinterop>\n";
  362. $foot = "</questestinterop>\n";
  363. }
  364. return $head
  365. . $this->start_item()
  366. . $this->start_presentation()
  367. . $this->answer->imsExportResponses($this->questionIdent)
  368. . $this->end_presentation()
  369. . $this->start_processing()
  370. . $this->answer->imsExportProcessing($this->questionIdent)
  371. . $this->end_processing()
  372. . $this->answer->imsExportFeedback($this->questionIdent)
  373. . $this->end_item()
  374. . $foot;
  375. }
  376. }
  377. /**
  378. * Send a complete exercise in IMS/QTI format, from its ID
  379. *
  380. * @param int $exerciseId The exercise to export
  381. * @param boolean $standalone Wether it should include XML tag and DTD line.
  382. * @return The XML as a string, or an empty string if there's no exercise with given ID.
  383. */
  384. function export_exercise_to_qti($exerciseId, $standalone = true)
  385. {
  386. $exercise = new Exercise();
  387. if (!$exercise->read($exerciseId)) {
  388. return '';
  389. }
  390. $ims = new ImsSection($exercise);
  391. $xml = $ims->export($standalone);
  392. return $xml;
  393. }
  394. /**
  395. * Returns the XML flow corresponding to one question
  396. *
  397. * @param int $questionId
  398. * @param bool $standalone (ie including XML tag, DTD declaration, etc)
  399. */
  400. function export_question_qti($questionId, $standalone = true)
  401. {
  402. $question = new Ims2Question();
  403. $qst = $question->read($questionId);
  404. if (!$qst or $qst->type == FREE_ANSWER) {
  405. return '';
  406. }
  407. $question->id = $qst->id;
  408. $question->type = $qst->type;
  409. $question->question = $qst->question;
  410. $question->description = $qst->description;
  411. $question->weighting=$qst->weighting;
  412. $question->position=$qst->position;
  413. $question->picture=$qst->picture;
  414. $ims = new ImsAssessmentItem($question);
  415. return $ims->export($standalone);
  416. }
  417. /**
  418. * Clean text like a description
  419. **/
  420. function formatExerciseQtiDescription($text)
  421. {
  422. $entities = api_html_entity_decode($text);
  423. return htmlspecialchars($entities);
  424. }
  425. /**
  426. * Clean titles
  427. * @param $text
  428. * @return string
  429. */
  430. function formatExerciseQtiTitle($text)
  431. {
  432. return htmlspecialchars($text);
  433. }
  434. function cleanAttribute($text)
  435. {
  436. return $text;
  437. }