Skip to content

Commit

Permalink
Merge 6ffe599 into 9860352
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravaradhye committed Nov 1, 2015
2 parents 9860352 + 6ffe599 commit cd18b46
Show file tree
Hide file tree
Showing 7 changed files with 418 additions and 134 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ group :test do
gem 'rspec-rails'
gem 'shoulda'
gem 'test-unit'
gem 'rspec-mocks'
end

group :assets do
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ DEPENDENCIES
react-rails (~> 1.0)
rgl
rjb
rspec-mocks
rspec-rails
rubyzip
rwordnet (= 0.1.3)
Expand Down
138 changes: 72 additions & 66 deletions app/controllers/sign_up_sheet_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,54 +51,17 @@ def new
#that assignment id will virtually be the signup sheet id as well as we have assumed
#that every assignment will have only one signup sheet
def create
topic = SignUpTopic.where(topic_name: params[:topic][:topic_name], assignment_id: params[:id]).first
topic = SignUpTopic.where(topic_name: params[:topic][:topic_name], assignment_id: params[:id]).first

#if the topic already exists then update
if topic != nil
topic.topic_identifier = params[:topic][:topic_identifier]

#While saving the max choosers you should be careful; if there are users who have signed up for this particular
#topic and are on waitlist, then they have to be converted to confirmed topic based on the availability. But if
#there are choosers already and if there is an attempt to decrease the max choosers, as of now I am not allowing
#it.
if SignedUpTeam.find_by_topic_id(topic.id).nil? || topic.max_choosers == params[:topic][:max_choosers]
topic.max_choosers = params[:topic][:max_choosers]
else
if topic.max_choosers.to_i < params[:topic][:max_choosers].to_i
topic.update_waitlisted_users(params[:topic][:max_choosers])
topic.max_choosers = params[:topic][:max_choosers]
else
flash[:error] = 'Value of maximum choosers can only be increased! No change has been made to max choosers.'
end
end

topic.category = params[:topic][:category]
#topic.assignment_id = params[:id]
topic.save
redirect_to_sign_up(params[:id])
unless topic.nil?
update_existing_topic topic
else
set_values_for_new_topic

if @assignment.is_microtask?
@sign_up_topic.micropayment = params[:topic][:micropayment]
end

if @assignment.staggered_deadline?
topic_set = Array.new
topic = @sign_up_topic.id
end

if @sign_up_topic.save
undo_link("Topic: \"#{@sign_up_topic.topic_name}\" has been created successfully. ")
#changing the redirection url to topics tab in edit assignment view.
redirect_to edit_assignment_path(@sign_up_topic.assignment_id) + "#tabs-5"
else
render :action => 'new', :id => params[:id]
end
end
setup_new_topic
end
end

#This method is used to delete signup topics
#This method is used to delete signup topics
#Renaming delete method to destroy for rails 4 compatible
def destroy
@topic = SignUpTopic.find(params[:id])
Expand Down Expand Up @@ -132,20 +95,7 @@ def update
if @topic
@topic.topic_identifier = params[:topic][:topic_identifier]

#While saving the max choosers you should be careful; if there are users who have signed up for this particular
#topic and are on waitlist, then they have to be converted to confirmed topic based on the availability. But if
#there are choosers already and if there is an attempt to decrease the max choosers, as of now I am not allowing
#it.
if SignedUpTeam.find_by_topic_id(@topic.id).nil? || @topic.max_choosers == params[:topic][:max_choosers]
@topic.max_choosers = params[:topic][:max_choosers]
else
if @topic.max_choosers.to_i < params[:topic][:max_choosers].to_i
@topic.update_waitlisted_users(params[:topic][:max_choosers])
@topic.max_choosers = params[:topic][:max_choosers]
else
flash[:error] = 'Value of maximum choosers can only be increased! No change has been made to max choosers.'
end
end
update_max_choosers @topic

#update tables
@topic.category = params[:topic][:category]
Expand Down Expand Up @@ -210,6 +160,13 @@ def set_values_for_new_topic
@assignment = Assignment.find(params[:id])
end

