qti2_export.php 14 KB

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