fill_blanks.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * File containing the FillBlanks class.
  5. * @package chamilo.exercise
  6. * @author Eric Marguin
  7. * @author Julio Montoya Armas switchable fill in blank option added
  8. * @version $Id: admin.php 10680 2007-01-11 21:26:23Z pcool $
  9. */
  10. /**
  11. * Code
  12. */
  13. if(!class_exists('FillBlanks')):
  14. /**
  15. CLASS FillBlanks
  16. *
  17. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  18. * extending the class question
  19. *
  20. * @author Eric Marguin
  21. * @author Julio Montoya multiple fill in blank option added
  22. * @package chamilo.exercise
  23. **/
  24. class FillBlanks extends Question
  25. {
  26. static $typePicture = 'fill_in_blanks.gif';
  27. static $explanationLangVar = 'FillBlanks';
  28. /**
  29. * Constructor
  30. */
  31. function FillBlanks()
  32. {
  33. parent::question();
  34. $this -> type = FILL_IN_BLANKS;
  35. $this -> isContent = $this-> getIsContent();
  36. }
  37. /**
  38. * function which redifines Question::createAnswersForm
  39. * @param the formvalidator instance
  40. */
  41. function createAnswersForm ($form) {
  42. $defaults = array();
  43. if (!empty($this->id)) {
  44. $objAnswer = new answer($this->id);
  45. // the question is encoded like this
  46. // [A] B [C] D [E] F::10,10,10@1
  47. // number 1 before the "@" means that is a switchable fill in blank question
  48. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  49. // means that is a normal fill blank question
  50. $pre_array = explode('::', $objAnswer->selectAnswer(1));
  51. //make sure we only take the last bit to find special marks
  52. $sz = count($pre_array);
  53. $is_set_switchable = explode('@', $pre_array[$sz-1]);
  54. if ($is_set_switchable[1]) {
  55. $defaults['multiple_answer']=1;
  56. } else {
  57. $defaults['multiple_answer']=0;
  58. }
  59. //take the complete string except after the last '::'
  60. $defaults['answer'] = '';
  61. for ($i=0;$i<($sz-1);$i++) {
  62. $defaults['answer'] .= $pre_array[$i];
  63. }
  64. $a_weightings = explode(',', $is_set_switchable[0]);
  65. } else {
  66. $defaults['answer'] = get_lang('DefaultTextInBlanks');
  67. }
  68. // javascript
  69. echo '<script type="text/javascript">
  70. function FCKeditor_OnComplete( editorInstance ) {
  71. if (window.attachEvent) {
  72. editorInstance.EditorDocument.attachEvent("onkeyup", updateBlanks) ;
  73. } else {
  74. editorInstance.EditorDocument.addEventListener("keyup",updateBlanks,true);
  75. }
  76. }
  77. var firstTime = true;
  78. function updateBlanks() {
  79. if (firstTime) {
  80. field = document.getElementById("answer");
  81. var answer = field.value;
  82. } else {
  83. var oEditor = FCKeditorAPI.GetInstance(\'answer\');
  84. answer = oEditor.GetXHTML( true ) ;
  85. }
  86. var blanks = answer.match(/\[[^\]]*\]/g);
  87. var fields = "<div class=\"control-group\"><label class=\"control-label\">'.get_lang('Weighting').'</label><div class=\"controls\"><table>";
  88. if (blanks!=null) {
  89. for (i=0 ; i<blanks.length ; i++){
  90. if (document.getElementById("weighting["+i+"]"))
  91. value = document.getElementById("weighting["+i+"]").value;
  92. else
  93. value = "10";
  94. 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>";
  95. }
  96. }
  97. document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
  98. if (firstTime) {
  99. firstTime = false;
  100. ';
  101. if (count($a_weightings)>0) {
  102. foreach($a_weightings as $i=>$weighting) {
  103. echo 'document.getElementById("weighting['.$i.']").value = "'.$weighting.'";';
  104. }
  105. }
  106. echo '}
  107. }
  108. window.onload = updateBlanks;
  109. </script>';
  110. // answer
  111. $form -> addElement ('label', null, '<br /><br />'.get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank'));
  112. $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'));
  113. $form -> addRule ('answer',get_lang('GiveText'),'required');
  114. $form -> addRule ('answer',get_lang('DefineBlanks'),'regex','/\[.*\]/');
  115. //added multiple answers
  116. $form -> addElement ('checkbox','multiple_answer','', get_lang('FillInBlankSwitchable'));
  117. $form -> addElement('html','<div id="blanks_weighting"></div>');
  118. global $text, $class;
  119. // setting the save button here and not in the question class.php
  120. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  121. if (!empty($this -> id)) {
  122. $form -> setDefaults($defaults);
  123. } else {
  124. if ($this -> isContent == 1) {
  125. $form -> setDefaults($defaults);
  126. }
  127. }
  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. global $charset;
  136. $answer = $form -> getSubmitValue('answer');
  137. //Due the fckeditor transform the elements to their HTML value
  138. $answer = api_html_entity_decode($answer, ENT_QUOTES, $charset);
  139. //remove the :: eventually written by the user
  140. $answer = str_replace('::','',$answer);
  141. // get the blanks weightings
  142. $nb = preg_match_all('/\[[^\]]*\]/', $answer, $blanks);
  143. if(isset($_GET['editQuestion']))
  144. {
  145. $this -> weighting = 0;
  146. }
  147. if($nb>0)
  148. {
  149. $answer .= '::';
  150. for($i=0 ; $i<$nb ; ++$i)
  151. {
  152. $answer .= $form -> getSubmitValue('weighting['.$i.']').',';
  153. $this -> weighting += $form -> getSubmitValue('weighting['.$i.']');
  154. }
  155. $answer = api_substr($answer,0,-1);
  156. }
  157. $is_multiple = $form -> getSubmitValue('multiple_answer');
  158. $answer.='@'.$is_multiple;
  159. $this -> save();
  160. $objAnswer = new answer($this->id);
  161. $objAnswer->createAnswer($answer,0,'',0,'');
  162. $objAnswer->save();
  163. }
  164. function return_header($feedback_type = null, $counter = null, $score = null, $show_media = false) {
  165. $header = parent::return_header($feedback_type, $counter, $score, $show_media);
  166. $header .= '<table class="'.$this->question_table_class .'">
  167. <tr>
  168. <th>'.get_lang("Answer").'</th>
  169. </tr>';
  170. return $header;
  171. }
  172. }
  173. endif;