qti2_classes.php 13 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. 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 = '';
  135. $text .= $this->answerText;
  136. if (is_array($this->answerList)) {
  137. foreach ($this->answerList as $key=>$answer) {
  138. $key = $answer['id'];
  139. $answer = $answer['answer'];
  140. $len = api_strlen($answer);
  141. $text = str_replace('['.$answer.']','<textEntryInteraction responseIdentifier="fill_'.$key.'" expectedLength="'.api_strlen($answer).'"/>', $text);
  142. }
  143. }
  144. $out = $text;
  145. return $out;
  146. }
  147. /**
  148. *
  149. */
  150. public function imsExportResponsesDeclaration($questionIdent)
  151. {
  152. $this->answerList = $this->getAnswersList(true);
  153. $this->gradeList = $this->getGradesList();
  154. $out = '';
  155. if (is_array($this->answerList)) {
  156. foreach ($this->answerList as $answer) {
  157. $answerKey = $answer['id'];
  158. $answer = $answer['answer'];
  159. $out .= ' <responseDeclaration identifier="fill_' . $answerKey . '" cardinality="single" baseType="identifier">' . "\n";
  160. $out .= ' <correctResponse>'. "\n";
  161. $out .= ' <value><![CDATA['.formatExerciseQtiTitle($answer).']]></value>'. "\n";
  162. $out .= ' </correctResponse>'. "\n";
  163. if (isset($this->gradeList[$answerKey])) {
  164. $out .= ' <mapping>'. "\n";
  165. $out .= ' <mapEntry mapKey="'.$answer.'" mappedValue="'.$this->gradeList[$answerKey].'"/>'. "\n";
  166. $out .= ' </mapping>'. "\n";
  167. }
  168. $out .= ' </responseDeclaration>'. "\n";
  169. }
  170. }
  171. return $out;
  172. }
  173. }
  174. /**
  175. * Class
  176. * @package chamilo.exercise
  177. */
  178. class ImsAnswerMatching extends Answer
  179. {
  180. /**
  181. * Export the question part as a matrix-choice, with only one possible answer per line.
  182. */
  183. public function imsExportResponses($questionIdent, $questionStatment)
  184. {
  185. $this->answerList = $this->getAnswersList(true);
  186. $maxAssociation = max(count($this->leftList), count($this->rightList));
  187. $out = "";
  188. $out .= '<matchInteraction responseIdentifier="' . $questionIdent . '" maxAssociations="'. $maxAssociation .'">'. "\n";
  189. $out .= $questionStatment;
  190. //add left column
  191. $out .= ' <simpleMatchSet>'. "\n";
  192. if (is_array($this->leftList)) {
  193. foreach ($this->leftList as $leftKey=>$leftElement) {
  194. $out .= '
  195. <simpleAssociableChoice identifier="left_'.$leftKey.'" >
  196. <![CDATA['.formatExerciseQtiTitle($leftElement['answer']).']]>
  197. </simpleAssociableChoice>'. "\n";
  198. }
  199. }
  200. $out .= ' </simpleMatchSet>'. "\n";
  201. //add right column
  202. $out .= ' <simpleMatchSet>'. "\n";
  203. $i = 0;
  204. if (is_array($this->rightList)) {
  205. foreach($this->rightList as $rightKey=>$rightElement) {
  206. $out .= '<simpleAssociableChoice identifier="right_'.$i.'" >
  207. <![CDATA['.formatExerciseQtiTitle($rightElement['answer']).']]>
  208. </simpleAssociableChoice>'. "\n";
  209. $i++;
  210. }
  211. }
  212. $out .= ' </simpleMatchSet>'. "\n";
  213. $out .= '</matchInteraction>'. "\n";
  214. return $out;
  215. }
  216. /**
  217. *
  218. */
  219. public function imsExportResponsesDeclaration($questionIdent)
  220. {
  221. $this->answerList = $this->getAnswersList(true);
  222. $out = ' <responseDeclaration identifier="' . $questionIdent . '" cardinality="single" baseType="identifier">' . "\n";
  223. $out .= ' <correctResponse>' . "\n";
  224. $gradeArray = array();
  225. if (is_array($this->leftList)) {
  226. foreach ($this->leftList as $leftKey=>$leftElement) {
  227. $i=0;
  228. foreach ($this->rightList as $rightKey=>$rightElement) {
  229. if (($leftElement['match'] == $rightElement['code'])) {
  230. $out .= ' <value>left_' . $leftKey . ' right_'.$i.'</value>'. "\n";
  231. $gradeArray['left_' . $leftKey . ' right_'.$i] = $leftElement['grade'];
  232. }
  233. $i++;
  234. }
  235. }
  236. }
  237. $out .= ' </correctResponse>'. "\n";
  238. $out .= ' <mapping>' . "\n";
  239. if (is_array($gradeArray)) {
  240. foreach ($gradeArray as $gradeKey=>$grade) {
  241. $out .= ' <mapEntry mapKey="'.$gradeKey.'" mappedValue="'.$grade.'"/>' . "\n";
  242. }
  243. }
  244. $out .= ' </mapping>' . "\n";
  245. $out .= ' </responseDeclaration>'. "\n";
  246. return $out;
  247. }
  248. }
  249. /**
  250. * Class
  251. * @package chamilo.exercise
  252. */
  253. class ImsAnswerHotspot extends Answer
  254. {
  255. /**
  256. * TODO update this to match hot spots instead of copying matching
  257. * Export the question part as a matrix-choice, with only one possible answer per line.
  258. */
  259. public function imsExportResponses($questionIdent, $questionStatment, $questionDesc='', $questionMedia='')
  260. {
  261. $this->answerList = $this->getAnswersList(true);
  262. $questionMedia = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/images/'.$questionMedia;
  263. $mimetype = mime_content_type($questionMedia);
  264. if(empty($mimetype)){
  265. $mimetype = 'image/jpeg';
  266. }
  267. $text = ' <p>'.$questionStatment.'</p>'."\n";
  268. $text .= ' <graphicOrderInteraction responseIdentifier="hotspot_'.$questionIdent.'">'."\n";
  269. $text .= ' <prompt>'.$questionDesc.'</prompt>'."\n";
  270. $text .= ' <object type="'.$mimetype.'" width="250" height="230" data="'.$questionMedia.'">-</object>'."\n";
  271. if (is_array($this->answerList)) {
  272. foreach ($this->answerList as $key=>$answer) {
  273. $key = $answer['id'];
  274. $answerTxt = $answer['answer'];
  275. $len = api_strlen($answerTxt);
  276. //coords are transformed according to QTIv2 rules here: http://www.imsproject.org/question/qtiv2p1pd/imsqti_infov2p1pd.html#element10663
  277. $coords = '';
  278. $type = 'default';
  279. switch($answer['hotspot_type']){
  280. case 'square':
  281. $type = 'rect';
  282. $res = array();
  283. $coords = preg_match('/^\s*(\d+);(\d+)\|(\d+)\|(\d+)\s*$/',$answer['hotspot_coord'],$res);
  284. $coords = $res[1].','.$res[2].','.((int)$res[1]+(int)$res[3]).",".((int)$res[2]+(int)$res[4]);
  285. break;
  286. case 'circle':
  287. $type = 'circle';
  288. $res = array();
  289. $coords = preg_match('/^\s*(\d+);(\d+)\|(\d+)\|(\d+)\s*$/',$answer['hotspot_coord'],$res);
  290. $coords = $res[1].','.$res[2].','.sqrt(pow(($res[1]-$res[3]),2)+pow(($res[2]-$res[4])));
  291. break;
  292. case 'poly':
  293. $type = 'poly';
  294. $coords = str_replace(array(';','|'),array(',',','),$answer['hotspot_coord']);
  295. break;
  296. case 'delineation' :
  297. $type = 'delineation';
  298. $coords = str_replace(array(';','|'),array(',',','),$answer['hotspot_coord']);
  299. break;
  300. }
  301. $text .= ' <hotspotChoice shape="'.$type.'" coords="'.$coords.'" identifier="'.$key.'"/>'."\n";
  302. }
  303. }
  304. $text .= ' </graphicOrderInteraction>'."\n";
  305. $out = $text;
  306. return $out;
  307. }
  308. /**
  309. *
  310. */
  311. public function imsExportResponsesDeclaration($questionIdent)
  312. {
  313. $this->answerList = $this->getAnswersList(true);
  314. $this->gradeList = $this->getGradesList();
  315. $out = '';
  316. $out .= ' <responseDeclaration identifier="hotspot_'.$questionIdent.'" cardinality="ordered" baseType="identifier">' . "\n";
  317. $out .= ' <correctResponse>'. "\n";
  318. if (is_array($this->answerList)) {
  319. foreach ($this->answerList as $answerKey=>$answer) {
  320. $answerKey = $answer['id'];
  321. $answer = $answer['answer'];
  322. $out .= '<value><![CDATA['.formatExerciseQtiTitle($answerKey).']]></value>';
  323. }
  324. }
  325. $out .= ' </correctResponse>'. "\n";
  326. $out .= ' </responseDeclaration>'. "\n";
  327. return $out;
  328. }
  329. }
  330. /**
  331. * Class
  332. * @package chamilo.exercise
  333. */
  334. class ImsAnswerFree extends Answer
  335. {
  336. /**
  337. * TODO implement
  338. * Export the question part as a matrix-choice, with only one possible answer per line.
  339. */
  340. public function imsExportResponses($questionIdent, $questionStatment, $questionDesc='', $questionMedia='')
  341. {
  342. return '';
  343. }
  344. /**
  345. *
  346. */
  347. public function imsExportResponsesDeclaration($questionIdent)
  348. {
  349. return '';
  350. }
  351. }