<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Kjoshi4</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Kjoshi4"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Kjoshi4"/>
	<updated>2026-06-30T01:01:46Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165240</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165240"/>
		<updated>2025-04-24T23:30:25Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
===1) Moving Find Resource Out of Controller===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===2) Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb ===&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
===3) Renamed and improved method names ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz_15.jpeg|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
d) Renaming create_questions_and_answers to create_items_and_choices: There were discrepancies in the database. The question model was renamed to item and there were choices associated with it. The new name is more meaningful.&lt;br /&gt;
&lt;br /&gt;
===4) Implement self.assessment in Review Response Map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
===5) Moved Score Calculation from ResponseMap to ResponseObject===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
===6) Added Skippable Question Logic to Quizzes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
===7) Refactored Parameters===&lt;br /&gt;
&lt;br /&gt;
This change improves compatibility with the updated data model and supports dynamic quiz item rendering, skippable questions, and proper nesting for answer choices.&lt;br /&gt;
&lt;br /&gt;
'''Before:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name,&lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :assignment_id,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    questions_attributes: [&lt;br /&gt;
      :id,&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :correct_answer,&lt;br /&gt;
      :score_value,&lt;br /&gt;
      { answers_attributes: %i[id answer_text correct] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''After:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name, &lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    items_attributes: [&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :weight,&lt;br /&gt;
      :skippable,&lt;br /&gt;
      :seq,&lt;br /&gt;
      { quiz_question_choices_attributes: %i[txt iscorrect] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Migration files===&lt;br /&gt;
&lt;br /&gt;
* Added skippable to facilitate the skippable function to item table&lt;br /&gt;
* Changed the column_name to item in quiz_choices as the current table name is item, not questions.&lt;br /&gt;
&lt;br /&gt;
== Implementation Details ==&lt;br /&gt;
&lt;br /&gt;
===1) Refactoring and Moving Business Logic===&lt;br /&gt;
&lt;br /&gt;
The following changes were made to improve maintainability and follow MVC principles:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the create_score method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the process_answers method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c Moved findResource logic to a separate file]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398 Implemented self.assessments_for in ReviewResponseMap]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869 Renamed methods in student_quizzes_controller.rb for clarity]&lt;br /&gt;
&lt;br /&gt;
===2) Error Fixes===&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166 Fixed ResponseMap error]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49 Resolved assignment dependency issue]&lt;br /&gt;
&lt;br /&gt;
===3) RSpec Testing===&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/blob/b0079c802e46f4a7e22c1c4ea1757be51ebda23b/spec/requests/api/v1/student_quizzes_controller_spec.rb Added request specs for student_quizzes_controller]&lt;br /&gt;
&lt;br /&gt;
RSpec tests were written for all major controller actions, model logic (including skippable question scoring), and error handling paths. This ensures the correctness of the refactored implementation and guards against regressions.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
We added three test cases in the file:  &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored methods in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After adding the tests, our test overage increased by 6% from 68% to around 74%.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure the robustness of the refactored controller and the correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
https://youtu.be/T_qFnMeGhyg&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel32@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165194</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165194"/>
		<updated>2025-04-23T18:50:49Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* 3) RSpec Testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
===1) Moving Find Resource Out of Controller===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===2) Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb ===&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
===3) Renamed and improved method names ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz_15.jpeg|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
d) Renaming create_questions_and_answers to create_items_and_choices: There were discrepancies in the database. The question model was renamed to item and there were choices associated with it. The new name is more meaningful.&lt;br /&gt;
&lt;br /&gt;
===4) Implement self.assessment in Review Response Map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
===5) Moved Score Calculation from ResponseMap to ResponseObject===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
===6) Added Skippable Question Logic to Quizzes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
===7) Refactored Parameters===&lt;br /&gt;
&lt;br /&gt;
This change improves compatibility with the updated data model and supports dynamic quiz item rendering, skippable questions, and proper nesting for answer choices.&lt;br /&gt;
&lt;br /&gt;
'''Before:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name,&lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :assignment_id,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    questions_attributes: [&lt;br /&gt;
      :id,&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :correct_answer,&lt;br /&gt;
      :score_value,&lt;br /&gt;
      { answers_attributes: %i[id answer_text correct] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''After:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name, &lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    items_attributes: [&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :weight,&lt;br /&gt;
      :skippable,&lt;br /&gt;
      :seq,&lt;br /&gt;
      { quiz_question_choices_attributes: %i[txt iscorrect] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation Details ==&lt;br /&gt;
&lt;br /&gt;
===1) Refactoring and Moving Business Logic===&lt;br /&gt;
&lt;br /&gt;
The following changes were made to improve maintainability and follow MVC principles:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the create_score method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the process_answers method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c Moved findResource logic to a separate file]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398 Implemented self.assessments_for in ReviewResponseMap]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869 Renamed methods in student_quizzes_controller.rb for clarity]&lt;br /&gt;
&lt;br /&gt;
===2) Error Fixes===&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166 Fixed ResponseMap error]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49 Resolved assignment dependency issue]&lt;br /&gt;
&lt;br /&gt;
===3) RSpec Testing===&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/blob/b0079c802e46f4a7e22c1c4ea1757be51ebda23b/spec/requests/api/v1/student_quizzes_controller_spec.rb Added request specs for student_quizzes_controller]&lt;br /&gt;
&lt;br /&gt;
RSpec tests were written for all major controller actions, model logic (including skippable question scoring), and error handling paths. This ensures the correctness of the refactored implementation and guards against regressions.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
We added three test cases in the file:  &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored methods in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After adding the tests, our test overage increased by 6% from 68% to around 74%.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure the robustness of the refactored controller and the correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
https://youtu.be/T_qFnMeGhyg&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel32@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165192</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165192"/>
		<updated>2025-04-23T18:50:38Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* 2) Error Fixes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
===1) Moving Find Resource Out of Controller===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===2) Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb ===&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
===3) Renamed and improved method names ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz_15.jpeg|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
d) Renaming create_questions_and_answers to create_items_and_choices: There were discrepancies in the database. The question model was renamed to item and there were choices associated with it. The new name is more meaningful.&lt;br /&gt;
&lt;br /&gt;
===4) Implement self.assessment in Review Response Map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
===5) Moved Score Calculation from ResponseMap to ResponseObject===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
===6) Added Skippable Question Logic to Quizzes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
===7) Refactored Parameters===&lt;br /&gt;
&lt;br /&gt;
This change improves compatibility with the updated data model and supports dynamic quiz item rendering, skippable questions, and proper nesting for answer choices.&lt;br /&gt;
&lt;br /&gt;
'''Before:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name,&lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :assignment_id,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    questions_attributes: [&lt;br /&gt;
      :id,&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :correct_answer,&lt;br /&gt;
      :score_value,&lt;br /&gt;
      { answers_attributes: %i[id answer_text correct] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''After:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name, &lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    items_attributes: [&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :weight,&lt;br /&gt;
      :skippable,&lt;br /&gt;
      :seq,&lt;br /&gt;
      { quiz_question_choices_attributes: %i[txt iscorrect] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation Details ==&lt;br /&gt;
&lt;br /&gt;
===1) Refactoring and Moving Business Logic===&lt;br /&gt;
&lt;br /&gt;
The following changes were made to improve maintainability and follow MVC principles:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the create_score method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the process_answers method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c Moved findResource logic to a separate file]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398 Implemented self.assessments_for in ReviewResponseMap]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869 Renamed methods in student_quizzes_controller.rb for clarity]&lt;br /&gt;
&lt;br /&gt;
===2) Error Fixes===&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166 Fixed ResponseMap error]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49 Resolved assignment dependency issue]&lt;br /&gt;
&lt;br /&gt;
==='''3) RSpec Testing'''===&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/blob/b0079c802e46f4a7e22c1c4ea1757be51ebda23b/spec/requests/api/v1/student_quizzes_controller_spec.rb Added request specs for student_quizzes_controller]&lt;br /&gt;
&lt;br /&gt;
RSpec tests were written for all major controller actions, model logic (including skippable question scoring), and error handling paths. This ensures the correctness of the refactored implementation and guards against regressions.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
We added three test cases in the file:  &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored methods in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After adding the tests, our test overage increased by 6% from 68% to around 74%.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure the robustness of the refactored controller and the correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
https://youtu.be/T_qFnMeGhyg&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel32@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165191</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165191"/>
		<updated>2025-04-23T18:50:22Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* 1) Refactoring and Moving Business Logic */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
===1) Moving Find Resource Out of Controller===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===2) Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb ===&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
===3) Renamed and improved method names ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz_15.jpeg|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
d) Renaming create_questions_and_answers to create_items_and_choices: There were discrepancies in the database. The question model was renamed to item and there were choices associated with it. The new name is more meaningful.&lt;br /&gt;
&lt;br /&gt;
===4) Implement self.assessment in Review Response Map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
===5) Moved Score Calculation from ResponseMap to ResponseObject===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
===6) Added Skippable Question Logic to Quizzes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
===7) Refactored Parameters===&lt;br /&gt;
&lt;br /&gt;
This change improves compatibility with the updated data model and supports dynamic quiz item rendering, skippable questions, and proper nesting for answer choices.&lt;br /&gt;
&lt;br /&gt;
'''Before:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name,&lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :assignment_id,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    questions_attributes: [&lt;br /&gt;
      :id,&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :correct_answer,&lt;br /&gt;
      :score_value,&lt;br /&gt;
      { answers_attributes: %i[id answer_text correct] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''After:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name, &lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    items_attributes: [&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :weight,&lt;br /&gt;
      :skippable,&lt;br /&gt;
      :seq,&lt;br /&gt;
      { quiz_question_choices_attributes: %i[txt iscorrect] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation Details ==&lt;br /&gt;
&lt;br /&gt;
===1) Refactoring and Moving Business Logic===&lt;br /&gt;
&lt;br /&gt;
The following changes were made to improve maintainability and follow MVC principles:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the create_score method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the process_answers method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c Moved findResource logic to a separate file]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398 Implemented self.assessments_for in ReviewResponseMap]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869 Renamed methods in student_quizzes_controller.rb for clarity]&lt;br /&gt;
&lt;br /&gt;
==='''2) Error Fixes'''===&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166 Fixed ResponseMap error]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49 Resolved assignment dependency issue]&lt;br /&gt;
&lt;br /&gt;
==='''3) RSpec Testing'''===&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/blob/b0079c802e46f4a7e22c1c4ea1757be51ebda23b/spec/requests/api/v1/student_quizzes_controller_spec.rb Added request specs for student_quizzes_controller]&lt;br /&gt;
&lt;br /&gt;
RSpec tests were written for all major controller actions, model logic (including skippable question scoring), and error handling paths. This ensures the correctness of the refactored implementation and guards against regressions.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
We added three test cases in the file:  &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored methods in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After adding the tests, our test overage increased by 6% from 68% to around 74%.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure the robustness of the refactored controller and the correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
https://youtu.be/T_qFnMeGhyg&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel32@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165189</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165189"/>
		<updated>2025-04-23T18:49:56Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Implementation Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
===1) Moving Find Resource Out of Controller===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===2) Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb ===&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
===3) Renamed and improved method names ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz_15.jpeg|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
d) Renaming create_questions_and_answers to create_items_and_choices: There were discrepancies in the database. The question model was renamed to item and there were choices associated with it. The new name is more meaningful.&lt;br /&gt;
&lt;br /&gt;
===4) Implement self.assessment in Review Response Map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
===5) Moved Score Calculation from ResponseMap to ResponseObject===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
===6) Added Skippable Question Logic to Quizzes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
===7) Refactored Parameters===&lt;br /&gt;
&lt;br /&gt;
This change improves compatibility with the updated data model and supports dynamic quiz item rendering, skippable questions, and proper nesting for answer choices.&lt;br /&gt;
&lt;br /&gt;
'''Before:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name,&lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :assignment_id,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    questions_attributes: [&lt;br /&gt;
      :id,&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :correct_answer,&lt;br /&gt;
      :score_value,&lt;br /&gt;
      { answers_attributes: %i[id answer_text correct] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''After:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name, &lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    items_attributes: [&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :weight,&lt;br /&gt;
      :skippable,&lt;br /&gt;
      :seq,&lt;br /&gt;
      { quiz_question_choices_attributes: %i[txt iscorrect] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation Details ==&lt;br /&gt;
&lt;br /&gt;
==='''1) Refactoring and Moving Business Logic'''===&lt;br /&gt;
&lt;br /&gt;
The following changes were made to improve maintainability and follow MVC principles:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the create_score method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the process_answers method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c Moved findResource logic to a separate file]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398 Implemented self.assessments_for in ReviewResponseMap]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869 Renamed methods in student_quizzes_controller.rb for clarity]&lt;br /&gt;
&lt;br /&gt;
==='''2) Error Fixes'''===&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166 Fixed ResponseMap error]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49 Resolved assignment dependency issue]&lt;br /&gt;
&lt;br /&gt;
==='''3) RSpec Testing'''===&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/blob/b0079c802e46f4a7e22c1c4ea1757be51ebda23b/spec/requests/api/v1/student_quizzes_controller_spec.rb Added request specs for student_quizzes_controller]&lt;br /&gt;
&lt;br /&gt;
RSpec tests were written for all major controller actions, model logic (including skippable question scoring), and error handling paths. This ensures the correctness of the refactored implementation and guards against regressions.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
We added three test cases in the file:  &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored methods in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After adding the tests, our test overage increased by 6% from 68% to around 74%.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure the robustness of the refactored controller and the correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
https://youtu.be/T_qFnMeGhyg&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel32@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165187</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165187"/>
		<updated>2025-04-23T18:49:08Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
===1) Moving Find Resource Out of Controller===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===2) Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb ===&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
===3) Renamed and improved method names ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz_15.jpeg|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
d) Renaming create_questions_and_answers to create_items_and_choices: There were discrepancies in the database. The question model was renamed to item and there were choices associated with it. The new name is more meaningful.&lt;br /&gt;
&lt;br /&gt;
===4) Implement self.assessment in Review Response Map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
===5) Moved Score Calculation from ResponseMap to ResponseObject===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
===6) Added Skippable Question Logic to Quizzes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
===7) Refactored Parameters===&lt;br /&gt;
&lt;br /&gt;
This change improves compatibility with the updated data model and supports dynamic quiz item rendering, skippable questions, and proper nesting for answer choices.&lt;br /&gt;
&lt;br /&gt;
'''Before:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name,&lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :assignment_id,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    questions_attributes: [&lt;br /&gt;
      :id,&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :correct_answer,&lt;br /&gt;
      :score_value,&lt;br /&gt;
      { answers_attributes: %i[id answer_text correct] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''After:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name, &lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    items_attributes: [&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :weight,&lt;br /&gt;
      :skippable,&lt;br /&gt;
      :seq,&lt;br /&gt;
      { quiz_question_choices_attributes: %i[txt iscorrect] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation Details ==&lt;br /&gt;
&lt;br /&gt;
'''1) Refactoring and Moving Business Logic'''&lt;br /&gt;
&lt;br /&gt;
The following changes were made to improve maintainability and follow MVC principles:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the create_score method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the process_answers method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c Moved findResource logic to a separate file]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398 Implemented self.assessments_for in ReviewResponseMap]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869 Renamed methods in student_quizzes_controller.rb for clarity]&lt;br /&gt;
&lt;br /&gt;
'''2) Error Fixes'''&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166 Fixed ResponseMap error]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49 Resolved assignment dependency issue]&lt;br /&gt;
&lt;br /&gt;
'''3) RSpec Testing'''&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/blob/b0079c802e46f4a7e22c1c4ea1757be51ebda23b/spec/requests/api/v1/student_quizzes_controller_spec.rb Added request specs for student_quizzes_controller]&lt;br /&gt;
&lt;br /&gt;
RSpec tests were written for all major controller actions, model logic (including skippable question scoring), and error handling paths. This ensures the correctness of the refactored implementation and guards against regressions.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
We added three test cases in the file:  &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored methods in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After adding the tests, our test overage increased by 6% from 68% to around 74%.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure the robustness of the refactored controller and the correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
https://youtu.be/T_qFnMeGhyg&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel32@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165186</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165186"/>
		<updated>2025-04-23T18:47:45Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz_15.jpeg|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
