CSC/ECE 517 Fall 2021 - E2130. Refactor submitted content controller.rb: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(19 intermediate revisions by one other user not shown)
Line 1: Line 1:
'''E2130: Refactor 'submitted_content_controller' Controller'''
'''E2130: Refactor 'submitted_content_controller' '''


This page provides detailed explanation of the Submitted Content Controller which is a part of the '''Expertiza''' project. The aim of the project is to refactor the 'submitted_content_controller', which contains the methods to submit assignment related information such as submit & remove hyperlinks, files and other relevant information that could be part of the assignment. It also handles views based on the user roles and permissions they have.
This page provides detailed explanation of the Submitted Content Controller which is a part of the '''Expertiza''' project. The aim of the project is to refactor the 'submitted_content_controller', which contains the methods to submit assignment related information such as submit & remove hyperlinks, files and other relevant information that could be part of the assignment. It also handles views based on the user roles and permissions they have.
The project involved refactoring some parts of the controller to incorporate the OODD principles so that the readability of code could be improved.  
The project involved refactoring some parts of the controller to incorporate the OODD principles so that the readability of code could be improved.  


==Introduction to Expertiza==
==About Expertiza==
[http://expertiza.ncsu.edu/ Expertiza] is a peer review based system which provides incremental learning from the class. This project has been developed together by faculty and students using [http://rubyonrails.org/ Ruby on Rails] framework. Expertiza allows the instructor to create, edit and delete assignments, create new assignment topics, assign them to a particular class or selected students, have students work on teams and then review each other's assignments at the end. For the students, they can signup for topics, form teams, and submit their projects and assignments.  
The [http://expertiza.ncsu.edu/ Expertiza] platform employs a divide-and-conquer strategy for creating reusable learning objects via active-learning exercises built entirely on [http://rubyonrails.org/ Ruby on Rails] framework. Students get to choose from a list of tasks to complete either individually or in teams. They then prepare their work and submit it to a peer-review mechanism. On submission, other students can assess their peers work and provide feedback. Expertiza encourages students to collaborate in order to improve the learning experiences from one another. It aids their learning by making them translate what is taught in the lectures and apply those concepts to a real-world issue.
Students then review the work done by other students and give suggestions to improve. Teams after reviews are allotted scores and they can refer to the peer comments to further improve their work. It also supports submission of different file types for assignments, including the URLs and wiki pages.


== Project Description ==
==Problem Statement==
'''submitted_content_Controller''' had some problems that violate some essential Rails design principles which needed to be rectified. There are few methods in this controller that are far too long and need to be broken down into multiple methods. A few methods need to be commented as well. Few Methods need to be renamed.  
'''submitted_content_controller''' had some problems that violate essential Rails design principles which needed to be rectified. Issues included some methods being too long which needed to be broken down, a few methods needed better naming and a few that were no longer needed.


Broadly, the following issues were addressed as a part of refactoring this controller:
* Renaming methods to more appropriate and functionality specific names.
* The existing code was reused to perform either the same function or re-purposed to do a similar function adhering to standards and improving overall quality of the code.
* Introduction of modular code in order to make each module easier to understand, test and refactor independently of others.


===Create Method===
==Problems and Solutions ==
The method '''Create''' is called when an instructor tries to create a team manually. This works for both, creating ''assignment teams'' and ''course teams''. Assignment teams are teams made to do a particular assignment together and ''Course Teams'' are teams which are made for the whole course.
* '''Problem 1''': action_allowed method should be changed to use new_access_control methods.  
 
:The original code does not make use of the new access control methods which are a part of the authorization_helper.rb
===Delete Method===
<pre>
The method '''Delete''' is called when an instructor tries to delete a team manually. This works for both, deleting ''assignment teams'' and ''course teams''.
def action_allowed?
 
    ['Instructor',
===List Method===
    'Teaching Assistant',
The method '''List''' lists all the team nodes and the instructor is able to expand each team node to see the user nodes as well.
    'Administrator',
 
    'Super-Administrator',
===Inherit Method===
    'Student'].include? current_role_name and
The method '''Inherit''' inherits teams from course to assignments, but the “Inherit Teams From Course” option should not display when either 1) we are editing a course object or 2) the current assignment object does not have a course associated with it.
     ((%w[edit].include? action_name) ? are_needed_authorizations_present?(params[:id], "reader", "reviewer") : true) and
 
     one_team_can_submit_work?
