fill_blanks.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. if(!class_exists('FillBlanks')):
  14. /**
  15. CLASS FillBlanks
  16. *
  17. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  18. * extending the class question
  19. *
  20. * @author Eric Marguin
  21. * @author Julio Montoya multiple fill in blank option added
  22. * @package chamilo.exercise
  23. **/
  24. class FillBlanks extends Question
  25. {
  26. static $typePicture = 'fill_in_blanks.gif';
  27. static $explanationLangVar = 'FillBlanks';
  28. /**
  29. * Constructor
  30. */
  31. function FillBlanks()
  32. {
  33. parent::question();
  34. $this -> type = FILL_IN_BLANKS;
  35. $this -> isContent = $this-> getIsContent();
  36. }
  37. /**
  38. * function which redifines Question::createAnswersForm
  39. * @param the formvalidator instance
  40. */
  41. function createAnswersForm ($form)
  42. {
  43. $defaults = array();
  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. $pre_array = explode('::', $objAnswer->selectAnswer(1));
  52. //make sure we only take the last bit to find special marks
  53. $sz = count($pre_array);
  54. $is_set_switchable = explode('@', $pre_array[$sz-1]);
  55. if ($is_set_switchable[1]) {
  56. $defaults['multiple_answer']=1;
  57. } else {
  58. $defaults['multiple_answer']=0;
  59. }
  60. //take the complete string except after the last '::'
  61. $defaults['answer'] = '';
  62. for($i=0;$i<($sz-1);$i++) {
  63. $defaults['answer'] .= $pre_array[$i];
  64. }
  65. $a_weightings = explode(',',$is_set_switchable[0]);
  66. } else {
  67. $defaults['answer'] = get_lang('DefaultTextInBlanks');
  68. }
  69. // javascript
  70. echo '
  71. <script type="text/javascript">
  72. function FCKeditor_OnComplete( editorInstance )
  73. {
  74. if (window.attachEvent) {
  75. editorInstance.EditorDocument.attachEvent("onkeyup", updateBlanks) ;
  76. } else {
  77. editorInstance.EditorDocument.addEventListener("keyup",updateBlanks,true);
  78. }
  79. }
  80. var firstTime = true;
  81. function updateBlanks()
  82. {
  83. if (firstTime) {
  84. field = document.getElementById("answer");
  85. var answer = field.value; }
  86. else {
  87. var oEditor = FCKeditorAPI.GetInstance(\'answer\');
  88. answer = oEditor.GetXHTML( true ) ;
  89. }
  90. var blanks = answer.match(/\[[^\]]*\]/g);
  91. var fields = "<div class=\"row\"><div class=\"label\">'.get_lang('Weighting').'</div><div class=\"formw\"><table>";
  92. if(blanks!=null){
  93. for(i=0 ; i<blanks.length ; i++){
  94. if(document.getElementById("weighting["+i+"]"))
  95. value = document.getElementById("weighting["+i+"]").value;
  96. else
  97. value = "10";
  98. 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>";
  99. }
  100. }
  101. document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
  102. if(firstTime){
  103. firstTime = false;
  104. ';
  105. if(count($a_weightings)>0)
  106. {
  107. foreach($a_weightings as $i=>$weighting)
  108. {
  109. echo 'document.getElementById("weighting['.$i.']").value = "'.$weighting.'";';
  110. }
  111. }
  112. echo '}
  113. }
  114. window.onload = updateBlanks;
  115. </script>
  116. ';
  117. // answer
  118. $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>');
  119. $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'));
  120. $form -> addRule ('answer',get_lang('GiveText'),'required');
  121. $form -> addRule ('answer',get_lang('DefineBlanks'),'regex','/\[.*\]/');
  122. //added multiple answers
  123. $form -> addElement ('checkbox','multiple_answer','', get_lang('FillInBlankSwitchable'));
  124. $form -> addElement('html','<div id="blanks_weighting"></div>');
  125. global $text, $class;
  126. // setting the save button here and not in the question class.php
  127. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  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. {
  152. $this -> weighting = 0;
  153. }
  154. if($nb>0)
  155. {
  156. $answer .= '::';
  157. for($i=0 ; $i<$nb ; ++$i)
  158. {
  159. $answer .= $form -> getSubmitValue('weighting['.$i.']').',';
  160. $this -> weighting += $form -> getSubmitValue('weighting['.$i.']');
  161. }
  162. $answer = api_substr($answer,0,-1);
  163. }
  164. $is_multiple = $form -> getSubmitValue('multiple_answer');
  165. $answer.='@'.$is_multiple;
  166. $this -> save();
  167. $objAnswer = new answer($this->id);
  168. $objAnswer->createAnswer($answer,0,'',0,'');
  169. $objAnswer->save();
  170. }
  171. function return_header($feedback_type, $counter = null) {
  172. parent::return_header($feedback_type, $counter);
  173. $header = '<table width="100%" class="data_table_exercise_result_left">
  174. <tr>
  175. <td><i>'.get_lang("Answer").'</i> </td>
  176. </tr>';
  177. return $header;
  178. }
  179. }
  180. endif;
  181. ?>