Open VanMeerbergenRenaud opened 3 months ago
Ajout d'une méthode store dans AttendanceController:
#[NoReturn] public function store(): void
{
$data = Validator::check([
'jiri_id' => 'required',
'contacts' => 'required',
'role' => 'required',
]);
foreach ($data['contacts'] as $contact_id) {
$role_key = 'role-' . $contact_id;
$role = $data[$role_key];
$this->attendance->create([
'jiri_id' => $data['jiri_id'],
'contact_id' => $contact_id,
'role' => $role
]);
}
Response::redirect('/jiri?id=' . $data['jiri_id']);
}
Ajout de la route avec la méthode post dans nos attendances :
$router->post('/attendance', [AttendanceController::class, 'store'])->only('auth')->csrf();
Ajout du code dans la view jiri/edit.view.php :
<!-- If no attendances -->
<?php if (empty($jiri->students) && empty($jiri->evaluators)): ?>
<!-- Form to add attendances -->
<form action="/attendance" method="post">
<?php csrf_token() ?>
<input type="hidden" name="jiri_id" value="<?= $jiri->id ?>">
<!-- No attendances-->
<legend>NB : Aucun participant pour le moment</legend>
<div>
<?php if (!empty($contacts)): ?>
<fieldset>
<legend class="font-bold mb-2 uppercase">Les participants</legend>
<div class="flex flex-col gap-2">
<?php
/** @var array $contacts */
foreach ($contacts as $contact): ?>
<div>
<input id="c-<?= $contact->id ?>"
type="checkbox"
name="contacts[]"
class="h-4 w-4"
value="<?= $contact->id ?>"
>
<label for="c-<?= $contact->id ?>"><?= $contact->name ?></label>
<select name="role-<?= $contact->id ?>"
id="role-<?= $contact->id ?>"
class="mx-2 p-2 rounded">
<option value="student">Étudiant</option>
<option value="evaluator">Évaluateur</option>
</select>
<label for="role-<?= $contact->id ?>"
class="font-bold sr-only">Rôle</label>
</div>
<?php endforeach; ?>
</div>
</fieldset>
<?php endif; ?>
</div>
<!-- Button to add attendances to the jiri -->
<div>
<?php component('forms.controls.button', ['text' => 'Ajouter ces contacts au jiri']) ?>
</div>
</form>
<?php endif; ?>
A. Ajouter la route correspondante :
$router->delete('/attendance', [AttendanceController::class, 'destroy'])->only('auth')->csrf();
B. Dans le controller AttendanceController :
#[NoReturn] public function destroy(): void
{
$data = Validator::check([
'jiri_id' => 'required',
'contact_id' => 'required',
]);
$this->attendance->delete($data);
Response::redirect('/jiri/edit?id='.$data['jiri_id']);
}
C. Dans le model Attendance.php :
public function delete(array|string $data): bool
{
$sql = <<<SQL
DELETE FROM $this->table
WHERE jiri_id = :jiri_id
AND contact_id = :contact_id
SQL;
$statement = $this->prepare($sql);
$statement->bindValue('jiri_id', $data['jiri_id']);
$statement->bindValue('contact_id', $data['contact_id']);
return $statement->execute();
}
A. Pour indiquer qu'aucune attendance n'a été crée :
<?php if (empty($jiri->students) && empty($jiri->evaluators)): ?>
<legend>NB : Aucun participant pour le moment</legend>
<?php endif; ?>
B. Ajouter une variable $distinctsContacts qui va sélectionner uniquement les contacts qui ne sont pas encore des attendances :
$distinctsContacts = [];
$distinctsContacts = $this->contact->distinctsContacts($jiri->id, Auth::id());
C. Création de la fonction distinctsContacts() dans notre model Contact.php:
public function distinctsContacts(int $jiri_id, int $user_id): array
{
// Select only the contacts that are not in the attendance table for the given jiri_id for the authorized user
$sql = <<<SQL
SELECT * FROM contacts
WHERE id NOT IN (
SELECT contact_id FROM attendances
WHERE jiri_id = :jiri_id
)
AND user_id = :user_id
ORDER BY name;
SQL;
$statement = $this->prepare($sql);
$statement->bindValue(':jiri_id', $jiri_id);
$statement->bindValue(':user_id', $user_id);
$statement->execute();
return $statement->fetchAll();
}
In the 'views/jiris/edit.view.php' file :
<!-- Add the feature to the role of a contact --> Same code as the show.view.php
In the JiriController add the
$jiri->students and $jiri->evaluators
into the functionChange the redirection in the Attendance controller :
Response::redirect('/jiri/edit?id='.$data['jiri_id']);