PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / trunk
Fluent Support – Helpdesk & Customer Support Ticket System vtrunk
2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 All 67 releases
fluent-support / app / Modules / Reporting / ReportingHelperTrait.php

ReportingHelperTrait.php in Fluent Support – Helpdesk & Customer Support Ticket System trunk, at app/Modules/Reporting/ReportingHelperTrait.php

221 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Modules\Reporting;
4
5 /**
6 * ReportingHelperTrait is act as helper of report
7 * This will help to get frequency, several date format, get advanced or backdated date and other
8 * @package FluentSupport\App\Modules\Reporting
9 *
10 * @version 1.0.0
11 */
12
13 trait ReportingHelperTrait
14 {
15 protected static $hourly = 'PT1H';
16 protected static $daily = 'P1D';
17 protected static $weekly = 'P1W';
18 protected static $monthly = 'P1M';
19
20 protected function db() {
21 return \FluentSupport\App\App::getInstance('db');
22 }
23
24 protected function makeFromDate($from)
25 {
26 $from = $from ?: '-30 days';
27
28 return new \DateTime($from);
29 }
30
31 protected function makeToDate($to = false)
32 {
33 $to = $to ?: '+1 days';
34
35 return new \DateTime($to);
36 }
37
38 protected function makeDatePeriod($from, $to, $interval = null)
39 {
40 $interval = $interval ?: static::$daily;
41
42 return new \DatePeriod($from, new \DateInterval($interval), $to);
43 }
44
45
46 /**
47 * getFrequency method will return the frequency
48 * this function get date from and date to as parameter and find the frequency and return
49 * @param $from
50 * @param $to
51 * @return string
52 */
53 protected function getFrequency($from, $to)
54 {
55 $numDays = $to->diff($from)->format("%a");
56 //If number of days in the range is greater than 2 month and less than or equal to 3 months
57 if($numDays <= 0) {
58 return static::$hourly;
59 }
60 else if ($numDays > 62 && $numDays <= 181) {
61 return static::$weekly;
62 } else if ($numDays > 181) {
63 //Greater than 3 months
64 return static::$monthly;
65 }
66
67 return static::$daily;
68 }
69
70 /**
71 * prepareSelect method will prepare the field to select
72 * @param $frequency
73 * @param string $dateField
74 * @return array
75 */
76 protected function prepareSelect($frequency, $dateField = 'created_at', $type = 'count_id')
77 {
78 switch ($type) {
79 case 'count_id':
80 $countField = 'COUNT(id) AS count';
81 break;
82 case 'response_count':
83 $countField = 'SUM(response_count) AS count';
84 break;
85 default:
86 $countField = 'COUNT(id) AS count';
87 }
88
89 $select = [
90 $this->db()->raw($countField),
91 $this->db()->raw('DATE('.$dateField.') AS date'),
92 $this->db()->raw('TIME('.$dateField.') AS time')
93 ];
94
95 if ($frequency == static::$hourly) {
96 $select[] = $this->db()->raw('HOUR(created_at) hourly');
97 } else if ($frequency == static::$weekly) {
98 $select[] = $this->db()->raw('WEEK(created_at) week');
99 } else if ($frequency == static::$monthly) {
100 $select[] = $this->db()->raw('MONTH(created_at) month');
101 }
102
103 return $select;
104 }
105
106 protected function prepareBetween($frequency, $from, $to)
107 {
108 if($frequency == static::$hourly){
109 return [
110 $from->format('Y-m-d')." 00:00:00",
111 $to->format('Y-m-d')." 23:59:59"
112 ];
113 }
114 return [
115 $from->format('Y-m-d')." 00:00:00",
116 $to->format('Y-m-d')." 23:59:59"
117 ];
118 }
119
120 /**
121 * getGroupAndOrder method will return order by and group by value based on frequency
122 * @param $frequency
123 * @return string[]
124 */
125 protected function getGroupAndOrder($frequency)
126 {
127 $orderBy = $groupBy = 'date';
128
129 if ($frequency == static::$hourly) {
130 $orderBy = $groupBy = 'hourly';
131 } elseif ($frequency == static::$weekly) {
132 $orderBy = $groupBy = 'week';
133 } else if ($frequency == static::$monthly) {
134 $orderBy = $groupBy = 'month';
135 }
136
137 return [$groupBy, $orderBy];
138 }
139
140 protected function getDateRangeArray($period)
141 {
142 $range = [];
143
144 $formatter = 'basicFormatter';
145
146 if ($this->isMonthly($period)) {
147 $formatter = 'monYearFormatter';
148 }
149
150 foreach ($period as $date) {
151 $date = $this->{$formatter}($date);
152 $range[$date] = 0;
153 }
154
155 return $range;
156 }
157
158 protected function getResult($period, $items)
159 {
160 $range = $this->getDateRangeArray($period);
161
162 $formatter = 'basicFormatter';
163
164 if ($this->isHourly($period)) {
165 $formatter = 'hourlyFormatter';
166 }
167 else if ($this->isMonthly($period)) {
168 $formatter = 'monYearFormatter';
169 }
170
171 foreach ($items as $item) {
172 if($this->isHourly($period)){
173 $time = $this->{$formatter}($item->time);
174 $range[$time] = (int) $item->count;
175 }else{
176 $date = $this->{$formatter}($item->date);
177 $range[$date] = (int) $item->count;
178 }
179 }
180
181 return $range;
182 }
183
184 protected function isMonthly($period)
185 {
186 return !!$period->getDateInterval()->m;
187 }
188
189 protected function isHourly($period)
190 {
191 return !!$period->getDateInterval()->h;
192 }
193
194 protected function basicFormatter($date)
195 {
196 if (is_string($date)) {
197 $date = new \DateTime($date);
198 }
199
200 return $date->format('Y-m-d');
201 }
202
203 protected function hourlyFormatter($date)
204 {
205 if (is_string($date)) {
206 $date = new \DateTime($date);
207 }
208
209 return $date->format('H').':00' .' - '. $date->format('H').':59';
210 }
211
212 protected function monYearFormatter($date)
213 {
214 if (is_string($date)) {
215 $date = new \DateTime($date);
216 }
217
218 return $date->format('M Y');
219 }
220 }
221