Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Feedback #171

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ApplicationController < ActionController::Base
helper_method :show_record_path
helper_method :rss_search_request_url
helper_method :on_campus?
helper_method :turbo_frame_request?

def set_locale
return unless helpers.locale_switching_enabled?
Expand Down
72 changes: 72 additions & 0 deletions app/controllers/feedbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class FeedbacksController < ApplicationController

ENABLED = Config[:feedback, :enabled, default: false]

def new
if ENABLED
@feedback = Feedback.new

if params[:record_id] && params[:record_scope]
record = SearchEngine[params[:record_scope]].get_record(params[:record_id])

@feedback.record_scope = params[:record_scope]
@feedback.record_id = record.id
@feedback.record_title = record.title
end

if current_user.present?
@feedback.firstname = current_user.first_name.presence || ""
@feedback.lastname = current_user.last_name.presence || ""
@feedback.email = current_user.email.presence
end

if turbo_frame_request?
render "show-modal"
else
render :new
end
end
end

def create
if ENABLED
@feedback = Feedback.new(feedback_params)

if @feedback.valid?
@feedback.user_id = current_user&.ils_primary_id.presence || "anonymous"

FeedbacksMailer.with(feedback: @feedback.as_json).notify_ub.deliver_later
FeedbacksMailer.with(feedback: @feedback.as_json).notify_user.deliver_later

if turbo_frame_request?
render "success-modal"
else
flash[:success] = t(".success")
redirect_to root_path
end
else
if turbo_frame_request?
render "show-modal", status: :unprocessable_entity
else
render :new, status: :unprocessable_entity
end
end
end
end

private

def feedback_params
params.require(:feedback).permit(
:type,
:record_scope,
:record_id,
:record_title,
:message,
:firstname,
:lastname,
:email
)
end

end
27 changes: 27 additions & 0 deletions app/mailers/feedbacks_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class FeedbacksMailer < ApplicationMailer
before_action { @feedback = Feedback.new(params[:feedback]) }

ENABLED = Config[:feedback, :enabled, default: false]

def notify_ub
if ENABLED
default_ubmail_config = Config[:feedback, :types, :default, :mail, :ub]
feedback_type_config = Config[:feedback, :types, @feedback.type.to_sym, :mail, :ub, default: {}]

merged_config = default_ubmail_config.merge(feedback_type_config)

mail(from: merged_config[:from], to: merged_config[:to], subject: merged_config[:subject], template_name: merged_config[:template_name])
end
end

def notify_user
if ENABLED
default_usermail_config = Config[:feedback, :types, :default, :mail, :user]
feedback_type_config = Config[:feedback, :types, @feedback.type.to_sym, :mail, :user, default: {}]

merged_config = default_usermail_config.merge(feedback_type_config)

mail(from: merged_config[:from], to: @feedback.email, subject: merged_config[:subject], template_name: merged_config[:template_name])
end
end
end
51 changes: 51 additions & 0 deletions app/models/feedback.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Feedback
include ActiveModel::Model
include ActiveModel::Serializers::JSON

EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d-]+(\.[a-z\d-]+)*\.[a-z]+\z/i

TYPES = [
"general",
"loan_question",
"broken_link",
"missing_media"
].freeze

attr_accessor :user_id, :firstname, :lastname, :email
attr_accessor :type, :record_scope, :record_id, :record_title, :message

def attributes=(hash)
hash.each do |key, value|
send("#{key}=", value)
end
end

def attributes
{
"user_id" => nil,
"firstname" => nil,
"lastname" => nil,
"email" => nil,
"type" => nil,
"record_scope" => nil,
"record_id" => nil,
"record_title" => nil,
"message" => nil
}
end

validates :firstname, presence: true
validates :lastname, presence: true
validates :email, presence: true

validates :type, presence: true
validates :message, presence: true

validate :validate_email

def validate_email
errors.add(:email, :invalid) if email.present? && email !~ EMAIL_REGEX
errors.add(:email, :blank) if email.blank?
end

end
30 changes: 30 additions & 0 deletions app/views/feedbacks/_details.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
= simple_form_for @feedback do |f|
- if @feedback.errors.any?
.callout.callout-danger
= t("feedback.form.error", count: @feedback.errors.count)

- if @feedback.record_title.present?
= f.input :record_scope, readonly: true, as: :hidden
= f.input :record_id, readonly: true, as: :hidden
= f.input :record_title, readonly: true

= f.input :type, as: :select, collection: Feedback::TYPES, include_blank: true, \
label_method: ->(code){ t("feedback.form.types.#{code}", default: code) }
= f.input :message, as: :text, :input_html => { :rows => 5 }

fieldset.border.p-3.mb-3
legend: h5.mb-0 = t("feedback.form.contact_information")
.row
.col-lg
= f.input :firstname, readonly: current_user.present? ? true : false
.col-lg
= f.input :lastname, readonly: current_user.present? ? true : false

= f.input :email, required: true

