TK2.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * TimeKeeper front page controller: shows the student's stats, checks a
  4. * submitted answer, and asks a question generator for the next question.
  5. *
  6. * Changes from the current repo version:
  7. * - removed adminDev(): for every Main Admin it ran on every page load,
  8. * printed debug dumps (the students' settings and stats, test numbers),
  9. * hard-coded two students' user names, set the time zone, and generated a
  10. * question from the unfinished "continents" generator
  11. * - checkAnswer() called delete(..., true): the third argument is e107's
  12. * debug flag, so the SQL was printed on the page after every answer.
  13. * It's gone, and the user name is escaped
  14. * - generators are loaded with require_once and an absolute path (include
  15. * plus ./relative paths depended on the working directory and could
  16. * redeclare a class)
  17. * - getQuestion(): generalKnowledge fell through into elements (no break);
  18. * countries/flags/capitalCities built an elements2 object after including
  19. * continents.php (class not found). Subjects that aren't built yet now
  20. * show a plain message instead
  21. * - getScores()/getLastQuestion(): MPREFIX instead of `e107.e107_...`, user
  22. * escaped, and a student with no answers this month gets zeros (not
  23. * null) for the stats
  24. * - showMessage(): user name escaped
  25. * - someone with neither the student nor the teacher class now sees a
  26. * message instead of an empty page
  27. * - division and rounding are switched on (rounding at 50,000, division at 300,000)
  28. * - new fractions subject (switched off: see $subjectsList)
  29. * - new word_problems subject (switched off: see $subjectsList); the old
  30. * worded_* subjects were mapped to the spelling generator and are gone
  31. */
  32. require_once __DIR__ . '/TkAnswerStore.php';
  33. class TK2 {
  34. private $data = [];
  35. private $subjectsList = [
  36. // Math
  37. [['numbers', 1, 0], // number sequences
  38. ['addition', 1, 10000], // Numeracy adding
  39. ['subtraction', 1, 20000], // Numeracy minus'
  40. ['multiplication', 1, 200000], // Numeracy times'
  41. ['division', 1, 300000], // Numeracy deviding
  42. ['rounding', 1, 50000], // Numeracy rounding (money to the nearest 5 cents)
  43. ['word_problems', 1, 250000], // Worded maths problems: change the 0 to a 1 to switch them on
  44. ['fractions', 1, 350000], // Fractions: change the 0 to a 1 to switch them on
  45. ],
  46. // English
  47. [['spelling', 1, 0], // Plain spelling
  48. ['anagrams', 1, 4000000], // Shuffle letters in a word
  49. ],
  50. // General knowledge
  51. [['generalKnowledge', 0, 0], // Name kinda says it all
  52. ],
  53. // Science
  54. [['elements', 1, 150000], // Periodic table of elements
  55. ],
  56. // Geography
  57. [['continents', 0, 0], // Point to the continent
  58. ['countries', 0, 0], // Point to the country
  59. ['flags', 0, 0], // Name the country by the flag picture
  60. ['capitalCities', 0, 0], // Name the capital for a country
  61. ],
  62. // History
  63. // Art
  64. [['artists', 0, 0], // Name the artist for famous paintings
  65. ],
  66. // Technology
  67. // language
  68. [['malaySpelling', 0, 0], // This could be interesting
  69. ],
  70. // Health - Health and Physical Education
  71. [['nameBodyParts', 0, 0], // Name the body parts from the images or point to the part
  72. ['nameBodyOrgans', 0, 0], // Name the organs or what they do
  73. ['basicFirstAid', 0, 0], // Some BASIC things to know for first aid
  74. ],
  75. ];
  76. /**
  77. * Run the timekeeper functionality
  78. */
  79. public function run(): void {
  80. $isStudent = check_class("TIMEKEEPER_STUDENT");
  81. $isTeacher = check_class("TIMEKEEPER_TEACHER");
  82. if ($isStudent) {
  83. $this->student();
  84. }
  85. if ($isTeacher) {
  86. $this->teacher();
  87. }
  88. if (!$isStudent && !$isTeacher) {
  89. e107::getRender()->tablerender('TimeKeeper', USER ? 'Your account has no TimeKeeper role (student or teacher).' : 'Please log in to use TimeKeeper.');
  90. }
  91. }
  92. /**
  93. * Run the sequence for teachers
  94. */
  95. private function teacher(): void {
  96. }
  97. /**
  98. * Run the sequence for students
  99. */
  100. private function student(): void {
  101. $user = e107::getUser()->getLoginName();
  102. $this->checkAnswer($user);
  103. $this->getScores($user);
  104. $this->showMessage();
  105. // If the last question has not been answered ask again.
  106. if ($this->getLastQuestion()) {
  107. return;
  108. }
  109. $this->getSubject();
  110. $this->getQuestion();
  111. }
  112. /**
  113. * Check if the last question was answered and check the answer
  114. * @param string $user
  115. */
  116. private function checkAnswer($user): void {
  117. $subject = filter_input(INPUT_POST, 'subject');
  118. if (is_string($subject) && $subject !== '') {
  119. require_once __DIR__ . '/answerChecker2.php';
  120. $ac = new answerChecker2();
  121. $ac->checkQuestionSubject($subject, $user);
  122. $db = e107::getDb();
  123. $db->delete('tkLastQuestion', "user = '" . $db->escape($user) . "'");
  124. }
  125. }
  126. /**
  127. * Show the last question asked if it hasn't been answered.
  128. * @return bool True when a question was shown.
  129. */
  130. private function getLastQuestion(): bool {
  131. $db = e107::getDb();
  132. $lastQuestion = $db->retrieve("SELECT LastQuestionHtml FROM `" . MPREFIX . "tkLastQuestion` WHERE user = '" . $db->escape($this->data['user']) . "' ORDER BY id DESC LIMIT 1");
  133. if (!empty($lastQuestion)) {
  134. e107::getRender()->tablerender('Last Question', base64_decode($lastQuestion));
  135. return true;
  136. }
  137. return false;
  138. }
  139. /**
  140. * Get the question based on selected subject
  141. * @return void
  142. */
  143. private function getQuestion(): void {
  144. switch ($this->data['selectedSubject']) {
  145. case 'numbers':
  146. require_once __DIR__ . '/questionGenerators/math/numbers2.php';
  147. $questionGen = new numbers2($this->data['user']);
  148. $questionGen->getNumbers();
  149. break;
  150. case 'addition':
  151. case 'subtraction':
  152. case 'multiplication':
  153. case 'division':
  154. case 'rounding':
  155. require_once __DIR__ . '/questionGenerators/math/numeracy2.php';
  156. $questionGen = new numeracy2($this->data['user'], $this->data['selectedSubject']);
  157. $questionGen->getNumeracy();
  158. break;
  159. case 'fractions':
  160. require_once __DIR__ . '/questionGenerators/math/fractions.php';
  161. $questionGen = new fractions($this->data['user']);
  162. $questionGen->getFractions();
  163. break;
  164. case 'word_problems':
  165. require_once __DIR__ . '/questionGenerators/math/math_word_problems.php';
  166. $questionGen = new math_word_problems($this->data['user']);
  167. $questionGen->getWordProblem();
  168. break;
  169. case 'spelling':
  170. require_once __DIR__ . '/questionGenerators/english/spelling2.php';
  171. $questionGen = new spelling2($this->data['user']);
  172. $questionGen->getSpellingWord();
  173. break;
  174. case 'anagrams':
  175. require_once __DIR__ . '/questionGenerators/english/anagrams.php';
  176. $questionGen = new anagrams($this->data['user']);
  177. $questionGen->getanagram();
  178. break;
  179. case 'elements':
  180. require_once __DIR__ . '/questionGenerators/science/elements2.php';
  181. $questionGen = new elements2($this->data['user']);
  182. $questionGen->getElements();
  183. break;
  184. case 'generalKnowledge':
  185. case 'continents':
  186. case 'countries':
  187. case 'flags':
  188. case 'capitalCities':
  189. case 'artists':
  190. case 'malaySpelling':
  191. case 'nameBodyParts':
  192. case 'nameBodyOrgans':
  193. case 'basicFirstAid':
  194. default:
  195. // Not built yet (or unknown): none of these are in the random pool.
  196. e107::getRender()->tablerender('TimeKeeper', 'This subject is not available yet. Please refresh the page.');
  197. break;
  198. }
  199. }
  200. /**
  201. * Get the list of available subjects and select a random one.
  202. * @return void
  203. */
  204. private function getSubject(): void {
  205. $usableSubjects = [];
  206. foreach ($this->subjectsList as $subjects) {
  207. foreach ($subjects as $subject) {
  208. if ($subject[1] === 1 && $this->data['difficulty'] >= $subject[2]) {
  209. $usableSubjects[] = $subject[0];
  210. }
  211. }
  212. }
  213. if (empty($usableSubjects)) {
  214. $usableSubjects[] = 'numbers';
  215. }
  216. $this->data['selectedSubject'] = $usableSubjects[mt_rand(0, count($usableSubjects) - 1)];
  217. }
  218. /**
  219. * Show an initial stats message to the user.
  220. * @return void
  221. */
  222. private function showMessage(): void {
  223. $text = "You have answered " . (int) $this->data['cntr'] . " questions this month.<br/>"
  224. . "Your accuacy for the month is " . htmlspecialchars((string) $this->data['percent']) . "%.<br/>"
  225. . "Your difficulty is set at " . htmlspecialchars((string) $this->data['difficulty']) . ".<br/><br/>";
  226. e107::getRender()->tablerender('Your stats ' . htmlspecialchars(ucfirst($this->data['user'])), $text);
  227. }
  228. /**
  229. * Get the initial values needed to generate a sane question.
  230. * @param string $user
  231. * @return void
  232. */
  233. private function getScores($user): void {
  234. $db = e107::getDb();
  235. $u = $db->escape($user);
  236. $returnArray = [];
  237. $returnArray['totalQuestionCount'] = (int) $db->count('tkReport', '(id)', "username = '{$u}'");
  238. $db->gen("SELECT COUNT(id) AS cntr, CAST((SUM(correct)/COUNT(id)*100) AS DECIMAL(5,2)) AS percent
  239. FROM `" . MPREFIX . "tkReport` WHERE username = '{$u}'
  240. AND answereddatestamp >= UNIX_TIMESTAMP(CURRENT_DATE() - INTERVAL 1 MONTH)
  241. AND answereddatestamp <= UNIX_TIMESTAMP(CURRENT_DATE() + INTERVAL 1 DAY)");
  242. $row = $db->fetch();
  243. $returnArray['cntr'] = is_array($row) ? (int) $row['cntr'] : 0;
  244. $returnArray['percent'] = (is_array($row) && $row['percent'] !== null) ? (float) $row['percent'] : 0;
  245. $returnArray['user'] = $user;
  246. $returnArray['difficulty'] = $returnArray['totalQuestionCount'] * $returnArray['percent'];
  247. $this->data = $returnArray;
  248. }
  249. }