forms.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. /**
  8. * Create various forms for timekeeper.
  9. *
  10. * @author gregory
  11. */
  12. class forms {
  13. private $questionData;
  14. private $returnQuestion;
  15. /**
  16. * __construct()
  17. * @param array() $question Question data to insert into template data
  18. */
  19. public function __construct($question) {
  20. $this->questionData = $question;
  21. }
  22. /**
  23. * Select which template to use to create the needed form.
  24. *
  25. * @return type
  26. */
  27. public function templater() {
  28. foreach ($this->questionData as $key => $question) {
  29. switch ($key) {
  30. case 'numbers':
  31. $this->numbers($question['question']);
  32. break;
  33. case 'spelling':
  34. $this->spelling($question['question']);
  35. break;
  36. case 'numeracy':
  37. $this->numeracy($question['question']);
  38. break;
  39. case 'math_word_problems':
  40. $this->math_word_problems($question['question']);
  41. break;
  42. case 'elements':
  43. $this->elements($question['question']);
  44. break;
  45. case 'DebugSubjects':
  46. break;
  47. default:
  48. $this->returnQuestion = 'form error.';
  49. print_a([$key => $this->questionData]);
  50. break;
  51. }
  52. }
  53. return $this->returnQuestion;
  54. }
  55. /**
  56. * Create a number sequence form using the supplied question.
  57. * And write it into "$this->returnQuestion".
  58. *
  59. * @param array() $data Question data to be used.
  60. */
  61. private function numbers($data) {
  62. $frm = e107::getForm();
  63. $qString = $this->formStart('Number sequences', 'Find the missing number.', $data);
  64. foreach ($data['question'] as $seq) {
  65. $qString .= $frm->label($seq . " &nbsp; ") . "\n";
  66. }
  67. $qString .= $this->formEnd($data);
  68. $this->returnQuestion = $qString;
  69. }
  70. /**
  71. * Create a spelling form using the supplied question.
  72. * And write it into "$this->returnQuestion".
  73. *
  74. * @param array() $data Question data to be used.
  75. */
  76. private function spelling($data) {
  77. $tp = e107::getParser();
  78. $qString = $this->formStart(__FUNCTION__, 'Spell the word in the sentence.', $data);
  79. if (!file_exists('./audio/' . md5($data['answer1']) . '2v.wav')) {
  80. exec('./audio/flite-2.0.0-release/bin/flite -voice ./audio/voices/cmu_us_aew.flitevox -o "./audio/' . md5($data['answer1']) . '2v.wav" "Spell the word, ' . $tp->toHTML($data['answer1']) . '. ' . $tp->toHTML($data['question']) . '"');
  81. }
  82. $qString .= "<audio controls><source src='audio/" . md5($data['answer1']) . "2v.wav' type='audio/wav'>Your browser does not support the audio element.</audio>\n";
  83. $qString .= $this->formEnd($data);
  84. $this->returnQuestion = $qString;
  85. }
  86. /**
  87. * Create a numeracy form using the supplied question.
  88. * And write it into "$this->returnQuestion".
  89. *
  90. * @param array() $data Question data to be used.
  91. */
  92. private function numeracy($data) {
  93. $frm = e107::getForm();
  94. $qString = $this->formStart(__FUNCTION__, $this->captionSupport($data['question']), $data);
  95. if ($data['question'][1] == 'r') {
  96. $qString .= $frm->label('$' . $data['question'][0]) . "\n";
  97. } elseif ($data['question'][1] == '/') {
  98. $qString .= $frm->label($data['question'][0] . " &nbsp; " .
  99. "&#247; &nbsp; " . $data['question'][2]) . "\n";
  100. } else {
  101. $qString .= $frm->label($data['question'][0] . " &nbsp; " .
  102. $data['question'][1] . " &nbsp; " . $data['question'][2]) . "\n";
  103. }
  104. $qString .= $this->formEnd($data);
  105. $this->returnQuestion = $qString;
  106. }
  107. /**
  108. * Create a math_word_problems form using the supplied question.
  109. * And write it into "$this->returnQuestion".
  110. *
  111. * @param array() $data Question data to be used.
  112. */
  113. private function math_word_problems($data) {
  114. $frm = e107::getForm();
  115. $qString = $this->formStart(str_replace('_', ' ', __FUNCTION__), 'Solve the following. (Round off to the nearest whole number)', $data);
  116. $qString .= $frm->label($data['question']) . "\n";
  117. $qString .= $this->formEnd($data);
  118. $this->returnQuestion = $qString;
  119. }
  120. /**
  121. * Create a elements form using the supplied question.
  122. * And write it into "$this->returnQuestion".
  123. *
  124. * @param array() $data Question data to be used.
  125. */
  126. private function elements($data) {
  127. //$frm = e107::getForm();
  128. $tp = e107::getParser();
  129. $sc = e107::getScBatch('timekeeper', TRUE);
  130. $elementData = $data['question']['element'];
  131. $elementData[$data['question'][1]] = '???';
  132. $sc->setVars($elementData);
  133. $qString = $this->formStart(str_replace('_', ' ', __FUNCTION__), 'Periodic Table of Elements. Fill in the missing information.', $data);
  134. $qString .= $tp->parseTemplate(file_get_contents('templates/elementTable.html'), true, $sc);
  135. //$qString .= $frm->label($data['question']) . "\n";
  136. $qString .= $this->formEnd($data);
  137. $this->returnQuestion = $qString;
  138. }
  139. private function captionSupport($questionData) {
  140. $descriptions = [
  141. '+' => 'Add the following together.',
  142. '-' => 'Subtract one amount from the other.',
  143. 'x' => 'Multiplication, times one number by another.',
  144. '/' => 'Divide one number from another.',
  145. 'r' => 'Round the following off to the nearest 5 cents.',
  146. ];
  147. return $descriptions[$questionData[1]];
  148. }
  149. private function formStart($label, $caption) {
  150. $frm = e107::getForm();
  151. $fString = $frm->open('questionForm', 'post', '', ['autocomplete' => 'off', 'class' => 'formclass']);
  152. $fString .= $frm->label('<h4>' . ucfirst($label) . '.</h4>') . "</br>\n";
  153. $fString .= $frm->label($caption) . "\n";
  154. $fString .= '</br>';
  155. return $fString;
  156. }
  157. private function formEnd($data) {
  158. $frm = e107::getForm();
  159. $fString = '</br></br>';
  160. $fString .= $frm->text('a1', '', 80, ['placeholder' => 'Write answer here!', 'autocomplete' => "off", 'spellcheck' => "false"]) . "\n";
  161. $fString .= $frm->hidden('checkerData', base64_encode(json_encode($data)), ['id' => 'checkerData']);
  162. $fString .= '</br>';
  163. $fString .= e107::getForm()->button('Answer', 'Check answer') . "\n";
  164. $fString .= e107::getForm()->close();
  165. $fString .= '</br></br>';
  166. return $fString;
  167. }
  168. }