Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User list export: apply filters (fixes #298) #481

Merged
merged 7 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions app/Exports/UsersExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,32 @@
use App\Exports\UsersSheets\StatusesExport;
use App\Exports\UsersSheets\StudentsCouncilFeedback;
use App\Models\Role;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Concerns\WithDefaultStyles;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use PhpOffice\PhpSpreadsheet\Style\Style;

class UsersExport implements WithMultipleSheets, WithDefaultStyles
{
private Collection|User $includedUsers;

public function __construct(Collection|User $includedUsers)
{
$this->includedUsers = $includedUsers->sortBy('name');
}

public function sheets(): array
{
$sheets = [new CollegistsExport(), new StatusesExport()];
$sheets = [
new CollegistsExport($this->includedUsers),
new StatusesExport($this->includedUsers),
];

if(user()->can('viewSemesterEvaluation', User::class)) {
$sheets[] = new SemesterEvaluationExport();
$sheets[] = new SemesterEvaluationExport($this->includedUsers);
if(user()->hasRole(Role::STUDENT_COUNCIL)) {
$sheets[] = new StudentsCouncilFeedback(true);
$sheets[] = new StudentsCouncilFeedback($this->includedUsers);
}
}

Expand Down
5 changes: 3 additions & 2 deletions app/Exports/UsersSheets/CollegistsExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Semester;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
Expand All @@ -17,9 +18,9 @@ class CollegistsExport implements FromCollection, WithTitle, WithMapping, WithHe
protected $users;
protected $semester;

public function __construct()
public function __construct(Collection|User $includedUsers)
{
$this->users = User::canView()->orderBy('name')->get();
$this->users = $includedUsers;
$this->semester = Semester::current();
}

Expand Down
6 changes: 3 additions & 3 deletions app/Exports/UsersSheets/SemesterEvaluationExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Role;
use App\Models\SemesterEvaluation;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
Expand All @@ -18,12 +19,11 @@ class SemesterEvaluationExport implements FromCollection, WithTitle, WithMapping
{
protected $evaluations;
protected $semester;
protected $show_feedback = false;

public function __construct()
public function __construct(Collection|User $includedUsers)
{
$this->semester = SemesterEvaluation::query()->orderBy('created_at', 'desc')->first()?->semester;
$users = User::query()->canView()->get(['id'])->pluck('id');
$users = $includedUsers->pluck('id');
$this->evaluations = SemesterEvaluation::query()
->where('semester_id', $this->semester?->id)
->whereIn('user_id', $users)
Expand Down
5 changes: 3 additions & 2 deletions app/Exports/UsersSheets/StatusesExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Semester;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
Expand All @@ -17,9 +18,9 @@ class StatusesExport implements FromCollection, WithTitle, WithMapping, WithHead
protected $users;
protected $semesters;

public function __construct()
public function __construct(Collection|User $includedUsers)
{
$this->users = User::canView()->orderBy('name')->get();
$this->users = $includedUsers;
$this->semesters = Semester::allUntilCurrent()->sortByDesc('tag');
}

Expand Down
12 changes: 9 additions & 3 deletions app/Exports/UsersSheets/StudentsCouncilFeedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Exports\UsersSheets;

use App\Models\SemesterEvaluation;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
Expand All @@ -13,10 +15,14 @@ class StudentsCouncilFeedback implements FromCollection, WithTitle, WithMapping,
{
protected $evaluations;

public function __construct()
public function __construct(Collection|User $includedUsers)
{
$last_semester_id = SemesterEvaluation::query()->orderBy('created_at', 'desc')->first()->semester_id;
$this->evaluations = SemesterEvaluation::query()->where('semester_id', $last_semester_id)->whereNotNull('feedback')->get();
$last_semester_id = SemesterEvaluation::query()->orderBy('created_at', 'desc')->first()?->semester_id;
$this->evaluations = SemesterEvaluation::query()
->where('semester_id', $last_semester_id)
->whereNotNull('feedback')
->whereIn('user_id', $includedUsers->pluck('id'))
->get();

}

Expand Down
12 changes: 0 additions & 12 deletions app/Http/Controllers/Secretariat/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,6 @@ public function show(User $user)
]);
}

/**
* Exports the list of users to an Excel file.
* @return BinaryFileResponse
* @throws AuthorizationException
*/
public function export()
{
$this->authorize('viewAny', User::class);

return Excel::download(new UsersExport(), 'uran_export.xlsx');
}

/**
* Adds a role to the user.
* @param Request $request
Expand Down
84 changes: 39 additions & 45 deletions app/Livewire/ListUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace App\Livewire;

use App\Models\Semester;
use App\Exports\UsersExport;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\View\View;
use Illuminate\Database\Eloquent\Collection;
use Livewire\Component;
use Maatwebsite\Excel\Facades\Excel;

class ListUsers extends Component
{
Expand All @@ -18,47 +20,31 @@ class ListUsers extends Component
public $filter_name = '';

/**
* Return the `users` property.
* @return User|Builder the query with all filters (the properties of this class) applied
*/
public function getUsersProperty()
private function createFilteredQuery(): User|Builder
{
$query = User::canView();

$query->where(function (Builder $query) {
foreach ($this->roles as $role) {
$query->whereHas('roles', function (Builder $query) use ($role) {
$query->where('id', $role);
});
}

foreach ($this->workshops as $workshop) {
$query->whereHas('workshops', function (Builder $query) use ($workshop) {
$query->where('id', $workshop);
});
}

if (isset($this->year_of_acceptance)) {
$query->whereHas('educationalInformation', function (Builder $query) {
$query->where('year_of_acceptance', $this->year_of_acceptance);
});
}

if (isset($this->filter_name)) {
$query->where('name', 'like', '%' . $this->filter_name . '%');
}
});

//'or' between statuses
$query->where(function ($query) {
foreach ($this->statuses as $status) {
$query->orWhereHas('semesterStatuses', function (Builder $query) use ($status) {
$query->where('status', $status);
$query->where('id', Semester::current()->id);
});
}
});
$query = User::canView()
->hasAllRoleIds($this->roles)
->inAllWorkshopIds($this->workshops)
->hasStatusAnyOf($this->statuses);
if (isset($this->year_of_acceptance) && $this->year_of_acceptance !== '') {
$query->yearOfAcceptance($this->year_of_acceptance);
}
if (isset($this->filter_name)) {
$query->nameLike($this->filter_name);
}
return $query;
}

return $query
/**
* Return the `users` property.
*
* @return array|User[]|Collection the users that match the specified filters, so the users that should be listed
*/
public function getUsersProperty(): array|Collection
{
return $this->createFilteredQuery()
->with(['roles', 'workshops', 'educationalInformation', 'semesterStatuses'])
->orderBy('name')->get();
}
Expand Down Expand Up @@ -86,21 +72,21 @@ public function deleteRole($role_id)
/**
* Add a status to filter on.
*
* @param int $status_id
* @param string $status
*/
public function addStatus($status_id)
public function addStatus($status)
{
$this->statuses[] = $status_id;
$this->statuses[] = $status;
}

/**
* Delete a status from the list of statuses to filter on.
*
* @param int $status_id
* @param string $status
*/
public function deleteStatus($status_id)
public function deleteStatus($status)
{
$this->statuses = \array_diff($this->statuses, [$status_id]);
$this->statuses = \array_diff($this->statuses, [$status]);
}

/**
Expand All @@ -123,6 +109,14 @@ public function deleteWorkshop($workshop_id)
$this->workshops = \array_diff($this->workshops, [$workshop_id]);
}

/**
* Export listed users to excel
*/
public function export()
{
return Excel::download(new UsersExport($this->createFilteredQuery()->get()), 'uran_export.xlsx');
}

/**
* Render the component.
*
Expand Down
59 changes: 59 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,65 @@ public function scopeHasToPayKKTNetregInSemester(Builder $query, int $semester_i
});
}

/**
* Scope a query to only include users who got accepted in the specified year.
*/
public function scopeYearOfAcceptance(Builder $query, int $yearOfAcceptance): Builder
{
return $query->whereHas('educationalInformation', function (Builder $query) use ($yearOfAcceptance) {
$query->where('year_of_acceptance', $yearOfAcceptance);
});
}

/**
* Scope a query to only include users whose name contains the given string.
*/
public function scopeNameLike(Builder $query, string $nameLike): Builder
{
return $query->where('name', 'like', '%' . $nameLike . '%');
}

/**
* Scope a query to only include users who have all the specified roles.
* The roles are specified by their IDs.
*/
public function scopeHasAllRoleIds(Builder $query, array $roleIdsAll): void
{
foreach ($roleIdsAll as $roleId) {
$query->whereHas('roles', function (Builder $query) use ($roleId) {
$query->where('id', $roleId);
Trigary marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
Trigary marked this conversation as resolved.
Show resolved Hide resolved

/**
* Scope a query to only include users who are in all the specified workshops.
* The workshops are specified by their IDs.
*/
public function scopeInAllWorkshopIds(Builder $query, array $workshopsIdsAll): void
{
foreach ($workshopsIdsAll as $workshopId) {
$query->whereHas('workshops', function (Builder $query) use ($workshopId) {
$query->where('id', $workshopId);
Trigary marked this conversation as resolved.
Show resolved Hide resolved
});
}
}

/**
* Scope a query to only include users who have any of the specified roles.
*/
public function scopeHasStatusAnyOf(Builder $query, array $statusesAny): Builder
{
return $query->where(function ($query) use ($statusesAny) {
foreach ($statusesAny as $status) {
$query->orWhereHas('semesterStatuses', function (Builder $query) use ($status) {
$query->where('status', $status);
$query->where('id', Semester::current()->id);
});
}
});
}

/*
|--------------------------------------------------------------------------
| Public functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
beszámoló mellett.
</p>
<p>
A kérdőív eredményei <a href="{{ route('users.export') }}">letölthetőek</a> a Collegisták menüpont alatt. A
táblázatban az "értékelés" fül alatt találhatóak az eddigi kitöltések (A határidő lejáratáig még szabadon
módosíthatnak az értékeken.)<br/>
A kérdőív eredményei letölthetőek a <a href="{{ route('users.index') }}">@lang("general.users")</a> menüpont
alatt. A táblázatban az "értékelés" fül alatt találhatóak az eddigi kitöltések. (A határidő lejáratáig még
szabadon módosíthatnak az értékeken.)<br/>
Az eredményekhez a műhelyvezetők is hozzáférnek.
</p>
@endcomponent
6 changes: 3 additions & 3 deletions resources/views/emails/evaluation_form_closed.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
@component('mail::message')
<h1>@lang('mail.dear') {{ $recipient }}!</h1>
<p>
A szemeszter végi értékelő form eredményei <a href="{{ route('users.export') }}">letölthetőek</a> a Collegisták
menüpont alatt. A
táblázatban az "értékelés" fül alatt találhatóak az értékek.
A szemeszter végi értékelő form eredményei letölthetőek a
<a href="{{ route('users.index') }}">@lang("general.users")</a> menüpont alatt.
A táblázatban az "értékelés" fül alatt találhatóak az értékek.
</p>
@if($deactivated)
<p>
Expand Down
6 changes: 0 additions & 6 deletions resources/views/secretariat/user/list.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,4 @@

@livewire('list-users')

{{-- Export --}}
<div class="fixed-action-btn tooltipped" data-position="left" data-tooltip="Exportálás">
<a href="{{ route('users.export') }}" class="btn-floating btn-large">
<i class="large material-icons">file_download</i>
</a>
</div>
@endsection
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,11 @@
</div>
</div>
</div>

{{-- Export --}}
<div class="fixed-action-btn tooltipped" data-position="left" data-tooltip="Szűrt felhasználók adatainak exportálása">
<span wire:click="export()" class="btn-floating btn-large">
<i class="large material-icons">file_download</i>
</span>
</div>
</div>
1 change: 0 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
/** User related routes */
Route::get('/profile', [UserController::class, 'profile'])->name('profile');
Route::get('/users', [UserController::class, 'index'])->name('users.index');
Route::get('/users/export', [UserController::class, 'export'])->name('users.export');
Route::get('/users/{user}', [UserController::class, 'show'])->name('users.show');
Route::post('/users/{user}/tenant_until', [UserController::class, 'updateTenantUntil'])->name('users.update.tenant_until');
Route::post('/users/{user}/roles/{role}', [UserController::class, 'addRole'])->name('users.roles.add');
Expand Down