qti2_classes.php 14 KB

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