fill_blanks.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. if(!class_exists('FillBlanks')):
  11. /**
  12. CLASS FillBlanks
  13. *
  14. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  15. * extending the class question
  16. *
  17. * @author Eric Marguin
  18. * @author Julio Montoya multiple fill in blank option added
  19. * @package dokeos.exercise
  20. **/
  21. class FillBlanks extends Question
  22. {
  23. static $typePicture = 'fill_in_blanks.gif';
  24. static $explanationLangVar = 'FillBlanks';
  25. /**
  26. * Constructor
  27. */
  28. function FillBlanks()
  29. {
  30. parent::question();
  31. $this -> type = FILL_IN_BLANKS;
  32. $this -> isContent = $this-> getIsContent();
  33. }
  34. /**
  35. * function which redifines Question::createAnswersForm
  36. * @param the formvalidator instance
  37. */
  38. function createAnswersForm ($form)
  39. {
  40. $defaults = array();
  41. if(!empty($this->id)) {
  42. $objAnswer = new answer($this->id);
  43. // the question is encoded like this
  44. // [A] B [C] D [E] F::10,10,10@1
  45. // number 1 before the "@" means that is a switchable fill in blank question
  46. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  47. // means that is a normal fill blank question
  48. $pre_array = explode('::', $objAnswer->selectAnswer(1));
  49. //make sure we only take the last bit to find special marks
  50. $sz = count($pre_array);
  51. $is_set_switchable = explode('@', $pre_array[$sz-1]);
  52. if ($is_set_switchable[1]) {
  53. $defaults['multiple_answer']=1;
  54. } else {
  55. $defaults['multiple_answer']=0;
  56. }
  57. //take the complete string except after the last '::'
  58. $defaults['answer'] = '';
  59. for($i=0;$i<($sz-1);$i++) {
  60. $defaults['answer'] .= $pre_array[$i];
  61. }
  62. $a_weightings = explode(',',$is_set_switchable[0]);
  63. } else {
  64. $defaults['answer'] = get_lang('DefaultTextInBlanks');
  65. }
  66. // javascript
  67. echo '
  68. <script type="text/javascript">
  69. function FCKeditor_OnComplete( editorInstance )
  70. {
  71. if (window.attachEvent) {
  72. editorInstance.EditorDocument.attachEvent("onkeyup", updateBlanks) ;
  73. } else {
  74. editorInstance.EditorDocument.addEventListener("keyup",updateBlanks,true);
  75. }
  76. }
  77. var firstTime = true;
  78. function updateBlanks()
  79. {
  80. if (firstTime) {
  81. field = document.getElementById("answer");
  82. var answer = field.value; }
  83. else {
  84. var oEditor = FCKeditorAPI.GetInstance(\'answer\');
  85. answer = oEditor.GetXHTML( true ) ;
  86. }
  87. var blanks = answer.match(/\[[^\]]*\]/g);
  88. var fields = "<div class=\"row\"><div class=\"label\">'.get_lang('Weighting').'</div><div class=\"formw\"><table>";
  89. if(blanks!=null){
  90. for(i=0 ; i<blanks.length ; i++){
  91. if(document.getElementById("weighting["+i+"]"))
  92. value = document.getElementById("weighting["+i+"]").value;
  93. else
  94. value = "10";
  95. 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>";
  96. }
  97. }
  98. document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
  99. if(firstTime){
  100. firstTime = false;
  101. ';
  102. if(count($a_weightings)>0)
  103. {
  104. foreach($a_weightings as $i=>$weighting)
  105. {
  106. echo 'document.getElementById("weighting['.$i.']").value = "'.$weighting.'";';
  107. }
  108. }
  109. echo '}
  110. }
  111. window.onload = updateBlanks;
  112. </script>
  113. ';
  114. // answer
  115. $form -> addElement ('html', '<br /><br /><div class="row"><div class="label"></div><div class="formw">'.get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank').'</div></div>');
  116. $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'));
  117. $form -> addRule ('answer',get_lang('GiveText'),'required');
  118. $form -> addRule ('answer',get_lang('DefineBlanks'),'regex','/\[.*\]/');
  119. //added multiple answers
  120. $form -> addElement ('checkbox','multiple_answer','', get_lang('FillInBlankSwitchable'));
  121. $form -> addElement('html','<div id="blanks_weighting"></div>');
  122. global $text, $class;
  123. // setting the save button here and not in the question class.php
  124. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  125. if (!empty($this -> id)) {
  126. $form -> setDefaults($defaults);
  127. } else {
  128. if ($this -> isContent == 1) {
  129. $form -> setDefaults($defaults);
  130. }
  131. }
  132. }
  133. /**
  134. * abstract function which creates the form to create / edit the answers of the question
  135. * @param the formvalidator instance
  136. */
  137. function processAnswersCreation($form)
  138. {
  139. global $charset;
  140. $answer = $form -> getSubmitValue('answer');
  141. //Due the fckeditor transform the elements to their HTML value
  142. $answer = api_html_entity_decode($answer, ENT_QUOTES, $charset);
  143. //remove the :: eventually written by the user
  144. $answer = str_replace('::','',$answer);
  145. // get the blanks weightings
  146. $nb = preg_match_all('/\[[^\]]*\]/', $answer, $blanks);
  147. if(isset($_GET['editQuestion']))
  148. {
  149. $this -> weighting = 0;
  150. }
  151. if($nb>0)
  152. {
  153. $answer .= '::';
  154. for($i=0 ; $i<$nb ; ++$i)
  155. {
  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. function return_header($feedback_type, $counter = null) {
  169. parent::return_header($feedback_type, $counter);
  170. $header = '<table width="100%" border="0" cellspacing="3" cellpadding="3">
  171. <tr>
  172. <td>&nbsp;</td>
  173. </tr>
  174. <tr>
  175. <td><i><?php echo get_lang("Answer"); ?></i> </td>
  176. </tr>
  177. <tr>
  178. <td>&nbsp;</td>
  179. </tr>';
  180. return $header;
  181. }
  182. }
  183. endif;
  184. ?>