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 +
+ + + + \ No newline at end of file diff --git a/Views/Welcome/showWelcome.phtml b/Views/Welcome/showWelcome.phtml index 58c50fc..fef246b 100644 --- a/Views/Welcome/showWelcome.phtml +++ b/Views/Welcome/showWelcome.phtml @@ -60,46 +60,37 @@ $location = $_GET['location'] ?? ''; if (!empty($filteredKurse)) { echo '
'; foreach ($filteredKurse as $kurs) { - $courseCard = $doc->createElement('div'); - $courseCard->setAttribute('class', 'course-card'); + foreach ($filteredKurse as $kurs) { + $id = htmlspecialchars($kurs['id']); + $note = htmlspecialchars($kurs['note'] ?? 'Keine Bewertung') . ' ★'; + $name = htmlspecialchars($kurs['name']); + $address = htmlspecialchars($kurs['strasse'] . ', ' . $kurs['stadt'] . ' ' . $kurs['plz']); + $preis = htmlspecialchars($kurs['preis']) . ' €'; + $kategorie = htmlspecialchars($kurs['kategorie'] ?? 'Keine Kategorie'); - $courseImage = $doc->createElement('div'); - $courseImage->setAttribute('class', 'course-image'); - $courseCard->appendChild($courseImage); - - $courseContent = $doc->createElement('div'); - $courseContent->setAttribute('class', 'course-content'); - $courseCard->appendChild($courseContent); - - $courseLeft = $doc->createElement('div'); - $courseLeft->setAttribute('class', 'course-left'); - $courseContent->appendChild($courseLeft); - - $note = $doc->createElement('div', htmlspecialchars($kurs['note'] ?? 'Keine Bewertung') . ' ★'); - $courseLeft->appendChild($note); - - $name = $doc->createElement('div', htmlspecialchars($kurs['name'])); - $courseLeft->appendChild($name); - - $address = $doc->createElement('div', htmlspecialchars($kurs['strasse'] . ', ' . $kurs['stadt'] . ' ' . $kurs['plz'])); - $courseLeft->appendChild($address); - - $courseRight = $doc->createElement('div'); - $courseRight->setAttribute('class', 'course-right'); - $courseContent->appendChild($courseRight); - - $price = $doc->createElement('div', htmlspecialchars($kurs['preis']) . ' €'); - $courseRight->appendChild($price); - - $category = $doc->createElement('div', htmlspecialchars($kurs['kategorie'] ?? 'Keine Kategorie')); - $courseRight->appendChild($category); - - $editLink = $doc->createElement('a', "Bearbeiten"); - $editLink->setAttribute('href', '?controller=User&do=showUserAccountPage&id=' . $kurs['id']); - $editLink->setAttribute('class', 'course-card-link'); - $courseRight->appendChild($editLink); - - echo $doc->saveHTML($courseCard); + echo << +
+
+
+
$note
+
$name
+
$address
+
+
+
$preis
+
$kategorie
+
+ + + + +
+
+
+
+ HTML; + } } echo ''; } else { @@ -111,6 +102,8 @@ $location = $_GET['location'] ?? ''; + +