whereDate(); if ($user == "") { return; } $text = '

TimeKeeper Report

'; $sql = e107::getDb(); // $text .= $this->coverPage($sql,$user,$whereDate); $text .= $this->questionsTable($sql, $user, $whereDate); $text .= "\nMemory use: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MiB.\nGeneration time " . (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) . ' seconds.'; file_put_contents('test.html', $text); $sql->close(); $stdOut = ''; $output = filter_input(INPUT_GET, 'output'); if ($output == 'html') { echo $text; exit; } elseif ($output == 'pdf') { if (!preg_match('/^[A-Za-z0-9_-]{1,50}$/', $user)) { return; } $html = tempnam(sys_get_temp_dir(), 'tk') . '.html'; $pdf = tempnam(sys_get_temp_dir(), 'tk') . '.pdf'; file_put_contents($html, $text); exec('wkhtmltopdf ' . escapeshellarg($html) . ' ' . escapeshellarg($pdf)); } $filename = "$user.pdf"; // Header content type header("Content-type: application/pdf"); header("Content-Length: " . filesize($filename)); // Send the file to the browser. readfile($filename); exit; } private function coverPage($sql, $user, $whereDate) { if (false) { $sql->gen("SELECT `subject`, ANY_VALUE(`percent`) as percent, ANY_VALUE(`quantity`) as quantity FROM e107.`e107_tkStats` WHERE `username` = '$user' GROUP BY (`subject`);"); $text = '' . ''; while ($row = $sql->fetch()) { $text .= ''; } } else { $sql->gen("SELECT COUNT(_id) as cntr, CAST((SUM(correct)/COUNT(id)*100) as DECIMAL(5,2)) as percent FROM e107.`e107_tkReport` WHERE username = '$user' $whereDate"); while ($row = $sql->fetch()) { if (filter_input(INPUT_GET, 'range') == "" || filter_input(INPUT_GET, 'range') == "today") { $text .= '

' . $row['cntr'] . ' question(s) have been answered for today with ' . $row['percent'] . '% accuracy.

'; } else { $text .= '

' . $row['cntr'] . ' question(s) have been answered for this ' . filter_input(INPUT_GET, 'range') . ' with ' . $row['percent'] . '% accuracy.

'; } } } $text .= '
SubjectScoreQuestions Remaining
' . ucfirst($row['subject']) . '' . $row['percent'] . '%' . (1000 - $row['quantity']) . '
'; return $text; } private function questionsTable($sql, $user, $whereDate) { $sql->gen("SELECT * FROM e107.e107_tkReport WHERE username='$user' $whereDate ORDER BY subject;"); $head = ""; $text = ""; while ($row = $sql->fetch()) { if ($head == "") { $head = $row["subject"]; $text .= '

' . ucfirst($head) . '

' . '' . '' . '' . '' . '' . '' . ''; } elseif ($head != $row["subject"]) { $head = $row["subject"]; $text .= '
QuestionUser AnswerCorrect Answer

' . ucfirst($head) . '

