tk_Main.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. *
  4. * @author gregory
  5. */
  6. class tk_Main {
  7. private $activeSubjectsList = [];
  8. function __construct() {
  9. //e107::js('timekeeper','js/my.js','jquery'); // Load Plugin javascript and include jQuery framework
  10. //e107::css('timekeeper','css/my.css'); // load css file
  11. //e107::lan('timekeeper'); // load language file ie. e107_plugins/timekeeper/languages/English.php
  12. }
  13. public function run() {
  14. //$sql = e107::getDB(); // mysql class object
  15. $tp = e107::getParser(); // parser for converting to HTML and parsing templates etc.
  16. $sc = e107::getScBatch('timekeeper', TRUE); // loads e107_plugins/myplugin/myplugin_shortcodes.php
  17. $ns = e107::getRender(); // render in theme box.
  18. $form = new FormGenerator();
  19. //load development area for main site admin only
  20. if (check_class("Main Admin")) {
  21. $ns->tablerender('Start of Development Area', '<br/>');
  22. require_once (e_PLUGIN . 'timekeeper/DevelopmentArea.php');
  23. $ns->tablerender('End of Development Area', '<br/>');
  24. }
  25. //Check user submitted answer
  26. $submitButton = filter_input(INPUT_POST, 'Answered');
  27. if (isset($submitButton)) {
  28. $AnswerChecker = new AnswerChecker();
  29. $text = $AnswerChecker->checkAnswers();
  30. $ns->tablerender("Check answer", $text . '<br/>');
  31. }
  32. /////////////////////////////////////////
  33. /// Student section
  34. /////////////////////////////////////////
  35. if (check_class("TIMEKEEPER_STUDENT")) {
  36. $this->MainEntry();
  37. $text = '';
  38. $subject = $this->subjectSelector();
  39. $year = $this->yearLevelSelector($subject);
  40. //Return random generated question
  41. $QG = new timekeeper_Question_Engine;
  42. $QG->subjectSelector = $subject;
  43. $QG->yearlevel = $year;
  44. $content = $QG->GetRandomQuestion();
  45. $text .= $content; //$this->renderQuestion($frm, $content);
  46. $ns->tablerender("Question", $text . '<br/>');
  47. }
  48. /////////////////////////////////////////
  49. /// Teacher section
  50. /////////////////////////////////////////
  51. if (check_class("TIMEKEEPER_TEACHER")) {
  52. //$ns->tablerender("ExcelGenerator", $form->ExcelGenerator());
  53. }
  54. }
  55. private function subjectRandomizer() {
  56. $subjects = [
  57. 'english' => 0,
  58. 'pePd' => 0, //health-and-physical-education
  59. 'socialStudies' => 0, //humanities-and-social-sciences
  60. 'languages' => 0,
  61. 'mathematics' => 0,
  62. 'science' => 0,
  63. 'technologies' => 0,
  64. 'art' => 0,
  65. 'general' => 0 // general knowlage
  66. ];
  67. foreach ($this->activeSubjectsList as $key => $sub) {
  68. $subjects[$key] = $sub['timekeeper_percent'] + rand(0, 200 / $sub['timekeeper_yearLevel']);
  69. }
  70. //print_a($subjects);
  71. //print_a($this->activeSubjectsList);
  72. return $subjects;
  73. }
  74. private function subjectSelector() {
  75. $returnSubject = '';
  76. $returnScore = 0;
  77. foreach ($this->subjectRandomizer() as $sub => $score) {
  78. if ($returnScore <= $score && $score <> 0) {
  79. $returnSubject = $sub;
  80. $returnScore = $score;
  81. }
  82. }
  83. //print_a($returnSubject);
  84. // Add maths as the starting subject for new users
  85. if ($returnSubject === '') {
  86. $returnSubject = 'mathematics';
  87. }
  88. return $returnSubject;
  89. }
  90. private function yearLevelSelector($subject) {
  91. return 1;
  92. }
  93. /**
  94. * Main entry point for checking and updating the statistics and year levels.
  95. *
  96. * Description needed once finished....TODO
  97. *
  98. */
  99. private function MainEntry() {
  100. $sql = e107::getDb();
  101. $userName = e107::getUser()->getLoginName();
  102. $this->GetActiveSubjects($sql, $userName);
  103. $this->GetActiveYearForSubjects($sql, $userName);
  104. $this->CheckSubjects($sql, $userName);
  105. }
  106. /**
  107. * Get a list of active subjects.
  108. *
  109. * Populate the private class variable $activeSubjectsList with an array of active subjects for use in statistics and question selection.
  110. *
  111. */
  112. private function GetActiveSubjects($sql, $userName) {
  113. $this->activeSubjectsList = [];
  114. $sql->gen("SELECT DISTINCT `timekeeper_subject` FROM e107.e107_timekeeperReport WHERE `timekeeper_username`='$userName';");
  115. while ($row = $sql->fetch()) {
  116. $this->activeSubjectsList = array_merge($this->activeSubjectsList, [$row['timekeeper_subject'] => []]);
  117. }
  118. }
  119. /**
  120. * Get the year level and percentage for the active subjects
  121. *
  122. * Populate the private class variable $activeSubjectsList with the current year level and correct percentage for the current subjects.
  123. *
  124. */
  125. private function GetActiveYearForSubjects($sql, $userName) {
  126. foreach ($this->activeSubjectsList as $subject => &$arrayHolder) {
  127. $selectQuery = '`timekeeper_username` = \'' . $userName . '\' AND `timekeeper_subject` = \'' . $subject . '\' ORDER BY `timekeeper_yearLevel` DESC';
  128. $arrayHolder = ($sql->retrieve('timekeeperStats', '`timekeeper_yearLevel`,`timekeeper_percent`', $selectQuery));
  129. }
  130. }
  131. /**
  132. * Check to see what subjects the user has answered questions for.
  133. *
  134. * 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'.
  135. *
  136. * @param int $yearlevel The year to check for records in the database.
  137. *
  138. */
  139. private function CheckSubjects($sql, $userName) {
  140. $activeSubjects = array();
  141. $subjectToProcess = array();
  142. foreach ($this->activeSubjectsList as $subject => $dataArrays) {
  143. $tmpArray = array();
  144. $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;");
  145. while ($row = $sql->fetch()) {
  146. array_push($tmpArray, $row);
  147. }
  148. $subjectToProcess = ['userName' => $userName, 'subject' => $subject, 'yearlevel' => $dataArrays['timekeeper_yearLevel'], 'rows' => $tmpArray];
  149. //print_a($subjectToProcess);
  150. $this->UpdateYearAndStats($sql, $subjectToProcess);
  151. }
  152. }
  153. private function BuildStatisticPdf($sql, $menuYear) {
  154. //print_a(['BuildStatisticMenu',$menuYear]);
  155. $userName = e107::getUser()->getLoginName();
  156. $sql->gen("SELECT `timekeeper_subject`,`timekeeper_percent`,`timekeeper_quantity` FROM e107_timekeeperStats WHERE `timekeeper_yearLevel`=$menuYear AND `timekeeper_username`='$userName';");
  157. $text = "<h4>Hello $userName</h4>"
  158. . "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>"
  159. . "Your current level is <b>$menuYear</b></br>.";
  160. while ($row = $sql->fetch()) {
  161. $text .= 'For the subject ' . $row['timekeeper_subject'] . ' you have scored ' . $row['timekeeper_percent'] . '% and have completed ' . $row['timekeeper_quantity'] . ' questions.';
  162. }
  163. //$text .= $sql->getLastErrorText();
  164. e107::getRender()->tablerender('Score', $text);
  165. }
  166. private function BuildStatisticPage($sql, $menuYear) {
  167. //print_a(['BuildStatisticMenu',$menuYear]);
  168. $userName = e107::getUser()->getLoginName();
  169. $sql->gen("SELECT `timekeeper_subject`,`timekeeper_percent`,`timekeeper_quantity` FROM e107_timekeeperStats WHERE `timekeeper_yearLevel`=$menuYear AND `timekeeper_username`='$userName';");
  170. $text = "<h4>Hello $userName</h4>"
  171. . "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>"
  172. . "Your current level is <b>$menuYear</b></br>.";
  173. while ($row = $sql->fetch()) {
  174. $text .= 'For the subject ' . $row['timekeeper_subject'] . ' you have scored ' . $row['timekeeper_percent'] . '% and have completed ' . $row['timekeeper_quantity'] . ' questions.';
  175. }
  176. //$text .= $sql->getLastErrorText();
  177. e107::getRender()->tablerender('Score', $text);
  178. }
  179. private function UpdateYearAndStats($sql, $subjectToProcess) {
  180. $query = ['timekeeper_percent' => $this->getPercent($subjectToProcess['rows']), 'timekeeper_quantity' => count($subjectToProcess['rows'])];
  181. $selectQuery = '`timekeeper_username` = \'' . $subjectToProcess['userName'] . '\' AND `timekeeper_subject` = \'' . $subjectToProcess['subject'] . '\' AND ' . '`timekeeper_yearLevel`=\'' . $subjectToProcess['yearlevel'] . '\'';
  182. $updateQuery = array_merge($query, ['WHERE' => '`timekeeper_username`=\'' . $subjectToProcess['userName'] . '\' AND `timekeeper_subject`=\'' . $subjectToProcess['subject'] . '\' AND ' . '`timekeeper_yearLevel`=' . $subjectToProcess['yearlevel']]);
  183. $insertQuery = array_merge($query, ['timekeeper_username' => $subjectToProcess['userName'], 'timekeeper_subject' => $subjectToProcess['subject'], 'timekeeper_yearLevel' => $subjectToProcess['yearlevel']]);
  184. if ($sql->retrieve('timekeeperStats', 'timekeeper_id', $selectQuery) === NULL) {
  185. $sql->insert('timekeeperStats', $insertQuery);
  186. } else {
  187. $sql->update('timekeeperStats', $updateQuery);
  188. }
  189. //print_a($updateQuery);
  190. if (count($subjectToProcess['rows']) >= 1000 && (int) $this->getPercent($subjectToProcess['rows']) >= 92) {
  191. $insertNewYear = ['timekeeper_percent' => 100, 'timekeeper_quantity' => 0, 'timekeeper_username' => $subjectToProcess['userName'], 'timekeeper_subject' => $subjectToProcess['subject'], 'timekeeper_yearLevel' => ((int) $subjectToProcess['yearlevel'] + 1)];
  192. $sql->insert('timekeeperStats', $insertNewYear);
  193. $ns = e107::getRender();
  194. $ns->tablerender("Congratulations", 'Congratulations ' . e107::getUser()->getRealName() . ' you have moved up a year level for the subject ' . $subjectToProcess['subject'] . '.');
  195. }
  196. }
  197. ////////////////////////////////////////////////////////////////////////////
  198. ////////////////////////////////////////////////////////////////////////////
  199. ////////////////////////////////////////////////////////////////////////////
  200. //
  201. // Functions beyond here are working and don't need anymore work
  202. //
  203. ////////////////////////////////////////////////////////////////////////////
  204. ////////////////////////////////////////////////////////////////////////////
  205. /**
  206. * Build a table and display it for the statistics menu of Timekeeper.
  207. *
  208. *
  209. *
  210. * @return void()
  211. */
  212. public function BuildStatisticMenu() {
  213. $sql = e107::getDb();
  214. $userName = e107::getUser()->getLoginName();
  215. $sql->gen("SELECT MAX(`timekeeper_yearLevel`) as timekeeper_yearLevel, `timekeeper_subject`,
  216. (`timekeeper_percent`) as timekeeper_percent, (`timekeeper_quantity`) as timekeeper_quantity
  217. FROM e107.`e107_timekeeperStats` WHERE `timekeeper_username` = '$userName' GROUP BY (`timekeeper_subject`);");
  218. $text = "<table style='width:100%;text-align:center;'>"
  219. . "<thead><tr><td style='text-align:left'>Year</td><td style='text-align:left'>Subject</td><td>Score</td><td>No.Questions</td></tr></thead>"
  220. . "<tbody>";
  221. while ($row = $sql->fetch()) {
  222. $text .= '<tr><td>' . $row['timekeeper_yearLevel'] . '</td><td style=\'text-align:left\'>' . $row['timekeeper_subject'] . '</td><td>' . $row['timekeeper_percent'] . '%</td><td>' . $row['timekeeper_quantity'] . '</td></tr>';
  223. }
  224. $sql->gen("SELECT COUNT(timekeeper_id) as cntr, CAST((SUM(timekeeper_correct)/COUNT(timekeeper_id)*100) as DECIMAL(5,2)) as percent
  225. FROM e107.`e107_timekeeperReport` WHERE timekeeper_username = '$userName'
  226. AND timekeeper_answereddatestamp >= '" . mktime(0, 0, 0, date('n'), date('j')) . "'
  227. AND timekeeper_answereddatestamp <= '" . mktime(23, 59, 59, date('n'), date('j')) . "'");
  228. while ($row = $sql->fetch()) {
  229. $text .= '</tbody></table><p>' . $row['cntr'] . ' questions have been answered<br/>today with ' . $row['percent'] . '% accuracy.<p>';
  230. }
  231. e107::getRender()->tablerender('Score', $text);
  232. }
  233. private function getPercent($subjectToProcess) {
  234. $questions = 0;
  235. $correct = 0;
  236. foreach ($subjectToProcess as $dbCorrect) {
  237. $questions++;
  238. if ($dbCorrect['timekeeper_correct'] == 1) {
  239. $correct++;
  240. }
  241. if ($questions >= 1000) {
  242. break;
  243. }
  244. }
  245. //prevent devision by zero
  246. if (!$questions == 0) {
  247. return (100 - ((($questions - $correct) / $questions) * 100));
  248. } else {
  249. return 0;
  250. }
  251. }
  252. }