PreHeaderFunctions.php 15 KB

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