fill_blanks.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. For a full list of contributors, see "credits.txt".
  7. The full license can be read in "license.txt".
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. See the GNU General Public License for more details.
  13. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  14. Mail: info@dokeos.com
  15. ==============================================================================
  16. */
  17. /**
  18. * File containing the FillBlanks class.
  19. * @package dokeos.exercise
  20. * @author Eric Marguin
  21. * @author Julio Montoya Armas switchable fill in blank option added
  22. * @version $Id: admin.php 10680 2007-01-11 21:26:23Z pcool $
  23. */
  24. if(!class_exists('FillBlanks')):
  25. /**
  26. CLASS FillBlanks
  27. *
  28. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  29. * extending the class question
  30. *
  31. * @author Eric Marguin
  32. * @author Julio Montoya multiple fill in blank option added
  33. * @package dokeos.exercise
  34. **/
  35. class FillBlanks extends Question
  36. {
  37. static $typePicture = 'fill_in_blanks.gif';
  38. static $explanationLangVar = 'FillBlanks';
  39. /**
  40. * Constructor
  41. */
  42. function FillBlanks()
  43. {
  44. parent::question();
  45. $this -> type = FILL_IN_BLANKS;
  46. }
  47. /**
  48. * function which redifines Question::createAnswersForm
  49. * @param the formvalidator instance
  50. */
  51. function createAnswersForm ($form)
  52. {
  53. $defaults = array();
  54. global $fck_attribute;
  55. $fck_attribute = array();
  56. //$fck_attribute['Width'] = '348px';
  57. $fck_attribute['Width'] = '100%';
  58. $fck_attribute['Height'] = '350px';
  59. $fck_attribute['ToolbarSet'] = 'Full';
  60. if(!empty($this->id)) {
  61. $objAnswer = new answer($this->id);
  62. // the question is encoded like this
  63. // [A] B [C] D [E] F::10,10,10@1
  64. // number 1 before the "@" means that is a switchable fill in blank question
  65. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  66. // means that is a normal fill blank question
  67. $pre_array = explode('::', $objAnswer->selectAnswer(1));
  68. //make sure we only take the last bit to find special marks
  69. $sz = count($pre_array);
  70. $is_set_switchable = explode('@', $pre_array[$sz-1]);
  71. if ($is_set_switchable[1]) {
  72. $defaults['multiple_answer']=1;
  73. } else {
  74. $defaults['multiple_answer']=0;
  75. }
  76. //take the complete string except after the last '::'
  77. $defaults['answer'] = '';
  78. for($i=0;$i<($sz-1);$i++) {
  79. $defaults['answer'] .= $pre_array[$i];
  80. }
  81. $a_weightings = explode(',',$is_set_switchable[0]);
  82. } else {
  83. $defaults['answer'] = get_lang('DefaultTextInBlanks');
  84. }
  85. // javascript
  86. echo '
  87. <script type="text/javascript">
  88. function FCKeditor_OnComplete( editorInstance )
  89. {
  90. editorInstance.EditorDocument.addEventListener( \'keyup\', updateBlanks, true ) ;
  91. }
  92. var firstTime = true;
  93. function updateBlanks()
  94. {
  95. if (firstTime) {
  96. field = document.getElementById("answer");
  97. var answer = field.value; }
  98. else {
  99. var oEditor = FCKeditorAPI.GetInstance(\'answer\');
  100. answer = oEditor.GetXHTML( true ) ;
  101. }
  102. var blanks = answer.match(/\[[^\]]*\]/g);
  103. var fields = "<div class=\"row\"><div class=\"label\">'.get_lang('Weighting').'</div><div class=\"formw\"><table>";
  104. if(blanks!=null){
  105. for(i=0 ; i<blanks.length ; i++){
  106. if(document.getElementById("weighting["+i+"]"))
  107. value = document.getElementById("weighting["+i+"]").value;
  108. else
  109. value = "10";
  110. 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>";
  111. }
  112. }
  113. document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
  114. if(firstTime){
  115. firstTime = false;
  116. ';
  117. if(count($a_weightings)>0)
  118. {
  119. foreach($a_weightings as $i=>$weighting)
  120. {
  121. echo 'document.getElementById("weighting['.$i.']").value = "'.$weighting.'";';
  122. }
  123. }
  124. echo '}
  125. }
  126. window.onload = updateBlanks;
  127. </script>
  128. ';
  129. // answer
  130. $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>');
  131. $form -> addElement ('html_editor', 'answer','<img src="../img/fill_field.png">','id="answer" cols="122" rows="6" onkeyup="updateBlanks(this)"');
  132. $form -> addRule ('answer',get_lang('GiveText'),'required');
  133. $form -> addRule ('answer',get_lang('DefineBlanks'),'regex','/\[.*\]/');
  134. //added multiple answers
  135. $form -> addElement ('checkbox','multiple_answer','', get_lang('FillInBlankSwitchable'));
  136. $form -> addElement('html','<div id="blanks_weighting"></div>');
  137. global $text, $class;
  138. // setting the save button here and not in the question class.php
  139. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  140. $form -> setDefaults($defaults);
  141. }
  142. /**
  143. * abstract function which creates the form to create / edit the answers of the question
  144. * @param the formvalidator instance
  145. */
  146. function processAnswersCreation($form)
  147. {
  148. global $charset;
  149. $answer = $form -> getSubmitValue('answer');
  150. //Due the fckeditor transform the elements to their HTML value
  151. $answer = api_html_entity_decode($answer, ENT_QUOTES, $charset);
  152. //remove the :: eventually written by the user
  153. $answer = str_replace('::','',$answer);
  154. // get the blanks weightings
  155. $nb = preg_match_all('/\[[^\]]*\]/', $answer, $blanks);
  156. if(isset($_GET['editQuestion']))
  157. {
  158. $this -> weighting = 0;
  159. }
  160. if($nb>0)
  161. {
  162. $answer .= '::';
  163. for($i=0 ; $i<$nb ; ++$i)
  164. {
  165. $answer .= $form -> getSubmitValue('weighting['.$i.']').',';
  166. $this -> weighting += $form -> getSubmitValue('weighting['.$i.']');
  167. }
  168. $answer = api_substr($answer,0,-1);
  169. }
  170. $is_multiple = $form -> getSubmitValue('multiple_answer');
  171. $answer.='@'.$is_multiple;
  172. $this -> save();
  173. $objAnswer = new answer($this->id);
  174. $objAnswer->createAnswer($answer,0,'',0,'');
  175. $objAnswer->save();
  176. }
  177. }
  178. endif;
  179. ?>