fill_blanks.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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',null, '');
  132. $form -> addElement ('html_editor', 'answer','<img src="../img/fill_field.png">','id="answer" cols="122" rows="6" onkeyup="updateBlanks(this)"');
  133. $form -> addRule ('answer',get_lang('GiveText'),'required');
  134. $form -> addRule ('answer',get_lang('DefineBlanks'),'regex','/\[.*\]/');
  135. //added multiple answers
  136. $form -> addElement ('checkbox','multiple_answer','', get_lang('FillInBlankSwitchable'));
  137. $form -> addElement('html','<div id="blanks_weighting"></div>');
  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. $answer = $form -> getSubmitValue('answer');
  147. //remove the :: eventually written by the user
  148. $answer = str_replace('::','',$answer);
  149. // get the blanks weightings
  150. $nb = preg_match_all('/\[[^\]]*\]/', $answer, $blanks);
  151. if(isset($_GET['editQuestion']))
  152. {
  153. $this -> weighting = 0;
  154. }
  155. if($nb>0)
  156. {
  157. $answer .= '::';
  158. for($i=0 ; $i<$nb ; ++$i)
  159. {
  160. $answer .= $form -> getSubmitValue('weighting['.$i.']').',';
  161. $this -> weighting += $form -> getSubmitValue('weighting['.$i.']');
  162. }
  163. $answer = substr($answer,0,-1);
  164. }
  165. $is_multiple = $form -> getSubmitValue('multiple_answer');
  166. $answer.='@'.$is_multiple;
  167. $this -> save();
  168. $objAnswer = new answer($this->id);
  169. $objAnswer->createAnswer($answer,0,'',0,'');
  170. $objAnswer->save();
  171. }
  172. }
  173. endif;
  174. ?>