Skip to content

Commit

Permalink
added switching roles for admins
Browse files Browse the repository at this point in the history
  • Loading branch information
Lung committed Dec 9, 2023
1 parent 9892041 commit 797ce80
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Application/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ public function addRoutesInto(App $app): App
$app->post('/changeDetails', AdminController::class . '::changeParticipantDetails')
->setName('admin-change-participant-details');
});

$app->group('/changeRole/{participantId}', function (RouteCollectorProxy $app) {
$app->get('/show', AdminController::class . '::showRole')
->setName('admin-show-role');

$app->post('/change', AdminController::class . '::changeRole')
->setName('admin-change-role');
});

$app->get('/showStats', AdminController::class . '::showStats')
->setName('admin-show-stats');
Expand Down
25 changes: 25 additions & 0 deletions src/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use kissj\Event\EventType\Nsj\EventTypeNsj;
use kissj\Event\EventType\Wsj\EventTypeWsj;
use kissj\Orm\EntityDatetime;
use kissj\Participant\ParticipantRole;

/**
* @property int $id
Expand Down Expand Up @@ -93,4 +94,28 @@ public function canRegistrationBeLocked(): bool
{
return $this->startRegistration <= DateTimeUtils::getDateTime();
}

/**
* @return ParticipantRole[]
*/
public function getAvailableRoles(): array
{
$roles = [];
if ($this->allowPatrols) {
$roles[] = ParticipantRole::PatrolLeader;
$roles[] = ParticipantRole::PatrolParticipant;
}
if ($this->allowTroops) {
$roles[] = ParticipantRole::TroopLeader;
$roles[] = ParticipantRole::TroopParticipant;
}
if ($this->allowIsts) {
$roles[] = ParticipantRole::Ist;
}
if ($this->allowGuests) {
$roles[] = ParticipantRole::Guest;
}

return $roles;
}
}
33 changes: 33 additions & 0 deletions src/Participant/Admin/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,39 @@ public function changeParticipantDetails(Request $request, Response $response, i
'admin-show-stats',
);
}

public function showRole(Response $response, int $participantId, Event $event): Response
{
$participant = $this->participantRepository->get($participantId);

return $this->view->render($response, 'admin/changeRole.twig', [
'person' => $participant,
'roles' => $event->getAvailableRoles(),
]);
}

public function changeRole(Request $request, Response $response, int $participantId, Event $event): Response
{
$participant = $this->participantRepository->get($participantId);
$roleFromBody = $this->getParameterFromBody($request, 'role');

$success = $this->participantService->tryChangeRoleWithMessages($roleFromBody, $participant, $event);

if ($success) {
return $this->redirect(
$request,
$response,
'admin-show-open',
);
}

return $this->redirect(
$request,
$response,
'admin-show-role',
['participantId' => (string)$participantId],
);
}

public function generateMorePayments(Request $request, Response $response, Event $event): Response
{
Expand Down
46 changes: 46 additions & 0 deletions src/Participant/ParticipantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use kissj\FlashMessages\FlashMessagesBySession;
use kissj\Mailer\PhpMailerWrapper;
use kissj\Participant\Guest\Guest;
use kissj\Participant\Patrol\PatrolLeader;
use kissj\Participant\Troop\TroopLeader;
use kissj\Participant\Troop\TroopParticipant;
use kissj\Payment\Payment;
Expand Down Expand Up @@ -455,4 +456,49 @@ public function setAsEntered(Participant $participant): Participant

return $participant;
}

