qti2_classes.php 13 KB

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