Skip to content

Commit

Permalink
Merge 682bba8 into af5f58e
Browse files Browse the repository at this point in the history
  • Loading branch information
dinesh-pasupuleti committed Apr 29, 2024
2 parents af5f58e + 682bba8 commit f71600f
Show file tree
Hide file tree
Showing 17 changed files with 466 additions and 206 deletions.
22 changes: 17 additions & 5 deletions app/controllers/sign_up_sheet_controller.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def load_add_signup_topics(assignment_id)
@participants = SignedUpTeam.find_team_participants(assignment_id, session[:ip])
end


def set_values_for_new_topic
@sign_up_topic = SignUpTopic.new
@sign_up_topic.topic_identifier = params[:topic][:topic_identifier]
Expand Down Expand Up @@ -218,7 +219,7 @@ def list
# Find whether the user has signed up for any topics; if so the user won't be able to
# sign up again unless the former was a waitlisted topic
# if team assignment, then team id needs to be passed as parameter else the user's id
users_team = SignedUpTeam.find_team_users(@assignment.id, session[:user].id)
users_team = Team.find_team_users(@assignment.id, session[:user].id)
@selected_topics = if users_team.empty?
nil
else
Expand Down Expand Up @@ -278,7 +279,8 @@ def delete_signup
flash[:error] = 'You cannot drop your topic after the drop topic deadline!'
ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].id, 'Dropping topic for ended work: ' + params[:topic_id].to_s)
else
delete_signup_for_topic(assignment.id, params[:topic_id], session[:user].id)
users_team = Team.find_team_users(assignment.id, session[:user].id)
delete_signup_for_topic(params[:topic_id], users_team[0].t_id)
flash[:success] = 'You have successfully dropped your topic!'
ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].id, 'Student has dropped the topic: ' + params[:topic_id].to_s)
end
Expand All @@ -299,7 +301,7 @@ def delete_signup_as_instructor
flash[:error] = 'You cannot drop a student after the drop topic deadline!'
ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].id, 'Drop failed for ended work: ' + params[:topic_id].to_s)
else
delete_signup_for_topic(assignment.id, params[:topic_id], participant.user_id)
delete_signup_for_topic(params[:topic_id], team.id)
flash[:success] = 'You have successfully dropped the student from the topic!'
ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].id, 'Student has been dropped from the topic: ' + params[:topic_id].to_s)
end
Expand Down Expand Up @@ -511,7 +513,17 @@ def ad_info(_assignment_id, topic_id)
@ad_information
end

def delete_signup_for_topic(assignment_id, topic_id, user_id)
SignUpTopic.reassign_topic(user_id, assignment_id, topic_id)
def delete_signup_for_topic(topic_id, team_id)
# Delete a signup record for a specific topic and team.

# Find the SignUpTopic record with the specified topic_id using the `find_by` method.
@sign_up_topic = SignUpTopic.find_by(id: topic_id)
# Check if the @sign_up_topic record exists (is not nil).
unless @sign_up_topic.nil?
# If the @sign_up_topic record exists, reassign the topic for the specified team by calling the `reassign_topic`
# instance method of SignUpTopic.
@sign_up_topic.reassign_topic(team_id)
end
end

end
26 changes: 17 additions & 9 deletions app/controllers/teams_controller.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,26 @@ def delete
# delete records in team, teams_users, signed_up_teams table
@team = Team.find_by(id: params[:id])
unless @team.nil?
# Find all SignedUpTeam records associated with the found team.
@signed_up_team = SignedUpTeam.where(team_id: @team.id)
# Find all TeamsUser records associated with the found team.
@teams_users = TeamsUser.where(team_id: @team.id)

if @signed_up_team == 1 && !@signUps.first.is_waitlisted # if a topic is assigned to this team
# if there is another team in waitlist, assign this topic to the new team
topic_id = @signed_up_team.first.topic_id
next_wait_listed_team = SignedUpTeam.where(topic_id: topic_id, is_waitlisted: true).first
# Save the topic's new assigned team and delete all waitlists for this team
SignUpTopic.assign_to_first_waiting_team(next_wait_listed_team) if next_wait_listed_team
# Check if there are SignedUpTeam records associated with the found team.
unless @signed_up_team.nil?
# If a topic is assigned to this team and there is only one signed up team record, and it's not waitlisted.
if @signed_up_team.count == 1 && !@signed_up_team.first.is_waitlisted # if a topic is assigned to this team
# Fetch the SignUpTopic object associated with the single signed up team.
@signed_topic = SignUpTopic.find_by(id: @signed_up_team.first.topic_id)
unless @signed_topic.nil?
# Call the instance method `reassign_topic` of SignUpTopic to reassign the topic.
@signed_topic.reassign_topic(@signed_up_team.first.team_id)
end
else
# Drop all waitlists in SignedUpTeam for the specified team ID.
SignedUpTeam.drop_off_waitlists(params[:id])
end
end