d) Renaming create_questions_and_answers to create_items_and_choices: There were discrepancies in the database. The question model was renamed to item and there were choices associated with it. The new name is more meaningful.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;7) Refactored Parameters&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This change improves compatibility with the updated data model and supports dynamic quiz item rendering, skippable questions, and proper nesting for answer choices.&lt;br /&gt;
&lt;br /&gt;
'''Before:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name,&lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :assignment_id,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    questions_attributes: [&lt;br /&gt;
      :id,&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :correct_answer,&lt;br /&gt;
      :score_value,&lt;br /&gt;
      { answers_attributes: %i[id answer_text correct] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''After:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name, &lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    items_attributes: [&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :weight,&lt;br /&gt;
      :skippable,&lt;br /&gt;
      :seq,&lt;br /&gt;
      { quiz_question_choices_attributes: %i[txt iscorrect] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation Details ==&lt;br /&gt;
&lt;br /&gt;
'''1) Refactoring and Moving Business Logic'''&lt;br /&gt;
&lt;br /&gt;
The following changes were made to improve maintainability and follow MVC principles:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the create_score method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the process_answers method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c Moved findResource logic to a separate file]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398 Implemented self.assessments_for in ReviewResponseMap]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869 Renamed methods in student_quizzes_controller.rb for clarity]&lt;br /&gt;
&lt;br /&gt;
'''2) Error Fixes'''&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166 Fixed ResponseMap error]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49 Resolved assignment dependency issue]&lt;br /&gt;
&lt;br /&gt;
'''3) RSpec Testing'''&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/blob/b0079c802e46f4a7e22c1c4ea1757be51ebda23b/spec/requests/api/v1/student_quizzes_controller_spec.rb Added request specs for student_quizzes_controller]&lt;br /&gt;
&lt;br /&gt;
RSpec tests were written for all major controller actions, model logic (including skippable question scoring), and error handling paths. This ensures the correctness of the refactored implementation and guards against regressions.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
We added three test cases in the file:  &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored methods in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After adding the tests, our test overage increased by 6% from 68% to around 74%.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure the robustness of the refactored controller and the correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
https://youtu.be/T_qFnMeGhyg&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel32@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165183</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165183"/>
		<updated>2025-04-23T18:47:08Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz_15.jpeg|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
d) Renaming create_questions_and_answers to create_items_and_choices: There were discrepancies in the database. The question model was renamed to item and there were choices associated with it. The new name is more meaningful.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;7) Refactored Parameters&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This change improves compatibility with the updated data model and supports dynamic quiz item rendering, skippable questions, and proper nesting for answer choices.&lt;br /&gt;
&lt;br /&gt;
'''Before:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name,&lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :assignment_id,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    questions_attributes: [&lt;br /&gt;
      :id,&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :correct_answer,&lt;br /&gt;
      :score_value,&lt;br /&gt;
      { answers_attributes: %i[id answer_text correct] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''After:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name, &lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    items_attributes: [&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :weight,&lt;br /&gt;
      :skippable,&lt;br /&gt;
      :seq,&lt;br /&gt;
      { quiz_question_choices_attributes: %i[txt iscorrect] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation Details ==&lt;br /&gt;
&lt;br /&gt;
'''1) Refactoring and Moving Business Logic'''&lt;br /&gt;
&lt;br /&gt;
The following changes were made to improve maintainability and follow MVC principles:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the create_score method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the process_answers method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c Moved findResource logic to a separate file]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398 Implemented self.assessments_for in ReviewResponseMap]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869 Renamed methods in student_quizzes_controller.rb for clarity]&lt;br /&gt;
&lt;br /&gt;
'''2) Error Fixes'''&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166 Fixed ResponseMap error]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49 Resolved assignment dependency issue]&lt;br /&gt;
&lt;br /&gt;
'''3) RSpec Testing'''&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/blob/b0079c802e46f4a7e22c1c4ea1757be51ebda23b/spec/requests/api/v1/student_quizzes_controller_spec.rb Added request specs for student_quizzes_controller]&lt;br /&gt;
&lt;br /&gt;
RSpec tests were written for all major controller actions, model logic (including skippable question scoring), and error handling paths. This ensures the correctness of the refactored implementation and guards against regressions.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
We added three test cases in the file:  &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored methods in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After adding the tests, our test overage increased by 6% from 68% to around 74%.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure the robustness of the refactored controller and the correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
https://youtu.be/T_qFnMeGhyg&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel32@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165181</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=165181"/>
		<updated>2025-04-23T18:46:36Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Implementation Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz_15.jpeg|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
d) Renaming create_questions_and_answers to create_items_and_choices: There were discrepancies in the database. The question model was renamed to item and there were choices associated with it. The new name is more meaningful.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;7) Refactored Parameters&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Before:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name,&lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :assignment_id,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    questions_attributes: [&lt;br /&gt;
      :id,&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :correct_answer,&lt;br /&gt;
      :score_value,&lt;br /&gt;
      { answers_attributes: %i[id answer_text correct] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''After:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name, &lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    items_attributes: [&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :weight,&lt;br /&gt;
      :skippable,&lt;br /&gt;
      :seq,&lt;br /&gt;
      { quiz_question_choices_attributes: %i[txt iscorrect] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This change improves compatibility with the updated data model and supports dynamic quiz item rendering, skippable questions, and proper nesting for answer choices.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Implementation Details ==&lt;br /&gt;
&lt;br /&gt;
'''1) Refactoring and Moving Business Logic'''&lt;br /&gt;
&lt;br /&gt;
The following changes were made to improve maintainability and follow MVC principles:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the create_score method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7 Refactored the process_answers method]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c Moved findResource logic to a separate file]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398 Implemented self.assessments_for in ReviewResponseMap]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869 Renamed methods in student_quizzes_controller.rb for clarity]&lt;br /&gt;
&lt;br /&gt;
'''2) Error Fixes'''&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166 Fixed ResponseMap error]&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49 Resolved assignment dependency issue]&lt;br /&gt;
&lt;br /&gt;
'''3) RSpec Testing'''&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Kiraschild/reimplementation-back-end/blob/b0079c802e46f4a7e22c1c4ea1757be51ebda23b/spec/requests/api/v1/student_quizzes_controller_spec.rb Added request specs for student_quizzes_controller]&lt;br /&gt;
&lt;br /&gt;
RSpec tests were written for all major controller actions, model logic (including skippable question scoring), and error handling paths. This ensures the correctness of the refactored implementation and guards against regressions.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
We added three test cases in the file:  &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored methods in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After adding the tests, our test overage increased by 6% from 68% to around 74%.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure the robustness of the refactored controller and the correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
https://youtu.be/T_qFnMeGhyg&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel32@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164986</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164986"/>
		<updated>2025-04-23T02:03:28Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz_15.jpeg|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
d) Renaming create_questions_and_answers to create_items_and_choices: There were discrepancies in the database. The question model was renamed to item and there were choices associated with it. The new name is more meaningful.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;7) Refactored Parameters&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Before:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name,&lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :assignment_id,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    questions_attributes: [&lt;br /&gt;
      :id,&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :correct_answer,&lt;br /&gt;
      :score_value,&lt;br /&gt;
      { answers_attributes: %i[id answer_text correct] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''After:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def questionnaire_params&lt;br /&gt;
  params.require(:questionnaire).permit(&lt;br /&gt;
    :name, &lt;br /&gt;
    :instructor_id,&lt;br /&gt;
    :min_question_score,&lt;br /&gt;
    :max_question_score,&lt;br /&gt;
    :questionnaire_type,&lt;br /&gt;
    :private,&lt;br /&gt;
    items_attributes: [&lt;br /&gt;
      :txt,&lt;br /&gt;
      :question_type,&lt;br /&gt;
      :break_before,&lt;br /&gt;
      :weight,&lt;br /&gt;
      :skippable,&lt;br /&gt;
      :seq,&lt;br /&gt;
      { quiz_question_choices_attributes: %i[txt iscorrect] }&lt;br /&gt;
    ]&lt;br /&gt;
  )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This change improves compatibility with the updated data model and supports dynamic quiz item rendering, skippable questions, and proper nesting for answer choices.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/blob/b0079c802e46f4a7e22c1c4ea1757be51ebda23b/spec/requests/api/v1/student_quizzes_controller_spec.rb) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
We added three test cases in the file:  &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored methods in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After adding the tests, our test overage increased by 6% from 68% to around 74%.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure the robustness of the refactored controller and the correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
https://youtu.be/T_qFnMeGhyg&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164979</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164979"/>
		<updated>2025-04-23T01:59:03Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Video */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz_15.jpeg|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
d) Renaming create_questions_and_answers to create_items_and_choices: There were discrepancies in the database. The question model was renamed to item and there were choices associated with it. The new name is more meaningful.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/blob/b0079c802e46f4a7e22c1c4ea1757be51ebda23b/spec/requests/api/v1/student_quizzes_controller_spec.rb) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
We added three test cases in the file:  &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored methods in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After adding the tests, our test overage increased by 6% from 68% to around 74%.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure the robustness of the refactored controller and the correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
https://youtu.be/T_qFnMeGhyg&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164978</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164978"/>
		<updated>2025-04-23T01:58:46Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz_15.jpeg|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
d) Renaming create_questions_and_answers to create_items_and_choices: There were discrepancies in the database. The question model was renamed to item and there were choices associated with it. The new name is more meaningful.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/blob/b0079c802e46f4a7e22c1c4ea1757be51ebda23b/spec/requests/api/v1/student_quizzes_controller_spec.rb) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
We added three test cases in the file:  &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored methods in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After adding the tests, our test overage increased by 6% from 68% to around 74%.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure the robustness of the refactored controller and the correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
https://drive.google.com/file/d/14sq5o2o3nLxn9rcJlVfXYfeQOg0EXlly/view?usp=sharing&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quiz_15.jpeg&amp;diff=164974</id>
		<title>File:Student quiz 15.jpeg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quiz_15.jpeg&amp;diff=164974"/>
		<updated>2025-04-23T01:52:32Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164727</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164727"/>
		<updated>2025-04-22T20:19:53Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/1e22637ed9ab350ac1e75ef5607ea8d4bdd7edd2) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
As outlined above, we refactored three key files. To ensure comprehensive coverage and maintain system reliability, we have implemented thorough testing for all affected components. This includes both unit and request tests, which are written using RSpec. The test coverage has been added to the following files:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/find_resource_service_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/services/api/v1/resource_map_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored method in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Below is a comprehensive test matrix for each component and method.&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! HTTP Method !! Endpoint/ Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| index || GET || /api/v1/student_quizzes || #index || Fetch all quizzes || Create sample quiz.&amp;lt;br&amp;gt;GET request.&amp;lt;br&amp;gt;Expect 200 OK &amp;amp; array response.&lt;br /&gt;
|-&lt;br /&gt;
| create (skippable) || POST || /api/v1/student_quizzes || #create || Create quiz with skippable items || POST with `skippable: true`.&amp;lt;br&amp;gt;Expect 201 Created.&lt;br /&gt;
|-&lt;br /&gt;
| create (non-skippable) || POST || /api/v1/student_quizzes || #create || Create quiz with non-skippable items || POST with `skippable: false`.&amp;lt;br&amp;gt;Expect 201 Created.&lt;br /&gt;
|-&lt;br /&gt;
| destroy || DELETE || /api/v1/student_quizzes/:id || #destroy || Delete a quiz || Create quiz.&amp;lt;br&amp;gt;DELETE request.&amp;lt;br&amp;gt;Expect 204 No Content.&lt;br /&gt;
|-&lt;br /&gt;
| show || GET || /api/v1/student_quizzes/:id || #show || Show quiz || Fetch by ID.&amp;lt;br&amp;gt;Expect 200 OK.&lt;br /&gt;
|-&lt;br /&gt;
| assign_quiz || POST || /student_quizzes/assign || #assign_quiz || Assign to participant || POST assignment.&amp;lt;br&amp;gt;Expect 201 Created.&lt;br /&gt;
|-&lt;br /&gt;
| submit_quiz || POST || /submit_answers || #submit_quiz || Submit answers || POST quiz answers.&amp;lt;br&amp;gt;Expect 200 OK + score in response.&lt;br /&gt;
|-&lt;br /&gt;
| update || PUT || /student_quizzes/:id || #update || Update quiz || PUT updated name.&amp;lt;br&amp;gt;Expect 200 OK.&lt;br /&gt;
|-&lt;br /&gt;
| fetch_quiz || GET || /student_quizzes/:id || #fetch_quiz || Fetch quiz || Validate correct fetch using new method.&lt;br /&gt;
|-&lt;br /&gt;
| render_success || GET || /student_quizzes || #render_success || Render success || Expect 200 OK.&lt;br /&gt;
|-&lt;br /&gt;
| questionnaire_params || N/A || questionnaire_params(params) || #questionnaire_params || Permit strong parameters || Handles nested attributes + validation.&lt;br /&gt;
|-&lt;br /&gt;
| create_questions_and_answers || N/A || create_questions_and_answers || #create_questions_and_answers || Create nested resources || Save questions/answers with `quiz_question_choices_attributes`.&lt;br /&gt;
|-&lt;br /&gt;
| render_error || N/A || render_error || #render_error || Custom error responses || 422 + error message check.&lt;br /&gt;
|-&lt;br /&gt;
| quiz_assigned? || N/A || quiz_assigned? || #quiz_assigned? || Check assignment || Return true/false.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || N/A || build_response_map || #build_response_map || Link quiz + participant || Valid object or nil return.&lt;br /&gt;
|-&lt;br /&gt;
| check_instructor_role || N/A || check_instructor_role || #check_instructor_role || Role verification || Auth role check.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;resource_map_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| calculate_score || calculate_score || #calculate_score || Score responses || Mock score.&amp;lt;br&amp;gt;Expect correct total.&lt;br /&gt;
|-&lt;br /&gt;
| find_or_initialize_response || find_or_initialize_response || #find_or_initialize_response || Load/init response || Existing or new response returned.&lt;br /&gt;
|-&lt;br /&gt;
| get_score || get_score || #get_score || Fetch quiz score || Return score or nil.&lt;br /&gt;
|-&lt;br /&gt;
| process_answers || process_answers || #process_answers || Grade answers || Mock pass/fail.&amp;lt;br&amp;gt;Return boolean.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || build_response_map || #build_response_map || Return valid map || Valid object or nil.&lt;br /&gt;
|-&lt;br /&gt;
| find_for_current_user || find_for_current_user || #find_for_current_user || Locate user’s response || Return matching map.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;response_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
This model now handles the core quiz scoring logic and validation.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Method !! Description !! Key Scenarios Tested&lt;br /&gt;
|-&lt;br /&gt;
| reportable_difference? || Detect score deviation across responses || False when only one response.&amp;lt;br&amp;gt;True if deviation &amp;gt; notification limit.&lt;br /&gt;
|-&lt;br /&gt;
| calculate_total_score || Compute total score using weighted scorable items || Correct score returned when answers match and skippable items are ignored.&lt;br /&gt;
|-&lt;br /&gt;
| average_score || Calculate % based on total/max || 0 when max score = 0.&amp;lt;br&amp;gt;80 when total = 4 and max = 5.&lt;br /&gt;
|-&lt;br /&gt;
| maximum_score || Total max possible based on weights || 0 if all items are skipped.&amp;lt;br&amp;gt;Correct total otherwise.&lt;br /&gt;
|-&lt;br /&gt;
| response_assignment || Fetch assignment from ResponseMap or ReviewResponseMap || Proper assignment returned from either Participant or Reviewee link.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#maximum_score' do&lt;br /&gt;
  it 'ignores skippable items from max score' do&lt;br /&gt;
    item_skipped = ScoredItem.new(weight: 2, skippable: true)&lt;br /&gt;
    questionnaire.items = [item_skipped]&lt;br /&gt;
    allow(questionnaire).to receive(:max_question_score).and_return(5)&lt;br /&gt;
    allow(response).to receive(:questionnaire_by_answer).and_return(questionnaire)&lt;br /&gt;
    response.scores = []&lt;br /&gt;
    expect(response.maximum_score).to eq(0)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Additional Spec Files ====&lt;br /&gt;
&lt;br /&gt;
- &amp;lt;code&amp;gt;find_resource_service_spec.rb&amp;lt;/code&amp;gt;: Validates correctness of resource fetching and exception handling for missing models.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure robustness of the refactored controller, and correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164726</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164726"/>
		<updated>2025-04-22T20:19:16Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/1e22637ed9ab350ac1e75ef5607ea8d4bdd7edd2) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
As outlined above, we refactored three key files. To ensure comprehensive coverage and maintain system reliability, we have implemented thorough testing for all affected components. This includes both unit and request tests, which are written using RSpec. The test coverage has been added to the following files:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/find_resource_service_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/services/api/v1/resource_map_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored method in `StudentQuizzesController` and associated models.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.  &lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Below is a comprehensive test matrix for each component and method.&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! HTTP Method !! Endpoint/ Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| index || GET || /api/v1/student_quizzes || #index || Fetch all quizzes || Create sample quiz.&amp;lt;br&amp;gt;GET request.&amp;lt;br&amp;gt;Expect 200 OK &amp;amp; array response.&lt;br /&gt;
|-&lt;br /&gt;
| create (skippable) || POST || /api/v1/student_quizzes || #create || Create quiz with skippable items || POST with `skippable: true`.&amp;lt;br&amp;gt;Expect 201 Created.&lt;br /&gt;
|-&lt;br /&gt;
| create (non-skippable) || POST || /api/v1/student_quizzes || #create || Create quiz with non-skippable items || POST with `skippable: false`.&amp;lt;br&amp;gt;Expect 201 Created.&lt;br /&gt;
|-&lt;br /&gt;
| destroy || DELETE || /api/v1/student_quizzes/:id || #destroy || Delete a quiz || Create quiz.&amp;lt;br&amp;gt;DELETE request.&amp;lt;br&amp;gt;Expect 204 No Content.&lt;br /&gt;
|-&lt;br /&gt;
| show || GET || /api/v1/student_quizzes/:id || #show || Show quiz || Fetch by ID.&amp;lt;br&amp;gt;Expect 200 OK.&lt;br /&gt;
|-&lt;br /&gt;
| assign_quiz || POST || /student_quizzes/assign || #assign_quiz || Assign to participant || POST assignment.&amp;lt;br&amp;gt;Expect 201 Created.&lt;br /&gt;
|-&lt;br /&gt;
| submit_quiz || POST || /submit_answers || #submit_quiz || Submit answers || POST quiz answers.&amp;lt;br&amp;gt;Expect 200 OK + score in response.&lt;br /&gt;
|-&lt;br /&gt;
| update || PUT || /student_quizzes/:id || #update || Update quiz || PUT updated name.&amp;lt;br&amp;gt;Expect 200 OK.&lt;br /&gt;
|-&lt;br /&gt;
| fetch_quiz || GET || /student_quizzes/:id || #fetch_quiz || Fetch quiz || Validate correct fetch using new method.&lt;br /&gt;
|-&lt;br /&gt;
| render_success || GET || /student_quizzes || #render_success || Render success || Expect 200 OK.&lt;br /&gt;
|-&lt;br /&gt;
| questionnaire_params || N/A || questionnaire_params(params) || #questionnaire_params || Permit strong parameters || Handles nested attributes + validation.&lt;br /&gt;
|-&lt;br /&gt;
| create_questions_and_answers || N/A || create_questions_and_answers || #create_questions_and_answers || Create nested resources || Save questions/answers with `quiz_question_choices_attributes`.&lt;br /&gt;
|-&lt;br /&gt;
| render_error || N/A || render_error || #render_error || Custom error responses || 422 + error message check.&lt;br /&gt;
|-&lt;br /&gt;
| quiz_assigned? || N/A || quiz_assigned? || #quiz_assigned? || Check assignment || Return true/false.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || N/A || build_response_map || #build_response_map || Link quiz + participant || Valid object or nil return.&lt;br /&gt;
|-&lt;br /&gt;
| check_instructor_role || N/A || check_instructor_role || #check_instructor_role || Role verification || Auth role check.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;resource_map_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| calculate_score || calculate_score || #calculate_score || Score responses || Mock score.&amp;lt;br&amp;gt;Expect correct total.&lt;br /&gt;
|-&lt;br /&gt;
| find_or_initialize_response || find_or_initialize_response || #find_or_initialize_response || Load/init response || Existing or new response returned.&lt;br /&gt;
|-&lt;br /&gt;
| get_score || get_score || #get_score || Fetch quiz score || Return score or nil.&lt;br /&gt;
|-&lt;br /&gt;
| process_answers || process_answers || #process_answers || Grade answers || Mock pass/fail.&amp;lt;br&amp;gt;Return boolean.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || build_response_map || #build_response_map || Return valid map || Valid object or nil.&lt;br /&gt;
|-&lt;br /&gt;
| find_for_current_user || find_for_current_user || #find_for_current_user || Locate user’s response || Return matching map.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;response_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
This model now handles the core quiz scoring logic and validation.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Method !! Description !! Key Scenarios Tested&lt;br /&gt;
|-&lt;br /&gt;
| reportable_difference? || Detect score deviation across responses || False when only one response.&amp;lt;br&amp;gt;True if deviation &amp;gt; notification limit.&lt;br /&gt;
|-&lt;br /&gt;
| calculate_total_score || Compute total score using weighted scorable items || Correct score returned when answers match and skippable items are ignored.&lt;br /&gt;
|-&lt;br /&gt;
| average_score || Calculate % based on total/max || 0 when max score = 0.&amp;lt;br&amp;gt;80 when total = 4 and max = 5.&lt;br /&gt;
|-&lt;br /&gt;
| maximum_score || Total max possible based on weights || 0 if all items are skipped.&amp;lt;br&amp;gt;Correct total otherwise.&lt;br /&gt;
|-&lt;br /&gt;
| response_assignment || Fetch assignment from ResponseMap or ReviewResponseMap || Proper assignment returned from either Participant or Reviewee link.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#maximum_score' do&lt;br /&gt;
  it 'ignores skippable items from max score' do&lt;br /&gt;
    item_skipped = ScoredItem.new(weight: 2, skippable: true)&lt;br /&gt;
    questionnaire.items = [item_skipped]&lt;br /&gt;
    allow(questionnaire).to receive(:max_question_score).and_return(5)&lt;br /&gt;
    allow(response).to receive(:questionnaire_by_answer).and_return(questionnaire)&lt;br /&gt;
    response.scores = []&lt;br /&gt;
    expect(response.maximum_score).to eq(0)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Additional Spec Files ====&lt;br /&gt;
