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 all 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()
->withAllRoles($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
44 changes: 44 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,50 @@ 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 are in all the specified workshops.
* The workshops are specified by their IDs.
*/
public function scopeInAllWorkshopIds(Builder $query, array $workshopsIdsAll): Builder
{
return $query->whereHas('workshops', function (Builder $query) use ($workshopsIdsAll) {
$query->whereIn('id', $workshopsIdsAll);
}, '=', count($workshopsIdsAll));
}

/**
* 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
33 changes: 31 additions & 2 deletions app/Utils/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ abstract public function roles(): BelongsToMany;
* See also: hasRole(...) getter for User models.
*
* @param Builder $query
* @param Role|string $role
* @param Role|int|string $role
* @param Workshop|RoleObject|string|null $object
* @return Builder
*/
public function scopeWithRole(Builder $query, Role|string $role, Workshop|RoleObject|string $object = null): Builder
public function scopeWithRole(Builder $query, Role|int|string $role, Workshop|RoleObject|string $object = null): Builder
{
$role = Role::get($role);
if ($object) {
Expand All @@ -49,6 +49,35 @@ public function scopeWithRole(Builder $query, Role|string $role, Workshop|RoleOb
});
}

/**
* Scope a query to only include users with all the given roles.
* Usage: ->withAllRoles(...)
*
* @param Builder $query
* @param Role[]|int[]|string[] $allRoles a homogeneous array of Role objects, role IDs or role names
* @return Builder
*/
public function scopeWithAllRoles(Builder $query, array $allRoles): Builder
{
// Empty array => nothing to filter, nothing to do
if (empty($allRoles)) return $query;

// Input is an array of role names => filter based on names
if (is_string($allRoles[0])) {
return $query->whereHas('roles', function (Builder $query) use ($allRoles) {
$query->whereIn('name', $allRoles);
}, '=', count($allRoles));
}

// Input is an array of role objects => convert objects to IDs
if ($allRoles[0] instanceof Role) $allRoles = array_map(fn($role) => $role->id, $allRoles);

// Input is an array of role IDs => filter based on IDs
return $query->whereHas('roles', function (Builder $query) use ($allRoles) {
$query->whereIn('id', $allRoles);
}, '=', count($allRoles));
}

/**
* Decides if the user has any of the given roles.
* See also: withRole(...) scope for query builders.
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