TK2.php 11 KB

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