&lt;br /&gt;
- &amp;lt;code&amp;gt;find_resource_service_spec.rb&amp;lt;/code&amp;gt;: Validates correctness of resource fetching and exception handling for missing models.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''':  &lt;br /&gt;
The RSpec suite thoroughly validates controller functionality, model-level scoring, and key edge cases like skippable questions and zero scoring boundaries. These tests ensure robustness of the refactored controller, and correctness of all new features introduced.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164724</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164724"/>
		<updated>2025-04-22T19:54:40Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/1e22637ed9ab350ac1e75ef5607ea8d4bdd7edd2) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
As outlined above, we refactored three key files. To ensure comprehensive coverage and maintain system reliability, we have implemented thorough testing for all affected components. This includes both unit and request tests, which are written using RSpec. The test coverage has been added to the following files:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/find_resource_service_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/services/api/v1/resource_map_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
&lt;br /&gt;
After setting up authentication and creating test users and roles, we implemented request-level and unit-level RSpec tests to validate the functionality of each action and refactored method in `StudentQuizzesController`.&lt;br /&gt;
&lt;br /&gt;
Below is an example of testing the &amp;lt;code&amp;gt;#index&amp;lt;/code&amp;gt; action:&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.&lt;br /&gt;
&lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(&lt;br /&gt;
      name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
      min_question_score: 0, max_question_score: 10&lt;br /&gt;
    )&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the above, we tested quiz creation with both skippable and non-skippable items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#create' do&lt;br /&gt;
  it 'creates a questionnaire with skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it 'creates a questionnaire with non-skippable items and choices' do&lt;br /&gt;
    post api_v1_student_quizzes_path,&lt;br /&gt;
        params: non_skipable_valid_attrs.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:created)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also tested quiz update functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#update' do&lt;br /&gt;
  it 'updates a questionnaire' do&lt;br /&gt;
    put &amp;quot;/api/v1/student_quizzes/#{quiz.id}&amp;quot;,&lt;br /&gt;
        params: { questionnaire: { name: 'Updated Questionnaire' } }.to_json,&lt;br /&gt;
        headers: auth_headers&lt;br /&gt;
&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    expect(JSON.parse(response.body)['name']).to eq('Updated Questionnaire')&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Below is a comprehensive test matrix for each component and method.&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! HTTP Method !! Endpoint/ Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| index || GET || /api/v1/student_quizzes || #index || Fetch all quizzes || Create sample quiz.&amp;lt;br&amp;gt;GET request.&amp;lt;br&amp;gt;Expect 200 OK &amp;amp; array response.&lt;br /&gt;
|-&lt;br /&gt;
| create (skippable) || POST || /api/v1/student_quizzes || #create || Create quiz with skippable items || POST with `skippable: true`.&amp;lt;br&amp;gt;Expect 201 Created.&lt;br /&gt;
|-&lt;br /&gt;
| create (non-skippable) || POST || /api/v1/student_quizzes || #create || Create quiz with non-skippable items || POST with `skippable: false`.&amp;lt;br&amp;gt;Expect 201 Created.&lt;br /&gt;
|-&lt;br /&gt;
| destroy || DELETE || /api/v1/student_quizzes/:id || #destroy || Delete a quiz || Create quiz.&amp;lt;br&amp;gt;DELETE request.&amp;lt;br&amp;gt;Expect 204 No Content.&lt;br /&gt;
|-&lt;br /&gt;
| show || GET || /api/v1/student_quizzes/:id || #show || Show quiz || Fetch by ID.&amp;lt;br&amp;gt;Expect 200 OK.&lt;br /&gt;
|-&lt;br /&gt;
| assign_quiz || POST || /student_quizzes/assign || #assign_quiz || Assign to participant || POST assignment.&amp;lt;br&amp;gt;Expect 201 Created.&lt;br /&gt;
|-&lt;br /&gt;
| submit_quiz || POST || /submit_answers || #submit_quiz || Submit answers || POST quiz answers.&amp;lt;br&amp;gt;Expect 200 OK + score in response.&lt;br /&gt;
|-&lt;br /&gt;
| update || PUT || /student_quizzes/:id || #update || Update quiz || PUT updated name.&amp;lt;br&amp;gt;Expect 200 OK.&lt;br /&gt;
|-&lt;br /&gt;
| fetch_quiz || GET || /student_quizzes/:id || #fetch_quiz || Fetch quiz || Validate correct fetch using new method.&lt;br /&gt;
|-&lt;br /&gt;
| render_success || GET || /student_quizzes || #render_success || Render success || Expect 200 OK.&lt;br /&gt;
|-&lt;br /&gt;
| questionnaire_params || N/A || questionnaire_params(params) || #questionnaire_params || Permit strong parameters || Handles nested attributes + validation.&lt;br /&gt;
|-&lt;br /&gt;
| create_questions_and_answers || N/A || create_questions_and_answers || #create_questions_and_answers || Create nested resources || Save questions/answers with `quiz_question_choices_attributes`.&lt;br /&gt;
|-&lt;br /&gt;
| render_error || N/A || render_error || #render_error || Custom error responses || 422 + error message check.&lt;br /&gt;
|-&lt;br /&gt;
| quiz_assigned? || N/A || quiz_assigned? || #quiz_assigned? || Check assignment || Return true/false.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || N/A || build_response_map || #build_response_map || Link quiz + participant || Valid object or nil return.&lt;br /&gt;
|-&lt;br /&gt;
| check_instructor_role || N/A || check_instructor_role || #check_instructor_role || Role verification || Auth role check.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;resource_map_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| calculate_score || calculate_score || #calculate_score || Score responses || Mock score.&amp;lt;br&amp;gt;Expect correct total.&lt;br /&gt;
|-&lt;br /&gt;
| find_or_initialize_response || find_or_initialize_response || #find_or_initialize_response || Load/init response || Existing or new response returned.&lt;br /&gt;
|-&lt;br /&gt;
| get_score || get_score || #get_score || Fetch quiz score || Return score or nil.&lt;br /&gt;
|-&lt;br /&gt;
| process_answers || process_answers || #process_answers || Grade answers || Mock pass/fail.&amp;lt;br&amp;gt;Return boolean.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || build_response_map || #build_response_map || Return valid map || Valid object or nil.&lt;br /&gt;
|-&lt;br /&gt;
| find_for_current_user || find_for_current_user || #find_for_current_user || Locate user’s response || Return matching map.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Additional Spec Files ====&lt;br /&gt;
- &amp;lt;code&amp;gt;find_resource_service_spec.rb&amp;lt;/code&amp;gt;: Validates correctness of resource fetching logic and custom error handling for missing records.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
'''Conclusion''': The test suite comprehensively validates controller behavior, model logic, and edge cases (like quiz skipping, parameter whitelisting, and deletion). All test cases passed successfully, ensuring stable reimplementation.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164723</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164723"/>
		<updated>2025-04-22T19:45:33Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Next Steps */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/1e22637ed9ab350ac1e75ef5607ea8d4bdd7edd2) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
As outlined above, we refactored three key files. To ensure comprehensive coverage and maintain system reliability, we have implemented thorough testing for all affected components. This includes both unit and request tests, which are written using RSpec. The test coverage has been added to the following files:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/find_resource_service_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/services/api/v1/resource_map_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
After the setup and authentication, we test each refactored file method. Below is an example of testing &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt; Method in &amp;lt;code&amp;gt;StudentQuizzesController&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.&lt;br /&gt;
&lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
                              min_question_score: 0, max_question_score: 10)&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below are the complete tables that list our full test plans.&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! HTTP Method !! Endpoint/ Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| index || GET || /api/v1/student_quizzes || #index || Fetch all quizzes || Creates a sample quiz.&amp;lt;br&amp;gt;Sends a GET request to fetch quizzes.&amp;lt;br&amp;gt;Expects 200 OK and validates the response is an array.&lt;br /&gt;
|-&lt;br /&gt;
| create || POST || /api/v1/student_quizzes || #create || Create a new quiz || Prepares quiz parameters with nested questions and answers.&amp;lt;br&amp;gt;Sends a POST request to create a new quiz.&amp;lt;br&amp;gt;Expects 201 Created and checks for 'name' and 'questions' in response.&lt;br /&gt;
|-&lt;br /&gt;
| destroy || DELETE || /api/v1/student_quizzes/:id || #destroy || Delete a quiz || Creates a quiz record.&amp;lt;br&amp;gt;Sends a DELETE request to remove the quiz.&amp;lt;br&amp;gt;Expects 204 No Content and confirms quiz is deleted from the database.&lt;br /&gt;
|-&lt;br /&gt;
| show || GET || /api/v1/student_quizzes/:id || #show || Show quiz details || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request to fetch the quiz by ID.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name and ID in the response.&lt;br /&gt;
|-&lt;br /&gt;
| assign_quiz || POST || /api/v1/student_quizzes/assign || #assign_quiz || Assign quiz to a participant || Assigns a quiz to a Participant model instance.&amp;lt;br&amp;gt;Sends a POST request to the assign endpoint.&amp;lt;br&amp;gt;Verifies 201 Created status.&lt;br /&gt;
|-&lt;br /&gt;
| submit_quiz || POST || /api/v1/student_quizzes/submit_answers || #submit_quiz || Submit answers || Creates a quiz and a corresponding response map.&amp;lt;br&amp;gt;Sends quiz answer submission via POST request.&amp;lt;br&amp;gt;Checks for 200 OK and validates 'total_score' is present in response.&lt;br /&gt;
|-&lt;br /&gt;
| update || PUT || /api/v1/student_quizzes/:id || #update || Update quiz || Creates a quiz with an initial name.&amp;lt;br&amp;gt;Sends a PUT request with updated name.&amp;lt;br&amp;gt;Expects 200 OK and checks that the name is updated in the response.&lt;br /&gt;
|-&lt;br /&gt;
| fetch_quiz || GET || /api/v1/student_quizzes/:id || #fetch_quiz || Fetch quiz using fetch_quiz method || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request using fetch_quiz logic.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name in response.&lt;br /&gt;
|-&lt;br /&gt;
| render_success || GET || /api/v1/student_quizzes || #render_success || Render success response || Calls an action that invokes render_success (index).&amp;lt;br&amp;gt;Sends a GET request and expects 200 OK response.&lt;br /&gt;
|-&lt;br /&gt;
| create_questionnaire || POST || /api/v1/student_quizzes || #create_questionnaire || Create questionnaire || Sends a POST request with questionnaire attributes.&amp;lt;br&amp;gt;Checks if the questionnaire is created and returns 201 Created.&amp;lt;br&amp;gt;Validates the name in the response.&lt;br /&gt;
|-&lt;br /&gt;
| questionnaire_params || N/A || questionnaire_params(params) || #questionnaire_params || Extract strong parameters || Validates strong parameter extraction logic.&amp;lt;br&amp;gt;Handles valid, missing, and invalid parameters.&amp;lt;br&amp;gt;Raises ActionController::ParameterMissing when necessary.&lt;br /&gt;
|-&lt;br /&gt;
| create_questions_and_answers || N/A || create_questions_and_answers(questionnaire, questions_attributes) || #create_questions_and_answers || Build nested resources || Creates a questionnaire object.&amp;lt;br&amp;gt;Calls the method with nested question and answer attributes.&amp;lt;br&amp;gt;Validates that questions and answers are successfully saved.&lt;br /&gt;
|-&lt;br /&gt;
| render_error || N/A || render_error(message, status) || #render_error || Render error response || Triggers an action with invalid parameters.&amp;lt;br&amp;gt;Checks that 422 Unprocessable Entity is returned.&amp;lt;br&amp;gt;Validates the presence of an error message in the response.&lt;br /&gt;
|-&lt;br /&gt;
| quiz_assigned? || N/A || quiz_assigned?(participant, quiz) || #quiz_assigned? || Check quiz assignment || Checks if a quiz has already been assigned to a participant.&amp;lt;br&amp;gt;Returns true or false based on assignment existence and input validity.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || N/A || build_response_map(quiz, participant) || #build_response_map || Create response map || Creates a quiz and participant.&amp;lt;br&amp;gt;Builds a ResponseMap and checks attributes.&amp;lt;br&amp;gt;Returns nil when quiz or participant is missing.&lt;br /&gt;
|-&lt;br /&gt;
| check_instructor_role || N/A || check_instructor_role || #check_instructor_role || Role verification || Checks if current user is an instructor.&amp;lt;br&amp;gt;Returns true if user has instructor role, false otherwise.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;resource_map_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| calculate_score || calculate_score || #calculate_score || Calculate the score based on associated responses. || Mocks a response with a known score.&amp;lt;br&amp;gt;Calls `calculate_score` on the response_map.&amp;lt;br&amp;gt;Expects the calculated score to match.&lt;br /&gt;
|-&lt;br /&gt;
| find_or_initialize_response || find_or_initialize_response(questionnaire) || #find_or_initialize_response || Retrieve an existing response or initialize a new one for the given questionnaire. || Checks if a response already exists and returns it.&amp;lt;br&amp;gt;If none exists, it initializes a new response with the questionnaire.&lt;br /&gt;
|-&lt;br /&gt;
| get_score || get_score(questionnaire) || #get_score || Fetch the score from a response for a specific questionnaire. || Mocks a score and ensures `get_score` returns it.&amp;lt;br&amp;gt;Returns nil if there is no associated response.&lt;br /&gt;
|-&lt;br /&gt;
| process_answers || process_answers || #process_answers || Process answers in the associated response and update state. || Mocks success and failure conditions of answer processing.&amp;lt;br&amp;gt;Returns true or false accordingly.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || build_response_map(questionnaire, participant) || #build_response_map || Create and return a response map for a given questionnaire and participant. || Creates and returns a valid ResponseMap object.&amp;lt;br&amp;gt;Returns nil if either questionnaire or participant is not valid.&lt;br /&gt;
|-&lt;br /&gt;
| find_for_current_user || find_for_current_user(user) || #find_for_current_user || Locate the response map associated with the current user. || Returns the existing response map if present.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Beside those, we test &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; that can be found in &amp;lt;code&amp;gt; find_resource_service_spec.rb&amp;lt;/code&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164722</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164722"/>
		<updated>2025-04-22T19:43:41Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/1e22637ed9ab350ac1e75ef5607ea8d4bdd7edd2) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
As outlined above, we refactored three key files. To ensure comprehensive coverage and maintain system reliability, we have implemented thorough testing for all affected components. This includes both unit and request tests, which are written using RSpec. The test coverage has been added to the following files:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/find_resource_service_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/services/api/v1/resource_map_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
After the setup and authentication, we test each refactored file method. Below is an example of testing &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt; Method in &amp;lt;code&amp;gt;StudentQuizzesController&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.&lt;br /&gt;
&lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
                              min_question_score: 0, max_question_score: 10)&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below are the complete tables that list our full test plans.&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! HTTP Method !! Endpoint/ Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| index || GET || /api/v1/student_quizzes || #index || Fetch all quizzes || Creates a sample quiz.&amp;lt;br&amp;gt;Sends a GET request to fetch quizzes.&amp;lt;br&amp;gt;Expects 200 OK and validates the response is an array.&lt;br /&gt;
|-&lt;br /&gt;
| create || POST || /api/v1/student_quizzes || #create || Create a new quiz || Prepares quiz parameters with nested questions and answers.&amp;lt;br&amp;gt;Sends a POST request to create a new quiz.&amp;lt;br&amp;gt;Expects 201 Created and checks for 'name' and 'questions' in response.&lt;br /&gt;
|-&lt;br /&gt;
| destroy || DELETE || /api/v1/student_quizzes/:id || #destroy || Delete a quiz || Creates a quiz record.&amp;lt;br&amp;gt;Sends a DELETE request to remove the quiz.&amp;lt;br&amp;gt;Expects 204 No Content and confirms quiz is deleted from the database.&lt;br /&gt;
|-&lt;br /&gt;
| show || GET || /api/v1/student_quizzes/:id || #show || Show quiz details || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request to fetch the quiz by ID.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name and ID in the response.&lt;br /&gt;
|-&lt;br /&gt;
| assign_quiz || POST || /api/v1/student_quizzes/assign || #assign_quiz || Assign quiz to a participant || Assigns a quiz to a Participant model instance.&amp;lt;br&amp;gt;Sends a POST request to the assign endpoint.&amp;lt;br&amp;gt;Verifies 201 Created status.&lt;br /&gt;
|-&lt;br /&gt;
| submit_quiz || POST || /api/v1/student_quizzes/submit_answers || #submit_quiz || Submit answers || Creates a quiz and a corresponding response map.&amp;lt;br&amp;gt;Sends quiz answer submission via POST request.&amp;lt;br&amp;gt;Checks for 200 OK and validates 'total_score' is present in response.&lt;br /&gt;
|-&lt;br /&gt;
| update || PUT || /api/v1/student_quizzes/:id || #update || Update quiz || Creates a quiz with an initial name.&amp;lt;br&amp;gt;Sends a PUT request with updated name.&amp;lt;br&amp;gt;Expects 200 OK and checks that the name is updated in the response.&lt;br /&gt;
|-&lt;br /&gt;
| fetch_quiz || GET || /api/v1/student_quizzes/:id || #fetch_quiz || Fetch quiz using fetch_quiz method || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request using fetch_quiz logic.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name in response.&lt;br /&gt;
|-&lt;br /&gt;
| render_success || GET || /api/v1/student_quizzes || #render_success || Render success response || Calls an action that invokes render_success (index).&amp;lt;br&amp;gt;Sends a GET request and expects 200 OK response.&lt;br /&gt;
|-&lt;br /&gt;
| create_questionnaire || POST || /api/v1/student_quizzes || #create_questionnaire || Create questionnaire || Sends a POST request with questionnaire attributes.&amp;lt;br&amp;gt;Checks if the questionnaire is created and returns 201 Created.&amp;lt;br&amp;gt;Validates the name in the response.&lt;br /&gt;
|-&lt;br /&gt;
| questionnaire_params || N/A || questionnaire_params(params) || #questionnaire_params || Extract strong parameters || Validates strong parameter extraction logic.&amp;lt;br&amp;gt;Handles valid, missing, and invalid parameters.&amp;lt;br&amp;gt;Raises ActionController::ParameterMissing when necessary.&lt;br /&gt;
|-&lt;br /&gt;
| create_questions_and_answers || N/A || create_questions_and_answers(questionnaire, questions_attributes) || #create_questions_and_answers || Build nested resources || Creates a questionnaire object.&amp;lt;br&amp;gt;Calls the method with nested question and answer attributes.&amp;lt;br&amp;gt;Validates that questions and answers are successfully saved.&lt;br /&gt;
|-&lt;br /&gt;
| render_error || N/A || render_error(message, status) || #render_error || Render error response || Triggers an action with invalid parameters.&amp;lt;br&amp;gt;Checks that 422 Unprocessable Entity is returned.&amp;lt;br&amp;gt;Validates the presence of an error message in the response.&lt;br /&gt;
|-&lt;br /&gt;
| quiz_assigned? || N/A || quiz_assigned?(participant, quiz) || #quiz_assigned? || Check quiz assignment || Checks if a quiz has already been assigned to a participant.&amp;lt;br&amp;gt;Returns true or false based on assignment existence and input validity.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || N/A || build_response_map(quiz, participant) || #build_response_map || Create response map || Creates a quiz and participant.&amp;lt;br&amp;gt;Builds a ResponseMap and checks attributes.&amp;lt;br&amp;gt;Returns nil when quiz or participant is missing.&lt;br /&gt;
|-&lt;br /&gt;
| check_instructor_role || N/A || check_instructor_role || #check_instructor_role || Role verification || Checks if current user is an instructor.&amp;lt;br&amp;gt;Returns true if user has instructor role, false otherwise.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;resource_map_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| calculate_score || calculate_score || #calculate_score || Calculate the score based on associated responses. || Mocks a response with a known score.&amp;lt;br&amp;gt;Calls `calculate_score` on the response_map.&amp;lt;br&amp;gt;Expects the calculated score to match.&lt;br /&gt;
|-&lt;br /&gt;
| find_or_initialize_response || find_or_initialize_response(questionnaire) || #find_or_initialize_response || Retrieve an existing response or initialize a new one for the given questionnaire. || Checks if a response already exists and returns it.&amp;lt;br&amp;gt;If none exists, it initializes a new response with the questionnaire.&lt;br /&gt;
|-&lt;br /&gt;
| get_score || get_score(questionnaire) || #get_score || Fetch the score from a response for a specific questionnaire. || Mocks a score and ensures `get_score` returns it.&amp;lt;br&amp;gt;Returns nil if there is no associated response.&lt;br /&gt;
|-&lt;br /&gt;
| process_answers || process_answers || #process_answers || Process answers in the associated response and update state. || Mocks success and failure conditions of answer processing.&amp;lt;br&amp;gt;Returns true or false accordingly.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || build_response_map(questionnaire, participant) || #build_response_map || Create and return a response map for a given questionnaire and participant. || Creates and returns a valid ResponseMap object.&amp;lt;br&amp;gt;Returns nil if either questionnaire or participant is not valid.&lt;br /&gt;
|-&lt;br /&gt;
| find_for_current_user || find_for_current_user(user) || #find_for_current_user || Locate the response map associated with the current user. || Returns the existing response map if present.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Beside those, we test &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; that can be found in &amp;lt;code&amp;gt; find_resource_service_spec.rb&amp;lt;/code&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==Next Steps==&lt;br /&gt;
&lt;br /&gt;
* We will implement the feature that lets students skip quiz questions, including updating the rubric model and UI to support it properly.&lt;br /&gt;
&lt;br /&gt;
* We plan to move score calculation logic from the response map to the response object to better support cases where users take the quiz multiple times.&lt;br /&gt;
&lt;br /&gt;
* The current test coverage is 69.72%; we are confident of increasing the test coverage by implementing all of our test plans.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164721</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164721"/>
		<updated>2025-04-22T19:43:11Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Skippable quiz questions are now supported with a `skippable` attribute added to quiz items. The system will now skip over these questions during answer validation and scoring, improving flexibility for quiz creators. Logic to process `skippable` answers was added during submission.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/1e22637ed9ab350ac1e75ef5607ea8d4bdd7edd2) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
As outlined above, we refactored three key files. To ensure comprehensive coverage and maintain system reliability, we have implemented thorough testing for all affected components. This includes both unit and request tests, which are written using RSpec. The test coverage has been added to the following files:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/find_resource_service_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/services/api/v1/resource_map_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
After the setup and authentication, we test each refactored file method. Below is an example of testing &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt; Method in &amp;lt;code&amp;gt;StudentQuizzesController&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.&lt;br /&gt;
&lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
                              min_question_score: 0, max_question_score: 10)&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below are the complete tables that list our full test plans.&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! HTTP Method !! Endpoint/ Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| index || GET || /api/v1/student_quizzes || #index || Fetch all quizzes || Creates a sample quiz.&amp;lt;br&amp;gt;Sends a GET request to fetch quizzes.&amp;lt;br&amp;gt;Expects 200 OK and validates the response is an array.&lt;br /&gt;
|-&lt;br /&gt;
| create || POST || /api/v1/student_quizzes || #create || Create a new quiz || Prepares quiz parameters with nested questions and answers.&amp;lt;br&amp;gt;Sends a POST request to create a new quiz.&amp;lt;br&amp;gt;Expects 201 Created and checks for 'name' and 'questions' in response.&lt;br /&gt;
|-&lt;br /&gt;
| destroy || DELETE || /api/v1/student_quizzes/:id || #destroy || Delete a quiz || Creates a quiz record.&amp;lt;br&amp;gt;Sends a DELETE request to remove the quiz.&amp;lt;br&amp;gt;Expects 204 No Content and confirms quiz is deleted from the database.&lt;br /&gt;
|-&lt;br /&gt;
| show || GET || /api/v1/student_quizzes/:id || #show || Show quiz details || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request to fetch the quiz by ID.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name and ID in the response.&lt;br /&gt;
|-&lt;br /&gt;
| assign_quiz || POST || /api/v1/student_quizzes/assign || #assign_quiz || Assign quiz to a participant || Assigns a quiz to a Participant model instance.&amp;lt;br&amp;gt;Sends a POST request to the assign endpoint.&amp;lt;br&amp;gt;Verifies 201 Created status.&lt;br /&gt;
|-&lt;br /&gt;
| submit_quiz || POST || /api/v1/student_quizzes/submit_answers || #submit_quiz || Submit answers || Creates a quiz and a corresponding response map.&amp;lt;br&amp;gt;Sends quiz answer submission via POST request.&amp;lt;br&amp;gt;Checks for 200 OK and validates 'total_score' is present in response.&lt;br /&gt;
|-&lt;br /&gt;
| update || PUT || /api/v1/student_quizzes/:id || #update || Update quiz || Creates a quiz with an initial name.&amp;lt;br&amp;gt;Sends a PUT request with updated name.&amp;lt;br&amp;gt;Expects 200 OK and checks that the name is updated in the response.&lt;br /&gt;
|-&lt;br /&gt;
| fetch_quiz || GET || /api/v1/student_quizzes/:id || #fetch_quiz || Fetch quiz using fetch_quiz method || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request using fetch_quiz logic.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name in response.&lt;br /&gt;
|-&lt;br /&gt;
| render_success || GET || /api/v1/student_quizzes || #render_success || Render success response || Calls an action that invokes render_success (index).&amp;lt;br&amp;gt;Sends a GET request and expects 200 OK response.&lt;br /&gt;
|-&lt;br /&gt;
| create_questionnaire || POST || /api/v1/student_quizzes || #create_questionnaire || Create questionnaire || Sends a POST request with questionnaire attributes.&amp;lt;br&amp;gt;Checks if the questionnaire is created and returns 201 Created.&amp;lt;br&amp;gt;Validates the name in the response.&lt;br /&gt;
|-&lt;br /&gt;
| questionnaire_params || N/A || questionnaire_params(params) || #questionnaire_params || Extract strong parameters || Validates strong parameter extraction logic.&amp;lt;br&amp;gt;Handles valid, missing, and invalid parameters.&amp;lt;br&amp;gt;Raises ActionController::ParameterMissing when necessary.&lt;br /&gt;
|-&lt;br /&gt;
| create_questions_and_answers || N/A || create_questions_and_answers(questionnaire, questions_attributes) || #create_questions_and_answers || Build nested resources || Creates a questionnaire object.&amp;lt;br&amp;gt;Calls the method with nested question and answer attributes.&amp;lt;br&amp;gt;Validates that questions and answers are successfully saved.&lt;br /&gt;
|-&lt;br /&gt;
| render_error || N/A || render_error(message, status) || #render_error || Render error response || Triggers an action with invalid parameters.&amp;lt;br&amp;gt;Checks that 422 Unprocessable Entity is returned.&amp;lt;br&amp;gt;Validates the presence of an error message in the response.&lt;br /&gt;
|-&lt;br /&gt;
| quiz_assigned? || N/A || quiz_assigned?(participant, quiz) || #quiz_assigned? || Check quiz assignment || Checks if a quiz has already been assigned to a participant.&amp;lt;br&amp;gt;Returns true or false based on assignment existence and input validity.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || N/A || build_response_map(quiz, participant) || #build_response_map || Create response map || Creates a quiz and participant.&amp;lt;br&amp;gt;Builds a ResponseMap and checks attributes.&amp;lt;br&amp;gt;Returns nil when quiz or participant is missing.&lt;br /&gt;
|-&lt;br /&gt;
| check_instructor_role || N/A || check_instructor_role || #check_instructor_role || Role verification || Checks if current user is an instructor.&amp;lt;br&amp;gt;Returns true if user has instructor role, false otherwise.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;resource_map_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| calculate_score || calculate_score || #calculate_score || Calculate the score based on associated responses. || Mocks a response with a known score.&amp;lt;br&amp;gt;Calls `calculate_score` on the response_map.&amp;lt;br&amp;gt;Expects the calculated score to match.&lt;br /&gt;
|-&lt;br /&gt;
| find_or_initialize_response || find_or_initialize_response(questionnaire) || #find_or_initialize_response || Retrieve an existing response or initialize a new one for the given questionnaire. || Checks if a response already exists and returns it.&amp;lt;br&amp;gt;If none exists, it initializes a new response with the questionnaire.&lt;br /&gt;
|-&lt;br /&gt;
| get_score || get_score(questionnaire) || #get_score || Fetch the score from a response for a specific questionnaire. || Mocks a score and ensures `get_score` returns it.&amp;lt;br&amp;gt;Returns nil if there is no associated response.&lt;br /&gt;
|-&lt;br /&gt;
| process_answers || process_answers || #process_answers || Process answers in the associated response and update state. || Mocks success and failure conditions of answer processing.&amp;lt;br&amp;gt;Returns true or false accordingly.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || build_response_map(questionnaire, participant) || #build_response_map || Create and return a response map for a given questionnaire and participant. || Creates and returns a valid ResponseMap object.&amp;lt;br&amp;gt;Returns nil if either questionnaire or participant is not valid.&lt;br /&gt;
|-&lt;br /&gt;
| find_for_current_user || find_for_current_user(user) || #find_for_current_user || Locate the response map associated with the current user. || Returns the existing response map if present.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Beside those, we test &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; that can be found in &amp;lt;code&amp;gt; find_resource_service_spec.rb&amp;lt;/code&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==Next Steps==&lt;br /&gt;
&lt;br /&gt;
* We will implement the feature that lets students skip quiz questions, including updating the rubric model and UI to support it properly.&lt;br /&gt;
&lt;br /&gt;
* We plan to move score calculation logic from the response map to the response object to better support cases where users take the quiz multiple times.&lt;br /&gt;
&lt;br /&gt;
* The current test coverage is 69.72%; we are confident of increasing the test coverage by implementing all of our test plans.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164720</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164720"/>
		<updated>2025-04-22T19:39:43Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;6) Added Skippable Question Logic to Quizzes&amp;lt;b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/1e22637ed9ab350ac1e75ef5607ea8d4bdd7edd2) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
As outlined above, we refactored three key files. To ensure comprehensive coverage and maintain system reliability, we have implemented thorough testing for all affected components. This includes both unit and request tests, which are written using RSpec. The test coverage has been added to the following files:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/find_resource_service_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/services/api/v1/resource_map_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
After the setup and authentication, we test each refactored file method. Below is an example of testing &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt; Method in &amp;lt;code&amp;gt;StudentQuizzesController&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.&lt;br /&gt;
&lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
                              min_question_score: 0, max_question_score: 10)&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below are the complete tables that list our full test plans.&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! HTTP Method !! Endpoint/ Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| index || GET || /api/v1/student_quizzes || #index || Fetch all quizzes || Creates a sample quiz.&amp;lt;br&amp;gt;Sends a GET request to fetch quizzes.&amp;lt;br&amp;gt;Expects 200 OK and validates the response is an array.&lt;br /&gt;
|-&lt;br /&gt;
| create || POST || /api/v1/student_quizzes || #create || Create a new quiz || Prepares quiz parameters with nested questions and answers.&amp;lt;br&amp;gt;Sends a POST request to create a new quiz.&amp;lt;br&amp;gt;Expects 201 Created and checks for 'name' and 'questions' in response.&lt;br /&gt;
|-&lt;br /&gt;
| destroy || DELETE || /api/v1/student_quizzes/:id || #destroy || Delete a quiz || Creates a quiz record.&amp;lt;br&amp;gt;Sends a DELETE request to remove the quiz.&amp;lt;br&amp;gt;Expects 204 No Content and confirms quiz is deleted from the database.&lt;br /&gt;
|-&lt;br /&gt;
| show || GET || /api/v1/student_quizzes/:id || #show || Show quiz details || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request to fetch the quiz by ID.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name and ID in the response.&lt;br /&gt;
|-&lt;br /&gt;
| assign_quiz || POST || /api/v1/student_quizzes/assign || #assign_quiz || Assign quiz to a participant || Assigns a quiz to a Participant model instance.&amp;lt;br&amp;gt;Sends a POST request to the assign endpoint.&amp;lt;br&amp;gt;Verifies 201 Created status.&lt;br /&gt;
|-&lt;br /&gt;
| submit_quiz || POST || /api/v1/student_quizzes/submit_answers || #submit_quiz || Submit answers || Creates a quiz and a corresponding response map.&amp;lt;br&amp;gt;Sends quiz answer submission via POST request.&amp;lt;br&amp;gt;Checks for 200 OK and validates 'total_score' is present in response.&lt;br /&gt;
|-&lt;br /&gt;
| update || PUT || /api/v1/student_quizzes/:id || #update || Update quiz || Creates a quiz with an initial name.&amp;lt;br&amp;gt;Sends a PUT request with updated name.&amp;lt;br&amp;gt;Expects 200 OK and checks that the name is updated in the response.&lt;br /&gt;
|-&lt;br /&gt;
| fetch_quiz || GET || /api/v1/student_quizzes/:id || #fetch_quiz || Fetch quiz using fetch_quiz method || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request using fetch_quiz logic.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name in response.&lt;br /&gt;
|-&lt;br /&gt;
| render_success || GET || /api/v1/student_quizzes || #render_success || Render success response || Calls an action that invokes render_success (index).&amp;lt;br&amp;gt;Sends a GET request and expects 200 OK response.&lt;br /&gt;
|-&lt;br /&gt;
| create_questionnaire || POST || /api/v1/student_quizzes || #create_questionnaire || Create questionnaire || Sends a POST request with questionnaire attributes.&amp;lt;br&amp;gt;Checks if the questionnaire is created and returns 201 Created.&amp;lt;br&amp;gt;Validates the name in the response.&lt;br /&gt;
|-&lt;br /&gt;
| questionnaire_params || N/A || questionnaire_params(params) || #questionnaire_params || Extract strong parameters || Validates strong parameter extraction logic.&amp;lt;br&amp;gt;Handles valid, missing, and invalid parameters.&amp;lt;br&amp;gt;Raises ActionController::ParameterMissing when necessary.&lt;br /&gt;
|-&lt;br /&gt;
| create_questions_and_answers || N/A || create_questions_and_answers(questionnaire, questions_attributes) || #create_questions_and_answers || Build nested resources || Creates a questionnaire object.&amp;lt;br&amp;gt;Calls the method with nested question and answer attributes.&amp;lt;br&amp;gt;Validates that questions and answers are successfully saved.&lt;br /&gt;
|-&lt;br /&gt;
| render_error || N/A || render_error(message, status) || #render_error || Render error response || Triggers an action with invalid parameters.&amp;lt;br&amp;gt;Checks that 422 Unprocessable Entity is returned.&amp;lt;br&amp;gt;Validates the presence of an error message in the response.&lt;br /&gt;
|-&lt;br /&gt;
| quiz_assigned? || N/A || quiz_assigned?(participant, quiz) || #quiz_assigned? || Check quiz assignment || Checks if a quiz has already been assigned to a participant.&amp;lt;br&amp;gt;Returns true or false based on assignment existence and input validity.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || N/A || build_response_map(quiz, participant) || #build_response_map || Create response map || Creates a quiz and participant.&amp;lt;br&amp;gt;Builds a ResponseMap and checks attributes.&amp;lt;br&amp;gt;Returns nil when quiz or participant is missing.&lt;br /&gt;
|-&lt;br /&gt;
| check_instructor_role || N/A || check_instructor_role || #check_instructor_role || Role verification || Checks if current user is an instructor.&amp;lt;br&amp;gt;Returns true if user has instructor role, false otherwise.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;resource_map_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| calculate_score || calculate_score || #calculate_score || Calculate the score based on associated responses. || Mocks a response with a known score.&amp;lt;br&amp;gt;Calls `calculate_score` on the response_map.&amp;lt;br&amp;gt;Expects the calculated score to match.&lt;br /&gt;
|-&lt;br /&gt;
| find_or_initialize_response || find_or_initialize_response(questionnaire) || #find_or_initialize_response || Retrieve an existing response or initialize a new one for the given questionnaire. || Checks if a response already exists and returns it.&amp;lt;br&amp;gt;If none exists, it initializes a new response with the questionnaire.&lt;br /&gt;
|-&lt;br /&gt;
| get_score || get_score(questionnaire) || #get_score || Fetch the score from a response for a specific questionnaire. || Mocks a score and ensures `get_score` returns it.&amp;lt;br&amp;gt;Returns nil if there is no associated response.&lt;br /&gt;
|-&lt;br /&gt;
| process_answers || process_answers || #process_answers || Process answers in the associated response and update state. || Mocks success and failure conditions of answer processing.&amp;lt;br&amp;gt;Returns true or false accordingly.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || build_response_map(questionnaire, participant) || #build_response_map || Create and return a response map for a given questionnaire and participant. || Creates and returns a valid ResponseMap object.&amp;lt;br&amp;gt;Returns nil if either questionnaire or participant is not valid.&lt;br /&gt;
|-&lt;br /&gt;
| find_for_current_user || find_for_current_user(user) || #find_for_current_user || Locate the response map associated with the current user. || Returns the existing response map if present.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Beside those, we test &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; that can be found in &amp;lt;code&amp;gt; find_resource_service_spec.rb&amp;lt;/code&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==Next Steps==&lt;br /&gt;
&lt;br /&gt;
* We will implement the feature that lets students skip quiz questions, including updating the rubric model and UI to support it properly.&lt;br /&gt;
&lt;br /&gt;
* We plan to move score calculation logic from the response map to the response object to better support cases where users take the quiz multiple times.&lt;br /&gt;
&lt;br /&gt;
* The current test coverage is 69.72%; we are confident of increasing the test coverage by implementing all of our test plans.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164719</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164719"/>
		<updated>2025-04-22T19:39:13Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Added Skippable Question Logic to Quizzes&amp;lt;b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/1e22637ed9ab350ac1e75ef5607ea8d4bdd7edd2) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
As outlined above, we refactored three key files. To ensure comprehensive coverage and maintain system reliability, we have implemented thorough testing for all affected components. This includes both unit and request tests, which are written using RSpec. The test coverage has been added to the following files:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/find_resource_service_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/services/api/v1/resource_map_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
After the setup and authentication, we test each refactored file method. Below is an example of testing &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt; Method in &amp;lt;code&amp;gt;StudentQuizzesController&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.&lt;br /&gt;
&lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
                              min_question_score: 0, max_question_score: 10)&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below are the complete tables that list our full test plans.&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! HTTP Method !! Endpoint/ Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| index || GET || /api/v1/student_quizzes || #index || Fetch all quizzes || Creates a sample quiz.&amp;lt;br&amp;gt;Sends a GET request to fetch quizzes.&amp;lt;br&amp;gt;Expects 200 OK and validates the response is an array.&lt;br /&gt;
|-&lt;br /&gt;
| create || POST || /api/v1/student_quizzes || #create || Create a new quiz || Prepares quiz parameters with nested questions and answers.&amp;lt;br&amp;gt;Sends a POST request to create a new quiz.&amp;lt;br&amp;gt;Expects 201 Created and checks for 'name' and 'questions' in response.&lt;br /&gt;
|-&lt;br /&gt;
| destroy || DELETE || /api/v1/student_quizzes/:id || #destroy || Delete a quiz || Creates a quiz record.&amp;lt;br&amp;gt;Sends a DELETE request to remove the quiz.&amp;lt;br&amp;gt;Expects 204 No Content and confirms quiz is deleted from the database.&lt;br /&gt;
|-&lt;br /&gt;
| show || GET || /api/v1/student_quizzes/:id || #show || Show quiz details || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request to fetch the quiz by ID.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name and ID in the response.&lt;br /&gt;
|-&lt;br /&gt;
| assign_quiz || POST || /api/v1/student_quizzes/assign || #assign_quiz || Assign quiz to a participant || Assigns a quiz to a Participant model instance.&amp;lt;br&amp;gt;Sends a POST request to the assign endpoint.&amp;lt;br&amp;gt;Verifies 201 Created status.&lt;br /&gt;
|-&lt;br /&gt;
| submit_quiz || POST || /api/v1/student_quizzes/submit_answers || #submit_quiz || Submit answers || Creates a quiz and a corresponding response map.&amp;lt;br&amp;gt;Sends quiz answer submission via POST request.&amp;lt;br&amp;gt;Checks for 200 OK and validates 'total_score' is present in response.&lt;br /&gt;
|-&lt;br /&gt;
| update || PUT || /api/v1/student_quizzes/:id || #update || Update quiz || Creates a quiz with an initial name.&amp;lt;br&amp;gt;Sends a PUT request with updated name.&amp;lt;br&amp;gt;Expects 200 OK and checks that the name is updated in the response.&lt;br /&gt;
|-&lt;br /&gt;
| fetch_quiz || GET || /api/v1/student_quizzes/:id || #fetch_quiz || Fetch quiz using fetch_quiz method || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request using fetch_quiz logic.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name in response.&lt;br /&gt;
|-&lt;br /&gt;
| render_success || GET || /api/v1/student_quizzes || #render_success || Render success response || Calls an action that invokes render_success (index).&amp;lt;br&amp;gt;Sends a GET request and expects 200 OK response.&lt;br /&gt;
|-&lt;br /&gt;
| create_questionnaire || POST || /api/v1/student_quizzes || #create_questionnaire || Create questionnaire || Sends a POST request with questionnaire attributes.&amp;lt;br&amp;gt;Checks if the questionnaire is created and returns 201 Created.&amp;lt;br&amp;gt;Validates the name in the response.&lt;br /&gt;
|-&lt;br /&gt;
| questionnaire_params || N/A || questionnaire_params(params) || #questionnaire_params || Extract strong parameters || Validates strong parameter extraction logic.&amp;lt;br&amp;gt;Handles valid, missing, and invalid parameters.&amp;lt;br&amp;gt;Raises ActionController::ParameterMissing when necessary.&lt;br /&gt;
|-&lt;br /&gt;
| create_questions_and_answers || N/A || create_questions_and_answers(questionnaire, questions_attributes) || #create_questions_and_answers || Build nested resources || Creates a questionnaire object.&amp;lt;br&amp;gt;Calls the method with nested question and answer attributes.&amp;lt;br&amp;gt;Validates that questions and answers are successfully saved.&lt;br /&gt;
|-&lt;br /&gt;
| render_error || N/A || render_error(message, status) || #render_error || Render error response || Triggers an action with invalid parameters.&amp;lt;br&amp;gt;Checks that 422 Unprocessable Entity is returned.&amp;lt;br&amp;gt;Validates the presence of an error message in the response.&lt;br /&gt;
|-&lt;br /&gt;
| quiz_assigned? || N/A || quiz_assigned?(participant, quiz) || #quiz_assigned? || Check quiz assignment || Checks if a quiz has already been assigned to a participant.&amp;lt;br&amp;gt;Returns true or false based on assignment existence and input validity.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || N/A || build_response_map(quiz, participant) || #build_response_map || Create response map || Creates a quiz and participant.&amp;lt;br&amp;gt;Builds a ResponseMap and checks attributes.&amp;lt;br&amp;gt;Returns nil when quiz or participant is missing.&lt;br /&gt;
|-&lt;br /&gt;
| check_instructor_role || N/A || check_instructor_role || #check_instructor_role || Role verification || Checks if current user is an instructor.&amp;lt;br&amp;gt;Returns true if user has instructor role, false otherwise.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;resource_map_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| calculate_score || calculate_score || #calculate_score || Calculate the score based on associated responses. || Mocks a response with a known score.&amp;lt;br&amp;gt;Calls `calculate_score` on the response_map.&amp;lt;br&amp;gt;Expects the calculated score to match.&lt;br /&gt;
|-&lt;br /&gt;
| find_or_initialize_response || find_or_initialize_response(questionnaire) || #find_or_initialize_response || Retrieve an existing response or initialize a new one for the given questionnaire. || Checks if a response already exists and returns it.&amp;lt;br&amp;gt;If none exists, it initializes a new response with the questionnaire.&lt;br /&gt;
|-&lt;br /&gt;
| get_score || get_score(questionnaire) || #get_score || Fetch the score from a response for a specific questionnaire. || Mocks a score and ensures `get_score` returns it.&amp;lt;br&amp;gt;Returns nil if there is no associated response.&lt;br /&gt;
|-&lt;br /&gt;
| process_answers || process_answers || #process_answers || Process answers in the associated response and update state. || Mocks success and failure conditions of answer processing.&amp;lt;br&amp;gt;Returns true or false accordingly.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || build_response_map(questionnaire, participant) || #build_response_map || Create and return a response map for a given questionnaire and participant. || Creates and returns a valid ResponseMap object.&amp;lt;br&amp;gt;Returns nil if either questionnaire or participant is not valid.&lt;br /&gt;
|-&lt;br /&gt;
| find_for_current_user || find_for_current_user(user) || #find_for_current_user || Locate the response map associated with the current user. || Returns the existing response map if present.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Beside those, we test &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; that can be found in &amp;lt;code&amp;gt; find_resource_service_spec.rb&amp;lt;/code&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==Next Steps==&lt;br /&gt;
&lt;br /&gt;
* We will implement the feature that lets students skip quiz questions, including updating the rubric model and UI to support it properly.&lt;br /&gt;
&lt;br /&gt;
* We plan to move score calculation logic from the response map to the response object to better support cases where users take the quiz multiple times.&lt;br /&gt;
&lt;br /&gt;
* The current test coverage is 69.72%; we are confident of increasing the test coverage by implementing all of our test plans.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quiz_13.jpeg&amp;diff=164718</id>
		<title>File:Student quiz 13.jpeg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quiz_13.jpeg&amp;diff=164718"/>
		<updated>2025-04-22T19:38:17Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quiz_13.png&amp;diff=164717</id>
		<title>File:Student quiz 13.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quiz_13.png&amp;diff=164717"/>
		<updated>2025-04-22T19:31:55Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quiz_14.jpeg&amp;diff=164716</id>
		<title>File:Student quiz 14.jpeg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quiz_14.jpeg&amp;diff=164716"/>
		<updated>2025-04-22T19:31:07Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164715</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164715"/>
		<updated>2025-04-22T19:30:38Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Added Skippable Question Logic to Quizzes&amp;lt;b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''db/migrate/20250421052650_add_skippable_to_items.rb'''  &lt;br /&gt;
    [[File:student_quiz_13.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz_14.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/1e22637ed9ab350ac1e75ef5607ea8d4bdd7edd2) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
As outlined above, we refactored three key files. To ensure comprehensive coverage and maintain system reliability, we have implemented thorough testing for all affected components. This includes both unit and request tests, which are written using RSpec. The test coverage has been added to the following files:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/find_resource_service_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/services/api/v1/resource_map_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
After the setup and authentication, we test each refactored file method. Below is an example of testing &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt; Method in &amp;lt;code&amp;gt;StudentQuizzesController&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.&lt;br /&gt;
&lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
                              min_question_score: 0, max_question_score: 10)&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below are the complete tables that list our full test plans.&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! HTTP Method !! Endpoint/ Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| index || GET || /api/v1/student_quizzes || #index || Fetch all quizzes || Creates a sample quiz.&amp;lt;br&amp;gt;Sends a GET request to fetch quizzes.&amp;lt;br&amp;gt;Expects 200 OK and validates the response is an array.&lt;br /&gt;
