| @@ -1,10 +1,19 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Modules\Reporting; |
| 4 | 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 | + | |
| 5 | 13 | trait ReportingHelperTrait |
| 6 | 14 | { |
| 15 | + protected static $hourly = 'PT1H'; | |
| 7 | 16 | protected static $daily = 'P1D'; |
| 8 | 17 | protected static $weekly = 'P1W'; |
| 9 | 18 | protected static $monthly = 'P1M'; |
| 10 | 19 | |
| @@ -32,15 +41,27 @@ | ||
| 32 | 41 | |
| 33 | 42 | return new \DatePeriod($from, new \DateInterval($interval), $to); |
| 34 | 43 | } |
| 35 | 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 | + */ | |
| 36 | 53 | protected function getFrequency($from, $to) |
| 37 | 54 | { |
| 38 | 55 | $numDays = $to->diff($from)->format("%a"); |
| 39 | - | |
| 40 | - if ($numDays > 62 && $numDays <= 181) { | |
| 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) { | |
| 41 | 61 | return static::$weekly; |
| 42 | 62 | } else if ($numDays > 181) { |
| 63 | + //Greater than 3 months | |
| 43 | 64 | return static::$monthly; |
| 44 | 65 | } |
| 45 | 66 | |
| 46 | 67 | return static::$daily; |
| @@ -45,16 +66,36 @@ | ||
| 45 | 66 | |
| 46 | 67 | return static::$daily; |
| 47 | 68 | } |
| 48 | 69 | |
| 49 | - protected function prepareSelect($frequency, $dateField = 'created_at') | |
| 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') | |
| 50 | 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 | + | |
| 51 | 89 | $select = [ |
| 52 | - $this->db()->raw('COUNT(id) AS count'), | |
| 53 | - $this->db()->raw('DATE('.$dateField.') AS date') | |
| 90 | + $this->db()->raw($countField), | |
| 91 | + $this->db()->raw('DATE('.$dateField.') AS date'), | |
| 92 | + $this->db()->raw('TIME('.$dateField.') AS time') | |
| 54 | 93 | ]; |
| 55 | 94 | |
| 56 | - if ($frequency == static::$weekly) { | |
| 95 | + if ($frequency == static::$hourly) { | |
| 96 | + $select[] = $this->db()->raw('HOUR(created_at) hourly'); | |
| 97 | + } else if ($frequency == static::$weekly) { | |
| 57 | 98 | $select[] = $this->db()->raw('WEEK(created_at) week'); |
| 58 | 99 | } else if ($frequency == static::$monthly) { |
| 59 | 100 | $select[] = $this->db()->raw('MONTH(created_at) month'); |
| 60 | 101 | } |
| @@ -61,13 +102,34 @@ | ||
| 61 | 102 | |
| 62 | 103 | return $select; |
| 63 | 104 | } |
| 64 | 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 | + */ | |
| 65 | 125 | protected function getGroupAndOrder($frequency) |
| 66 | 126 | { |
| 67 | 127 | $orderBy = $groupBy = 'date'; |
| 68 | 128 | |
| 69 | - if ($frequency == static::$weekly) { | |
| 129 | + if ($frequency == static::$hourly) { | |
| 130 | + $orderBy = $groupBy = 'hourly'; | |
| 131 | + } elseif ($frequency == static::$weekly) { | |
| 70 | 132 | $orderBy = $groupBy = 'week'; |
| 71 | 133 | } else if ($frequency == static::$monthly) { |
| 72 | 134 | $orderBy = $groupBy = 'month'; |
| 73 | 135 | } |
| @@ -98,15 +160,23 @@ | ||
| 98 | 160 | $range = $this->getDateRangeArray($period); |
| 99 | 161 | |
| 100 | 162 | $formatter = 'basicFormatter'; |
| 101 | 163 | |
| 102 | - if ($this->isMonthly($period)) { | |
| 164 | + if ($this->isHourly($period)) { | |
| 165 | + $formatter = 'hourlyFormatter'; | |
| 166 | + } | |
| 167 | + else if ($this->isMonthly($period)) { | |
| 103 | 168 | $formatter = 'monYearFormatter'; |
| 104 | 169 | } |
| 105 | 170 | |
| 106 | 171 | foreach ($items as $item) { |
| 107 | - $date = $this->{$formatter}($item->date); | |
| 108 | - $range[$date] = (int) $item->count; | |
| 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 | + } | |
| 109 | 179 | } |
| 110 | 180 | |
| 111 | 181 | return $range; |
| 112 | 182 | } |
| @@ -115,8 +185,13 @@ | ||
| 115 | 185 | { |
| 116 | 186 | return !!$period->getDateInterval()->m; |
| 117 | 187 | } |
| 118 | 188 | |
| 189 | + protected function isHourly($period) | |
| 190 | + { | |
| 191 | + return !!$period->getDateInterval()->h; | |
| 192 | + } | |
| 193 | + | |
| 119 | 194 | protected function basicFormatter($date) |
| 120 | 195 | { |
| 121 | 196 | if (is_string($date)) { |
| 122 | 197 | $date = new \DateTime($date); |
| @@ -122,8 +197,17 @@ | ||
| 122 | 197 | $date = new \DateTime($date); |
| 123 | 198 | } |
| 124 | 199 | |
| 125 | 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'; | |
| 126 | 210 | } |
| 127 | 211 | |
| 128 | 212 | protected function monYearFormatter($date) |
| 129 | 213 | { |