fill_blanks.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. *
  15. * This class allows to instantiate an object of type MULTIPLE_ANSWER
  16. * (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 the formvalidator instance
  39. */
  40. function createAnswersForm($form)
  41. {
  42. $defaults = array();
  43. if (!empty($this->id)) {
  44. $objAnswer = new answer($this->id);
  45. // the question is encoded like this
  46. // [A] B [C] D [E] F::10,10,10@1
  47. // number 1 before the "@" means that is a switchable fill in blank question
  48. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  49. // means that is a normal fill blank question
  50. $pre_array = explode('::', $objAnswer->selectAnswer(1));
  51. //make sure we only take the last bit to find special marks
  52. $sz = count($pre_array);
  53. $is_set_switchable = explode('@', $pre_array[$sz-1]);
  54. if ($is_set_switchable[1]) {
  55. $defaults['multiple_answer'] = 1;
  56. } else {
  57. $defaults['multiple_answer'] = 0;
  58. }
  59. //Take the complete string except after the last '::'
  60. $defaults['answer'] = '';
  61. for ($i=0; $i<($sz-1); $i++) {
  62. $defaults['answer'] .= $pre_array[$i];
  63. }
  64. $a_weightings = explode(',', $is_set_switchable[0]);
  65. } else {
  66. $defaults['answer'] = get_lang('DefaultTextInBlanks');
  67. }
  68. $setValues = null;
  69. if (isset($a_weightings) && count($a_weightings) > 0) {
  70. foreach ($a_weightings as $i => $weighting) {
  71. $setValues .= 'document.getElementById("weighting['.$i.']").value = "'.$weighting.'";';
  72. }
  73. }
  74. // javascript
  75. echo '<script>
  76. function FCKeditor_OnComplete(editorInstance) {
  77. if (window.attachEvent) {
  78. editorInstance.EditorDocument.attachEvent("onkeyup", updateBlanks) ;
  79. } else {
  80. editorInstance.EditorDocument.addEventListener("keyup", updateBlanks, true);
  81. }
  82. }
  83. var firstTime = true;
  84. function updateBlanks() {
  85. if (firstTime) {
  86. field = document.getElementById("answer");
  87. var answer = field.value;
  88. } else {
  89. var oEditor = FCKeditorAPI.GetInstance("answer");
  90. //var answer = oEditor.GetXHTML(true);
  91. var answer = oEditor.EditorDocument.body.innerHTML;
  92. }
  93. var blanks = answer.match(/\[[^\]]*\]/g);
  94. var fields = "<div class=\"control-group\"><label class=\"control-label\">'.get_lang('Weighting').'</label><div class=\"controls\"><table>";
  95. if (blanks!=null) {
  96. for (i=0 ; i<blanks.length ; i++){
  97. if (document.getElementById("weighting["+i+"]"))
  98. value = document.getElementById("weighting["+i+"]").value;
  99. else
  100. value = "10";
  101. fields += "<tr><td><label>"+blanks[i]+"</label></td><td><input style=\"margin-left: 0em;\" size=\"5\" value=\""+value+"\" type=\"text\" id=\"weighting["+i+"]\" name=\"weighting["+i+"]\" /></td></tr>";
  102. }
  103. }
  104. document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
  105. if (firstTime) {
  106. firstTime = false;
  107. '.$setValues.'
  108. }
  109. }
  110. window.onload = updateBlanks;
  111. </script>';
  112. // answer
  113. $form->addElement('label', null, '<br /><br />'.get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank'));
  114. $form->addElement('html_editor', 'answer', '<img src="../img/fill_field.png">','id="answer" cols="122" rows="6" onkeyup="javascript: updateBlanks(this);"', array('ToolbarSet' => 'TestQuestionDescription', 'Width' => '100%', 'Height' => '350'));
  115. $form->addRule('answer', get_lang('GiveText'),'required');
  116. $form->addRule('answer', get_lang('DefineBlanks'),'regex','/\[.*\]/');
  117. //added multiple answers
  118. $form->addElement('checkbox', 'multiple_answer','', get_lang('FillInBlankSwitchable'));
  119. $form->addElement('html', '<div id="blanks_weighting"></div>');
  120. global $text, $class;
  121. // setting the save button here and not in the question class.php
  122. $form->addElement('style_submit_button', 'submitQuestion', $text, 'class="'.$class.'"');
  123. if (!empty($this->id)) {
  124. $form -> setDefaults($defaults);
  125. } else {
  126. if ($this->isContent == 1) {
  127. $form->setDefaults($defaults);
  128. }
  129. }
  130. }
  131. /**
  132. * abstract function which creates the form to create / edit the answers of the question
  133. * @param FormValidator $form
  134. */
  135. function processAnswersCreation($form)
  136. {
  137. global $charset;
  138. $answer = $form->getSubmitValue('answer');
  139. //remove the :: eventually written by the user
  140. $answer = str_replace('::','',$answer);
  141. // get the blanks weightings
  142. $nb = preg_match_all('/\[[^\]]*\]/', $answer, $blanks);
  143. if (isset($_GET['editQuestion'])) {
  144. $this -> weighting = 0;
  145. }
  146. if ($nb>0) {
  147. $answer .= '::';
  148. for($i=0 ; $i<$nb ; ++$i) {
  149. $blankItem = $blanks[0][$i];
  150. $replace = array("[", "]");
  151. $newBlankItem = str_replace($replace, "", $blankItem);
  152. $newBlankItem = "[".trim($newBlankItem)."]";
  153. $answer = str_replace($blankItem, $newBlankItem, $answer);
  154. $answer .= $form->getSubmitValue('weighting['.$i.']').',';
  155. $this -> weighting += $form->getSubmitValue('weighting['.$i.']');
  156. }
  157. $answer = api_substr($answer,0,-1);
  158. }
  159. $is_multiple = $form->getSubmitValue('multiple_answer');
  160. $answer.='@'.$is_multiple;
  161. $this->save();
  162. $objAnswer = new answer($this->id);
  163. $objAnswer->createAnswer($answer, 0, '', 0, '1');
  164. $objAnswer->save();
  165. }
  166. function return_header($feedback_type = null, $counter = null, $score = null)
  167. {
  168. $header = parent::return_header($feedback_type, $counter, $score);
  169. $header .= '<table class="'.$this->question_table_class .'">
  170. <tr>
  171. <th>'.get_lang("Answer").'</th>
  172. </tr>';
  173. return $header;
  174. }
  175. }