vendor and env first commit
This commit is contained in:
+50
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\UserActivity\Repositories\Activity;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Collection as BaseCollection;
|
||||
use Vanguard\UserActivity\Activity;
|
||||
|
||||
interface ActivityRepository
|
||||
{
|
||||
/**
|
||||
* Log user activity.
|
||||
*
|
||||
* @param $data array Array with following fields:
|
||||
* description (string) - Description of user activity.
|
||||
* user_id (int) - User unique identifier.
|
||||
* ip_address (string) - Ip address from which user is accessing the website.
|
||||
* user_agent (string) - User's browser info.
|
||||
* @return mixed
|
||||
*/
|
||||
public function log(array $data): Activity;
|
||||
|
||||
/**
|
||||
* Paginate activities for user.
|
||||
*/
|
||||
public function paginateActivitiesForUser(
|
||||
int $userId,
|
||||
int $perPage = 20,
|
||||
?string $search = null
|
||||
): LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* Get specified number of latest user activity logs.
|
||||
*
|
||||
* @return Collection<Activity>
|
||||
*/
|
||||
public function getLatestActivitiesForUser(int $userId, int $activitiesCount = 10): Collection;
|
||||
|
||||
/**
|
||||
* Paginate all activity records.
|
||||
*/
|
||||
public function paginateActivities(int $perPage = 20, ?string $search = null): LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* Get count of user activities per day for given period of time.
|
||||
*/
|
||||
public function userActivityForPeriod(int $userId, Carbon $from, Carbon $to): BaseCollection;
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\UserActivity\Repositories\Activity;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection as BaseCollection;
|
||||
use Vanguard\UserActivity\Activity;
|
||||
|
||||
class EloquentActivity implements ActivityRepository
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function log($data): Activity
|
||||
{
|
||||
return Activity::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function paginateActivitiesForUser(
|
||||
int $userId,
|
||||
int $perPage = 20,
|
||||
?string $search = null
|
||||
): LengthAwarePaginator {
|
||||
$query = Activity::where('user_id', $userId);
|
||||
|
||||
return $this->paginateAndFilterResults($perPage, $search, $query);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLatestActivitiesForUser(int $userId, int $activitiesCount = 10): Collection
|
||||
{
|
||||
return Activity::where('user_id', $userId)
|
||||
->orderBy('created_at', 'DESC')
|
||||
->limit($activitiesCount)
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function paginateActivities(int $perPage = 20, ?string $search = null): LengthAwarePaginator
|
||||
{
|
||||
$query = Activity::with('user');
|
||||
|
||||
return $this->paginateAndFilterResults($perPage, $search, $query);
|
||||
}
|
||||
|
||||
private function paginateAndFilterResults($perPage, $search, $query): LengthAwarePaginator
|
||||
{
|
||||
if ($search) {
|
||||
$query->where('description', 'LIKE', "%$search%");
|
||||
}
|
||||
|
||||
$result = $query->orderBy('created_at', 'DESC')
|
||||
->paginate($perPage);
|
||||
|
||||
if ($search) {
|
||||
$result->appends(['search' => $search]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function userActivityForPeriod($userId, Carbon $from, Carbon $to): BaseCollection
|
||||
{
|
||||
$result = Activity::select([
|
||||
DB::raw('DATE(created_at) as day'),
|
||||
DB::raw('count(id) as count'),
|
||||
])
|
||||
->where('user_id', $userId)
|
||||
->whereBetween('created_at', [$from, $to])
|
||||
->groupBy('day')
|
||||
->orderBy('day', 'ASC')
|
||||
->pluck('count', 'day');
|
||||
|
||||
while (! $from->isSameDay($to)) {
|
||||
if (! $result->has($from->toDateString())) {
|
||||
$result->put($from->toDateString(), 0);
|
||||
}
|
||||
$from->addDay();
|
||||
}
|
||||
|
||||
return $result->sortBy(function ($value, $key) {
|
||||
return strtotime($key);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user