CSC/ECE 517 Fall 2021 - E2123. Refactor sign up sheet controller.rb

From Expertiza_Wiki
Jump to navigation Jump to search

This wiki page is for the information regarding the changes made for the E2123 OSS assignment for Fall 2021, CSC/ECE 517.

Introduction

Expertiza is a open source project developed on Ruby on Rails. This web application is maintained by the student and faculty at NC State. This application gives complete control to the instructor to maintain the assignments in their class. With multiple functionalities such as adding topics, creating groups, and peer reviews, Expertiza is a well developed application that can handle all types of assignments. To learn more about the full functionality Expertiza has to offer, visit the Expertiza wiki.

Peer Review Information

For users intending to view the deployed Expertiza associated with this assignment, the credentials are below:

  • Instructor login: username -> instructor6, password -> password

About Controller

The sign up sheet controller

  • Allows an instructor to add/remove topics
  • Allows an instructor to assign/remove students to topics

Issues

1. Naming is inconsistent. When used as a verb, “sign up” is two words. When an adjective or a noun, it should be one word, e.g., “signup_sheet_controller.” Cf. “sign_up_as_instructor”. Please make the naming consistent. Of course, this will result in changes in calling methods as well.

2. Create method has and if-else condition determining if create or update should be called. Create method should not be responsible for calling update. Identify why the if-else condition exists and rectify it. [This may have already been fixed.]

3. Update method has a plethora of instance variables defined before updating. These might not be necessary (For e.g., look at update method of bookmarks_controller)Decide whether so many instance variables are really needed. Refactor the variables not needed out. [This may have already been fixed, but save_topic_deadline also has these problems.]

4. Add_signup_topics_staggered does not do anything different from add add_signup_topics. Separate functions are needed, because add_signup_topics_staggered needs to make sure that the deadlines are set.

5. Several method names can be improved (including: load_add_signup_topics, list, ad_info etc.)

6. What are differences between signup_as_instructor and signup_as_instructor_action methods? Investigate if they are needed and improve the method names if both are needed. Provide comments as to what each method does.

7. The list method is too long and is sparsely commented. Provide comments and identify if the method can be split or made cleaner by moving code to models or helper methods.

8. Refactor participants variable in load_add_signup_topics

9. Signup_as_instructor_action has if-else ladder. It can be made more elegant.

10. Delete_signup and delete_signup_as_instructor have much in common and violate the DRY principle. Refactor.

Solutions

1. Changed the name of the controller from sign_up_sheet_controller.rb --> signup_sheet_controller.rb. Also changed several method names that had 'sign_up' being used as an adjective or a noun to 'signup'.

2. Changed the create method to only create a new topic if that topic doesn't already exist. If it does exist, the program flashes an error message and backs out.

3. Used a single update_attributes method in place of the many instance variables.

Before

def update
    @topic = SignUpTopic.find(params[:id])
    if @topic
      @topic.topic_identifier = params[:topic][:topic_identifier]
      update_max_choosers @topic
      @topic.category = params[:topic][:category]
      @topic.topic_name = params[:topic][:topic_name]
      @topic.micropayment = params[:topic][:micropayment]
      @topic.description = params[:topic][:description]
      @topic.link = params[:topic][:link]
      @topic.save
      params[:topic][:topic_name], micropayment: params[:topic][:micropayment], description: params[:topic][:description], link: params[:topic][:link])
      undo_link("The topic: \"#{@topic.topic_name}\" has been successfully updated. ")
    else
      flash[:error] = "The topic could not be updated."
    end
    # Akshay - correctly changing the redirection url to topics tab in edit assignment view.
    redirect_to edit_assignment_path(params[:assignment_id]) + "#tabs-2"
  end

After

def update
    @topic = SignUpTopic.find(params[:id])
    if @topic
      update_max_choosers @topic
      updated_max_choosers = @topic.max_choosers
      @topic.update_attributes(topic_identifier: params[:topic][:topic_identifier], max_choosers: updated_max_choosers, category: params[:topic][:category], topic_name: params[:topic][:topic_name], micropayment: params[:topic][:micropayment], description: params[:topic][:description], link: params[:topic][:link])
      undo_link("The topic: \"#{@topic.topic_name}\" has been successfully updated. ")
    else
      flash[:error] = "The topic could not be updated."
    end
    # Akshay - correctly changing the redirection url to topics tab in edit assignment view.
    redirect_to edit_assignment_path(params[:assignment_id]) + "#tabs-2"
  end

