TimeKeeperMain.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. *
  4. * @author gregory
  5. */
  6. class TimeKeeperMain {
  7. public $answerResponce;
  8. function __construct() {
  9. //include 'questionGenerators/subjects.php';
  10. //e107::js('timekeeper','js/my.js','jquery'); // Load Plugin javascript and include jQuery framework
  11. //e107::css('timekeeper','css/my.css'); // load css file
  12. //e107::lan('timekeeper'); // load language file ie. e107_plugins/timekeeper/languages/English.php
  13. }
  14. public function run() {
  15. //print_a($_COOKIE);
  16. //print_a($_POST);
  17. //$sql = e107::getDB(); // mysql class object
  18. $tp = e107::getParser(); // parser for converting to HTML and parsing templates etc.
  19. $sc = e107::getScBatch('timekeeper', TRUE); // loads e107_plugins/myplugin/myplugin_shortcodes.php
  20. $ns = e107::getRender(); // render in theme box.
  21. $form = new FormGenerator();
  22. if(!$this->answerResponce == '') {
  23. $ns->tablerender("Check answer", $this->answerResponce . '<br/>');
  24. header("Refresh:5");
  25. return;
  26. }
  27. //load development area for main site admin only
  28. if (check_class("Main Admin")&&false) {
  29. $ns->tablerender('Start of Developments Area', '<br/>');
  30. require_once (e_PLUGIN . 'timekeeper/DevelopmentArea.php');
  31. $ns->tablerender('End of Development Area', '<br/>');
  32. }
  33. //Check user submitted answer
  34. $submitButton = filter_input(INPUT_POST, 'Answered');
  35. $answerText = filter_input(INPUT_POST, 'a1');
  36. //print_a('-'.$answerText.'-');
  37. if (isset($submitButton)&&!empty($answerText)) {
  38. $AnswerChecker = new AnswerChecker();
  39. $text = $AnswerChecker->checkAnswers();
  40. $ns->tablerender("Check answer", $text . '<br/>');
  41. }
  42. //print_a(['Development area'=>'head var','session'=>$_SESSION]);//============================= debug val
  43. /////////////////////////////////////////
  44. /// Student section
  45. /////////////////////////////////////////
  46. if (check_class("TIMEKEEPER_STUDENT")) {
  47. $debugQ = false;
  48. $countTotal = e107::getDb()->count("tkReport", "(id)", "username = '" . e107::getUser()->getLoginName() . "'");
  49. $currentPercent = 0;
  50. e107::getDb()->gen("SELECT COUNT(id) as cntr, CAST((SUM(correct)/COUNT(id)*10) as DECIMAL(5,2)) as percent
  51. FROM e107.`e107_tkReport` WHERE username = '" . e107::getUser()->getLoginName() . "'
  52. AND answereddatestamp >= unix_timestamp(CURRENT_DATE())
  53. AND answereddatestamp <= unix_timestamp(CURRENT_DATE() + INTERVAL 1 DAY)");
  54. $text .= '</tbody></table>';
  55. while ($row = e107::getDb()->fetch()) {
  56. $currentPercent = (int)$row['percent'];
  57. }
  58. if(empty($_COOKIE['tkQuestion']))
  59. {
  60. // Instanciate the new queation engine with debug settings.
  61. // 0 = Math
  62. // 1 = English
  63. $newQuestions = new subjects((int) ($countTotal / 26000), $currentPercent, $debugQ);
  64. // get array of data for question
  65. $rawQuestion = $newQuestions->getQuestion();
  66. if ($debugQ) {
  67. print_a(['rawQuestion'=>$rawQuestion,'countLevel'=>(int) ($countTotal / 26000),'rawPercent'=>$currentPercent,]);
  68. }
  69. setcookie('tkQuestion', base64_encode(json_encode($rawQuestion)), time()+7200);
  70. }else if(json_decode($rawQuestion,true)==""){
  71. setcookie('tkQuestion', "", time()+1);
  72. header("Refresh:2");
  73. print_a("Error: Blank question detected, resetting now. Please wait.");
  74. }else{
  75. //print_a('reuse session val.');
  76. $cookieQuestion = $_COOKIE['tkQuestion'];
  77. $rawQuestion = json_decode(base64_decode($cookieQuestion),true);
  78. print_a(json_decode($rawQuestion,true));
  79. }
  80. include('forms.php');
  81. $template = new forms($rawQuestion);
  82. $tmpdata = $template->templater();
  83. if ($debugQ) {
  84. print_a($tmpdata);
  85. }
  86. // Get question from engine
  87. $ns->tablerender("Question", $tmpdata);
  88. // $this->MainEntry();
  89. // $text = '';
  90. // $subject = $this->subjectSelector();
  91. // $year = $this->yearLevelSelector($subject);
  92. //
  93. //
  94. // //Return random generated question
  95. // $QG = new timekeeper_Question_Engine;
  96. // $QG->subjectSelector = $subject;
  97. // $QG->yearlevel = $year;
  98. // $content = $QG->GetRandomQuestion();
  99. // $text .= $content; //$this->renderQuestion($frm, $content);
  100. // $ns->tablerender("Question (Do not use)", $text . '<br/>');
  101. }
  102. /////////////////////////////////////////
  103. /// Teacher section
  104. /////////////////////////////////////////
  105. if (check_class("TIMEKEEPER_TEACHER")) {
  106. //$ns->tablerender("ExcelGenerator", $form->ExcelGenerator());
  107. }
  108. }
  109. // private function subjectRandomizer() {
  110. //
  111. // $subjects = [
  112. // 'english' => 0,
  113. // 'pePd' => 0, //health-and-physical-education
  114. // 'socialStudies' => 0, //humanities-and-social-sciences
  115. // 'languages' => 0,
  116. // 'mathematics' => 0,
  117. // 'science' => 0,
  118. // 'technologies' => 0,
  119. // 'art' => 0,
  120. // 'general' => 0 // general knowlage
  121. // ];
  122. // foreach ($this->activeSubjectsList as $key => $sub) {
  123. // $subjects[$key] = $sub['timekeeper_percent'] + rand(0, 200 / $sub['timekeeper_yearLevel']);
  124. // }
  125. // //print_a($subjects);
  126. // //print_a($this->activeSubjectsList);
  127. // return $subjects;
  128. // }
  129. // private function subjectSelector() {
  130. // $returnSubject = '';
  131. // $returnScore = 0;
  132. // foreach ($this->subjectRandomizer() as $sub => $score) {
  133. // if ($returnScore <= $score && $score <> 0) {
  134. // $returnSubject = $sub;
  135. // $returnScore = $score;
  136. // }
  137. // }
  138. // //print_a($returnSubject);
  139. // // Add maths as the starting subject for new users
  140. // if ($returnSubject === '') {
  141. // $returnSubject = 'mathematics';
  142. // }
  143. // return $returnSubject;
  144. // }
  145. // private function yearLevelSelector($subject) {
  146. // return 1;
  147. // }
  148. /**
  149. * Main entry point for checking and updating the statistics and year levels.
  150. *
  151. * Description needed once finished....TODO
  152. *
  153. */
  154. // private function MainEntry() {
  155. // $sql = e107::getDb();
  156. // $userName = e107::getUser()->getLoginName();
  157. //
  158. // $this->GetActiveSubjects($sql, $userName);
  159. // $this->GetActiveYearForSubjects($sql, $userName);
  160. //
  161. // $this->CheckSubjects($sql, $userName);
  162. // }
  163. /**
  164. * Get a list of active subjects.
  165. *
  166. * Populate the private class variable $activeSubjectsList with an array of active subjects for use in statistics and question selection.
  167. *
  168. */
  169. // private function GetActiveSubjects($sql, $userName) {
  170. // $this->activeSubjectsList = [];
  171. //
  172. // $sql->gen("SELECT DISTINCT `timekeeper_subject` FROM e107.e107_timekeeperReport WHERE `timekeeper_username`='$userName';");
  173. // while ($row = $sql->fetch()) {
  174. // $this->activeSubjectsList = array_merge($this->activeSubjectsList, [$row['timekeeper_subject'] => []]);
  175. // }
  176. // }
  177. /**
  178. * Get the year level and percentage for the active subjects
  179. *
  180. * Populate the private class variable $activeSubjectsList with the current year level and correct percentage for the current subjects.
  181. *
  182. */
  183. // private function GetActiveYearForSubjects($sql, $userName) {
  184. //
  185. // foreach ($this->activeSubjectsList as $subject => &$arrayHolder) {
  186. // $selectQuery = '`timekeeper_username` = \'' . $userName . '\' AND `timekeeper_subject` = \'' . $subject . '\' ORDER BY `timekeeper_yearLevel` DESC';
  187. // $arrayHolder = ($sql->retrieve('timekeeperStats', '`timekeeper_yearLevel`,`timekeeper_percent`', $selectQuery));
  188. // }
  189. // }
  190. /**
  191. * Check to see what subjects the user has answered questions for.
  192. *
  193. * This function gets a list of unique subjects from the `e107_timekeeperReport` database for the year level specified and uses that to get all answered questions for that subject to pass to 'UpdateYearAndStats'.
  194. *
  195. * @param int $yearlevel The year to check for records in the database.
  196. *
  197. */
  198. // private function CheckSubjects($sql, $userName) {
  199. // $activeSubjects = array();
  200. // $subjectToProcess = array();
  201. //
  202. // foreach ($this->activeSubjectsList as $subject => $dataArrays) {
  203. // $tmpArray = array();
  204. // $sql->gen("SELECT * FROM e107.e107_timekeeperReport WHERE `timekeeper_username`='$userName' AND `timekeeper_yearLevel`=" . $dataArrays['timekeeper_yearLevel'] . " AND `timekeeper_subject`= '" . $subject . "' ORDER BY 'timekeeper_answereddatestamp' DESC LIMIT 1000;");
  205. // while ($row = $sql->fetch()) {
  206. // array_push($tmpArray, $row);
  207. // }
  208. // $subjectToProcess = ['userName' => $userName, 'subject' => $subject, 'yearlevel' => $dataArrays['timekeeper_yearLevel'], 'rows' => $tmpArray];
  209. // //print_a($subjectToProcess);
  210. // $this->UpdateYearAndStats($sql, $subjectToProcess);
  211. // }
  212. // }
  213. // private function BuildStatisticPdf($sql, $menuYear) {
  214. // //print_a(['BuildStatisticMenu',$menuYear]);
  215. // $userName = e107::getUser()->getLoginName();
  216. // $sql->gen("SELECT `timekeeper_subject`,`timekeeper_percent`,`timekeeper_quantity` FROM e107_timekeeperStats WHERE `timekeeper_yearLevel`=$menuYear AND `timekeeper_username`='$userName';");
  217. // $text = "<h4>Hello $userName</h4>"
  218. // . "You need to score at least 90% and answer at lest 1000 questions in each subject before you can progress to a higher level.</br>"
  219. // . "Your current level is <b>$menuYear</b></br>.";
  220. // while ($row = $sql->fetch()) {
  221. // $text .= 'For the subject ' . $row['timekeeper_subject'] . ' you have scored ' . $row['timekeeper_percent'] . '% and have completed ' . $row['timekeeper_quantity'] . ' questions.';
  222. // }
  223. // //$text .= $sql->getLastErrorText();
  224. // e107::getRender()->tablerender('Score', $text);
  225. // }
  226. // private function BuildStatisticPage($sql, $menuYear) {
  227. // //print_a(['BuildStatisticMenu',$menuYear]);
  228. // $userName = e107::getUser()->getLoginName();
  229. // $sql->gen("SELECT `timekeeper_subject`,`timekeeper_percent`,`timekeeper_quantity` FROM e107_timekeeperStats WHERE `timekeeper_yearLevel`=$menuYear AND `timekeeper_username`='$userName';");
  230. // $text = "<h4>Hello $userName</h4>"
  231. // . "You need to score at least 90% and answer at lest 1000 questions in each subject before you can progress to a higher level.</br>"
  232. // . "Your current level is <b>$menuYear</b></br>.";
  233. // while ($row = $sql->fetch()) {
  234. // $text .= 'For the subject ' . $row['timekeeper_subject'] . ' you have scored ' . $row['timekeeper_percent'] . '% and have completed ' . $row['timekeeper_quantity'] . ' questions.';
  235. // }
  236. // //$text .= $sql->getLastErrorText();
  237. // e107::getRender()->tablerender('Score', $text);
  238. // }
  239. // private function UpdateYearAndStats($sql, $subjectToProcess) {
  240. // $query = ['timekeeper_percent' => $this->getPercent($subjectToProcess['rows']), 'timekeeper_quantity' => count($subjectToProcess['rows'])];
  241. //
  242. // $selectQuery = '`timekeeper_username` = \'' . $subjectToProcess['userName'] . '\' AND `timekeeper_subject` = \'' . $subjectToProcess['subject'] . '\' AND ' . '`timekeeper_yearLevel`=\'' . $subjectToProcess['yearlevel'] . '\'';
  243. // $updateQuery = array_merge($query, ['WHERE' => '`timekeeper_username`=\'' . $subjectToProcess['userName'] . '\' AND `timekeeper_subject`=\'' . $subjectToProcess['subject'] . '\' AND ' . '`timekeeper_yearLevel`=' . $subjectToProcess['yearlevel']]);
  244. // $insertQuery = array_merge($query, ['timekeeper_username' => $subjectToProcess['userName'], 'timekeeper_subject' => $subjectToProcess['subject'], 'timekeeper_yearLevel' => $subjectToProcess['yearlevel']]);
  245. //
  246. // if ($sql->retrieve('timekeeperStats', 'timekeeper_id', $selectQuery) === NULL) {
  247. // $sql->insert('timekeeperStats', $insertQuery);
  248. // } else {
  249. // $sql->update('timekeeperStats', $updateQuery);
  250. // }
  251. // //print_a($updateQuery);
  252. // if (count($subjectToProcess['rows']) >= 1000 && (int) $this->getPercent($subjectToProcess['rows']) >= 92) {
  253. // $insertNewYear = ['timekeeper_percent' => 100, 'timekeeper_quantity' => 0, 'timekeeper_username' => $subjectToProcess['userName'], 'timekeeper_subject' => $subjectToProcess['subject'], 'timekeeper_yearLevel' => ((int) $subjectToProcess['yearlevel'] + 1)];
  254. // $sql->insert('timekeeperStats', $insertNewYear);
  255. // $ns = e107::getRender();
  256. // $ns->tablerender("Congratulations", 'Congratulations ' . e107::getUser()->getRealName() . ' you have moved up a year level for the subject ' . $subjectToProcess['subject'] . '.');
  257. // }
  258. // }
  259. ////////////////////////////////////////////////////////////////////////////
  260. ////////////////////////////////////////////////////////////////////////////
  261. ////////////////////////////////////////////////////////////////////////////
  262. //
  263. // Functions beyond here are working and don't need anymore work
  264. //
  265. ////////////////////////////////////////////////////////////////////////////
  266. ////////////////////////////////////////////////////////////////////////////
  267. // private function getPercent($subjectToProcess) {
  268. // $questions = 0;
  269. // $correct = 0;
  270. // foreach ($subjectToProcess as $dbCorrect) {
  271. // $questions++;
  272. // if ($dbCorrect['timekeeper_correct'] == 1) {
  273. // $correct++;
  274. // }
  275. // if ($questions >= 1000) {
  276. // break;
  277. // }
  278. // }
  279. // //prevent devision by zero
  280. // if (!$questions == 0) {
  281. // return (100 - ((($questions - $correct) / $questions) * 100));
  282. // } else {
  283. // return 0;
  284. // }
  285. // }
  286. }