CSC/ECE 517 Fall 2022 - E2275. Further refactoring and improvement of student quizzes controller

From Expertiza_Wiki
Jump to navigation Jump to search

Expertiza Overview

Ruby on Rails was used to create Expertiza, a multifunctional online application for instructors and students. Instructors using Expertiza have the ability to design their own teams, classes, assignments, quizzes, and much more. However, students are also permitted to work in groups, take tests, and do tasks. Additionally, Expertiza enables students to share peer reviews, allowing them to collaborate to enhance others' learning opportunities. It is an open-source program, and Expertiza is in Github repository Expertiza.

Prior Work

  • Modified and added comments for index function to clearly indicate it’s functionality
  • Refactored the calculate_score method and moved to helper method. which helped in increasing the readability and maintainability of the code while being constant with object oriented design principles.
  • In the finished_quiz function, renamed the variables to help understand their purpose better.
  • @quiz_taker = AssignmentTeam.find(@quiz_response_map.reviewee_id).participants.first This line of code seems to assume that only one person is on the team. Therefore, changed the name of the variables to better indicate their purpose.
  • For self.take_quiz, as the method comment is correct, the name of the method was changed to something relating to what tasks it performs as what it does is not particularly clear.
  • The name and the comments for the function were improved to describe the functionality better.
  • The comment were grammatically incorrect. Therefore, fixed the English in the comment.

Problem Statement

Background

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 DRY principle. There are 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.

OSS Project

OSS Project Wiki Page :- https://expertiza.csc.ncsu.edu/index.php/CSC/ECE_517_Fall_2022_-_E2255._Refactor_student_quizzes_controller.rb
Pull Request [Code changes] :- https://github.com/expertiza/expertiza/pull/2468

Required Changes

This project builds on E2225 and should begin with the refactoring done by that project. That project focused on simplifying the methods instudent_quizzes controller, while this project looks at making the code more understandable and transparent.

  • Method ‘calculate_score’ was moved to the helper completely. Although, the expectation is to retain the redirection logic in the controller and move the business logic to the helper function.
  • Method ‘review_questions’ contains the logic for quiz questionnaire. It makes more sense for it to be moved to the quiz_questionnaire controller instead.
  • Method ‘calculate_score’ contains logic that is similar to the compute_weighted_score method in the quiz_questionnaire.rb. Refactor the logic of the method to leverage the existing implementation and eliminate duplicate code.
  • Utilize the logic in method ‘validate_question’ from the quiz_questionnaire.rb in the calculate_score method instead of the multiple if else statements.

Because this is simply a refactoring project, no functionalities should change. Therefore, existing tests should still pass after these changes have been completed. To test this controller, create a new rspec file for student_quizzes_controller.rb and run the following commands to verify your changes do not break any core functions.
rspec spec/controllers/student_quizzes_controller_spec.rb
Many of these proposed changes require moving the function somewhere else, so some tests may break and require being moved or targeting a different file. Additionally, this file has very little coverage, so manual verification may be required.

Approach

Refactoring

Potential files to be modified during refactoring

1) student_quizzes_controller.rb
2) Quiz_questionnaire_controller.rb
3) student_quizzes_helper.rb
4) quiz_questionnaire.rb

Control Flow

Existing Flow:-
Image:900 pixels

Proposed Flow:-
Image:900 pixels

Polymorphism

This approach uses polymorphism because it helps to manage and reduce the coupling between distinct functionalities and because previously written codes and classes may be reused as needed, saving time for programmers. Additionally, it enables the programming code to expand itself in order to utilize the earlier version of the program, which can facilitate scalability and make debugging easier. In our example, the function calculate score would carry out different tasks depending on the input parameter, which is either a single choice radio button or a multiple-choice question checkboxes.

Design Pattern to be used:

Chain Of Responsibility

The Chain of Responsibility pattern was implemented as it relies on transforming specific behaviors into handlers which are stand-alone objects. Therefore, in our case, we are passing the responsibility of calculating the score of multiple varieties of questions i.e. checkbox and radio button, to their respective methods. This pattern is useful for the code as it encourages loose coupling between the sender and receiver of a request by allowing several objects a chance to handle the request.
Image:600 pixels

Design Principles

