PreHeaderFunctions.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. /**
  8. * Description of ExcelFunctions
  9. *
  10. * @author gregory
  11. */
  12. class ExcelFunctions {
  13. public function GeneratePdf() {
  14. $user = filter_input(INPUT_GET, 'user');
  15. if (!is_string($user) || !preg_match('/^[A-Za-z0-9_-]{1,50}$/', $user)) {
  16. return;
  17. } // before any query
  18. $whereDate = $this->whereDate();
  19. if ($user == "") {
  20. return;
  21. }
  22. $text = '<h1>TimeKeeper Report</h1>';
  23. $sql = e107::getDb();
  24. // $text .= $this->coverPage($sql,$user,$whereDate);
  25. $text .= $this->questionsTable($sql, $user, $whereDate);
  26. $text .= "\nMemory use: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MiB.\nGeneration time " . (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) . ' seconds.';
  27. $sql->close();
  28. $output = filter_input(INPUT_GET, 'output');
  29. if ($output == 'html') {
  30. echo $text;
  31. exit;
  32. } elseif ($output == 'pdf') {
  33. // pdf branch:
  34. $base = tempnam(sys_get_temp_dir(), 'tk');
  35. $html = $base . '.html';
  36. $pdf = $base . '.pdf';
  37. file_put_contents($html, $text);
  38. exec('wkhtmltopdf ' . escapeshellarg($html) . ' ' . escapeshellarg($pdf));
  39. header('Content-Type: application/pdf');
  40. header('Content-Length: ' . filesize($pdf));
  41. readfile($pdf);
  42. unlink($html);
  43. unlink($pdf);
  44. unlink($base);
  45. exit;
  46. }
  47. }
  48. private function coverPage($sql, $user, $whereDate) {
  49. if (false) {
  50. $sql->gen("SELECT `subject`,
  51. ANY_VALUE(`percent`) as percent, ANY_VALUE(`quantity`) as quantity
  52. FROM e107.`e107_tkStats` WHERE `username` = '$user' GROUP BY (`subject`);");
  53. $text = '<table border="1" cellspacing="3" cellpadding="2">'
  54. . '<tr><td style="text-align:left">Subject</td><td>Score</td><td>Questions Remaining</td></tr>';
  55. while ($row = $sql->fetch()) {
  56. $text .= '<tr><td style="text-align:left">' . ucfirst($row['subject']) . '</td><td>' . $row['percent'] . '%</td><td>' . (1000 - $row['quantity']) . '</td></tr>';
  57. }
  58. } else {
  59. $sql->gen("SELECT COUNT(_id) as cntr, CAST((SUM(correct)/COUNT(id)*100) as DECIMAL(5,2)) as percent
  60. FROM e107.`e107_tkReport` WHERE username = '$user'
  61. $whereDate");
  62. while ($row = $sql->fetch()) {
  63. if (filter_input(INPUT_GET, 'range') == "" || filter_input(INPUT_GET, 'range') == "today") {
  64. $text .= '<p>' . $row['cntr'] . ' question(s) have been answered for today with ' . $row['percent'] . '% accuracy.</p>';
  65. } else {
  66. $text .= '<p>' . $row['cntr'] . ' question(s) have been answered for this ' . filter_input(INPUT_GET, 'range') . ' with ' . $row['percent'] . '% accuracy.</p>';
  67. }
  68. }
  69. }
  70. $text .= '</table>';
  71. return $text;
  72. }
  73. private function questionsTable($sql, $user, $whereDate) {
  74. $sql->gen("SELECT * FROM e107.e107_tkReport WHERE username='$user' $whereDate ORDER BY subject;");
  75. $head = "";
  76. $text = "";
  77. while ($row = $sql->fetch()) {
  78. if ($head == "") {
  79. $head = $row["subject"];
  80. $text .= '<h3>' . ucfirst($head) . '</h3>'
  81. . '<table border="1" cellspacing="3" cellpadding="2">'
  82. . '<tr>'
  83. . '<td width="1295">Question</td>'
  84. . '<td width="75">User Answer</td>'
  85. . '<td width="75">Correct Answer</td>'
  86. . '<td width="25"></td>'
  87. . '</tr>';
  88. } elseif ($head != $row["subject"]) {
  89. $head = $row["subject"];
  90. $text .= '</table><h3>' . ucfirst($head) . '</h3><table border="1" cellspacing="3" cellpadding="2"><tr><td width="1295">Question</td><td width="75">User Answer</td><td width="75">Correct Answer</td><td width="25"></td></tr>';
  91. }
  92. $text .= "<tr><td>" . $row["question"] . '</td><td>' . $row["userAnswer"] . '</td><td>' . $row["answer"] . '</td><td>' . $this->correctOrNot($row["correct"]) . '</td></tr>';
  93. }
  94. $text .= '</table>';
  95. return $text;
  96. }
  97. private function whereDate() {
  98. $returnRange = filter_input(INPUT_GET, 'range');
  99. $unixTimeTodayEnd = mktime(23, 59, 59, date('n'), date('j'));
  100. $returnValues = ['today' => ['start' => strtotime('- 24 hours', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd]
  101. , 'yesterday' => ['start' => strtotime('- 48 hours', $unixTimeTodayEnd), 'end' => strtotime('- 24 hours', $unixTimeTodayEnd)]
  102. , 'pastWeek' => ['start' => strtotime('- 7 days', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd]
  103. , 'pastMonth' => ['start' => strtotime('- 1 month', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd]
  104. , 'pastQuarter' => ['start' => strtotime('- 3 month', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd]
  105. , 'pastYear' => ['start' => strtotime('- 1 year', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd]
  106. ];
  107. if (!isset($returnValues[$returnRange])) {
  108. $returnRange = 'today';
  109. }
  110. return "AND answereddatestamp>='" . $returnValues[$returnRange]['start'] . "' AND answereddatestamp<='" . $returnValues[$returnRange]['end'] . "'";
  111. }
  112. private function correctOrNot($state) {
  113. if ($state) {
  114. return '<img src="' . e_IMAGE . 'admin_images/true_16.png"/>';
  115. } else {
  116. return '<img src="' . e_IMAGE . 'admin_images/false_16.png"/>';
  117. }
  118. }
  119. public function GenerateExcel() {
  120. require_once('phar://lib/PHPExcel.phar/PHPExcel.php');
  121. $objPHPExcel = new PHPExcel();
  122. $objPHPExcel->getProperties()->setCreator("Gregory J Huxley")->setLastModifiedBy("TimeKeeper");
  123. $this->buildSheet($objPHPExcel);
  124. $this->setHeader();
  125. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  126. $objWriter->setPreCalculateFormulas(true);
  127. $objWriter->save('php://output');
  128. exit;
  129. }
  130. private function buildSheet($objPHPExcel) {
  131. $numberOfQuestions = filter_input(INPUT_POST, 'numberOfQuestions');
  132. if (!$numberOfQuestions) {
  133. $numberOfQuestions = 5;
  134. }
  135. $objPHPExcel->getActiveSheet()->setTitle("Questions");
  136. for ($i = 0; $i <= $numberOfQuestions; $i++) {
  137. $counter = 0;
  138. foreach ($this->buildQuestion($i) as $value) {
  139. $objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($counter, $i, $value);
  140. $counter++;
  141. }
  142. }
  143. }
  144. 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
  145. while (true) {
  146. switch (rand(1, 4)) {
  147. case 1:// addition
  148. $enabledSubject = filter_has_var(INPUT_POST, 'addition__switch');
  149. if ($enabledSubject) {
  150. return $this->addition($i);
  151. }
  152. break;
  153. case 2:// subtraction
  154. $enabledSubject = filter_has_var(INPUT_POST, 'subtraction__switch');
  155. if ($enabledSubject) {
  156. return $this->subtraction($i);
  157. }
  158. break;
  159. case 3:// multiplication
  160. $enabledSubject = filter_has_var(INPUT_POST, 'multiplication__switch');
  161. if ($enabledSubject) {
  162. return $this->multiplication($i);
  163. }
  164. break;
  165. case 4:// division
  166. $enabledSubject = filter_has_var(INPUT_POST, 'division__switch');
  167. if ($enabledSubject) {
  168. return $this->division($i);
  169. }
  170. break;
  171. }
  172. }
  173. }
  174. private function addition($i) {
  175. $question = [rand(1, 1000)];
  176. for ($int = 1; $int <= rand(1, 3); $int++) {
  177. array_push($question, '+');
  178. array_push($question, rand(1, 1000));
  179. }
  180. array_push($question, '=');
  181. array_push($question, $this->checkFormula($i, $question));
  182. return $question;
  183. }
  184. private function subtraction($i) {
  185. $question = [rand(100, 1000)];
  186. for ($int = 1; $int <= rand(1, 2); $int++) {
  187. array_push($question, '-');
  188. array_push($question, rand(1, $question[0] / 2));
  189. }
  190. array_push($question, '=');
  191. array_push($question, $this->checkFormula($i, $question));
  192. return $question;
  193. }
  194. private function multiplication($i) {
  195. $startNumber = [rand(1, 99)];
  196. $question = $startNumber;
  197. for ($int = 1; $int <= rand(1, 1); $int++) {
  198. array_push($question, '*');
  199. array_push($question, rand(1, $startNumber));
  200. }
  201. array_push($question, '=');
  202. array_push($question, $this->checkFormula($i, $question));
  203. return $question;
  204. }
  205. private function division($i) {
  206. $startNumber = rand(1, 50);
  207. $endNumber = rand(1, 5);
  208. $question = [$startNumber * $endNumber];
  209. array_push($question, '/');
  210. array_push($question, $endNumber);
  211. array_push($question, '=');
  212. array_push($question, $this->checkFormula($i, $question));
  213. return $question;
  214. }
  215. private function checkFormula($i, &$question) {
  216. $letters = ['A', 'C', 'E', 'G', 'I'];
  217. for ($int = count($question); $int <= 8; $int++) {
  218. array_push($question, '');
  219. }
  220. $formula = '(';
  221. $counter = 0;
  222. foreach ($question as &$value) {
  223. if ($value == '+' || $value == '-' || $value == '*' || $value == '/') {
  224. $formula .= $value;
  225. } elseif ($value == '=') {
  226. $formula .= ')=' . $letters[$counter] . $i;
  227. break;
  228. } else {
  229. $formula .= $letters[$counter] . $i;
  230. $counter++;
  231. }
  232. }
  233. return '=IF(' . substr(strrchr($formula, "="), 1) . '="","",IF(' . $formula . ',"Correct","Wrong"))'; // =IF(L1="","",IF((A1*C1*E1*H1*J1)=L1,"Correct","Wrong"))
  234. }
  235. private function setHeader() {
  236. // Redirect output to a client’s web browser (Excel2007)
  237. header('Content-Type: application/vnd.ms-excel');
  238. header('Content-Disposition: attachment;filename="Questions.xls"');
  239. header('Cache-Control: max-age=0');
  240. // If you're serving to IE 9, then the following may be needed
  241. header('Cache-Control: max-age=1');
  242. // If you're serving to IE over SSL, then the following may be needed
  243. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  244. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  245. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  246. header('Pragma: public'); // HTTP/1.0
  247. }
  248. }
  249. /**
  250. * Description of Summary
  251. *
  252. * @author gregory
  253. */
  254. class Summary {
  255. public function GenerateSummary() {//base64_encode
  256. $user = filter_input(INPUT_GET, 'user');
  257. $whereDate = $this->whereDate();
  258. if ($user == "") {
  259. return;
  260. }
  261. $sql = e107::getDb();
  262. $text = '<body style="background-color:#31363b;"><div style="font-size:250%;color:#fcfcfc;">';
  263. $text .= $this->coverPage($sql, $user, $whereDate);
  264. //$text .= "\nMemory use: ".(memory_get_peak_usage(true)/1024/1024)." MiB.\nGeneration time " . (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]).' seconds.';
  265. //print_a($whereDate);
  266. $sql->close();
  267. echo $text . '</div></body>';
  268. //$pdf->makePDF([$text, 'e107site', 'Gregory', 'Timekeeper Report', 'Timekeeper Report', 'Timekeeper Report', e_PLUGIN.'/timekeeper/timekeeper.php']);
  269. exit;
  270. }
  271. private function coverPage($sql, $user, $whereDate) {
  272. if (false) {
  273. $sql->gen("SELECT MAX(`timekeeper_yearLevel`) as timekeeper_yearLevel, `timekeeper_subject`,
  274. ANY_VALUE(`timekeeper_percent`) as timekeeper_percent, ANY_VALUE(`timekeeper_quantity`) as timekeeper_quantity
  275. FROM e107.`e107_timekeeperStats` WHERE `timekeeper_username` = '$user' GROUP BY (`timekeeper_subject`);");
  276. $text = '<table border="1" cellspacing="3" cellpadding="2">'
  277. . '<tr><td style="text-align:left">Year</td><td style="text-align:left">Subject</td><td>Score</td><td>Questions Remaining</td></tr>';
  278. while ($row = $sql->fetch()) {
  279. $text .= '<tr><td>' . $row['timekeeper_yearLevel'] . '</td><td style="text-align:left">' . ucfirst($row['timekeeper_subject']) . '</td><td>' . $row['timekeeper_percent'] . '%</td><td>' . (1000 - $row['timekeeper_quantity']) . '</td></tr>';
  280. }
  281. } else {
  282. $sql->gen("SELECT COUNT(id) as cntr, CAST((SUM(correct)/COUNT(id)*100) as DECIMAL(5,2)) as percent
  283. FROM e107.`e107_tkReport` WHERE username = '$user'
  284. $whereDate");
  285. while ($row = $sql->fetch()) {
  286. if (filter_input(INPUT_GET, 'range') == "" || filter_input(INPUT_GET, 'range') == "today") {
  287. $text .= 'Questions ' . filter_input(INPUT_GET, 'range') . '<br/><br/>' . $row['cntr'] . '<br/>' . $row['percent'] . '% correct.';
  288. } else {
  289. $text .= 'Questions ' . filter_input(INPUT_GET, 'range') . ' ' . $row['cntr'] . '<br/><br/>' . $row['percent'] . '% correct.';
  290. }
  291. }
  292. }
  293. $text .= '</table>';
  294. return $text;
  295. }
  296. private function whereDate() {
  297. $returnRange = filter_input(INPUT_GET, 'range');
  298. $unixTimeTodayEnd = mktime(23, 59, 59, date('n'), date('j'));
  299. $returnValues = ['today' => ['start' => strtotime('- 24 hours', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd]
  300. , 'yesterday' => ['start' => strtotime('- 48 hours', $unixTimeTodayEnd), 'end' => strtotime('- 24 hours', $unixTimeTodayEnd)]
  301. , 'pastWeek' => ['start' => strtotime('- 7 days', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd]
  302. , 'pastMonth' => ['start' => strtotime('- 1 month', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd]
  303. , 'pastQuarter' => ['start' => strtotime('- 3 month', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd]
  304. , 'pastYear' => ['start' => strtotime('- 1 year', $unixTimeTodayEnd), 'end' => $unixTimeTodayEnd]
  305. ];
  306. if (!isset($returnValues[$returnRange])) {
  307. $returnRange = 'today';
  308. }
  309. return "AND answereddatestamp>='" . $returnValues[$returnRange]['start'] . "' AND answereddatestamp<='" . $returnValues[$returnRange]['end'] . "'";
  310. }
  311. }