CSC/ECE 517 Fall 2014/oss E1458 sst: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 427: Line 427:
:1. [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza]
:1. [http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza]


:2. [http://152.7.99.43:3000/ VCL Link]
:2. [http://152.46.19.211:3000/ VCL Link]


:3. [https://github.com/nixtish/expertiza Git repository]
:3. [https://github.com/sjoshi6/expertiza Git repository]


:4. [https://docs.google.com/a/ncsu.edu/document/d/1Z0xjFZu-Zy-xm73YyUVUgFCtfWgRwhN1rZuThoyF-U0/edit Steps to setup Expertiza]
:4. [https://docs.google.com/a/ncsu.edu/document/d/1Z0xjFZu-Zy-xm73YyUVUgFCtfWgRwhN1rZuThoyF-U0/edit Steps to setup Expertiza]

Revision as of 04:01, 27 October 2014

E1458: Expertiza - Refactoring ResponseController

Expertiza

Expertiza is a peer review based course management system. Expertiza supports project submission,team creation, and review of the submitted material including URLs and wiki pages. Students can manage teammates and can conduct reviews on other's topics and projects. Expertiza is an open source project based on Ruby on Rails.

Background

As a part of the OSS project 1 we were expected to refactor the Response Controller of Expertiza. Response Controller is responsible for managing the review versions and finding the latest responses and fetching the review scores. This wiki provides a detailed walk through of our contributions to the Expertiza project with a focus on refactoring.

Project Description

The response controller allows the user to create and edit responses to questionnaires such as performing a review, rating a teammate or giving feedback to a reviewer. Our project requirement was to perform the following changes :

  • Perform authorization properly.
  • Remove the duplicated methods.
  • Reduce the complexity of the rereview method.
  • Move the functionality incorporated in the controller, to the model, as it is the model's responsibility to implement this functionality.

Refactoring carried out

The following changes have been made in the project, as described in the requirements document.

Perform Authorization correctly

  • Authorization to perform actions was done incorrectly via the redirect_when_disallowed method. It is supposed to be done through the action_allowed? method at the beginning of the class definition. Different authorizations are required for different operations.
  • For example, someone should be allowed to view a response if they wrote the response, or they are the person or on the team whose work the response is applied to, or if they are an instructor or Teaching Assistant for the class. The person who wrote a response should be allowed to edit it, but not the person/team who was being reviewed, nor the instructor and neither the Teaching Assistant for the class.
  • Earlier, the authorization was handled by denying incorrect access using the redirect_when_disallowed method, which was a more error-prone way of controlling access. This method has now been removed, and now the class has an action_allowed? method which does the authorization check and allows the user to perform the action only if he/she has the correct permissions.

Before Refactoring:

redirect_when_disallowed Method was used for authorization purposes.

def redirect_when_disallowed(response)
    # For author feedback, participants need to be able to read feedback submitted by other teammates.
    # If response is anything but author feedback, only the person who wrote feedback should be able to see it.
    if response.map.read_attribute(:type) == 'FeedbackResponseMap' && response.map.assignment.team_assignment?
      team = response.map.reviewer.team
      unless team.has_user session[:user]
        redirect_to '/denied?reason=You are not on the team that wrote this feedback'
      else
        return false
      end
      response.map.read_attribute(:type)
    end
    !current_user_id?(response.map.reviewer.user_id)
end

After Refactoring:

We replaced the redirect_when_disallowed Method by action_allowed? Method.

  def action_allowed?
    case params[:action]
      when 'view', 'edit', 'delete', 'rereview', 'update'
        if response.map.read_attribute(:type) == 'FeedbackResponseMap'
          team = response.map.reviewer.team
          if team.has_user session[:user]
           flag= true
          else
            flag=false
          end
        end
      else
        flag=true
    end
    return flag
  end

Removal of redundant methods

  • There were two copies of the edit, new_feedback and view methods. The second being the newer one, and, according to the rules for method definition, is the one that is currently in use because the latest version overrides the previous versions. We refactored the code by removing the redundant methods for edit, new_feedback and view.

Edit method:

  def edit
    @header = "Edit"
    @next_action = "update"
    @return = params[:return]
    @response = Response.find(params[:id])
    return if redirect_when_disallowed(@response)

    @map = @response.map
    array_not_empty=0
    @review_scores=Array.new
    @prev=Response.all
    for element in @prev
      if (element.map_id==@map.map_id)
        array_not_empty=1
        @review_scores << element
      end
    end
    if @prev.present?
      @sorted=@review_scores.sort { |m1, m2| (m1.version_num and m2.version_num) ? m2.version_num <=> m1.version_num : (m1.version_num ? -1 : 1) }
      @largest_version_num=@sorted[0]
    end
    @response = Response.where(map_id: @map.map_id, version_num:  @largest_version_num.version_num).first
    @modified_object = @response.response_id
    get_content
    @review_scores = Array.new
    @question_type = Array.new
    @questions.each do |question|
      @review_scores << Score.where(response_id: @response.response_id, question_id:  question.id).first
      @question_type << QuestionType.find_by_question_id(question.id)
    end
    # Check whether this is a custom rubric
    if @map.questionnaire.section.eql? "Custom"
      @next_action = "custom_update"
      render :action => 'custom_response'
    else
      # end of special code (except for the end below, to match the if above)
      #**********************
      render :action => 'response'
    end
  end

New_feedback method:

  def new_feedback
    review = Response.find(params[:id])
    if review
      reviewer = AssignmentParticipant.where(user_id: session[:user].id, parent_id:  review.map.assignment.id).first
      map = FeedbackResponseMap.where(reviewed_object_id: review.id, reviewer_id:  reviewer.id).first
      if map.nil?
        map = FeedbackResponseMap.create(:reviewed_object_id => review.id, :reviewer_id => reviewer.id, :reviewee_id => review.map.reviewer.id)
      end
      redirect_to :action => 'new', :id => map.map_id, :return => "feedback"
    else
      redirect_to :back
    end
  end

View method:

  def view
    @response = Response.find(params[:id])
    return if redirect_when_disallowed(@response)
    @map = @response.map
    get_content
    @review_scores = Array.new
    @question_type = Array.new
    @questions.each do |question|
      @review_scores << Score.where(response_id: @map.response_id, question_id:  question.id).first
      @question_type << QuestionType.find_by_question_id(question.id)
    end
  end

Cleaning up of the rereview moethod

  • The rereview method was 98 lines long. We refactored the code by turning several parts of it into methods. Now the code is 81 lines long.

Before Refactoring:

def rereview
    @map=ResponseMap.find(params[:id])
    get_content
    array_not_empty=0
    @review_scores=Array.new
    @prev=Response.all
    #get all versions and find the latest version
    for element in @prev
      if (element.map.id==@map.map.id)
        array_not_empty=1
        @review_scores << element
      end
    end

    latestResponseVersion
    #sort all the available versions in descending order.
    if @prev.present?
      @sorted=@review_scores.sort { |m1, m2| (m1.version_num and m2.version_num) ? m2.version_num <=> m1.version_num : (m1.version_num ? -1 : 1) }
      @largest_version_num=@sorted[0]
      @latest_phase=@largest_version_num.created_at
      due_dates = DueDate.where(["assignment_id = ?", @assignment.id])
      @sorted_deadlines=Array.new
      @sorted_deadlines=due_dates.sort { |m1, m2| (m1.due_at and m2.due_at) ? m1.due_at <=> m2.due_at : (m1.due_at ? -1 : 1) }
      current_time=Time.new.getutc
      #get the highest version numbered review
      next_due_date=@sorted_deadlines[0]
      #check in which phase the latest review was done.
      for deadline_version in @sorted_deadlines
        if (@largest_version_num.created_at < deadline_version.due_at)
          break
        end
      end
      for deadline_time in @sorted_deadlines
        if (current_time < deadline_time.due_at)
          break
        end
      end
    end
    #check if the latest review is done in the current phase.
    #if latest review is in current phase then edit the latest one.
    #else create a new version and update it.
    # editing the latest review
    if (deadline_version.due_at== deadline_time.due_at)
      #send it to edit here
      @header = "Edit"
      @next_action = "update"
      @return = params[:return]
      @response = Response.find_by_map_id_and_version_num(params[:id], @largest_version_num.version_num)
      return if redirect_when_disallowed(@response)
      @modified_object = @response.response_id
      @map = @response.map
      get_content
      @review_scores = Array.new
      @questions.each {
          |question|
        @review_scores << Score.find_by_response_id_and_question_id(@response.response_id, question.id)
      }
      #**********************
      # Check whether this is Jen's assgt. & if so, use her rubric
      if (@assignment.instructor_id == User.find_by_name("jace_smith").id) && @title == "Review"
        if @assignment.id < 469
          @next_action = "update"
          render :action => 'custom_response'
        else
          @next_action = "update"
          render :action => 'custom_response_2011'
        end
      else
        # end of special code (except for the end below, to match the if above)
        #**********************
        render :action => 'response'
      end
    else
      #else create a new version and update it.
      @header = "New"
      @next_action = "create"
      @feedback = params[:feedback]
      @map = ResponseMap.find(params[:id])
      @return = params[:return]
      @modified_object = @map.map_id
      get_content
      #**********************
      # Check whether this is Jen's assgt. & if so, use her rubric
      if (@assignment.instructor_id == User.find_by_name("jace_smith").id) && @title == "Review"
        if @assignment.id < 469
          @next_action = "create"
          render :action => 'custom_response'
        else
          @next_action = "create"
          render :action => 'custom_response_2011'
        end
      else
        # end of special code (except for the end below, to match the if above)
        #**********************
        render :action => 'response'
      end
    end
  end

After Refactoring:

def rereview
   @map=ResponseMap.find(params[:id])
    get_content
    array_not_empty=0
    @review_scores=Array.new
    @prev=Response.all
    #get all versions and find the latest version
    for element in @prev
      if (element.map.id==@map.map.id)
        array_not_empty=1
        @review_scores << element
      end
    end

    @review_scores=response.getAllResponseVersions
    #sort all the available versions in descending order.
    if @prev.present?
      @largest_version_num = Response.get_largest_version_number(@review_scores)
      @latest_phase=@largest_version_num.created_at
      due_dates = DueDate.where(["assignment_id = ?", @assignment.id])

      @sorted_deadlines = DueDate.sort_deadlines(due_dates)
      current_time=Time.new.getutc
      #get the highest version numbered review
      next_due_date=@sorted_deadlines[0]
      #check in which phase the latest review was done.
      for deadline_version in @sorted_deadlines
        if (@largest_version_num.created_at < deadline_version.due_at)
          break
        end
      end
      for deadline_time in @sorted_deadlines
        if (current_time < deadline_time.due_at)
          break
        end
      end
    end
    #check if the latest review is done in the current phase.
    #if latest review is in current phase then edit the latest one.
    #else create a new version and update it.
    # editing the latest review
    if (deadline_version.due_at== deadline_time.due_at)
      #send it to edit here
      @header = "Edit"
      @next_action = "update"
      @return = params[:return]
      @response = Response.where(map_id: params[:id], version_num:  @largest_version_num.version_num).first
      @modified_object = @response.response_id
      @map = @response.map
      get_content
      @review_scores = Array.new
      @questions.each {
          |question|
          @review_scores << Score.where(response_id: @response.response_id, question_id:  question.id).first
      }
      #**********************


      # Check whether this is Jace's assgt. & if so, use her rubric
      if (check_user_name_jace? && @title == "Review")
          handle_jace_kludge
      else
        # end of special code (except for the end below, to match the if above)
        #**********************
        render :action => 'response'
      end

    else
      #else create a new version and update it.
      @header = "New"
      @next_action = "create"
      @feedback = params[:feedback]
      @map = ResponseMap.find(params[:id])
      @return = params[:return]
      @modified_object = @map.map_id
      get_content
      #**********************
      # Check whether this is Jen's assgt. & if so, use her rubric
      if (check_user_name_jace? && @title == "Review")
        handle_jace_kludge
      else
        # end of special code (except for the end below, to match the if above)
        #**********************
        render :action => 'response'
      end
    end
  end

Creation of a Kludge

  • The rereview method contained a special code to check whether an assignment is “Jace’s assignment”; this was the first assignment that was ever created with a multipart rubric. It was hard-coded into the system, rather than working on a rubric that was created in the normal way. It is impossible to remove this code without breaking that assignment. It is now implemented as a separate method named handle_jace_kludge.

Before Refactoring:

The following code was present in the rereview method.

 # Check whether this is Jen's assgt. & if so, use her rubric
      if (@assignment.instructor_id == User.find_by_name("jace_smith").id) && @title == "Review"
        if @assignment.id < 469
          @next_action = "update"
          render :action => 'custom_response'
        else
          @next_action = "update"
          render :action => 'custom_response_2011'
        end
      else
        # end of special code (except for the end below, to match the if above)
        render :action => 'response'
      end     

After Refactoring:

We added two methods named 'handle_jace_kludge' and 'check_user_name_jace?'

  def check_user_name_jace?
    return @assignment.instructor_id == User.find_by_name("jace_smith").id
  end
  def handle_jace_kludge
    # ** if assignment belongs to Jace handle it depending on the assignment id **
    if @assignment.id < 469
      @next_action = "update"
      render :action => 'custom_response'
    else
      @next_action = "update"
      render :action => 'custom_response_2011'
    end
  end

Moved methods from Response Controller to appropriate models

  • Sorting review versions is not a controller responsibility; So we moved it to the Response model.
  def self.get_largest_version_number(review_scores)
    @sorted=@review_scores.sort { |m1, m2| (m1.version_num and m2.version_num) ? m2.version_num <=> m1.version_num : (m1.version_num ? -1 : 1) }
    @largest_version_num=@sorted[0]
  end

Similarly, the logic for determining whether a review is current or not(i.e., the review was done during the current assignment phase) is not a controller's responsibility and thus was moved to the Response model.

 def self.getAllResponseVersions
    #get all previous versions of responses for the response map.
    @review_scores=Array.new
    @prev=Response.where(map_id: @map.id)

    @prev.each do |element|
      @review_scores << element
    end

    return @review_scores
  end

External Links

1. Expertiza
2. VCL Link
3. Git repository
4. Steps to setup Expertiza