inital commit
This commit is contained in:
147
app/Exports/UserActivity/UserActivity.php
Normal file
147
app/Exports/UserActivity/UserActivity.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports\UserActivity;
|
||||
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Maatwebsite\Excel\Concerns\FromView;
|
||||
use App\Models\RoadItemsProject;
|
||||
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||||
use Maatwebsite\Excel\Events\AfterSheet;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use Maatwebsite\Excel\Concerns\WithDrawings;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
|
||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use Carbon\Carbon;
|
||||
use App\Models\Role;
|
||||
use DB;
|
||||
class UserActivity implements FromView, ShouldAutoSize, WithEvents, WithDrawings, WithStyles
|
||||
{
|
||||
public function __construct($role_id, $fromDate, $toDate)
|
||||
{
|
||||
$this->role_id = $role_id;
|
||||
$this->toDate = $toDate;
|
||||
$this->fromDate = $fromDate;
|
||||
}
|
||||
|
||||
public function view(): View
|
||||
{
|
||||
$this->fromDate = $this->fromDate ? $this->fromDate . " 00:00:00" : Carbon::now()->format('Y-m-d 00:00:00');
|
||||
$this->toDate = $this->toDate ? $this->toDate . " 23:59:59" : Carbon::tomorrow()->format('Y-m-d 23:59:59');
|
||||
$role = Role::find($this->role_id);
|
||||
$data = [];
|
||||
|
||||
$ids_list = '';
|
||||
|
||||
foreach ($role->users()->get() as $index => $item) {
|
||||
if (!$index) {
|
||||
$ids_list = $item['id'];
|
||||
} else {
|
||||
$ids_list .= ",".$item['id'];
|
||||
}
|
||||
}
|
||||
|
||||
$temp_sql_for_activity_time = "
|
||||
SELECT sum(majmoe) AS activity FROM (SELECT *,
|
||||
COUNT(cnt) AS majmoe
|
||||
FROM (
|
||||
SELECT *,
|
||||
COUNT(*) AS cnt
|
||||
FROM (
|
||||
SELECT YEAR (created_at) AS yy,
|
||||
MONTH (created_at) AS mm,
|
||||
DAY (created_at) AS dd,
|
||||
HOUR (created_at) AS hh,
|
||||
user_id
|
||||
FROM user_activity_logs
|
||||
WHERE created_at between '{$this->fromDate}' and '{$this->toDate}' and user_id IN(".$ids_list.")
|
||||
) user_activity_distinct
|
||||
GROUP BY yy,
|
||||
mm,
|
||||
dd,
|
||||
hh,
|
||||
user_id
|
||||
) user_activity_by_hour_and_
|
||||
GROUP BY user_id) AS calculated_table;";
|
||||
|
||||
$temp_sql_for_activity_list = "
|
||||
SELECT *,
|
||||
COUNT(*) AS cnt
|
||||
FROM (
|
||||
SELECT
|
||||
description,
|
||||
log_unique_code
|
||||
FROM user_activity_logs
|
||||
WHERE created_at between '{$this->fromDate}' and '{$this->toDate}' and user_id IN(".$ids_list.")
|
||||
) user_activity_distinct
|
||||
GROUP BY
|
||||
log_unique_code
|
||||
ORDER BY cnt desc";
|
||||
|
||||
$data = [
|
||||
'activity_time' => $ids_list ? DB::select(DB::raw($temp_sql_for_activity_time))[0]->activity : null,
|
||||
'average' => round(($ids_list ? DB::select(DB::raw($temp_sql_for_activity_time))[0]->activity : 0) / count($role->users()->get()),2) ,
|
||||
'activity_list' => $ids_list ? DB::select(DB::raw($temp_sql_for_activity_list)) : null,
|
||||
'group_name' => $role->name_fa,
|
||||
'user_count' => count($role->users()->get()),
|
||||
'group_id' => $role->id
|
||||
];
|
||||
|
||||
|
||||
return view('excel.user-actitvity-reports', [
|
||||
'array' => $data,
|
||||
'fromDate' => $this->fromDate,
|
||||
'toDate' => $this->toDate
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function registerEvents(): array
|
||||
{
|
||||
return [
|
||||
AfterSheet::class => function(AfterSheet $event) {
|
||||
$event->sheet->getDelegate()->setRightToLeft(true);
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public function drawings()
|
||||
{
|
||||
$drawing = new Drawing();
|
||||
$drawing->setName('Logo');
|
||||
$drawing->setDescription('This is my logo');
|
||||
$drawing->setPath(public_path('/dist/logo.png'));
|
||||
$drawing->setWidth(50);
|
||||
$drawing->setHeight(50);
|
||||
$drawing->setOffsetX(5);
|
||||
$drawing->setOffsetY(5);
|
||||
$drawing->setCoordinates('A1');
|
||||
|
||||
$drawing2 = new Drawing();
|
||||
$drawing2->setName('Logo');
|
||||
$drawing2->setDescription('This is my logo');
|
||||
$drawing2->setPath(public_path('/dist/141icon.png'));
|
||||
$drawing2->setWidth(50);
|
||||
$drawing2->setHeight(50);
|
||||
$drawing2->setOffsetX(5);
|
||||
$drawing2->setOffsetY(5);
|
||||
$drawing2->setCoordinates('B1');
|
||||
return [$drawing, $drawing2];
|
||||
}
|
||||
|
||||
public function styles(Worksheet $sheet)
|
||||
{
|
||||
|
||||
return [
|
||||
// Style the first row as bold text.
|
||||
'A:BA' => [
|
||||
'font' => [
|
||||
'name' => 'B Nazanin'
|
||||
]
|
||||
],
|
||||
];
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user