Skip to content

Commit

Permalink
Add Contract Model and setup selection of tutors and editors
Browse files Browse the repository at this point in the history
  • Loading branch information
fosterfarrell9 committed Aug 15, 2024
1 parent 84c742c commit 0da405e
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 30 deletions.
13 changes: 9 additions & 4 deletions app/controllers/lectures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ def update
recipients = User.where(id: new_ids)

recipients.each do |r|
NotificationMailer.with(recipient: r,
locale: r.locale,
lecture: @lecture)
.new_editor_email.deliver_later
notify_new_editor_by_mail(r)
end
end

Expand Down Expand Up @@ -312,6 +309,7 @@ def become_tutor
def become_editor
if Voucher.check_voucher(become_editor_params[:voucher_hash])
@lecture.update_editor_status!(current_user)
notify_new_editor_by_mail(current_user)
redirect_to edit_profile_path, notice: I18n.t("controllers.become_editor_success")
else
handle_invalid_voucher
Expand Down Expand Up @@ -492,4 +490,11 @@ def handle_invalid_voucher
format.html { redirect_to edit_profile_path, alert: error_message }
end
end

def notify_new_editor_by_mail(editor)
NotificationMailer.with(recipient: editor,
locale: editor.locale,
lecture: @lecture)
.new_editor_email.deliver_later
end
end
2 changes: 1 addition & 1 deletion app/helpers/lectures_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def lecture_view_icon(lecture)

def editors_preselection(lecture)
options_for_select(lecture.eligible_as_editors.map do |editor|
[editor.name, editor.id]
[editor.info, editor.id]
end, lecture.editor_ids)
end
end
2 changes: 1 addition & 1 deletion app/helpers/tutorials_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def cancel_editing_tutorial_path(tutorial)
end