|-&lt;br /&gt;
| create || POST || /api/v1/student_quizzes || #create || Create a new quiz || Prepares quiz parameters with nested questions and answers.&amp;lt;br&amp;gt;Sends a POST request to create a new quiz.&amp;lt;br&amp;gt;Expects 201 Created and checks for 'name' and 'questions' in response.&lt;br /&gt;
|-&lt;br /&gt;
| destroy || DELETE || /api/v1/student_quizzes/:id || #destroy || Delete a quiz || Creates a quiz record.&amp;lt;br&amp;gt;Sends a DELETE request to remove the quiz.&amp;lt;br&amp;gt;Expects 204 No Content and confirms quiz is deleted from the database.&lt;br /&gt;
|-&lt;br /&gt;
| show || GET || /api/v1/student_quizzes/:id || #show || Show quiz details || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request to fetch the quiz by ID.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name and ID in the response.&lt;br /&gt;
|-&lt;br /&gt;
| assign_quiz || POST || /api/v1/student_quizzes/assign || #assign_quiz || Assign quiz to a participant || Assigns a quiz to a Participant model instance.&amp;lt;br&amp;gt;Sends a POST request to the assign endpoint.&amp;lt;br&amp;gt;Verifies 201 Created status.&lt;br /&gt;
|-&lt;br /&gt;
| submit_quiz || POST || /api/v1/student_quizzes/submit_answers || #submit_quiz || Submit answers || Creates a quiz and a corresponding response map.&amp;lt;br&amp;gt;Sends quiz answer submission via POST request.&amp;lt;br&amp;gt;Checks for 200 OK and validates 'total_score' is present in response.&lt;br /&gt;
|-&lt;br /&gt;
| update || PUT || /api/v1/student_quizzes/:id || #update || Update quiz || Creates a quiz with an initial name.&amp;lt;br&amp;gt;Sends a PUT request with updated name.&amp;lt;br&amp;gt;Expects 200 OK and checks that the name is updated in the response.&lt;br /&gt;
|-&lt;br /&gt;
| fetch_quiz || GET || /api/v1/student_quizzes/:id || #fetch_quiz || Fetch quiz using fetch_quiz method || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request using fetch_quiz logic.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name in response.&lt;br /&gt;
|-&lt;br /&gt;
| render_success || GET || /api/v1/student_quizzes || #render_success || Render success response || Calls an action that invokes render_success (index).&amp;lt;br&amp;gt;Sends a GET request and expects 200 OK response.&lt;br /&gt;
|-&lt;br /&gt;
| create_questionnaire || POST || /api/v1/student_quizzes || #create_questionnaire || Create questionnaire || Sends a POST request with questionnaire attributes.&amp;lt;br&amp;gt;Checks if the questionnaire is created and returns 201 Created.&amp;lt;br&amp;gt;Validates the name in the response.&lt;br /&gt;
|-&lt;br /&gt;
| questionnaire_params || N/A || questionnaire_params(params) || #questionnaire_params || Extract strong parameters || Validates strong parameter extraction logic.&amp;lt;br&amp;gt;Handles valid, missing, and invalid parameters.&amp;lt;br&amp;gt;Raises ActionController::ParameterMissing when necessary.&lt;br /&gt;
|-&lt;br /&gt;
| create_questions_and_answers || N/A || create_questions_and_answers(questionnaire, questions_attributes) || #create_questions_and_answers || Build nested resources || Creates a questionnaire object.&amp;lt;br&amp;gt;Calls the method with nested question and answer attributes.&amp;lt;br&amp;gt;Validates that questions and answers are successfully saved.&lt;br /&gt;
|-&lt;br /&gt;
| render_error || N/A || render_error(message, status) || #render_error || Render error response || Triggers an action with invalid parameters.&amp;lt;br&amp;gt;Checks that 422 Unprocessable Entity is returned.&amp;lt;br&amp;gt;Validates the presence of an error message in the response.&lt;br /&gt;
|-&lt;br /&gt;
| quiz_assigned? || N/A || quiz_assigned?(participant, quiz) || #quiz_assigned? || Check quiz assignment || Checks if a quiz has already been assigned to a participant.&amp;lt;br&amp;gt;Returns true or false based on assignment existence and input validity.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || N/A || build_response_map(quiz, participant) || #build_response_map || Create response map || Creates a quiz and participant.&amp;lt;br&amp;gt;Builds a ResponseMap and checks attributes.&amp;lt;br&amp;gt;Returns nil when quiz or participant is missing.&lt;br /&gt;
|-&lt;br /&gt;
| check_instructor_role || N/A || check_instructor_role || #check_instructor_role || Role verification || Checks if current user is an instructor.&amp;lt;br&amp;gt;Returns true if user has instructor role, false otherwise.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;resource_map_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| calculate_score || calculate_score || #calculate_score || Calculate the score based on associated responses. || Mocks a response with a known score.&amp;lt;br&amp;gt;Calls `calculate_score` on the response_map.&amp;lt;br&amp;gt;Expects the calculated score to match.&lt;br /&gt;
|-&lt;br /&gt;
| find_or_initialize_response || find_or_initialize_response(questionnaire) || #find_or_initialize_response || Retrieve an existing response or initialize a new one for the given questionnaire. || Checks if a response already exists and returns it.&amp;lt;br&amp;gt;If none exists, it initializes a new response with the questionnaire.&lt;br /&gt;
|-&lt;br /&gt;
| get_score || get_score(questionnaire) || #get_score || Fetch the score from a response for a specific questionnaire. || Mocks a score and ensures `get_score` returns it.&amp;lt;br&amp;gt;Returns nil if there is no associated response.&lt;br /&gt;
|-&lt;br /&gt;
| process_answers || process_answers || #process_answers || Process answers in the associated response and update state. || Mocks success and failure conditions of answer processing.&amp;lt;br&amp;gt;Returns true or false accordingly.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || build_response_map(questionnaire, participant) || #build_response_map || Create and return a response map for a given questionnaire and participant. || Creates and returns a valid ResponseMap object.&amp;lt;br&amp;gt;Returns nil if either questionnaire or participant is not valid.&lt;br /&gt;
|-&lt;br /&gt;
| find_for_current_user || find_for_current_user(user) || #find_for_current_user || Locate the response map associated with the current user. || Returns the existing response map if present.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Beside those, we test &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; that can be found in &amp;lt;code&amp;gt; find_resource_service_spec.rb&amp;lt;/code&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==Next Steps==&lt;br /&gt;
&lt;br /&gt;
* We will implement the feature that lets students skip quiz questions, including updating the rubric model and UI to support it properly.&lt;br /&gt;
&lt;br /&gt;
* We plan to move score calculation logic from the response map to the response object to better support cases where users take the quiz multiple times.&lt;br /&gt;
&lt;br /&gt;
* The current test coverage is 69.72%; we are confident of increasing the test coverage by implementing all of our test plans.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164714</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164714"/>
		<updated>2025-04-22T19:25:17Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.jpeg|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.jpeg|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/1e22637ed9ab350ac1e75ef5607ea8d4bdd7edd2) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
