Skip to content

Commit

Permalink
add redemption link for invitation mail for submission
Browse files Browse the repository at this point in the history
  • Loading branch information
fosterfarrell9 authored and tynsh committed Sep 18, 2020
1 parent 0e85437 commit 9c4153e
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/controllers/assignments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AssignmentsController
class AssignmentsController < ApplicationController
before_action :set_assignment, only: [:edit, :destroy, :update, :cancel_edit]
# authorize_resource
authorize_resource

def new
@assignment = Assignment.new
Expand Down
59 changes: 44 additions & 15 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ class SubmissionsController < ApplicationController
before_action :set_submission, only: [:edit, :destroy, :leave, :cancel_edit,
:update, :show_manuscript]
before_action :set_assignment, only: [:new, :enter_code, :cancel_new]
before_action :set_lecture, only: :index
authorize_resource

def index
@lecture = Lecture.find_by_id(params[:lecture_id])
@assignments = @lecture.assignments.order(:deadline)
end

Expand Down Expand Up @@ -43,23 +44,29 @@ def destroy
def enter_code
end

def redeem_code
code = params[:code]
@submission = Submission.find_by(token: code)
check_code_validity
unless @error
@submission.users << current_user
redirect_to submissions_path(params:
{ lecture_id: @submission.tutorial
.lecture_id }),
notice: t('submission.joined_successfully',
assignment: @submission.assignment.title)
return
end
redirect_to :start, alert: t('submission.failed_redemption',
message: @error)
end

def join
@assignment = Assignment.find_by_id(join_params[:assignment_id])
code = join_params[:code]
@submission = Submission.find_by(token: code, assignment: @assignment)
if !@submission
@error = I18n.t('submission.invalid_code',
assignment: @assignment.title)
return
end
if current_user.in?(@submission.users)
@error = I18n.t('submission.already_in')
return
end
if !@assignment.active?
@error = I18n.t('submission.assignment_expired')
return
end
check_code_validity
return if @error
@submission.users << current_user
end

Expand Down Expand Up @@ -108,6 +115,12 @@ def set_assignment
return
end

def set_lecture
@lecture = Lecture.find_by_id(params[:lecture_id])
return if @lecture
redirect_to :root, alert: I18n.t('controllers.no_lecture_given')
end

def join_params
params.require(:join).permit(:code, :assignment_id)
end
Expand All @@ -122,8 +135,24 @@ def send_invitation_emails
NotificationMailer.with(recipient: i,
locale: i.locale,
assignment: @assignment,
code: @submission.token)
code: @submission.token,
issuer: current_user)
.submission_invitation_email.deliver_now
end
end

def check_code_validity
if !@submission && @assignment
@error = I18n.t('submission.invalid_code_for_assignment',
assignment: @assignment.title)
elsif !@submission
@error = I18n.t('submission.invalid_code')
elsif @assignment && !@assignment.active?
@error = I18n.t('submission.assignment_expired')
elsif current_user.in?(@submission.users)
@error = I18n.t('submission.already_in')
elsif !@submission.tutorial.lecture.in?(current_user.lectures)
@error = I18n.t('submission.lecture_not_subscribed')
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/tutorials_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# TutorialsController
class TutorialsController < ApplicationController
before_action :set_tutorial, only: [:edit, :destroy, :update, :cancel_edit]
# authorize_resource
authorize_resource

