qti2_classes.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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> - updated ImsAnswerHotspot to match QTI norms
  6. * @package chamilo.exercise
  7. */
  8. class Ims2Question extends Question
  9. {
  10. /**
  11. * Include the correct answer class and create answer
  12. * @return Answer
  13. */
  14. public function setAnswer()
  15. {
  16. switch ($this->type) {
  17. case MCUA:
  18. $answer = new ImsAnswerMultipleChoice($this->id);
  19. return $answer;
  20. case MCMA:
  21. $answer = new ImsAnswerMultipleChoice($this->id);
  22. return $answer;
  23. case TF:
  24. $answer = new ImsAnswerMultipleChoice($this->id);
  25. return $answer;
  26. case FIB:
  27. $answer = new ImsAnswerFillInBlanks($this->id);
  28. return $answer;
  29. case MATCHING:
  30. //no break
  31. case MATCHING_DRAGGABLE:
  32. $answer = new ImsAnswerMatching($this->id);
  33. return $answer;
  34. case FREE_ANSWER:
  35. $answer = new ImsAnswerFree($this->id);
  36. return $answer;
  37. case HOT_SPOT:
  38. $answer = new ImsAnswerHotspot($this->id);
  39. return $answer;
  40. default:
  41. $answer = null;
  42. break;
  43. }
  44. return $answer;
  45. }
  46. public function createAnswersForm($form)
  47. {
  48. return true;
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function processAnswersCreation($form, $exercise)
  54. {
  55. return true;
  56. }
  57. }
  58. /**
  59. * Class
  60. * @package chamilo.exercise
  61. */
  62. class ImsAnswerMultipleChoice extends Answer
  63. {
  64. /**
  65. * Return the XML flow for the possible answers.
  66. *
  67. */
  68. public function imsExportResponses($questionIdent, $questionStatment)
  69. {
  70. // @todo getAnswersList() converts the answers using api_html_entity_decode()
  71. $this->answerList = $this->getAnswersList(true);
  72. $out = ' <choiceInteraction responseIdentifier="'.$questionIdent.'" >'."\n";
  73. $out .= ' <prompt><![CDATA['.formatExerciseQtiTitle($questionStatment).']]></prompt>'."\n";
  74. if (is_array($this->answerList)) {
  75. foreach ($this->answerList as $current_answer) {
  76. $out .= '<simpleChoice identifier="answer_'.$current_answer['id'].'" fixed="false">
  77. <![CDATA['.formatExerciseQtiTitle($current_answer['answer']).']]>';
  78. if (isset($current_answer['comment']) && $current_answer['comment'] != '') {
  79. $out .= '<feedbackInline identifier="answer_'.$current_answer['id'].'">
  80. <![CDATA['.formatExerciseQtiTitle($current_answer['comment']).']]>
  81. </feedbackInline>';
  82. }
  83. $out .= '</simpleChoice>'."\n";
  84. }
  85. }
  86. $out .= ' </choiceInteraction>'."\n";
  87. return $out;
  88. }
  89. /**
  90. * Return the XML flow of answer ResponsesDeclaration
  91. *
  92. */
  93. public function imsExportResponsesDeclaration($questionIdent)
  94. {
  95. $this->answerList = $this->getAnswersList(true);
  96. $type = $this->getQuestionType();
  97. if ($type == MCMA) {
  98. $cardinality = 'multiple';
  99. } else {
  100. $cardinality = 'single';
  101. }
  102. $out = ' <responseDeclaration identifier="'.$questionIdent.'" cardinality="'.$cardinality.'" baseType="identifier">'."\n";
  103. // Match the correct answers.
  104. $out .= ' <correctResponse>'."\n";
  105. if (is_array($this->answerList)) {
  106. foreach ($this->answerList as $current_answer) {
  107. if ($current_answer['correct']) {
  108. $out .= ' <value>answer_'.$current_answer['id'].'</value>'."\n";
  109. }
  110. }
  111. }
  112. $out .= ' </correctResponse>'."\n";
  113. // Add the grading
  114. $out .= ' <mapping>'."\n";
  115. if (is_array($this->answerList)) {
  116. foreach ($this->answerList as $current_answer) {
  117. if (isset($current_answer['grade'])) {
  118. $out .= ' <mapEntry mapKey="answer_'.$current_answer['id'].'" mappedValue="'.$current_answer['grade'].'" />'."\n";
  119. }
  120. }
  121. }
  122. $out .= ' </mapping>'."\n";
  123. $out .= ' </responseDeclaration>'."\n";
  124. return $out;
  125. }
  126. }
  127. /**
  128. * Class
  129. * @package chamilo.exercise
  130. */
  131. class ImsAnswerFillInBlanks extends Answer
  132. {
  133. /**
  134. * Export the text with missing words.
  135. *
  136. *
  137. */
  138. public function imsExportResponses($questionIdent, $questionStatment)
  139. {
  140. $this->answerList = $this->getAnswersList(true);
  141. $text = isset($this->answerText) ? $this->answerText : '';
  142. if (is_array($this->answerList)) {
  143. foreach ($this->answerList as $key => $answer) {
  144. $key = $answer['id'];
  145. $answer = $answer['answer'];
  146. $len = api_strlen($answer);
  147. $text = str_replace('['.$answer.']', '<textEntryInteraction responseIdentifier="fill_'.$key.'" expectedLength="'.api_strlen($answer).'"/>', $text);
  148. }
  149. }
  150. $out = $text;
  151. return $out;
  152. }
  153. /**
  154. *
  155. */
  156. public function imsExportResponsesDeclaration($questionIdent)
  157. {
  158. $this->answerList = $this->getAnswersList(true);
  159. $this->gradeList = $this->getGradesList();
  160. $out = '';
  161. if (is_array($this->answerList)) {
  162. foreach ($this->answerList as $answer) {
  163. $answerKey = $answer['id'];
  164. $answer = $answer['answer'];
  165. $out .= ' <responseDeclaration identifier="fill_'.$answerKey.'" cardinality="single" baseType="identifier">'."\n";
  166. $out .= ' <correctResponse>'."\n";
  167. $out .= ' <value><![CDATA['.formatExerciseQtiTitle($answer).']]></value>'."\n";
  168. $out .= ' </correctResponse>'."\n";
  169. if (isset($this->gradeList[$answerKey])) {
  170. $out .= ' <mapping>'."\n";
  171. $out .= ' <mapEntry mapKey="'.$answer.'" mappedValue="'.$this->gradeList[$answerKey].'"/>'."\n";
  172. $out .= ' </mapping>'."\n";
  173. }
  174. $out .= ' </responseDeclaration>'."\n";
  175. }
  176. }
  177. return $out;
  178. }
  179. }
  180. /**
  181. * Class
  182. * @package chamilo.exercise
  183. */
  184. class ImsAnswerMatching extends Answer
  185. {
  186. /**
  187. * Export the question part as a matrix-choice, with only one possible answer per line.
  188. */
  189. public function imsExportResponses($questionIdent, $questionStatment)
  190. {
  191. $this->answerList = $this->getAnswersList(true);
  192. $maxAssociation = max(count($this->leftList), count($this->rightList));
  193. $out = '<matchInteraction responseIdentifier="'.$questionIdent.'" maxAssociations="'.$maxAssociation.'">'."\n";
  194. $out .= $questionStatment;
  195. //add left column
  196. $out .= ' <simpleMatchSet>'."\n";
  197. if (is_array($this->leftList)) {
  198. foreach ($this->leftList as $leftKey=>$leftElement) {
  199. $out .= '
  200. <simpleAssociableChoice identifier="left_'.$leftKey.'" >
  201. <![CDATA['.formatExerciseQtiTitle($leftElement['answer']).']]>
  202. </simpleAssociableChoice>'. "\n";
  203. }
  204. }
  205. $out .= ' </simpleMatchSet>'."\n";
  206. //add right column
  207. $out .= ' <simpleMatchSet>'."\n";
  208. $i = 0;
  209. if (is_array($this->rightList)) {
  210. foreach ($this->rightList as $rightKey => $rightElement) {
  211. $out .= '<simpleAssociableChoice identifier="right_'.$i.'" >
  212. <![CDATA['.formatExerciseQtiTitle($rightElement['answer']).']]>
  213. </simpleAssociableChoice>'. "\n";
  214. $i++;
  215. }
  216. }
  217. $out .= ' </simpleMatchSet>'."\n";
  218. $out .= '</matchInteraction>'."\n";
  219. return $out;
  220. }
  221. /**
  222. *
  223. */
  224. public function imsExportResponsesDeclaration($questionIdent)
  225. {
  226. $this->answerList = $this->getAnswersList(true);
  227. $out = ' <responseDeclaration identifier="'.$questionIdent.'" cardinality="single" baseType="identifier">'."\n";
  228. $out .= ' <correctResponse>'."\n";
  229. $gradeArray = array();
  230. if (isset($this->leftList) && is_array($this->leftList)) {
  231. foreach ($this->leftList as $leftKey => $leftElement) {
  232. $i = 0;
  233. foreach ($this->rightList as $rightKey => $rightElement) {
  234. if (($leftElement['match'] == $rightElement['code'])) {
  235. $out .= ' <value>left_'.$leftKey.' right_'.$i.'</value>'."\n";
  236. $gradeArray['left_'.$leftKey.' right_'.$i] = $leftElement['grade'];
  237. }
  238. $i++;
  239. }
  240. }
  241. }
  242. $out .= ' </correctResponse>'."\n";
  243. $out .= ' <mapping>'."\n";
  244. if (is_array($gradeArray)) {
  245. foreach ($gradeArray as $gradeKey => $grade) {
  246. $out .= ' <mapEntry mapKey="'.$gradeKey.'" mappedValue="'.$grade.'"/>'."\n";
  247. }
  248. }
  249. $out .= ' </mapping>'."\n";
  250. $out .= ' </responseDeclaration>'."\n";
  251. return $out;
  252. }
  253. }
  254. /**
  255. * Class
  256. * @package chamilo.exercise
  257. */
  258. class ImsAnswerHotspot extends Answer
  259. {
  260. /**
  261. * TODO update this to match hot spots instead of copying matching
  262. * Export the question part as a matrix-choice, with only one possible answer per line.
  263. */
  264. public function imsExportResponses($questionIdent, $questionStatment, $questionDesc = '', $questionMedia = '')
  265. {
  266. $this->answerList = $this->getAnswersList(true);
  267. $questionMedia = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/images/'.$questionMedia;
  268. $mimetype = mime_content_type($questionMedia);
  269. if (empty($mimetype)) {
  270. $mimetype = 'image/jpeg';
  271. }
  272. $text = ' <p>'.$questionStatment.'</p>'."\n";
  273. $text .= ' <graphicOrderInteraction responseIdentifier="hotspot_'.$questionIdent.'">'."\n";
  274. $text .= ' <prompt>'.$questionDesc.'</prompt>'."\n";
  275. $text .= ' <object type="'.$mimetype.'" width="250" height="230" data="'.$questionMedia.'">-</object>'."\n";
  276. if (is_array($this->answerList)) {
  277. foreach ($this->answerList as $key => $answer) {
  278. $key = $answer['id'];
  279. $answerTxt = $answer['answer'];
  280. $len = api_strlen($answerTxt);
  281. //coords are transformed according to QTIv2 rules here: http://www.imsproject.org/question/qtiv2p1pd/imsqti_infov2p1pd.html#element10663
  282. $coords = '';
  283. $type = 'default';
  284. switch ($answer['hotspot_type']) {
  285. case 'square':
  286. $type = 'rect';
  287. $res = array();
  288. $coords = preg_match('/^\s*(\d+);(\d+)\|(\d+)\|(\d+)\s*$/', $answer['hotspot_coord'], $res);
  289. $coords = $res[1].','.$res[2].','.((int) $res[1] + (int) $res[3]).",".((int) $res[2] + (int) $res[4]);
  290. break;
  291. case 'circle':
  292. $type = 'circle';
  293. $res = array();
  294. $coords = preg_match('/^\s*(\d+);(\d+)\|(\d+)\|(\d+)\s*$/', $answer['hotspot_coord'], $res);
  295. $coords = $res[1].','.$res[2].','.sqrt(pow(($res[1] - $res[3]), 2) + pow(($res[2] - $res[4])));
  296. break;
  297. case 'poly':
  298. $type = 'poly';
  299. $coords = str_replace(array(';', '|'), array(',', ','), $answer['hotspot_coord']);
  300. break;
  301. case 'delineation':
  302. $type = 'delineation';
  303. $coords = str_replace(array(';', '|'), array(',', ','), $answer['hotspot_coord']);
  304. break;
  305. }
  306. $text .= ' <hotspotChoice shape="'.$type.'" coords="'.$coords.'" identifier="'.$key.'"/>'."\n";
  307. }
  308. }
  309. $text .= ' </graphicOrderInteraction>'."\n";
  310. $out = $text;
  311. return $out;
  312. }
  313. /**
  314. *
  315. */
  316. public function imsExportResponsesDeclaration($questionIdent)
  317. {
  318. $this->answerList = $this->getAnswersList(true);
  319. $this->gradeList = $this->getGradesList();
  320. $out = '';
  321. $out .= ' <responseDeclaration identifier="hotspot_'.$questionIdent.'" cardinality="ordered" baseType="identifier">'."\n";
  322. $out .= ' <correctResponse>'."\n";
  323. if (is_array($this->answerList)) {
  324. foreach ($this->answerList as $answerKey => $answer) {
  325. $answerKey = $answer['id'];
  326. $answer = $answer['answer'];
  327. $out .= '<value><![CDATA['.formatExerciseQtiTitle($answerKey).']]></value>';
  328. }
  329. }
  330. $out .= ' </correctResponse>'."\n";
  331. $out .= ' </responseDeclaration>'."\n";
  332. return $out;
  333. }
  334. }
  335. /**
  336. * Class
  337. * @package chamilo.exercise
  338. */
  339. class ImsAnswerFree extends Answer
  340. {
  341. /**
  342. * TODO implement
  343. * Export the question part as a matrix-choice, with only one possible answer per line.
  344. */
  345. public function imsExportResponses($questionIdent, $questionStatment, $questionDesc = '', $questionMedia = '')
  346. {
  347. return '';
  348. }
  349. /**
  350. *
  351. */
  352. public function imsExportResponsesDeclaration($questionIdent)
  353. {
  354. return '';
  355. }
  356. }