diff --git a/CSS/Element/button.css b/CSS/Element/button.css index eed5813..67b854d 100644 --- a/CSS/Element/button.css +++ b/CSS/Element/button.css @@ -18,6 +18,13 @@ cursor: pointer; } +.btn-link { + text-decoration: underline; + background-color: transparent; + color: var(--brand-white); + border: none; +} + .btn-primary { background: var(--brand-primary); border-radius: 3px; diff --git a/Controller/UserController.php b/Controller/UserController.php index fa28575..49661d8 100644 --- a/Controller/UserController.php +++ b/Controller/UserController.php @@ -267,7 +267,7 @@ class UserController{ $currentUser = $this->db->getUserById($userId); if(!$currentUser){ - throw new \Exception("User nicht gefunden"); + new \Blog\Library\ErrorMsg("User nicht gefunden"); } $validData = [ @@ -296,7 +296,7 @@ class UserController{ $currentUser = $this->db->getUserById($userId); if (!$currentUser) { - throw new \Exception('User nicht gefunden'); + new \Blog\Library\ErrorMsg('User nicht gefunden'); } $submitted = [ @@ -388,5 +388,20 @@ class UserController{ } + public function enroll() { + $courseId = $_POST['id'] ?? null; + $userId = $_SESSION['user_id'] ?? null; + if ($userId === null) { + new \Blog\Library\ErrorMsg("Nicht eingeloggt."); + } + + if ($courseId === null) { + new \Blog\Library\ErrorMsg("Kein Kurs ausgewählt"); + } + + $this->db->enroll($courseId, $userId); + + $this->view->setDoMethodName("showEnrollmentConfirmation"); + } } \ No newline at end of file diff --git a/Model/UserModel.php b/Model/UserModel.php index 2c7548a..bcc7863 100644 --- a/Model/UserModel.php +++ b/Model/UserModel.php @@ -192,15 +192,32 @@ class UserModel extends Database } public function getAllCourses() { $pdo = $this->linkDB(); + $userId = $_SESSION['user_id'] ?? null; + + if ($userId === null) { + $sql = "SELECT + k.id, k.name, k.preis, k.dauer, k.rabatt, k.kategorie, k.beschreibung, k.ort_id, + o.stadt, o.strasse, o.plz, b.note, b.kommentar, + 0 AS isTeilnehmer + FROM kurs AS k + JOIN ort AS o ON o.id = k.ort_id + LEFT JOIN bewertungen AS b ON b.kurs_id = k.id"; + $params = []; + } else { + $sql = "SELECT + k.id, k.name, k.preis, k.dauer, k.rabatt, k.kategorie, k.beschreibung, k.ort_id, + o.stadt, o.strasse, o.plz, b.note, b.kommentar, + CASE WHEN ku.user_id IS NULL THEN 0 ELSE 1 END AS isTeilnehmer + FROM kurs AS k + JOIN ort AS o ON o.id = k.ort_id + LEFT JOIN bewertungen AS b ON b.kurs_id = k.id + LEFT JOIN kurs_user AS ku ON ku.kurs_id = k.id AND ku.user_id = :userId"; + $params = ['userId' => $userId]; + } - $sql = "SELECT k.id, k.name, k.preis, k.dauer, k.rabatt, k.kategorie, k.beschreibung, k.ort_id, - o.stadt, o.strasse, o.plz, b.note, b.kommentar - FROM kurs AS k - JOIN ort AS o ON o.id = k.ort_id - LEFT JOIN bewertungen AS b ON b.kurs_id = k.id"; try { $sth = $pdo->prepare($sql); - $sth->execute(); + $sth->execute($params); return $sth->fetchAll(\PDO::FETCH_ASSOC); } catch (PDOException $e) { new \Blog\Library\ErrorMsg("Fehler beim Lesen der Daten.", $e); @@ -208,7 +225,6 @@ class UserModel extends Database } } - public function updateCourse($course) { $pdo = $this->linkDB(); @@ -278,4 +294,35 @@ class UserModel extends Database return true; } + + public function enroll($courseId, $userId) { + $pdo = $this->linkDB(); + + try { + $checkSql = "SELECT COUNT(*) FROM kurs_user WHERE user_id = :user_id AND kurs_id = :kurs_id"; + $checkStmt = $pdo->prepare($checkSql); + $checkStmt->execute([ + ':user_id' => $userId, + ':kurs_id' => $courseId + ]); + + if ($checkStmt->fetchColumn() > 0) { + return false; + } + + $insertSql = "INSERT INTO kurs_user (user_id, kurs_id) VALUES (:user_id, :kurs_id)"; + $insertStmt = $pdo->prepare($insertSql); + $insertStmt->execute([ + ':user_id' => $userId, + ':kurs_id' => $courseId + ]); + + } catch (PDOException $e) { + new \Blog\Library\ErrorMsg("Fehler beim Einschreiben in den Kurs.", $e); + die; + } + + return true; + } + } \ No newline at end of file diff --git a/Views/User/showEnrollmentConfirmation.phtml b/Views/User/showEnrollmentConfirmation.phtml new file mode 100644 index 0000000..2d53d50 --- /dev/null +++ b/Views/User/showEnrollmentConfirmation.phtml @@ -0,0 +1,12 @@ + + +
Erfolgreich aufgenommen!
+Weiter +