TK2.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. require_once __DIR__ . '/TkAnswerStore.php';
  3. class TK2 {
  4. private $data = [];
  5. private $subjectsList = [
  6. // Math
  7. [['numbers',1,0],// number sequences
  8. ['addition',1,10000],// Numeracy adding
  9. ['subtraction',1,20000],// Numeracy minus'
  10. ['multiplication',1,200000],// Numeracy times'
  11. ['division',0,0],// Numeracy deviding
  12. ['rounding',0,0],// Numeracy rounding
  13. ['worded_add',0,0],// Numeracy worded adding
  14. ['worded_sub',0,0],// Numeracy worded minusing
  15. ['worded_mult',0,0],// Numeracy worded timesing
  16. ['worded_devi',0,0],// Numeracy worded deviding
  17. ],
  18. // English
  19. [['spelling',1,0],// Plain spelling
  20. ['anagrams',1,4000000],// Shuffle letters in a word
  21. ],
  22. // General knowledge
  23. [['generalKnowledge',0,0],// Name kinda says it all
  24. ],
  25. // Science
  26. [['elements',1,150000],// Periodic table of elements
  27. ],
  28. // Geography
  29. [['continents',0,0],// Point to the continent
  30. ['countries',0,0],// Point to the country
  31. ['flags',0,0],// Name the country by the flag picture
  32. ['capitalCities',0,0],// Name the capital for a country
  33. ],
  34. // History
  35. // Art
  36. [['artists',0,0],// Name the artist for famous paintings
  37. ],
  38. // Technology
  39. // language
  40. [['malaySpelling',0,0],// This could be interesting
  41. ],
  42. // Health - Health and Physical Education
  43. [['nameBodyParts',0,0],// Name the body parts from the images or point to the part
  44. ['nameBodyOrgans',0,0],// Name the organs or what they do
  45. ['basicFirstAid',0,0],// Some BASIC things to know for first aid
  46. ],
  47. ];
  48. /**
  49. * Run the timekeeper functionality
  50. */
  51. public function run(): void {
  52. if (check_class("TIMEKEEPER_STUDENT")) {
  53. $this->student();
  54. }
  55. if (check_class("TIMEKEEPER_TEACHER")) {
  56. $this->teacher();
  57. }
  58. if (check_class("Main Admin")) {
  59. $this->adminDev();
  60. }
  61. }
  62. /**
  63. * Run the sequence for admin/developer
  64. */
  65. /**
  66. * Run the sequence for admin/developer
  67. */
  68. private function adminDev(): void {
  69. e107::getRender()->tablerender('Developer', '=================================================================');
  70. ///////////////////////
  71. //// testing !!!!!!!
  72. ///////////////////////
  73. $sign = 'X';
  74. $difficulty = [
  75. 'count' => 64,
  76. 'percent' => 75
  77. ];
  78. if($sign=='X'||$sign=='/')
  79. {
  80. $difficulty['count'] = (int)$difficulty['count'] / 4;
  81. $difficulty['percent'] = (int)$difficulty['percent'] / 6;
  82. }
  83. if($difficulty['count'] >= 300) {
  84. $startingNumber = mt_rand(0, (int) pow($difficulty['count'], ($difficulty['percent']/100)*2));
  85. $endingNumber = mt_rand(0, (int) pow($difficulty['count'], ($difficulty['percent']/100)*2));
  86. $returnNumbers = [$startingNumber, $endingNumber];
  87. } elseif($difficulty['count'] >= 200) {
  88. $startingNumber = mt_rand(0, (int) $difficulty['count'] + ($difficulty['percent']/100)*2);
  89. $endingNumber = mt_rand(0, (int) $difficulty['count'] + ($difficulty['percent']/100)*2);
  90. $returnNumbers = [$startingNumber, $endingNumber];
  91. } elseif($difficulty['count'] >= 100) {
  92. $startingNumber = mt_rand(0, (int) ($difficulty['percent'])*2);
  93. $endingNumber = mt_rand(0, (int) ($difficulty['percent'])*2);
  94. $returnNumbers = [$startingNumber, $endingNumber];
  95. } else {
  96. $startingNumber = mt_rand(0, $difficulty['count'] + 1);
  97. $endingNumber = mt_rand(0, $difficulty['count'] + 1);
  98. $returnNumbers = [$startingNumber, $endingNumber];
  99. }
  100. print_a(['testingData'=>['$sign'=>$sign,'$difficulty[\'count\']'=>$difficulty['count'],'$difficulty[\'percent\']'=>$difficulty['percent'],'$startingNumber'=>$startingNumber,'$endingNumber'=>$endingNumber,'$returnNumbers'=>$returnNumbers]]);
  101. ///////////////////////
  102. //// Overrides !!!!!!!
  103. ///////////////////////
  104. $this->data['user'] = 'silas';
  105. $this->getScores($this->data['user']);
  106. $this->showMessage();
  107. $this->data['user'] = 'silas';//'gregory';
  108. $this->getScores($this->data['user']);
  109. $this->showMessage();
  110. $this->data['user'] = 'elliot';//'gregory';
  111. $this->getScores($this->data['user']);
  112. $this->showMessage();
  113. // =====================================================================
  114. // =========== Testing area ============================================
  115. // =====================================================================
  116. //$difficulty = ['count'=>22,'percent'=>63.6364]; // Silas add
  117. //$difficulty = ['count'=>213,'percent'=>99.0610]; // Elliot add
  118. //$difficulty = ['count'=>21,'percent'=>33.3333]; // Silas sub
  119. //$difficulty = ['count'=>207,'percent'=>97.5845]; // Elliot sub
  120. //$difficulty = ['count'=>0,'percent'=>null]; // Silas mult
  121. $difficulty = ['count'=>201,'percent'=>98.5075]; // Elliot mult
  122. $startingNumber4 = mt_rand(0, (int) pow($difficulty['count'], ($difficulty['percent']/100)*2));
  123. $endingNumber4 = mt_rand(0, (int) pow($difficulty['count'], ($difficulty['percent']/100)*2));
  124. $startingNumber3 = mt_rand(0, (int) $difficulty['count'] + ($difficulty['percent']/50)*2);
  125. $endingNumber3 = mt_rand(0, (int) $difficulty['count'] + ($difficulty['percent']/50)*2);
  126. $startingNumber2 = mt_rand(0, (int) ($difficulty['percent']/10)*4);
  127. $endingNumber2 = mt_rand(0, (int) ($difficulty['percent']/10)*4);
  128. $startingNumber1 = mt_rand(0, $difficulty['count'] + 5);
  129. $endingNumber1 = mt_rand(0, 10);
  130. print_a([['startingNumber1'=>$startingNumber1,'endingNumber1'=>$endingNumber1],
  131. ['startingNumber2'=>$startingNumber2,'endingNumber2'=>$endingNumber2],
  132. ['startingNumber3'=>$startingNumber3,'endingNumber3'=>$endingNumber3],
  133. ['startingNumber4'=>$startingNumber4,'endingNumber4'=>$endingNumber4],]);
  134. // =====================================================================
  135. // =========== End Testing area ========================================
  136. // =====================================================================
  137. //$this->checkAnswer();
  138. // If the last question has not been answered ask again.
  139. // if($this->getLastQuestion()){
  140. // return;
  141. // }
  142. //$this->data['selectedSubject'] = 'addition';
  143. //$this->getQuestion();
  144. //$this->data['selectedSubject'] = 'subtraction';
  145. //$this->getQuestion();
  146. //$this->data['selectedSubject'] = 'multiplication';
  147. $this->data['selectedSubject'] = 'continents';
  148. $this->getQuestion();
  149. //$this->data['selectedSubject'] = 'division';
  150. //$this->getQuestion();
  151. //$this->data['selectedSubject'] = 'rounding';
  152. //$this->getQuestion();
  153. date_default_timezone_set('Australia/Perth');
  154. $QuestionsAnswered = e107::getDb()->retrieve('tkReport', 'COUNT(id)', 'username="elliot" '
  155. . 'AND answereddatestamp >= "' . mktime(0, 0, 0, date('n'), date('j')) . '" '
  156. . 'AND answereddatestamp <= "' . mktime(23, 59, 59, date('n'), date('j')) . '"');
  157. $settings = e107::getDb()->retrieve('timekeeper', '*', 'username="elliot"');
  158. print_a(['ElliotsCounter'=>$QuestionsAnswered,'ElliotsSettings'=>$settings]);
  159. //print_a($this->subjectsList);
  160. print_a($this->data);
  161. $stats = e107::getDb()->retrieve("SELECT (SUM(correct)/COUNT(correct)*100) as percent,COUNT(correct) as count
  162. FROM e107.`e107_tkReport` WHERE
  163. answereddatestamp >= unix_timestamp((NOW()) - INTERVAL 1 MONTH)
  164. AND username = 'elliot' AND subject = 'multiplication';");
  165. print_a($stats);
  166. // Look inside data bases
  167. if(false)
  168. {
  169. //e107_timekeeper
  170. //e107_timekeeperPending
  171. //e107_timekeeperQuestions
  172. //e107_timekeeperReport
  173. //e107_timekeeperScore
  174. //e107_timekeeperStatistics
  175. //e107_timekeeperStats
  176. //e107_tkLastQuestion
  177. //e107_tkReport
  178. //e107_tk_wordList
  179. //e107::getDb()->gen("SHOW TABLES;");
  180. e107::getDb()->gen("SHOW COLUMNS FROM e107_tkReport;");
  181. while ($row = e107::getDb()->fetch()) {
  182. print_a( $row );
  183. }
  184. }
  185. e107::getRender()->tablerender('Developer.', '=================================================================');
  186. }
  187. /**
  188. * Run the sequence for teachers
  189. */
  190. /**
  191. * Run the sequence for teachers
  192. */
  193. private function teacher(): void {
  194. }
  195. /**
  196. * Run the sequence for students
  197. */
  198. /**
  199. * Run the sequence for students
  200. */
  201. private function student(): void {
  202. $this->checkAnswer(e107::getUser()->getLoginName());
  203. $this->getScores(e107::getUser()->getLoginName());
  204. $this->showMessage();
  205. // If the last question has not been answered ask again.
  206. if($this->getLastQuestion()){
  207. return;
  208. }
  209. $this->getSubject();
  210. $this->getQuestion();
  211. }
  212. /**
  213. * Check if the last question was answered and check the answer
  214. */
  215. /**
  216. * Check if the last question was answered and check the answer
  217. * @param string $user
  218. */
  219. private function checkAnswer($user): void {
  220. $subject = filter_input(INPUT_POST,'subject');
  221. if(isset($subject)){
  222. include './answerChecker2.php';
  223. $ac = new answerChecker2();
  224. $ac->checkQuestionSubject($subject,$user);
  225. e107::getDb()->delete('tkLastQuestion',"user = '".$user."'",true);
  226. }
  227. }
  228. /**
  229. * Get the the last question asked if available
  230. */
  231. /**
  232. * Get the last question asked if available
  233. * @return bool
  234. */
  235. private function getLastQuestion(): bool {
  236. $lastQuestion = e107::getDb()->retrieve("SELECT LastQuestionHtml FROM e107.`e107_tkLastQuestion` WHERE user = '".$this->data['user']."';");
  237. //print_a(base64_decode($lastQuestion));
  238. if(isset($lastQuestion)){
  239. e107::getRender()->tablerender('Last Question', base64_decode($lastQuestion));
  240. return true;
  241. }
  242. return false;
  243. }
  244. /**
  245. * Get the list of available subjects and select a random one.
  246. */
  247. /**
  248. * Get the question based on selected subject
  249. * @return void
  250. */
  251. private function getQuestion(): void {
  252. switch ($this->data['selectedSubject']) {
  253. case 'numbers':
  254. include './questionGenerators/math/numbers2.php';
  255. $questionGen = new numbers2($this->data['user']);
  256. $questionGen->getNumbers();
  257. break;
  258. case 'addition':
  259. case 'subtraction':
  260. case 'multiplication':
  261. case 'division':
  262. case 'rounding':
  263. include './questionGenerators/math/numeracy2.php';
  264. $questionGen = new numeracy2($this->data['user'],$this->data['selectedSubject']);
  265. $questionGen->getNumeracy();
  266. break;
  267. case 'worded_add':
  268. case 'worded_sub':
  269. case 'worded_mult':
  270. case 'worded_devi':
  271. case 'spelling':
  272. include './questionGenerators/english/spelling2.php';
  273. $questionGen = new spelling2($this->data['user']);
  274. $questionGen->getSpellingWord();
  275. break;
  276. case 'anagrams':
  277. include './questionGenerators/english/anagrams.php';
  278. $questionGen = new anagrams($this->data['user']);
  279. $questionGen->getanagram();
  280. break;
  281. case 'generalKnowledge':
  282. include './questionGenerators/general/generalKnowledge.php';//=========================== TODO:not working yet
  283. $questionGen = new generalKnowledge($this->data['user']);
  284. $questionGen->getgeneralKnowledge();
  285. case 'elements':
  286. include './questionGenerators/science/elements2.php';
  287. $questionGen = new elements2($this->data['user']);
  288. $questionGen->getElements();
  289. break;
  290. case 'continents':
  291. include './questionGenerators/geography/continents.php';//=========================== TODO:not working yet
  292. $questionGen = new continents($this->data['user']);
  293. $questionGen->getContinents();
  294. break;
  295. case 'countries':
  296. include './questionGenerators/geography/continents.php';//=========================== TODO:not working yet
  297. $questionGen = new elements2($this->data['user']);
  298. $questionGen->getElements();
  299. break;
  300. case 'flags':
  301. include './questionGenerators/geography/continents.php';//=========================== TODO:not working yet
  302. $questionGen = new elements2($this->data['user']);
  303. $questionGen->getElements();
  304. break;
  305. case 'capitalCities':
  306. include './questionGenerators/geography/continents.php';//=========================== TODO:not working yet
  307. $questionGen = new elements2($this->data['user']);
  308. $questionGen->getElements();
  309. break;
  310. case 'artists':
  311. case 'malaySpelling':
  312. case 'nameBodyParts':
  313. case 'nameBodyOrgans':
  314. case 'basicFirstAid':
  315. default:
  316. print_a('An error has been detected in the question selector. Please report this and refresh the page.');
  317. break;
  318. }
  319. }
  320. /**
  321. * Get the list of available subjects and select a random one.
  322. */
  323. /**
  324. * Get the list of available subjects and select a random one.
  325. * @return void
  326. */
  327. private function getSubject(): void {
  328. $usableSubjects = [];
  329. foreach ($this->subjectsList as $subjects){
  330. foreach ($subjects as $subject){
  331. if($subject[1]===1&&$this->data['difficulty']>=$subject[2]){
  332. $usableSubjects[] = $subject[0];
  333. }
  334. }
  335. }
  336. $this->data['selectedSubject'] = $usableSubjects[mt_rand('0',count($usableSubjects)-1)];
  337. }
  338. /**
  339. * Show an initial stats message to the user.
  340. */
  341. /**
  342. * Show an initial stats message to the user.
  343. * @return void
  344. */
  345. private function showMessage(): void {
  346. $ns = e107::getRender();
  347. $text = "You have answered ".$this->data['cntr']." questions this month.<br/>"
  348. . "Your accuacy for the month is ".$this->data['percent']."%.<br/>"
  349. . "Your difficulty is set at ".$this->data['difficulty'].".<br/><br/>";
  350. $ns->tablerender('Your stats '.ucfirst($this->data['user']), $text);
  351. }
  352. /**
  353. * Get the initial values needed to generate a sane question.
  354. * @param type $user
  355. */
  356. /**
  357. * Get the initial values needed to generate a sane question.
  358. * @param string $user
  359. * @return void
  360. */
  361. private function getScores($user): void {
  362. $returnArray = [];
  363. $returnArray['totalQuestionCount'] = e107::getDb()->count("tkReport", "(id)", "username = '" . $user . "'");
  364. e107::getDb()->gen("SELECT COUNT(id) as cntr, CAST((SUM(correct)/COUNT(id)*100) as DECIMAL(5,2)) as percent
  365. FROM e107.`e107_tkReport` WHERE username = '" . $user . "'
  366. AND answereddatestamp >= unix_timestamp(CURRENT_DATE() - INTERVAL 1 MONTH)
  367. AND answereddatestamp <= unix_timestamp(CURRENT_DATE() + INTERVAL 1 DAY)");
  368. while ($row = e107::getDb()->fetch()) {
  369. $returnArray = array_merge($returnArray,$row);
  370. }
  371. $returnArray['user'] = $user;
  372. $returnArray['difficulty'] = $returnArray['totalQuestionCount']*$returnArray['percent'];
  373. $this->data = $returnArray;
  374. }
  375. }