public function tryChangeRoleWithMessages(string $roleFromBody, Participant $participant, Event $event): bool
{
$role = ParticipantRole::tryFrom($roleFromBody);
if ($role === null || !in_array($role, $event->getAvailableRoles(), true)) {
$this->flashMessages->error($this->translator->trans('flash.error.roleNotValid'));

return false;
}

if ($participant instanceof PatrolLeader) {
if ($participant->getPatrolParticipantsCount() > 0) {
$this->flashMessages->warning($this->translator->trans('flash.warning.patrolHasParticipantsCannotChangeRole'));

return false;
}
}

if ($participant instanceof TroopLeader) {
if ($participant->getTroopParticipantsCount() > 0) {
$this->flashMessages->warning($this->translator->trans('flash.warning.troopHasParticipantsCannotChangeRole'));

return false;
}
}

if ($role === $participant->role) {
$this->flashMessages->warning($this->translator->trans('flash.warning.sameRoleCannotChangeRole'));

return false;
}

if ($participant->getUserButNotNull()->status !== UserStatus::Open) {
$this->flashMessages->warning($this->translator->trans('flash.warning.notOpenCannotChangeRole'));

return false;
}

$participant->role = $role;
$this->participantRepository->persist($participant);

$this->flashMessages->success($this->translator->trans('flash.success.roleChanged'));

return true;
}
}
8 changes: 8 additions & 0 deletions src/Templates/cs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ stats-admin:
numberPatrolParticipantsTotal: "v patrole celkem"
paid: "Zaplacení účastníci akce"
generate-more-payments: "Vygenerovat více plateb"
changeRole: "Změnit roli"
role: "Role"
open-admin:
open: "Neuzamčení účastníci na akci"
none: "Žádní účastníci s rolí"
Expand Down Expand Up @@ -443,6 +445,7 @@ flash:
troopParticipantTiedToTroopLeader: "Účastník úspěšně připojen do skupiny!"
troopTied: "Účastník úspěšně připojen do skupiny!"
participantUntied: "Účastník úspěšně odepnut od skupiny!"
roleChanged: "Role účastníka úspěšně změněna!"
warning:
noLock: "Registraci nelze uzamknout - nějaké informace jsou špatně vyplněné nebo chybí (pravděpodobně nějaké datum)"
plWrongData: "Nemůžeme uzamknout registraci - prosím oprav svoje údaje (nejspíš email nebo nějaké datum)"
Expand Down Expand Up @@ -484,6 +487,10 @@ flash:
troopParticipantHasTroopTieNotPossible: "Účastník již skupinu má, připojení do skupiny není možné"
troopParticipantNotFoundUntieNotPossible: "Účastník nebyl nalezen, odpojení od skupiny není možné"
troopParticipantPaidUntieNotPossible: "Účastník má zaplacenou registraci, odpojení od skupiny není možné"
patrolHasParticipantsCannotChangeRole: "Patrola má účastníky, změna role není možná"
troopHasParticipantsCannotChangeRole: "Skupina má účastníky, změna role není možná"
sameRoleCannotChangeRole: "Účastník už tuto roli má, změna role není možná"
notOpenCannotChangeRole: "Registrace účastníka není odemknutá, změna role není možná"
error:
adminOnly: "Sorry bro, pouze pro administrátory"
plOnly: "Pardon, nejsi přihlášený jako vedoucí patroly"
Expand All @@ -499,3 +506,4 @@ flash:
tieParticipantToLeaderError: "Připojení účastníka do skupiny se nepovedlo :("
skautisUserNotLoggedIn: "SkautIS hlásí, že účastník není přihlášený. Zkus se prosím odhlásit a znovu přihlásit"
skautisUserError: "Selhalo stažení dat ze skautISu. Zkus se prosím odhlásit a znovu přihlásit"
roleNotValid: "Tato role není pro tuto akci platná"
8 changes: 8 additions & 0 deletions src/Templates/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ stats-admin:
numberPatrolParticipantsTotal: "participants in total"
paid: "Paid participants for event"
generate-more-payments: "Generate more payments"
changeRole: "Change role"
role: "Role"
open-admin:
open: "Unlocked participants on event"
none: "No participants with role"
Expand Down Expand Up @@ -443,6 +445,7 @@ flash:
troopParticipantTiedToTroopLeader: "Troop participant is successfully tied to troop leader"
troopTied: "Participant successfully added to the group!"
participantUntied: "Participant successfully untied from the group!"
roleChanged: "Role successfully changed!"
warning:
noLock: "Cannot lock the registration - some details are wrong or missing (probably email or date)"
plWrongData: "Cannot lock the registration - some details are wrong or missing (probably email or date)"
Expand Down Expand Up @@ -484,6 +487,10 @@ flash:
troopParticipantHasTroopTieNotPossible: "Troop participant is already tied to a group, tie is not possible"
troopParticipantNotFoundUntieNotPossible: "Participant not found, untie is not possible"
troopParticipantPaidUntieNotPossible: "Participant has paid registration, untie is not possible"
patrolHasParticipantsCannotChangeRole: "Patrol has participants, role cannot be changed"
troopHasParticipantsCannotChangeRole: "Troop has participants, role cannot be changed"
sameRoleCannotChangeRole: "Cannot change role to the same role"
notOpenCannotChangeRole: "Registration is not unlocked, role cannot be changed"
error:
adminOnly: "Sorry bro, admins only"
plOnly: "Sorry, you are not logged as Patrol Leader"
Expand All @@ -499,3 +506,4 @@ flash:
tieParticipantToLeaderError: "Failed to tie participant to the group :("
skautisUserNotLoggedIn: "Skautis reports that the participant is not logged in. Please try to log out and log in again"
skautisUserError: "Failed to download data from Skautis. Please try to log out and log in again"
roleNotValid: "Role is not valid"
8 changes: 8 additions & 0 deletions src/Templates/sk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ stats-admin:
numberPatrolParticipantsTotal: "v patrole celkem"
paid: "Zaplacení účastníci akce"
generate-more-payments: "Vygenerovat více plateb"
changeRole: "Změnit roli"
role: "Role"
open-admin:
open: "Neuzamčení účastníci na akci"
none: "Žádní účastníci s rolí"
Expand Down Expand Up @@ -410,6 +412,7 @@ flash:
transfer: "Přesun platby proběhl úspěšně!"
troopTied: "Účastník úspešne pridaný do skupiny!"
participantUntied: "Účastník úspěšně odepnut od skupiny!"
roleChanged: "Rola účastníka úspěšně změněna!"
warning:
noLock: "Nie je možné uzamknúť registráciu - nejaký z údajov je nesprávny alebo chýbajúci (možno nejaký dátum)"
plWrongData: "Nie je možné uzamknúť registráciu - prosím oprav svoje údaje (nejspíš email nebo nějaké datum)"
Expand Down Expand Up @@ -441,6 +444,10 @@ flash:
troopParticipantHasTroopTieNotPossible: "Účastník už skupinu má, pridanie do skupiny nie je možné"
troopParticipantNotFoundUntieNotPossible: "Účastník nebyl nalezen, odpojení od skupiny není možné"
troopParticipantPaidUntieNotPossible: "Účastník má zaplacenou registraci, odpojení od skupiny není možné"
patrolHasParticipantsCannotChangeRole: "Patrola má účastníky, změna role není možná"
troopHasParticipantsCannotChangeRole: "Skupina má účastníky, změna role není možná"
sameRoleCannotChangeRole: "Účastník už tuto roli má, změna role není možná"
notOpenCannotChangeRole: "Registrace účastníka není odemknutá, změna role není možná"
error:
adminOnly: "Sorry bro, sem môžu len admini"
plOnly: "Prepáč, ale nie si prihlásený/á ako Patrol líder"
Expand All @@ -451,3 +458,4 @@ flash:
fioConnectionFailed: "Spojení do Fio banky selhalo, nejspíše kvůli špatně zadanému API klíči"
skautisUserNotLoggedIn: "Skautis hlásí, že účastník není přihlášený. Zkus se prosím odhlásit a znovu přihlásit"
skautisUserError: "Selhalo stažení dat ze Skautisu. Zkus se prosím odhlásit a znovu přihlásit"
roleNotValid: "Tato role není pro tuto akci platná"
39 changes: 39 additions & 0 deletions src/Templates/translatable/admin/changeRole.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% extends "_layout.twig" %}