4. The requirement that deadlines be submitted is no longer implemented and therefore a separate function for "staggered" is no longer needed.

5. Load_add_signup_topics was removed and combined with add_signup_topics. Ad_info was renamed to ads_for_topic and refactored to remove instance variables.

Before

def add_signup_topics
    load_add_signup_topics(params[:id])
    SignUpSheet.add_signup_topic(params[:id])
  end

  def add_signup_topics_staggered
    add_signup_topics
  end

  # retrieves all the data associated with the given assignment. Includes all topics,
def load_add_signup_topics(assignment_id)
    @id = assignment_id
    @sign_up_topics = SignUpTopic.where('assignment_id = ?', assignment_id)
    @slots_filled = SignUpTopic.find_slots_filled(assignment_id)
    @slots_waitlisted = SignUpTopic.find_slots_waitlisted(assignment_id)

    @assignment = Assignment.find(assignment_id)
    # ACS Removed the if condition (and corresponding else) which differentiate assignments as team and individual assignments
    # to treat all assignments as team assignments
    # Though called participants, @participants are actually records in signed_up_teams table, which
    # is a mapping table between teams and topics (waitlisted recored are also counted)
    ip = session[:ip]
    @participants = SignedUpTeam.find_team_participants(@id, ip)
  end

def show_team
    if !(assignment = Assignment.find(params[:assignment_id])).nil? and !(topic = SignUpTopic.find(params[:id])).nil?
      @results = ad_info(assignment.id, topic.id)
    .........

def ad_info(_assignment_id, topic_id)
    # List that contains individual result object
    @result_list = []
    # Get the results
    @results = SignedUpTeam.where("topic_id = ?", topic_id.to_s)
    # Iterate through the results of the query and get the required attributes
    @results.each do |result|
      team = result.team
      topic = result.topic
      resultMap = {}
      resultMap[:team_id] = team.id
      resultMap[:comments_for_advertisement] = team.comments_for_advertisement
      resultMap[:name] = team.name
      resultMap[:assignment_id] = topic.assignment_id
      resultMap[:advertise_for_partner] = team.advertise_for_partner

      # Append to the list
      @result_list.append(resultMap)
    end
    @result_list
  end

After

 def add_signup_topics
    / load_add_signup_topics(params[:id])/
    # retrieves all the data associated with the given assignment. Includes all topics,
    @id = params[:id]
    @sign_up_topics = SignUpTopic.where('assignment_id = ?', params[:id])
    @slots_filled = SignUpTopic.find_slots_filled(params[:id])
    @slots_waitlisted = SignUpTopic.find_slots_waitlisted(params[:id])

    @assignment = Assignment.find(params[:id])
    # ACS Removed the if condition (and corresponding else) which differentiate assignments as team and individual assignments
    # to treat all assignments as team assignments
    # Though called participants, @participants are actually records in signed_up_teams table, which
    # is a mapping table between teams and topics (waitlisted recored are also counted)
    @participants = SignedUpTeam.find_team_participants(params[:id], session[:ip])
    SignUpSheet.add_signup_topic(params[:id])
  end

def add_signup_topics_staggered
    add_signup_topics
  end

def show_team
    if !(assignment = Assignment.find(params[:assignment_id])).nil? and !(topic = SignUpTopic.find(params[:id])).nil?
      @results = ads_for_topic(assignment.id, topic.id)
    .........

def ads_for_topic(_assignment_id, topic_id)
    # List that contains individual result object
    ads = []
    # Get the results
    signed_up_teams = SignedUpTeam.where("topic_id = ?", topic_id.to_s)
    # Iterate through the results of the query and get the required attributes
    signed_up_teams.each do |result|
      team = result.team
      topic = result.topic
      new_ad = {}
      new_ad[:team_id] = team.id
      new_ad[:comments_for_advertisement] = team.comments_for_advertisement
      new_ad[:name] = team.name
      new_ad[:assignment_id] = topic.assignment_id
      new_ad[:advertise_for_partner] = team.advertise_for_partner

      # Append to the list
      ads.append(new_ad)
    end
    ads
  end