@sign_up_team.destroy_all if @sign_up_team
# @sign_up_team.destroy_all if @sign_up_team
@teams_users.destroy_all if @teams_users
@team.destroy if @team
undo_link("The team \"#{@team.name}\" has been successfully deleted.")
Expand Down
14 changes: 14 additions & 0 deletions app/models/assignment.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -654,5 +654,19 @@ def reviewer_metareviews_map(response_map_set)
end
reviewers = reviewers.sort_by { |a| a[1] }
end

#Method to drop all the SignedUpRecords of all topics for that assignment once the drop_topic deadline passes
def drop_waitlisted_teams
# Find all the topics (sign_up_topics) under the current assignment (self).
topics = SignUpTopic.where(assignment_id: self.id)

# Iterate through each topic to find and drop waitlisted teams.
topics.each do |topic|
signed_up_teams = SignedUpTeam.where(topic_id: topic.id, is_waitlisted: true)
# Remove all of the waitlisted SignedUpTeam entries for this topic.
signed_up_teams.destroy_all
end
end

end

Empty file modified app/models/assignment_team.rb
100644 → 100755
Empty file.
107 changes: 13 additions & 94 deletions app/models/sign_up_sheet.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,102 +1,21 @@
class SignUpSheet < ApplicationRecord
# Team lazy initialization method [zhewei, 06/27/2015]
def self.signup_team(assignment_id, user_id, topic_id = nil)
users_team = SignedUpTeam.find_team_users(assignment_id, user_id)
if users_team.empty?
# if team is not yet created, create new team.
# create Team and TeamNode
# Find the team ID for the given assignment and user
team_id = Team.find_team_users(assignment_id, user_id)&.first&.t_id
# If the team doesn't exist, create a new team and assign the team ID
if team_id.nil?
team = AssignmentTeam.create_team_with_users(assignment_id, [user_id])
# create SignedUpTeam
confirmationStatus = SignUpSheet.confirmTopic(user_id, team.id, topic_id, assignment_id) if topic_id
else
confirmationStatus = SignUpSheet.confirmTopic(user_id, users_team[0].t_id, topic_id, assignment_id) if topic_id
team_id = team.id
end
ExpertizaLogger.info "The signup topic save status:#{confirmationStatus} for assignment #{assignment_id} by #{user_id}"
confirmationStatus
end

def self.confirmTopic(user_id, team_id, topic_id, assignment_id)
# check whether user has signed up already
user_signup = SignUpSheet.otherConfirmedTopicforUser(assignment_id, team_id)
users_team = SignedUpTeam.find_team_users(assignment_id, user_id)
team = Team.find(users_team.first.t_id)
if SignedUpTeam.where(team_id: team.id, topic_id: topic_id).any?
return false
# Confirm the signup topic if a topic ID is provided
@signup_topic = SignUpTopic.find_by(id: topic_id)
unless @signup_topic.nil?
confirmation_status = @signup_topic.sign_team_up(team_id)
end

sign_up = SignedUpTeam.new
sign_up.topic_id = topic_id
sign_up.team_id = team_id
result = false
if user_signup.empty?

# Using a DB transaction to ensure atomic inserts
ApplicationRecord.transaction do
# check whether slots exist (params[:id] = topic_id) or has the user selected another topic
team_id, topic_id = create_SignUpTeam(assignment_id, sign_up, topic_id, user_id)
result = true if sign_up.save
end
else
# This line of code checks if the "user_signup_topic" is on the waitlist. If it is not on the waitlist, then the code returns
# false. If it is on the waitlist, the code continues to execute.
user_signup.each do |user_signup_topic|
return false unless user_signup_topic.is_waitlisted
end

# Using a DB transaction to ensure atomic inserts
ApplicationRecord.transaction do
# check whether user is clicking on a topic which is not going to place him in the waitlist
result = sign_up_wailisted(assignment_id, sign_up, team_id, topic_id)
end
end

result
end

def self.sign_up_wailisted(assignment_id, sign_up, team_id, topic_id)
if slotAvailable?(topic_id)
# if slot exist, then confirm the topic for the user and delete all the waitlist for this user
result = cancel_all_wailists(assignment_id, sign_up, team_id, topic_id)
else
sign_up.is_waitlisted = true
result = true if sign_up.save
ExpertizaLogger.info LoggerMessage.new('SignUpSheet', '', "Sign up sheet created for waitlisted with teamId #{team_id}")
end
result
end

