qti_export.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php // $Id: $
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @author Claro Team <cvs@claroline.net>
  5. * @author Amand Tihon <amand@alrj.org>
  6. * @author Sebastien Piraux <pir@cerdecam.be>
  7. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  8. * @package chamilo.exercise.qti
  9. */
  10. /**
  11. * Code
  12. */
  13. if ( count( get_included_files() ) == 1 ) die( '---' );
  14. include dirname(__FILE__) . '/qti_classes.php';
  15. /*--------------------------------------------------------
  16. Classes
  17. --------------------------------------------------------*/
  18. /**
  19. * This class represents an entire exercise to be exported in IMS/QTI.
  20. * It will be represented by a single <section> containing several <item>.
  21. *
  22. * Some properties cannot be exported, as IMS does not support them :
  23. * - type (one page or multiple pages)
  24. * - start_date and end_date
  25. * - max_attempts
  26. * - show_answer
  27. * - anonymous_attempts
  28. *
  29. * @author Amand Tihon <amand@alrj.org>
  30. * @package chamilo.exercise.qti
  31. */
  32. class ImsSection
  33. {
  34. var $exercise;
  35. /**
  36. * Constructor.
  37. * @param $exe The Exercise instance to export
  38. * @author Amand Tihon <amand@alrj.org>
  39. */
  40. function ImsSection($exe)
  41. {
  42. $this->exercise = $exe;
  43. }
  44. function start_section()
  45. {
  46. $out = '<section ident="EXO_' . $this->exercise->getId() . '" title="' . $this->exercise->getTitle() . '">' . "\n";
  47. return $out;
  48. }
  49. function end_section()
  50. {
  51. return "</section>\n";
  52. }
  53. function export_duration()
  54. {
  55. if ($max_time = $this->exercise->getTimeLimit())
  56. {
  57. // return exercise duration in ISO8601 format.
  58. $minutes = floor($max_time / 60);
  59. $seconds = $max_time % 60;
  60. return '<duration>PT' . $minutes . 'M' . $seconds . "S</duration>\n";
  61. }
  62. else
  63. {
  64. return '';
  65. }
  66. }
  67. /**
  68. * Export the presentation (Exercise's description)
  69. * @author Amand Tihon <amand@alrj.org>
  70. */
  71. function export_presentation()
  72. {
  73. $out = "<presentation_material><flow_mat><material>\n"
  74. . " <mattext><![CDATA[" . $this->exercise->getDescription() . "]]></mattext>\n"
  75. . "</material></flow_mat></presentation_material>\n";
  76. return $out;
  77. }
  78. /**
  79. * Export the ordering information.
  80. * Either sequential, through all questions, or random, with a selected number of questions.
  81. * @author Amand Tihon <amand@alrj.org>
  82. */
  83. function export_ordering()
  84. {
  85. $out = '';
  86. if ($n = $this->exercise->getShuffle()) {
  87. $out.= "<selection_ordering>"
  88. . " <selection>\n"
  89. . " <selection_number>" . $n . "</selection_number>\n"
  90. . " </selection>\n"
  91. . ' <order order_type="Random" />'
  92. . "\n</selection_ordering>\n";
  93. }
  94. else
  95. {
  96. $out.= '<selection_ordering sequence_type="Normal">' . "\n"
  97. . " <selection />\n"
  98. . "</selection_ordering>\n";
  99. }
  100. return $out;
  101. }
  102. /**
  103. * Export the questions, as a succession of <items>
  104. * @author Amand Tihon <amand@alrj.org>
  105. */
  106. function export_questions()
  107. {
  108. $out = "";
  109. foreach ($this->exercise->getQuestionList() as $q)
  110. {
  111. $out .= export_question($q['id'], False);
  112. }
  113. return $out;
  114. }
  115. /**
  116. * Export the exercise in IMS/QTI.
  117. *
  118. * @param bool $standalone Wether it should include XML tag and DTD line.
  119. * @return a string containing the XML flow
  120. * @author Amand Tihon <amand@alrj.org>
  121. */
  122. function export($standalone)
  123. {
  124. global $charset;
  125. $head = $foot = "";
  126. if ($standalone) {
  127. $head = '<?xml version = "1.0" encoding = "' . $charset . '" standalone = "no"?>' . "\n"
  128. . '<!DOCTYPE questestinterop SYSTEM "ims_qtiasiv1p2p1.dtd">' . "\n"
  129. . "<questestinterop>\n";
  130. $foot = "</questestinterop>\n";
  131. }
  132. $out = $head
  133. . $this->start_section()
  134. . $this->export_duration()
  135. . $this->export_presentation()
  136. . $this->export_ordering()
  137. . $this->export_questions()
  138. . $this->end_section()
  139. . $foot;
  140. return $out;
  141. }
  142. }
  143. /*
  144. Some quick notes on identifiers generation.
  145. The IMS format requires some blocks, like items, responses, feedbacks, to be uniquely
  146. identified.
  147. The unicity is mandatory in a single XML, of course, but it's prefered that the identifier stays
  148. coherent for an entire site.
  149. Here's the method used to generate those identifiers.
  150. Question identifier :: "QST_" + <Question Id from the DB> + "_" + <Question numeric type>
  151. Response identifier :: <Question identifier> + "_A_" + <Response Id from the DB>
  152. Condition identifier :: <Question identifier> + "_C_" + <Response Id from the DB>
  153. Feedback identifier :: <Question identifier> + "_F_" + <Response Id from the DB>
  154. */
  155. /**
  156. * An IMS/QTI item. It corresponds to a single question.
  157. * This class allows export from Claroline to IMS/QTI XML format.
  158. * It is not usable as-is, but must be subclassed, to support different kinds of questions.
  159. *
  160. * Every start_*() and corresponding end_*(), as well as export_*() methods return a string.
  161. *
  162. * Warning: Attached files are NOT exported.
  163. * @package chamilo.exercise.qti
  164. * @author Amand Tihon <amand@alrj.org>
  165. */
  166. class ImsItem
  167. {
  168. var $question;
  169. var $question_ident;
  170. var $answer;
  171. /**
  172. * Constructor.
  173. *
  174. * @param $question The Question object we want to export.
  175. * @author Anamd Tihon
  176. */
  177. function ImsItem($question)
  178. {
  179. $this->question = $question;
  180. $this->answer = $question->answer;
  181. $this->questionIdent = "QST_" . $question->getId() ;
  182. }
  183. /**
  184. * Start the XML flow.
  185. *
  186. * This opens the <item> block, with correct attributes.
  187. *
  188. * @author Amand Tihon <amand@alrj.org>
  189. */
  190. function start_item()
  191. {
  192. return '<item title="' . htmlspecialchars($this->question->getTitle()) . '" ident="' . $this->questionIdent . '">' . "\n";
  193. }
  194. /**
  195. * End the XML flow, closing the </item> tag.
  196. *
  197. * @author Amand Tihon <amand@alrj.org>
  198. */
  199. function end_item()
  200. {
  201. return "</item>\n";
  202. }
  203. /**
  204. * Create the opening, with the question itself.
  205. *
  206. * This means it opens the <presentation> but doesn't close it, as this is the role of end_presentation().
  207. * Inbetween, the export_responses from the subclass should have been called.
  208. *
  209. * @author Amand Tihon <amand@alrj.org>
  210. */
  211. function start_presentation()
  212. {
  213. return '<presentation label="' . $this->questionIdent . '"><flow>' . "\n"
  214. . '<material><mattext><![CDATA[' . $this->question->getDescription() . "]]></mattext></material>\n";
  215. }
  216. /**
  217. * End the </presentation> part, opened by export_header.
  218. *
  219. * @author Amand Tihon <amand@alrj.org>
  220. */
  221. function end_presentation()
  222. {
  223. return "</flow></presentation>\n";
  224. }
  225. /**
  226. * Start the response processing, and declare the default variable, SCORE, at 0 in the outcomes.
  227. *
  228. * @author Amand Tihon <amand@alrj.org>
  229. */
  230. function start_processing()
  231. {
  232. return '<resprocessing><outcomes><decvar vartype="Integer" defaultval="0" /></outcomes>' . "\n";
  233. }
  234. /**
  235. * End the response processing part.
  236. *
  237. * @author Amand Tihon <amand@alrj.org>
  238. */
  239. function end_processing()
  240. {
  241. return "</resprocessing>\n";
  242. }
  243. /**
  244. * Export the question as an IMS/QTI Item.
  245. *
  246. * This is a default behaviour, some classes may want to override this.
  247. *
  248. * @param $standalone: Boolean stating if it should be exported as a stand-alone question
  249. * @return A string, the XML flow for an Item.
  250. * @author Amand Tihon <amand@alrj.org>
  251. */
  252. function export($standalone = False)
  253. {
  254. global $charset;
  255. $head = $foot = "";
  256. if( $standalone )
  257. {
  258. $head = '<?xml version = "1.0" encoding = "'.$charset.'" standalone = "no"?>' . "\n"
  259. . '<!DOCTYPE questestinterop SYSTEM "ims_qtiasiv1p2p1.dtd">' . "\n"
  260. . "<questestinterop>\n";
  261. $foot = "</questestinterop>\n";
  262. }
  263. return $head
  264. . $this->start_item()
  265. . $this->start_presentation()
  266. . $this->answer->imsExportResponses($this->questionIdent)
  267. . $this->end_presentation()
  268. . $this->start_processing()
  269. . $this->answer->imsExportProcessing($this->questionIdent)
  270. . $this->end_processing()
  271. . $this->answer->imsExportFeedback($this->questionIdent)
  272. . $this->end_item()
  273. . $foot;
  274. }
  275. }
  276. /*--------------------------------------------------------
  277. Functions
  278. --------------------------------------------------------*/
  279. /**
  280. * Send a complete exercise in IMS/QTI format, from its ID
  281. *
  282. * @param int $exerciseId The exercise to exporte
  283. * @param boolean $standalone Wether it should include XML tag and DTD line.
  284. * @return The XML as a string, or an empty string if there's no exercise with given ID.
  285. * @author Amand Tihon <amand@alrj.org>
  286. */
  287. function export_exercise($exerciseId, $standalone=True)
  288. {
  289. $exercise = new Exercise();
  290. if (! $exercise->load($exerciseId))
  291. {
  292. return '';
  293. }
  294. $ims = new ImsSection($exercise);
  295. $xml = $ims->export($standalone);
  296. return $xml;
  297. }
  298. /**
  299. * Returns the XML flow corresponding to one question
  300. *
  301. * @param int The question ID
  302. * @param bool standalone (ie including XML tag, DTD declaration, etc)
  303. * @author Amand Tihon <amand@alrj.org>
  304. */
  305. function export_question($questionId, $standalone=True)
  306. {
  307. $question = new ImsQuestion();
  308. if( !$question->load($questionId) )
  309. {
  310. return '';
  311. }
  312. $ims = new ImsItem($question);
  313. return $ims->export($standalone);
  314. }
  315. ?>