6. signup_as_instructor is not needed and has been removed.

7. Comments on the list method have been provided, but the code is best left in the list method.

8. Refactored the participants variable and improved readability.

Before

@participants = SignedUpTeam.find_team_participants(assignment_id, session[:ip])

After

ip = session[:ip]
@participants = SignedUpTeam.find_team_participants(@id, ip)

9. Previously signup_as_instructor_action provided unhelpful feedback to the instructor by only stating if the instructor could/couldn't assign a topic for a student. The refactored code provides the instructor with the student id of the person that they are assigning, the team that the student is on, and the topic that the instructor is assigning to the team.

Before

def signup_as_instructor_action
    user = User.find_by(name: params[:username])
    if user.nil? # validate invalid user
      flash[:error] = "That student does not exist!"
    else
      if AssignmentParticipant.exists? user_id: user.id, parent_id: params[:assignment_id]
        if SignUpSheet.signup_team(params[:assignment_id], user.id, params[:topic_id])
          flash[:success] = "You have successfully signed up the student for the topic!"
          ExpertizaLogger.info LoggerMessage.new(controller_name, '', 'Instructor signed up student for topic: ' + params[:topic_id].to_s)
        else
          flash[:error] = "The student has already signed up for a topic!"
          ExpertizaLogger.info LoggerMessage.new(controller_name, '', 'Instructor is signing up a student who already has a topic')
        end
      else
        flash[:error] = "The student is not registered for the assignment!"
        ExpertizaLogger.info LoggerMessage.new(controller_name, '', 'The student is not registered for the assignment: ' << user.id)
      end
    end
    redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]
  end
  

After

def signup_as_instructor_action
    #put name of student and team in log
    # flash name of student and team
    user = User.find_by(name: params[:username])
    team = Team.find_team_for_assignment_and_user(params[:assignment_id], user.id).first
    assignment = Assignment.find(params[:assignment_id])
    if user.nil? # validate invalid user
      flash[:error] = user.name + " does not exist!"
    else #If the user is not null and the student exists then sign up student for topic
      if AssignmentParticipant.exists? user_id: user.id, parent_id: params[:assignment_id]
        if SignUpSheet.signup_team(params[:assignment_id], user.id, params[:topic_id])
          flash[:success] = "You have successfully signed up " + user.name + " on " + team.name + "  for the topic " + params[:topic_id].to_s
          ExpertizaLogger.info LoggerMessage.new(controller_name, '', 'Instructor signed up ' + user.name + ' on team ' + team.name + ' for topic: ' + params[:topic_id].to_s)
        else #If the students team already has a topic then cancel the sign up
          flash[:error] = user.name + " on " + team.name + " has already signed up for a topic!"
          ExpertizaLogger.info LoggerMessage.new(controller_name, '', 'Instructor is signing up a ' + user.name + 'on ' + team.name + ' who already has a topic')
        end
      else #If the student isn't a part of the assignment then cancel the sign up
        flash[:error] = user.name + " is not registered for the assignment!"
        ExpertizaLogger.info LoggerMessage.new(controller_name, '', user.name + ' is not registered for the assignment: ' + assignment.name)
      end
    end
    redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]
  end
  

10. delete_signup and delete_signup_as_instructor violated the DRY principle due to both functions using identical if/else statements. To solve this issue, delete_signup_heleper function was created.

Before

