ScormAnswerFillInBlanks.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class handles the SCORM export of fill-in-the-blanks questions.
  5. *
  6. * @package chamilo.exercise.scorm
  7. */
  8. class ScormAnswerFillInBlanks extends Answer
  9. {
  10. /**
  11. * Export the text with missing words.
  12. *
  13. * As a side effect, it stores two lists in the class :
  14. * the missing words and their respective weightings.
  15. */
  16. public function export()
  17. {
  18. $js = '';
  19. // get all enclosed answers
  20. $blankList = [];
  21. foreach ($this->answer as $i => $answer) {
  22. $blankList[] = '['.$answer.']';
  23. }
  24. // splits text and weightings that are joined with the character '::'
  25. $listAnswerInfo = FillBlanks::getAnswerInfo($answer);
  26. //$switchableAnswerSet = $listAnswerInfo['switchable'];
  27. // display empty [input] with the right width for student to fill it
  28. $answer = '';
  29. $answerList = [];
  30. for ($i = 0; $i < count($listAnswerInfo['common_words']) - 1; $i++) {
  31. // display the common words
  32. $answer .= $listAnswerInfo['common_words'][$i];
  33. // display the blank word
  34. $attributes['style'] = 'width:'.$listAnswerInfo['input_size'][$i].'px';
  35. $answer .= FillBlanks::getFillTheBlankHtml(
  36. $this->questionJSId,
  37. $this->questionJSId + 1,
  38. '',
  39. $attributes,
  40. $answer,
  41. $listAnswerInfo,
  42. true,
  43. $i,
  44. 'question_'.$this->questionJSId.'_fib_'.($i + 1)
  45. );
  46. $answerList[] = $i + 1;
  47. }
  48. // display the last common word
  49. $answer .= $listAnswerInfo['common_words'][$i];
  50. // because [] is parsed here we follow this procedure:
  51. // 1. find everything between the [ and ] tags
  52. $jstmpw = 'questions_answers_ponderation['.$this->questionJSId.'] = new Array();'."\n";
  53. $jstmpw .= 'questions_answers_ponderation['.$this->questionJSId.'][0] = 0;'."\n";
  54. foreach ($listAnswerInfo['weighting'] as $key => $weight) {
  55. $jstmpw .= 'questions_answers_ponderation['.$this->questionJSId.']['.($key + 1).'] = '.$weight.";\n";
  56. }
  57. $wordList = "'".implode("', '", $listAnswerInfo['words'])."'";
  58. $answerList = "'".implode("', '", $answerList)."'";
  59. $html = '<tr><td colspan="2"><table width="100%">';
  60. $html .= '<tr>
  61. <td>
  62. '.$answer.'
  63. </td>
  64. </tr></table></td></tr>';
  65. $js .= 'questions_answers['.$this->questionJSId.'] = new Array('.$answerList.');'."\n";
  66. $js .= 'questions_answers_correct['.$this->questionJSId.'] = new Array('.$wordList.');'."\n";
  67. $js .= 'questions_types['.$this->questionJSId.'] = \'fib\';'."\n";
  68. $js .= $jstmpw;
  69. return [$js, $html];
  70. }
  71. }