| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- /**
- *
- * @author gregory
- */
- class tk_Main {
- private $activeSubjectsList = [];
- function __construct() {
- //e107::js('timekeeper','js/my.js','jquery'); // Load Plugin javascript and include jQuery framework
- //e107::css('timekeeper','css/my.css'); // load css file
- //e107::lan('timekeeper'); // load language file ie. e107_plugins/timekeeper/languages/English.php
- }
- public function run() {
-
- //$sql = e107::getDB(); // mysql class object
- $tp = e107::getParser(); // parser for converting to HTML and parsing templates etc.
- $sc = e107::getScBatch('timekeeper', TRUE); // loads e107_plugins/myplugin/myplugin_shortcodes.php
- $ns = e107::getRender(); // render in theme box.
- $form = new FormGenerator();
-
- //load development area for main site admin only
- if (check_class("Main Admin")) {
- $ns->tablerender('Start of Development Area', '<br/>');
- require_once (e_PLUGIN . 'timekeeper/DevelopmentArea.php');
- $ns->tablerender('End of Development Area', '<br/>');
- }
-
- //Check user submitted answer
- $submitButton = filter_input(INPUT_POST, 'Answered');
- if (isset($submitButton)) {
- $AnswerChecker = new AnswerChecker();
- $text = $AnswerChecker->checkAnswers();
- $ns->tablerender("Check answer", $text . '<br/>');
- }
- /////////////////////////////////////////
- /// Student section
- /////////////////////////////////////////
- if (check_class("TIMEKEEPER_STUDENT")) {
- $this->MainEntry();
- $text = '';
- $subject = $this->subjectSelector();
- $year = $this->yearLevelSelector($subject);
- //Return random generated question
- $QG = new timekeeper_Question_Engine;
- $QG->subjectSelector = $subject;
- $QG->yearlevel = $year;
- $content = $QG->GetRandomQuestion();
- $text .= $content; //$this->renderQuestion($frm, $content);
- $ns->tablerender("Question", $text . '<br/>');
- }
- /////////////////////////////////////////
- /// Teacher section
- /////////////////////////////////////////
- if (check_class("TIMEKEEPER_TEACHER")) {
- //$ns->tablerender("ExcelGenerator", $form->ExcelGenerator());
- }
- }
- private function subjectRandomizer() {
- $subjects = [
- 'english' => 0,
- 'pePd' => 0, //health-and-physical-education
- 'socialStudies' => 0, //humanities-and-social-sciences
- 'languages' => 0,
- 'mathematics' => 0,
- 'science' => 0,
- 'technologies' => 0,
- 'art' => 0,
- 'general' => 0 // general knowlage
- ];
- foreach ($this->activeSubjectsList as $key => $sub) {
- $subjects[$key] = $sub['timekeeper_percent'] + rand(0, 200 / $sub['timekeeper_yearLevel']);
- }
- //print_a($subjects);
- //print_a($this->activeSubjectsList);
- return $subjects;
- }
- private function subjectSelector() {
- $returnSubject = '';
- $returnScore = 0;
- foreach ($this->subjectRandomizer() as $sub => $score) {
- if ($returnScore <= $score && $score <> 0) {
- $returnSubject = $sub;
- $returnScore = $score;
- }
- }
- //print_a($returnSubject);
- // Add maths as the starting subject for new users
- if ($returnSubject === '') {
- $returnSubject = 'mathematics';
- }
- return $returnSubject;
- }
- private function yearLevelSelector($subject) {
- return 1;
- }
- /**
- * Main entry point for checking and updating the statistics and year levels.
- *
- * Description needed once finished....TODO
- *
- */
- private function MainEntry() {
- $sql = e107::getDb();
- $userName = e107::getUser()->getLoginName();
- $this->GetActiveSubjects($sql, $userName);
- $this->GetActiveYearForSubjects($sql, $userName);
- $this->CheckSubjects($sql, $userName);
- }
- /**
- * Get a list of active subjects.
- *
- * Populate the private class variable $activeSubjectsList with an array of active subjects for use in statistics and question selection.
- *
- */
- private function GetActiveSubjects($sql, $userName) {
- $this->activeSubjectsList = [];
- $sql->gen("SELECT DISTINCT `timekeeper_subject` FROM e107.e107_timekeeperReport WHERE `timekeeper_username`='$userName';");
- while ($row = $sql->fetch()) {
- $this->activeSubjectsList = array_merge($this->activeSubjectsList, [$row['timekeeper_subject'] => []]);
- }
- }
- /**
- * Get the year level and percentage for the active subjects
- *
- * Populate the private class variable $activeSubjectsList with the current year level and correct percentage for the current subjects.
- *
- */
- private function GetActiveYearForSubjects($sql, $userName) {
- foreach ($this->activeSubjectsList as $subject => &$arrayHolder) {
- $selectQuery = '`timekeeper_username` = \'' . $userName . '\' AND `timekeeper_subject` = \'' . $subject . '\' ORDER BY `timekeeper_yearLevel` DESC';
- $arrayHolder = ($sql->retrieve('timekeeperStats', '`timekeeper_yearLevel`,`timekeeper_percent`', $selectQuery));
- }
- }
- /**
- * Check to see what subjects the user has answered questions for.
- *
- * 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'.
- *
- * @param int $yearlevel The year to check for records in the database.
- *
- */
- private function CheckSubjects($sql, $userName) {
- $activeSubjects = array();
- $subjectToProcess = array();
- foreach ($this->activeSubjectsList as $subject => $dataArrays) {
- $tmpArray = array();
- $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;");
- while ($row = $sql->fetch()) {
- array_push($tmpArray, $row);
- }
- $subjectToProcess = ['userName' => $userName, 'subject' => $subject, 'yearlevel' => $dataArrays['timekeeper_yearLevel'], 'rows' => $tmpArray];
- //print_a($subjectToProcess);
- $this->UpdateYearAndStats($sql, $subjectToProcess);
- }
- }
- private function BuildStatisticPdf($sql, $menuYear) {
- //print_a(['BuildStatisticMenu',$menuYear]);
- $userName = e107::getUser()->getLoginName();
- $sql->gen("SELECT `timekeeper_subject`,`timekeeper_percent`,`timekeeper_quantity` FROM e107_timekeeperStats WHERE `timekeeper_yearLevel`=$menuYear AND `timekeeper_username`='$userName';");
- $text = "<h4>Hello $userName</h4>"
- . "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>"
- . "Your current level is <b>$menuYear</b></br>.";
- while ($row = $sql->fetch()) {
- $text .= 'For the subject ' . $row['timekeeper_subject'] . ' you have scored ' . $row['timekeeper_percent'] . '% and have completed ' . $row['timekeeper_quantity'] . ' questions.';
- }
- //$text .= $sql->getLastErrorText();
- e107::getRender()->tablerender('Score', $text);
- }
- private function BuildStatisticPage($sql, $menuYear) {
- //print_a(['BuildStatisticMenu',$menuYear]);
- $userName = e107::getUser()->getLoginName();
- $sql->gen("SELECT `timekeeper_subject`,`timekeeper_percent`,`timekeeper_quantity` FROM e107_timekeeperStats WHERE `timekeeper_yearLevel`=$menuYear AND `timekeeper_username`='$userName';");
- $text = "<h4>Hello $userName</h4>"
- . "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>"
- . "Your current level is <b>$menuYear</b></br>.";
- while ($row = $sql->fetch()) {
- $text .= 'For the subject ' . $row['timekeeper_subject'] . ' you have scored ' . $row['timekeeper_percent'] . '% and have completed ' . $row['timekeeper_quantity'] . ' questions.';
- }
- //$text .= $sql->getLastErrorText();
- e107::getRender()->tablerender('Score', $text);
- }
- private function UpdateYearAndStats($sql, $subjectToProcess) {
- $query = ['timekeeper_percent' => $this->getPercent($subjectToProcess['rows']), 'timekeeper_quantity' => count($subjectToProcess['rows'])];
- $selectQuery = '`timekeeper_username` = \'' . $subjectToProcess['userName'] . '\' AND `timekeeper_subject` = \'' . $subjectToProcess['subject'] . '\' AND ' . '`timekeeper_yearLevel`=\'' . $subjectToProcess['yearlevel'] . '\'';
- $updateQuery = array_merge($query, ['WHERE' => '`timekeeper_username`=\'' . $subjectToProcess['userName'] . '\' AND `timekeeper_subject`=\'' . $subjectToProcess['subject'] . '\' AND ' . '`timekeeper_yearLevel`=' . $subjectToProcess['yearlevel']]);
- $insertQuery = array_merge($query, ['timekeeper_username' => $subjectToProcess['userName'], 'timekeeper_subject' => $subjectToProcess['subject'], 'timekeeper_yearLevel' => $subjectToProcess['yearlevel']]);
- if ($sql->retrieve('timekeeperStats', 'timekeeper_id', $selectQuery) === NULL) {
- $sql->insert('timekeeperStats', $insertQuery);
- } else {
- $sql->update('timekeeperStats', $updateQuery);
- }
- //print_a($updateQuery);
- if (count($subjectToProcess['rows']) >= 1000 && (int) $this->getPercent($subjectToProcess['rows']) >= 92) {
- $insertNewYear = ['timekeeper_percent' => 100, 'timekeeper_quantity' => 0, 'timekeeper_username' => $subjectToProcess['userName'], 'timekeeper_subject' => $subjectToProcess['subject'], 'timekeeper_yearLevel' => ((int) $subjectToProcess['yearlevel'] + 1)];
- $sql->insert('timekeeperStats', $insertNewYear);
- $ns = e107::getRender();
- $ns->tablerender("Congratulations", 'Congratulations ' . e107::getUser()->getRealName() . ' you have moved up a year level for the subject ' . $subjectToProcess['subject'] . '.');
- }
- }
- ////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////
- //
- // Functions beyond here are working and don't need anymore work
- //
- ////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////
- /**
- * Build a table and display it for the statistics menu of Timekeeper.
- *
- *
- *
- * @return void()
- */
- public function BuildStatisticMenu() {
- $sql = e107::getDb();
- $userName = e107::getUser()->getLoginName();
- $sql->gen("SELECT MAX(`timekeeper_yearLevel`) as timekeeper_yearLevel, `timekeeper_subject`,
- (`timekeeper_percent`) as timekeeper_percent, (`timekeeper_quantity`) as timekeeper_quantity
- FROM e107.`e107_timekeeperStats` WHERE `timekeeper_username` = '$userName' GROUP BY (`timekeeper_subject`);");
- $text = "<table style='width:100%;text-align:center;'>"
- . "<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>"
- . "<tbody>";
- while ($row = $sql->fetch()) {
- $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>';
- }
- $sql->gen("SELECT COUNT(timekeeper_id) as cntr, CAST((SUM(timekeeper_correct)/COUNT(timekeeper_id)*100) as DECIMAL(5,2)) as percent
- FROM e107.`e107_timekeeperReport` WHERE timekeeper_username = '$userName'
- AND timekeeper_answereddatestamp >= '" . mktime(0, 0, 0, date('n'), date('j')) . "'
- AND timekeeper_answereddatestamp <= '" . mktime(23, 59, 59, date('n'), date('j')) . "'");
- while ($row = $sql->fetch()) {
- $text .= '</tbody></table><p>' . $row['cntr'] . ' questions have been answered<br/>today with ' . $row['percent'] . '% accuracy.<p>';
- }
- e107::getRender()->tablerender('Score', $text);
- }
- private function getPercent($subjectToProcess) {
- $questions = 0;
- $correct = 0;
- foreach ($subjectToProcess as $dbCorrect) {
- $questions++;
- if ($dbCorrect['timekeeper_correct'] == 1) {
- $correct++;
- }
- if ($questions >= 1000) {
- break;
- }
- }
- //prevent devision by zero
- if (!$questions == 0) {
- return (100 - ((($questions - $correct) / $questions) * 100));
- } else {
- return 0;
- }
- }
- }
|