def self.cancel_all_wailists(assignment_id, sign_up, team_id, topic_id)
Waitlist.cancel_all_waitlists(team_id, assignment_id)
sign_up.is_waitlisted = false
sign_up.save
# Update topic_id in signed_up_teams table with the topic_id
signUp = SignedUpTeam.where(topic_id: topic_id).first
signUp.update_attribute('topic_id', topic_id)
return true
end

def self.create_SignUpTeam(assignment_id, sign_up, topic_id, user_id)
if slotAvailable?(topic_id)
sign_up.is_waitlisted = false
# Create new record in signed_up_teams table
team_id = TeamsUser.team_id(assignment_id, user_id)
topic_id = SignedUpTeam.topic_id(assignment_id, user_id)
SignedUpTeam.create(topic_id: topic_id, team_id: team_id, is_waitlisted: 0, preference_priority_number: nil)
ExpertizaLogger.info LoggerMessage.new('SignUpSheet', user_id, "Sign up sheet created with teamId #{team_id}")
else
sign_up.is_waitlisted = true
end
[team_id, topic_id]
end

def self.otherConfirmedTopicforUser(assignment_id, team_id)
user_signup = SignedUpTeam.find_user_signup_topics(assignment_id, team_id)
user_signup
end

# When using this method when creating fields, update race conditions by using db transactions
def self.slotAvailable?(topic_id)
SignUpTopic.slotAvailable?(topic_id)
# Log the signup topic save status
ExpertizaLogger.info "The signup topic save status:#{confirmation_status} for assignment #{assignment_id} by #{user_id}"
confirmation_status
end

def self.add_signup_topic(assignment_id)
Expand Down Expand Up @@ -180,4 +99,4 @@ def self.import(row_hash, session, _id = nil)
params += 1
end
end
end
end
140 changes: 96 additions & 44 deletions app/models/sign_up_topic.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -50,59 +50,26 @@ def self.find_waitlisted_topics_for_team(assignment_id, team_id)
SignedUpTeam.find_by_sql(['SELECT u.id FROM sign_up_topics t, signed_up_teams u WHERE t.id = u.topic_id and u.is_waitlisted = true and t.assignment_id = ? and u.team_id = ?', assignment_id.to_s, team_id.to_s])
end

def self.slotAvailable?(topic_id)
def slot_available?
topic_id = self.id
# Retrieve the SignUpTopic record based on the given topic_id
topic = SignUpTopic.find(topic_id)

# Find the number of students who have selected the topic and are not waitlisted
no_of_students_who_selected_the_topic = SignedUpTeam.where(topic_id: topic_id, is_waitlisted: false)


# Check if no students have selected the topic yet
if no_of_students_who_selected_the_topic.nil?
return true
else
# Check if the number of students who selected the topic is less than the maximum allowed
if topic.max_choosers > no_of_students_who_selected_the_topic.size
return true
return true # There are available slots for this topic
else
return false
return false # All slots for this topic are filled
end
end
end

def self.reassign_topic(session_user_id, assignment_id, topic_id)
# find whether assignment is team assignment
assignment = Assignment.find(assignment_id)

# making sure that the drop date deadline hasn't passed
dropDate = AssignmentDueDate.where(parent_id: assignment.id, deadline_type_id: '6').first
if dropDate.nil? || dropDate.due_at >= Time.now
# if team assignment find the creator id from teamusers table and teams
# ACS Removed the if condition (and corresponding else) which differentiate assignments as team and individual assignments
# to treat all assignments as team assignments
# users_team will contain the team id of the team to which the user belongs
users_team = SignedUpTeam.find_team_users(assignment_id, session_user_id)
signup_record = SignedUpTeam.where(topic_id: topic_id, team_id: users_team[0].t_id).first
assignment = Assignment.find(assignment_id)
# if a confirmed slot is deleted then push the first waiting list member to confirmed slot if someone is on the waitlist
unless assignment.is_intelligent?
unless signup_record.try(:is_waitlisted)
# find the first wait listed user if exists
first_waitlisted_user = SignedUpTeam.where(topic_id: topic_id, is_waitlisted: true).first

unless first_waitlisted_user.nil?
# As this user is going to be allocated a confirmed topic, all of his waitlisted topic signups should be purged
### Bad policy! Should be changed! (once users are allowed to specify waitlist priorities) -efg
first_waitlisted_user.is_waitlisted = false
first_waitlisted_user.save

