ScormAnswerMultipleChoice.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class handles the export to SCORM of a multiple choice question
  5. * (be it single answer or multiple answers).
  6. *
  7. * @package chamilo.exercise.scorm
  8. */
  9. class ScormAnswerMultipleChoice extends Answer
  10. {
  11. /**
  12. * Return HTML code for possible answers.
  13. */
  14. public function export()
  15. {
  16. $js = [];
  17. $type = $this->getQuestionType();
  18. $questionId = $this->questionJSId;
  19. $jstmpw = 'questions_answers_ponderation['.$questionId.'] = new Array();';
  20. $jstmpw .= 'questions_answers_ponderation['.$questionId.'][0] = 0;';
  21. $jstmpw .= 'questions_answers_correct['.$questionId.'] = new Array();';
  22. $html = [];
  23. //not sure if we are going to export also the MULTIPLE_ANSWER_COMBINATION to SCORM
  24. //if ($type == MCMA || $type == MULTIPLE_ANSWER_COMBINATION ) {
  25. if ($type == MCMA) {
  26. $id = 1;
  27. $jstmp = '';
  28. $jstmpc = '';
  29. foreach ($this->answer as $i => $answer) {
  30. $identifier = 'question_'.$questionId.'_multiple_'.$i;
  31. $html[] =
  32. '<tr>
  33. <td align="center" width="5%">
  34. <input name="'.$identifier.'" id="'.$identifier.'" value="'.$i.'" type="checkbox" />
  35. </td>
  36. <td width="95%">
  37. <label for="'.$identifier.'">'.Security::remove_XSS($this->answer[$i]).'</label>
  38. </td>
  39. </tr>';
  40. $jstmp .= $i.',';
  41. if ($this->correct[$i]) {
  42. $jstmpc .= $i.',';
  43. }
  44. $jstmpw .= 'questions_answers_ponderation['.$questionId.']['.$i.'] = '.$this->weighting[$i].";";
  45. $jstmpw .= 'questions_answers_correct['.$questionId.']['.$i.'] = '.$this->correct[$i].';';
  46. $id++;
  47. }
  48. $js[] = 'questions_answers['.$questionId.'] = new Array('.substr($jstmp, 0, -1).');'."\n";
  49. $js[] = 'questions_types['.$questionId.'] = \'mcma\';'."\n";
  50. $js[] = $jstmpw;
  51. } elseif ($type == MULTIPLE_ANSWER_COMBINATION) {
  52. $js = '';
  53. $id = 1;
  54. $jstmp = '';
  55. $jstmpc = '';
  56. foreach ($this->answer as $i => $answer) {
  57. $identifier = 'question_'.$questionId.'_exact_'.$i;
  58. $html[] =
  59. '<tr>
  60. <td align="center" width="5%">
  61. <input name="'.$identifier.'" id="'.$identifier.'" value="'.$i.'" type="checkbox" />
  62. </td>
  63. <td width="95%">
  64. <label for="'.$identifier.'">'.Security::remove_XSS($this->answer[$i]).'</label>
  65. </td>
  66. </tr>';
  67. $jstmp .= $i.',';
  68. if ($this->correct[$i]) {
  69. $jstmpc .= $i.',';
  70. }
  71. $jstmpw .= 'questions_answers_ponderation['.$questionId.']['.$i.'] = '.$this->weighting[$i].';';
  72. $jstmpw .= 'questions_answers_correct['.$questionId.']['.$i.'] = '.$this->correct[$i].';';
  73. $id++;
  74. }
  75. $js[] = 'questions_answers['.$questionId.'] = new Array('.substr($jstmp, 0, -1).');';
  76. $js[] = 'questions_types['.$questionId.'] = "exact";';
  77. $js[] = $jstmpw;
  78. } else {
  79. $id = 1;
  80. $jstmp = '';
  81. $jstmpc = '';
  82. foreach ($this->answer as $i => $answer) {
  83. $identifier = 'question_'.$questionId.'_unique_'.$i;
  84. $identifier_name = 'question_'.$questionId.'_unique_answer';
  85. $html[] =
  86. '<tr>
  87. <td align="center" width="5%">
  88. <input name="'.$identifier_name.'" id="'.$identifier.'" value="'.$i.'" type="checkbox"/>
  89. </td>
  90. <td width="95%">
  91. <label for="'.$identifier.'">'.Security::remove_XSS($this->answer[$i]).'</label>
  92. </td>
  93. </tr>';
  94. $jstmp .= $i.',';
  95. if ($this->correct[$i]) {
  96. $jstmpc .= $i;
  97. }
  98. $jstmpw .= 'questions_answers_ponderation['.$questionId.']['.$i.'] = '.$this->weighting[$i].';';
  99. $jstmpw .= 'questions_answers_correct['.$questionId.']['.$i.'] = '.$this->correct[$i].';';
  100. $id++;
  101. }
  102. $js[] = 'questions_answers['.$questionId.'] = new Array('.substr($jstmp, 0, -1).');';
  103. $js[] = 'questions_types['.$questionId.'] = \'mcua\';';
  104. $js[] = $jstmpw;
  105. }
  106. $htmlResult = '<tr><td colspan="2"><table id="question_'.$questionId.'" width="100%">';
  107. $htmlResult .= implode("\n", $html);
  108. $htmlResult .= '</table></td></tr>';
  109. $js = implode("\n", $js);
  110. return [$js, $htmlResult];
  111. }
  112. }