{% block content %}
<div class="card card-half paid-theme">
<h2>{% trans %}stats-admin.changeRole{% endtrans %}</h2>
<div>
<h4>{{ person.getFullName }}</h4>
{% trans %}detail.email{% endtrans %}: <b>{{ person.getEmail }}</b><br/>
{% trans %}detail.notice{% endtrans %}: <b>{{ person.getNotes }}</b><br/>
{% include 'widgets/adminNotice.twig' with {'participantId': person.id} %}<br/>
</div>
<form
method="POST"
action="{{ url_for('admin-change-role', {'eventSlug': event.slug, 'participantId': person.id}) }}"
class="form-group"
>
<div class="form-group">
<label for="country">{% trans %}stats-admin.role{% endtrans %}:</label>
<select
id="role"
class="form-control form-wide"
name="role"
>
{% for role in roles %}
<option
value="{{ role.value }}"
{% if person.role == role %} selected="selected"{% endif %}
>{{ dump }}
{{ ('role.' ~ role.value)|trans }}
</option>
{% endfor %}
</select>
</div>
<input class="btn" type="submit" value="{% trans %}stats-admin.changeRole{% endtrans %}">
</form>
<a href="{{ url_for('admin-show-open', {'eventSlug': event.slug}) }}"
class="btn btn-grey btn-small">{% trans %}changeDetails.back{% endtrans %}</a>
</div>
{% endblock %}
8 changes: 7 additions & 1 deletion src/Templates/translatable/widgets/detailsMinimal.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@
{% endif %}{% if ca.notes %}
{% trans %}detail.notice{% endtrans %}: <b>{{ person.getNotes }}</b><br/>
{% endif %}
{% include 'widgets/adminNotice.twig' with {'participantId': person.id} %}
{% include 'widgets/adminNotice.twig' with {'participantId': person.id} %}<br/>
<a
href="{{ url_for('admin-show-role', {'participantId' : person.id, 'eventSlug': event.slug}) }}"
class="btn btn-small btn-mini"
>
{% trans %}stats-admin.changeRole{% endtrans %}
</a>

0 comments on commit 797ce80

Please sign in to comment.