Added latest activity into users list view

This commit is contained in:
Dan Brown 2020-11-20 20:10:18 +00:00
parent bd6a1a66d1
commit c0680d5717
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
4 changed files with 31 additions and 6 deletions

View File

@ -1,5 +1,6 @@
<?php namespace BookStack\Auth;
use BookStack\Actions\Activity;
use BookStack\Api\ApiToken;
use BookStack\Interfaces\Loggable;
use BookStack\Model;
@ -12,6 +13,7 @@ use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Notifications\Notifiable;
/**
@ -230,6 +232,14 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
return $this->hasMany(ApiToken::class);
}
/**
* Get the latest activity instance for this user.
*/
public function latestActivity(): HasOne
{
return $this->hasOne(Activity::class)->latest();
}
/**
* Get the url for editing this user.
*/

View File

@ -10,6 +10,7 @@ use BookStack\Exceptions\UserUpdateException;
use BookStack\Uploads\Image;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Images;
use Log;
@ -56,13 +57,19 @@ class UserRepo
/**
* Get all the users with their permissions in a paginated format.
* @param int $count
* @param $sortData
* @return Builder|static
*/
public function getAllUsersPaginatedAndSorted($count, $sortData)
public function getAllUsersPaginatedAndSorted(int $count, array $sortData): LengthAwarePaginator
{
$query = $this->user->with('roles', 'avatar')->orderBy($sortData['sort'], $sortData['order']);
$sort = $sortData['sort'];
if ($sort === 'latest_activity') {
$sort = \BookStack\Actions\Activity::query()->select('created_at')
->whereColumn('activities.user_id', 'users.id')
->latest()
->take(1);
}
$query = $this->user->with(['roles', 'avatar', 'latestActivity'])
->orderBy($sort, $sortData['order']);
if ($sortData['search']) {
$term = '%' . $sortData['search'] . '%';

View File

@ -157,6 +157,7 @@ return [
'user_profile' => 'User Profile',
'users_add_new' => 'Add New User',
'users_search' => 'Search Users',
'users_latest_activity' => 'Latest Activity',
'users_details' => 'User Details',
'users_details_desc' => 'Set a display name and an email address for this user. The email address will be used for logging into the application.',
'users_details_desc_no_email' => 'Set a display name for this user so others can recognise them.',

View File

@ -27,7 +27,6 @@
</div>
</div>
{{--TODO - Add last login--}}
<table class="table">
<tr>
<th></th>
@ -37,6 +36,9 @@
<a href="{{ sortUrl('/settings/users', $listDetails, ['sort' => 'email']) }}">{{ trans('auth.email') }}</a>
</th>
<th>{{ trans('settings.role_user_roles') }}</th>
<th class="text-right">
<a href="{{ sortUrl('/settings/users', $listDetails, ['sort' => 'latest_activity']) }}">{{ trans('settings.users_latest_activity') }}</a>
</th>
</tr>
@foreach($users as $user)
<tr>
@ -55,6 +57,11 @@
<small><a href="{{ url("/settings/roles/{$role->id}") }}">{{$role->display_name}}</a>@if($index !== count($user->roles) -1),@endif</small>
@endforeach
</td>
<td class="text-right text-muted">
@if($user->latestActivity)
<small title="{{ $user->latestActivity->created_at->format('Y-m-d H:i:s') }}">{{ $user->latestActivity->created_at->diffForHumans() }}</small>
@endif
</td>
</tr>
@endforeach
</table>