From a6b44ecbf080ecde7474b6c81b5b56dc68909869 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Fri, 16 Aug 2024 06:25:57 +0000
Subject: [PATCH] fix misc errors on feedback element, fixes #4475

Closes #4475

Merge request studip/studip!3269
---
 app/controllers/course/feedback.php            | 18 ++++++++----------
 .../course/feedback/_add_edit_entry_form.php   | 13 ++++++++++---
 resources/assets/javascripts/feedback.js       |  8 +++++---
 3 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/app/controllers/course/feedback.php b/app/controllers/course/feedback.php
index c6eb9771be6..df600c7a1f5 100644
--- a/app/controllers/course/feedback.php
+++ b/app/controllers/course/feedback.php
@@ -85,26 +85,24 @@ class Course_FeedbackController extends AuthenticatedController
         if (!Feedback::hasRangeAccess($range_id, $range_type)) {
             throw new AccessDeniedException();
         } elseif ($this->create_perm) {
-            if (Request::bool('comment_only')) {
-                $mode = FeedbackElement::MODE_NO_RATING;
-                $commentable = 1;
-            } else {
-                $mode           = Request::int('mode', FeedbackElement::MODE_NO_RATING);
-                $commentable    = Request::bool('commentable');
+            $mode = FeedbackElement::MODE_NO_RATING;
+            $commentable = true;
+            if (!Request::bool('comment_only')) {
+                $mode = Request::int('mode', $mode);
+                $commentable = Request::bool('commentable', false);
             }
-            $feedback = FeedbackElement::build([
+            $feedback = FeedbackElement::create([
                 'range_id'          => $range_id,
                 'range_type'        => $range_type,
                 'user_id'           => $GLOBALS['user']->id,
                 'course_id'         => $this->course_id,
                 'question'          => trim(Request::get('question')),
                 'description'       =>  Studip\Markup::purifyHtml(Request::get('description')),
-                'results_visible'   => intval(Request::get('results_visible')),
+                'results_visible'   => Request::bool('results_visible', false),
                 'commentable'       => $commentable,
-                'anonymous_entries' => intval(Request::get('anonymous_entries')),
+                'anonymous_entries' => Request::bool('anonymous_entries', false),
                 'mode'              => $mode
             ]);
-            $feedback->store();
             PageLayout::postSuccess(_('Feedback-Element erfolgreich angelegt'));
         } else {
             PageLayout::postError(_('Sie haben keine Berechtigung, an dieser Stelle ein Feedback-Element anzulegen.'));
diff --git a/app/views/course/feedback/_add_edit_entry_form.php b/app/views/course/feedback/_add_edit_entry_form.php
index 1481f57fb74..f55390f8075 100644
--- a/app/views/course/feedback/_add_edit_entry_form.php
+++ b/app/views/course/feedback/_add_edit_entry_form.php
@@ -17,7 +17,7 @@
     <label class="star-rating undecorated <?= (isset($entry) && $i <= $entry->rating) || $i === 1 ? ' checked' : '' ?>">
         <input class="star-rating-input" name="rating" value="<?= $i ?>" type="radio"
                required
-               <? if (isset($entry) && $i == $entry->rating) echo 'selected'; ?>>
+               <? if (isset($entry) && $i == $entry->rating || $i === 1) echo 'checked'; ?>>
         <?= Icon::create('star') ?>
     </label>
     <? endfor; ?>
@@ -26,16 +26,23 @@
 <? if ($feedback->commentable) : ?>
 <label>
     <?= _('Kommentar') ?>
-    <textarea name="comment"><?= htmlReady(isset($entry) ? $entry->comment : '') ?></textarea>
+    <textarea name="comment"><?= htmlReady($entry->comment ?? '') ?></textarea>
 </label>
 <? endif; ?>
 <? if ($feedback->anonymous_entries) : ?>
 <label>
-    <input type="checkbox" name="anonymous" value="1" <?= $entry->anonymous ? 'checked' : '' ?> >
+    <input type="checkbox" name="anonymous" value="1" <?= !empty($entry->anonymous) ? 'checked' : '' ?> >
     <?= _('Kommentar anonym abgeben') ?>
 </label>
 <? endif; ?>
+
+<? if (Request::isDialog()): ?>
+<div data-dialog-button>
+    <?= Studip\Button::createAccept(_('Absenden'), 'add', ['class' => 'feedback-entry-submit']) ?>
+</div>
+<? else: ?>
 <div>
     <?= Studip\Button::createAccept(_('Absenden'), 'add', ['class' => 'feedback-entry-submit']) ?>
     <?= Studip\Button::createCancel(_('Abbrechen'), 'cancel', ['class' => 'feedback-entry-cancel']) ?>
 </div>
+<? endif; ?>
diff --git a/resources/assets/javascripts/feedback.js b/resources/assets/javascripts/feedback.js
index 5555bb7ea2a..316bc42bc9b 100644
--- a/resources/assets/javascripts/feedback.js
+++ b/resources/assets/javascripts/feedback.js
@@ -125,12 +125,14 @@ STUDIP.Feedback = {
                 }
             );
         }
-        if ($('.feedback-entry-cancel').length) {
+        if ($('.feedback-entry-cancel').length > 0) {
             $('.feedback-entry-cancel').prop("onclick", null).off("click");
             $('.feedback-entry-cancel').click(function (event) {
                 event.preventDefault();
-                $(this).closest('form')[0].reset();
-                $(this).closest('form').find('.star-rating').removeClass('checked');
+                $(this).closest('form').each(function () {
+                    this.reset();
+                    $(this).find('.star-rating').removeClass('checked');
+                });
             });
         }
     }
-- 
GitLab