fill_blanks.class.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * File containing the FillBlanks class.
  5. * @package chamilo.exercise
  6. * @author Eric Marguin
  7. * @author Julio Montoya Armas switchable fill in blank option added
  8. * @version $Id: admin.php 10680 2007-01-11 21:26:23Z pcool $
  9. */
  10. /**
  11. * Code
  12. */
  13. /**
  14. * CLASS FillBlanks
  15. *
  16. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  17. * extending the class question
  18. *
  19. * @author Eric Marguin
  20. * @author Julio Montoya multiple fill in blank option added
  21. * @package chamilo.exercise
  22. **/
  23. class FillBlanks extends Question
  24. {
  25. static $typePicture = 'fill_in_blanks.gif';
  26. static $explanationLangVar = 'FillBlanks';
  27. /**
  28. * Constructor
  29. */
  30. public function FillBlanks()
  31. {
  32. parent::question();
  33. $this->type = FILL_IN_BLANKS;
  34. $this->isContent = $this->getIsContent();
  35. }
  36. /**
  37. * function which redifines Question::createAnswersForm
  38. * @param \FormValidator instance
  39. */
  40. public function createAnswersForm($form)
  41. {
  42. $defaults = array();
  43. $a_weightings = null;
  44. if (!empty($this->id)) {
  45. $objAnswer = new Answer($this->id);
  46. // the question is encoded like this.
  47. // [A] B [C] D [E] F::10,10,10@1
  48. // number 1 before the "@" means that is a switchable fill in blank question
  49. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  50. // means that is a normal fill blank question
  51. $answer_id = $objAnswer->getRealAnswerIdFromList(1);
  52. $pre_array = explode('::', $objAnswer->selectAnswer($answer_id));
  53. //make sure we only take the last bit to find special marks
  54. $sz = count($pre_array);
  55. $is_set_switchable = explode('@', $pre_array[$sz - 1]);
  56. if ($is_set_switchable[1]) {
  57. $defaults['multiple_answer'] = 1;
  58. } else {
  59. $defaults['multiple_answer'] = 0;
  60. }
  61. //take the complete string except after the last '::'
  62. $defaults['answer'] = '';
  63. for ($i = 0; $i < ($sz - 1); $i++) {
  64. $defaults['answer'] .= $pre_array[$i];
  65. }
  66. $a_weightings = explode(',', $is_set_switchable[0]);
  67. } else {
  68. $defaults['answer'] = get_lang('DefaultTextInBlanks');
  69. }
  70. // javascript
  71. echo '<script>
  72. function processFields(answer) {
  73. var blanks = answer.match(/\[[^\]]*\]/g);
  74. var fields = "<div class=\"control-group\"><label class=\"control-label\">'.get_lang('Weighting').'</label><div class=\"controls\"><table>";
  75. if (blanks!=null) {
  76. for (i=0 ; i<blanks.length ; i++){
  77. if (document.getElementById("weighting["+i+"]")) {
  78. value = document.getElementById("weighting["+i+"]").value;
  79. } else {
  80. value = "10";
  81. }
  82. fields += "<tr><td>"+blanks[i]+"</td><td><input style=\"margin-left: 0em;\" size=\"5\" value=\""+value+"\" type=\"text\" id=\"weighting["+i+"]\" name=\"weighting["+i+"]\" /></td></tr>";
  83. }
  84. document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
  85. }
  86. }
  87. function updateBlanks() {
  88. var answer = "";
  89. var editor = CKEDITOR.instances["answer"];
  90. if (editor) {
  91. editor.on("instanceReady", function(){
  92. answer = editor.getData();
  93. processFields(answer);
  94. this.document.on("keyup", function() {
  95. answer = editor.getData();
  96. processFields(answer);
  97. });
  98. });
  99. } else {
  100. field = document.getElementById("answer");
  101. answer = field.value;
  102. processFields(answer);
  103. }
  104. ';
  105. if (count($a_weightings) > 0) {
  106. foreach ($a_weightings as $i => $weighting) {
  107. echo '$("#weighting['.$i.']").attr("value", "'.$weighting.'");';
  108. }
  109. }
  110. echo '
  111. }
  112. window.onload = updateBlanks;
  113. </script>';
  114. // answer
  115. $form->addElement(
  116. 'label',
  117. null,
  118. '<br /><br />'.get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank')
  119. );
  120. $form->addElement('html_editor','answer', Display::return_icon('fill_field.png'), 'id="answer" cols="122" rows="6" onkeyup="javascript: updateBlanks(this);"', array('ToolbarSet' => 'TestQuestionDescription', 'Width' => '100%', 'Height' => '350'));
  121. $form->addRule('answer', get_lang('GiveText'), 'required');
  122. $form->addRule('answer', get_lang('DefineBlanks'), 'regex', '/\[.*\]/');
  123. //added multiple answers
  124. $form->addElement('checkbox', 'multiple_answer', '', get_lang('FillInBlankSwitchable'));
  125. $form->addElement('html', '<div id="blanks_weighting"></div>');
  126. // setting the save button here and not in the question class.php
  127. $form->addElement('style_submit_button', 'submitQuestion', $this->submitText, 'class="'.$this->submitClass.'"');
  128. if (!empty($this->id)) {
  129. $form->setDefaults($defaults);
  130. } else {
  131. if ($this->isContent == 1) {
  132. $form->setDefaults($defaults);
  133. }
  134. }
  135. }
  136. /**
  137. * abstract function which creates the form to create / edit the answers of the question
  138. * @param the formvalidator instance
  139. */
  140. function processAnswersCreation($form)
  141. {
  142. global $charset;
  143. $answer = $form->getSubmitValue('answer');
  144. //Due the fckeditor transform the elements to their HTML value
  145. $answer = api_html_entity_decode($answer, ENT_QUOTES, $charset);
  146. //remove the :: eventually written by the user
  147. $answer = str_replace('::', '', $answer);
  148. // get the blanks weightings
  149. $nb = preg_match_all('/\[[^\]]*\]/', $answer, $blanks);
  150. if (isset($_GET['editQuestion'])) {
  151. $this->weighting = 0;
  152. }
  153. if ($nb > 0) {
  154. $answer .= '::';
  155. for ($i = 0; $i < $nb; ++$i) {
  156. $answer .= $form->getSubmitValue('weighting['.$i.']').',';
  157. $this->weighting += $form->getSubmitValue('weighting['.$i.']');
  158. }
  159. $answer = api_substr($answer, 0, -1);
  160. }
  161. $is_multiple = $form->getSubmitValue('multiple_answer');
  162. $answer .= '@'.$is_multiple;
  163. $this->save();
  164. $objAnswer = new answer($this->id);
  165. $objAnswer->createAnswer($answer, 0, '', 0, '');
  166. $objAnswer->save();
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. function return_header($feedback_type = null, $counter = null, $score = null, $show_media = false, $hideTitle = 0)
  172. {
  173. $header = parent::return_header($feedback_type, $counter, $score, $show_media, $hideTitle);
  174. $header .= '<table class="'.$this->question_table_class.'">
  175. <tr>
  176. <th>'.get_lang("Answer").'</th>
  177. </tr>';
  178. return $header;
  179. }
  180. }