fill_blanks.class.php 5.4 KB

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