def new
@tutorial = Tutorial.new
Expand Down
1 change: 1 addition & 0 deletions app/mailers/notification_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def submission_invitation_email
@recipient = params[:recipient]
@assignment = params[:assignment]
@code = params[:code]
@issuer = params[:issuer]
mail(from: @sender,
to: @recipient.email,
subject: t('mailer.submission_invitation_subject',
Expand Down
24 changes: 23 additions & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ def initialize(user)

can [:list_tags, :list_sections, :display], Section

can [:index, :new, :redeem_code], Submission

# an editor might still be a student in some other course
can [:edit, :update, :create, :destroy, :enter_code, :join, :leave,
:cancel_edit, :cancel_new], Submission do |submission|
user.in?(submission.users)
end

can :show_manuscript do |submission|
user.in?(submission.users) ||
submission.tutorial.lecture.edited_by?(user)
end

can :manage, Tag
can [:display_cyto, :fill_tag_select, :fill_course_tags,
:take_random_quiz, :postprocess], Tag
Expand Down Expand Up @@ -138,7 +151,7 @@ def initialize(user)
else
can :read, :all
can :start, :main
cannot :read, [:administration, Term, User, Announcement]
cannot :read, [:administration, Term, User, Announcement, Assignment]
cannot :index, Interaction
# guest users can play/display media only when their release status
# is 'all', logged in users can do that unless the release status is
Expand Down Expand Up @@ -202,6 +215,15 @@ def initialize(user)

can [:show_example, :find_example, :show_property, :show_structure,
:find_tags, :display_info], :erdbeere

can [:index, :new], Submission

# an editor might still be a student in some other course
can [:edit, :update, :create, :destroy, :enter_code, :join, :leave,
:cancel_edit, :cancel_new, :show_manuscript,
:redeem_code], Submission do |submission|
user.in?(submission.users)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<%= t('mailer.submission_invitation',
<%= t('mailer.submission_invitation_html',
code: @code,
assignment: @assignment.title,
lecture: @assignment.lecture.short_title) %>
lecture: @assignment.lecture.short_title,
issuer: @issuer.name,
link: link_to(t('basics.link'),
redeem_submission_code_url(params: { code: @code }))) %>
14 changes: 13 additions & 1 deletion config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2289,9 +2289,12 @@ de:
Anzahl der Medien in von den von Dir abonnierten
Veranstaltungen bzw. Modulen, in denen es neue Kommentare gibt:
submission:
invalid_code: Der eingegebene Code ist ungültig für %{assignment}.
invalid_code: Der eingegebene Code ist ungültig.
invalid_code_for_assignment: >
Der eingegebene Code ist ungültig für %{assignment}.
assignment_expired: Die Frist für diese Hausaufgabe ist bereits abgelaufen.
already_in: Bei dieser Abgabe bist Du schon dabei.
lecture_not_subscribed: Du hast die zugehörige Veranstaltung icht abonniert.
no_partners_no_leave: >
Du kannst nicht austreten, da Du die einzige Person in der Abgabegruppe
bist. Dafür kannst Du die Abgabe löschen.
Expand All @@ -2301,6 +2304,10 @@ de:
nothing_here_past: >
Zu dieser Hausaufgabe wurde nichts hinterlegt.
invitations_to: Einladungen an
joined_successfully: >
Du bist der Abgabe zu %{assignment} erfolgreich beigetreten.
failed_redemption: >
Die Einlösung des Codes ist fehlgeschlagen: %{message}
assignment:
expired: Die Abgabefrist ist abgelaufen.
locales:
Expand Down Expand Up @@ -2336,6 +2343,10 @@ de:
Du kannst es über Deine Profileinstellungen abonnieren.
submission_invitation_subject: >
Einladung zu Abgabe für %{assignment} in %{lecture}
submission_invitation_html: >
%{issuer} hat Dich eingeladen, einer Abgabe zu %{assignment} in %{lecture}
beizutreten. Der Code zum Beitreten lautet: %{code}. Du kannst ihn MaMpf
selbst eingeben und einlösen oder diesen %{link} benutzen.
statistics:
video_thyme: 'THymE-Aufrufe'
video_downloads: 'Downloads'
Expand Down Expand Up @@ -2707,6 +2718,7 @@ de:
no_tutorial: 'Ein Tutorium mit der angeforderten id existiert nicht.'
no_submission: Eine Abgabe mit der angeforderten id existiert nicht.
no_assignment: Eine Hausaufgabe mit der angeforderten id existiert nicht.
no_lecture_given: Es wurde keine oder eine ungültige Vorlesung angegeben.
activerecord:
errors:
no_valid_url: 'Das ist keine gültige URL.'
Expand Down
15 changes: 14 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2156,11 +2156,15 @@ en:
en: 'English'
de: 'German'
submission:
invalid_code: The code you entered is invalid for %{assignment}.
invalid_code_for_assignment: >
The code you entered is invalid for %{assignment}.
invalid_code: The code you entered is invalid.
assignment_expired: >
The due date for this homework assignment has already
expired.
already_in: You are already a partner for this assgnment.
lecture_not_subscribed: >
You are not subscribed to the coresponding event series.
no_partners_no_leave: >
You cannot leave as you are the only person in this group (but you
you can delete this submission).
Expand All @@ -2169,6 +2173,10 @@ en:
nothing_here_past: >
Nothing was submitted for this assignment.
invitations_to: Invitations to
joined_successfully: >
You successfully joind the submission to %{assignment}.
failed_redemption: >
Redemption of submission code failed: %{message}.
assignment:
expired: The due date has expired.
mailer:
Expand Down Expand Up @@ -2199,6 +2207,10 @@ en:
You can subscribe to it in your profile settings.
submission_invitation_subject: >
Invitation to submission of %{assignment} in %{lecture}
submission_invitation: >
%{issuer} issuer has invited you to join a submission to %{assignment}
in %{lecture}. The code to join is: %{code}. You can enter and redeem
it in MaMpf manually or follow this %{link}.
statistics:
video_thyme: 'Accessed by THymE'
video_downloads: 'Downloads'
Expand Down Expand Up @@ -2563,6 +2575,7 @@ en:
no_tutorial: 'There is no tutorial with this id.'
no_submission: There is no submission with this id.
no_assignment: There is no assignment with this id.
no_lecture_given: Invalid or no lecture was given.
activerecord:
errors:
no_valid_url: 'This is not a valid URL.'
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@
as: 'join_submission'
get 'submissions/enter_code', to: 'submissions#enter_code',
as: 'enter_submission_code'
get 'submissions/redeem_code', to: 'submissions#redeem_code',
as: 'redeem_submission_code'

delete 'submissions/:id/leave', to: 'submissions#leave',
as: 'leave_submission'
Expand Down

0 comments on commit 9c4153e

Please sign in to comment.