def tutors_preselection(tutorial)
options_for_select(tutorial.eligible_as_tutors.map do |t|
options_for_select(tutorial.lecture.eligible_as_tutors.map do |t|
[t.tutorial_info, t.id]
end, tutorial.tutor_ids)
end
Expand Down
11 changes: 11 additions & 0 deletions app/models/contract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Contract < ApplicationRecord
belongs_to :user
belongs_to :lecture

scope :tutors, -> { where(role: ROLE_HASH[:tutor]) }
scope :editors, -> { where(role: ROLE_HASH[:editor]) }

ROLE_HASH = { tutor: 0, editor: 1 }.freeze

enum role: ROLE_HASH
end
26 changes: 25 additions & 1 deletion app/models/lecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class Lecture < ApplicationRecord
# users to tutors, editors or teachers
has_many :vouchers, dependent: :destroy

# a lecture has many student assistants
has_many :contracts, dependent: :destroy
# has_many :assistants, -> { distinct }, through: :contracts, source: :assistant

# a lecture has many structure_ids, referring to the ids of structures
# in the erdbeere database
serialize :structure_ids, type: Array, coder: YAML
Expand Down Expand Up @@ -860,6 +864,7 @@ def update_tutor_status!(user, selected_tutorials)
remove_tutor(t, user)
end
end
Contract.create(user: user, lecture: self, role: :tutor)
# touch to invalidate the cache
touch
end
Expand All @@ -868,12 +873,31 @@ def update_editor_status!(user)
return if editors.include?(user)

editors << user
Contract.create(user: user, lecture: self, role: :editor)
# touch to invalidate the cache
touch
end

def eligible_as_tutors
(tutors + tutors_with_contract + editors + [teacher]).uniq
# the first one should (in the future) actually be contained in the sum of
# the other ones, but in the transition phase where some tutor statuses were
# still given by the old system, this will not be true
end

def eligible_as_editors
(editors + course.editors).uniq
(editors + editors_with_contract + course.editors - [teacher]).uniq
# the first one should (in the future) actually be contained in the sum of
# the other ones, but in the transition phase where some editor statuses were
# still given by the old system, this will not be true
end

def tutors_with_contract
User.where(id: Contract.where(lecture: self, role: :tutor).select(:user_id))
end

def editors_with_contract
User.where(id: Contract.where(lecture: self, role: :editor).select(:user_id))
end

private
Expand Down
4 changes: 0 additions & 4 deletions app/models/tutorial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ def teams_to_csv(assignment)
end
end

def eligible_as_tutors
(lecture.tutors + lecture.editors + [lecture.teacher]).uniq
end

private

def check_destructibility
Expand Down
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class User < ApplicationRecord

has_many :feedbacks, dependent: :destroy

# a user has contracts as asssistant in lectures
has_many :contracts, dependent: :destroy

include ScreenshotUploader[:image]

# if a homepage is given it should at leat be a valid address
Expand Down
33 changes: 17 additions & 16 deletions app/views/lectures/edit/_people.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
class: "form-label" %>
<%= helpdesk(t('admin.lecture.info.teacher'), false) %>
<div id="lecture_teacher_select" %>
<%= f.select :teacher_id,
editors_preselection(lecture),
{},
{ class: 'selectize',
data: { ajax: true,
model: 'user',
filled: false,
placeholder:
t('basics.enter_two_letters'),
no_results:
t('basics.no_results') } } %>
<%= f.select :teacher_id,
options_for_select([[lecture.teacher.info,
lecture.teacher.id]],
lecture.teacher.id),
{},
{ class: 'selectize',
data: { ajax: true,
model: 'user',
filled: false,
placeholder:
t('basics.enter_two_letters'),
no_results:
t('basics.no_results') } } %>
<div class="invalid-feedback" id="lecture-teacher-error">
</div>
</div>
Expand All @@ -32,11 +34,10 @@
<%= helpdesk(t('admin.lecture.info.lecture_editors'), false) %>
<div id="lecture_editors_select" %>
<%= f.select :editor_ids,
options_for_select(lecture.select_editors,
lecture.editors.map(&:id)),
{},
{ multiple: true,
class: 'selectize' } %>
editors_preselection(lecture),
{},
{ multiple: true,
class: 'selectize' } %>
</div>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2188,8 +2188,8 @@ de:
Wir haben Deine Anfrage erhalten und werden Dir Deine Daten zeitnah schicken.
redeem_voucher: Gutschein einlösen
become_tutor: >
Mit diesem Gutschein kannst Du ein oder mehrere Tutorien in der
Vorlesung %{lecture} übernehmen.
Mit diesem Gutschein kannst Du TutorIn in der Vorlesung %{lecture} werden.
Wenn Du schon weißt, welche Tutorien du übernimmst, kannst du diese hier auswählen.
select_tutorials: Tutorien auswählen
take_over_tutorials: Tutorien übernehmen
become_editor: >
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,8 @@ en:
redeem_voucher: Redeeem Voucher
become_tutor: >
With this voucher, you can become a tutor in the lecture %{lecture}.
If you already know which tutorials you will be responsible for,
you can select them here.
select_tutorials: Select Tutorials
take_over_tutorials: Take Over Tutorials
become_editor: >
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20240815143156_create_contracts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateContracts < ActiveRecord::Migration[7.1]
def change
create_table :contracts do |t|
t.references :user, null: false, foreign_key: true
t.references :lecture, null: false, foreign_key: true
t.integer :role, null: false

t.timestamps
end
end
end
14 changes: 13 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_07_28_123817) do
ActiveRecord::Schema[7.1].define(version: 2024_08_15_143156) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -140,6 +140,16 @@
t.index ["commontable_type", "commontable_id"], name: "index_commontator_threads_on_c_id_and_c_type", unique: true
end

create_table "contracts", force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "lecture_id", null: false
t.integer "role", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["lecture_id"], name: "index_contracts_on_lecture_id"
t.index ["user_id"], name: "index_contracts_on_user_id"
end

create_table "course_self_joins", force: :cascade do |t|
t.bigint "course_id"
t.bigint "preceding_course_id"
Expand Down Expand Up @@ -950,6 +960,8 @@
add_foreign_key "commontator_comments", "commontator_comments", column: "parent_id", on_update: :restrict, on_delete: :cascade
add_foreign_key "commontator_comments", "commontator_threads", column: "thread_id", on_update: :cascade, on_delete: :cascade
add_foreign_key "commontator_subscriptions", "commontator_threads", column: "thread_id", on_update: :cascade, on_delete: :cascade
add_foreign_key "contracts", "lectures"
add_foreign_key "contracts", "users"
add_foreign_key "course_self_joins", "courses"
add_foreign_key "divisions", "programs"
add_foreign_key "feedbacks", "users"
Expand Down

0 comments on commit 0da405e

Please sign in to comment.