# ACS Removed the if condition (and corresponding else) which differentiate assignments as team and individual assignments
# to treat all assignments as team assignments
Waitlist.cancel_all_waitlists(first_waitlisted_user.team_id, assignment_id)
end
end
end
signup_record.destroy unless signup_record.nil?
ExpertizaLogger.info LoggerMessage.new('SignUpTopic', session_user_id, "Topic dropped: #{topic_id}")
else
# flash[:error] = "You cannot drop this topic because the drop deadline has passed."
end # end condition for 'drop deadline' check
end
end

def self.assign_to_first_waiting_team(next_wait_listed_team)
team_id = next_wait_listed_team.team_id
Expand Down Expand Up @@ -160,4 +127,89 @@ def self.new_topic_from_suggestion(suggestion)
return 'failed'
end
end

def longest_waiting_team(topic_id)
# Find and return the team that has been waiting the longest for the specified topic.

# Find the first waitlisted user (team) for the given topic by querying the SignedUpTeam table.
# Check for records where the topic_id matches the specified topic_id and is_waitlisted is true.
first_waitlisted_user = SignedUpTeam.where(topic_id: topic_id, is_waitlisted: true).first
# If a waitlisted user (team) is found, return it as the team that has been waiting the longest.
unless first_waitlisted_user.nil?
return first_waitlisted_user
end
# If no waitlisted user is found, return nil to indicate that there are no teams waiting.
return nil
end

def reassign_topic(team_id)
# Reassigns topic when a team is dropped from a topic.
# Get the topic ID for reassignment.
topic_id = self.id
# Fetch the record in SignedUpTeam where topic_id matches the topic that needs reassignment
# and team_id matches the provided team_id. Retrieve the first matching record.
signup_record = SignedUpTeam.where(topic_id: topic_id, team_id: team_id).first
# If the signup record is not marked as waitlisted (is_waitlisted is either false or nil),
# proceed with reassignment.
unless signup_record.try(:is_waitlisted)
# Find the longest waiting team for the same topic.
longest_waiting_team = longest_waiting_team(topic_id)
# If a longest waiting team is found, proceed with reassignment.
unless longest_waiting_team.nil?
# Assign the topic to the longest waiting team.
# Update the is_waitlisted boolean to false for the longest waiting team.
longest_waiting_team.is_waitlisted = false
longest_waiting_team.save
# Drop all waitlisted records for that team.
SignedUpTeam.drop_off_waitlists(longest_waiting_team.team_id)
end
end
# Delete the entry for the team that was either previously assigned or waiting.
SignedUpTeam.drop_signup_record(topic_id, team_id)
end

# Method to handle the process when a user signs up
def sign_team_up(team_id)
topic_id = self.id
team = Team.find(team_id)
# Fetch all topics for the user within the team for the assignment
user_signup = SignedUpTeam.find_user_signup_topics(team.parent_id, team_id)
# Check if the user is already signed up and waitlisted for the topic
if !user_signup.empty?
return false unless user_signup.first&.is_waitlisted == true
end
# Create a new SignedUpTeam instance with the provided topic and team details
signup = SignedUpTeam.new(topic_id: topic_id, team_id: team_id)
@signed_topic = SignUpTopic.find_by(id: topic_id)
if @signed_topic.slot_available?
# Assign the topic to the team if a slot is available and drop off the team from all waitlists
SignUpTopic.assign_topic_to_team(signup, topic_id)
# Once assigned, drop all the waitlisted topics for this team
result = SignedUpTeam.drop_off_waitlists(team_id)
else
# Save the team as waitlisted if no slots are available
result = SignUpTopic.save_waitlist_entry(signup, team_id)
end
result
end

# Method to assign a topic to the team and update the waitlist status
def self.assign_topic_to_team(signup, topic_id)
# Set the team's waitlist status to false as they are assigned a topic
signup.update(is_waitlisted: false)
# Update the topic_id in the signed_up_teams table for the user
signed_up_team = SignedUpTeam.find_by(topic_id: topic_id)
signed_up_team.update(topic_id: topic_id) if signed_up_team
end

# Method to save the user as waitlisted if no slots are available
def self.save_waitlist_entry(signup, team_id)
signup.is_waitlisted = true
# Save the user's waitlist status
result = signup.save
# Log the creation of the sign-up sheet for the waitlisted user
ExpertizaLogger.info(LoggerMessage.new('SignUpSheet', '', "Sign up sheet created for waitlisted with teamId #{team_id}"))
result
end

end

0 comments on commit f71600f

Please sign in to comment.