===Running Tests===
  rspec spec/controllers/submitted_content_controller_spec.rb
 
===Files Modified===
  1. teams_controller.rb
  2. team.rb
  3. signed_up_team.rb
  4. routes.rb
  5. app/views/teams/new.html.erb
 
== Refactoring ==
[https://en.wikipedia.org/wiki/Code_refactoring '''Refactoring'''] is restructuring of code without the need of changing any external behavior. It reduces complexity and improves readability. It also becomes easy to extend the application with respect to different modules and their functionalities.
Some common techniques to refactor are:
 
* Moving methods to appropriate modules
* Breaking methods into more meaningful functionality
* Creating more generalized code.
* Renaming methods and variable.
* Inheritance
 
== Tasks ==
 
===Refactoring delete method===
Description: The delete method manipulates a waitlist. This code is moved to signed_up_team.rb model class.
 
Initially, the code was:
 
  def delete
     # delete records in team, teams_users, signed_up_teams table
    @team = Team.find_by(id: params[:id])
    unless @team.nil?
      course = Object.const_get(session[:team_type]).find(@team.parent_id)
      @signed_up_team = SignedUpTeam.where(team_id: @team.id)
      @teams_users = TeamsUser.where(team_id: @team.id)
      if @signed_up_team == 1 && !@signUps.first.is_waitlisted # this team hold a topic
        # if there is another team in waitlist, make this team hold this topic
        topic_id = @signed_up_team.first.topic_id
        next_wait_listed_team = SignedUpTeam.where(topic_id: topic_id, is_waitlisted: true).first
        # if slot exist, then confirm the topic for this team and delete all waitlists for this team
        SignUpTopic.assign_to_first_waiting_team(next_wait_listed_team) if next_wait_listed_team
      end
      @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.")
     end
    redirect_to :back
   end
   end
 
</pre>
Which was changed to:
* '''Solution''': On using the new access control methods:  
 
<pre>
  def delete
   def action_allowed?
    # delete records in team, teams_users, signed_up_teams table
     if %w[edit update list_submissions].include? params[:action]
    @team = Team.find_by(id: params[:id])
       current_user_has_admin_privileges? || current_user_teaching_staff_of_assignment?(params[:id])
    unless @team.nil?
      course = Object.const_get(session[:team_type]).find(@team.parent_id)
      @signed_up_team = SignedUpTeam.where(team_id: @team.id)
      @teams_users = TeamsUser.where(team_id: @team.id)
      # On team deletion topic team was holding will be assigned to first team in waitlist.
      SignedUpTeam.assign_topic_to_first_in_waitlist_post_team_deletion(@signed_up_team, @signups)
      @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.")
    end
    redirect_to :back
  end
 
and a new method was introduced in signed_up_team.rb model:
  # this method checks when a team is deleted if there is a team in waitlist for the topic
  # deleted team was holding, then assign topic to first team in waitlist
   def self.assign_topic_to_first_in_waitlist_post_team_deletion (signed_up_team, signups)
     if signed_up_team == 1 && !signups.first.is_waitlisted # this team hold a topic
      # if there is another team in waitlist, make this team hold this topic
      topic_id = signed_up_team.first.topic_id
      next_wait_listed_team = SignedUpTeam.where(topic_id: topic_id, is_waitlisted: true).first
      # if slot exist, then confirm the topic for this team and delete all waitlists for this team
      SignUpTopic.assign_to_first_waiting_team(next_wait_listed_team) if next_wait_listed_team
    end
  end
 
===Refactoring inherit method===
 
Description: The code that copies a list is moved to team.rb model class.
 
Initially, the code was:
  def inherit
    assignment = Assignment.find(params[:id])
    if assignment.course_id >= 0
      course = Course.find(assignment.course_id)
      teams = course.get_teams
       unless teams.empty?
        teams.each do |team|
          team.copy(assignment.id)
        end
      else
        flash[:note] = "No teams were found when trying to inherit."
      end
     else
     else
       flash[:error] = "No course was found for this assignment."
       current_user_has_ta_privileges?
     end
     end
    redirect_to controller: 'teams', action: 'list', id: assignment.id
  end


Which was changed to:
     case params[:action]
  def inherit
     when 'edit'
     assignment = Assignment.find(params[:id])
       current_user_has_student_privileges? &&
     if assignment.course_id >= 0
       are_needed_authorizations_present?(params[:id], "reader", "reviewer")
       course = Course.find(assignment.course_id)
    when 'submit_file', 'submit_hyperlink'
       teams = course.get_teams
      current_user_has_student_privileges? &&
      unless teams.empty?
       one_team_can_submit_work?
        # copy_assignment method copies teams to assignment
        Team.copy_assignment(teams,assignment)
      else
        flash[:note] = "No teams were found when trying to inherit."
       end
     else
     else
       flash[:error] = "No course was found for this assignment."
       current_user_has_student_privileges?
     end
     end
    redirect_to controller: 'teams', action: 'list', id: assignment.id
   end
   end
</pre>


and a new method was introduced in team.rb model:
2. '''Problem 2''': Remove the comment  “# hence use team count for the check”.
  def self.copy_assignment(teams,assignment)
::The current code no longer checks for the team count to see if a participant belongs to a team. Comment on line #19 removed.
    teams.each do |team|
      team.copy(assignment.id)
    end
  end


===Refactoring create method===


Description: It creates new teams against a parent id
3. '''Problem 3''': Change the method name view to something more informative of the method.
 
:The original code uses the generic 'view' method name to display a view corresponding to a case when submissions cannot be accepted, for instance in the case when a deadline is passed.
Initially the create method was:
<pre>
 
def view
  def create
     @participant = AssignmentParticipant.find(params[:id])
     parent = Object.const_get(session[:team_type]).find(params[:id])
     return unless current_user_id?(@participant.user_id)
     begin
    @assignment = @participant.assignment
      Team.check_for_existing(parent, params[:team][:name], session[:team_type])
    # @can_submit is the flag indicating if the user can submit or not in current stage
      @team = Object.const_get(session[:team_type] + 'Team').create(name: params[:team][:name], parent_id: parent.id)
    @can_submit = false
      TeamNode.create(parent_id: parent.id, node_object_id: @team.id)
    @stage = @assignment.get_current_stage(SignedUpTeam.topic_id(@participant.parent_id, @participant.user_id))
      undo_link("The team \"#{@team.name}\" has been successfully created.")
    redirect_to action: 'edit', id: params[:id], view: true
      redirect_to action: 'list', id: parent.id
    rescue TeamExistsError
      flash[:error] = $ERROR_INFO
      redirect_to action: 'new', id: parent.id
    end
   end
   end
 
</pre>
Which changed to:
* '''Solution''': We found changing the name to disable_submission to be more apt in this case
 
<pre>
  def create
def disable_submission
     begin
     @participant = AssignmentParticipant.find(params[:id])
      parent = get_parent_and_check_if_exists(params[:id])
    return unless current_user_id?(@participant.user_id)
      @team = Object.const_get(session[:team_type] + 'Team').create(name: params[:team][:name], parent_id: parent.id)
    @assignment = @participant.assignment
      TeamNode.create(parent_id: parent.id, node_object_id: @team.id)
    # @can_submit is the flag indicating if the user can submit or not in current stage
      undo_link("The team \"#{@team.name}\" has been successfully created.")
    @can_submit = false
      redirect_to action: 'list', id: parent.id
    @stage = @assignment.get_current_stage(SignedUpTeam.topic_id(@participant.parent_id, @participant.user_id))
    rescue TeamExistsError
    redirect_to action: 'edit', id: params[:id], view: true
      flash_and_redirect_on_update_or_create_error('new', parent_id)
    end
   end
   end
</pre>


  # called to fetch parent and check if team with same name and type already exists.
4. '''Problem 4''': submit_hyperlink is filled with a lot of logging code.
  def get_parent_and_check_if_exists(parent_id)
:Logging is essential to understand the behavior of the application and to debug unexpected issues or for simply tracking events as in the production environment, we can’t debug issues without proper log files.
     parent = Object.const_get(session[:team_type]).find(parent_id)
* '''Solution''': We added a function for the sole purpose of logging so that it can be reused wherever required.
    Team.check_for_existing(parent, params[:team][:name], session[:team_type])
<pre>
    return parent
def log_info(controller_name, participant_name, message, request)
     ExpertizaLogger.info LoggerMessage.new(controller_name, participant_name, message, request)
   end
   end
</pre>


  # to flash and redirect the user when there is any update or create error
  def flash_and_redirect_on_update_or_create_error(action, id)
    flash[:error] = $ERROR_INFO
    redirect_to action: action, id: id
  end


===Refactoring update method===
5. '''Problem 5''': Check the validity of the comment ”# Note: This is not used yet in the view until we all decide to do so”.
:
* '''Solution''': The function to remove_hyperlink performs as expected, hence the comment is no longer valid.


Description: It updates an existing team name


Initially the update method was:
6. '''Problem 6''': submit_file method is long.
:
* '''Solution''': Implemented modular code by separating the function into simpler functions.
<pre>
  def tested
    @current_folder = DisplayOption.new
    @current_folder.name = "/"
    @current_folder.name = FileHelper.sanitize_folder(params[:current_folder][:name]) if params[:current_folder]
    curr_directory = if params[:origin] == 'review'
                      participant.review_file_path(params[:response_map_id]).to_s + @current_folder.name
                    else
                      participant.team.path.to_s + @current_folder.name
                    end
    return curr_directory
  end
</pre>


  def update
7. '''Problem 7''': Move mail_assigned_reviewers to a mailer class.
    @team = Team.find(params[:id])
:
     parent = Object.const_get(session[:team_type]).find(@team.parent_id)
* '''Solution''': Moved the mail_assigned_reviewers to mailer_helper.rb
     begin
<pre>
       Team.check_for_existing(parent, params[:team][:name], session[:team_type])
def self.mail_assigned_reviewers(team)
      @team.name = params[:team][:name]
     maps = ResponseMap.where(reviewed_object_id: @participant.assignment.id, reviewee_id: team.id, type: 'ReviewResponseMap')
      @team.save
     unless maps.nil?
      flash[:success] = "The team \"#{@team.name}\" has been successfully updated."
       maps.each do |map|
      undo_link("")
        # Mailing function
      redirect_to action: 'list', id: parent.id
        Mailer.general_email(
    rescue TeamExistsError
          to: User.find(Participant.find(map.reviewer_id).user_id).email,
      flash[:error] = $ERROR_INFO
          subject: "Link to update the review for Assignment '#{@participant.assignment.name}'",
      redirect_to action: 'edit', id: @team.id
          cc: User.find_by(@participant.assignment.instructor_id).email,
          link: "Link: https://expertiza.ncsu.edu/response/new?id=#{map.id}",
          assignment: @participant.assignment.name
        ).deliver_now
      end
     end
     end
   end
   end
</pre>


Which changed to:  
8. '''Problem 8''': Change the method name of folder_action.
 
:
  # It updates an existing team name
* '''Solution''': Renamed folder_action to perform_folder_action.
  def update
<pre>
     @team = Team.find(params[:id])
def perform_folder_action
     begin
     @participant = AssignmentParticipant.find(params[:id])
      parent = get_parent_and_check_if_exists(@team.parent_id)
     return unless current_user_id?(@participant.user_id)
      @team.name = params[:team][:name]
    @current_folder = DisplayOption.new
       @team.save
    @current_folder.name = "/"
       flash[:success] = "The team \"#{@team.name}\" has been successfully updated."
    @current_folder.name = FileHelper.sanitize_folder(params[:current_folder][:name]) if params[:current_folder]
       undo_link("")
    if params[:faction][:delete]
      redirect_to action: 'list', id: parent.id
       SubmittedFiles.delete_selected_files
     rescue TeamExistsError
    elsif params[:faction][:rename]
       flash_and_redirect_on_update_or_create_error('edit', @team.id)
       SubmittedFiles.rename_selected_file
    elsif params[:faction][:move]
       SubmittedFiles.move_selected_file
    elsif params[:faction][:copy]
      SubmittedFiles.copy_selected_file
     elsif params[:faction][:create]
       create_new_folder
     end
     end
    redirect_to action: 'edit', id: @participant.id
   end
   end
</pre>


  # called to fetch parent and check if team with same name and type already exists.
9. '''Problem 9''': Some of the actions in the code (from line 187 to 247) could perhaps be moved to another class
  def get_parent_and_check_if_exists(parent_id)
:
    parent = Object.const_get(session[:team_type]).find(parent_id)
* '''Solution''': Separated functions related to submission of files into a new helper class called submitted_files_helper.rb.
    Team.check_for_existing(parent, params[:team][:name], session[:team_type])
<pre>
    return parent
module SubmittedFiles
  end
 
  # to flash and redirect the user when there is any update or create error
  def flash_and_redirect_on_update_or_create_error(action, id)
    flash[:error] = $ERROR_INFO
    redirect_to action: action, id: id
  end
 
 
===Create Teams===
 
The new method self.create_teams() is created in Team.rb model and placed the common of create_teams method.
  # This function is used to create teams with random names.
  # Instructors can call by clicking "Create temas" icon anc then click "Create teams" at the bottom.
  def self.create_teams(session,params)
    parent = Object.const_get(session[:team_type]).find(params[:id])
    Team.randomize_all_by_parent(parent, session[:team_type], params[:team_size].to_i)
  end
 
Exiting code of create_teams() method in teams_controller.rb


  # This function is used to create teams with random names.
def move_selected_file
   # Instructors can call by clicking "Create temas" icon anc then click "Create teams" at the bottom.
   old_filename = params[:directories][params[:chk_files]] + "/" + params[:filenames][params[:chk_files]]
  def create_teams
  newloc = @participant.dir_path
    parent = Object.const_get(session[:team_type]).find(params[:id])
  newloc += "/"
    Team.randomize_all_by_parent(parent, session[:team_type], params[:team_size].to_i)
  newloc += params[:faction][:move]
    undo_link("Random teams have been successfully created.")
   begin
    ExpertizaLogger.info LoggerMessage.new(controller_name, '', 'Random teams have been successfully created', request)
      FileHelper.move_file(old_filename, newloc)
    redirect_to action: 'list', id: parent.id
      flash[:note] = "The file was successfully moved from \"/#{params[:filenames][params[:chk_files]]}\" to \"/#{params[:faction][:move]}\""
   end
     rescue StandardError => e
 
       flash[:error] = "There was a problem moving the file: " + e.message
Modified the teams_controller.rb file and it is now using the Team.create_teams(session,params) present in Team.rb model to create teams.
 
  # This function is used to create teams with random names.
  # Instructors can call by clicking "Create temas" icon anc then click "Create teams" at the bottom.
  def create_teams
    Team.create_teams(session,params)
    undo_link("Random teams have been successfully created.")
    ExpertizaLogger.info LoggerMessage.new(controller_name, '', 'Random teams have been successfully created', request)
    redirect_to action: 'list', id: parent.id
  end
 
===signUps should be signups===
Existing code for the signUps nouns are modified to signups.
 
Existing code teams_controller.rb
 
def delete
    # delete records in team, teams_users, signed_up_teams table
    @team = Team.find_by(id: params[:id])
     unless @team.nil?
       course = Object.const_get(session[:team_type]).find(@team.parent_id)
      @signed_up_team = SignedUpTeam.where(team_id: @team.id)
      @teams_users = TeamsUser.where(team_id: @team.id)
 
      SignedUpTeam.assign_topic_to_first_in_waitlist_post_team_deletion(@signed_up_team, @signUps)
 
      @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.")
     end
     end
    redirect_to :back
end
  end


Modified code in teams_controller.rb


def delete
def rename_selected_file
    # delete records in team, teams_users, signed_up_teams table
old_filename = params[:directories][params[:chk_files]] + "/" + params[:filenames][params[:chk_files]]
    @team = Team.find_by(id: params[:id])
new_filename = params[:directories][params[:chk_files]] + "/" + FileHelper.sanitize_filename(params[:faction][:rename])
    unless @team.nil?
begin
      course = Object.const_get(session[:team_type]).find(@team.parent_id)
  raise "A file already exists in this directory with the name \"#{params[:faction][:rename]}\"" if File.exist?(new_filename)
      @signed_up_team = SignedUpTeam.where(team_id: @team.id)
  File.send("rename", old_filename, new_filename)
      @teams_users = TeamsUser.where(team_id: @team.id)
rescue StandardError => e
  flash[:error] = "There was a problem renaming the file: " + e.message
end
end


      SignedUpTeam.assign_topic_to_first_in_waitlist_post_team_deletion(@signed_up_team, @signups)


      @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.")
    end
    redirect_to :back
  end


Existing code signed_up_team.rb
def delete_selected_files


   def self.assign_topic_to_first_in_waitlist_post_team_deletion (signed_up_team, signUps)
   filename = params[:directories][params[:chk_files]] + "/" + params[:filenames][params[:chk_files]]
    if signed_up_team == 1 && !signups.first.is_waitlisted # this team hold a topic
  FileUtils.rm_r(filename)
      # if there is another team in waitlist, make this team hold this topic
  participant = Participant.find_by(id: params[:id])
      topic_id = signed_up_team.first.topic_id
  assignment = participant.try(:assignment)
      next_wait_listed_team = SignedUpTeam.where(topic_id: topic_id, is_waitlisted: true).first
  team = participant.try(:team)
      # if slot exist, then confirm the topic for this team and delete all waitlists for this team
  SubmissionRecord.create(team_id: team.try(:id),
      SignUpTopic.assign_to_first_waiting_team(next_wait_listed_team) if next_wait_listed_team
                          content: filename,
    end
                          user: participant.try(:name),
  end
                          assignment_id: assignment.try(:id),
 
                          operation: "Remove File")
Modified code in signed_up_team.rb
  ExpertizaLogger.info LoggerMessage.new(controller_name, @participant.name, 'The selected file has been deleted.', request)
end


   def self.assign_topic_to_first_in_waitlist_post_team_deletion (signed_up_team, signups)
def copy_selected_file
     if signed_up_team == 1 && !signups.first.is_waitlisted # this team hold a topic
   old_filename = params[:directories][params[:chk_files]] + "/" + params[:filenames][params[:chk_files]]
      # if there is another team in waitlist, make this team hold this topic
  new_filename = params[:directories][params[:chk_files]] + "/" + FileHelper.sanitize_filename(params[:faction][:copy])
      topic_id = signed_up_team.first.topic_id
  begin
      next_wait_listed_team = SignedUpTeam.where(topic_id: topic_id, is_waitlisted: true).first
     raise "A file with this name already exists. Please delete the existing file before copying." if File.exist?(new_filename)
      # if slot exist, then confirm the topic for this team and delete all waitlists for this team
    raise "The referenced file does not exist." unless File.exist?(old_filename)
      SignUpTopic.assign_to_first_waiting_team(next_wait_listed_team) if next_wait_listed_team
    FileUtils.cp_r(old_filename, new_filename)
    end
  rescue StandardError => e
    flash[:error] = "There was a problem copying the file: " + e.message
   end
   end
end


== Testing the submitted_content_controller ==
end
 
=== Instructions for testing Rspecs ===
 
1. git clone https://github.com/Neelkanth7/expertiza.git
 
2. Change to the expertiza directory and then perform "bundle install" and rake db:migrate.
 
3. Start the rails server
 
4. In a new terminal and in the expertiza directory, perform the following commands:
    i.  rspec spec/controllers/submitted_content_controller_spec.rb
 
 
 
===Testing Inherit Method===
 


===Testing Create Method===
</pre>


== Testing Requirements ==


===Testing Delete Method===
1. git clone https://github.com/Neelkanth7/expertiza
   


===Testing List Method===
2. Change directory to expertiza. Run "bundle install" and rails db:migrate.


3. Start the rails server.


4. Run the following command in a new terminal of the expertiza directory:
    i.  rspec spec/controllers/submitted_content_controller_spec.rb (Test file mentioned missing)


==References==
==References==

Latest revision as of 20:07, 6 November 2021

E2130: Refactor 'submitted_content_controller'

This page provides detailed explanation of the Submitted Content Controller which is a part of the Expertiza project. The aim of the project is to refactor the 'submitted_content_controller', which contains the methods to submit assignment related information such as submit & remove hyperlinks, files and other relevant information that could be part of the assignment. It also handles views based on the user roles and permissions they have. The project involved refactoring some parts of the controller to incorporate the OODD principles so that the readability of code could be improved.

About Expertiza

The Expertiza platform employs a divide-and-conquer strategy for creating reusable learning objects via active-learning exercises built entirely on Ruby on Rails framework. Students get to choose from a list of tasks to complete either individually or in teams. They then prepare their work and submit it to a peer-review mechanism. On submission, other students can assess their peers work and provide feedback. Expertiza encourages students to collaborate in order to improve the learning experiences from one another. It aids their learning by making them translate what is taught in the lectures and apply those concepts to a real-world issue.

Problem Statement

submitted_content_controller had some problems that violate essential Rails design principles which needed to be rectified. Issues included some methods being too long which needed to be broken down, a few methods needed better naming and a few that were no longer needed.

Broadly, the following issues were addressed as a part of refactoring this controller:

  • Renaming methods to more appropriate and functionality specific names.
  • The existing code was reused to perform either the same function or re-purposed to do a similar function adhering to standards and improving overall quality of the code.
  • Introduction of modular code in order to make each module easier to understand, test and refactor independently of others.

Problems and Solutions

  • Problem 1: action_allowed method should be changed to use new_access_control methods.
The original code does not make use of the new access control methods which are a part of the authorization_helper.rb
def action_allowed?
    ['Instructor',
     'Teaching Assistant',
     'Administrator',
     'Super-Administrator',
     'Student'].include? current_role_name and
    ((%w[edit].include? action_name) ? are_needed_authorizations_present?(params[:id], "reader", "reviewer") : true) and
    one_team_can_submit_work?
  end
  • Solution: On using the new access control methods:
  def action_allowed?
    if %w[edit update list_submissions].include? params[:action]
      current_user_has_admin_privileges? || current_user_teaching_staff_of_assignment?(params[:id])
    else
      current_user_has_ta_privileges?
    end

    case params[:action]
    when 'edit'
      current_user_has_student_privileges? &&
      are_needed_authorizations_present?(params[:id], "reader", "reviewer")
    when 'submit_file', 'submit_hyperlink'
      current_user_has_student_privileges? &&
      one_team_can_submit_work?
    else
      current_user_has_student_privileges?
    end
  end

2. Problem 2: Remove the comment “# hence use team count for the check”.

The current code no longer checks for the team count to see if a participant belongs to a team. Comment on line #19 removed.


3. Problem 3: Change the method name view to something more informative of the method.

The original code uses the generic 'view' method name to display a view corresponding to a case when submissions cannot be accepted, for instance in the case when a deadline is passed.
 def view
    @participant = AssignmentParticipant.find(params[:id])
    return unless current_user_id?(@participant.user_id)
    @assignment = @participant.assignment
    # @can_submit is the flag indicating if the user can submit or not in current stage
    @can_submit = false
    @stage = @assignment.get_current_stage(SignedUpTeam.topic_id(@participant.parent_id, @participant.user_id))
    redirect_to action: 'edit', id: params[:id], view: true
  end
  • Solution: We found changing the name to disable_submission to be more apt in this case
 def disable_submission
    @participant = AssignmentParticipant.find(params[:id])
    return unless current_user_id?(@participant.user_id)
    @assignment = @participant.assignment
    # @can_submit is the flag indicating if the user can submit or not in current stage
    @can_submit = false
    @stage = @assignment.get_current_stage(SignedUpTeam.topic_id(@participant.parent_id, @participant.user_id))
    redirect_to action: 'edit', id: params[:id], view: true
  end

4. Problem 4: submit_hyperlink is filled with a lot of logging code.

Logging is essential to understand the behavior of the application and to debug unexpected issues or for simply tracking events as in the production environment, we can’t debug issues without proper log files.
  • Solution: We added a function for the sole purpose of logging so that it can be reused wherever required.
 def log_info(controller_name, participant_name, message, request)
    ExpertizaLogger.info LoggerMessage.new(controller_name, participant_name, message, request)
  end


5. Problem 5: Check the validity of the comment ”# Note: This is not used yet in the view until we all decide to do so”.

  • Solution: The function to remove_hyperlink performs as expected, hence the comment is no longer valid.


6. Problem 6: submit_file method is long.

  • Solution: Implemented modular code by separating the function into simpler functions.
  def tested
    @current_folder = DisplayOption.new
    @current_folder.name = "/"
    @current_folder.name = FileHelper.sanitize_folder(params[:current_folder][:name]) if params[:current_folder]
    curr_directory = if params[:origin] == 'review'
                       participant.review_file_path(params[:response_map_id]).to_s + @current_folder.name
                     else
                       participant.team.path.to_s + @current_folder.name
                     end
    return curr_directory
  end

7. Problem 7: Move mail_assigned_reviewers to a mailer class.

  • Solution: Moved the mail_assigned_reviewers to mailer_helper.rb
 def self.mail_assigned_reviewers(team)
    maps = ResponseMap.where(reviewed_object_id: @participant.assignment.id, reviewee_id: team.id, type: 'ReviewResponseMap')
    unless maps.nil?
      maps.each do |map|
        # Mailing function
        Mailer.general_email(
          to: User.find(Participant.find(map.reviewer_id).user_id).email,
          subject:  "Link to update the review for Assignment '#{@participant.assignment.name}'",
          cc: User.find_by(@participant.assignment.instructor_id).email,
          link: "Link: https://expertiza.ncsu.edu/response/new?id=#{map.id}",
          assignment: @participant.assignment.name
        ).deliver_now
      end
    end
  end

8. Problem 8: Change the method name of folder_action.

  • Solution: Renamed folder_action to perform_folder_action.
 def perform_folder_action
    @participant = AssignmentParticipant.find(params[:id])
    return unless current_user_id?(@participant.user_id)
    @current_folder = DisplayOption.new
    @current_folder.name = "/"
    @current_folder.name = FileHelper.sanitize_folder(params[:current_folder][:name]) if params[:current_folder]
    if params[:faction][:delete]
      SubmittedFiles.delete_selected_files
    elsif params[:faction][:rename]
      SubmittedFiles.rename_selected_file
    elsif params[:faction][:move]
      SubmittedFiles.move_selected_file
    elsif params[:faction][:copy]
      SubmittedFiles.copy_selected_file
    elsif params[:faction][:create]
      create_new_folder
    end
    redirect_to action: 'edit', id: @participant.id
  end

9. Problem 9: Some of the actions in the code (from line 187 to 247) could perhaps be moved to another class

  • Solution: Separated functions related to submission of files into a new helper class called submitted_files_helper.rb.
module SubmittedFiles

def move_selected_file
  old_filename = params[:directories][params[:chk_files]] + "/" + params[:filenames][params[:chk_files]]
  newloc = @participant.dir_path
  newloc += "/"
  newloc += params[:faction][:move]
  begin
      FileHelper.move_file(old_filename, newloc)
      flash[:note] = "The file was successfully moved from \"/#{params[:filenames][params[:chk_files]]}\" to \"/#{params[:faction][:move]}\""
    rescue StandardError => e
      flash[:error] = "There was a problem moving the file: " + e.message
    end
end


def rename_selected_file
old_filename = params[:directories][params[:chk_files]] + "/" + params[:filenames][params[:chk_files]]
new_filename = params[:directories][params[:chk_files]] + "/" + FileHelper.sanitize_filename(params[:faction][:rename])
begin
  raise "A file already exists in this directory with the name \"#{params[:faction][:rename]}\"" if File.exist?(new_filename)
  File.send("rename", old_filename, new_filename)
rescue StandardError => e
  flash[:error] = "There was a problem renaming the file: " + e.message
end
end



def delete_selected_files

  filename = params[:directories][params[:chk_files]] + "/" + params[:filenames][params[:chk_files]]
  FileUtils.rm_r(filename)
  participant = Participant.find_by(id: params[:id])
  assignment = participant.try(:assignment)
  team = participant.try(:team)
  SubmissionRecord.create(team_id: team.try(:id),
                          content: filename,
                          user: participant.try(:name),
                          assignment_id: assignment.try(:id),
                          operation: "Remove File")
  ExpertizaLogger.info LoggerMessage.new(controller_name, @participant.name, 'The selected file has been deleted.', request)
end

def copy_selected_file
  old_filename = params[:directories][params[:chk_files]] + "/" + params[:filenames][params[:chk_files]]
  new_filename = params[:directories][params[:chk_files]] + "/" + FileHelper.sanitize_filename(params[:faction][:copy])
  begin
    raise "A file with this name already exists. Please delete the existing file before copying." if File.exist?(new_filename)
    raise "The referenced file does not exist." unless File.exist?(old_filename)
    FileUtils.cp_r(old_filename, new_filename)
  rescue StandardError => e
    flash[:error] = "There was a problem copying the file: " + e.message
  end
end

end

Testing Requirements

1. git clone https://github.com/Neelkanth7/expertiza

2. Change directory to expertiza. Run "bundle install" and rails db:migrate.

3. Start the rails server.

4. Run the following command in a new terminal of the expertiza directory:

   i.  rspec spec/controllers/submitted_content_controller_spec.rb (Test file mentioned missing)

References

  • Github link to the project.
  • Link to the project description.