#simple function that redirects ti the /add_signup_topics or the /add_signup_topics_staggered page depending on assignment type
#staggered means that different topics can have different deadlines.
def redirect_to_sign_up(assignment_id)
assignment = Assignment.find(assignment_id)
(assignment.staggered_deadline == true)?(redirect_to :action => 'add_signup_topics_staggered', :id => assignment_id):(redirect_to :action => 'add_signup_topics', :id => assignment_id)
end

#simple function that redirects to assignment->edit->topic panel to display /add_signup_topics or the /add_signup_topics_staggered page
#staggered means that different topics can have different deadlines.
def redirect_to_assignment_edit(assignment_id)
Expand Down Expand Up @@ -370,25 +327,25 @@ def save_topic_deadlines
review_rounds = Assignment.find(params[:assignment_id]).get_review_rounds
# j represents the review rounds
j = 0
topics.each { |topic|
topics.each_with_index do |topic, j|
for i in 1..review_rounds
topic_deadline_type_subm = DeadlineType.find_by_name('submission').id
topic_deadline_type_subm = DeadlineType.where(name: 'submission').first.id
topic_deadline_subm = TopicDeadline.where(topic_id: session[:duedates][j]['id'].to_i, deadline_type_id: topic_deadline_type_subm, round: i).first

topic_deadline_subm.update_attributes({'due_at' => due_dates[session[:duedates][j]['id'].to_s + '_submission_' + i.to_s + '_due_date']})
topic_deadline_subm.update_attributes({due_at: due_dates[session[:duedates][j]['id'].to_s + '_submission_' + i.to_s + '_due_date']})
flash[:error] = "Please enter a valid " + (i > 1 ? "Resubmission deadline " + (i-1).to_s : "Submission deadline") if topic_deadline_subm.errors.length > 0

topic_deadline_type_rev = DeadlineType.find_by_name('review').id
topic_deadline_type_rev = DeadlineType.where(name: 'review').first.id
topic_deadline_rev = TopicDeadline.where(topic_id: session[:duedates][j]['id'].to_i, deadline_type_id: topic_deadline_type_rev, round: i).first
topic_deadline_rev.update_attributes({'due_at' => due_dates[session[:duedates][j]['id'].to_s + '_review_' + i.to_s + '_due_date']})
topic_deadline_rev.update_attributes({due_at: due_dates[session[:duedates][j]['id'].to_s + '_review_' + i.to_s + '_due_date']})
flash[:error] = "Please enter a valid Review deadline " + (i > 1 ? (i-1).to_s : "") if topic_deadline_rev.errors.length > 0
end

topic_deadline_subm = TopicDeadline.where(topic_id: session[:duedates][j]['id'], deadline_type_id: DeadlineType.find_by_name('metareview').id).first
topic_deadline_subm.update_attributes({'due_at' => due_dates[session[:duedates][j]['id'].to_s + '_submission_' + (review_rounds+1).to_s + '_due_date']})
deadline_type = DeadlineType.where(name: 'metareview')
topic_deadline_subm = TopicDeadline.where(topic_id: session[:duedates][j]['id'], deadline_type_id: deadline_type.id).first
topic_deadline_subm.update_attributes({due_at: due_dates[session[:duedates][j]['id'].to_s + '_submission_' + (review_rounds+1).to_s + '_due_date']})
flash[:error] = "Please enter a valid Meta review deadline" if topic_deadline_subm.errors.length > 0
j = j + 1
}
end

redirect_to_assignment_edit(params[:assignment_id])
end
Expand Down Expand Up @@ -510,4 +467,53 @@ def are_needed_authorizations_present?
return true
end
end

def setup_new_topic
set_values_for_new_topic

if @assignment.is_microtask?
@sign_up_topic.micropayment = params[:topic][:micropayment]
end

if @assignment.staggered_deadline?
topic_set = Array.new
topic = @sign_up_topic.id
end

if @sign_up_topic.save
undo_link "Topic: \"#{@sign_up_topic.topic_name}\" has been created successfully. "
#changing the redirection url to topics tab in edit assignment view.
redirect_to edit_assignment_path(@sign_up_topic.assignment_id) + "#tabs-5"
else
render :action => 'new', :id => params[:id]
end
end

def update_existing_topic(topic)
topic.topic_identifier = params[:topic][:topic_identifier]

update_max_choosers topic

topic.category = params[:topic][:category]
#topic.assignment_id = params[:id]
topic.save
redirect_to_sign_up params[:id]
end

