calculated_answer.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class CalculatedAnswer
  5. * This class contains calculated answer form and answer processing functions
  6. *
  7. * @author Imanol Losada
  8. * @package chamilo.exercise
  9. **/
  10. class CalculatedAnswer extends Question
  11. {
  12. static $typePicture = 'calculated_answer.png';
  13. static $explanationLangVar = 'CalculatedAnswer';
  14. /**
  15. * Constructor
  16. */
  17. public function CalculatedAnswer()
  18. {
  19. parent::question();
  20. $this -> type = CALCULATED_ANSWER;
  21. $this -> isContent = $this-> getIsContent();
  22. }
  23. /**
  24. * function which redefines Question::createAnswersForm
  25. * @param the formvalidator instance
  26. */
  27. function createAnswersForm($form)
  28. {
  29. $defaults = array();
  30. if (!empty($this->id)) {
  31. $objAnswer = new answer($this->id);
  32. $preArray = explode('@@', $objAnswer->selectAnswer(1));
  33. $defaults['formula'] = array_pop($preArray);
  34. $defaults['answer'] = array_shift($preArray);
  35. $defaults['answer'] = preg_replace("/\[.*\]/", "", $defaults['answer']);
  36. $defaults['weighting'] = $this->weighting;
  37. } else {
  38. $defaults['answer'] = get_lang('DefaultTextInBlanks');
  39. }
  40. $lowestValue = "1.00";
  41. $highestValue = "20.00";
  42. // javascript //
  43. echo '<script>
  44. function parseTextNumber(textNumber, floatValue) {
  45. if (textNumber.indexOf(".") > -1) {
  46. textNumber = parseFloat(textNumber);
  47. floatValue.exists = "true";
  48. } else {
  49. textNumber = parseInt(textNumber);
  50. }
  51. return textNumber;
  52. }
  53. function updateRandomValue(element) {
  54. // "floatValue" helps to distinguish between an integer (10) and a float with all 0 decimals (10.00)
  55. var floatValue = { exists: "false" };
  56. var index = (element.name).match(/\[[^\]]*\]/g);
  57. var lowestValue = parseTextNumber(document.getElementById("lowestValue"+index).value, floatValue);
  58. var highestValue = parseTextNumber(document.getElementById("highestValue"+index).value, floatValue);
  59. var result = Math.random() * (highestValue - lowestValue) + lowestValue;
  60. if (floatValue.exists == "true") {
  61. result = parseFloat(result).toFixed(2);
  62. } else {
  63. result = parseInt(result);
  64. }
  65. document.getElementById("randomValue"+index).innerHTML = "'.get_lang("ExampleValue").': " + result;
  66. }
  67. function FCKeditor_OnComplete(editorInstance) {
  68. if (window.attachEvent) {
  69. editorInstance.EditorDocument.attachEvent("onkeyup", updateBlanks) ;
  70. } else {
  71. editorInstance.EditorDocument.addEventListener("keyup", updateBlanks, true);
  72. }
  73. }
  74. var firstTime = true;
  75. function updateBlanks() {
  76. if (firstTime) {
  77. field = document.getElementById("answer");
  78. var answer = field.value;
  79. } else {
  80. var oEditor = FCKeditorAPI.GetInstance("answer");
  81. //var answer = oEditor.GetXHTML(true);
  82. var answer = oEditor.EditorDocument.body.innerHTML;
  83. }
  84. var blanks = answer.match(/\[[^\]]*\]/g);
  85. var fields = "<div class=\"control-group\"><label class=\"control-label\">'.get_lang('VariableRanges').'</label><div class=\"controls\"><table>";
  86. if (blanks!=null) {
  87. if (typeof updateBlanks.randomValues === "undefined") {
  88. updateBlanks.randomValues = [];
  89. }
  90. for (i=0 ; i<blanks.length ; i++){
  91. if (document.getElementById("lowestValue["+i+"]") && document.getElementById("highestValue["+i+"]")) {
  92. lowestValue = document.getElementById("lowestValue["+i+"]").value;
  93. highestValue = document.getElementById("highestValue["+i+"]").value;
  94. } else {
  95. lowestValue = '.$lowestValue.'.toFixed(2);
  96. highestValue = '.$highestValue.'.toFixed(2);
  97. for (j=0; j<blanks.length; j++) {
  98. updateBlanks.randomValues[j] = parseFloat(Math.random() * (highestValue - lowestValue) + lowestValue).toFixed(2);
  99. }
  100. }
  101. fields += "<tr><td><label>"+blanks[i]+"</label></td><td><input class=\"span1\" style=\"margin-left: 0em;\" size=\"5\" value=\""+lowestValue+"\" type=\"text\" id=\"lowestValue["+i+"]\" name=\"lowestValue["+i+"]\" onblur=\"updateRandomValue(this)\"/></td><td><input class=\"span1\" style=\"margin-left: 0em; width:80px;\" size=\"5\" value=\""+highestValue+"\" type=\"text\" id=\"highestValue["+i+"]\" name=\"highestValue["+i+"]\" onblur=\"updateRandomValue(this)\"/></td><td><label class=\"span3\" id=\"randomValue["+i+"]\"/>'.get_lang('ExampleValue').': "+updateBlanks.randomValues[i]+"</label></td></tr>";
  102. }
  103. }
  104. document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
  105. if (firstTime) {
  106. firstTime = false;
  107. }
  108. }
  109. window.onload = updateBlanks;
  110. </script>';
  111. // answer
  112. $form->addElement('label', null, '<br /><br />'.get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank'));
  113. $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'));
  114. $form->addRule('answer', get_lang('GiveText'),'required');
  115. $form->addRule('answer', get_lang('DefineBlanks'),'regex','/\[.*\]/');
  116. $form->addElement('label', null, get_lang('IfYouWantOnlyIntegerValuesWriteBothLimitsWithoutDecimals'));
  117. $form->addElement('html', '<div id="blanks_weighting"></div>');
  118. $notationListButton = Display::url(
  119. get_lang('NotationList'),
  120. api_get_path(WEB_PATH).'main/exercice/evalmathnotation.php',
  121. array(
  122. 'class' => 'btn ajax',
  123. '_target' => '_blank'
  124. )
  125. );
  126. $form->addElement(
  127. 'html',
  128. '<div class="control-group">
  129. <label class="control-label"></label>
  130. <div class="controls">'.$notationListButton.'</div>
  131. </div>');
  132. $form->addElement('label', null, get_lang('FormulaExample'));
  133. $form->addElement('text', 'formula', get_lang('Formula'), array('id' => 'formula', 'class' => 'span4'));
  134. $form->addRule('formula', get_lang('GiveFormula'), 'required');
  135. $form->addElement('text', 'weighting', get_lang('Weighting'), array('id' => 'weighting', 'class' => 'span1'));
  136. $form->setDefaults(array('weighting' => '10'));
  137. $form->addElement('text', 'answerVariations', get_lang('AnswerVariations'), array('class' => 'span1'));
  138. $form->addRule('answerVariations', get_lang('GiveAnswerVariations'),'required');
  139. $form->setDefaults(array('answerVariations' => '1'));
  140. global $text, $class;
  141. // setting the save button here and not in the question class.php
  142. $form->addElement('style_submit_button', 'submitQuestion', $text, 'class="'.$class.'"');
  143. if (!empty($this->id)) {
  144. $form -> setDefaults($defaults);
  145. } else {
  146. if ($this->isContent == 1) {
  147. $form->setDefaults($defaults);
  148. }
  149. }
  150. }
  151. /**
  152. * abstract function which creates the form to create / edit the answers of the question
  153. * @param FormValidator $form
  154. */
  155. function processAnswersCreation($form)
  156. {
  157. if (!self::isAnswered()) {
  158. $table = Database::get_course_table(TABLE_QUIZ_ANSWER);
  159. Database::delete(
  160. $table,
  161. array(
  162. 'c_id = ? AND question_id = ?' => array(
  163. $this->course['real_id'],
  164. $this->id
  165. )
  166. )
  167. );
  168. $answer = $form->getSubmitValue('answer');
  169. $formula = $form->getSubmitValue('formula');
  170. $lowestValues = $form->getSubmitValue('lowestValue');
  171. $highestValues = $form->getSubmitValue('highestValue');
  172. $answerVariations = $form->getSubmitValue('answerVariations');
  173. $this->weighting = $form->getSubmitValue('weighting');
  174. // Create as many answers as $answerVariations
  175. for ($j=0 ; $j < $answerVariations; $j++) {
  176. $auxAnswer = $answer;
  177. $auxFormula = $formula;
  178. $nb = preg_match_all('/\[[^\]]*\]/', $auxAnswer, $blanks);
  179. if ($nb > 0) {
  180. for ($i=0 ; $i < $nb; ++$i) {
  181. $blankItem = $blanks[0][$i];
  182. $replace = array("[", "]");
  183. $newBlankItem = str_replace($replace, "", $blankItem);
  184. $newBlankItem = "[".trim($newBlankItem)."]";
  185. // take random float values when one or both edge values have a decimal point
  186. $randomValue =
  187. (strpos($lowestValues[$i],'.') !== false ||
  188. strpos($highestValues[$i],'.') !== false) ?
  189. mt_rand($lowestValues[$i]*100,$highestValues[$i]*100)/100 :
  190. mt_rand($lowestValues[$i],$highestValues[$i]);
  191. $auxAnswer = str_replace($blankItem, $randomValue, $auxAnswer);
  192. $auxFormula = str_replace($blankItem, $randomValue, $auxFormula);
  193. }
  194. require_once(api_get_path(LIBRARY_PATH).'evalmath.class.php');
  195. $math = new EvalMath();
  196. $result = $math->evaluate($auxFormula);
  197. $result = number_format($result, 2, ".", "");
  198. // Remove decimal trailing zeros
  199. $result = rtrim($result, "0");
  200. // If it is an integer (ends in .00) remove the decimal point
  201. if (mb_substr($result, -1) === ".") {
  202. $result = str_replace(".", "", $result);
  203. }
  204. // Attach formula
  205. $auxAnswer .= " [".$result."]@@".$formula;
  206. }
  207. $this->save();
  208. $objAnswer = new answer($this->id);
  209. $objAnswer->createAnswer($auxAnswer, 1, '', $this->weighting, null);
  210. $objAnswer->position = array();
  211. $objAnswer->save();
  212. }
  213. }
  214. }
  215. /**
  216. * @param null $feedback_type
  217. * @param null $counter
  218. * @param null $score
  219. * @return null|string
  220. */
  221. function return_header($feedback_type = null, $counter = null, $score = null)
  222. {
  223. $header = parent::return_header($feedback_type, $counter, $score);
  224. $header .= '<table class="'.$this->question_table_class .'">
  225. <tr>
  226. <th>'.get_lang("Answer").'</th>
  227. </tr>';
  228. return $header;
  229. }
  230. /**
  231. * Returns true if the current question has been attempted to be answered
  232. * @return boolean
  233. */
  234. public function isAnswered()
  235. {
  236. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  237. $result = Database::select(
  238. 'question_id',
  239. $table,
  240. array(
  241. 'where' => array(
  242. 'question_id = ? AND course_code = ?' => array(
  243. $this->id,
  244. $this->course['code']
  245. )
  246. )
  247. )
  248. );
  249. return empty($result) ? false : true;
  250. }
  251. }