.d-flex
= f.submit t("feedback.form.submit"), class: "btn btn-primary", data: {"turbo-frame": "_self"}
- if turbo_frame_request?
= link_to t("feedback.form.cancel"), root_path, class: "btn btn-link", data: {"turbo-frame": "_self", "controller": "modal", "modal-modal-dialog-outlet": "#modal-dialog", "action": "click->modal#close"}
- else
= link_to t("feedback.form.cancel"), root_path, class: "btn btn-link"
4 changes: 4 additions & 0 deletions app/views/feedbacks/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.card.card-shadowed
h5.card-header = t("feedback.title")
.card-body
= render "details"
6 changes: 6 additions & 0 deletions app/views/feedbacks/show-modal.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= turbo_frame_tag "modal" do
.modal-header
h5.modal-title = t("feedback.title")
button(type="button" class="btn-close" data-bs-dismiss="modal")
.modal-body
= render "details"
11 changes: 11 additions & 0 deletions app/views/feedbacks/success-modal.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
= turbo_frame_tag "modal" do
.modal-header
h5.modal-title = t("feedback.title")
button(type="button" class="btn-close" data-bs-dismiss="modal")
.modal-body
.d-flex.justify-content-evenly.align-items-center.flex-column(style="height: 16em;")
i.fa-regular.fa-paper-plane.fa-5x.opacity-75
div
= t("feedbacks.create.success")
.d-flex
= link_to t("feedback.form.close"), root_path, class: "btn btn-primary", data: {"turbo-frame": "_self", "controller": "modal", "modal-modal-dialog-outlet": "#modal-dialog", "action": "click->modal#close"}
14 changes: 14 additions & 0 deletions app/views/feedbacks_mailer/default_ub_mail.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
feedback:
type: [<%= @feedback.type %>]
message: [<%= @feedback.message %>]

record:
scope: [<%= @feedback.record_scope %>]
id: [<%= @feedback.record_id %>]
title: [<%= @feedback.record_title %>]

user:
id: [<%= @feedback.user_id %>]
firstname: [<%= @feedback.firstname %>]
lastname: [<%= @feedback.lastname %>]
email: [<%= @feedback.email %>]
17 changes: 17 additions & 0 deletions app/views/feedbacks_mailer/default_user_mail.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Sehr geehrte(r) <%= @feedback.firstname %> <%= @feedback.lastname %>,

Vielen Dank für Ihr Feedback. Wir kümmern uns schnellstmöglich darum und geben Ihnen dann Rückmeldung.

Mit freundlichen Grüßen

Ihre Universitätsbibliothek

---
Universitätsbibliothek Paderborn
Dezernat Benutzung & Service
Sachgebiet Ortsleihe
Warburger Str. 100
33098 Paderborn
Tel. 05251 / 60-2012
E-Mail: [email protected]
WWW: http:https://www.ub.uni-paderborn.de
3 changes: 2 additions & 1 deletion app/views/records/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
= render "searches/record", \
record: @record, \
enable_watch_lists_panel: enable_watch_list_panel, \
enable_notes: enable_notes
enable_notes: enable_notes, \
enable_feedback: true
hr.mb-3

/ ----------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion app/views/searches/_hits.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ ruby:
record: hit.record, \
enable_watch_lists_panel: enable_watch_list_panel, \
enable_fulltext_links: true, \
enable_notes: enable_notes
enable_notes: enable_notes, \
enable_feedback: true
.card-footer
= paginator
8 changes: 8 additions & 0 deletions app/views/searches/_record.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ruby:
enable_fulltext_links = local_assigns.fetch(:enable_fulltext_links) { false }
search_scope = local_assigns.fetch(:search_scope) { current_search_scope }
enable_notes = local_assigns.fetch(:enable_notes) { false }
enable_feedback = local_assigns.fetch(:enable_feedback) { false } && Config[:feedback, :enabled]

.d-flex(
data-record-scope=search_scope
Expand Down Expand Up @@ -50,6 +51,13 @@ ruby:
)
.ms-2 = render(watch_lists_panel_component)

/ Feedback button
- if enable_feedback
.ms-2.d-print-none
div(data-controller="modal" data-modal-modal-dialog-outlet="#modal-dialog")
= link_to new_feedback_path(record_scope: search_scope, record_id: record_id), class: "btn btn-outline-primary btn-sm", "data-action": "click->modal#open" do
i.fa-solid.fa-message.fa-fw.pe-none

/ Citation / Export
.ms-2
.dropdown
Expand Down
35 changes: 35 additions & 0 deletions config/locales/de/feedback.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
de:
feedback:
title: "Feedback"
form:
types:
none: "Keine"
general: "Allgemein"
loan_question: "Frage zur Ausleihbarkeit"
broken_link: "Fehlerhafter Link"
missing_media: "Fehlendes Medium"
contact_information: "Kontaktinformationen"
submit: "Abschicken"
cancel: "Abbrechen"
close: "Schließen"
error:
one: >
Ein Fehler verhindert das Abschicken des Feedbacks. Bitte prüfen Sie das Formular auf
rot makierte Felder.
other:
"%{count} Fehler verhindern das Abschicken des Feedbacks. Bitte prüfen Sie das Formular auf
rot makierte Felder."

feedbacks:
create:
success: "Ihr Feedback wurde erfolgreich übermittelt."

activemodel:
attributes:
feedback:
record_title: "Titel"
firstname: "Vorname"
lastname: "Nachname"
email: "Email"
type: "Anliegen"
message: "Mitteilung"
35 changes: 35 additions & 0 deletions config/locales/en/feedback.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
en:
feedback:
title: "Feedback"
form:
types:
none: "None"
general: "General"
loan_question: "Question regarding loan"
broken_link: "Broken link"
missing_media: "Missing media"
contact_information: "Contact information"
submit: "Submit"
cancel: "Cancel"
close: "Close"
error:
one: "%{count} error prohibited this %{model} from being saved"
other: "%{count} errors prohibited this %{model} from being saved"
one: >
"An error is preventing the feedback from being sent. Please check the form for errors highlighted in red."
other:
"%{count} errors are preventing the feedback from being sent. Please check the form for errors highlighted in red."

feedbacks:
create:
success: "Your feedback has been successfully submitted."

activemodel:
attributes:
feedback:
record_title: "Title"
firstname: "First name"
lastname: "Last name"
email: "Email"
type: "Request"
message: "Message"
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
resources :recommendations, only: [:index]
end

resources :feedbacks, only: [:new, :create], path: "feedback"

# Open URL Link-Resolver
get "openurl", to: "link_resolver#show"

Expand Down