def delete_signup
    participant = AssignmentParticipant.find(params[:id])
    assignment = participant.assignment
    drop_topic_deadline = assignment.due_dates.find_by(deadline_type_id: 6)
    # A student who has already submitted work should not be allowed to drop his/her topic!
    # (A student/team has submitted if participant directory_num is non-null or submitted_hyperlinks is non-null.)
    # If there is no drop topic deadline, student can drop topic at any time (if all the submissions are deleted)
    # If there is a drop topic deadline, student cannot drop topic after this deadline.
    if !participant.team.submitted_files.empty? or !participant.team.hyperlinks.empty?
      flash[:error] = "You have already submitted your work, so you are not allowed to drop your topic."
      ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].id, 'Dropping topic for already submitted a work: ' + params[:topic_id].to_s)
    elsif !drop_topic_deadline.nil? and Time.now > drop_topic_deadline.due_at
      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)
      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
    redirect_to action: 'list', id: params[:id]
  end

  def delete_signup_as_instructor
    # find participant using assignment using team and topic ids
    team = Team.find(params[:id])
    assignment = Assignment.find(team.parent_id)
    user = TeamsUser.find_by(team_id: team.id).user
    participant = AssignmentParticipant.find_by(user_id: user.id, parent_id: assignment.id)
    drop_topic_deadline = assignment.due_dates.find_by(deadline_type_id: 6)
    if !participant.team.submitted_files.empty? or !participant.team.hyperlinks.empty?
      flash[:error] = "The student has already submitted their work, so you are not allowed to remove them."
      ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].id, 'Drop failed for already submitted work: ' + params[:topic_id].to_s)
    elsif !drop_topic_deadline.nil? and Time.now > drop_topic_deadline.due_at
      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)
      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
    redirect_to controller: 'assignments', action: 'edit', id: assignment.id
  end

After

def delete_signup_helper(isInstructor, participant, assignment, drop_topic_deadline)
    messages = ["You have already submitted your work, so you are not allowed to drop your topic.",
                "You cannot drop your topic after the drop topic deadline!",
                "You have successfully dropped your topic!",
                "The team has already submitted their work, so you are not allowed to remove them.",
                "You cannot drop a student after the drop topic deadline!",
                "You have successfully dropped the student from the topic!"
    ]
    if isInstructor == false
      flash1 = messages[0]
      flash2 = messages[1]
      flash3 = messages[2]
      id_param = session[:user]
    else
      flash1 = messages[3]
      flash2 = messages[4]
      flash3 = messages[5]
      id_param = participant
    end

    if !participant.team.submitted_files.empty? or !participant.team.hyperlinks.empty?
      flash[:error] = flash1
      ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].id, 'Dropping failed for already submitted a work: ' + params[:topic_id].to_s)
    elsif !drop_topic_deadline.nil? and Time.now > drop_topic_deadline.due_at
      flash[:error] = flash2
      ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].id, 'Dropping failed for ended work: ' + params[:topic_id].to_s)
    else
      delete_signup_for_topic(assignment.id, params[:topic_id], id_param.id)
      flash[:success] = flash3
      ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].id, 'Student has dropped the topic: ' + params[:topic_id].to_s)
    end
  end

  # this function is used to delete a previous signup
  def delete_signup
    participant = AssignmentParticipant.find(params[:id])
    assignment = participant.assignment
    drop_topic_deadline = assignment.due_dates.find_by(deadline_type_id: 6)
    # A student who has already submitted work should not be allowed to drop his/her topic!
    # (A student/team has submitted if participant directory_num is non-null or submitted_hyperlinks is non-null.)
    # If there is no drop topic deadline, student can drop topic at any time (if all the submissions are deleted)
    # If there is a drop topic deadline, student cannot drop topic after this deadline.
    delete_signup_helper(false, participant, assignment, drop_topic_deadline)
    redirect_to action: 'list', id: params[:id]
  end

  def delete_signup_as_instructor
    # find participant using assignment using team and topic ids
    team = Team.find(params[:id])
    assignment = Assignment.find(team.parent_id)
    user = TeamsUser.find_by(team_id: team.id).user
    participant = AssignmentParticipant.find_by(user_id: user.id, parent_id: assignment.id)
    drop_topic_deadline = assignment.due_dates.find_by(deadline_type_id: 6)
    delete_signup_helper(true, participant, assignment, drop_topic_deadline)
    redirect_to controller: 'assignments', action: 'edit', id: assignment.id
  end

Testing

RSpec
signup_as_instructor_action is the only function updated. All other functions has remained the same. To run the spec file, run:
rspec spec/controllers/signup_sheet_controller_spec.rb
Physical Testing
  1. Login as instructor
  2. Hover on Manage Tab
  3. Click on Assignments Tab
  4. Click on Edit Button for "OSS project & documentation" assignment
  5. Select the Topic Tab
  6. Click the Green Check for any topic
  7. Enter a student UnityID and click submit