qti2_export.php 14 KB

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