def update_max_choosers(topic)
#While saving the max choosers you should be careful; if there are users who have signed up for this particular
#topic and are on waitlist, then they have to be converted to confirmed topic based on the availability. But if
#there are choosers already and if there is an attempt to decrease the max choosers, as of now I am not allowing
#it.
if SignedUpTeam.find_by_topic_id(topic.id).nil? || topic.max_choosers == params[:topic][:max_choosers]
topic.max_choosers = params[:topic][:max_choosers]
else
if topic.max_choosers.to_i < params[:topic][:max_choosers].to_i
topic.update_waitlisted_users params[:topic][:max_choosers]
topic.max_choosers = params[:topic][:max_choosers]
else
flash[:error] = 'Value of maximum choosers can only be increased! No change has been made to max choosers.'
end
end
end
end
155 changes: 91 additions & 64 deletions app/models/sign_up_sheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ def self.confirmTopic(user_id, team_id, topic_id, assignment_id)
# Using a DB transaction to ensure atomic inserts
ActiveRecord::Base.transaction do
#check whether slots exist (params[:id] = topic_id) or has the user selected another topic
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)
else
sign_up.is_waitlisted = true
end
team_id, topic_id = create_SignUpTeam(assignment_id, sign_up, topic_id, user_id)
if sign_up.save
result = true
end
Expand All @@ -53,28 +45,50 @@ def self.confirmTopic(user_id, team_id, topic_id, assignment_id)
# Using a DB transaction to ensure atomic inserts
ActiveRecord::Base.transaction do
#check whether user is clicking on a topic which is not going to place him in the waitlist
if !slotAvailable?(topic_id)
sign_up.is_waitlisted = true
if sign_up.save
result = true
end
else
#if slot exist, then confirm the topic for the user and delete all the waitlist for this user
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
team_id = SignedUpTeam.find_team_users(assignment_id, user_id)
signUp = SignedUpTeam.where(topic_id: topic_id).first
signUp.update_attribute('topic_id', topic_id)
result = true
end
result = sign_up_wailisted(assignment_id, sign_up, team_id, topic_id, user_id)
end
end

result
end

def self.sign_up_wailisted(assignment_id, sign_up, team_id, topic_id, user_id)
if !slotAvailable?(topic_id)
sign_up.is_waitlisted = true
if sign_up.save
result = true
end
else
#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, user_id)
end
result
end

def self.cancel_all_wailists(assignment_id, sign_up, team_id, topic_id, user_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
team_id = SignedUpTeam.find_team_users(assignment_id, user_id)
signUp = SignedUpTeam.where(topic_id: topic_id).first
signUp.update_attribute('topic_id', topic_id)
result = 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)
else
sign_up.is_waitlisted = true
end
return 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
Expand All @@ -85,7 +99,7 @@ def self.slotAvailable?(topic_id)
SignUpTopic.slotAvailable?(topic_id)
end

def self.create_dependency_graph(topics,node)
def self.create_dependency_graph(topics, node)
dg = RGL::DirectedAdjacencyGraph.new

#create a graph of the assignment with appropriate dependency
Expand All @@ -109,43 +123,56 @@ def self.create_dependency_graph(topics,node)
dg
end

def self.add_signup_topic ( assignment_id )
@review_rounds = Assignment.find(assignment_id).get_review_rounds
@topics = SignUpTopic.where(assignment_id: assignment_id)