As outlined above, we refactored three key files. To ensure comprehensive coverage and maintain system reliability, we have implemented thorough testing for all affected components. This includes both unit and request tests, which are written using RSpec. The test coverage has been added to the following files:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/find_resource_service_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/services/api/v1/resource_map_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
After the setup and authentication, we test each refactored file method. Below is an example of testing &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt; Method in &amp;lt;code&amp;gt;StudentQuizzesController&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.&lt;br /&gt;
&lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
                              min_question_score: 0, max_question_score: 10)&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below are the complete tables that list our full test plans.&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! HTTP Method !! Endpoint/ Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| index || GET || /api/v1/student_quizzes || #index || Fetch all quizzes || Creates a sample quiz.&amp;lt;br&amp;gt;Sends a GET request to fetch quizzes.&amp;lt;br&amp;gt;Expects 200 OK and validates the response is an array.&lt;br /&gt;
|-&lt;br /&gt;
| create || POST || /api/v1/student_quizzes || #create || Create a new quiz || Prepares quiz parameters with nested questions and answers.&amp;lt;br&amp;gt;Sends a POST request to create a new quiz.&amp;lt;br&amp;gt;Expects 201 Created and checks for 'name' and 'questions' in response.&lt;br /&gt;
|-&lt;br /&gt;
| destroy || DELETE || /api/v1/student_quizzes/:id || #destroy || Delete a quiz || Creates a quiz record.&amp;lt;br&amp;gt;Sends a DELETE request to remove the quiz.&amp;lt;br&amp;gt;Expects 204 No Content and confirms quiz is deleted from the database.&lt;br /&gt;
|-&lt;br /&gt;
| show || GET || /api/v1/student_quizzes/:id || #show || Show quiz details || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request to fetch the quiz by ID.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name and ID in the response.&lt;br /&gt;
|-&lt;br /&gt;
| assign_quiz || POST || /api/v1/student_quizzes/assign || #assign_quiz || Assign quiz to a participant || Assigns a quiz to a Participant model instance.&amp;lt;br&amp;gt;Sends a POST request to the assign endpoint.&amp;lt;br&amp;gt;Verifies 201 Created status.&lt;br /&gt;
|-&lt;br /&gt;
| submit_quiz || POST || /api/v1/student_quizzes/submit_answers || #submit_quiz || Submit answers || Creates a quiz and a corresponding response map.&amp;lt;br&amp;gt;Sends quiz answer submission via POST request.&amp;lt;br&amp;gt;Checks for 200 OK and validates 'total_score' is present in response.&lt;br /&gt;
|-&lt;br /&gt;
| update || PUT || /api/v1/student_quizzes/:id || #update || Update quiz || Creates a quiz with an initial name.&amp;lt;br&amp;gt;Sends a PUT request with updated name.&amp;lt;br&amp;gt;Expects 200 OK and checks that the name is updated in the response.&lt;br /&gt;
|-&lt;br /&gt;
| fetch_quiz || GET || /api/v1/student_quizzes/:id || #fetch_quiz || Fetch quiz using fetch_quiz method || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request using fetch_quiz logic.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name in response.&lt;br /&gt;
|-&lt;br /&gt;
| render_success || GET || /api/v1/student_quizzes || #render_success || Render success response || Calls an action that invokes render_success (index).&amp;lt;br&amp;gt;Sends a GET request and expects 200 OK response.&lt;br /&gt;
|-&lt;br /&gt;
| create_questionnaire || POST || /api/v1/student_quizzes || #create_questionnaire || Create questionnaire || Sends a POST request with questionnaire attributes.&amp;lt;br&amp;gt;Checks if the questionnaire is created and returns 201 Created.&amp;lt;br&amp;gt;Validates the name in the response.&lt;br /&gt;
|-&lt;br /&gt;
| questionnaire_params || N/A || questionnaire_params(params) || #questionnaire_params || Extract strong parameters || Validates strong parameter extraction logic.&amp;lt;br&amp;gt;Handles valid, missing, and invalid parameters.&amp;lt;br&amp;gt;Raises ActionController::ParameterMissing when necessary.&lt;br /&gt;
|-&lt;br /&gt;
| create_questions_and_answers || N/A || create_questions_and_answers(questionnaire, questions_attributes) || #create_questions_and_answers || Build nested resources || Creates a questionnaire object.&amp;lt;br&amp;gt;Calls the method with nested question and answer attributes.&amp;lt;br&amp;gt;Validates that questions and answers are successfully saved.&lt;br /&gt;
|-&lt;br /&gt;
| render_error || N/A || render_error(message, status) || #render_error || Render error response || Triggers an action with invalid parameters.&amp;lt;br&amp;gt;Checks that 422 Unprocessable Entity is returned.&amp;lt;br&amp;gt;Validates the presence of an error message in the response.&lt;br /&gt;
|-&lt;br /&gt;
| quiz_assigned? || N/A || quiz_assigned?(participant, quiz) || #quiz_assigned? || Check quiz assignment || Checks if a quiz has already been assigned to a participant.&amp;lt;br&amp;gt;Returns true or false based on assignment existence and input validity.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || N/A || build_response_map(quiz, participant) || #build_response_map || Create response map || Creates a quiz and participant.&amp;lt;br&amp;gt;Builds a ResponseMap and checks attributes.&amp;lt;br&amp;gt;Returns nil when quiz or participant is missing.&lt;br /&gt;
|-&lt;br /&gt;
| check_instructor_role || N/A || check_instructor_role || #check_instructor_role || Role verification || Checks if current user is an instructor.&amp;lt;br&amp;gt;Returns true if user has instructor role, false otherwise.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;resource_map_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| calculate_score || calculate_score || #calculate_score || Calculate the score based on associated responses. || Mocks a response with a known score.&amp;lt;br&amp;gt;Calls `calculate_score` on the response_map.&amp;lt;br&amp;gt;Expects the calculated score to match.&lt;br /&gt;
|-&lt;br /&gt;
| find_or_initialize_response || find_or_initialize_response(questionnaire) || #find_or_initialize_response || Retrieve an existing response or initialize a new one for the given questionnaire. || Checks if a response already exists and returns it.&amp;lt;br&amp;gt;If none exists, it initializes a new response with the questionnaire.&lt;br /&gt;
|-&lt;br /&gt;
| get_score || get_score(questionnaire) || #get_score || Fetch the score from a response for a specific questionnaire. || Mocks a score and ensures `get_score` returns it.&amp;lt;br&amp;gt;Returns nil if there is no associated response.&lt;br /&gt;
|-&lt;br /&gt;
| process_answers || process_answers || #process_answers || Process answers in the associated response and update state. || Mocks success and failure conditions of answer processing.&amp;lt;br&amp;gt;Returns true or false accordingly.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || build_response_map(questionnaire, participant) || #build_response_map || Create and return a response map for a given questionnaire and participant. || Creates and returns a valid ResponseMap object.&amp;lt;br&amp;gt;Returns nil if either questionnaire or participant is not valid.&lt;br /&gt;
|-&lt;br /&gt;
| find_for_current_user || find_for_current_user(user) || #find_for_current_user || Locate the response map associated with the current user. || Returns the existing response map if present.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Beside those, we test &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; that can be found in &amp;lt;code&amp;gt; find_resource_service_spec.rb&amp;lt;/code&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==Next Steps==&lt;br /&gt;
&lt;br /&gt;
* We will implement the feature that lets students skip quiz questions, including updating the rubric model and UI to support it properly.&lt;br /&gt;
&lt;br /&gt;
* We plan to move score calculation logic from the response map to the response object to better support cases where users take the quiz multiple times.&lt;br /&gt;
&lt;br /&gt;
* The current test coverage is 69.72%; we are confident of increasing the test coverage by implementing all of our test plans.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quiz_12.jpeg&amp;diff=164713</id>
		<title>File:Student quiz 12.jpeg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quiz_12.jpeg&amp;diff=164713"/>
		<updated>2025-04-22T19:22:37Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quiz_11.jpeg&amp;diff=164712</id>
		<title>File:Student quiz 11.jpeg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quiz_11.jpeg&amp;diff=164712"/>
		<updated>2025-04-22T19:21:31Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164711</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164711"/>
		<updated>2025-04-22T19:17:57Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;b&amp;gt;5) Moved Score Calculation from ResponseMap to ResponseObject&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response.rb'''  &lt;br /&gt;
    [[File:student_quiz_11.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz_12.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method to calculate quiz scores was previously placed in `ResponseMap`, which violates SRP and tightly couples quiz logic to mapping logic. This has now been moved to the `Response` model, which represents student answers and is the appropriate layer to handle score calculation. This also improves future support for multiple quiz attempts and better aligns with the system's domain model.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/1e22637ed9ab350ac1e75ef5607ea8d4bdd7edd2) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
As outlined above, we refactored three key files. To ensure comprehensive coverage and maintain system reliability, we have implemented thorough testing for all affected components. This includes both unit and request tests, which are written using RSpec. The test coverage has been added to the following files:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/find_resource_service_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/services/api/v1/resource_map_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
After the setup and authentication, we test each refactored file method. Below is an example of testing &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt; Method in &amp;lt;code&amp;gt;StudentQuizzesController&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.&lt;br /&gt;
&lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
                              min_question_score: 0, max_question_score: 10)&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below are the complete tables that list our full test plans.&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! HTTP Method !! Endpoint/ Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| index || GET || /api/v1/student_quizzes || #index || Fetch all quizzes || Creates a sample quiz.&amp;lt;br&amp;gt;Sends a GET request to fetch quizzes.&amp;lt;br&amp;gt;Expects 200 OK and validates the response is an array.&lt;br /&gt;
|-&lt;br /&gt;
| create || POST || /api/v1/student_quizzes || #create || Create a new quiz || Prepares quiz parameters with nested questions and answers.&amp;lt;br&amp;gt;Sends a POST request to create a new quiz.&amp;lt;br&amp;gt;Expects 201 Created and checks for 'name' and 'questions' in response.&lt;br /&gt;
|-&lt;br /&gt;
| destroy || DELETE || /api/v1/student_quizzes/:id || #destroy || Delete a quiz || Creates a quiz record.&amp;lt;br&amp;gt;Sends a DELETE request to remove the quiz.&amp;lt;br&amp;gt;Expects 204 No Content and confirms quiz is deleted from the database.&lt;br /&gt;
|-&lt;br /&gt;
| show || GET || /api/v1/student_quizzes/:id || #show || Show quiz details || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request to fetch the quiz by ID.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name and ID in the response.&lt;br /&gt;
|-&lt;br /&gt;
| assign_quiz || POST || /api/v1/student_quizzes/assign || #assign_quiz || Assign quiz to a participant || Assigns a quiz to a Participant model instance.&amp;lt;br&amp;gt;Sends a POST request to the assign endpoint.&amp;lt;br&amp;gt;Verifies 201 Created status.&lt;br /&gt;
|-&lt;br /&gt;
| submit_quiz || POST || /api/v1/student_quizzes/submit_answers || #submit_quiz || Submit answers || Creates a quiz and a corresponding response map.&amp;lt;br&amp;gt;Sends quiz answer submission via POST request.&amp;lt;br&amp;gt;Checks for 200 OK and validates 'total_score' is present in response.&lt;br /&gt;
|-&lt;br /&gt;
| update || PUT || /api/v1/student_quizzes/:id || #update || Update quiz || Creates a quiz with an initial name.&amp;lt;br&amp;gt;Sends a PUT request with updated name.&amp;lt;br&amp;gt;Expects 200 OK and checks that the name is updated in the response.&lt;br /&gt;
|-&lt;br /&gt;
| fetch_quiz || GET || /api/v1/student_quizzes/:id || #fetch_quiz || Fetch quiz using fetch_quiz method || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request using fetch_quiz logic.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name in response.&lt;br /&gt;
|-&lt;br /&gt;
| render_success || GET || /api/v1/student_quizzes || #render_success || Render success response || Calls an action that invokes render_success (index).&amp;lt;br&amp;gt;Sends a GET request and expects 200 OK response.&lt;br /&gt;
|-&lt;br /&gt;
| create_questionnaire || POST || /api/v1/student_quizzes || #create_questionnaire || Create questionnaire || Sends a POST request with questionnaire attributes.&amp;lt;br&amp;gt;Checks if the questionnaire is created and returns 201 Created.&amp;lt;br&amp;gt;Validates the name in the response.&lt;br /&gt;
|-&lt;br /&gt;
| questionnaire_params || N/A || questionnaire_params(params) || #questionnaire_params || Extract strong parameters || Validates strong parameter extraction logic.&amp;lt;br&amp;gt;Handles valid, missing, and invalid parameters.&amp;lt;br&amp;gt;Raises ActionController::ParameterMissing when necessary.&lt;br /&gt;
|-&lt;br /&gt;
| create_questions_and_answers || N/A || create_questions_and_answers(questionnaire, questions_attributes) || #create_questions_and_answers || Build nested resources || Creates a questionnaire object.&amp;lt;br&amp;gt;Calls the method with nested question and answer attributes.&amp;lt;br&amp;gt;Validates that questions and answers are successfully saved.&lt;br /&gt;
|-&lt;br /&gt;
| render_error || N/A || render_error(message, status) || #render_error || Render error response || Triggers an action with invalid parameters.&amp;lt;br&amp;gt;Checks that 422 Unprocessable Entity is returned.&amp;lt;br&amp;gt;Validates the presence of an error message in the response.&lt;br /&gt;
|-&lt;br /&gt;
| quiz_assigned? || N/A || quiz_assigned?(participant, quiz) || #quiz_assigned? || Check quiz assignment || Checks if a quiz has already been assigned to a participant.&amp;lt;br&amp;gt;Returns true or false based on assignment existence and input validity.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || N/A || build_response_map(quiz, participant) || #build_response_map || Create response map || Creates a quiz and participant.&amp;lt;br&amp;gt;Builds a ResponseMap and checks attributes.&amp;lt;br&amp;gt;Returns nil when quiz or participant is missing.&lt;br /&gt;
|-&lt;br /&gt;
| check_instructor_role || N/A || check_instructor_role || #check_instructor_role || Role verification || Checks if current user is an instructor.&amp;lt;br&amp;gt;Returns true if user has instructor role, false otherwise.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;resource_map_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| calculate_score || calculate_score || #calculate_score || Calculate the score based on associated responses. || Mocks a response with a known score.&amp;lt;br&amp;gt;Calls `calculate_score` on the response_map.&amp;lt;br&amp;gt;Expects the calculated score to match.&lt;br /&gt;
|-&lt;br /&gt;
| find_or_initialize_response || find_or_initialize_response(questionnaire) || #find_or_initialize_response || Retrieve an existing response or initialize a new one for the given questionnaire. || Checks if a response already exists and returns it.&amp;lt;br&amp;gt;If none exists, it initializes a new response with the questionnaire.&lt;br /&gt;
|-&lt;br /&gt;
| get_score || get_score(questionnaire) || #get_score || Fetch the score from a response for a specific questionnaire. || Mocks a score and ensures `get_score` returns it.&amp;lt;br&amp;gt;Returns nil if there is no associated response.&lt;br /&gt;
|-&lt;br /&gt;
| process_answers || process_answers || #process_answers || Process answers in the associated response and update state. || Mocks success and failure conditions of answer processing.&amp;lt;br&amp;gt;Returns true or false accordingly.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || build_response_map(questionnaire, participant) || #build_response_map || Create and return a response map for a given questionnaire and participant. || Creates and returns a valid ResponseMap object.&amp;lt;br&amp;gt;Returns nil if either questionnaire or participant is not valid.&lt;br /&gt;
|-&lt;br /&gt;
| find_for_current_user || find_for_current_user(user) || #find_for_current_user || Locate the response map associated with the current user. || Returns the existing response map if present.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Beside those, we test &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; that can be found in &amp;lt;code&amp;gt; find_resource_service_spec.rb&amp;lt;/code&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==Next Steps==&lt;br /&gt;
&lt;br /&gt;
* We will implement the feature that lets students skip quiz questions, including updating the rubric model and UI to support it properly.&lt;br /&gt;
&lt;br /&gt;
* We plan to move score calculation logic from the response map to the response object to better support cases where users take the quiz multiple times.&lt;br /&gt;
&lt;br /&gt;
* The current test coverage is 69.72%; we are confident of increasing the test coverage by implementing all of our test plans.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164710</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=164710"/>
		<updated>2025-04-22T19:01:00Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
* app/models/response.rb&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/services/resource_map_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Refactoring and Moving Business Logic&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the create_score method)&lt;br /&gt;
&amp;lt;br&amp;gt;  &lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/31e2b94777e833961124b24fa18a4923c4e344f7) (Refactoring the process_answers method)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/233e8449d80fedff4797b38549c974ab82586d9c) (Reimplementing the findResource in a different file)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/adf3b13b5b21cfa1f25ad5199e2027b37237c398) (Reimplementing the self.assessment in Review Response Map)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/b9418c9c705916c95bda3107134a6225effd9869) (Fixing method names in student_quizzes_controller.rb)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Error Fixes&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/77e448591711ea5f0fc3ec1a718ded0589bfe166) (Response Map error fix)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/000694b8ced3b667762eeac01c7fcea76b74dd49) (Fixing the Assignment Dependency)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
(https://github.com/Kiraschild/reimplementation-back-end/commit/1e22637ed9ab350ac1e75ef5607ea8d4bdd7edd2) (Adding Rspec Tests)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
As outlined above, we refactored three key files. To ensure comprehensive coverage and maintain system reliability, we have implemented thorough testing for all affected components. This includes both unit and request tests, which are written using RSpec. The test coverage has been added to the following files:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/requests/api/v1/find_resource_service_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;spec/services/api/v1/resource_map_spec.rb&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Pre-Test Setup ===&lt;br /&gt;
&lt;br /&gt;
* Hierarchical roles are created using create_roles_hierarchy : &amp;lt;code&amp;gt;@roles = create_roles_hierarchy&amp;lt;/code&amp;gt;&lt;br /&gt;
* Users with appropriate roles are instantiated for test scenarios.&lt;br /&gt;
&lt;br /&gt;
JWT is used in tests to pretend a real user is logged in so we can check if only the right people can access certain API actions. It helps test authentication and authorization properly.&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
let(:token) { JsonWebToken.encode({ id: instructor.id }) }&lt;br /&gt;
let(:auth_headers) { { &amp;quot;Authorization&amp;quot; =&amp;gt; &amp;quot;Bearer #{token}&amp;quot;, &amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;quot;application/json&amp;quot; } }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Testing ===&lt;br /&gt;
After the setup and authentication, we test each refactored file method. Below is an example of testing &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt; Method in &amp;lt;code&amp;gt;StudentQuizzesController&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Purpose''': Retrieve a list of all quizzes.&lt;br /&gt;
&lt;br /&gt;
'''Test''': Validates that the endpoint returns a 200 OK status and a JSON array of quizzes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
describe '#index' do&lt;br /&gt;
  it &amp;quot;returns a list of quizzes&amp;quot; do&lt;br /&gt;
    Questionnaire.create!(name: &amp;quot;Quiz One&amp;quot;, instructor_id: instructor.id,&lt;br /&gt;
                              min_question_score: 0, max_question_score: 10)&lt;br /&gt;
    get '/api/v1/student_quizzes', headers: auth_headers&lt;br /&gt;
    expect(response).to have_http_status(:ok)&lt;br /&gt;
    quizzes = JSON.parse(response.body)&lt;br /&gt;
    expect(quizzes).to be_an(Array)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below are the complete tables that list our full test plans.&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;student_quizzes_controller_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! HTTP Method !! Endpoint/ Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| index || GET || /api/v1/student_quizzes || #index || Fetch all quizzes || Creates a sample quiz.&amp;lt;br&amp;gt;Sends a GET request to fetch quizzes.&amp;lt;br&amp;gt;Expects 200 OK and validates the response is an array.&lt;br /&gt;
