qti_export.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php // $Id: $
  2. if ( count( get_included_files() ) == 1 ) die( '---' );
  3. /**
  4. * @copyright (c) 2007 Dokeos
  5. * @copyright (c) 2001-2006 Universite catholique de Louvain (UCL)
  6. *
  7. * @license http://www.gnu.org/copyleft/gpl.html (GPL) GENERAL PUBLIC LICENSE
  8. *
  9. * @author Claro Team <cvs@claroline.net>
  10. * @author Amand Tihon <amand@alrj.org>
  11. * @author Sebastien Piraux <pir@cerdecam.be>
  12. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  13. *
  14. */
  15. include dirname(__FILE__) . '/qti_classes.php';
  16. /*--------------------------------------------------------
  17. Classes
  18. --------------------------------------------------------*/
  19. /**
  20. * This class represents an entire exercise to be exported in IMS/QTI.
  21. * It will be represented by a single <section> containing several <item>.
  22. *
  23. * Some properties cannot be exported, as IMS does not support them :
  24. * - type (one page or multiple pages)
  25. * - start_date and end_date
  26. * - max_attempts
  27. * - show_answer
  28. * - anonymous_attempts
  29. *
  30. * @author Amand Tihon <amand@alrj.org>
  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. * @author Amand Tihon <amand@alrj.org>
  164. */
  165. class ImsItem
  166. {
  167. var $question;
  168. var $question_ident;
  169. var $answer;
  170. /**
  171. * Constructor.
  172. *
  173. * @param $question The Question object we want to export.
  174. * @author Anamd Tihon
  175. */
  176. function ImsItem($question)
  177. {
  178. $this->question = $question;
  179. $this->answer = $question->answer;
  180. $this->questionIdent = "QST_" . $question->getId() ;
  181. }
  182. /**
  183. * Start the XML flow.
  184. *
  185. * This opens the <item> block, with correct attributes.
  186. *
  187. * @author Amand Tihon <amand@alrj.org>
  188. */
  189. function start_item()
  190. {
  191. return '<item title="' . htmlspecialchars($this->question->getTitle()) . '" ident="' . $this->questionIdent . '">' . "\n";
  192. }
  193. /**
  194. * End the XML flow, closing the </item> tag.
  195. *
  196. * @author Amand Tihon <amand@alrj.org>
  197. */
  198. function end_item()
  199. {
  200. return "</item>\n";
  201. }
  202. /**
  203. * Create the opening, with the question itself.
  204. *
  205. * This means it opens the <presentation> but doesn't close it, as this is the role of end_presentation().
  206. * Inbetween, the export_responses from the subclass should have been called.
  207. *
  208. * @author Amand Tihon <amand@alrj.org>
  209. */
  210. function start_presentation()
  211. {
  212. return '<presentation label="' . $this->questionIdent . '"><flow>' . "\n"
  213. . '<material><mattext><![CDATA[' . $this->question->getDescription() . "]]></mattext></material>\n";
  214. }
  215. /**
  216. * End the </presentation> part, opened by export_header.
  217. *
  218. * @author Amand Tihon <amand@alrj.org>
  219. */
  220. function end_presentation()
  221. {
  222. return "</flow></presentation>\n";
  223. }
  224. /**
  225. * Start the response processing, and declare the default variable, SCORE, at 0 in the outcomes.
  226. *
  227. * @author Amand Tihon <amand@alrj.org>
  228. */
  229. function start_processing()
  230. {
  231. return '<resprocessing><outcomes><decvar vartype="Integer" defaultval="0" /></outcomes>' . "\n";
  232. }
  233. /**
  234. * End the response processing part.
  235. *
  236. * @author Amand Tihon <amand@alrj.org>
  237. */
  238. function end_processing()
  239. {
  240. return "</resprocessing>\n";
  241. }
  242. /**
  243. * Export the question as an IMS/QTI Item.
  244. *
  245. * This is a default behaviour, some classes may want to override this.
  246. *
  247. * @param $standalone: Boolean stating if it should be exported as a stand-alone question
  248. * @return A string, the XML flow for an Item.
  249. * @author Amand Tihon <amand@alrj.org>
  250. */
  251. function export($standalone = False)
  252. {
  253. global $charset;
  254. $head = $foot = "";
  255. if( $standalone )
  256. {
  257. $head = '<?xml version = "1.0" encoding = "'.$charset.'" standalone = "no"?>' . "\n"
  258. . '<!DOCTYPE questestinterop SYSTEM "ims_qtiasiv1p2p1.dtd">' . "\n"
  259. . "<questestinterop>\n";
  260. $foot = "</questestinterop>\n";
  261. }
  262. return $head
  263. . $this->start_item()
  264. . $this->start_presentation()
  265. . $this->answer->imsExportResponses($this->questionIdent)
  266. . $this->end_presentation()
  267. . $this->start_processing()
  268. . $this->answer->imsExportProcessing($this->questionIdent)
  269. . $this->end_processing()
  270. . $this->answer->imsExportFeedback($this->questionIdent)
  271. . $this->end_item()
  272. . $foot;
  273. }
  274. }
  275. /*--------------------------------------------------------
  276. Functions
  277. --------------------------------------------------------*/
  278. /**
  279. * Send a complete exercise in IMS/QTI format, from its ID
  280. *
  281. * @param int $exerciseId The exercise to exporte
  282. * @param boolean $standalone Wether it should include XML tag and DTD line.
  283. * @return The XML as a string, or an empty string if there's no exercise with given ID.
  284. * @author Amand Tihon <amand@alrj.org>
  285. */
  286. function export_exercise($exerciseId, $standalone=True)
  287. {
  288. $exercise = new Exercise();
  289. if (! $exercise->load($exerciseId))
  290. {
  291. return '';
  292. }
  293. $ims = new ImsSection($exercise);
  294. $xml = $ims->export($standalone);
  295. return $xml;
  296. }
  297. /**
  298. * Returns the XML flow corresponding to one question
  299. *
  300. * @param int The question ID
  301. * @param bool standalone (ie including XML tag, DTD declaration, etc)
  302. * @author Amand Tihon <amand@alrj.org>
  303. */
  304. function export_question($questionId, $standalone=True)
  305. {
  306. $question = new ImsQuestion();
  307. if( !$question->load($questionId) )
  308. {
  309. return '';
  310. }
  311. $ims = new ImsItem($question);
  312. return $ims->export($standalone);
  313. }
  314. ?>