| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- /**
- * e107 website system
- *
- * Copyright (C) 2008-2016 e107 Inc (e107.org)
- * Released under the terms and conditions of the
- * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
- *
- */
- class timekeeper_cron {
- function config() { // Setup
- $cron = array();
- $cron[] = array(
- 'name' => "Send Reports", // Displayed in admin area. .
- 'function' => "sendReportsEmail", // Name of the function which is defined below.
- 'category' => 'mail', // Choose between: mail, user, content, notify, or backup
- 'description' => "Send Timekeeper reports out to receivers." // Displayed in admin area.
- );
- return $cron;
- }
- function sendReportsEmail() {
- //$adminEmail = e107::getPref('siteadminemail');
- //$adminName = e107::getPref('siteadmin');
- require_once(e_HANDLER . "mail.php");
-
- $mailContent = $this->generateEmail('elliot');
-
- $eml = array(
- 'subject' => $mailContent['subject'],
- 'body' => $mailContent['message'],
- 'sender_email'=>'service@pfip.au'
- );
-
-
- foreach([$mailContent['recipients'],'elliot@pfip.au'] as $recipient)
- {
- e107::getEmail()->sendEmail($recipient, 'Gregory', $eml);
- }
-
- //sendemail($mailContent['recipients'], $mailContent['subject'], $mailContent['message']);
-
- $mailContent = $this->generateEmail('silas');
-
- $eml = array(
- 'subject' => $mailContent['subject'],
- 'body' => $mailContent['message'],
- 'sender_email'=>'service@pfip.au'
- );
- foreach([$mailContent['recipients'],'silas@pfip.au'] as $recipient)
- {
- e107::getEmail()->sendEmail($recipient, 'Gregory', $eml);
- }
- //sendemail($mailContent['recipients'], $mailContent['subject'], $mailContent['message']);
-
- }
- public function generateEmail($user) {
- //
- //Get recipients and users here
- //
- $returnArray['subject'] = 'TimeKeeper Report for '.$user;
- //$returnArray['recipients'] = 'pigs.fly.in.packs@gmail.com';
- $returnArray['recipients'] = 'gregory@pfip.au';
- $sql = e107::getDb();
- $coverPageData = $this->coverPage($sql, $user);
- $questionsPageData = $this->questionsTable($sql, $user);
-
- $returnArray['message'] = $this->template($coverPageData,$questionsPageData);
- $sql->close();
-
- return $returnArray;
- }
- private function coverPage($sql, $user) {
-
- $sql->gen("SELECT `subject`, COUNT(`correct`) as total, CAST((SUM(correct)/COUNT(id)*100) as DECIMAL(5,2)) as percent
- FROM e107.`e107_tkReport` WHERE FROM_UNIXTIME(`answereddatestamp`)>=DATE_ADD(CURDATE(), INTERVAL -1 DAY) AND `username` = '".$user."' GROUP BY (`subject`);");
-
- $Scores = [];
- $cntr = 0;
- $percentTotal = 0;
-
- while ($row = $sql->fetch()) {
-
- $Scores[] = ['subject'=>$row['subject'],'total'=>$row['total'],'percent'=>$row['percent']];
- $percentTotal = $percentTotal + $row['percent'];
- $cntr++;
- }
-
- if(is_numeric($percentTotal) && is_numeric($cntr) && $percentTotal != 0 && $cntr != 0)
- {
- $returnPercentTotal = $percentTotal/$cntr;
- }else{
- $returnPercentTotal = 0;
- }
-
- return ['user'=>$user,'TotalPercent'=>$returnPercentTotal,'Scores'=>$Scores];
- }
- private function questionsTable($sql, $user) {
- $sql->gen("SELECT `question`,`answereddatestamp`,`userAnswer`,`correct`,`subject` FROM e107.e107_tkReport WHERE username='$user'
- AND FROM_UNIXTIME(`answereddatestamp`)>=DATE_ADD(CURDATE(), INTERVAL -1 DAY) ORDER BY subject;");
- $questionsData['questions'] = [];
- $startTime = 9999999999;
- $endTime = 0;
-
- while ($row = $sql->fetch()) {
- if(!isset($questionsData['questions'][$row["subject"]])) {
- $questionsData['questions'][$row["subject"]] = [];
- }
-
- $questionsData['questions'][$row["subject"]][] = ['question'=>$row["question"],'userAnswer'=>$row["userAnswer"],'correct'=>$row["correct"]];
-
- if($row["answereddatestamp"]<=$startTime) {
- $startTime = $row["answereddatestamp"];
- }
-
- if($row["answereddatestamp"]>=$endTime) {
- $endTime = $row["answereddatestamp"];
- }
- }
-
- $questionsData['startTime'] = $startTime;
- $questionsData['endTime'] = $endTime;
-
- return $questionsData;
- }
- private function correctOrNot($state) {
- if ($state) {
- return '<img alt="Correct" src="' . e_IMAGE . 'admin_images/true_16.png"/>';
- } else {
- return '<img alt="Incorrect" src="' . e_IMAGE . 'admin_images/false_16.png"/>';
- }
- }
- private function template($coverPageData,$questionsPageData) {
- $emailTemplate = '<!DOCTYPE html>
- <html>
- <head>
- <style>
- body {
- background-color: #1a1a1a;
- color: #ffffff;
- font-family: Arial, sans-serif;
- }
- #sum {
- font-size: 32px;
- text-align: center;
- color: #ff9900;
- }
- #styTi {
- font-size: 14px;
- text-align: center;
- color: #cccccc;
- }
- #coverP, #questions {
- font-family: Arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- margin-top: 20px;
- }
- #coverP td, #coverP th, #questions td, #questions th {
- border: 1px solid #444;
- padding: 12px;
- text-align: left;
- }
- #coverP tr:nth-child(even), #questions tr:nth-child(even) {
- background-color: #2a2a2a;
- }
- #coverP tr:hover, #questions tr:hover {
- background-color: #333;
- }
- #coverP th, #questions th {
- padding-top: 12px;
- padding-bottom: 12px;
- text-align: left;
- background-color: #ff9900;
- color: #1a1a1a;
- font-weight: bold;
- }
- #coverP td, #questions td {
- color: #ffffff;
- }
- </style>
- </head>
- <body>
- <div id="sum">'.ucfirst($coverPageData["user"]).'\'s overall score for yesterday is '.$coverPageData["TotalPercent"].'%.</div>
- <div id="styTi">'.round(abs($questionsPageData["endTime"] - $questionsPageData["startTime"]) / 60, 2)." minutes were spent studying today.".'</div>
- <br/>
- <table id="coverP">
- <tr>
- <th>Subject</th>
- <th>Count</th>
- <th>Percentage</th>
- </tr>';
-
- foreach ($coverPageData["Scores"] as $Score) {
- $emailTemplate .= '
- <tr>
- <td>'.ucfirst($Score["subject"]).'</td>
- <td>'.$Score["total"].'</td>
- <td>'.$Score["percent"].'%</td>
- </tr>';
- }
-
- $emailTemplate .= '
- </table>
- <br/>
- <table id="questions">
- <tr>
- <th>Question</th>
- <th>Answered</th>
- <th>Correct?</th>
- </tr>';
-
- $subjectNow = '';
- foreach ($questionsPageData["questions"] as $key => $questionSub) {
- foreach ($questionSub as $question) {
- if($subjectNow == '' || $subjectNow != $key) {
- $subjectNow = $key;
- $emailTemplate .= '
- <tr>
- <td colspan="3" style="text-align: center; font-size: 16px; background-color: #333; color: #ff9900;">'.ucfirst($subjectNow).'</td>
- </tr>';
- }
-
- $emailTemplate .= '
- <tr>
- <td>'.$question["question"].'</td>
- <td>'.$question["userAnswer"].'</td>
- <td>'.$this->correctOrNot($question["correct"]).'</td>
- </tr>';
- }
- }
-
- $emailTemplate .= '
- </table>
- </body>
- </html>';
-
- return $emailTemplate;
- }
-
- }
|