#Use this until you figure out how to initialize this array
#@duedates = SignUpTopic.find_by_sql("SELECT s.id as topic_id FROM sign_up_topics s WHERE s.assignment_id = " + assignment_id.to_s)
@duedates = {}
return @duedates if @topics.nil?
i=0
@topics.each { |topic|
@duedates[i] = {}
@duedates[i]['id'] = topic.id
@duedates[i]['topic_identifier'] = topic.topic_identifier
@duedates[i]['topic_name'] = topic.topic_name

for j in 1..@review_rounds
duedate_subm = TopicDeadline.where(topic_id: topic.id, deadline_type_id: DeadlineType.find_by_name('submission').id, round: j).first
duedate_rev = TopicDeadline.where(topic_id: topic.id, deadline_type_id: DeadlineType.find_by_name('review').id, round: j).first
if !duedate_subm.nil? && !duedate_rev.nil?
@duedates[i]['submission_'+ j.to_s] = DateTime.parse(duedate_subm['due_at'].to_s).strftime("%Y-%m-%d %H:%M:%S")
@duedates[i]['review_'+ j.to_s] = DateTime.parse(duedate_rev['due_at'].to_s).strftime("%Y-%m-%d %H:%M:%S")
else
#the topic is new. so copy deadlines from assignment
set_of_due_dates = DueDate.where(assignment_id: assignment_id)
set_of_due_dates.each { |due_date|
DueDate.assign_topic_deadline(due_date, 0, topic.id)
}
duedate_subm = TopicDeadline.where(topic_id: topic.id, deadline_type_id: DeadlineType.find_by_name('submission').id, round: j).first
duedate_rev = TopicDeadline.where(topic_id: topic.id, deadline_type_id: DeadlineType.find_by_name('review').id, round: j).first
@duedates[i]['submission_'+ j.to_s] = DateTime.parse(duedate_subm['due_at'].to_s).strftime("%Y-%m-%d %H:%M:%S")
@duedates[i]['review_'+ j.to_s] = DateTime.parse(duedate_rev['due_at'].to_s).strftime("%Y-%m-%d %H:%M:%S")
end
end
duedate_subm = TopicDeadline.where(topic_id: topic.id, deadline_type_id: DeadlineType.find_by_name('metareview').id).first
@duedates[i]['submission_'+ (@review_rounds+1).to_s] = !(duedate_subm.nil?)?(DateTime.parse(duedate_subm['due_at'].to_s).strftime("%Y-%m-%d %H:%M:%S")):nil
i = i + 1
}
return @duedates
def self.add_signup_topic (assignment_id)
@review_rounds = Assignment.find(assignment_id).get_review_rounds
@topics = SignUpTopic.where(assignment_id: assignment_id)

#Use this until you figure out how to initialize this array
#@duedates = SignUpTopic.find_by_sql("SELECT s.id as topic_id FROM sign_up_topics s WHERE s.assignment_id = " + assignment_id.to_s)
@duedates = {}
return @duedates if @topics.nil?
@topics.each_with_index do |topic, i|
@duedates[i] = duedate = {}
duedate['id'] = topic.id
duedate['topic_identifier'] = topic.topic_identifier
duedate['topic_name'] = topic.topic_name

for round in 1..@review_rounds
process_review_round(assignment_id, duedate, round, topic)
end

deadline_type_subm = DeadlineType.find_by_name('metareview').id
duedate_subm = TopicDeadline.where(topic_id: topic.id, deadline_type_id: deadline_type_subm).first
subm_string = duedate_subm.nil? ? nil : DateTime.parse(duedate_subm['due_at'].to_s).strftime("%Y-%m-%d %H:%M:%S")
duedate['submission_'+ (@review_rounds+1).to_s] = subm_string
end
@duedates
end

class << self
private
def process_review_round(assignment_id, duedate, round, topic)
duedate_rev, duedate_subm = find_topic_duedates(round, topic)

if duedate_subm.nil? || duedate_rev.nil?
#the topic is new. so copy deadlines from assignment
set_of_due_dates = DueDate.where(assignment_id: assignment_id)
set_of_due_dates.each { |due_date|
DueDate.assign_topic_deadline(due_date, 0, topic.id)
}
duedate_rev, duedate_subm = find_topic_duedates(round, topic)
end

duedate['submission_'+ round.to_s] = DateTime.parse(duedate_subm['due_at'].to_s).strftime("%Y-%m-%d %H:%M:%S")
duedate['review_'+ round.to_s] = DateTime.parse(duedate_rev['due_at'].to_s).strftime("%Y-%m-%d %H:%M:%S")
end

def find_topic_duedates(round, topic)
deadline_type_subm = DeadlineType.find_by_name('submission').id
duedate_subm = TopicDeadline.where(topic_id: topic.id, deadline_type_id: deadline_type_subm, round: round).first
deadline_type_rev = DeadlineType.find_by_name('review').id
duedate_rev = TopicDeadline.where(topic_id: topic.id, deadline_type_id: deadline_type_rev, round: round).first
return duedate_rev, duedate_subm
end
end
end
Loading

0 comments on commit cd18b46

Please sign in to comment.