qti2_export.php 14 KB

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