'; } $text .= "'; } $text .= '
QuestionUser AnswerCorrect Answer
" . $row["question"] . '' . $row["userAnswer"] . '' . $row["answer"] . '' . $this->correctOrNot($row["correct"]) . '
'; return $text; } private function whereDate() { $returnRange = filter_input(INPUT_GET, 'range'); $unixTimeTodayEnd = mktime(23, 59, 59, date('n'), date('j')); $returnValues = ['today' => ['start' => strtotime('- 24 hours', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd] , 'yesterday' => ['start' => strtotime('- 48 hours', $unixTimeTodayEnd), 'end' => strtotime('- 24 hours', $unixTimeTodayEnd)] , 'pastWeek' => ['start' => strtotime('- 7 days', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd] , 'pastMonth' => ['start' => strtotime('- 1 month', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd] , 'pastQuarter' => ['start' => strtotime('- 3 month', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd] , 'pastYear' => ['start' => strtotime('- 1 year', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd] ]; if (!isset($returnValues[$returnRange])) { $returnRange = 'today'; } return "AND answereddatestamp>='" . $returnValues[$returnRange]['start'] . "' AND answereddatestamp<='" . $returnValues[$returnRange]['end'] . "'"; } private function correctOrNot($state) { if ($state) { return ''; } else { return ''; } } public function GenerateExcel() { require_once('phar://lib/PHPExcel.phar/PHPExcel.php'); $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setCreator("Gregory J Huxley")->setLastModifiedBy("TimeKeeper"); $this->buildSheet($objPHPExcel); $this->setHeader(); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->setPreCalculateFormulas(true); $objWriter->save('php://output'); exit; } private function buildSheet($objPHPExcel) { $numberOfQuestions = filter_input(INPUT_POST, 'numberOfQuestions'); if (!$numberOfQuestions) { $numberOfQuestions = 5; } $objPHPExcel->getActiveSheet()->setTitle("Questions"); for ($i = 0; $i <= $numberOfQuestions; $i++) { $counter = 0; foreach ($this->buildQuestion($i) as $value) { $objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($counter, $i, $value); $counter++; } } } private function buildQuestion($i) {//http://localhost/e107_plugins/timekeeper/timekeeper.php?numberOfQuestions=20&addition=0&addition__switch=0&subtraction=0&multiplication=0&division=0&GetExcel=Generate+Sheet while (true) { switch (rand(1, 4)) { case 1:// addition $enabledSubject = filter_has_var(INPUT_POST, 'addition__switch'); if ($enabledSubject) { return $this->addition($i); } break; case 2:// subtraction $enabledSubject = filter_has_var(INPUT_POST, 'subtraction__switch'); if ($enabledSubject) { return $this->subtraction($i); } break; case 3:// multiplication $enabledSubject = filter_has_var(INPUT_POST, 'multiplication__switch'); if ($enabledSubject) { return $this->multiplication($i); } break; case 4:// division $enabledSubject = filter_has_var(INPUT_POST, 'division__switch'); if ($enabledSubject) { return $this->division($i); } break; } } } private function addition($i) { $question = [rand(1, 1000)]; for ($int = 1; $int <= rand(1, 3); $int++) { array_push($question, '+'); array_push($question, rand(1, 1000)); } array_push($question, '='); array_push($question, $this->checkFormula($i, $question)); return $question; } private function subtraction($i) { $question = [rand(100, 1000)]; for ($int = 1; $int <= rand(1, 2); $int++) { array_push($question, '-'); array_push($question, rand(1, $question[0] / 2)); } array_push($question, '='); array_push($question, $this->checkFormula($i, $question)); return $question; } private function multiplication($i) { $startNumber = [rand(1, 99)]; $question = $startNumber; for ($int = 1; $int <= rand(1, 1); $int++) { array_push($question, '*'); array_push($question, rand(1, $startNumber)); } array_push($question, '='); array_push($question, $this->checkFormula($i, $question)); return $question; } private function division($i) { $startNumber = rand(1, 50); $endNumber = rand(1, 5); $question = [$startNumber * $endNumber]; array_push($question, '/'); array_push($question, $endNumber); array_push($question, '='); array_push($question, $this->checkFormula($i, $question)); return $question; } private function checkFormula($i, &$question) { $letters = ['A', 'C', 'E', 'G', 'I']; for ($int = count($question); $int <= 8; $int++) { array_push($question, ''); } $formula = '('; $counter = 0; foreach ($question as &$value) { if ($value == '+' || $value == '-' || $value == '*' || $value == '/') { $formula .= $value; } elseif ($value == '=') { $formula .= ')=' . $letters[$counter] . $i; break; } else { $formula .= $letters[$counter] . $i; $counter++; } } return '=IF(' . substr(strrchr($formula, "="), 1) . '="","",IF(' . $formula . ',"Correct","Wrong"))'; // =IF(L1="","",IF((A1*C1*E1*H1*J1)=L1,"Correct","Wrong")) } private function setHeader() { // Redirect output to a client’s web browser (Excel2007) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="Questions.xls"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 } } /** * Description of Summary * * @author gregory */ class Summary { public function GenerateSummary() {//base64_encode $user = filter_input(INPUT_GET, 'user'); $whereDate = $this->whereDate(); if ($user == "") { return; } $sql = e107::getDb(); $text = '
'; $text .= $this->coverPage($sql, $user, $whereDate); //$text .= "\nMemory use: ".(memory_get_peak_usage(true)/1024/1024)." MiB.\nGeneration time " . (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]).' seconds.'; //print_a($whereDate); $sql->close(); echo $text . '
'; //$pdf->makePDF([$text, 'e107site', 'Gregory', 'Timekeeper Report', 'Timekeeper Report', 'Timekeeper Report', e_PLUGIN.'/timekeeper/timekeeper.php']); exit; } private function coverPage($sql, $user, $whereDate) { if (false) { $sql->gen("SELECT MAX(`timekeeper_yearLevel`) as timekeeper_yearLevel, `timekeeper_subject`, ANY_VALUE(`timekeeper_percent`) as timekeeper_percent, ANY_VALUE(`timekeeper_quantity`) as timekeeper_quantity FROM e107.`e107_timekeeperStats` WHERE `timekeeper_username` = '$user' GROUP BY (`timekeeper_subject`);"); $text = '' . ''; while ($row = $sql->fetch()) { $text .= ''; } } else { $sql->gen("SELECT COUNT(id) as cntr, CAST((SUM(correct)/COUNT(id)*100) as DECIMAL(5,2)) as percent FROM e107.`e107_tkReport` WHERE username = '$user' $whereDate"); while ($row = $sql->fetch()) { if (filter_input(INPUT_GET, 'range') == "" || filter_input(INPUT_GET, 'range') == "today") { $text .= 'Questions ' . filter_input(INPUT_GET, 'range') . '

' . $row['cntr'] . '
' . $row['percent'] . '% correct.'; } else { $text .= 'Questions ' . filter_input(INPUT_GET, 'range') . ' ' . $row['cntr'] . '

' . $row['percent'] . '% correct.'; } } } $text .= '
YearSubjectScoreQuestions Remaining
' . $row['timekeeper_yearLevel'] . '' . ucfirst($row['timekeeper_subject']) . '' . $row['timekeeper_percent'] . '%' . (1000 - $row['timekeeper_quantity']) . '
'; return $text; } private function whereDate() { $returnRange = filter_input(INPUT_GET, 'range'); $unixTimeTodayEnd = mktime(23, 59, 59, date('n'), date('j')); $returnValues = ['today' => ['start' => strtotime('- 24 hours', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd] , 'yesterday' => ['start' => strtotime('- 48 hours', $unixTimeTodayEnd), 'end' => strtotime('- 24 hours', $unixTimeTodayEnd)] , 'pastWeek' => ['start' => strtotime('- 7 days', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd] , 'pastMonth' => ['start' => strtotime('- 1 month', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd] , 'pastQuarter' => ['start' => strtotime('- 3 month', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd] , 'pastYear' => ['start' => strtotime('- 1 year', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd] ]; if (!isset($returnValues[$returnRange])) { $returnRange = 'today'; } return "AND answereddatestamp>='" . $returnValues[$returnRange]['start'] . "' AND answereddatestamp<='" . $returnValues[$returnRange]['end'] . "'"; } }