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

Adding a resident-extern role #358

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Exports/UsersSheets/CollegistsExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public function map($user): array
return [
'=HYPERLINK("'.route('users.show', ['user' => $user->id]).'", "'.$user->name.'")',
$user->educationalInformation?->neptun,
$user->isResident() ? 'Bentlakó' : ($user->isExtern() ? 'Bejáró' : ($user->isAlumni() ? "Alumni" : ($user->isTenant() ? "Vendég" : ""))),
$user->isResident() ? 'Bentlakó' : ($user->isResidentExtern() ? 'Bentlakó-bejáró' :
($user->isExtern() ? 'Bejáró' : ($user->isAlumni() ? "Alumni" : ($user->isTenant() ? "Vendég" : "")))),
$user->getStatus($this->semester)?->translatedStatus(),
$user->email,
$user->educationalInformation?->email,
Expand Down
3 changes: 2 additions & 1 deletion app/Exports/UsersSheets/SemesterEvaluationExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public function map($evaluation): array
$user->educationalInformation?->neptun,
implode(" \n", $user->faculties->pluck('name')->toArray()),
implode(" \n", $user->workshops->pluck('name')->toArray()),
$user->isResident() ? 'Bentlakó' : ($user->isExtern() ? 'Bejáró' : ($user->isAlumni() ? "Alumni" : ($user->isTenant() ? "Vendég" : ""))),
$user->isResident() ? 'Bentlakó' : ($user->isResidentExtern() ? 'Bentlakó-bejáró' :
($user->isExtern() ? 'Bejáró' : ($user->isAlumni() ? "Alumni" : ($user->isTenant() ? "Vendég" : "")))),
$evaluation->resign_residency ? 'Igen' : '',
$user->getStatus($evaluation->semester)?->translatedStatus(),
$user->getStatus($evaluation->semester->succ())?->translatedStatus(),
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Dormitory/RoomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function index()
public function modify()
{
$this->authorize('updateAny', Room::class);
$users = User::collegist()->active()->get(); // externs too (to solve the resident-extern problem temporarily)
$users = User::active()->resides()->get();
$tenants = User::currentTenant()->get();
$users = $users->concat($tenants)->unique();
$rooms = Room::with('users')->get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function store(Request $request)
return back()->with('error', "A státusz megadása kötelező!")->with('section', $request->section);
}
$user->setStatusFor(Semester::next(), $request->next_status, $request->next_status_note);
if ($request->has('resign_residency') && $user->isResident()) {
if ($request->has('resign_residency') && $user->resides()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resides() is a scope, which I think returns a query builder (test it in tinker). Check if these checks work as expected. In the tests it also seems fine, just double check it, I would not call scopes this way. I would rather make it a simple function

$user->setExtern();
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Secretariat/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public function tenantToApplicant()
$user->personalInformation()->update(['tenant_until' => null]);
$user->update(['verified' => false]);
$user->removeRole(Role::get(Role::TENANT));
$user->setExtern();
$user->setResidentExtern();
$user->application()->create();
Cache::forget('collegists');
return back();
Expand Down
5 changes: 4 additions & 1 deletion app/Models/ApplicationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
return $this->belongsTo('App\Models\User')->withoutGlobalScope('verified');
}

/**
* Lists the files belonging to the application form.
*/
public function files(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany('App\Models\File');
Expand Down Expand Up @@ -286,7 +289,7 @@ public function missingData(): array
$missingData[] = 'Megjelölt kar';
}

if (!$user->isResident() && !$user->isExtern()) {
if (!$user->resides() && !$user->isExtern()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

$missingData[] = 'Megjelölt collegista státusz';
}

Expand Down
2 changes: 2 additions & 0 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ class Role extends Model

//collegist related roles
public const RESIDENT = 'resident';
public const RESIDENT_EXTERN = 'resident-extern';
public const EXTERN = 'extern';
public const STATUSES = [self::RESIDENT, self::RESIDENT_EXTERN, self::EXTERN];

// all roles
public const ALL = [
Expand Down
62 changes: 61 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
* @method collegist()
* @method active()
* @method resident()
* @method residentExtern()
* @method extern()
* @method currentTenant()
* @method hasToPayKKTNetregInSemester(int $semester_id)
Expand Down Expand Up @@ -533,6 +534,28 @@ public function scopeResident(Builder $query): Builder
return $query->withRole(Role::COLLEGIST, Role::RESIDENT);
}

/**
* Scope a query to only include resident-extern users.
*
* @param Builder $query
* @return Builder
*/
public function scopeResidentExtern(Builder $query): Builder
{
return $query->withRole(Role::COLLEGIST, RoleObject::firstWhere('name', Role::RESIDENT_EXTERN));
}

/**
* Scope a query to only include users who are either residents or resident-externs.
*
* @param Builder $query
* @return Builder
*/
public function scopeResides(Builder $query): Builder
{
return $query->resident()->orWhere->residentExtern();
}

/**
* Scope a query to only include extern users.
*
Expand Down Expand Up @@ -682,7 +705,7 @@ public function isCollegist($alumni = true): bool
}

/**
* Attach collegist role as extern or resident.
* Attach collegist role as extern, resident-extern or resident.
* If the user is already a collegist, the object is updated.
*/
public function setCollegist($objectName): void
Expand Down Expand Up @@ -721,6 +744,43 @@ public function setResident(): void
$this->setCollegist(Role::RESIDENT);
}

/**
* Decides if the user is a resident-extern currently.
*
* @return bool
*/
public function isResidentExtern(): bool
{
//Is this 'if' for applications? Maybe it could be deleted then.
if($this->verified == false) {
return $this->roles()
->where('role_id', Role::collegist()->id)
->where('object_id', RoleObject::firstWhere('name', Role::RESIDENT_EXTERN)->id)
->exists();
}
return $this->hasRole([Role::COLLEGIST => Role::RESIDENT_EXTERN]);
}

/**
* Set the collegist to be resident-extern.
* Only applies for collegists.
*/
public function setResidentExtern(): void
{
$this->setCollegist(Role::RESIDENT_EXTERN);
}

/**
* Decides if the user is a resident or resident-extern.
* Returns true if the user is a collegist
* who effectively lives in the building
* (so has either of the two roles).
*/
public function resides(): bool
{
return $this->isResident() || $this->isResidentExtern();
}

/**
* Decides if the user is an extern collegist currently.
*
Expand Down
22 changes: 22 additions & 0 deletions app/Models/Workshop.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,37 @@ class Workshop extends Model
self::TORTENESZ,
];

/**
* Lists all members of the workshop.
*/
public function users()
{
return $this->belongsToMany(User::class, 'workshop_users');
}

/**
* Lists the workshop's residents.
*/
public function residents()
{
return $this->users->filter(function ($user, $key) {
return $user->isResident();
});
}

/**
* Lists the workshop's resident-externs.
*/
public function residentExterns()
{
return $this->users->filter(function ($user, $key) {
return $user->isResidentExtern();
});
}

/**
* Lists the workshop's externs.
*/
public function externs()
{
return $this->users->filter(function ($user, $key) {
Expand All @@ -97,6 +116,9 @@ public function balance(int $semester = null): ?WorkshopBalance
return $this->balances()->firstWhere('semester_id', $semester ?? Semester::current()->id);
}

/**
* Returns the color of the workshop's badge to be displayed.
*/
public function color()
{
switch ($this->name) {
Expand Down
3 changes: 2 additions & 1 deletion app/Models/WorkshopBalance.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function workshop()
}

/**
* Returns the semester the balance has been calculated for.
* Returns the semester the balance belongs to.
*/
public function semester()
{
Expand Down Expand Up @@ -113,6 +113,7 @@ public static function generateBalances(
$amount *= $workshop_balance_resident;
$resident++;
} else {
// resident-externs belong here
$amount *= $workshop_balance_extern;
$extern++;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use App\Models\Role;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::table('role_objects')->insert([
'role_id' => Role::where('name', 'collegist')->first()->id,
'name' => 'resident-extern'
]);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::table('role_objects')->where([
'role_id' => Role::where('name', 'collegist')->first()->id,
'name' => 'resident-extern'
])->delete();
}
};
5 changes: 1 addition & 4 deletions database/seeders/UsersTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ private function createCollegist($user)
$user->roles()->attach(
Role::get(Role::COLLEGIST)->id,
[
'object_id' => rand(
RoleObject::firstWhere('name', 'resident')->id,
RoleObject::firstWhere('name', 'extern')->id
)
'object_id' => RoleObject::firstWhere('name', Role::STATUSES[rand(0, count(Role::STATUSES) - 1)])->id
]
);
$user->educationalInformation()->save(EducationalInformation::factory()->make(['user_id' => $user->id]));
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/role.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'printer' => 'Printer',
'reception' => 'Reception desk',
'resident' => 'Resident',
'resident-extern' => 'Resident-extern',
'role_attached' => 'New role: :role',
'role_detached' => 'Role detached: :role',
'roles' => 'Roles',
Expand Down
1 change: 1 addition & 0 deletions resources/lang/hu/role.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'printer' => 'Nyomtató',
'reception' => 'Porta',
'resident' => 'Bentlakó',
'resident-extern' => 'Bentlakó-bejáró',
'role_attached' => 'Új jogosultság: :role',
'role_detached' => 'Eltávolított jogosultság: :role',
'roles' => 'Jogosultságok',
Expand Down
8 changes: 7 additions & 1 deletion resources/views/auth/application/application.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@
@lang('role.resident')
</span>
@endif
@if ($user->isResidentExtern())
<span class="new badge cyan darken-4 tag" style="float:none;padding:4px;margin:0 10px 0px 2px;"
data-badge-caption="">
@lang('role.resident-extern')
</span>
@endif
@if ($user->isExtern())
<span class="new badge coli orange tag"
style="float:none;padding:4px;margin:0 10px 0px 2px;"
data-badge-caption="">
@lang('role.extern')
</span>
@endif
@if(!$user->isResident() && !$user->isExtern())
@if(!$user->resides() && !$user->isExtern())
<span style="font-style:italic;color:red">hiányzó státusz</span>
@endif
</p>
Expand Down
1 change: 1 addition & 0 deletions resources/views/auth/application/questions.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<span>@lang('role.resident')</span>
</label>
</p>
{{-- resident-extern status is irrelevant here --}}
<p>
@php $checked = old('status') ? old('status') == 'extern' : $user->isExtern() @endphp
<label>
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ public function test_set_collegist()
$user->setExtern();

$this->assertTrue($user->isExtern());
$this->assertFalse($user->resides());
$this->assertTrue($user->hasRole(Role::get(Role::COLLEGIST)));
$this->assertTrue($user->hasRole([Role::COLLEGIST => Role::EXTERN]));

$user->setCollegist(Role::RESIDENT);

$this->assertTrue($user->isResident());
$this->assertTrue($user->resides());
$this->assertTrue($user->hasRole(Role::get(Role::COLLEGIST)));
$this->assertTrue($user->hasRole([Role::COLLEGIST => Role::RESIDENT]));

$user->setCollegist(Role::RESIDENT_EXTERN);
$this->assertTrue($user->isResidentExtern());
$this->assertTrue($user->resides());
$this->assertTrue($user->hasRole(Role::get(Role::COLLEGIST)));
$this->assertTrue($user->hasRole([Role::COLLEGIST => Role::RESIDENT_EXTERN]));

}

Expand Down