|-&lt;br /&gt;
| create || POST || /api/v1/student_quizzes || #create || Create a new quiz || Prepares quiz parameters with nested questions and answers.&amp;lt;br&amp;gt;Sends a POST request to create a new quiz.&amp;lt;br&amp;gt;Expects 201 Created and checks for 'name' and 'questions' in response.&lt;br /&gt;
|-&lt;br /&gt;
| destroy || DELETE || /api/v1/student_quizzes/:id || #destroy || Delete a quiz || Creates a quiz record.&amp;lt;br&amp;gt;Sends a DELETE request to remove the quiz.&amp;lt;br&amp;gt;Expects 204 No Content and confirms quiz is deleted from the database.&lt;br /&gt;
|-&lt;br /&gt;
| show || GET || /api/v1/student_quizzes/:id || #show || Show quiz details || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request to fetch the quiz by ID.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name and ID in the response.&lt;br /&gt;
|-&lt;br /&gt;
| assign_quiz || POST || /api/v1/student_quizzes/assign || #assign_quiz || Assign quiz to a participant || Assigns a quiz to a Participant model instance.&amp;lt;br&amp;gt;Sends a POST request to the assign endpoint.&amp;lt;br&amp;gt;Verifies 201 Created status.&lt;br /&gt;
|-&lt;br /&gt;
| submit_quiz || POST || /api/v1/student_quizzes/submit_answers || #submit_quiz || Submit answers || Creates a quiz and a corresponding response map.&amp;lt;br&amp;gt;Sends quiz answer submission via POST request.&amp;lt;br&amp;gt;Checks for 200 OK and validates 'total_score' is present in response.&lt;br /&gt;
|-&lt;br /&gt;
| update || PUT || /api/v1/student_quizzes/:id || #update || Update quiz || Creates a quiz with an initial name.&amp;lt;br&amp;gt;Sends a PUT request with updated name.&amp;lt;br&amp;gt;Expects 200 OK and checks that the name is updated in the response.&lt;br /&gt;
|-&lt;br /&gt;
| fetch_quiz || GET || /api/v1/student_quizzes/:id || #fetch_quiz || Fetch quiz using fetch_quiz method || Creates a quiz record.&amp;lt;br&amp;gt;Sends a GET request using fetch_quiz logic.&amp;lt;br&amp;gt;Expects 200 OK and validates the quiz's name in response.&lt;br /&gt;
|-&lt;br /&gt;
| render_success || GET || /api/v1/student_quizzes || #render_success || Render success response || Calls an action that invokes render_success (index).&amp;lt;br&amp;gt;Sends a GET request and expects 200 OK response.&lt;br /&gt;
|-&lt;br /&gt;
| create_questionnaire || POST || /api/v1/student_quizzes || #create_questionnaire || Create questionnaire || Sends a POST request with questionnaire attributes.&amp;lt;br&amp;gt;Checks if the questionnaire is created and returns 201 Created.&amp;lt;br&amp;gt;Validates the name in the response.&lt;br /&gt;
|-&lt;br /&gt;
| questionnaire_params || N/A || questionnaire_params(params) || #questionnaire_params || Extract strong parameters || Validates strong parameter extraction logic.&amp;lt;br&amp;gt;Handles valid, missing, and invalid parameters.&amp;lt;br&amp;gt;Raises ActionController::ParameterMissing when necessary.&lt;br /&gt;
|-&lt;br /&gt;
| create_questions_and_answers || N/A || create_questions_and_answers(questionnaire, questions_attributes) || #create_questions_and_answers || Build nested resources || Creates a questionnaire object.&amp;lt;br&amp;gt;Calls the method with nested question and answer attributes.&amp;lt;br&amp;gt;Validates that questions and answers are successfully saved.&lt;br /&gt;
|-&lt;br /&gt;
| render_error || N/A || render_error(message, status) || #render_error || Render error response || Triggers an action with invalid parameters.&amp;lt;br&amp;gt;Checks that 422 Unprocessable Entity is returned.&amp;lt;br&amp;gt;Validates the presence of an error message in the response.&lt;br /&gt;
|-&lt;br /&gt;
| quiz_assigned? || N/A || quiz_assigned?(participant, quiz) || #quiz_assigned? || Check quiz assignment || Checks if a quiz has already been assigned to a participant.&amp;lt;br&amp;gt;Returns true or false based on assignment existence and input validity.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || N/A || build_response_map(quiz, participant) || #build_response_map || Create response map || Creates a quiz and participant.&amp;lt;br&amp;gt;Builds a ResponseMap and checks attributes.&amp;lt;br&amp;gt;Returns nil when quiz or participant is missing.&lt;br /&gt;
|-&lt;br /&gt;
| check_instructor_role || N/A || check_instructor_role || #check_instructor_role || Role verification || Checks if current user is an instructor.&amp;lt;br&amp;gt;Returns true if user has instructor role, false otherwise.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Tests for &amp;lt;code&amp;gt;resource_map_spec.rb&amp;lt;/code&amp;gt; ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Test Name !! Method !! Action !! Purpose !! Tests&lt;br /&gt;
|-&lt;br /&gt;
| calculate_score || calculate_score || #calculate_score || Calculate the score based on associated responses. || Mocks a response with a known score.&amp;lt;br&amp;gt;Calls `calculate_score` on the response_map.&amp;lt;br&amp;gt;Expects the calculated score to match.&lt;br /&gt;
|-&lt;br /&gt;
| find_or_initialize_response || find_or_initialize_response(questionnaire) || #find_or_initialize_response || Retrieve an existing response or initialize a new one for the given questionnaire. || Checks if a response already exists and returns it.&amp;lt;br&amp;gt;If none exists, it initializes a new response with the questionnaire.&lt;br /&gt;
|-&lt;br /&gt;
| get_score || get_score(questionnaire) || #get_score || Fetch the score from a response for a specific questionnaire. || Mocks a score and ensures `get_score` returns it.&amp;lt;br&amp;gt;Returns nil if there is no associated response.&lt;br /&gt;
|-&lt;br /&gt;
| process_answers || process_answers || #process_answers || Process answers in the associated response and update state. || Mocks success and failure conditions of answer processing.&amp;lt;br&amp;gt;Returns true or false accordingly.&lt;br /&gt;
|-&lt;br /&gt;
| build_response_map || build_response_map(questionnaire, participant) || #build_response_map || Create and return a response map for a given questionnaire and participant. || Creates and returns a valid ResponseMap object.&amp;lt;br&amp;gt;Returns nil if either questionnaire or participant is not valid.&lt;br /&gt;
|-&lt;br /&gt;
| find_for_current_user || find_for_current_user(user) || #find_for_current_user || Locate the response map associated with the current user. || Returns the existing response map if present.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Beside those, we test &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; that can be found in &amp;lt;code&amp;gt; find_resource_service_spec.rb&amp;lt;/code&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==Next Steps==&lt;br /&gt;
&lt;br /&gt;
* We will implement the feature that lets students skip quiz questions, including updating the rubric model and UI to support it properly.&lt;br /&gt;
&lt;br /&gt;
* We plan to move score calculation logic from the response map to the response object to better support cases where users take the quiz multiple times.&lt;br /&gt;
&lt;br /&gt;
* The current test coverage is 69.72%; we are confident of increasing the test coverage by implementing all of our test plans.&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163558</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163558"/>
		<updated>2025-04-08T01:09:33Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Sequence of Actions in Assigning a Quiz */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Waitlist Teams Table, Validations, and Model File&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/1/files&amp;quot;&amp;gt;Adding waitlist_teams table&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/2/files&amp;quot;&amp;gt;Migration script changes to add unique constraint on waitlist_teams table and remove id column&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/4/files&amp;quot;&amp;gt;Schema changes&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/3/files&amp;quot;&amp;gt;Validations&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
