Busiest_Day_Of_Week_Module.php
5 months ago
Busiest_Time_Of_Day_Module.php
1 year ago
Line_Chart_Module.php
9 months ago
Map_Module.php
9 months ago
Module.php
3 months ago
New_Sessions_Module.php
1 year ago
Pie_Chart_Module.php
9 months ago
Quick_Stats_Module.php
9 months ago
Recent_Conversions_Module.php
5 months ago
Recent_Views_Module.php
5 months ago
Top_Ten_Module.php
9 months ago
Top_Ten_Module.php
65 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Overview\Modules; |
| 4 | |
| 5 | use IAWP\Date_Range\Relative_Date_Range; |
| 6 | use IAWP\Env; |
| 7 | use IAWP\Tables\Table; |
| 8 | use IAWPSCOPED\Illuminate\Support\Collection; |
| 9 | /** @internal */ |
| 10 | class Top_Ten_Module extends \IAWP\Overview\Modules\Module |
| 11 | { |
| 12 | public function module_type() : string |
| 13 | { |
| 14 | return 'top-ten'; |
| 15 | } |
| 16 | public function module_name() : string |
| 17 | { |
| 18 | return \__('Top 10 List', 'independent-analytics'); |
| 19 | } |
| 20 | public function calculate_dataset() |
| 21 | { |
| 22 | $sort_by = $this->attributes['sort_by'] ?? null; |
| 23 | $sort_direction = $this->attributes['sort_direction'] ?? null; |
| 24 | $date_range = Relative_Date_Range::range_by_id($this->attributes['date_range'] ?? null); |
| 25 | $table_class = Env::get_table($this->report()->type()); |
| 26 | /** @var Table $table */ |
| 27 | $table = new $table_class($this->report()->group_name(), \true); |
| 28 | $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction); |
| 29 | if ($this->report()->has_filters()) { |
| 30 | $filters = $table->sanitize_filters($this->report()->filters()); |
| 31 | } else { |
| 32 | $filters = null; |
| 33 | } |
| 34 | $rows_class = $table->group()->rows_class(); |
| 35 | $rows_query = new $rows_class($date_range, $sort_configuration, 10, $filters); |
| 36 | $rows = $rows_query->rows(); |
| 37 | return Collection::make($rows)->map(function ($model) use($table, $sort_configuration) { |
| 38 | $title_column = $table->group()->title_column(); |
| 39 | $metric_column = $sort_configuration->column(); |
| 40 | return [$model->{$title_column}(), $table->formatted_csv_cell_content($table->get_column($metric_column), $model->{$metric_column}())]; |
| 41 | })->all(); |
| 42 | } |
| 43 | public function primary_column_name() : string |
| 44 | { |
| 45 | $table_class = Env::get_table($this->report()->type()); |
| 46 | /** @var Table $table */ |
| 47 | $table = new $table_class($this->report()->group_name(), \true); |
| 48 | $title_column = $table->group()->title_column(); |
| 49 | $column = $table->get_column($title_column); |
| 50 | return $column->name(); |
| 51 | } |
| 52 | public function metric_column_name() : string |
| 53 | { |
| 54 | $table_class = Env::get_table($this->report()->type()); |
| 55 | /** @var Table $table */ |
| 56 | $table = new $table_class($this->report()->group_name(), \true); |
| 57 | $column = $table->get_column($this->attributes['sort_by']); |
| 58 | return $column->name(); |
| 59 | } |
| 60 | protected function module_fields() : array |
| 61 | { |
| 62 | return ['report', 'sort_by', 'sort_direction', 'date_range']; |
| 63 | } |
| 64 | } |
| 65 |