ScormAnswerMatching.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class handles the SCORM export of matching questions.
  5. *
  6. * @package chamilo.exercise.scorm
  7. */
  8. class ScormAnswerMatching extends Answer
  9. {
  10. /**
  11. * Export the question part as a matrix-choice, with only one possible answer per line.
  12. *
  13. * @author Amand Tihon <amand@alrj.org>
  14. */
  15. public function export()
  16. {
  17. $js = '';
  18. // prepare list of right proposition to allow
  19. // - easiest display
  20. // - easiest randomisation if needed one day
  21. // (here I use array_values to change array keys from $code1 $code2 ... to 0 1 ...)
  22. // get max length of displayed array
  23. $nbrAnswers = $this->selectNbrAnswers();
  24. $counter = 1;
  25. $questionId = $this->questionJSId;
  26. $jstmpw = 'questions_answers_ponderation['.$questionId.'] = new Array();'."\n";
  27. $jstmpw .= 'questions_answers_ponderation['.$questionId.'][0] = 0;'."\n";
  28. // Options (A, B, C, ...) that will be put into the list-box
  29. $options = [];
  30. $letter = 'A';
  31. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  32. $answerCorrect = $this->isCorrect($answerId);
  33. $answer = $this->selectAnswer($answerId);
  34. $realAnswerId = $this->selectAutoId($answerId);
  35. if (!$answerCorrect) {
  36. $options[$realAnswerId]['Lettre'] = $letter;
  37. // answers that will be shown at the right side
  38. $options[$realAnswerId]['Reponse'] = $answer;
  39. $letter++;
  40. }
  41. }
  42. $html = [];
  43. $jstmp = '';
  44. $jstmpc = '';
  45. // Answers
  46. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  47. $identifier = 'question_'.$questionId.'_matching_';
  48. $answer = $this->selectAnswer($answerId);
  49. $answerCorrect = $this->isCorrect($answerId);
  50. $weight = $this->selectWeighting($answerId);
  51. $jstmp .= $answerId.',';
  52. if ($answerCorrect) {
  53. $html[] = '<tr class="option_row">';
  54. $html[] = '<td width="40%" valign="top">&nbsp;'.$answer."</td>";
  55. $html[] = '<td width="20%" align="center">&nbsp;&nbsp;';
  56. $html[] = '<select name="'.$identifier.$counter.'" id="'.$identifier.$counter.'">';
  57. $html[] = ' <option value="0">--</option>';
  58. // fills the list-box
  59. foreach ($options as $key => $val) {
  60. $html[] = '<option value="'.$key.'">'.$val['Lettre'].'</option>';
  61. }
  62. $html[] = '</select>&nbsp;&nbsp;</td>';
  63. $html[] = '<td width="40%" valign="top">';
  64. foreach ($options as $key => $val) {
  65. $html[] = '<b>'.$val['Lettre'].'.</b> '.$val['Reponse'].'<br />';
  66. }
  67. $html[] = '</td></tr>';
  68. $jstmpc .= '['.$answerCorrect.','.$counter.'],';
  69. $myWeight = explode('@', $weight);
  70. if (count($myWeight) == 2) {
  71. $weight = $myWeight[0];
  72. } else {
  73. $weight = $myWeight[0];
  74. }
  75. $jstmpw .= 'questions_answers_ponderation['.$questionId.']['.$counter.'] = '.$weight.";\n";
  76. $counter++;
  77. }
  78. }
  79. $js .= 'questions_answers['.$questionId.'] = new Array('.substr($jstmp, 0, -1).');'."\n";
  80. $js .= 'questions_answers_correct['.$questionId.'] = new Array('.substr($jstmpc, 0, -1).');'."\n";
  81. $js .= 'questions_types['.$questionId.'] = \'matching\';'."\n";
  82. $js .= $jstmpw;
  83. $htmlResult = '<tr><td colspan="2"><table id="question_'.$questionId.'" width="100%">';
  84. $htmlResult .= implode("\n", $html);
  85. $htmlResult .= '</table></td></tr>'."\n";
  86. return [$js, $htmlResult];
  87. }
  88. }