The creation of the `waitlist_teams` table and migration scripts that modify the schema. It also adds a unique constraint to the `waitlist_teams` table and removes the `id` column for better data integrity.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Migration Scripts&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/10/files&amp;quot;&amp;gt;Migrate existing data in signed_up_teams to waitlist_teams&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
This migration script migrates existing data in the `signed_up_teams` table to the new `waitlist_teams` table to ensure that all relevant data is properly migrated and preserved.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Business Logic Changes&amp;lt;/b&amp;gt;&lt;br /&gt;
Adding new methods for adding, deleting, and signing up waitlist teams in `WaitlistTeam.rb`. Changes were also made to existing method calls in model files.&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/5/files&amp;quot;&amp;gt;WaitlistTeam model methods like CRUD operations&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/11/files&amp;quot;&amp;gt;Methods to handle deletion cases&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/12&amp;quot;&amp;gt;Check if team is waitlisted before a topic signup&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/13&amp;quot;&amp;gt;Release waitlist and signup when a user joins another team and there are no other users in the old team&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/14/files&amp;quot;&amp;gt;Synchronize waitlist features in other files&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
These changes introduce methods for adding, deleting, and managing waitlist teams, along with improvements to handle edge cases, such as when a user joins a new team, and the synchronization of waitlist functionality across various files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/13&amp;quot;&amp;gt;RSpec changes to test model methods&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163557</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163557"/>
		<updated>2025-04-08T01:09:26Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* UML Diagram */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Waitlist Teams Table, Validations, and Model File&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/1/files&amp;quot;&amp;gt;Adding waitlist_teams table&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/2/files&amp;quot;&amp;gt;Migration script changes to add unique constraint on waitlist_teams table and remove id column&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/4/files&amp;quot;&amp;gt;Schema changes&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/3/files&amp;quot;&amp;gt;Validations&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
The creation of the `waitlist_teams` table and migration scripts that modify the schema. It also adds a unique constraint to the `waitlist_teams` table and removes the `id` column for better data integrity.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Migration Scripts&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/10/files&amp;quot;&amp;gt;Migrate existing data in signed_up_teams to waitlist_teams&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
This migration script migrates existing data in the `signed_up_teams` table to the new `waitlist_teams` table to ensure that all relevant data is properly migrated and preserved.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Business Logic Changes&amp;lt;/b&amp;gt;&lt;br /&gt;
Adding new methods for adding, deleting, and signing up waitlist teams in `WaitlistTeam.rb`. Changes were also made to existing method calls in model files.&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/5/files&amp;quot;&amp;gt;WaitlistTeam model methods like CRUD operations&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/11/files&amp;quot;&amp;gt;Methods to handle deletion cases&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/12&amp;quot;&amp;gt;Check if team is waitlisted before a topic signup&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/13&amp;quot;&amp;gt;Release waitlist and signup when a user joins another team and there are no other users in the old team&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/14/files&amp;quot;&amp;gt;Synchronize waitlist features in other files&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
These changes introduce methods for adding, deleting, and managing waitlist teams, along with improvements to handle edge cases, such as when a user joins a new team, and the synchronization of waitlist functionality across various files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/13&amp;quot;&amp;gt;RSpec changes to test model methods&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163555</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163555"/>
		<updated>2025-04-08T01:07:12Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
===UML Diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Waitlist Teams Table, Validations, and Model File&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/1/files&amp;quot;&amp;gt;Adding waitlist_teams table&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/2/files&amp;quot;&amp;gt;Migration script changes to add unique constraint on waitlist_teams table and remove id column&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/4/files&amp;quot;&amp;gt;Schema changes&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/3/files&amp;quot;&amp;gt;Validations&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
The creation of the `waitlist_teams` table and migration scripts that modify the schema. It also adds a unique constraint to the `waitlist_teams` table and removes the `id` column for better data integrity.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Migration Scripts&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/10/files&amp;quot;&amp;gt;Migrate existing data in signed_up_teams to waitlist_teams&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
This migration script migrates existing data in the `signed_up_teams` table to the new `waitlist_teams` table to ensure that all relevant data is properly migrated and preserved.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Business Logic Changes&amp;lt;/b&amp;gt;&lt;br /&gt;
Adding new methods for adding, deleting, and signing up waitlist teams in `WaitlistTeam.rb`. Changes were also made to existing method calls in model files.&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/5/files&amp;quot;&amp;gt;WaitlistTeam model methods like CRUD operations&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/11/files&amp;quot;&amp;gt;Methods to handle deletion cases&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/12&amp;quot;&amp;gt;Check if team is waitlisted before a topic signup&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/13&amp;quot;&amp;gt;Release waitlist and signup when a user joins another team and there are no other users in the old team&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/14/files&amp;quot;&amp;gt;Synchronize waitlist features in other files&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
These changes introduce methods for adding, deleting, and managing waitlist teams, along with improvements to handle edge cases, such as when a user joins a new team, and the synchronization of waitlist functionality across various files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) RSpec&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/akhilkumarmengani/expertiza/pull/13&amp;quot;&amp;gt;RSpec changes to test model methods&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
RSpec tests were added to validate the new methods and ensure the business logic for managing waitlist teams functions correctly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163537</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163537"/>
		<updated>2025-04-08T00:55:23Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the &amp;lt;code&amp;gt;student_quizzes_controller.rb&amp;lt;/code&amp;gt; behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming &amp;lt;code&amp;gt;assign_quiz_to_student&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt;: The refactored name, &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt;, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming &amp;lt;code&amp;gt;quiz_already_assigned?&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt;: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming &amp;lt;code&amp;gt;set_student_quiz&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;fetch_quiz&amp;lt;/code&amp;gt;: The new name, &amp;lt;code&amp;gt;fetch_quiz&amp;lt;/code&amp;gt;, is more descriptive and aligns better with the method's purpose of retrieving a quiz from the database. The previous name, &amp;lt;code&amp;gt;set_student_quiz&amp;lt;/code&amp;gt;, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name &amp;lt;code&amp;gt;fetch_quiz&amp;lt;/code&amp;gt; also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method &amp;lt;code&amp;gt;self.assessments_for&amp;lt;/code&amp;gt;, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163535</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163535"/>
		<updated>2025-04-08T00:54:53Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the &amp;lt;code&amp;gt;student_quizzes_controller.rb&amp;lt;/code&amp;gt; behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming &amp;lt;code&amp;gt;assign_quiz_to_student&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt;: The refactored name, &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt;, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming &amp;lt;code&amp;gt;quiz_already_assigned?&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt;: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming &amp;lt;code&amp;gt;set_student_quiz&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;fetch_quiz&amp;lt;/code&amp;gt;: The new name, &amp;lt;code&amp;gt;fetch_quiz&amp;lt;/code&amp;gt;, is more descriptive and aligns better with the method's purpose of retrieving a quiz from the database. The previous name, &amp;lt;code&amp;gt;set_student_quiz&amp;lt;/code&amp;gt;, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name &amp;lt;code&amp;gt;fetch_quiz&amp;lt;/code&amp;gt; also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method &amp;lt;code&amp;gt;self.assessments_for&amp;lt;/code, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163532</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163532"/>
		<updated>2025-04-08T00:54:16Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming &amp;lt;code&amp;gt;assign_quiz_to_student&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt;: The refactored name, &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt;, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming &amp;lt;code&amp;gt;quiz_already_assigned?&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt;: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming &amp;lt;code&amp;gt;set_student_quiz&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;fetch_quiz&amp;lt;/code&amp;gt;: The new name, &amp;lt;code&amp;gt;fetch_quiz&amp;lt;/code&amp;gt;, is more descriptive and aligns better with the method's purpose of retrieving a quiz from the database. The previous name, &amp;lt;code&amp;gt;set_student_quiz&amp;lt;/code&amp;gt;, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name &amp;lt;code&amp;gt;fetch_quiz&amp;lt;/code&amp;gt; also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method &amp;lt;code&amp;gt;self.assessments_for&amp;lt;/code, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163530</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163530"/>
		<updated>2025-04-08T00:53:55Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming &amp;lt;code&amp;gt;assign_quiz_to_student&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt;: The refactored name, &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt;, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming &amp;lt;code&amp;gt;quiz_already_assigned?&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt;: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming &amp;lt;code&amp;gt;set_student_quiz&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;fetch_quiz&amp;lt;/code&amp;gt;: The new name, &amp;lt;code&amp;gt;fetch_quiz&amp;lt;/code&amp;gt;, is more descriptive and aligns better with the method's purpose of retrieving a quiz from the database. The previous name, &amp;lt;code&amp;gt;set_student_quiz&amp;lt;/code&amp;gt;, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name &amp;lt;code&amp;gt;fetch_quiz&amp;lt;/code&amp;gt; also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method &amp;lt;code&amp;gt;self.assessments_for&amp;lt;/code, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163526</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163526"/>
		<updated>2025-04-08T00:52:00Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Changes Implemented */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose of retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163514</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163514"/>
		<updated>2025-04-08T00:47:56Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===UML diagram===&lt;br /&gt;
