qti2_classes.php 14 KB

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