calculated_answer.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Webit\Util\EvalMath\EvalMath;
  4. /**
  5. * Class CalculatedAnswer
  6. * This class contains calculated answer form and answer processing functions.
  7. *
  8. * @author Imanol Losada
  9. */
  10. class CalculatedAnswer extends Question
  11. {
  12. public $typePicture = 'calculated_answer.png';
  13. public $explanationLangVar = 'CalculatedAnswer';
  14. /**
  15. * Constructor.
  16. */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->type = CALCULATED_ANSWER;
  21. $this->isContent = $this->getIsContent();
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function createAnswersForm($form)
  27. {
  28. $defaults = [];
  29. //$defaults['answer'] = get_lang('<table cellspacing="0" cellpadding="10" border="1" width="720" style="" height:=""> <tbody> <tr> <td colspan="2"> <h3>Example fill the form activity : calculate the Body Mass Index</h3> </td> </tr> <tr> <td style="text-align: right;"><strong>Age</strong></td> <td width="75%" style="">[25] years old</td> </tr> <tr> <td style="text-align: right;"><strong>Sex</strong></td> <td style="" text-align:="">[M] (M or F)</td> </tr> <tr> <td style="text-align: right;"><strong>Weight</strong></td> <td style="" text-align:="">95 Kg</td> </tr> <tr> <td style="vertical-align: top; text-align: right;"><strong>Height</strong></td> <td style="vertical-align: top;">1.81 m</td> </tr> <tr> <td style="vertical-align: top; text-align: right;"><strong>Body Mass Index</strong></td> <td style="vertical-align: top;">[29] BMI =Weight/Size<sup>2</sup> (Cf.<a href="http://en.wikipedia.org/wiki/Body_mass_index" onclick="window.open(this.href,'','resizable=yes,location=yes,menubar=no,scrollbars=yes,status=yes,toolbar=no,fullscreen=no,dependent=no,width=800,height=600,left=40,top=40,status'); return false"> Wikipedia article</a>)</td> </tr> </tbody></table>');
  30. $defaults['answer'] = get_lang('DefaultTextInBlanks');
  31. if (!empty($this->id)) {
  32. $objAnswer = new Answer($this->id);
  33. $preArray = explode('@@', $objAnswer->selectAnswer(1));
  34. $defaults['formula'] = array_pop($preArray);
  35. $defaults['answer'] = array_shift($preArray);
  36. $defaults['answer'] = preg_replace("/\[.*\]/", '', $defaults['answer']);
  37. $defaults['weighting'] = $this->weighting;
  38. }
  39. $lowestValue = '1.00';
  40. $highestValue = '20.00';
  41. // javascript //
  42. echo '<script>
  43. function parseTextNumber(textNumber, floatValue) {
  44. if (textNumber.indexOf(".") > -1) {
  45. textNumber = parseFloat(textNumber);
  46. floatValue.exists = "true";
  47. } else {
  48. textNumber = parseInt(textNumber);
  49. }
  50. return textNumber;
  51. }
  52. function updateRandomValue(element) {
  53. // "floatValue" helps to distinguish between an integer (10) and a float with all 0 decimals (10.00)
  54. var floatValue = { exists: "false" };
  55. var index = (element.name).match(/\[[^\]]*\]/g);
  56. var lowestValue = parseTextNumber(document.getElementById("lowestValue"+index).value, floatValue);
  57. var highestValue = parseTextNumber(document.getElementById("highestValue"+index).value, floatValue);
  58. var result = Math.random() * (highestValue - lowestValue) + lowestValue;
  59. if (floatValue.exists == "true") {
  60. result = parseFloat(result).toFixed(2);
  61. } else {
  62. result = parseInt(result);
  63. }
  64. document.getElementById("randomValue"+index).innerHTML = "'.get_lang("Range value").': " + result;
  65. }
  66. CKEDITOR.on("instanceCreated", function(e) {
  67. if (e.editor.name === "answer") {
  68. e.editor.on("change", updateBlanks);
  69. }
  70. });
  71. var firstTime = true;
  72. function updateBlanks(e) {
  73. if (firstTime) {
  74. field = document.getElementById("answer");
  75. var answer = field.value;
  76. } else {
  77. var answer = e.editor.getData();
  78. }
  79. var blanks = answer.match(/\[[^\]]*\]/g);
  80. var fields = "<div class=\"form-group\"><label class=\"col-sm-2\">'.get_lang('Variable ranges').'</label><div class=\"col-sm-8\"><table>";
  81. if (blanks!=null) {
  82. if (typeof updateBlanks.randomValues === "undefined") {
  83. updateBlanks.randomValues = [];
  84. }
  85. for (i=0 ; i<blanks.length ; i++){
  86. if (document.getElementById("lowestValue["+i+"]") && document.getElementById("highestValue["+i+"]")) {
  87. lowestValue = document.getElementById("lowestValue["+i+"]").value;
  88. highestValue = document.getElementById("highestValue["+i+"]").value;
  89. } else {
  90. lowestValue = '.$lowestValue.'.toFixed(2);
  91. highestValue = '.$highestValue.'.toFixed(2);
  92. for (j=0; j<blanks.length; j++) {
  93. updateBlanks.randomValues[j] = parseFloat(Math.random() * (highestValue - lowestValue) + lowestValue).toFixed(2);
  94. }
  95. }
  96. 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('Range value').': "+updateBlanks.randomValues[i]+"</label></td></tr>";
  97. }
  98. }
  99. document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
  100. if (firstTime) {
  101. firstTime = false;
  102. }
  103. }
  104. window.onload = updateBlanks;
  105. </script>';
  106. // answer
  107. $form->addElement('label', null, '<br /><br />'.get_lang('Please type your text below').', '.get_lang('and').' '.get_lang('use square brackets [...] to define one or more blanks'));
  108. $form->addElement(
  109. 'html_editor',
  110. 'answer',
  111. Display::return_icon('fill_field.png'),
  112. [
  113. 'id' => 'answer',
  114. 'onkeyup' => 'javascript: updateBlanks(this);',
  115. ],
  116. [
  117. 'ToolbarSet' => 'TestQuestionDescription',
  118. 'Width' => '100%',
  119. 'Height' => '350',
  120. ]
  121. );
  122. $form->addRule('answer', get_lang('Please type the text'), 'required');
  123. $form->addRule('answer', get_lang('Please define at least one blank with square brackets [...]'), 'regex', '/\[.*\]/');
  124. $form->addElement('label', null, get_lang('If you want only integer values write both limits without decimals'));
  125. $form->addElement('html', '<div id="blanks_weighting"></div>');
  126. $notationListButton = Display::url(
  127. get_lang('Formula notation'),
  128. api_get_path(WEB_CODE_PATH).'exercise/evalmathnotation.php',
  129. [
  130. 'class' => 'btn btn-info ajax',
  131. 'data-title' => get_lang('Formula notation'),
  132. '_target' => '_blank',
  133. ]
  134. );
  135. $form->addElement(
  136. 'label',
  137. null,
  138. $notationListButton
  139. );
  140. $form->addElement('text', 'formula', [get_lang('Formula'), get_lang('Formula sample: sqrt( [x] / [y] ) * ( e ^ ( ln(pi) ) )')], ['id' => 'formula']);
  141. $form->addRule('formula', get_lang('Please, write the formula'), 'required');
  142. $form->addElement('text', 'weighting', get_lang('Score'), ['id' => 'weighting']);
  143. $form->setDefaults(['weighting' => '10']);
  144. $form->addElement('text', 'answerVariations', get_lang('Question variations'));
  145. $form->addRule(
  146. 'answerVariations',
  147. get_lang('GiveQuestion variations'),
  148. 'required'
  149. );
  150. $form->setDefaults(['answerVariations' => '1']);
  151. global $text;
  152. // setting the save button here and not in the question class.php
  153. $form->addButtonSave($text, 'submitQuestion');
  154. if (!empty($this->id)) {
  155. $form->setDefaults($defaults);
  156. } else {
  157. if ($this->isContent == 1) {
  158. $form->setDefaults($defaults);
  159. }
  160. }
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function processAnswersCreation($form, $exercise)
  166. {
  167. if (!self::isAnswered()) {
  168. $table = Database::get_course_table(TABLE_QUIZ_ANSWER);
  169. Database::delete(
  170. $table,
  171. [
  172. 'c_id = ? AND question_id = ?' => [
  173. $this->course['real_id'],
  174. $this->id,
  175. ],
  176. ]
  177. );
  178. $answer = $form->getSubmitValue('answer');
  179. $formula = $form->getSubmitValue('formula');
  180. $lowestValues = $form->getSubmitValue('lowestValue');
  181. $highestValues = $form->getSubmitValue('highestValue');
  182. $answerVariations = $form->getSubmitValue('answerVariations');
  183. $this->weighting = $form->getSubmitValue('weighting');
  184. // Create as many answers as $answerVariations
  185. for ($j = 0; $j < $answerVariations; $j++) {
  186. $auxAnswer = $answer;
  187. $auxFormula = $formula;
  188. $nb = preg_match_all('/\[[^\]]*\]/', $auxAnswer, $blanks);
  189. if ($nb > 0) {
  190. for ($i = 0; $i < $nb; $i++) {
  191. $blankItem = $blanks[0][$i];
  192. // take random float values when one or both edge values have a decimal point
  193. $randomValue =
  194. (strpos($lowestValues[$i], '.') !== false ||
  195. strpos($highestValues[$i], '.') !== false) ?
  196. mt_rand($lowestValues[$i] * 100, $highestValues[$i] * 100) / 100 : mt_rand($lowestValues[$i], $highestValues[$i]);
  197. $auxAnswer = str_replace($blankItem, $randomValue, $auxAnswer);
  198. $auxFormula = str_replace($blankItem, $randomValue, $auxFormula);
  199. }
  200. $math = new EvalMath();
  201. $result = $math->evaluate($auxFormula);
  202. $result = number_format($result, 2, '.', '');
  203. // Remove decimal trailing zeros
  204. $result = rtrim($result, '0');
  205. // If it is an integer (ends in .00) remove the decimal point
  206. if (mb_substr($result, -1) === '.') {
  207. $result = str_replace('.', '', $result);
  208. }
  209. // Attach formula
  210. $auxAnswer .= " [".$result."]@@".$formula;
  211. }
  212. $this->save($exercise);
  213. $objAnswer = new Answer($this->id);
  214. $objAnswer->createAnswer($auxAnswer, 1, '', $this->weighting, '');
  215. $objAnswer->position = [];
  216. $objAnswer->save();
  217. }
  218. }
  219. }
  220. /**
  221. * {@inheritdoc}
  222. */
  223. public function return_header(Exercise $exercise, $counter = null, $score = [])
  224. {
  225. $header = parent::return_header($exercise, $counter, $score);
  226. $header .= '<table class="'.$this->question_table_class.'"><tr>';
  227. $header .= '<th>'.get_lang('Answer').'</th>';
  228. if ($exercise->showExpectedChoice()) {
  229. $header .= '<th>'.get_lang('Your choice').'</th>';
  230. if ($exercise->showExpectedChoiceColumn()) {
  231. $header .= '<th>'.get_lang('Expected choice').'</th>';
  232. }
  233. $header .= '<th>'.get_lang('Status').'</th>';
  234. }
  235. $header .= '</tr>';
  236. return $header;
  237. }
  238. /**
  239. * Returns true if the current question has been attempted to be answered.
  240. *
  241. * @return bool
  242. */
  243. public function isAnswered()
  244. {
  245. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  246. $result = Database::select(
  247. 'question_id',
  248. $table,
  249. [
  250. 'where' => [
  251. 'question_id = ? AND c_id = ?' => [
  252. $this->id,
  253. $this->course['real_id'],
  254. ],
  255. ],
  256. ]
  257. );
  258. return empty($result) ? false : true;
  259. }
  260. }