|
2 | 2 | // Copyright (c) 2022-present Instituto Superior Técnico.
|
3 | 3 | // Distributed under the MIT license that can be found in the LICENSE file.
|
4 | 4 |
|
| 5 | +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
| 6 | +use Symfony\Component\Form\Extension\Core\Type\FormType; |
| 7 | +use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
| 8 | + |
5 | 9 | auth_require_at_least(ROLE_PROF);
|
6 | 10 |
|
7 | 11 | $year = get_current_year();
|
8 | 12 | $shifts = db_fetch_shifts($year);
|
9 |
| -$profs = db_get_all_profs(true); |
10 | 13 |
|
11 |
| -if (isset($_POST['submit'])) { |
12 |
| - foreach ($shifts as $shift) { |
13 |
| - $var = "shift_$shift->id"; |
14 |
| - if (!empty($_POST[$var])) { |
15 |
| - $user = db_fetch_user($_POST[$var]); |
16 |
| - if (!$user || !$user->roleAtLeast(ROLE_TA)) |
17 |
| - die("Unknown user"); |
18 |
| - $shift->prof = $user; |
19 |
| - } |
20 |
| - } |
21 |
| - db_flush(); |
22 |
| - echo "<p>Saved!</p>"; |
23 |
| -} |
| 14 | +$form = $formFactory->createBuilder(FormType::class); |
24 | 15 |
|
25 |
| -foreach ($profs as $prof) { |
26 |
| - echo "<td>{$prof->shortName()}</td>"; |
| 16 | +foreach (db_get_all_profs(true) as $prof) { |
| 17 | + $profs[$prof->shortName()] = $prof->id; |
27 | 18 | }
|
28 | 19 |
|
29 | 20 | foreach ($shifts as $shift) {
|
30 |
| - echo "<tr><td>", htmlspecialchars($shift->name), "</td>"; |
31 |
| - foreach ($profs as $prof) { |
32 |
| - $selected = ''; |
33 |
| - if ($shift->prof == $prof) |
34 |
| - $selected = ' checked'; |
35 |
| - echo "<td><input type=\"radio\" name=\"shift_$shift->id\" value=\"", |
36 |
| - htmlspecialchars($prof->id), "\"$selected></td>"; |
| 21 | + $form->add("shift_$shift->id", ChoiceType::class, [ |
| 22 | + 'label' => $shift->name, |
| 23 | + 'choices' => $profs, |
| 24 | + 'data' => $shift->prof ? $shift->prof->id : null, |
| 25 | + ]); |
| 26 | +} |
| 27 | + |
| 28 | +$form->add('submit', SubmitType::class); |
| 29 | +$form = $form->getForm(); |
| 30 | + |
| 31 | +$form->handleRequest($request); |
| 32 | + |
| 33 | +if ($form->isSubmitted() && $form->isValid()) { |
| 34 | + foreach ($shifts as $shift) { |
| 35 | + $var = "shift_$shift->id"; |
| 36 | + $user = db_fetch_user($form->get($var)->getData()); |
| 37 | + if (!$user || !$user->roleAtLeast(ROLE_TA)) |
| 38 | + die("Unknown user"); |
| 39 | + $shift->prof = $user; |
37 | 40 | }
|
| 41 | + $success_message = 'Saved!'; |
38 | 42 | }
|
0 commit comments