src/Controller/Teacher/DefaultController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Teacher;
  3. use App\Entity\Core\Classroom\Classroom;
  4. use App\Entity\Core\Classroom\ClassroomStudent;
  5. use App\Entity\User\User;
  6. use App\Services\Core\ActivityDisplayService;
  7. use App\Services\Core\ActivityRoutingService;
  8. use App\Services\Core\AuthService;
  9. use App\Services\Core\EventLogger;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Yaml\Yaml;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. use App\Form\Teacher\TeacherType;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. class DefaultController extends AbstractController
  20. {
  21. public function __construct(
  22. private string $scope,
  23. private string $rootDir,
  24. private EntityManagerInterface $em,
  25. private TranslatorInterface $translator
  26. ) {
  27. }
  28. #[Route('/teacher/index/{one}/{two}/{three}/{four}/{five}', name: 'a_ba_cus_teacher_homepage', defaults: ['one' => null, 'two' => null, 'three' => null, 'four' => null, 'five' => null])]
  29. public function index(ActivityDisplayService $activityDisplayService, ActivityRoutingService $activityRoutingService): Response
  30. {
  31. /** @var User $teacher */
  32. $teacher = $this->getUser();
  33. $config = Yaml::parseFile($this->rootDir . '/config/' . $this->scope . '.yml');
  34. $props = [
  35. 'brand' => $this->translator->trans("brand", [], $this->scope),
  36. 'activityDisplays' => $activityDisplayService->getFull($teacher),
  37. 'activityCreateRoutes' => $activityRoutingService->getCreateRoutes(),
  38. 'demos' => $config["demo"] ?? null,
  39. 'userId' => $teacher->getId(),
  40. 'reactRoutes' => $config["react_routes"]["teacher"]["index"],
  41. "videoCollectionSegments" => $config["video_collection_segments"] ?? null,
  42. ];
  43. return $this->render('Teacher/Default/index.html.twig', [
  44. 'props' => $props,
  45. ]);
  46. }
  47. #[Route('/teacher/help', name: 'teacher_help')]
  48. public function help(EventLogger $eventLogger): Response
  49. {
  50. /** @var User $teacher */
  51. $teacher = $this->getUser();
  52. $eventLogger->log('teacher_clicks_helppage', $teacher);
  53. return $this->render('Teacher/Default/help.html.twig');
  54. }
  55. /**
  56. * @return RedirectResponse|Response
  57. */
  58. #[Route('/teacher/profile', name: 'teacher_edit_profile')]
  59. public function editProfile(AuthService $authService, Request $request)
  60. {
  61. $classroomRepo = $this->em->getRepository(Classroom::class);
  62. $studentRepo = $this->em->getRepository(ClassroomStudent::class);
  63. /** @var User $teacher */
  64. $teacher = $this->getUser();
  65. $form = $this->createForm(TeacherType::class, $teacher);
  66. $form->handleRequest($request);
  67. if ($form->isSubmitted() && $form->isValid()) {
  68. $submittedData = $request->request->get('teacher');
  69. $teacherDemoClassroom = $classroomRepo->getTeacherDemoClassroom($teacher);
  70. if ($teacherDemoClassroom) {
  71. $classroomStudents = $studentRepo->findStudentsInClassroom($teacherDemoClassroom->getId());
  72. foreach ($classroomStudents as $classroomStudent) {
  73. $student = $classroomStudent->getStudent();
  74. if ($student->isDemo()) {
  75. $student->setInstitution($teacher->getInstitution());
  76. if( isset( $submittedData['class_level'] ) ){
  77. $student->setClassLevel( (int)$submittedData['class_level'] );
  78. }
  79. }
  80. }
  81. }
  82. $this->em->flush();
  83. $this->addFlash(
  84. 'notice',
  85. $this->translator->trans('Din brugerprofil er blevet opdateret 👍')
  86. );
  87. return $this->redirectToRoute('teacher_edit_profile');
  88. }
  89. return $this->render('Teacher/Default/edit_profile.html.twig', [
  90. 'form' => $form->createView(),
  91. ]);
  92. }
  93. }