CSC/ECE 517 Spring 2022 - E2215: Refactor student quizzes controller

From Expertiza_Wiki
Jump to navigation Jump to search

About Expertiza

Expertiza is a multi-purpose web application built using Ruby on Rails for Students and Instructors. Instructors enrolled in Expertiza can create and customize classes, teams, assignments, quizzes, and many more. On the other hand, Students are also allowed to form teams, attempt quizzes, and complete assignments. Apart from that, Expertiza also allows students to provide peer reviews enabling them to work together to improve others' learning experiences. It is an open-source application and its Github repository is Expertiza.

E2215. Refactoring student_quizzes_controller.rb in Expertiza

This page is a description of Expertiza OSS project E2215, which is refactoring the student_quizzes_controller.rb file.

Controller Description

The student_quizzes_controller consists of methods involved in creating, scoring & recording responses of the quizzes taken by reviewers or students of the other teams with the same assignment. This controller has some issues that violate essential Rails design principles such as the DRY principle. There are a few methods in this controller that should have been in model classes. Some methods share code, which creates code repetition. Some method comments need to be rewritten.

Files Modified

1. app/controllers/student_quizzes_controller.rb
2. app/views/student_quizzes/finished_quiz.html
3. app/helpers/student_quizzes_helper.rb

Modifications made to the existing code

1. Modification - Added comments for custom methods in this controller explaining the purpose.

# Based on the logged in user, verifies user's authourizations and privileges
def action_allowed?
# Initializes instance variables needed to fetch the necessary details of the quizzes.
def index
# For the response provided, this methods displays the questions, right/wrong answers and the final score.
def finished_quiz
# Lists all the available quizzes created by the other teams in the current project which can be attempted.
def self.take_quiz(assignment_id, reviewer_id)
# Stores the answers entered by the quiz taker and calculates the score based on the answers entered.
def record_response
# This method is only for quiz questionnaires, it is called when instructors click "view quiz questions" on the pop-up panel.
# Using the current assignment id parameter, fetches all the questions for each quiz and finally lists all the answers and scores for each submission.
def review_questions
# This method as whole fetches the answers provided and calculates the final scores for the quiz.
# Also calls seperate methods for handling single answer/ true or false evaluations and mulitple answer evaluations for calculating score.
def calculate_score(map, response)
# Evaluates scores for questions that contains only single/ true or false answers
def single_answer_evaluation(answers, params, question, correct_answers, has_response, response)
# Evaluates scores for questions that contains multiple answers
def multiple_answer_evaluation(answers, params, question, correct_answers, has_response, response)

2. Modification - Line 29 - .first has been changed to .last to count the score of the most recent attempt of the quiz.
student_quizzes_controller.rb:
Old:

@response = Response.where(map_id: params[:map_id]).first

New:

@response = Response.where(map_id: params[:map_id]).last

3. Modification - Line 33 - @map has been renamed to @quiz_response_map to more accurately describe its function.
student_quizzes_controller.rb:
Old:

@map = ResponseMap.find(params[:map_id])
@participant = AssignmentTeam.find(@map.reviewee_id).participants.first

New:

@quiz_response_map = ResponseMap.find(params[:map_id])
@quiz_taker = AssignmentTeam.find(@quiz_response_map.reviewee_id).participants.first

4. Modification - Line 34 - @participant has been changed to @quiz_taker to more accurately describe its function.
student_quizzes_controller.rb:
Old:

@participant = AssignmentTeam.find(@map.reviewee_id).participants.first

New:

quiz_taker = AssignmentTeam.find(@quiz_response_map.reviewee_id).participants.first

finished_quiz.html.erb:
Old:

<%  @assignment = Assignment.find(@participant.parent_id)%>
<%= render :partial => 'submitted_content/main', :locals => {:participant => @participant, :stage => ""} %>

New:

<%  @assignment = Assignment.find(@quiz_taker.parent_id)%>
<%= render :partial => 'submitted_content/main', :locals => {:participant => @quiz_taker, :stage => ""} %>

5. Modification - Line 39 - The method comment of self.take_quiz has been updated to describe its function
student_quizzes_controller.rb:
Old:

# Gives details of the list of quizzes created by other teams for current project that a reviewer can take

New:

# Lists all the available quizzes created by the other teams in the current project which can be attempted.

6. Modification - Controller Method calculate_score has been split into three separate functions since the original method is too big and is split to use the other methods to handle score calculation for Single choice / True or False questions and Multiple answer questions separately. Then all three methods have been moved to student_quizzes_helper.rb file since they are being reused.
student_quizzes_helper.rb:
New:

module StudentQuizzesHelper

  # This method as whole fetches the answers provided and calculates the final scores for the quiz.
  # Also calls seperate methods for handling single answer/ true or false evaluations and mulitple answer evaluations for calculating score.
  def calculate_score(map, response)
    questionnaire = Questionnaire.find(map.reviewed_object_id)
    answers = []
    has_response = true
    questions = Question.where(questionnaire_id: questionnaire.id)
    questions.each do |question|
      correct_answers = QuizQuestionChoice.where(question_id: question.id, iscorrect: true)
      ques_type = question.type
      if ques_type.eql? 'MultipleChoiceCheckbox'
        has_response = multiple_answer_evaluation(answers, params, question, correct_answers, has_response, response)
      # TrueFalse and MultipleChoiceRadio
      else
        has_response = single_answer_evaluation(answers, params, question, correct_answers, has_response, response)
      end
    end
    if has_response
      answers.each(&:save)
      redirect_to controller: 'student_quizzes', action: 'finished_quiz', map_id: map.id
    else
      response.destroy
      flash[:error] = 'Please answer every question.'
      redirect_to action: :take_quiz, assignment_id: params[:assignment_id], questionnaire_id: questionnaire.id, map_id: map.id
    end
  end

  # Evaluates scores for questions that contains multiple answers
  def multiple_answer_evaluation(answers, params, question, correct_answers, has_response, response)
    score = 0
    if params[question.id.to_s].nil?
        has_response = false
      else
        params[question.id.to_s].each do |choice|
          # loop the quiz taker's choices and see if 1)all the correct choice are checked and 2) # of quiz taker's choice matches the # of the correct choices
          correct_answers.each do |correct|
            score += 1 if choice.eql? correct.txt
          end
        end
        score = score == correct_answers.count && score == params[question.id.to_s].count ? 1 : 0
        # for MultipleChoiceCheckbox, score =1 means the quiz taker have done this question correctly, not just make select this choice correctly.
        params[question.id.to_s].each do |choice|
          new_answer = Answer.new comments: choice, question_id: question.id, response_id: response.id, answer: score

          has_response = false unless new_answer.valid?
          answers.push(new_answer)
        end
    end
    return has_response
  end

  # Evaluates scores for questions that contains only single/ true or false answers
  def single_answer_evaluation(answers, params, question, correct_answers, has_response, response)
    correct_answer = correct_answers.first
    score = correct_answer.txt == params[question.id.to_s] ? 1 : 0
    new_score = Answer.new comments: params[question.id.to_s], question_id: question.id, response_id: response.id, answer: score
    has_response = false if new_score.nil? || new_score.comments.nil? || new_score.comments.empty?
    answers.push(new_score)
    return has_response
  end

end

7. Modification - Line 60 - A method comment has been added for record_response to describe its function
student_quizzes_controller.rb:

# Stores the answers entered by the quiz taker and calculates the score based on the answers entered.

8. Modification - -graded? has been removed as it is no longer necessary.

9. Modification - Line 72 - The Flash message has been updated.
student_quizzes_controller.rb:
Old:

flash[:error] = 'You have already taken this quiz, below are the records for your response.'

New:

flash[:error] = 'You have already taken this quiz. Below are the responses of your previous attempt.'

10. Modification - Line 77 - The method comment for review_questions has been updated.
student_quizzes_controller.rb:
Old:

# This method is only for quiz questionnaires, it is called when instructors click "view quiz questions" on the pop-up panel.

New:

# This method is only for quiz questionnaires, it is called when instructors click "view quiz questions" on the pop-up panel.
# Using the current assignment id parameter, fetches all the questions for each quiz and finally lists all the answers and scores for each submission.

Test Plan

Manual Testing

Follow these instructions to manually test the functionality of student_quizzes_controller.
1. Log in as instructor using the credentials, username: instructor6, password: password.
2. Select the Assignments subheading. Here, create a new assignment by entering the necessary details. Enable the Has Teams? checkbox and create. Also enable the Has quiz? checkbox.
3. Navigate to the Due Dates tab and set appropriate due dates for submission and review. For Round 1: Review, enable Has Quiz using the dropdown Menu. Once you save, A Number of Quiz questions field will be visible on the Assignments page. Set this to 2 and save.
4. At the top of the page, there will be a notice asking you to add participants. Using the link provided, add a few students to the assignment.
5. Logout as the Instructor and Login as the students. Make submissions for the assignment before the submission deadline.
Note: You must create a team and set team name to be able to make a submission.
6. At the bottom of the submission page, you will see a Create a Quiz link. Select that link.
7. This takes you to the quiz creation page. Here you can set Radio Button MCQs, Checkbox MCQs and True/False Questions and set the correct answers.
Then select Create Quiz. This creates the quiz.
8. Logout and login as another student registered in the assignment. Make a submission as this student. Once the submission deadline has passed, review the first student's work and return to the main menu.
9. You will see the option to Take quizzes. Here, select the quiz you have created and select Request. Here, you can take the quiz created by the other student.

Manual Testing Walkthorugh

The video below shows the steps for manually testing the controller functionality.

Manual testing video

Testing Video

Originally the student_quizzes_controller did not have any test cases, so according to the documentation, we wrote some to check if the changes we introduced have broken the functionality. Our test cases have run successfully. Here is the video for the same.

Test Video link

Pull Request

The link for our pull request - https://github.com/expertiza/expertiza/pull/2340

Team members

Jyothi Sumer Goud Maduru (jmaduru@ncsu.edu)
Pradyumna Vemuri (pvemuri@ncsu.edu)
Karthik Narayanan Gopala Sundaresan (kgopala3@ncsu.edu)

Team Mentor

Divyang Doshi (ddoshi2@ncsu.edu)