e_cron.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * e107 website system
  4. *
  5. * Copyright (C) 2008-2016 e107 Inc (e107.org)
  6. * Released under the terms and conditions of the
  7. * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
  8. *
  9. */
  10. class timekeeper_cron {
  11. function config() { // Setup
  12. $cron = array();
  13. $cron[] = array(
  14. 'name' => "Send Reports", // Displayed in admin area. .
  15. 'function' => "sendReportsEmail", // Name of the function which is defined below.
  16. 'category' => 'mail', // Choose between: mail, user, content, notify, or backup
  17. 'description' => "Send Timekeeper reports out to receivers." // Displayed in admin area.
  18. );
  19. return $cron;
  20. }
  21. function sendReportsEmail() {
  22. //$adminEmail = e107::getPref('siteadminemail');
  23. //$adminName = e107::getPref('siteadmin');
  24. require_once(e_HANDLER . "mail.php");
  25. $mailContent = $this->generateEmail('elliot');
  26. $eml = array(
  27. 'subject' => $mailContent['subject'],
  28. 'body' => $mailContent['message'],
  29. 'sender_email'=>'service@pfip.au'
  30. );
  31. foreach([$mailContent['recipients'],'elliot@pfip.au'] as $recipient)
  32. {
  33. e107::getEmail()->sendEmail($recipient, 'Gregory', $eml);
  34. }
  35. //sendemail($mailContent['recipients'], $mailContent['subject'], $mailContent['message']);
  36. $mailContent = $this->generateEmail('silas');
  37. $eml = array(
  38. 'subject' => $mailContent['subject'],
  39. 'body' => $mailContent['message'],
  40. 'sender_email'=>'service@pfip.au'
  41. );
  42. foreach([$mailContent['recipients'],'silas@pfip.au'] as $recipient)
  43. {
  44. e107::getEmail()->sendEmail($recipient, 'Gregory', $eml);
  45. }
  46. //sendemail($mailContent['recipients'], $mailContent['subject'], $mailContent['message']);
  47. }
  48. public function generateEmail($user) {
  49. //
  50. //Get recipients and users here
  51. //
  52. $returnArray['subject'] = 'TimeKeeper Report for '.$user;
  53. //$returnArray['recipients'] = 'pigs.fly.in.packs@gmail.com';
  54. $returnArray['recipients'] = 'gregory@pfip.au';
  55. $sql = e107::getDb();
  56. $coverPageData = $this->coverPage($sql, $user);
  57. $questionsPageData = $this->questionsTable($sql, $user);
  58. $returnArray['message'] = $this->template($coverPageData,$questionsPageData);
  59. $sql->close();
  60. return $returnArray;
  61. }
  62. private function coverPage($sql, $user) {
  63. $sql->gen("SELECT `subject`, COUNT(`correct`) as total, CAST((SUM(correct)/COUNT(id)*100) as DECIMAL(5,2)) as percent
  64. FROM e107.`e107_tkReport` WHERE FROM_UNIXTIME(`answereddatestamp`)>=DATE_ADD(CURDATE(), INTERVAL -1 DAY) AND `username` = '".$user."' GROUP BY (`subject`);");
  65. $Scores = [];
  66. $cntr = 0;
  67. $percentTotal = 0;
  68. while ($row = $sql->fetch()) {
  69. $Scores[] = ['subject'=>$row['subject'],'total'=>$row['total'],'percent'=>$row['percent']];
  70. $percentTotal = $percentTotal + $row['percent'];
  71. $cntr++;
  72. }
  73. if(is_numeric($percentTotal) && is_numeric($cntr) && $percentTotal != 0 && $cntr != 0)
  74. {
  75. $returnPercentTotal = $percentTotal/$cntr;
  76. }else{
  77. $returnPercentTotal = 0;
  78. }
  79. return ['user'=>$user,'TotalPercent'=>$returnPercentTotal,'Scores'=>$Scores];
  80. }
  81. private function questionsTable($sql, $user) {
  82. $sql->gen("SELECT `question`,`answereddatestamp`,`userAnswer`,`correct`,`subject` FROM e107.e107_tkReport WHERE username='$user'
  83. AND FROM_UNIXTIME(`answereddatestamp`)>=DATE_ADD(CURDATE(), INTERVAL -1 DAY) ORDER BY subject;");
  84. $questionsData['questions'] = [];
  85. $startTime = 9999999999;
  86. $endTime = 0;
  87. while ($row = $sql->fetch()) {
  88. if(!isset($questionsData['questions'][$row["subject"]])) {
  89. $questionsData['questions'][$row["subject"]] = [];
  90. }
  91. $questionsData['questions'][$row["subject"]][] = ['question'=>$row["question"],'userAnswer'=>$row["userAnswer"],'correct'=>$row["correct"]];
  92. if($row["answereddatestamp"]<=$startTime) {
  93. $startTime = $row["answereddatestamp"];
  94. }
  95. if($row["answereddatestamp"]>=$endTime) {
  96. $endTime = $row["answereddatestamp"];
  97. }
  98. }
  99. $questionsData['startTime'] = $startTime;
  100. $questionsData['endTime'] = $endTime;
  101. return $questionsData;
  102. }
  103. private function correctOrNot($state) {
  104. if ($state) {
  105. return '<img alt="Correct" src="' . e_IMAGE . 'admin_images/true_16.png"/>';
  106. } else {
  107. return '<img alt="Incorrect" src="' . e_IMAGE . 'admin_images/false_16.png"/>';
  108. }
  109. }
  110. private function template($coverPageData,$questionsPageData) {
  111. $emailTemplate = '<!DOCTYPE html>
  112. <html>
  113. <head>
  114. <style>
  115. body {
  116. background-color: #1a1a1a;
  117. color: #ffffff;
  118. font-family: Arial, sans-serif;
  119. }
  120. #sum {
  121. font-size: 32px;
  122. text-align: center;
  123. color: #ff9900;
  124. }
  125. #styTi {
  126. font-size: 14px;
  127. text-align: center;
  128. color: #cccccc;
  129. }
  130. #coverP, #questions {
  131. font-family: Arial, sans-serif;
  132. border-collapse: collapse;
  133. width: 100%;
  134. margin-top: 20px;
  135. }
  136. #coverP td, #coverP th, #questions td, #questions th {
  137. border: 1px solid #444;
  138. padding: 12px;
  139. text-align: left;
  140. }
  141. #coverP tr:nth-child(even), #questions tr:nth-child(even) {
  142. background-color: #2a2a2a;
  143. }
  144. #coverP tr:hover, #questions tr:hover {
  145. background-color: #333;
  146. }
  147. #coverP th, #questions th {
  148. padding-top: 12px;
  149. padding-bottom: 12px;
  150. text-align: left;
  151. background-color: #ff9900;
  152. color: #1a1a1a;
  153. font-weight: bold;
  154. }
  155. #coverP td, #questions td {
  156. color: #ffffff;
  157. }
  158. </style>
  159. </head>
  160. <body>
  161. <div id="sum">'.ucfirst($coverPageData["user"]).'\'s overall score for yesterday is '.$coverPageData["TotalPercent"].'%.</div>
  162. <div id="styTi">'.round(abs($questionsPageData["endTime"] - $questionsPageData["startTime"]) / 60, 2)." minutes were spent studying today.".'</div>
  163. <br/>
  164. <table id="coverP">
  165. <tr>
  166. <th>Subject</th>
  167. <th>Count</th>
  168. <th>Percentage</th>
  169. </tr>';
  170. foreach ($coverPageData["Scores"] as $Score) {
  171. $emailTemplate .= '
  172. <tr>
  173. <td>'.ucfirst($Score["subject"]).'</td>
  174. <td>'.$Score["total"].'</td>
  175. <td>'.$Score["percent"].'%</td>
  176. </tr>';
  177. }
  178. $emailTemplate .= '
  179. </table>
  180. <br/>
  181. <table id="questions">
  182. <tr>
  183. <th>Question</th>
  184. <th>Answered</th>
  185. <th>Correct?</th>
  186. </tr>';
  187. $subjectNow = '';
  188. foreach ($questionsPageData["questions"] as $key => $questionSub) {
  189. foreach ($questionSub as $question) {
  190. if($subjectNow == '' || $subjectNow != $key) {
  191. $subjectNow = $key;
  192. $emailTemplate .= '
  193. <tr>
  194. <td colspan="3" style="text-align: center; font-size: 16px; background-color: #333; color: #ff9900;">'.ucfirst($subjectNow).'</td>
  195. </tr>';
  196. }
  197. $emailTemplate .= '
  198. <tr>
  199. <td>'.$question["question"].'</td>
  200. <td>'.$question["userAnswer"].'</td>
  201. <td>'.$this->correctOrNot($question["correct"]).'</td>
  202. </tr>';
  203. }
  204. }
  205. $emailTemplate .= '
  206. </table>
  207. </body>
  208. </html>';
  209. return $emailTemplate;
  210. }
  211. }