fill_blanks.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. if(!empty($this->id)) {
  55. $objAnswer = new answer($this->id);
  56. // the question is encoded like this
  57. // [A] B [C] D [E] F::10,10,10@1
  58. // number 1 before the "@" means that is a switchable fill in blank question
  59. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  60. // means that is a normal fill blank question
  61. $pre_array = explode('::', $objAnswer->selectAnswer(1));
  62. //make sure we only take the last bit to find special marks
  63. $sz = count($pre_array);
  64. $is_set_switchable = explode('@', $pre_array[$sz-1]);
  65. if ($is_set_switchable[1]) {
  66. $defaults['multiple_answer']=1;
  67. } else {
  68. $defaults['multiple_answer']=0;
  69. }
  70. //take the complete string except after the last '::'
  71. $defaults['answer'] = '';
  72. for($i=0;$i<($sz-1);$i++) {
  73. $defaults['answer'] .= $pre_array[$i];
  74. }
  75. $a_weightings = explode(',',$is_set_switchable[0]);
  76. } else {
  77. $defaults['answer'] = get_lang('DefaultTextInBlanks');
  78. }
  79. // javascript
  80. echo '
  81. <script type="text/javascript">
  82. function FCKeditor_OnComplete( editorInstance )
  83. {
  84. if (window.attachEvent) {
  85. editorInstance.EditorDocument.attachEvent("onkeyup", updateBlanks) ;
  86. } else {
  87. editorInstance.EditorDocument.addEventListener("keyup",updateBlanks,true);
  88. }
  89. }
  90. var firstTime = true;
  91. function updateBlanks()
  92. {
  93. if (firstTime) {
  94. field = document.getElementById("answer");
  95. var answer = field.value; }
  96. else {
  97. var oEditor = FCKeditorAPI.GetInstance(\'answer\');
  98. answer = oEditor.GetXHTML( true ) ;
  99. }
  100. var blanks = answer.match(/\[[^\]]*\]/g);
  101. var fields = "<div class=\"row\"><div class=\"label\">'.get_lang('Weighting').'</div><div class=\"formw\"><table>";
  102. if(blanks!=null){
  103. for(i=0 ; i<blanks.length ; i++){
  104. if(document.getElementById("weighting["+i+"]"))
  105. value = document.getElementById("weighting["+i+"]").value;
  106. else
  107. value = "10";
  108. 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>";
  109. }
  110. }
  111. document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
  112. if(firstTime){
  113. firstTime = false;
  114. ';
  115. if(count($a_weightings)>0)
  116. {
  117. foreach($a_weightings as $i=>$weighting)
  118. {
  119. echo 'document.getElementById("weighting['.$i.']").value = "'.$weighting.'";';
  120. }
  121. }
  122. echo '}
  123. }
  124. window.onload = updateBlanks;
  125. </script>
  126. ';
  127. // answer
  128. $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>');
  129. $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'));
  130. $form -> addRule ('answer',get_lang('GiveText'),'required');
  131. $form -> addRule ('answer',get_lang('DefineBlanks'),'regex','/\[.*\]/');
  132. //added multiple answers
  133. $form -> addElement ('checkbox','multiple_answer','', get_lang('FillInBlankSwitchable'));
  134. $form -> addElement('html','<div id="blanks_weighting"></div>');
  135. global $text, $class;
  136. // setting the save button here and not in the question class.php
  137. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  138. $form -> setDefaults($defaults);
  139. }
  140. /**
  141. * abstract function which creates the form to create / edit the answers of the question
  142. * @param the formvalidator instance
  143. */
  144. function processAnswersCreation($form)
  145. {
  146. global $charset;
  147. $answer = $form -> getSubmitValue('answer');
  148. //Due the fckeditor transform the elements to their HTML value
  149. $answer = api_html_entity_decode($answer, ENT_QUOTES, $charset);
  150. //remove the :: eventually written by the user
  151. $answer = str_replace('::','',$answer);
  152. // get the blanks weightings
  153. $nb = preg_match_all('/\[[^\]]*\]/', $answer, $blanks);
  154. if(isset($_GET['editQuestion']))
  155. {
  156. $this -> weighting = 0;
  157. }
  158. if($nb>0)
  159. {
  160. $answer .= '::';
  161. for($i=0 ; $i<$nb ; ++$i)
  162. {
  163. $answer .= $form -> getSubmitValue('weighting['.$i.']').',';
  164. $this -> weighting += $form -> getSubmitValue('weighting['.$i.']');
  165. }
  166. $answer = api_substr($answer,0,-1);
  167. }
  168. $is_multiple = $form -> getSubmitValue('multiple_answer');
  169. $answer.='@'.$is_multiple;
  170. $this -> save();
  171. $objAnswer = new answer($this->id);
  172. $objAnswer->createAnswer($answer,0,'',0,'');
  173. $objAnswer->save();
  174. }
  175. }
  176. endif;
  177. ?>