fill_blanks.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /*
  3. DOKEOS - elearning and course management software
  4. For a full list of contributors, see documentation/credits.html
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. See "documentation/licence.html" more details.
  10. Contact:
  11. Dokeos
  12. Rue des Palais 44 Paleizenstraat
  13. B-1030 Brussels - Belgium
  14. Tel. +32 (2) 211 34 56
  15. */
  16. /**
  17. * File containing the FillBlanks class.
  18. * @package dokeos.exercise
  19. * @author Eric Marguin
  20. * @version $Id: admin.php 10680 2007-01-11 21:26:23Z pcool $
  21. */
  22. if(!class_exists('FillBlanks')):
  23. /**
  24. CLASS FillBlanks
  25. *
  26. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  27. * extending the class question
  28. *
  29. * @author Eric Marguin
  30. * @package dokeos.exercise
  31. **/
  32. class FillBlanks extends Question {
  33. static $typePicture = 'fill_in_blanks.gif';
  34. static $explanationLangVar = 'FillBlanks';
  35. /**
  36. * Constructor
  37. */
  38. function FillBlanks(){
  39. parent::question();
  40. $this -> type = FILL_IN_BLANKS;
  41. }
  42. /**
  43. * function which redifines Question::createAnswersForm
  44. * @param the formvalidator instance
  45. */
  46. function createAnswersForm ($form) {
  47. $defaults = array();
  48. if(!empty($this->id))
  49. {
  50. $objAnswer = new answer($this->id);
  51. $a_answer = explode('::', $objAnswer->selectAnswer(1));
  52. $defaults['answer'] = $a_answer[0];
  53. $a_weightings = explode(',',$a_answer[1]);
  54. }
  55. else
  56. {
  57. $defaults['answer'] = get_lang('DefaultTextInBlanks');
  58. }
  59. // javascript
  60. echo '
  61. <script type="text/javascript">
  62. var firstTime = true;
  63. function updateBlanks() {
  64. field = document.getElementById("answer");
  65. var answer = field.value;
  66. var blanks = answer.match(/\[[^\]]*\]/g);
  67. var fields = "<div class=\"row\"><div class=\"label\">'.get_lang('Weighting').'</div><div class=\"formw\"><table>";
  68. if(blanks!=null){
  69. for(i=0 ; i<blanks.length ; i++){
  70. if(document.getElementById("weighting["+i+"]"))
  71. value = document.getElementById("weighting["+i+"]").value;
  72. else
  73. value = "10";
  74. 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>";
  75. }
  76. }
  77. document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
  78. if(firstTime){
  79. firstTime = false;
  80. ';
  81. if(count($a_weightings)>0)
  82. {
  83. foreach($a_weightings as $i=>$weighting)
  84. {
  85. echo 'document.getElementById("weighting['.$i.']").value = "'.$weighting.'";';
  86. }
  87. }
  88. echo '}
  89. }
  90. window.onload = updateBlanks;
  91. </script>
  92. ';
  93. // answer
  94. $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>');
  95. $form -> addElement ('textarea', 'answer',get_lang('Answer'),'id="answer" cols="65" rows="6" onkeyup="updateBlanks(this)"');
  96. $form -> addRule ('answer',get_lang('GiveText'),'required');
  97. $form -> addRule ('answer',get_lang('DefineBlanks'),'regex','/\[.*\]/');
  98. $form -> addElement('html','<div id="blanks_weighting"></div>');
  99. $form -> setDefaults($defaults);
  100. }
  101. /**
  102. * abstract function which creates the form to create / edit the answers of the question
  103. * @param the formvalidator instance
  104. */
  105. function processAnswersCreation($form)
  106. {
  107. $answer = $form -> getSubmitValue('answer');
  108. //remove the :: eventually written by the user
  109. $answer = str_replace('::','',$answer);
  110. // get the blanks weightings
  111. $nb = preg_match_all('/\[[^\]]*\]/', $answer, $blanks);
  112. if(isset($_GET['editQuestion'])){
  113. $this -> weighting = 0;
  114. }
  115. if($nb>0)
  116. {
  117. $answer .= '::';
  118. for($i=0 ; $i<$nb ; ++$i)
  119. {
  120. $answer .= $form -> getSubmitValue('weighting['.$i.']').',';
  121. $this -> weighting += $form -> getSubmitValue('weighting['.$i.']');
  122. }
  123. $answer = substr($answer,0,-1);
  124. }
  125. $this -> save();
  126. $objAnswer = new answer($this->id);
  127. $objAnswer->createAnswer($answer,0,'',0,'');
  128. $objAnswer->save();
  129. }
  130. }
  131. endif;
  132. ?>