DRY (Don't Repeat Yourself)

DRY principle states that any piece of code should not be repeated, therefore our current approach satisfies the principle by removing the calculate_score method and using the already existing ‘validate_question’ method in the quiz_quesionnaire.rb file to perform the same task.

Single Responsibility

The idea behind the Single Responsibility Principle is that every class, module, or function in a program should have one responsibility or purpose in a program. This would be consistent with our approach as, calculate_scores method which is currently performing multiple task, would be refactored into different methods for handling a specific task.

Maximizing cohesion

This principle ensures that design components that are self-contained: independent, and with a single, well-defined purpose. As our code would follow the strategy principle where all the methods have their specific function along with the DRY principle, this makes sure that the maximum cohesion principle is followed by the code.

Minimizing Coupling

Minimizing coupling principle expects the Modules to be as independent as possible from other modules so that changes to modules don’t heavily impact other modules. Hence, we will be moving the calculate_score method which is tightly coupled with student_quizzes_controller file to its helper file. This would prevent the changes made in the original file from impacting the module.

Increasing readability

This principle simply refers to writing and displaying your code in a way that makes it simple to understand and comprehend. This would be achieved by adding relevant indentations, comments, and documentation. Additionally, code grouping and using name schema would further make the code more cogent and easier to understand.

Work Done

Refactoring

Files Modified

1) student_quizzes_controller.rb

Image:600 pixels

Image:600 pixels

Image:600 pixels

2) student_quizzes_helper.rb

Image:600 pixels

Image:600 pixels

3) quiz_questionnaire_controller.rb

Image:600 pixels

4) routes.rb

Image:600 pixels

5) review_questions.html.erb and take_quiz.html.erb

Image:600 pixels

6) finished_quiz.html.erb

Image:600 pixels

7) set_dynamic_quiz.html.erb

Image:600 pixels

Comments

Many useful comments where added, which aided in better understanding of the fucntion the variable serves and the logic that the method is performing.

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. Set the proper due dates for submission review and evaluation by going to the Due Dates page. 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. There will be a message inviting you to add participants at the top of the page. Several students should be added to the task using the given link.
5. After logging out as the teacher, log back in as the pupils. Before the deadline for submissions, turn in your assignment.
Note: To submit, you must first establish a team and give it a name.
6. There is a link to "Create a Quiz" at the bottom of the submission page. Select that link.
7. You are then sent to the quiz creation page. Here, you may set True/False questions, Checkbox questions, and Radio button questions along with the appropriate responses.
Then click "Create Quiz." Thus, the quiz is made.
8. Log out and re-enter the assignment as a different student who has registered. submit a document in this student's place. Review the first student's work when the submission deadline has passed, then go back to the main menu.
9. The option to "Take quizzes" will be visible. Select "Request" and then choose the quiz you made. You may now take the test that the other student made here.

Manual Testing Video

Alt text

Automated Testing using RSpec

There were no existing RSpec test file for student_quizzes_controller.rb to test its functionality. During our prior work we have added 3 RSpec test to test authorization of user to access quiz functionality. We will add more automated test cases to test calculate_score and other functions using RSpec.

Scenarios

Scenario 1: Student is trying to access quizzes without authorization.
Given: User logged in as student and doesn't have authorization for reviewing or submitting quiz.
When: User trying to access quizzes using /index url.
Then: User should be redirected to home page.

Scenario 2: Student is accessing quizzes with authorization.
Given: User logged in as student and user have authorization for reviewing or submitting quiz.
When: User clicks on Show Quizzes button
Then: User should be able to view quizzes for which he/she access to.

Scenario 3: Instructor is accessing quizzes.
Given: User logged in as instructor.
When: User clicks on Show Quizzes button
Then: User should be able to view all quizzes.

Scenario 4: Student submits quiz by completing all questionnaire.
Given: User logged in as student and have quiz pending.
When: User answers all questionnaire in quiz and submits the quiz.
Then: Score of the quiz should get calculated and results are stored in database successfully.

Image:600 pixels

Below is the image of the testing results:-

Image:700 pixels

GitHub and Related Link

GitHub repository corresponding to this task is publicly available at E2275 GitHub.

GitHub pull request and file changes can be viewed at Pull Request.

Contributors

Mentor

Neha Kotcherlakota (nkotche@ncsu.edu)

Team Members

Priyam Swatil Shah (pshah9@ncsu.edu)
Sanay Yogesh Shah (sshah34@ncsu.edu)
Subodh Gujar (sgujar@ncsu.edu)