[[File:student_quizzes_UML.png|650px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quizzes_UML.png&amp;diff=163511</id>
		<title>File:Student quizzes UML.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Student_quizzes_UML.png&amp;diff=163511"/>
		<updated>2025-04-08T00:46:17Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163493</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163493"/>
		<updated>2025-04-08T00:27:24Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Sequence of Actions in Submitting Quiz Answers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163492</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163492"/>
		<updated>2025-04-08T00:27:16Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Sequence of Actions in Assigning a Quiz */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163491</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163491"/>
		<updated>2025-04-08T00:26:57Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Sequence of Actions in Submitting Quiz Answers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163490</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163490"/>
		<updated>2025-04-08T00:26:28Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Sequence of Actions in Assigning a Quiz */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&amp;lt;br&amp;gt;&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&amp;lt;br&amp;gt;&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&lt;br /&gt;
3) Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163489</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163489"/>
		<updated>2025-04-08T00:25:57Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Key Features */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&lt;br /&gt;
3) Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163488</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163488"/>
		<updated>2025-04-08T00:25:36Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;1) Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;2) Services and Helpers:&amp;lt;/b&amp;gt; Several helper methods and service objects are used to encapsulate complex logic and improve code readability.&lt;br /&gt;
* FindResourceService: This service is responsible for fetching resources (e.g., &amp;lt;code&amp;gt;Participant&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Questionnaire&amp;lt;/code&amp;gt;) from the database. It helps keep the controller code clean and focused on the action logic.&lt;br /&gt;
* ResponseMap: This class is used to map students to quizzes and track their responses. The controller checks if a student is already assigned to a quiz before creating a new assignment. It also handles the processing of answers and the calculation of total scores.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Role-Based Access Control:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;before_action&amp;lt;/code&amp;gt;callback &amp;lt;code&amp;gt;check_instructor_role&amp;lt;/code&amp;gt; ensures that only users with an instructor role (role_id = 2) can perform certain actions, such as assigning quizzes and creating new ones. This ensures that only authorized users can access sensitive functionalities.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Error Handling:&amp;lt;/b&amp;gt; The controller uses standard error handling to ensure the system responds gracefully in case of any issues. If an error occurs (e.g., quiz already assigned, invalid data), a custom error message is rendered along with the appropriate HTTP status code.&lt;br /&gt;
For example, when a quiz is assigned to a student, if the student is already assigned, the system returns a unprocessable_entity error with a descriptive message.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;5) Transaction Handling:&amp;lt;/b&amp;gt; The &amp;lt;code&amp;gt;create&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; actions use database transactions (ActiveRecord::Base.transaction) to ensure that operations are atomic. If any part of the process fails (e.g., saving answers), the entire transaction is rolled back to maintain data integrity.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Assigning a Quiz===&lt;br /&gt;
&lt;br /&gt;
1) The instructor calls the &amp;lt;code&amp;gt;POST /student_quizzes/assign&amp;lt;/code&amp;gt; endpoint to assign a quiz.&lt;br /&gt;
2) The &amp;lt;code&amp;gt;assign_quiz&amp;lt;/code&amp;gt; method is invoked:&lt;br /&gt;
* FindParticipant: The &amp;lt;code&amp;gt;FindResourceService&amp;lt;/code&amp;gt; is called to retrieve the Participant and Questionnaire by their respective IDs.&lt;br /&gt;
* Check Assignment: The &amp;lt;code&amp;gt;quiz_assigned?&amp;lt;/code&amp;gt; method checks whether the quiz has already been assigned to the student.&lt;br /&gt;
* Build ResponseMap: A new &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is created, linking the student to the quiz.&lt;br /&gt;
* Save ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is saved to the database, completing the assignment process.&lt;br /&gt;
* The system responds with the newly created ResponseMap and a success message.&lt;br /&gt;
&lt;br /&gt;
===Sequence of Actions in Submitting Quiz Answers===&lt;br /&gt;
1) The student submits their answers to the quiz via the &amp;lt;code&amp;gt;POST /student_quizzes/submit_answers&amp;lt;/code&amp;gt; endpoint.&lt;br /&gt;
2) The &amp;lt;code&amp;gt;submit_quiz&amp;lt;/code&amp;gt; method is invoked:&lt;br /&gt;
3) Find ResponseMap: The system retrieves the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; for the current user and quiz.&lt;br /&gt;
* Process Answers: The student's answers are processed and associated with the &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Calculate Score: The &amp;lt;code&amp;gt;calculate_score&amp;lt;/code&amp;gt; method is called to compute the total score based on the answers provided.&lt;br /&gt;
* Update ResponseMap: The &amp;lt;code&amp;gt;ResponseMap&amp;lt;/code&amp;gt; is updated with the total score.&lt;br /&gt;
* The system responds with the calculated score.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163480</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163480"/>
		<updated>2025-04-08T00:15:12Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Key Features */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index &amp;lt;code&amp;gt;(GET /student_quizzes)&amp;lt;/code&amp;gt;: Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show &amp;lt;code&amp;gt;(GET /student_quizzes/:id)&amp;lt;/code&amp;gt;: Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create &amp;lt;code&amp;gt;(POST /student_quizzes)&amp;lt;/code&amp;gt;: Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/assign)&amp;lt;/code&amp;gt;: Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz &amp;lt;code&amp;gt;(POST /student_quizzes/submit_answers)&amp;lt;/code&amp;gt;: Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update &amp;lt;code&amp;gt;(PUT /student_quizzes/:id)&amp;lt;/code&amp;gt;: Updates an existing quiz.&lt;br /&gt;
* Destroy &amp;lt;code&amp;gt;(DELETE /student_quizzes/:id)&amp;lt;/code&amp;gt;: Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163477</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163477"/>
		<updated>2025-04-08T00:11:34Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Key Features===&lt;br /&gt;
&amp;lt;b&amp;gt;Controller Actions:&amp;lt;/b&amp;gt; The StudentQuizzesController is responsible for managing quiz-related functionalities such as creating quizzes, assigning quizzes to students, submitting answers, updating quizzes, and deleting quizzes. Below is an overview of the main actions and their responsibilities:&lt;br /&gt;
* Index (GET /student_quizzes): Retrieves all available quizzes (questionnaires) from the database.&lt;br /&gt;
* Show (GET /student_quizzes/:id): Fetches a single quiz (questionnaire) by its ID.&lt;br /&gt;
* Create (POST /student_quizzes): Creates a new quiz along with its associated questions and answers.&lt;br /&gt;
* Assign Quiz (POST /student_quizzes/assign): Assigns a specific quiz to a student.&lt;br /&gt;
* Submit Quiz (POST /student_quizzes/submit_answers): Allows the student to submit answers for a quiz and calculates the total score.&lt;br /&gt;
* Update (PUT /student_quizzes/:id): Updates an existing quiz.&lt;br /&gt;
* Destroy (DELETE /student_quizzes/:id): Deletes a specific quiz.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025&amp;diff=163447</id>
		<title>CSC/ECE 517 Spring 2025</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025&amp;diff=163447"/>
		<updated>2025-04-07T21:13:05Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: /* Final Projects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* [[CSC/ECE 517 Spring 2025 - E2503. Refactor the Team hierarchy]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2504. Mentor-meeting management]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2505. Testing Answer Tagging]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2508. Reimplement bidding-algorithm web service]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2509. Reimplement feedback_response_map.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2519. Implement view for results of bidding]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2522. Enhancing UI Consistency in Expertiza 1]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2520. Reimplement heatgrid UI for reviews]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2517. Reimplement internationalization (frontend + backend)]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2523: Enhancing UI Consistency in Expertiza 2]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2515:  Reimplement student_teams_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2501:  Refactor review_mapping_helper.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2502:  Refactor review_mapping_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2512. Reimplement responses controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2510. Reimplement grades_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2507. Reimplement back end for submission records]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2514. Reimplement student_quizzes_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2518. Reimplement password resets (frontend + backend)]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2516. Reimplement teams_users_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2511. Reimplement participants_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2521. UI for View submissions/assign grades (except heatgrid)]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2513. Reimplement sign_up_topic.rb as project_topic.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2506. Implement testing for new Bookmarks Controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2527. Mentor-meeting management: assignments with topics]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Final Projects ==&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2509 Refactoring and Enhancing the Feedback Response Map Controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2539 Reimplement Student Task View (Frontend + Backend)]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2521 UI for View submissions/assign grades (except heatgrid)]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2524 Reimplement student review controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2525 Reimplement review_mapping_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2526 Reimplement Teams and Participant hierarchies]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2530 Reimplement Grades Controller (Frontend + Backend)]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2538 Reimplementing Questionnaire Page in Expertiza]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2534 UI for Assign Reviewers]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2540 Integration of Assignment participant Frontend with participant controller Backend]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2532. Reimplement Missing ResponseMap Subclasses]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2542. Refactor review_bids_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2533. ​​Reimplement the Team hierarchy]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2531 Refactor participants_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2528 Testing for Survey Deployment]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2025 - E2514. Reimplement student_quizzes_controller.rb]]&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163411</id>
		<title>CSC/ECE 517 Spring 2025 - E2514. Reimplement student quizzes controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2025_-_E2514._Reimplement_student_quizzes_controller.rb&amp;diff=163411"/>
		<updated>2025-04-07T18:52:47Z</updated>

		<summary type="html">&lt;p&gt;Kjoshi4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Introduction==&lt;br /&gt;
[https://expertiza.ncsu.edu/ Expertiza] is an educational web application collaboratively developed and maintained by students and faculty at NCSU. As an open-source project built on the Ruby on Rails platform, its code is accessible on GitHub. The platform enables students to provide peer reviews and refine their work based on feedback.&lt;br /&gt;
&lt;br /&gt;
==Project Overview==&lt;br /&gt;
Quizzes in Expertiza are designed to ensure that reviewers comprehend the material they are evaluating. When an assignment includes quizzes, submitting teams create these quizzes based on their work. Reviewers must complete the quizzes before reviewing to demonstrate their understanding. If a reviewer performs poorly, their review can be discounted to maintain quality.&lt;br /&gt;
&lt;br /&gt;
The student_quizzes_controller.rb manages quiz-related actions, including quiz creation, scoring, and recording responses for both students and reviewers. However, the existing implementation violates several key Rails principles, including the DRY (Don't Repeat Yourself) principle and the improper placement of business logic within the controller. Instead, this logic should reside in model classes, in adherence to the MVC (Model-View-Controller) architecture.&lt;br /&gt;
&lt;br /&gt;
==Previous Implementation==&lt;br /&gt;
The previous implementation of the student_quizzes_controller.rb contained several design issues. While some progress was made, such as moving calculate_score and process_answers to the ResponseMap model and replacing role ID checks with Role model methods, several key issues remained. &lt;br /&gt;
Business logic was still embedded in the controller, violating the Single Responsibility Principle (SRP). Method names were unclear, like set_student_quiz, and resource-finding logic was incorrectly placed in the controller. Additionally, quiz retake policies and question-skipping lacked adequate support. Testing coverage was also insufficient, and documentation needed improvement. Fixing these issues will significantly enhance the code's maintainability, clarity, and overall structure.&lt;br /&gt;
&lt;br /&gt;
==Project Goals==&lt;br /&gt;
This project focuses on reimplementing student_quizzes_controller.rb in the reimplementation-backend repository, addressing issues from the previous implementation. Key improvements will include:&lt;br /&gt;
&lt;br /&gt;
* Improving method names and readability to better align with their purposes&lt;br /&gt;
* Refactoring to remove redundant code and consolidate functionality into reusable functions&lt;br /&gt;
* Enhancing test coverage and documentation clarity&lt;br /&gt;
&lt;br /&gt;
By addressing these issues, the project aims to produce a well-structured, maintainable, and scalable student_quizzes_controller.rb that follows Rails best practices, improves code organization, and enhances the overall maintainability of the Expertiza platform.&lt;br /&gt;
&lt;br /&gt;
==Files Modified==&lt;br /&gt;
&lt;br /&gt;
* app/models/response_map.rb&lt;br /&gt;
* app/controllers/api/v1/student_quizzes_controller.rb&lt;br /&gt;
* app/services/find_resource_service.rb&lt;br /&gt;
&lt;br /&gt;
==Changes Implemented==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;1) Moving Find Resource Out of Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/controllers/api/v1/student_quizzes_controller.rb'''  &lt;br /&gt;
    [[File:student_quiz1.png|400px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/services/find_resource_service.rb'''  &lt;br /&gt;
    [[File:student_quiz2.png|650px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method renaming and extraction of logic to a service are intended to improve the maintainability, readability, and scalability of the code. The refactor helps ensure that the controller remains focused on its core responsibility of handling HTTP requests while delegating resource lookup to the service. The FindResourceService improves error handling by ensuring that any ActiveRecord::RecordNotFound exceptions are caught and a custom message is raised, enhancing the user experience.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
2)&lt;br /&gt;
Ensured deleting a quiz is handled correctly in student_quizzes_controller.rb &amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz3.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;'&lt;br /&gt;
&lt;br /&gt;
This change ensures that the destroy action in the student_quizzes_controller.rb behaves correctly in all cases, including when attempting to delete a non-existent quiz. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;3) Renamed and improved method names &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
    [[File:student_quiz4.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz5.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt; &lt;br /&gt;
    [[File:student_quiz7.png|300px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a) Renaming assign_quiz_to_student to assign_quiz: The refactored name, assign_quiz, is simpler and still clear in the context of assigning a quiz. It follows the principle of keeping method names concise without losing meaning, making the code easier to maintain and read.&lt;br /&gt;
&lt;br /&gt;
b) Renaming quiz_already_assigned? to quiz_assigned?: The renaming simplifies the method name while keeping its meaning intact. The method still performs the same check, but the new name improves code clarity and readability.&lt;br /&gt;
&lt;br /&gt;
c) Renaming set_student_quiz to fetch_quiz: The new name, fetch_quiz, is more descriptive and aligns better with the method's purpose—retrieving a quiz from the database. The previous name, set_student_quiz, was less intuitive and didn’t clearly convey the action of retrieving a quiz. The name fetch_quiz also adheres to common naming conventions, making it easier to understand at a glance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;4) Implement self.assessment in Review Response Map &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;display:flex; justify-content:center; gap:20px; align-items:flex-start;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;text-align:center;&amp;quot;&amp;gt;&lt;br /&gt;
    '''app/models/review_response_map.rb'''  &lt;br /&gt;
    [[File:student_quiz10.png|600px]]&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing this class method self.assessments_for, the logic for retrieving all assessments for a reviewee is encapsulated, making it reusable and easier to call from other parts of the codebase.&lt;br /&gt;
&lt;br /&gt;
==Test files==&lt;br /&gt;
We have added the tests in the following files:&lt;br /&gt;
&lt;br /&gt;
* spec/requests/api/v1/student_quizzes_controller_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
* spec/requests/api/v1/find_resource_service_spec.rb&lt;br /&gt;
&lt;br /&gt;
==Github Pull Request==&lt;br /&gt;
https://github.com/expertiza/reimplementation-back-end/pull/180&lt;br /&gt;
&lt;br /&gt;
==Mentor==&lt;br /&gt;
&lt;br /&gt;
* Anish Toorpu ([mailto:atoorpu@ncsu.edu atoorpu@ncsu.edu])&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
* Kavya Lalbahadur Joshi               ([mailto:kjoshi4@ncsu.edu kjoshi4@ncsu.edu])&lt;br /&gt;
* Vedant Patel                 ([mailto:vpatel32@ncsu.edu vpatel@ncsu.edu])&lt;br /&gt;
* Uchswas Paul           ([mailto:upaul@ncsu.edu upaul@ncsu.edu])&lt;/div&gt;</summary>
		<author><name>Kjoshi4</name></author>
	</entry>
</feed>