<?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=Sbabu</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=Sbabu"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Sbabu"/>
	<updated>2026-05-09T04:14:12Z</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_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145418</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145418"/>
		<updated>2022-04-26T03:49:12Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Future Scope */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id. Ideally, the application should use only participant_id, but to handle the existing data in the database without running a massive migration, the application should be able to handle both old records that use user_id and new ones that shall, from after this project is completed, use participant_id instead.&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
* However, it is to be noted that this must also work with the existing functionality and data which makes use of user_id, so the code written must take both into account in its working.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored as this view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the CRUD functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:ER_Updated.png |900px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
Main files which were changed are&lt;br /&gt;
* app/models/participant.rb&lt;br /&gt;
* app/models/team.rb&lt;br /&gt;
* app/models/teams_user.rb&lt;br /&gt;
* app/models/user.rb&lt;br /&gt;
&lt;br /&gt;
As the project tries to introduce participant_id as a replacement of user_id, we've calculated all the details with both user_id and participant_id (whichever is available based on whether the data is old or new). We've also added comments everywhere to show the code snippets which need to be removed once user_id is deprecated. (Check for '''comments with #E2243''')&lt;br /&gt;
&lt;br /&gt;
Currently teams_user table has user_id or participant_id for now. So we've ensured that checks happen based on both the IDs unless user_id is removed. The default check is overridden to ensure newer datas which only have participant_id are also indexed and found using the function call here.&lt;br /&gt;
&lt;br /&gt;
'''Code Snippet'''&lt;br /&gt;
 &lt;br /&gt;
 TeamsUser.where(user_id: user_id).or(TeamsUser.where(participant_id: participant_id))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===find_by_team_id_and_user_id===&lt;br /&gt;
&lt;br /&gt;
''.where'' function makes a lot of DB calls when asked for user_id, we've introduced a module called &amp;quot;'''''find_by_team_id_and_user_id()'''''&amp;quot; which will override ''.where'' function. If user_id is not present in the table, it loads based on participant_id instead, as you can have only one as the key and not both.&lt;br /&gt;
&lt;br /&gt;
So wherever .where is used, &lt;br /&gt;
 TeamsUser.where('team_id = ? and user_id = ?', @team.id, @user.id), &lt;br /&gt;
we've replaced it with &lt;br /&gt;
 TeamsUser.find_by_team_id_and_user_id(@team.id, @user.id). &lt;br /&gt;
&lt;br /&gt;
[[File:Find_by_team_id_and_user_id.png |600px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This way instead of changing the query for every call, we've used a single function for all the calls, thereby implementing '''DRY''' principle.&lt;br /&gt;
&lt;br /&gt;
===add_participant_to_team===&lt;br /&gt;
&lt;br /&gt;
The existing function to add a member&lt;br /&gt;
 add_member(user,team.parent_id)&lt;br /&gt;
adds user to a team based on the user_id and team_id.&lt;br /&gt;
&lt;br /&gt;
We've introduced a new function called&lt;br /&gt;
 add_participant_to_team(student, team.parent_id)&lt;br /&gt;
which adds participant(student) to a team based on participant_id(or user_id wherever participant_id is not available)&lt;br /&gt;
&lt;br /&gt;
This function takes participant and team_id, and assigns that participant to a team. In future, once user_id is removed, add_participant_to_team can be used as a replacement.&lt;br /&gt;
&lt;br /&gt;
[[File:Add_participant_to_team.png|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
Please see the next sections for some screenshots of the UI related to the above described partials.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
1. Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
&lt;br /&gt;
[[File:Student_Creates_Team_SendInvite.png|600px]]&lt;br /&gt;
&lt;br /&gt;
2. Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
&lt;br /&gt;
[[File:Student_Joins_Team_ReceivesInvite.png|600px]]&lt;br /&gt;
&lt;br /&gt;
3.  Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Teams_Instructors_Manually.png|600px]]&lt;br /&gt;
&lt;br /&gt;
4. Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Teams_Instructors_Automatically.png|600px]]&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb :&lt;br /&gt;
&lt;br /&gt;
* ''object initialization''&lt;br /&gt;
* ''#update_duties''&lt;br /&gt;
* ''when user is added to assignment team''&lt;br /&gt;
* ''when user is added to assignment team''&lt;br /&gt;
* ''when user is added to course team''&lt;br /&gt;
* ''when user is already on an assignment team''&lt;br /&gt;
* ''when user is added to assignment team while on a team already'' &lt;br /&gt;
* ''when user is already on a course team'' &lt;br /&gt;
&lt;br /&gt;
the new methods which use both participant_id and user_id have been incorporated as needed to ensure continued quality of the testing suite and maintenance of code coverage.&lt;br /&gt;
&lt;br /&gt;
==== Fixed Test Files ====&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/course_team_spec.rb&lt;br /&gt;
* spec/models/invitation_spec.rb&lt;br /&gt;
* spec/models/student_task_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
* spec/helpers/grades_helper_spec.rb&lt;br /&gt;
* spec/helpers/sign_up_sheet_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/grades_controller_spec.rb&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
* spec/controllers/invitations_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Future Scope ==&lt;br /&gt;
&lt;br /&gt;
# Create a migration to compute the participant_id field for all the rows in the teams_users table that currently have a user_id field. &lt;br /&gt;
# Remove the user_id column from the teams_users table. &lt;br /&gt;
# Rename the teams_user table to teams_participant so that this better reflects its new functionality and use cases in the system.&lt;br /&gt;
# Modify functions which use user_id and replace them with functions using participant_id only (Note: Look for code comments with #E2243 for all the replacement functions)&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;br /&gt;
&lt;br /&gt;
== Code Details == &lt;br /&gt;
Github Pull Request - https://github.com/expertiza/expertiza/pull/2391&lt;br /&gt;
&lt;br /&gt;
Repo - https://github.com/sak1997/expertiza/tree/beta&lt;br /&gt;
&lt;br /&gt;
Demo - https://drive.google.com/file/d/1zWnHJNgSkioMd_xmB2L-7wPM2_OP7yka/view?usp=sharing&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145380</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145380"/>
		<updated>2022-04-26T03:04:35Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* View Changes and Refactoring */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id. Ideally, the application should use only participant_id, but to handle the existing data in the database without running a massive migration, the application should be able to handle both old records that use user_id and new ones that shall, from after this project is completed, use participant_id instead.&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
* However, it is to be noted that this must also work with the existing functionality and data which makes use of user_id, so the code written must take both into account in its working.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored as this view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the CRUD functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
Main files which were changed are&lt;br /&gt;
* app/models/participant.rb&lt;br /&gt;
* app/models/team.rb&lt;br /&gt;
* app/models/teams_user.rb&lt;br /&gt;
* app/models/user.rb&lt;br /&gt;
&lt;br /&gt;
As the project tries to introduce participant_id as a replacement of user_id, we've calculated all the details with both user_id and participant_id (whichever is available based on whether the data is old or new). We've also added comments everywhere to show the code snippets which need to be removed once user_id is deprecated. (Check for '''comments with #E2243''')&lt;br /&gt;
&lt;br /&gt;
Currently teams_user table has user_id or participant_id for now. So we've ensured that checks happen based on both the IDs unless user_id is removed. The default check is overridden to ensure newer datas which only have participant_id are also indexed and found using the function call here.&lt;br /&gt;
&lt;br /&gt;
'''Code Snippet'''&lt;br /&gt;
 &lt;br /&gt;
 TeamsUser.where(user_id: user_id).or(TeamsUser.where(participant_id: participant_id))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===find_by_team_id_and_user_id===&lt;br /&gt;
&lt;br /&gt;
''.where'' function makes a lot of DB calls when asked for user_id, we've introduced a module called &amp;quot;'''''find_by_team_id_and_user_id()'''''&amp;quot; which will override ''.where'' function. If user_id is not present in the table, it loads based on participant_id instead, as you can have only one as the key and not both.&lt;br /&gt;
&lt;br /&gt;
So wherever .where is used, &lt;br /&gt;
 TeamsUser.where('team_id = ? and user_id = ?', @team.id, @user.id), &lt;br /&gt;
we've replaced it with &lt;br /&gt;
 TeamsUser.find_by_team_id_and_user_id(@team.id, @user.id). &lt;br /&gt;
&lt;br /&gt;
[[File:Find_by_team_id_and_user_id.png |600px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This way instead of changing the query for every call, we've used a single function for all the calls, thereby implementing '''DRY''' principle.&lt;br /&gt;
&lt;br /&gt;
===add_participant_to_team===&lt;br /&gt;
&lt;br /&gt;
The existing function to add a member&lt;br /&gt;
 add_member(user,team.parent_id)&lt;br /&gt;
adds user to a team based on the user_id and team_id.&lt;br /&gt;
&lt;br /&gt;
We've introduced a new function called&lt;br /&gt;
 add_participant_to_team(student, team.parent_id)&lt;br /&gt;
which adds participant(student) to a team based on participant_id(or user_id wherever participant_id is not available)&lt;br /&gt;
&lt;br /&gt;
This function takes participant and team_id, and assigns that participant to a team. In future, once user_id is removed, add_participant_to_team can be used as a replacement.&lt;br /&gt;
&lt;br /&gt;
[[File:Add_participant_to_team.png|600px]]&lt;br /&gt;
&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
Please see the next sections for some screenshots of the UI related to the above described partials.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
1. Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
&lt;br /&gt;
[[File:Student_Creates_Team_SendInvite.png|600px]]&lt;br /&gt;
&lt;br /&gt;
2. Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
&lt;br /&gt;
[[File:Student_Joins_Team_ReceivesInvite.png|600px]]&lt;br /&gt;
&lt;br /&gt;
3.  Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Teams_Instructors_Manually.png|600px]]&lt;br /&gt;
&lt;br /&gt;
4. Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Teams_Instructors_Automatically.png|600px]]&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb :&lt;br /&gt;
&lt;br /&gt;
* ''object initialization''&lt;br /&gt;
* ''#update_duties''&lt;br /&gt;
* ''when user is added to assignment team''&lt;br /&gt;
* ''when user is added to assignment team''&lt;br /&gt;
* ''when user is added to course team''&lt;br /&gt;
* ''when user is already on an assignment team''&lt;br /&gt;
* ''when user is added to assignment team while on a team already'' &lt;br /&gt;
* ''when user is already on a course team'' &lt;br /&gt;
&lt;br /&gt;
the new methods which use both participant_id and user_id have been incorporated as needed to ensure continued quality of the testing suite and maintenance of code coverage.&lt;br /&gt;
&lt;br /&gt;
==== Fixed Test Files ====&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/course_team_spec.rb&lt;br /&gt;
* spec/models/invitation_spec.rb&lt;br /&gt;
* spec/models/student_task_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
* spec/helpers/grades_helper_spec.rb&lt;br /&gt;
* spec/helpers/sign_up_sheet_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/grades_controller_spec.rb&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
* spec/controllers/invitations_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;br /&gt;
&lt;br /&gt;
== Code Details == &lt;br /&gt;
Github Pull Request - https://github.com/expertiza/expertiza/pull/2391&lt;br /&gt;
&lt;br /&gt;
Repo - https://github.com/sak1997/expertiza/tree/beta&lt;br /&gt;
&lt;br /&gt;
Demo - https://drive.google.com/file/d/1RXINSdJYUv3Bd25epE8AP7vJgM6UXche/view (accessible only by users with ncsu.edu email address)&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145326</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145326"/>
		<updated>2022-04-26T00:23:30Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Automated Testing RSpec */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id. Ideally, the application should use only participant_id, but to handle the existing data in the database without running a massive migration, the application should be able to handle both old records that use user_id and new ones that shall, from after this project is completed, use participant_id instead.&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
* However, it is to be noted that this must also work with the existing functionality and data which makes use of user_id, so the code written must take both into account in its working.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored as this view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb :&lt;br /&gt;
&lt;br /&gt;
* ''object initialization''&lt;br /&gt;
* ''#update_duties''&lt;br /&gt;
* ''when user is added to assignment team''&lt;br /&gt;
* ''when user is added to assignment team''&lt;br /&gt;
* ''when user is added to course team''&lt;br /&gt;
* ''when user is already on an assignment team''&lt;br /&gt;
* ''when user is added to assignment team while on a team already'' &lt;br /&gt;
* ''when user is already on a course team'' &lt;br /&gt;
&lt;br /&gt;
the new methods which use both participant_id and user_id have been incorporated as needed to ensure continued quality of the testing suite and maintenance of code coverage.&lt;br /&gt;
&lt;br /&gt;
==== Fixed Test Files ====&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145325</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145325"/>
		<updated>2022-04-26T00:21:43Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Refactoring the teams rendering (/views/student_teams/view.html.erb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id. Ideally, the application should use only participant_id, but to handle the existing data in the database without running a massive migration, the application should be able to handle both old records that use user_id and new ones that shall, from after this project is completed, use participant_id instead.&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
* However, it is to be noted that this must also work with the existing functionality and data which makes use of user_id, so the code written must take both into account in its working.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored as this view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' the new methods which use both participant_id and user_id are to be incorporated to ensure continued quality of testing.&lt;br /&gt;
&lt;br /&gt;
==== Fixed Test Files ====&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145324</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145324"/>
		<updated>2022-04-26T00:21:12Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Changing the schema */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id. Ideally, the application should use only participant_id, but to handle the existing data in the database without running a massive migration, the application should be able to handle both old records that use user_id and new ones that shall, from after this project is completed, use participant_id instead.&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
* However, it is to be noted that this must also work with the existing functionality and data which makes use of user_id, so the code written must take both into account in its working.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' the new methods which use both participant_id and user_id are to be incorporated to ensure continued quality of testing.&lt;br /&gt;
&lt;br /&gt;
==== Fixed Test Files ====&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145321</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145321"/>
		<updated>2022-04-26T00:13:32Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Effects of code changes in the Expertiza Application */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id. Ideally, the application should use only participant_id, but to handle the existing data in the database without running a massive migration, the application should be able to handle both old records that use user_id and new ones that shall, from after this project is completed, use participant_id instead.&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' the new methods which use both participant_id and user_id are to be incorporated to ensure continued quality of testing.&lt;br /&gt;
&lt;br /&gt;
==== Fixed Test Files ====&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145318</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145318"/>
		<updated>2022-04-26T00:10:43Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Given Requirements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id. Ideally, the application should use only participant_id, but to handle the existing data in the database without running a massive migration, the application should be able to handle both old records that use user_id and new ones that shall, from after this project is completed, use participant_id instead.&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' the new methods which use both participant_id and user_id are to be incorporated to ensure continued quality of testing.&lt;br /&gt;
&lt;br /&gt;
==== Fixed Test Files ====&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145317</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145317"/>
		<updated>2022-04-26T00:10:30Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Other Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id. Ideally, the application should use only participant_id, but to handle the existing data in the database without running a massive migration, the application should be able to handle both old records that use user_id and new ones that shall, from after this project is completed, use participant_id instead.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' the new methods which use both participant_id and user_id are to be incorporated to ensure continued quality of testing.&lt;br /&gt;
&lt;br /&gt;
==== Fixed Test Files ====&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145316</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145316"/>
		<updated>2022-04-26T00:09:36Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Automated Testing RSpec */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id. Ideally, the application should use only participant_id, but to handle the existing data in the database without running a massive migration, the application should be able to handle both old records that use user_id and new ones that shall, from after this project is completed, use participant_id instead.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' the new methods which use both participant_id and user_id are to be incorporated to ensure continued quality of testing.&lt;br /&gt;
&lt;br /&gt;
==== Fixed Test Files ====&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145315</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145315"/>
		<updated>2022-04-26T00:08:23Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Fixed Test Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id. Ideally, the application should use only participant_id, but to handle the existing data in the database without running a massive migration, the application should be able to handle both old records that use user_id and new ones that shall, from after this project is completed, use participant_id instead.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified.&lt;br /&gt;
&lt;br /&gt;
==== Fixed Test Files ====&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145314</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145314"/>
		<updated>2022-04-26T00:08:06Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Automated Testing RSpec */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id. Ideally, the application should use only participant_id, but to handle the existing data in the database without running a massive migration, the application should be able to handle both old records that use user_id and new ones that shall, from after this project is completed, use participant_id instead.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified.&lt;br /&gt;
&lt;br /&gt;
=== Fixed Test Files ===&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145312</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145312"/>
		<updated>2022-04-26T00:06:04Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Background */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id. Ideally, the application should use only participant_id, but to handle the existing data in the database without running a massive migration, the application should be able to handle both old records that use user_id and new ones that shall, from after this project is completed, use participant_id instead.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
=== Fixed Test Files ===&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145310</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145310"/>
		<updated>2022-04-26T00:04:27Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Fixed Test Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
=== Fixed Test Files ===&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the below files, and we have worked on fixing these to work with the new and updated functionality. The main issues were involving tests using the old methods for creating a team, adding participants to the team etc. &lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145308</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145308"/>
		<updated>2022-04-26T00:03:17Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Manual Testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
In addition to verifying team creation in all of the above scenarios, we also verified other operations such as addition of a team member after the team capacity was exhausted, deletion and re-addition of the same user, invitations to already existing participants in the same team etc and ensure the system was working as expected in these scenarios as well.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
=== Fixed Test Files ===&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the following files, and we have worked on fixing these to work with the new and updated functionality:&lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145305</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145305"/>
		<updated>2022-04-26T00:01:47Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Manual Testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# Instructor creates teams automatically: An instructor wishes to assign all the students involved in an assignment randomly to teams of a certain size. They open expertiza, choose the assignment, and then click on the icon to add a new team, and in the &amp;quot;create teams automatically&amp;quot; section, enter the team size they wish to use. The system then creates teams of the specified size, adding students randomly to teams which is reflected in both student and instructor views.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
=== Fixed Test Files ===&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the following files, and we have worked on fixing these to work with the new and updated functionality:&lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145303</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145303"/>
		<updated>2022-04-25T23:59:56Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: i&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
&lt;br /&gt;
The changes in teams_users means we need to re-examine the flows and user stories through where teams_users is used. This, in practice, means that we manually have checked the different ways in which a team for an assignment can be created in expertiza, and other associated operations such as removal of a user from a team, addition of a new user, deletion of a team etc. This testing was necessary to ensure that the functionality in expertiza still worked as expected even after our refactoring was completed. &lt;br /&gt;
&lt;br /&gt;
The main user stories we examined are:&lt;br /&gt;
&lt;br /&gt;
# Student creates a team: A student, newly added to an assignment, opens the application and goes to &amp;quot;my team&amp;quot; after opening the assignment on expertiza. He enters his team's name and the team is created.&lt;br /&gt;
# Student joins a team: A student, is invited to join a team for an assignment, and accepts. He is added to an existing team, possibly created by another student or instructor.&lt;br /&gt;
# Instructor creates a team manually: An instructor who wishes to create a team with certain students on it, chooses the assignment on expertiza, and clicks on the icon to add a new team and in the manual section, creates a new team by giving a team name. They add students or participants to the newly created team and can remove them too.&lt;br /&gt;
# &lt;br /&gt;
&lt;br /&gt;
There is limited scope for testing this feature manually, as the refactoring targets computing participant_id in the teams_users table. This will improve the performance, and will not be shown in the user's view.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
=== Fixed Test Files ===&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the following files, and we have worked on fixing these to work with the new and updated functionality:&lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145302</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145302"/>
		<updated>2022-04-25T23:51:14Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Other Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Updated logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Fixed tests to incorporate the new logic as a result of using both user_id and participant_id in the teams_user logic.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
There is limited scope for testing this feature manually, as the refactoring targets computing participant_id in the teams_users table. This will improve the performance, and will not be shown in the user's view.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
=== Fixed Test Files ===&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the following files, and we have worked on fixing these to work with the new and updated functionality:&lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145300</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145300"/>
		<updated>2022-04-25T23:50:32Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Refactoring the teams rendering (/views/student_teams/view.html.erb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We have made partials and placed those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
There is limited scope for testing this feature manually, as the refactoring targets computing participant_id in the teams_users table. This will improve the performance, and will not be shown in the user's view.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
=== Fixed Test Files ===&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the following files, and we have worked on fixing these to work with the new and updated functionality:&lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145299</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145299"/>
		<updated>2022-04-25T23:50:09Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Given Requirements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Use this participant_id field in all operations for teams going forward. There are a number of workflows involving teams we have examined and worked on, which are elaborated below.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
There is limited scope for testing this feature manually, as the refactoring targets computing participant_id in the teams_users table. This will improve the performance, and will not be shown in the user's view.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
=== Fixed Test Files ===&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the following files, and we have worked on fixing these to work with the new and updated functionality:&lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145094</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145094"/>
		<updated>2022-04-24T22:15:31Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== View Changes and Refactoring ==&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
There is limited scope for testing this feature manually, as the refactoring targets computing participant_id in the teams_users table. This will improve the performance, and will not be shown in the user's view.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
=== Fixed Test Files ===&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the following files, and we have worked on fixing these to work with the new and updated functionality:&lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145093</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145093"/>
		<updated>2022-04-24T22:14:03Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Team Members */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
==== View Changes and Refactoring ====&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
There is limited scope for testing this feature manually, as the refactoring targets computing participant_id in the teams_users table. This will improve the performance, and will not be shown in the user's view.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
=== Fixed Test Files ===&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the following files, and we have worked on fixing these to work with the new and updated functionality:&lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
Mentor: Naman Shrimali&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145092</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145092"/>
		<updated>2022-04-24T22:13:17Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Test Plan */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
==== View Changes and Refactoring ====&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
There is limited scope for testing this feature manually, as the refactoring targets computing participant_id in the teams_users table. This will improve the performance, and will not be shown in the user's view.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
=== Fixed Test Files ===&lt;br /&gt;
&lt;br /&gt;
After our refactoring, we noticed failing tests in the following files, and we have worked on fixing these to work with the new and updated functionality:&lt;br /&gt;
&lt;br /&gt;
Models:&lt;br /&gt;
* spec/models/assignment_team_spec.rb&lt;br /&gt;
* spec/models/team_spec.rb&lt;br /&gt;
&lt;br /&gt;
Helper:&lt;br /&gt;
* spec/helpers/authorization_helper_spec.rb&lt;br /&gt;
&lt;br /&gt;
Controllers:&lt;br /&gt;
* spec/controllers/join_team_request_controller_spec.rb&lt;br /&gt;
* spec/controllers/student_teams_controller_spec.rb&lt;br /&gt;
* spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
* spec/controllers/lottery_controller_spec.rb&lt;br /&gt;
* spec/controllers/sign_up_sheet_controller_spec.rb&lt;br /&gt;
* spec/controllers/submitted_content_controller_spec.rb&lt;br /&gt;
* spec/controllers/teams_users_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145087</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145087"/>
		<updated>2022-04-24T22:05:37Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Changing the schema */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
==== View Changes and Refactoring ====&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
There is limited scope for testing this feature manually, as the refactoring targets computing participant_id in the teams_users table. This will improve the performance, and will not be shown in the user's view.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145086</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=145086"/>
		<updated>2022-04-24T22:05:18Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: added content to view section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Updated_Proposed_ERR_for_student_teams_E2243.png |900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
==== View Changes and Refactoring ====&lt;br /&gt;
&lt;br /&gt;
One of the requirements in E2243 is the refactoring of the student teams view in views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
This view is quite cluttered and as a result, makes editing, understanding and extension difficult and tiresome for any future updates to the expertiza application. The code in this file can be modularized to obtain simpler and easier-to-understand, which is what we have achieved. The view was reafactored to incorporate partials, and some of the partials created as a result are:&lt;br /&gt;
&lt;br /&gt;
* _invited_people.html.erb : partial for displaying the invited participants to the assignment team&lt;br /&gt;
* _display_advertisements.html.erb : partial for displaying advertisements in the view&lt;br /&gt;
* _send_invitaions.html.erb : partial for the send invitations functionality&lt;br /&gt;
* _received_invitations.html.erb : partial to display the received invitations for the current user&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller_spec.rb(expertiza/spec/controllers/teams_users_controller_spec.rb)&lt;br /&gt;
The test file for the controller will be modified so that existing test cases pass the new changes, and also introduce new test cases to increase test coverage.&lt;br /&gt;
&lt;br /&gt;
== Test Plan ==&lt;br /&gt;
&lt;br /&gt;
=== Manual Testing ===&lt;br /&gt;
There is limited scope for testing this feature manually, as the refactoring targets computing participant_id in the teams_users table. This will improve the performance, and will not be shown in the user's view.&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing RSpec ===&lt;br /&gt;
Since this is a refactoring project, the scope of testing is limited to the changes made as part of refactoring and also to ensure that the existing test cases do not break or are modified accordingly.As per our proposed design, we're working on adding participant_id computation, which will not be used on the views for the end user.&lt;br /&gt;
&lt;br /&gt;
In all the existing test cases in expertiza/spec/controllers/teams_users_controller_spec.rb -- ''object initialization'', ''#update_duties'', ''when user is added to assignment team'', ''when user is added to assignment team'', ''when user is added to course team'', ''when user is already on an assignment team'', ''when user is added to assignment team while on a team already'' and ''when user is already on a course team'' participant_id needs to be incorporated and modified. &lt;br /&gt;
&lt;br /&gt;
The team is also planning to add more test scenarios based on the implementation of the proposed solution to increase the test coverage.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Team Members ==&lt;br /&gt;
# Ashok Kumar Selvam (aselvam@ncsu.edu)&lt;br /&gt;
# Sri Athithya Kruth Babu (sbabu@ncsu.edu)&lt;br /&gt;
# Subramanian Venkataraman (svenka25@ncsu.edu)&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144887</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144887"/>
		<updated>2022-04-12T02:28:26Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Impacted Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
==== View Changes ====&lt;br /&gt;
&lt;br /&gt;
The existing views in relation to this functionality would not need to undergo signification refactoring since we are only adding a new field and removing any of the existing ones. That being said, we will be adding new partials to achieve similar views using the participant_id field instead of the user_id field in the partials for future changes to the application as mentioned in the specifications.&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;br /&gt;
The view will be updated to reflect the new name of the model, and contain new partials that make use of the participant_id field.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144884</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144884"/>
		<updated>2022-04-12T02:27:50Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* View Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
==== View Changes ====&lt;br /&gt;
&lt;br /&gt;
The existing views in relation to this functionality would not need to undergo signification refactoring since we are only adding a new field and removing any of the existing ones. That being said, we will be adding new partials to achieve similar views using the participant_id field instead of the user_id field in the partials for future changes to the application as mentioned in the specifications.&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144879</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144879"/>
		<updated>2022-04-12T02:23:36Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Proposed Implementation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
==== View Changes ====&lt;br /&gt;
&lt;br /&gt;
The existing views in relation to this functionality would not need to undergo signification refactoring, since we are only adding a new field and removing any of the existing ones.&lt;br /&gt;
That being said, we will be adding new partials to achieve the similar views using the participant_id field instead of the user_id field in the partials for future changes to the application as mentioned in the specifications.&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144846</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144846"/>
		<updated>2022-04-12T01:34:26Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: added effects section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
== Effects of code changes in the Expertiza Application ==&lt;br /&gt;
&lt;br /&gt;
While the renaming of the table to teams_participants has rippling effects throughout the program since multiple files contain calls to this class, It must be noted that not much else needs to change apart from the class name. In most cases, only the class name would be changed since we are primarily adding a new field(participant_id) and not removing any existing fields. &lt;br /&gt;
&lt;br /&gt;
In most use cases noted in the application for the teams_users class, the main use cases seen are computed the team_id from the user_id and assignment_id or manipulations on the users in a teams_user object - adding and removing a team members from a team, for instance.&lt;br /&gt;
&lt;br /&gt;
The changes we plan to make here will not cause a need for changes in other parts of the application apart from the replacement of the class name to teams_participants.&lt;br /&gt;
&lt;br /&gt;
== Impacted Files ==&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144811</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144811"/>
		<updated>2022-04-12T01:17:18Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Analysis */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they crrespond to same attribute, user_id alone cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
=== Impacted Files ===&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144809</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144809"/>
		<updated>2022-04-12T01:15:57Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Background */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
As given in the specifications for the project, the name of the table &amp;quot;teams_users&amp;quot; should be changed to &amp;quot;teams_participants&amp;quot;. The participant_id field should be added to the model, and references that use user_id should also incorporate participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they ocrrespond to same attribute, it cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
=== Impacted Files ===&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022&amp;diff=144594</id>
		<title>CSC/ECE 517 Spring 2022</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022&amp;diff=144594"/>
		<updated>2022-04-07T20:38:28Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== OSS Projects ==&lt;br /&gt;
&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2200: Testing advice_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2203: Adding tests for courses_controller, eula_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2204: Adding tests for markup_styles_controller, lock_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2208: Testing for submission_records_controller, profile_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2218: Refactor response_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2217: Refactor questionnaires_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2216: Refactor late_policies_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2215: Refactor student_quizzes_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2214: Refactor teams_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2219: Improve assessment360_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2211: Testing for summary_helper]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2225: Refactor review_mapping_helper.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2221: Refactor submitted content controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2206: Testing for users_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2207: Testing for submitted_content_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2205: Testing for participants_controller, versions_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2212: Testing for hamer.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - S2200: Refactoring evaluation of SQL queries (Java/JUnit)]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - S2222: Refactor impersonate_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2202- Testing for badges_controller, publishing_controller]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2022 - E2220: Refactor reputation_web_service_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2224: Refactor review_mapping_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2209: Testing for analytic helper.rb, join team requests helper.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2227: SQL Injection Fix]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2223. Refactor sign up sheet controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2201: Testing for assignment_questionnaire_controller.rb]]&lt;br /&gt;
&lt;br /&gt;
== Final Projects ==&lt;br /&gt;
&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2245: View for results of bidding]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2241: Heatgrid fixes and improvements]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2237: Grading audit trail]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - S2201: Improving User Experience for SQLFE]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2236: Fix issues related to deadlines and late policies]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2231: Allow reviewers to bid on what to review]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2233. Improving search facility in Expertiza]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2234. Calibration submissions should be copied along with calibration assignments]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2244. Support for saying your team pair-programmed]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2246: Email your authors/reviewers, with complete tests]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2229: Track the time students look at other submissions]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2239: Further refactoring and improvement of review mapping helper.rb ]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2240. Re-write waitlist functionality ]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2242. Fix teammate-review view ]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2243. Refactor student_teams functionality]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2235. Issues related to meta-reviewing]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2230. Integrate Suggestion Detection Algorithm]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2238. Implementing and testing Import &amp;amp; Export controllers]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2228. Refactor JavaScript on Expertiza]]&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144529</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144529"/>
		<updated>2022-04-07T02:03:10Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Impacted Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
teams_users should be changed to teams_participants. the team_id, user_id mapping in this table should be changed to team_id, participant_id. All the references of user_id should be updated to use participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they ocrrespond to same attribute, it cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
=== Impacted Files ===&lt;br /&gt;
&lt;br /&gt;
* teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
* teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
* teams_users(expertiza/app/views/teams_users)&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144528</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=144528"/>
		<updated>2022-04-07T02:01:49Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Impacted Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
teams_users should be changed to teams_participants. the team_id, user_id mapping in this table should be changed to team_id, participant_id. All the references of user_id should be updated to use participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|900px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
''user_id'' exists as a foreign key in 2 relations - Participants and teams_users. Although they ocrrespond to same attribute, it cannot be used to find corresponding Participants because same user_id can exist for multiple Participants - due to Expertiza's design wherein for every assignment, every user is added to participant's table. Therefore Participant ID can be mapped to (assignment_id, user_id) pair.&lt;br /&gt;
&lt;br /&gt;
== Proposed Implementation ==&lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID using Team ID ====&lt;br /&gt;
&lt;br /&gt;
Each team is associated with an assignment. The parent_id in teams table corresponds to assignment_id. Therefore, team_id in teams_users table can be used to get the Team object and in turn fetch the Assignment ID (present as parent_id in the teams table) &lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Approach One ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Approach Two ===&lt;br /&gt;
In the brute force approach (Approach One), database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
=== Impacted Files ===&lt;br /&gt;
&lt;br /&gt;
# teams_users_controller.rb (expertiza/app/controller/teams_user_controller.rb)&lt;br /&gt;
We will be adding code here to compute the participant_id using the methodologies described above.&lt;br /&gt;
&lt;br /&gt;
# teams_users.rb (expertiza/app/models/teams_user.rb)&lt;br /&gt;
The model will be updated (as can be seen in the ER diagram above) to have the participant_id added.&lt;br /&gt;
&lt;br /&gt;
# teams_users(expertiza/app/views/teams_users)&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=143939</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=143939"/>
		<updated>2022-04-04T00:18:36Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Schema Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
teams_users should be changed to teams_participants. the team_id, user_id mapping in this table should be changed to team_id, participant_id. All the references of user_id should be updated to use participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
* Get rid of the user_id column.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. We plan to remove the '''user_id''' column '''only after data migration''' since the user_id field is used predominantly in several other controllers in a variety of ways, and as such its removal must be done carefully.&lt;br /&gt;
&lt;br /&gt;
The participant_id field in the the teams_users relation is computed as follows:&lt;br /&gt;
&lt;br /&gt;
* Finding Assignment ID By Team ID&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id but not the participant_id. Since the same user_id can be associated with multiple participant_ids (since the same user can participate in multiple assignments and gets a unique participant_id in each assignment they take part in), we need to get the user_ids in the assignment for which we need the particular user's participant_id. For this reason, we need to first find the assignment_id.&lt;br /&gt;
&lt;br /&gt;
* Finding Participant ID By Assignment ID and User ID&lt;br /&gt;
With the assignment_id retrieved, we can find the list of participant_ids that participated in the assignment, and using the user_id, get the participant_id which is saved in the participants table. &lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
The Participants relation contains the user_id attribute which is a foreign key from users table. teams_users contains user_id which is also a foreign key from users table. Although the user_id field corresponds to same attribute, we cannot use this attribute alone to find the respective participant id because user_id is not a primary key in the participants table. i.e. same user_id can be associated with multiple participants. This is because in expertiza, all the users are stored in users table but, for each assignment, for each user, an entry is added in participants table. So, each user will have an entry in participants tables for all the assignments. &lt;br /&gt;
&lt;br /&gt;
An Assignment contains a list of participants (An Assignment has many Participants). Since a user can have only one entry in the participants table for each assignment, (assignment_id, user_id) pair will have only one participant id associated with it. &lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID By Team ID ====&lt;br /&gt;
&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id. Each team is associated with an assignment. The parent_id attribute in the teams table corresponds to assignment id. So, team_id in the teams_users table can be used to get the Team object using which we can fetch the assignment ID (parent_id).&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Brute Force Data Migration ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Efficient Data Migration ===&lt;br /&gt;
In the brute force approach, database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
In this approach,  we make a total of 3 database calls which is considerably better than the previous brute-force approach.&lt;br /&gt;
&lt;br /&gt;
== Code Changes ==&lt;br /&gt;
The user_id attribute is used in multiple places within the application. Since, the new schema changes do not include user_id attribute in teams_users table, all the references of user_id must be updated to use participant_id, which is its new equivalent instead.&lt;br /&gt;
&lt;br /&gt;
=== Impacted Files ===&lt;br /&gt;
&lt;br /&gt;
# teams_users_controller&lt;br /&gt;
# suggestion_controller&lt;br /&gt;
# assessment360_controller&lt;br /&gt;
# grades_controller&lt;br /&gt;
# join_team_requests_controller&lt;br /&gt;
# review_mapping_controller&lt;br /&gt;
# student_teams_controller&lt;br /&gt;
&lt;br /&gt;
The respective test cases associated with these files must be fixed before fixing the code so that we can make sure that the proposed refactoring does not raise any new bugs.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=143906</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=143906"/>
		<updated>2022-04-01T23:57:29Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Changing the schema */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
teams_users should be changed to teams_participants. the team_id, user_id mapping in this table should be changed to team_id, participant_id. All the references of user_id should be updated to use participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
* We need to rename the table table teams_users to teams_participants.&lt;br /&gt;
* Create a column that would be called “participant_id”, which will be a foreign key that references the participants table.&lt;br /&gt;
* Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
* Get rid of the user_id column.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. Remove the '''user_id''' column '''only after data migration'''&lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
The Participants relation contains the user_id attribute which is a foreign key from users table. teams_users contains user_id which is also a foreign key from users table. Although the user_id field corresponds to same attribute, we cannot use this attribute alone to find the respective participant id because user_id is not a primary key in the participants table. i.e. same user_id can be associated with multiple participants. This is because in expertiza, all the users are stored in users table but, for each assignment, for each user, an entry is added in participants table. So, each user will have an entry in participants tables for all the assignments. &lt;br /&gt;
&lt;br /&gt;
An Assignment contains a list of participants (An Assignment has many Participants). Since a user can have only one entry in the participants table for each assignment, (assignment_id, user_id) pair will have only one participant id associated with it. &lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID By Team ID ====&lt;br /&gt;
&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id. Each team is associated with an assignment. The parent_id attribute in the teams table corresponds to assignment id. So, team_id in the teams_users table can be used to get the Team object using which we can fetch the assignment ID (parent_id).&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Brute Force Data Migration ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Efficient Data Migration ===&lt;br /&gt;
In the brute force approach, database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
In this approach,  we make a total of 3 database calls which is considerably better than the previous brute-force approach.&lt;br /&gt;
&lt;br /&gt;
== Code Changes ==&lt;br /&gt;
The user_id attribute is used in multiple places within the application. Since, the new schema changes do not include user_id attribute in teams_users table, all the references of user_id must be updated to use participant_id, which is its new equivalent instead.&lt;br /&gt;
&lt;br /&gt;
=== Impacted Files ===&lt;br /&gt;
&lt;br /&gt;
# teams_users_controller&lt;br /&gt;
# suggestion_controller&lt;br /&gt;
# assessment360_controller&lt;br /&gt;
# grades_controller&lt;br /&gt;
# join_team_requests_controller&lt;br /&gt;
# review_mapping_controller&lt;br /&gt;
# student_teams_controller&lt;br /&gt;
&lt;br /&gt;
The respective test cases associated with these files must be fixed before fixing the code so that we can make sure that the proposed refactoring does not raise any new bugs.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=143905</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=143905"/>
		<updated>2022-04-01T23:56:39Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Background */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the CSC517 OODD class at NC State is a user in Expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit their work to be graded.&lt;br /&gt;
&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. All the tables use participant_id to track contributions to assignments, except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants. This anomaly in the teams_users relation causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
teams_users should be changed to teams_participants. the team_id, user_id mapping in this table should be changed to team_id, participant_id. All the references of user_id should be updated to use participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
We need to rename the table table teams_users to teams_participants&lt;br /&gt;
Create a column that would be called “participant_id”, which will be a foreign key that references participants table.&lt;br /&gt;
Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
Get rid of the user_id column.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. Remove the '''user_id''' column '''only after data migration'''&lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
The Participants relation contains the user_id attribute which is a foreign key from users table. teams_users contains user_id which is also a foreign key from users table. Although the user_id field corresponds to same attribute, we cannot use this attribute alone to find the respective participant id because user_id is not a primary key in the participants table. i.e. same user_id can be associated with multiple participants. This is because in expertiza, all the users are stored in users table but, for each assignment, for each user, an entry is added in participants table. So, each user will have an entry in participants tables for all the assignments. &lt;br /&gt;
&lt;br /&gt;
An Assignment contains a list of participants (An Assignment has many Participants). Since a user can have only one entry in the participants table for each assignment, (assignment_id, user_id) pair will have only one participant id associated with it. &lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID By Team ID ====&lt;br /&gt;
&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id. Each team is associated with an assignment. The parent_id attribute in the teams table corresponds to assignment id. So, team_id in the teams_users table can be used to get the Team object using which we can fetch the assignment ID (parent_id).&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Brute Force Data Migration ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Efficient Data Migration ===&lt;br /&gt;
In the brute force approach, database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
In this approach,  we make a total of 3 database calls which is considerably better than the previous brute-force approach.&lt;br /&gt;
&lt;br /&gt;
== Code Changes ==&lt;br /&gt;
The user_id attribute is used in multiple places within the application. Since, the new schema changes do not include user_id attribute in teams_users table, all the references of user_id must be updated to use participant_id, which is its new equivalent instead.&lt;br /&gt;
&lt;br /&gt;
=== Impacted Files ===&lt;br /&gt;
&lt;br /&gt;
# teams_users_controller&lt;br /&gt;
# suggestion_controller&lt;br /&gt;
# assessment360_controller&lt;br /&gt;
# grades_controller&lt;br /&gt;
# join_team_requests_controller&lt;br /&gt;
# review_mapping_controller&lt;br /&gt;
# student_teams_controller&lt;br /&gt;
&lt;br /&gt;
The respective test cases associated with these files must be fixed before fixing the code so that we can make sure that the proposed refactoring does not raise any new bugs.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=143899</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=143899"/>
		<updated>2022-04-01T14:14:58Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: Added requirements&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the OODD class is a user in expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit.&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. That is, everything except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants.&lt;br /&gt;
This anomaly causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
teams_users should be changed to teams_participants. the team_id, user_id mapping in this table should be changed to team_id, participant_id. All the references of user_id should be updated to use participant_id.&lt;br /&gt;
&lt;br /&gt;
== Given Requirements ==&lt;br /&gt;
&lt;br /&gt;
===Changing the schema ===&lt;br /&gt;
We need to rename the table table teams_users to teams_participants&lt;br /&gt;
Create a column that would be called “participant_id”, which will be a foreign key that references participants table.&lt;br /&gt;
Create a migration that fetches the assignment_id for each tuple from the teams table. We would find the participant with the help of the assignment_id we just got from the teams table and the user_id that is already present in the current table, and store its “id” in the participant_id column we created in the last step.&lt;br /&gt;
Get rid of the user_id column.&lt;br /&gt;
&lt;br /&gt;
=== Refactoring the teams rendering (/views/student_teams/view.html.erb) ===&lt;br /&gt;
The logic in the team's view (where participants can see their team members) that iterates and fetches information of all the team members and displays them would be refactored so that it matches our new design. &lt;br /&gt;
The view is quite bloated and has lots of functionalities written on it. We’ll be making partials and placing those functionality in their specific partials.&lt;br /&gt;
&lt;br /&gt;
=== Other Changes ===&lt;br /&gt;
&lt;br /&gt;
Update logic for all the crud functionalities in teams_users controller (wherever applicable).&lt;br /&gt;
Rename the teams_users_controller to teams_participants_controller (and all its references)&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. Remove the '''user_id''' column '''only after data migration'''&lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
The Participants relation contains the user_id attribute which is a foreign key from users table. teams_users contains user_id which is also a foreign key from users table. Although the user_id field corresponds to same attribute, we cannot use this attribute alone to find the respective participant id because user_id is not a primary key in the participants table. i.e. same user_id can be associated with multiple participants. This is because in expertiza, all the users are stored in users table but, for each assignment, for each user, an entry is added in participants table. So, each user will have an entry in participants tables for all the assignments. &lt;br /&gt;
&lt;br /&gt;
An Assignment contains a list of participants (An Assignment has many Participants). Since a user can have only one entry in the participants table for each assignment, (assignment_id, user_id) pair will have only one participant id associated with it. &lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID By Team ID ====&lt;br /&gt;
&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id. Each team is associated with an assignment. The parent_id attribute in the teams table corresponds to assignment id. So, team_id in the teams_users table can be used to get the Team object using which we can fetch the assignment ID (parent_id).&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Brute Force Data Migration ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Efficient Data Migration ===&lt;br /&gt;
In the brute force approach, database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
In this approach,  we make a total of 3 database calls which is considerably better than the previous brute-force approach.&lt;br /&gt;
&lt;br /&gt;
== Code Changes ==&lt;br /&gt;
The user_id attribute is used in multiple places within the application. Since, the new schema changes do not include user_id attribute in teams_users table, all the references of user_id must be updated to use participant_id, which is its new equivalent instead.&lt;br /&gt;
&lt;br /&gt;
=== Impacted Files ===&lt;br /&gt;
&lt;br /&gt;
# teams_users_controller&lt;br /&gt;
# suggestion_controller&lt;br /&gt;
# assessment360_controller&lt;br /&gt;
# grades_controller&lt;br /&gt;
# join_team_requests_controller&lt;br /&gt;
# review_mapping_controller&lt;br /&gt;
# student_teams_controller&lt;br /&gt;
&lt;br /&gt;
The respective test cases associated with these files must be fixed before fixing the code so that we can make sure that the proposed refactoring does not raise any new bugs.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=143898</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=143898"/>
		<updated>2022-04-01T13:59:10Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: edited for clarity&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the design of the project.&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Users &amp;amp; Participants: Everyone who took the OODD class is a user in expertiza, and instructors can add users as participants in an assignment so that they can access it to contribute, make changes and submit.&lt;br /&gt;
Almost all of the works within Assignments is done by participants, not users. That is, everything except teams_users. In the documentation, it can be noted that the team_users table references users instead of participants.&lt;br /&gt;
This anomaly causes problems with how student teams are rendered in the UI, and it doesn’t mix well with the new functionality that was recently introduced.&lt;br /&gt;
&lt;br /&gt;
teams_users should be changed to teams_participants. the team_id, user_id mapping in this table should be changed to team_id, participant_id. All the references of user_id should be updated to use participant_id.&lt;br /&gt;
&lt;br /&gt;
== Schema Changes ==&lt;br /&gt;
&lt;br /&gt;
In teams_users table, we can add a new column '''participant_id''' which is a foreign key from participants table and remove the column '''user_id'''. Remove the '''user_id''' column '''only after data migration'''&lt;br /&gt;
&lt;br /&gt;
=== ER diagram for updated schema ===&lt;br /&gt;
[[File:Proposed_ERR_for_student_teams_E2243.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Data Migration ==&lt;br /&gt;
=== Analysis ===&lt;br /&gt;
&lt;br /&gt;
The Participants relation contains the user_id attribute which is a foreign key from users table. teams_users contains user_id which is also a foreign key from users table. Although the user_id field corresponds to same attribute, we cannot use this attribute alone to find the respective participant id because user_id is not a primary key in the participants table. i.e. same user_id can be associated with multiple participants. This is because in expertiza, all the users are stored in users table but, for each assignment, for each user, an entry is added in participants table. So, each user will have an entry in participants tables for all the assignments. &lt;br /&gt;
&lt;br /&gt;
An Assignment contains a list of participants (An Assignment has many Participants). Since a user can have only one entry in the participants table for each assignment, (assignment_id, user_id) pair will have only one participant id associated with it. &lt;br /&gt;
&lt;br /&gt;
==== Finding Assignment ID By Team ID ====&lt;br /&gt;
&lt;br /&gt;
The teams_users table contains team_id, user_id and duty_id. Each team is associated with an assignment. The parent_id attribute in the teams table corresponds to assignment id. So, team_id in the teams_users table can be used to get the Team object using which we can fetch the assignment ID (parent_id).&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch assignment_id based on team_id&lt;br /&gt;
 assignment_id = Team.find(team_id).select(:parent_id)&lt;br /&gt;
&lt;br /&gt;
==== Finding Participant ID By Assignment ID and User ID ====&lt;br /&gt;
&lt;br /&gt;
The Assignment object can be fetched using the assignment ID. Each assignment contains a list of participants. The Participant object contains a user_id attribute. Since, a user can have only one entry in the Participants table for an assignment, we can use user_id to find the user's participant_id from this list.&lt;br /&gt;
&lt;br /&gt;
 # Code snippet to fetch participant_id based on assignment_id and user_id&lt;br /&gt;
 participant_id = Assignment.find(assignment_id).participants.find_by(:user_id user_id)&lt;br /&gt;
&lt;br /&gt;
=== Brute Force Data Migration ===&lt;br /&gt;
&lt;br /&gt;
The above mentioned code snippets can be used for each tuple in the teams_users table.&lt;br /&gt;
&lt;br /&gt;
 # Brute Force Code Snippet&lt;br /&gt;
 TeamsUser.all.each do |teamsUser|&lt;br /&gt;
     assignment_id = Team.find(teamsUser.team_id).select(:parent_id)&lt;br /&gt;
     participant_id = Assignment.find(assignment_id).participants.find_by(:user_id teamsUser.user_id)&lt;br /&gt;
     teamsUser.update_attribute(:participant_id, participant_id)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
=== Efficient Data Migration ===&lt;br /&gt;
In the brute force approach, database calls are made multiple times inside the loop. Moving the database calls out of the loop will give us a more efficient solution.&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
&lt;br /&gt;
# Load all the TeamUser objects.&lt;br /&gt;
# Load all the Team objects for all the team_ids in TeamUser object&lt;br /&gt;
# Create a map with team_id as key and assignment_id as value. The resultant map usage: map[team_id] = assignment_id&lt;br /&gt;
# Load all the Assignment objects for all the assignment_ids from the map in the previous step. Also eager fetch participants along with the assignments.&lt;br /&gt;
# Create a nested map with assignment_id and user_id as key and participant_id as value. The resultant map usage: map[assignment_id][user_id] = participant_id&lt;br /&gt;
# For all TeamUser objects, find the participant_id from the maps generated from the previous steps and update the participant_id attribute.&lt;br /&gt;
&lt;br /&gt;
In this approach,  we make a total of 3 database calls which is considerably better than the previous brute-force approach.&lt;br /&gt;
&lt;br /&gt;
== Code Changes ==&lt;br /&gt;
The user_id attribute is used in multiple places within the application. Since, the new schema changes do not include user_id attribute in teams_users table, all the references of user_id must be updated to use participant_id, which is its new equivalent instead.&lt;br /&gt;
&lt;br /&gt;
=== Impacted Files ===&lt;br /&gt;
&lt;br /&gt;
# teams_users_controller&lt;br /&gt;
# suggestion_controller&lt;br /&gt;
# assessment360_controller&lt;br /&gt;
# grades_controller&lt;br /&gt;
# join_team_requests_controller&lt;br /&gt;
# review_mapping_controller&lt;br /&gt;
# student_teams_controller&lt;br /&gt;
&lt;br /&gt;
The respective test cases associated with these files must be fixed before fixing the code so that we can make sure that the proposed refactoring does not raise any new bugs.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=143849</id>
		<title>CSC/ECE 517 Spring 2022 - E2243. Refactor student teams functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2243._Refactor_student_teams_functionality&amp;diff=143849"/>
		<updated>2022-03-31T01:38:09Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: created page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2243. Refactor student teams functionality which was a project in CSC517 Spring 2022. &lt;br /&gt;
&lt;br /&gt;
Please see below for a description of the project.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022&amp;diff=143848</id>
		<title>CSC/ECE 517 Spring 2022</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022&amp;diff=143848"/>
		<updated>2022-03-31T01:37:12Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* OSS Projects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== OSS Projects ==&lt;br /&gt;
&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2200: Testing advice_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2203: Adding tests for courses_controller, eula_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2204: Adding tests for markup_styles_controller, lock_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2208: Testing for submission_records_controller, profile_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2218: Refactor response_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2217: Refactor questionnaires_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2216: Refactor late_policies_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2215: Refactor student_quizzes_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2214: Refactor teams_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2219: Improve assessment360_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2211: Testing for summary_helper]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2225: Refactor review_mapping_helper.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2221: Refactor submitted content controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2206: Testing for users_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2207: Testing for submitted_content_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2205: Testing for participants_controller, versions_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2212: Testing for hamer.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - S2200: Refactoring evaluation of SQL queries (Java/JUnit)]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - S2222: Refactor impersonate_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2202- Testing for badges_controller, publishing_controller]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2022 - E2220: Refactor reputation_web_service_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2224: Refactor review_mapping_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2209: Testing for analytic helper.rb, join team requests helper.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2227: SQL Injection Fix]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2223. Refactor sign up sheet controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2201: Testing for assignment_questionnaire_controller.rb]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2243. Refactor student_teams functionality]]&lt;br /&gt;
&lt;br /&gt;
== Final Projects ==&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143559</id>
		<title>CSC/ECE 517 Spring 2022 - E2219: Improve assessment360 controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143559"/>
		<updated>2022-03-24T18:13:01Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Automated Testing using RSPEC */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2219 Improve assessment360_controller.rb which was a project in CSC517 Spring 2022. Please see below for a description of the improvements made, issues fixed, code changed, testing information, details required to peer-review the project, and future improvements.&lt;br /&gt;
&lt;br /&gt;
== Peer Review Information ==&lt;br /&gt;
&lt;br /&gt;
For users who would like to view and test the assessment 360 feature, the deployed Expertiza application can be found at the URL mentioned below and can be access using the credentials below: &lt;br /&gt;
&lt;br /&gt;
URL: http://152.7.99.75:8080&lt;br /&gt;
&lt;br /&gt;
Instructor login: &lt;br /&gt;
&lt;br /&gt;
Username: '''instructor6'''&lt;br /&gt;
&lt;br /&gt;
Password: '''password'''&lt;br /&gt;
&lt;br /&gt;
Click on courses and then click on the icon on any one course to go to the assessment 360 page, or log in and paste this URL into the browser: http://152.7.99.75:8080/assessment360/index?course_id=184&lt;br /&gt;
The course ID here is an example, you can add the course ID of your choice.&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 Navigation.jpeg|800px]]&lt;br /&gt;
&lt;br /&gt;
Please note that due to VCL constraints, the page will take few mins to load and render the data. Please wait for the page to '''load completely''' before starting the peer review. &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
The Expertiza project contains a feature that allows instructors to get a full or 360-degree view of a course they teach that includes all students in the course, all assignments, and the scores of each student in each assignment, averages, and final scores as well. This feature, called assessment 360, was written in a previous year, and due to issues identified with the code such as variable names, redundancy, code smells etc, a need for improvement of the same was identified. &lt;br /&gt;
&lt;br /&gt;
== Issues and Description of Fixes ==&lt;br /&gt;
In our work on improving this feature, we have handled the following issues and made the following improvements:&lt;br /&gt;
&lt;br /&gt;
=== Refactoring ===&lt;br /&gt;
&lt;br /&gt;
The previous implementation of the controller made a number of database calls, several of which were extraneous and not required. We have refactored the functions pertaining to these calls so that the number of unneeded calls to the database is reduced for loading teammate and metareviews. &lt;br /&gt;
&lt;br /&gt;
The older implementation of the computation for class averages, and students average scores incorrectly factored in students who had not received grades for an assignment or who had not taken part in an assignment. &lt;br /&gt;
In addition to correcting this, we also simplified the process by refactoring the code(please see the next section for the code).&lt;br /&gt;
&lt;br /&gt;
The past implementation of the controller was unclear and non-Rubyistic in naming conventions, function names, and implementation in the code. We have refactored the controller to make it more Rubyistic overall and also re-written parts of the code to make it more understandable, as well as more CRUD-like. Additionally, we made several methods whose functionality was required and used only inside this controller private.&lt;br /&gt;
&lt;br /&gt;
=== UI changes ===&lt;br /&gt;
The previous implementation had 2 views – all_students_all_reviews and course_student_grade_summary. Both of these views cover some of the data required for a 360-degree view of the course. &lt;br /&gt;
We have combined these pages together into a single page that contains checkboxes which allow the user to select what data they would like to view and shows only those columns they would like to see. This both simplifies and enhances the functioning of the feature.&lt;br /&gt;
&lt;br /&gt;
=== Variable Names, Cosmetic Issues, Comments ===&lt;br /&gt;
&lt;br /&gt;
In the older implementation, there were some unclear variable names such as teamed_count, and some other minor cosmetic issues such as a hyphen being displayed for empty fields where a dash would more visible and clearer. These have been fixed in both the code and the UI of the application to be user-friendly and developer friendly.&lt;br /&gt;
Additionally, comments have been added everywhere to make the code easier to understand and modify for future improvements.&lt;br /&gt;
&lt;br /&gt;
== Fix details and Code Explanation ==&lt;br /&gt;
&lt;br /&gt;
=== Files Changed ===&lt;br /&gt;
The following files were altered and edited in the course of our work on this feature:&lt;br /&gt;
Assessment360_Controller.rb (under app/controller)&lt;br /&gt;
This file contains the controller for the assessment 360 feature which we have improved in our work. The code has been refactored to be cleaner, modular,  easier to understand and also to incorporate DRY. &lt;br /&gt;
&lt;br /&gt;
Index.html.erb (under app/views/assessment360)&lt;br /&gt;
The view for the assessment 360 feature have been combined as stated in the requirements and all the data is now viewable from the index.html.erb page. This page also features checkboxes so the user can view only the data they are interested in without cluttering caused by unneeded data.&lt;br /&gt;
&lt;br /&gt;
=== Issue Fix Details ===&lt;br /&gt;
&lt;br /&gt;
1) '''app/controllers/assessment360_controller.rb'''&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 1.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 2.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 3.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 4.png|800px]]&lt;br /&gt;
&lt;br /&gt;
'''Code changes'''&lt;br /&gt;
&lt;br /&gt;
1. In all_student_all_reviews function, the DB calls are by creating init_data function.&lt;br /&gt;
&lt;br /&gt;
2. The init_data function, removes DB calls inside nested for-loops (look for old code, below) which makes db call for every iteration, the code has been refactored in such a way that it preloads the required data using assignments.includes([:participants]) (look for new code, below). This way, instead of making multiple db calls across diff functions, we load the required data and thereby reduce the processing time.&lt;br /&gt;
&lt;br /&gt;
3. The major change, which we've incorporated here is moving the functions which weren't used in other modules into private. Moving excess functions to private, helps keep the controller more CRUD-like and differentiates to the developer which functions are used by the controller to work, and which ones are the functions of the controller.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Old Code (with complex for loops and more db calls)'''&lt;br /&gt;
&lt;br /&gt;
 @course_participants.each do |cp|&lt;br /&gt;
      # for each assignment&lt;br /&gt;
      # [aggregrate_review_grades_per_stu, review_count_per_stu] --&amp;gt; [0, 0]&lt;br /&gt;
      %w[teammate meta].each { |type| instance_variable_set(&amp;quot;@#{type}_review_info_per_stu&amp;quot;, [0, 0]) }&lt;br /&gt;
      students_teamed = StudentTask.teamed_students(cp.user)&lt;br /&gt;
      @teamed_count[cp.id] = students_teamed[course.id].try(:size).to_i&lt;br /&gt;
      @assignments.each do |assignment|&lt;br /&gt;
        @meta_review[cp.id] = {} unless @meta_review.key?(cp.id)&lt;br /&gt;
        @teammate_review[cp.id] = {} unless @teammate_review.key?(cp.id)&lt;br /&gt;
        assignment_participant = assignment.participants.find_by(user_id: cp.user_id)&lt;br /&gt;
        next if assignment_participant.nil?&lt;br /&gt;
        teammate_reviews = assignment_participant.teammate_reviews&lt;br /&gt;
        meta_reviews = assignment_participant.metareviews&lt;br /&gt;
        calc_overall_review_info(assignment,&lt;br /&gt;
                                 cp,&lt;br /&gt;
                                 teammate_reviews,&lt;br /&gt;
                                 @teammate_review,&lt;br /&gt;
                                 @overall_teammate_review_grades,&lt;br /&gt;
                                 @overall_teammate_review_count,&lt;br /&gt;
                                 @teammate_review_info_per_stu)&lt;br /&gt;
        calc_overall_review_info(assignment,&lt;br /&gt;
                                 cp,&lt;br /&gt;
                                 meta_reviews,&lt;br /&gt;
                                 @meta_review,&lt;br /&gt;
                                 @overall_meta_review_grades,&lt;br /&gt;
                                 @overall_meta_review_count,&lt;br /&gt;
                                 @meta_review_info_per_stu)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Refactored Code (with limited iteration and db calls)'''&lt;br /&gt;
    @assignments.each do |assignment|&lt;br /&gt;
      assignment.participants.each do |assignment_participant|&lt;br /&gt;
        reviews[assignment_participant.user_id] = {} unless reviews.key?(assignment_participant.user_id)&lt;br /&gt;
        assignment_reviews = assignment_participant.public_send(reviews_type) if assignment_participant.respond_to? reviews_type&lt;br /&gt;
        # If a student has not taken an assignment or if they have not received any grade for the same,&lt;br /&gt;
        # assign it as nil(not returning anything). This helps in easier calculation of overall grade&lt;br /&gt;
        score = calc_avg_score(assignment_reviews)&lt;br /&gt;
        reviews[assignment_participant.user_id][assignment.id] = score unless score.nil?&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2) '''app/views/assessment360/index.html.erb'''&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 indexhtmlerb 1.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 indexhtmlerb 2.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 indexhtmlerb 3.png|800px]]&lt;br /&gt;
&lt;br /&gt;
1. We've created a new file called ''index.html.erb''. This file contains all the logic for the frontend of this module.&lt;br /&gt;
&lt;br /&gt;
2. Major change that has been made is the addition of checkboxes. This way, users can filter the data that they view and view the data efficiently. &lt;br /&gt;
&lt;br /&gt;
3. This page loads all the assignment data, and their metadata like - meta reviews, teammate reviews, peer score, topic and instructor grade. The table also contains Aggregate score and Total grade.&lt;br /&gt;
&lt;br /&gt;
4. The checkboxes have been programmed in a way that whenever a checkbox is disabled, the corresponding column gets removed and the colspan rendering gets adjusted. Along with that, when all assignment parameters are unchecked, the assignment columns disappear completely. Same applies for aggregate score and Total Instructor Grade.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3) '''config/routes.rb'''&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 routes 1.png|800px]]&lt;br /&gt;
&lt;br /&gt;
Removed old route to this page and pointed it to the newly created index page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can check all the issues fixed here - https://github.com/expertiza/expertiza/pull/2337&lt;br /&gt;
&lt;br /&gt;
== Test Plan==&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing using RSPEC ===&lt;br /&gt;
Please note that we have removed several older tests which tested functions that are now private and inaccessible outside the controller class itself. However, the methods that call these private functions are still tested and tests are passing successfully.&lt;br /&gt;
&lt;br /&gt;
1. After refactoring, most of our new test cases focus on functions which are the core of the controller - covering important aspects like who accesses the page, checking permissions for the page, how the page behaves on edge cases etc.&lt;br /&gt;
&lt;br /&gt;
2. Using RSpec, the test cases cover various scenarios and tests each scenario in depth. &lt;br /&gt;
Eg, for access control, we check the access for all roles for a user comprehensively to ensure that only people with permissions can view the page. &lt;br /&gt;
&lt;br /&gt;
3. Since most of the work in this module is computations in the back and rendering data in the frontend, we've ensured that the computations happen in their specific order and enough test cases are present for edge cases. &lt;br /&gt;
&lt;br /&gt;
4. For backend computations, we've covered lot of edge cases like no participants to a course, when there are no reviews made by a participant, when assignment doesn't exists, insure existence_of being called before executing index page etc. &lt;br /&gt;
&lt;br /&gt;
5. For frontend, we've covered cases like formatting of the data in the page, null dataset, score being int, float, null, displaying percentages when data is int, float, null etc.&lt;br /&gt;
&lt;br /&gt;
Even though we've moved most of the code into private functions, the core functionalities of the controller are refactored and appropriate test cases are written to ensure high coverage. &lt;br /&gt;
&lt;br /&gt;
Please run the following command to execute the tests:&lt;br /&gt;
&lt;br /&gt;
 rspec spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
=== Testing from UI ===&lt;br /&gt;
The following steps needs to be performed to test this code from UI:&lt;br /&gt;
&lt;br /&gt;
1.	Login as instructor(username: instructor6 , password: password). Please either do step 2 or 3, as both are not required.&lt;br /&gt;
&lt;br /&gt;
2.	Select Courses, and the page will show a list of courses. Please select the rightmost icons for any course (we recommend CSC517 Fall 2017, CSC517 Fall 2011 so there’s more data, but it can take some time for it load since there’s a lot of pre-loaded data in the VCL)&lt;br /&gt;
&lt;br /&gt;
3.	Alternatively, you can login and go to this URL: http://152.7.99.75:8080/assessment360/index?course_id=184&lt;br /&gt;
&lt;br /&gt;
4.	You should be able to see a webpage with a table and checkboxes at the top. This table is an instructor’s 360-degree view of a course, containing the scores of each students in the course for every assignment the instructor has added to Expertiza. &lt;br /&gt;
&lt;br /&gt;
5.	Unchecking some or all of the checkboxes removes and brings back various columns from/to the page. Please feel free to test this out.&lt;br /&gt;
&lt;br /&gt;
== Scope for future improvement ==&lt;br /&gt;
&lt;br /&gt;
The UI of the index page which shows all of the data can be improved to make it look neater and more aesthetically pleasing. The table’s positioning and centering can be improved for a more convenient experience overall.&lt;br /&gt;
&lt;br /&gt;
The user’s preferred (checked on the UI checkboxes) can be saved to either the Database or a cache, and on subsequent reloads be the fields loaded on the page for a particular. We suggest using the database for this rather than a cache. &lt;br /&gt;
&lt;br /&gt;
Lazy loading can be implemented for the columns the user last chose(assuming these are stored in the DB) and other columns can lazy-loaded if the user clicks on the checkboxes on the screen.&lt;br /&gt;
&lt;br /&gt;
'''''course_student_grade_summary''''' function inside the assessment360_controller.rb contains few unnecessary db calls which can be refactored further to increase the efficiency and reduce the processing time of the module.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143558</id>
		<title>CSC/ECE 517 Spring 2022 - E2219: Improve assessment360 controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143558"/>
		<updated>2022-03-24T18:11:11Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Testing from UI */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2219 Improve assessment360_controller.rb which was a project in CSC517 Spring 2022. Please see below for a description of the improvements made, issues fixed, code changed, testing information, details required to peer-review the project, and future improvements.&lt;br /&gt;
&lt;br /&gt;
== Peer Review Information ==&lt;br /&gt;
&lt;br /&gt;
For users who would like to view and test the assessment 360 feature, the deployed Expertiza application can be found at the URL mentioned below and can be access using the credentials below: &lt;br /&gt;
&lt;br /&gt;
URL: http://152.7.99.75:8080&lt;br /&gt;
&lt;br /&gt;
Instructor login: &lt;br /&gt;
&lt;br /&gt;
Username: '''instructor6'''&lt;br /&gt;
&lt;br /&gt;
Password: '''password'''&lt;br /&gt;
&lt;br /&gt;
Click on courses and then click on the icon on any one course to go to the assessment 360 page, or log in and paste this URL into the browser: http://152.7.99.75:8080/assessment360/index?course_id=184&lt;br /&gt;
The course ID here is an example, you can add the course ID of your choice.&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 Navigation.jpeg|800px]]&lt;br /&gt;
&lt;br /&gt;
Please note that due to VCL constraints, the page will take few mins to load and render the data. Please wait for the page to '''load completely''' before starting the peer review. &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
The Expertiza project contains a feature that allows instructors to get a full or 360-degree view of a course they teach that includes all students in the course, all assignments, and the scores of each student in each assignment, averages, and final scores as well. This feature, called assessment 360, was written in a previous year, and due to issues identified with the code such as variable names, redundancy, code smells etc, a need for improvement of the same was identified. &lt;br /&gt;
&lt;br /&gt;
== Issues and Description of Fixes ==&lt;br /&gt;
In our work on improving this feature, we have handled the following issues and made the following improvements:&lt;br /&gt;
&lt;br /&gt;
=== Refactoring ===&lt;br /&gt;
&lt;br /&gt;
The previous implementation of the controller made a number of database calls, several of which were extraneous and not required. We have refactored the functions pertaining to these calls so that the number of unneeded calls to the database is reduced for loading teammate and metareviews. &lt;br /&gt;
&lt;br /&gt;
The older implementation of the computation for class averages, and students average scores incorrectly factored in students who had not received grades for an assignment or who had not taken part in an assignment. &lt;br /&gt;
In addition to correcting this, we also simplified the process by refactoring the code(please see the next section for the code).&lt;br /&gt;
&lt;br /&gt;
The past implementation of the controller was unclear and non-Rubyistic in naming conventions, function names, and implementation in the code. We have refactored the controller to make it more Rubyistic overall and also re-written parts of the code to make it more understandable, as well as more CRUD-like. Additionally, we made several methods whose functionality was required and used only inside this controller private.&lt;br /&gt;
&lt;br /&gt;
=== UI changes ===&lt;br /&gt;
The previous implementation had 2 views – all_students_all_reviews and course_student_grade_summary. Both of these views cover some of the data required for a 360-degree view of the course. &lt;br /&gt;
We have combined these pages together into a single page that contains checkboxes which allow the user to select what data they would like to view and shows only those columns they would like to see. This both simplifies and enhances the functioning of the feature.&lt;br /&gt;
&lt;br /&gt;
=== Variable Names, Cosmetic Issues, Comments ===&lt;br /&gt;
&lt;br /&gt;
In the older implementation, there were some unclear variable names such as teamed_count, and some other minor cosmetic issues such as a hyphen being displayed for empty fields where a dash would more visible and clearer. These have been fixed in both the code and the UI of the application to be user-friendly and developer friendly.&lt;br /&gt;
Additionally, comments have been added everywhere to make the code easier to understand and modify for future improvements.&lt;br /&gt;
&lt;br /&gt;
== Fix details and Code Explanation ==&lt;br /&gt;
&lt;br /&gt;
=== Files Changed ===&lt;br /&gt;
The following files were altered and edited in the course of our work on this feature:&lt;br /&gt;
Assessment360_Controller.rb (under app/controller)&lt;br /&gt;
This file contains the controller for the assessment 360 feature which we have improved in our work. The code has been refactored to be cleaner, modular,  easier to understand and also to incorporate DRY. &lt;br /&gt;
&lt;br /&gt;
Index.html.erb (under app/views/assessment360)&lt;br /&gt;
The view for the assessment 360 feature have been combined as stated in the requirements and all the data is now viewable from the index.html.erb page. This page also features checkboxes so the user can view only the data they are interested in without cluttering caused by unneeded data.&lt;br /&gt;
&lt;br /&gt;
=== Issue Fix Details ===&lt;br /&gt;
&lt;br /&gt;
1) '''app/controllers/assessment360_controller.rb'''&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 1.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 2.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 3.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 4.png|800px]]&lt;br /&gt;
&lt;br /&gt;
'''Code changes'''&lt;br /&gt;
&lt;br /&gt;
1. In all_student_all_reviews function, the DB calls are by creating init_data function.&lt;br /&gt;
&lt;br /&gt;
2. The init_data function, removes DB calls inside nested for-loops (look for old code, below) which makes db call for every iteration, the code has been refactored in such a way that it preloads the required data using assignments.includes([:participants]) (look for new code, below). This way, instead of making multiple db calls across diff functions, we load the required data and thereby reduce the processing time.&lt;br /&gt;
&lt;br /&gt;
3. The major change, which we've incorporated here is moving the functions which weren't used in other modules into private. Moving excess functions to private, helps keep the controller more CRUD-like and differentiates to the developer which functions are used by the controller to work, and which ones are the functions of the controller.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Old Code (with complex for loops and more db calls)'''&lt;br /&gt;
&lt;br /&gt;
 @course_participants.each do |cp|&lt;br /&gt;
      # for each assignment&lt;br /&gt;
      # [aggregrate_review_grades_per_stu, review_count_per_stu] --&amp;gt; [0, 0]&lt;br /&gt;
      %w[teammate meta].each { |type| instance_variable_set(&amp;quot;@#{type}_review_info_per_stu&amp;quot;, [0, 0]) }&lt;br /&gt;
      students_teamed = StudentTask.teamed_students(cp.user)&lt;br /&gt;
      @teamed_count[cp.id] = students_teamed[course.id].try(:size).to_i&lt;br /&gt;
      @assignments.each do |assignment|&lt;br /&gt;
        @meta_review[cp.id] = {} unless @meta_review.key?(cp.id)&lt;br /&gt;
        @teammate_review[cp.id] = {} unless @teammate_review.key?(cp.id)&lt;br /&gt;
        assignment_participant = assignment.participants.find_by(user_id: cp.user_id)&lt;br /&gt;
        next if assignment_participant.nil?&lt;br /&gt;
        teammate_reviews = assignment_participant.teammate_reviews&lt;br /&gt;
        meta_reviews = assignment_participant.metareviews&lt;br /&gt;
        calc_overall_review_info(assignment,&lt;br /&gt;
                                 cp,&lt;br /&gt;
                                 teammate_reviews,&lt;br /&gt;
                                 @teammate_review,&lt;br /&gt;
                                 @overall_teammate_review_grades,&lt;br /&gt;
                                 @overall_teammate_review_count,&lt;br /&gt;
                                 @teammate_review_info_per_stu)&lt;br /&gt;
        calc_overall_review_info(assignment,&lt;br /&gt;
                                 cp,&lt;br /&gt;
                                 meta_reviews,&lt;br /&gt;
                                 @meta_review,&lt;br /&gt;
                                 @overall_meta_review_grades,&lt;br /&gt;
                                 @overall_meta_review_count,&lt;br /&gt;
                                 @meta_review_info_per_stu)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Refactored Code (with limited iteration and db calls)'''&lt;br /&gt;
    @assignments.each do |assignment|&lt;br /&gt;
      assignment.participants.each do |assignment_participant|&lt;br /&gt;
        reviews[assignment_participant.user_id] = {} unless reviews.key?(assignment_participant.user_id)&lt;br /&gt;
        assignment_reviews = assignment_participant.public_send(reviews_type) if assignment_participant.respond_to? reviews_type&lt;br /&gt;
        # If a student has not taken an assignment or if they have not received any grade for the same,&lt;br /&gt;
        # assign it as nil(not returning anything). This helps in easier calculation of overall grade&lt;br /&gt;
        score = calc_avg_score(assignment_reviews)&lt;br /&gt;
        reviews[assignment_participant.user_id][assignment.id] = score unless score.nil?&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2) '''app/views/assessment360/index.html.erb'''&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 indexhtmlerb 1.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 indexhtmlerb 2.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 indexhtmlerb 3.png|800px]]&lt;br /&gt;
&lt;br /&gt;
1. We've created a new file called ''index.html.erb''. This file contains all the logic for the frontend of this module.&lt;br /&gt;
&lt;br /&gt;
2. Major change that has been made is the addition of checkboxes. This way, users can filter the data that they view and view the data efficiently. &lt;br /&gt;
&lt;br /&gt;
3. This page loads all the assignment data, and their metadata like - meta reviews, teammate reviews, peer score, topic and instructor grade. The table also contains Aggregate score and Total grade.&lt;br /&gt;
&lt;br /&gt;
4. The checkboxes have been programmed in a way that whenever a checkbox is disabled, the corresponding column gets removed and the colspan rendering gets adjusted. Along with that, when all assignment parameters are unchecked, the assignment columns disappear completely. Same applies for aggregate score and Total Instructor Grade.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3) '''config/routes.rb'''&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 routes 1.png|800px]]&lt;br /&gt;
&lt;br /&gt;
Removed old route to this page and pointed it to the newly created index page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can check all the issues fixed here - https://github.com/expertiza/expertiza/pull/2337&lt;br /&gt;
&lt;br /&gt;
== Test Plan==&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing using RSPEC ===&lt;br /&gt;
Please note that we have removed several older tests which tested functions that are now private and inaccessible outside the controller class itself. However, the methods that call these private functions are still tested and tests are passing successfully.&lt;br /&gt;
&lt;br /&gt;
1. After refactoring, most of our test cases focus on functions which are the core of the controller - covering important aspects like who accesses the page, checking permissions for the page, how the page behaves on edge cases etc.&lt;br /&gt;
&lt;br /&gt;
2. Using RSpec, the test cases cover various scenarios and tests each scenario in depth. &lt;br /&gt;
Eg, for access control, we check the access for all roles for a user comprehensively to ensure that only people with permissions can view the page. &lt;br /&gt;
&lt;br /&gt;
3. Since most of the work in this module is computations in the back and rendering data in the frontend, we've ensured that the computations happen in their specific order and enough test cases are present for edge cases. &lt;br /&gt;
&lt;br /&gt;
4. For backend computations, we've covered lot of edge cases like no participants to a course, when there are no reviews made by a participant, when assignment doesn't exists, insure existence_of being called before executing index page etc. &lt;br /&gt;
&lt;br /&gt;
5. For frontend, we've covered cases like formatting of the data in the page, null dataset, score being int, float, null, displaying percentages when data is int, float, null etc.&lt;br /&gt;
&lt;br /&gt;
Even though we've moved most of the code into private functions, the core functionalities of the controller are refactored and appropriate test cases are written to ensure high coverage. &lt;br /&gt;
&lt;br /&gt;
Please run the following command to execute the tests:&lt;br /&gt;
&lt;br /&gt;
 rspec spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
=== Testing from UI ===&lt;br /&gt;
The following steps needs to be performed to test this code from UI:&lt;br /&gt;
&lt;br /&gt;
1.	Login as instructor(username: instructor6 , password: password). Please either do step 2 or 3, as both are not required.&lt;br /&gt;
&lt;br /&gt;
2.	Select Courses, and the page will show a list of courses. Please select the rightmost icons for any course (we recommend CSC517 Fall 2017, CSC517 Fall 2011 so there’s more data, but it can take some time for it load since there’s a lot of pre-loaded data in the VCL)&lt;br /&gt;
&lt;br /&gt;
3.	Alternatively, you can login and go to this URL: http://152.7.99.75:8080/assessment360/index?course_id=184&lt;br /&gt;
&lt;br /&gt;
4.	You should be able to see a webpage with a table and checkboxes at the top. This table is an instructor’s 360-degree view of a course, containing the scores of each students in the course for every assignment the instructor has added to Expertiza. &lt;br /&gt;
&lt;br /&gt;
5.	Unchecking some or all of the checkboxes removes and brings back various columns from/to the page. Please feel free to test this out.&lt;br /&gt;
&lt;br /&gt;
== Scope for future improvement ==&lt;br /&gt;
&lt;br /&gt;
The UI of the index page which shows all of the data can be improved to make it look neater and more aesthetically pleasing. The table’s positioning and centering can be improved for a more convenient experience overall.&lt;br /&gt;
&lt;br /&gt;
The user’s preferred (checked on the UI checkboxes) can be saved to either the Database or a cache, and on subsequent reloads be the fields loaded on the page for a particular. We suggest using the database for this rather than a cache. &lt;br /&gt;
&lt;br /&gt;
Lazy loading can be implemented for the columns the user last chose(assuming these are stored in the DB) and other columns can lazy-loaded if the user clicks on the checkboxes on the screen.&lt;br /&gt;
&lt;br /&gt;
'''''course_student_grade_summary''''' function inside the assessment360_controller.rb contains few unnecessary db calls which can be refactored further to increase the efficiency and reduce the processing time of the module.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143557</id>
		<title>CSC/ECE 517 Spring 2022 - E2219: Improve assessment360 controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143557"/>
		<updated>2022-03-24T18:09:43Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2219 Improve assessment360_controller.rb which was a project in CSC517 Spring 2022. Please see below for a description of the improvements made, issues fixed, code changed, testing information, details required to peer-review the project, and future improvements.&lt;br /&gt;
&lt;br /&gt;
== Peer Review Information ==&lt;br /&gt;
&lt;br /&gt;
For users who would like to view and test the assessment 360 feature, the deployed Expertiza application can be found at the URL mentioned below and can be access using the credentials below: &lt;br /&gt;
&lt;br /&gt;
URL: http://152.7.99.75:8080&lt;br /&gt;
&lt;br /&gt;
Instructor login: &lt;br /&gt;
&lt;br /&gt;
Username: '''instructor6'''&lt;br /&gt;
&lt;br /&gt;
Password: '''password'''&lt;br /&gt;
&lt;br /&gt;
Click on courses and then click on the icon on any one course to go to the assessment 360 page, or log in and paste this URL into the browser: http://152.7.99.75:8080/assessment360/index?course_id=184&lt;br /&gt;
The course ID here is an example, you can add the course ID of your choice.&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 Navigation.jpeg|800px]]&lt;br /&gt;
&lt;br /&gt;
Please note that due to VCL constraints, the page will take few mins to load and render the data. Please wait for the page to '''load completely''' before starting the peer review. &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
The Expertiza project contains a feature that allows instructors to get a full or 360-degree view of a course they teach that includes all students in the course, all assignments, and the scores of each student in each assignment, averages, and final scores as well. This feature, called assessment 360, was written in a previous year, and due to issues identified with the code such as variable names, redundancy, code smells etc, a need for improvement of the same was identified. &lt;br /&gt;
&lt;br /&gt;
== Issues and Description of Fixes ==&lt;br /&gt;
In our work on improving this feature, we have handled the following issues and made the following improvements:&lt;br /&gt;
&lt;br /&gt;
=== Refactoring ===&lt;br /&gt;
&lt;br /&gt;
The previous implementation of the controller made a number of database calls, several of which were extraneous and not required. We have refactored the functions pertaining to these calls so that the number of unneeded calls to the database is reduced for loading teammate and metareviews. &lt;br /&gt;
&lt;br /&gt;
The older implementation of the computation for class averages, and students average scores incorrectly factored in students who had not received grades for an assignment or who had not taken part in an assignment. &lt;br /&gt;
In addition to correcting this, we also simplified the process by refactoring the code(please see the next section for the code).&lt;br /&gt;
&lt;br /&gt;
The past implementation of the controller was unclear and non-Rubyistic in naming conventions, function names, and implementation in the code. We have refactored the controller to make it more Rubyistic overall and also re-written parts of the code to make it more understandable, as well as more CRUD-like. Additionally, we made several methods whose functionality was required and used only inside this controller private.&lt;br /&gt;
&lt;br /&gt;
=== UI changes ===&lt;br /&gt;
The previous implementation had 2 views – all_students_all_reviews and course_student_grade_summary. Both of these views cover some of the data required for a 360-degree view of the course. &lt;br /&gt;
We have combined these pages together into a single page that contains checkboxes which allow the user to select what data they would like to view and shows only those columns they would like to see. This both simplifies and enhances the functioning of the feature.&lt;br /&gt;
&lt;br /&gt;
=== Variable Names, Cosmetic Issues, Comments ===&lt;br /&gt;
&lt;br /&gt;
In the older implementation, there were some unclear variable names such as teamed_count, and some other minor cosmetic issues such as a hyphen being displayed for empty fields where a dash would more visible and clearer. These have been fixed in both the code and the UI of the application to be user-friendly and developer friendly.&lt;br /&gt;
Additionally, comments have been added everywhere to make the code easier to understand and modify for future improvements.&lt;br /&gt;
&lt;br /&gt;
== Fix details and Code Explanation ==&lt;br /&gt;
&lt;br /&gt;
=== Files Changed ===&lt;br /&gt;
The following files were altered and edited in the course of our work on this feature:&lt;br /&gt;
Assessment360_Controller.rb (under app/controller)&lt;br /&gt;
This file contains the controller for the assessment 360 feature which we have improved in our work. The code has been refactored to be cleaner, modular,  easier to understand and also to incorporate DRY. &lt;br /&gt;
&lt;br /&gt;
Index.html.erb (under app/views/assessment360)&lt;br /&gt;
The view for the assessment 360 feature have been combined as stated in the requirements and all the data is now viewable from the index.html.erb page. This page also features checkboxes so the user can view only the data they are interested in without cluttering caused by unneeded data.&lt;br /&gt;
&lt;br /&gt;
=== Issue Fix Details ===&lt;br /&gt;
&lt;br /&gt;
1) '''app/controllers/assessment360_controller.rb'''&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 1.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 2.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 3.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 4.png|800px]]&lt;br /&gt;
&lt;br /&gt;
'''Code changes'''&lt;br /&gt;
&lt;br /&gt;
1. In all_student_all_reviews function, the DB calls are by creating init_data function.&lt;br /&gt;
&lt;br /&gt;
2. The init_data function, removes DB calls inside nested for-loops (look for old code, below) which makes db call for every iteration, the code has been refactored in such a way that it preloads the required data using assignments.includes([:participants]) (look for new code, below). This way, instead of making multiple db calls across diff functions, we load the required data and thereby reduce the processing time.&lt;br /&gt;
&lt;br /&gt;
3. The major change, which we've incorporated here is moving the functions which weren't used in other modules into private. Moving excess functions to private, helps keep the controller more CRUD-like and differentiates to the developer which functions are used by the controller to work, and which ones are the functions of the controller.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Old Code (with complex for loops and more db calls)'''&lt;br /&gt;
&lt;br /&gt;
 @course_participants.each do |cp|&lt;br /&gt;
      # for each assignment&lt;br /&gt;
      # [aggregrate_review_grades_per_stu, review_count_per_stu] --&amp;gt; [0, 0]&lt;br /&gt;
      %w[teammate meta].each { |type| instance_variable_set(&amp;quot;@#{type}_review_info_per_stu&amp;quot;, [0, 0]) }&lt;br /&gt;
      students_teamed = StudentTask.teamed_students(cp.user)&lt;br /&gt;
      @teamed_count[cp.id] = students_teamed[course.id].try(:size).to_i&lt;br /&gt;
      @assignments.each do |assignment|&lt;br /&gt;
        @meta_review[cp.id] = {} unless @meta_review.key?(cp.id)&lt;br /&gt;
        @teammate_review[cp.id] = {} unless @teammate_review.key?(cp.id)&lt;br /&gt;
        assignment_participant = assignment.participants.find_by(user_id: cp.user_id)&lt;br /&gt;
        next if assignment_participant.nil?&lt;br /&gt;
        teammate_reviews = assignment_participant.teammate_reviews&lt;br /&gt;
        meta_reviews = assignment_participant.metareviews&lt;br /&gt;
        calc_overall_review_info(assignment,&lt;br /&gt;
                                 cp,&lt;br /&gt;
                                 teammate_reviews,&lt;br /&gt;
                                 @teammate_review,&lt;br /&gt;
                                 @overall_teammate_review_grades,&lt;br /&gt;
                                 @overall_teammate_review_count,&lt;br /&gt;
                                 @teammate_review_info_per_stu)&lt;br /&gt;
        calc_overall_review_info(assignment,&lt;br /&gt;
                                 cp,&lt;br /&gt;
                                 meta_reviews,&lt;br /&gt;
                                 @meta_review,&lt;br /&gt;
                                 @overall_meta_review_grades,&lt;br /&gt;
                                 @overall_meta_review_count,&lt;br /&gt;
                                 @meta_review_info_per_stu)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Refactored Code (with limited iteration and db calls)'''&lt;br /&gt;
    @assignments.each do |assignment|&lt;br /&gt;
      assignment.participants.each do |assignment_participant|&lt;br /&gt;
        reviews[assignment_participant.user_id] = {} unless reviews.key?(assignment_participant.user_id)&lt;br /&gt;
        assignment_reviews = assignment_participant.public_send(reviews_type) if assignment_participant.respond_to? reviews_type&lt;br /&gt;
        # If a student has not taken an assignment or if they have not received any grade for the same,&lt;br /&gt;
        # assign it as nil(not returning anything). This helps in easier calculation of overall grade&lt;br /&gt;
        score = calc_avg_score(assignment_reviews)&lt;br /&gt;
        reviews[assignment_participant.user_id][assignment.id] = score unless score.nil?&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2) '''app/views/assessment360/index.html.erb'''&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 indexhtmlerb 1.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 indexhtmlerb 2.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 indexhtmlerb 3.png|800px]]&lt;br /&gt;
&lt;br /&gt;
1. We've created a new file called ''index.html.erb''. This file contains all the logic for the frontend of this module.&lt;br /&gt;
&lt;br /&gt;
2. Major change that has been made is the addition of checkboxes. This way, users can filter the data that they view and view the data efficiently. &lt;br /&gt;
&lt;br /&gt;
3. This page loads all the assignment data, and their metadata like - meta reviews, teammate reviews, peer score, topic and instructor grade. The table also contains Aggregate score and Total grade.&lt;br /&gt;
&lt;br /&gt;
4. The checkboxes have been programmed in a way that whenever a checkbox is disabled, the corresponding column gets removed and the colspan rendering gets adjusted. Along with that, when all assignment parameters are unchecked, the assignment columns disappear completely. Same applies for aggregate score and Total Instructor Grade.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3) '''config/routes.rb'''&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 routes 1.png|800px]]&lt;br /&gt;
&lt;br /&gt;
Removed old route to this page and pointed it to the newly created index page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can check all the issues fixed here - https://github.com/expertiza/expertiza/pull/2337&lt;br /&gt;
&lt;br /&gt;
== Test Plan==&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing using RSPEC ===&lt;br /&gt;
Please note that we have removed several older tests which tested functions that are now private and inaccessible outside the controller class itself. However, the methods that call these private functions are still tested and tests are passing successfully.&lt;br /&gt;
&lt;br /&gt;
1. After refactoring, most of our test cases focus on functions which are the core of the controller - covering important aspects like who accesses the page, checking permissions for the page, how the page behaves on edge cases etc.&lt;br /&gt;
&lt;br /&gt;
2. Using RSpec, the test cases cover various scenarios and tests each scenario in depth. &lt;br /&gt;
Eg, for access control, we check the access for all roles for a user comprehensively to ensure that only people with permissions can view the page. &lt;br /&gt;
&lt;br /&gt;
3. Since most of the work in this module is computations in the back and rendering data in the frontend, we've ensured that the computations happen in their specific order and enough test cases are present for edge cases. &lt;br /&gt;
&lt;br /&gt;
4. For backend computations, we've covered lot of edge cases like no participants to a course, when there are no reviews made by a participant, when assignment doesn't exists, insure existence_of being called before executing index page etc. &lt;br /&gt;
&lt;br /&gt;
5. For frontend, we've covered cases like formatting of the data in the page, null dataset, score being int, float, null, displaying percentages when data is int, float, null etc.&lt;br /&gt;
&lt;br /&gt;
Even though we've moved most of the code into private functions, the core functionalities of the controller are refactored and appropriate test cases are written to ensure high coverage. &lt;br /&gt;
&lt;br /&gt;
Please run the following command to execute the tests:&lt;br /&gt;
&lt;br /&gt;
 rspec spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
=== Testing from UI ===&lt;br /&gt;
The following steps needs to be performed to test this code from UI:&lt;br /&gt;
&lt;br /&gt;
1.	Login as instructor(username: instructor6 , password: password). Please either do step 2 or 3, as both are not required.&lt;br /&gt;
&lt;br /&gt;
2.	Select Courses, and the page will show a list of courses. Please select the rightmost icons for any course (we recommend CSC517 Fall 2017, CSC517 Fall 2011 so there’s more data, but it can take some time for it load since there’s a lot of pre-loaded data in the VCL)&lt;br /&gt;
&lt;br /&gt;
3.	Alternatively, you can login and go to this URL: &amp;lt;needs to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4.	You should be able to see a webpage with a table and checkboxes at the top. This table is an instructor’s 360-degree view of a course, containing the scores of each students in the course for every assignment the instructor has added to Expertiza. &lt;br /&gt;
&lt;br /&gt;
5.	Unchecking some or all of the checkboxes removes and brings back various columns from/to the page. Please feel free to test this out.&lt;br /&gt;
&lt;br /&gt;
== Scope for future improvement ==&lt;br /&gt;
&lt;br /&gt;
The UI of the index page which shows all of the data can be improved to make it look neater and more aesthetically pleasing. The table’s positioning and centering can be improved for a more convenient experience overall.&lt;br /&gt;
&lt;br /&gt;
The user’s preferred (checked on the UI checkboxes) can be saved to either the Database or a cache, and on subsequent reloads be the fields loaded on the page for a particular. We suggest using the database for this rather than a cache. &lt;br /&gt;
&lt;br /&gt;
Lazy loading can be implemented for the columns the user last chose(assuming these are stored in the DB) and other columns can lazy-loaded if the user clicks on the checkboxes on the screen.&lt;br /&gt;
&lt;br /&gt;
'''''course_student_grade_summary''''' function inside the assessment360_controller.rb contains few unnecessary db calls which can be refactored further to increase the efficiency and reduce the processing time of the module.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143536</id>
		<title>CSC/ECE 517 Spring 2022 - E2219: Improve assessment360 controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143536"/>
		<updated>2022-03-22T20:13:34Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: added rspec command for tests&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2219 Improve assessment360_controller.rb which was a project in CSC517 Spring 2022. Please see below for a description of the improvements made, issues fixed, code changed, testing information, details required to peer-review the project, and future improvements.&lt;br /&gt;
&lt;br /&gt;
== Peer Review Information ==&lt;br /&gt;
&lt;br /&gt;
For users who would like to view and test the assessment 360 feature, the deployed Expertiza application can be found at the URL mentioned below and can be access using the credentials below: &lt;br /&gt;
&lt;br /&gt;
URL: http://152.7.99.75:8080&lt;br /&gt;
&lt;br /&gt;
Instructor login: &lt;br /&gt;
&lt;br /&gt;
Username: '''instructor6'''&lt;br /&gt;
&lt;br /&gt;
Password: '''password'''&lt;br /&gt;
&lt;br /&gt;
Click on courses and then click on the icon on any one course to go to the assessment 360 page, or log in and paste this URL into the browser: http://152.7.99.75:8080/assessment360/index?course_id=184&lt;br /&gt;
The course ID here is an example, you can add the course ID of your choice.&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 Navigation.jpeg|800px]]&lt;br /&gt;
&lt;br /&gt;
Please note that due to VCL constraints, the page will take few mins to load and render the data. Please wait for the page to '''load completely''' before starting the peer review. &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
The Expertiza project contains a feature that allows instructors to get a full or 360-degree view of a course they teach that includes all students in the course, all assignments, and the scores of each student in each assignment, averages, and final scores as well. This feature, called assessment 360, was written in a previous year, and due to issues identified with the code such as variable names, redundancy, code smells etc, a need for improvement of the same was identified. &lt;br /&gt;
&lt;br /&gt;
== Issues and Description of Fixes ==&lt;br /&gt;
In our work on improving this feature, we have handled the following issues and made the following improvements:&lt;br /&gt;
&lt;br /&gt;
=== Refactoring ===&lt;br /&gt;
&lt;br /&gt;
The previous implementation of the controller made a number of database calls, several of which were extraneous and not required. We have refactored the functions pertaining to these calls so that the number of unneeded calls to the database is reduced for loading teammate and metareviews. &lt;br /&gt;
&lt;br /&gt;
The older implementation of the computation for class averages, and students average scores incorrectly factored in students who had not received grades for an assignment or who had not taken part in an assignment. &lt;br /&gt;
In addition to correcting this, we also simplified the process by refactoring the code(please see the next section for the code).&lt;br /&gt;
&lt;br /&gt;
The past implementation of the controller was unclear and non-Rubyistic in naming conventions, function names, and implementation in the code. We have refactored the controller to make it more Rubyistic overall and also re-written parts of the code to make it more understandable, as well as more CRUD-like. Additionally, we made several methods whose functionality was required and used only inside this controller private.&lt;br /&gt;
&lt;br /&gt;
=== UI changes ===&lt;br /&gt;
The previous implementation had 2 views – all_students_all_reviews and course_student_grade_summary. Both of these views cover some of the data required for a 360-degree view of the course. &lt;br /&gt;
We have combined these pages together into a single page that contains checkboxes which allow the user to select what data they would like to view and shows only those columns they would like to see. This both simplifies and enhances the functioning of the feature.&lt;br /&gt;
&lt;br /&gt;
=== Variable Names, Cosmetic Issues, Comments ===&lt;br /&gt;
&lt;br /&gt;
In the older implementation, there were some unclear variable names such as teamed_count, and some other minor cosmetic issues such as a hyphen being displayed for empty fields where a dash would more visible and clearer. These have been fixed in both the code and the UI of the application to be user-friendly and developer friendly.&lt;br /&gt;
Additionally, comments have been added everywhere to make the code easier to understand and modify for future improvements.&lt;br /&gt;
&lt;br /&gt;
== Fix details and Code Explanation ==&lt;br /&gt;
&lt;br /&gt;
=== Files Changed ===&lt;br /&gt;
The following files were altered and edited in the course of our work on this feature:&lt;br /&gt;
Assessment360_Controller.rb (under app/controller)&lt;br /&gt;
This file contains the controller for the assessment 360 feature which we have improved in our work. The code has been refactored to be cleaner, modular,  easier to understand and also to incorporate DRY. &lt;br /&gt;
&lt;br /&gt;
Index.html.erb (under app/views/assessment360)&lt;br /&gt;
The view for the assessment 360 feature have been combined as stated in the requirements and all the data is now viewable from the index.html.erb page. This page also features checkboxes so the user can view only the data they are interested in without cluttering caused by unneeded data.&lt;br /&gt;
&lt;br /&gt;
=== Issue Fix Details ===&lt;br /&gt;
&lt;br /&gt;
1) '''app/controllers/assessment360_controller.rb'''&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 1.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 2.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 3.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 assessment360 controller 4.png|800px]]&lt;br /&gt;
&lt;br /&gt;
'''Code changes'''&lt;br /&gt;
&lt;br /&gt;
1. In all_student_all_reviews function, the DB calls are by creating init_data function.&lt;br /&gt;
&lt;br /&gt;
2. The init_data function, removes DB calls inside nested for-loops (look for old code, below) which makes db call for every iteration, the code has been refactored in such a way that it preloads the required data using assignments.includes([:participants]) (look for new code, below). This way, instead of making multiple db calls across diff functions, we load the required data and thereby reduce the processing time.&lt;br /&gt;
&lt;br /&gt;
3. The major change, which we've incorporated here is moving the functions which weren't used in other modules into private. Moving excess functions to private, helps keep the controller more CRUD-like and differentiates to the developer which functions are used by the controller to work, and which ones are the functions of the controller.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Old Code (with complex for loops and more db calls)'''&lt;br /&gt;
&lt;br /&gt;
 @course_participants.each do |cp|&lt;br /&gt;
      # for each assignment&lt;br /&gt;
      # [aggregrate_review_grades_per_stu, review_count_per_stu] --&amp;gt; [0, 0]&lt;br /&gt;
      %w[teammate meta].each { |type| instance_variable_set(&amp;quot;@#{type}_review_info_per_stu&amp;quot;, [0, 0]) }&lt;br /&gt;
      students_teamed = StudentTask.teamed_students(cp.user)&lt;br /&gt;
      @teamed_count[cp.id] = students_teamed[course.id].try(:size).to_i&lt;br /&gt;
      @assignments.each do |assignment|&lt;br /&gt;
        @meta_review[cp.id] = {} unless @meta_review.key?(cp.id)&lt;br /&gt;
        @teammate_review[cp.id] = {} unless @teammate_review.key?(cp.id)&lt;br /&gt;
        assignment_participant = assignment.participants.find_by(user_id: cp.user_id)&lt;br /&gt;
        next if assignment_participant.nil?&lt;br /&gt;
        teammate_reviews = assignment_participant.teammate_reviews&lt;br /&gt;
        meta_reviews = assignment_participant.metareviews&lt;br /&gt;
        calc_overall_review_info(assignment,&lt;br /&gt;
                                 cp,&lt;br /&gt;
                                 teammate_reviews,&lt;br /&gt;
                                 @teammate_review,&lt;br /&gt;
                                 @overall_teammate_review_grades,&lt;br /&gt;
                                 @overall_teammate_review_count,&lt;br /&gt;
                                 @teammate_review_info_per_stu)&lt;br /&gt;
        calc_overall_review_info(assignment,&lt;br /&gt;
                                 cp,&lt;br /&gt;
                                 meta_reviews,&lt;br /&gt;
                                 @meta_review,&lt;br /&gt;
                                 @overall_meta_review_grades,&lt;br /&gt;
                                 @overall_meta_review_count,&lt;br /&gt;
                                 @meta_review_info_per_stu)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Refactored Code (with limited iteration and db calls)'''&lt;br /&gt;
    @assignments.each do |assignment|&lt;br /&gt;
      assignment.participants.each do |assignment_participant|&lt;br /&gt;
        reviews[assignment_participant.user_id] = {} unless reviews.key?(assignment_participant.user_id)&lt;br /&gt;
        assignment_reviews = assignment_participant.public_send(reviews_type) if assignment_participant.respond_to? reviews_type&lt;br /&gt;
        # If a student has not taken an assignment or if they have not received any grade for the same,&lt;br /&gt;
        # assign it as nil(not returning anything). This helps in easier calculation of overall grade&lt;br /&gt;
        score = calc_avg_score(assignment_reviews)&lt;br /&gt;
        reviews[assignment_participant.user_id][assignment.id] = score unless score.nil?&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2) '''app/views/assessment360/index.html.erb'''&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 indexhtmlerb 1.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 indexhtmlerb 2.png|800px]]&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 indexhtmlerb 3.png|800px]]&lt;br /&gt;
&lt;br /&gt;
1. We've created a new file called ''index.html.erb''. This file contains all the logic for the frontend of this module.&lt;br /&gt;
&lt;br /&gt;
2. Major change that has been made is the addition of checkboxes. This way, users can filter the data that they view and view the data efficiently. &lt;br /&gt;
&lt;br /&gt;
3. This page loads all the assignment data, and their metadata like - meta reviews, teammate reviews, peer score, topic and instructor grade. The table also contains Aggregate score and Total grade.&lt;br /&gt;
&lt;br /&gt;
4. The checkboxes have been programmed in a way that whenever a checkbox is disabled, the corresponding column gets removed and the colspan rendering gets adjusted. Along with that, when all assignment parameters are unchecked, the assignment columns disappear completely. Same applies for aggregate score and Total Instructor Grade.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3) '''config/routes.rb'''&lt;br /&gt;
&lt;br /&gt;
[[File:E2219 routes 1.png|800px]]&lt;br /&gt;
&lt;br /&gt;
Removed old route to this page and pointed it to the newly created index page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can check all the issues fixed here - https://github.com/expertiza/expertiza/pull/2337&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing using RSPEC ===&lt;br /&gt;
Please note that we have removed several older tests which tested functions that are now private and inaccessible outside the controller class itself. However, the methods that call these private functions are still tested and tests are passing successfully.&lt;br /&gt;
&lt;br /&gt;
1. After refactoring, most of our test cases focus on functions which are the core of the controller - covering important aspects like who accesses the page, checking permissions for the page, how the page behaves on edge cases etc.&lt;br /&gt;
&lt;br /&gt;
2. Using RSpec, the test cases cover various scenarios and tests each scenario in depth. &lt;br /&gt;
Eg, for access control, we check the access for all roles for a user comprehensively to ensure that only people with permissions can view the page. &lt;br /&gt;
&lt;br /&gt;
3. Since most of the work in this module is computations in the back and rendering data in the frontend, we've ensured that the computations happen in their specific order and enough test cases are present for edge cases. &lt;br /&gt;
&lt;br /&gt;
4. For backend computations, we've covered lot of edge cases like no participants to a course, when there are no reviews made by a participant, when assignment doesn't exists, insure existence_of being called before executing index page etc. &lt;br /&gt;
&lt;br /&gt;
5. For frontend, we've covered cases like formatting of the data in the page, null dataset, score being int, float, null, displaying percentages when data is int, float, null etc.&lt;br /&gt;
&lt;br /&gt;
Even though we've moved most of the code into private functions, the core functionalities of the controller are refactored and appropriate test cases are written to ensure high coverage. &lt;br /&gt;
&lt;br /&gt;
Please run the following command to execute the tests:&lt;br /&gt;
&lt;br /&gt;
 rspec spec/controllers/assessment360_controller_spec.rb&lt;br /&gt;
&lt;br /&gt;
=== Testing from UI ===&lt;br /&gt;
The following steps needs to be performed to test this code from UI:&lt;br /&gt;
&lt;br /&gt;
1.	Login as instructor(username: instructor6 , password: password). Please either do step 2 or 3, as both are not required.&lt;br /&gt;
&lt;br /&gt;
2.	Select Courses, and the page will show a list of courses. Please select the rightmost icons for any course (we recommend CSC517 Fall 2017, CSC517 Fall 2011 so there’s more data, but it can take some time for it load since there’s a lot of pre-loaded data in the VCL)&lt;br /&gt;
&lt;br /&gt;
3.	Alternatively, you can login and go to this URL: &amp;lt;needs to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4.	You should be able to see a webpage with a table and checkboxes at the top. This table is an instructor’s 360-degree view of a course, containing the scores of each students in the course for every assignment the instructor has added to Expertiza. &lt;br /&gt;
&lt;br /&gt;
5.	Unchecking some or all of the checkboxes removes and brings back various columns from/to the page. Please feel free to test this out.&lt;br /&gt;
&lt;br /&gt;
== Scope for future improvement ==&lt;br /&gt;
&lt;br /&gt;
The UI of the index page which shows all of the data can be improved to make it look neater and more aesthetically pleasing. The table’s positioning and centering can be improved for a more convenient experience overall.&lt;br /&gt;
&lt;br /&gt;
The user’s preferred (checked on the UI checkboxes) can be saved to either the Database or a cache, and on subsequent reloads be the fields loaded on the page for a particular. We suggest using the database for this rather than a cache. &lt;br /&gt;
&lt;br /&gt;
Lazy loading can be implemented for the columns the user last chose(assuming these are stored in the DB) and other columns can lazy-loaded if the user clicks on the checkboxes on the screen.&lt;br /&gt;
&lt;br /&gt;
'''''course_student_grade_summary''''' function inside the assessment360_controller.rb contains few unnecessary db calls which can be refactored further to increase the efficiency and reduce the processing time of the module.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143160</id>
		<title>CSC/ECE 517 Spring 2022 - E2219: Improve assessment360 controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143160"/>
		<updated>2022-03-21T17:13:11Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2219 Improve assessment360_controller.rb which was a project in CSC517 Spring 2022. Please see below for a description of the improvements made, issues fixed, code changed, testing information, details required to peer-review the project, and future improvements.&lt;br /&gt;
&lt;br /&gt;
== Peer Review Information ==&lt;br /&gt;
&lt;br /&gt;
For users who would like to view and test the assessment 360 feature, the deployed Expertiza application can be found at the URL mentioned below and can be access using the credentials below: &lt;br /&gt;
&lt;br /&gt;
URL: &amp;lt;needs to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instructor login: &lt;br /&gt;
&lt;br /&gt;
Username: '''instructor6'''&lt;br /&gt;
&lt;br /&gt;
Password: '''password'''&lt;br /&gt;
&lt;br /&gt;
Click on courses and then click on the icon on any one course to go to the assessment 360 page, or log in and paste this URL into the browser: &amp;lt;to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
The Expertiza project contains a feature that allows instructors to get a full or 360-degree view of a course they teach that includes all students in the course, all assignments, and the scores of each student in each assignment, averages, and final scores as well. This feature, called assessment 360, was written in a previous year, and due to issues identified with the code such as variable names, redundancy, code smells etc, a need for improvement of the same was identified. &lt;br /&gt;
&lt;br /&gt;
== Issues and Description of Fixes ==&lt;br /&gt;
In our work on improving this feature, we have handled the following issues and made the following improvements:&lt;br /&gt;
&lt;br /&gt;
=== Refactoring ===&lt;br /&gt;
&lt;br /&gt;
The previous implementation of the controller made a number of database calls, several of which were extraneous and not required. We have refactored the functions pertaining to these calls so that the number of unneeded calls to the database is reduced for loading teammate and metareviews. &lt;br /&gt;
&lt;br /&gt;
The older implementation of the computation for class averages, and students average scores incorrectly factored in students who had not received grades for an assignment or who had not taken part in an assignment. &lt;br /&gt;
In addition to correcting this, we also simplified the process by refactoring the code(please see the next section for the code).&lt;br /&gt;
&lt;br /&gt;
The past implementation of the controller was unclear and non-Rubyistic in naming conventions, function names, and implementation in the code. We have refactored the controller to make it more Rubyistic overall and also re-written parts of the code to make it more understandable, as well as more CRUD-like. Additionally, we made several methods whose functionality was required and used only inside this controller private.&lt;br /&gt;
&lt;br /&gt;
=== UI changes ===&lt;br /&gt;
The previous implementation had 2 views – all_students_all_reviews and course_student_grade_summary. Both of these views cover some of the data required for a 360-degree view of the course. &lt;br /&gt;
We have combined these pages together into a single page that contains checkboxes which allow the user to select what data they would like to view and shows only those columns they would like to see. This both simplifies and enhances the functioning of the feature.&lt;br /&gt;
&lt;br /&gt;
=== Variable Names, Cosmetic Issues, Comments ===&lt;br /&gt;
&lt;br /&gt;
In the older implementation, there were some unclear variable names such as teamed_count, and some other minor cosmetic issues such as a hyphen being displayed for empty fields where a dash would more visible and clearer. These have been fixed in both the code and the UI of the application to be user-friendly and developer friendly.&lt;br /&gt;
Additionally, comments have been added everywhere to make the code easier to understand and modify for future improvements.&lt;br /&gt;
&lt;br /&gt;
== Fix details and Code Explanation ==&lt;br /&gt;
&lt;br /&gt;
=== Files Changed ===&lt;br /&gt;
The following files were altered and edited in the course of our work on this feature:&lt;br /&gt;
Assessment360_Controller.rb (under app/controller)&lt;br /&gt;
This file contains the controller for the assessment 360 feature which we have improved in our work. The code has been refactored to be cleaner, modular,  easier to understand and also to incorporate DRY. &lt;br /&gt;
&lt;br /&gt;
Index.html.erb (under app/views/assessment360)&lt;br /&gt;
The view for the assessment 360 feature have been combined as stated in the requirements and all the data is now viewable from the index.html.erb page. This page also features checkboxes so the user can view only the data they are interested in without cluttering caused by unneeded data.&lt;br /&gt;
&lt;br /&gt;
=== Issue Fix Details ===&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
=== Automated Testing using RSPEC ===&lt;br /&gt;
Please note that we have removed several older tests which tested functions that are now private and inaccessible outside the controller class itself. However, the methods that call these private functions are still tested and tests are passing successfully.&lt;br /&gt;
&lt;br /&gt;
=== Testing from UI ===&lt;br /&gt;
The following steps needs to be performed to test this code from UI:&lt;br /&gt;
&lt;br /&gt;
1.	Login as instructor(username: instructor6 , password: password). Please either do step 2 or 3, as both are not required.&lt;br /&gt;
&lt;br /&gt;
2.	Select Courses, and the page will show a list of courses. Please select the rightmost icons for any course (we recommend CSC517 Fall 2017, CSC517 Fall 2011 so there’s more data, but it can take some time for it load since there’s a lot of pre-loaded data in the VCL)&lt;br /&gt;
&lt;br /&gt;
3.	Alternatively, you can login and go to this URL: &amp;lt;needs to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4.	You should be able to see a webpage with a table and checkboxes at the top. This table is an instructor’s 360-degree view of a course, containing the scores of each students in the course for every assignment the instructor has added to Expertiza. &lt;br /&gt;
&lt;br /&gt;
5.	Unchecking some or all of the checkboxes removes and brings back various columns from/to the page. Please feel free to test this out.&lt;br /&gt;
&lt;br /&gt;
== Scope for future improvement ==&lt;br /&gt;
&lt;br /&gt;
The UI of the index page which shows all of the data can be improved to make it look neater and more aesthetically pleasing. The table’s positioning and centering can be improved for a more convenient experience overall.&lt;br /&gt;
&lt;br /&gt;
The user’s preferred (checked on the UI checkboxes) can be saved to either the Database or a cache, and on subsequent reloads be the fields loaded on the page for a particular. We suggest using the database for this rather than a cache. &lt;br /&gt;
&lt;br /&gt;
Lazy loading can be implemented for the columns the user last chose(assuming these are stored in the DB) and other columns can lazy-loaded if the user clicks on the checkboxes on the screen.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143096</id>
		<title>CSC/ECE 517 Spring 2022 - E2219: Improve assessment360 controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143096"/>
		<updated>2022-03-21T00:14:57Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Scope for future improvement */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2219 Improve assessment360_controller.rb which was a project in CSC517 Spring 2022. Please see below for a description of the improvements made, issues fixed, code changed, testing information, details required to peer-review the project, and future improvements.&lt;br /&gt;
&lt;br /&gt;
== Peer Review Information ==&lt;br /&gt;
&lt;br /&gt;
For users who would like to view and test the assessment 360 feature, the deployed Expertiza application can be found at the URL mentioned below and can be access using the credentials below: &lt;br /&gt;
&lt;br /&gt;
URL: &amp;lt;needs to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instructor login: &lt;br /&gt;
&lt;br /&gt;
Username: '''instructor6'''&lt;br /&gt;
&lt;br /&gt;
Password: '''password'''&lt;br /&gt;
&lt;br /&gt;
Click on courses and then click on the icon on any one course to go to the assessment 360 page, or log in and paste this URL into the browser: &amp;lt;to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
The Expertiza project contains a feature that allows instructors to get a full or 360-degree view of a course they teach that includes all students in the course, all assignments, and the scores of each student in each assignment, averages, and final scores as well. This feature, called assessment 360, was written in a previous year, and due to issues identified with the code such as variable names, redundancy, code smells etc, a need for improvement of the same was identified. &lt;br /&gt;
&lt;br /&gt;
== Issues and Description of Fixes ==&lt;br /&gt;
In our work on improving this feature, we have handled the following issues and made the following improvements:&lt;br /&gt;
&lt;br /&gt;
=== Refactoring ===&lt;br /&gt;
&lt;br /&gt;
The previous implementation of the controller made a number of database calls, several of which were extraneous and not required. We have refactored the functions pertaining to these calls so that the number of unneeded calls to the database is reduced for loading teammate and metareviews. &lt;br /&gt;
&lt;br /&gt;
The older implementation of the computation for class averages, and students average scores incorrectly factored in students who had not received grades for an assignment or who had not taken part in an assignment. &lt;br /&gt;
In addition to correcting this, we also simplified the process by refactoring the code(please see the next section for the code).&lt;br /&gt;
&lt;br /&gt;
The past implementation of the controller was unclear and non-Rubyistic in naming conventions, function names, and implementation in the code. We have refactored the controller to make it more Rubyistic overall and also re-written parts of the code to make it more understandable, as well as more CRUD-like.&lt;br /&gt;
&lt;br /&gt;
=== UI changes ===&lt;br /&gt;
The previous implementation had 2 views – all_students_all_reviews and course_student_grade_summary. Both of these views cover some of the data required for a 360-degree view of the course. &lt;br /&gt;
We have combined these pages together into a single page that contains checkboxes which allow the user to select what data they would like to view and shows only those columns they would like to see. This both simplifies and enhances the functioning of the feature.&lt;br /&gt;
&lt;br /&gt;
=== Variable Names, Cosmetic Issues, Comments ===&lt;br /&gt;
&lt;br /&gt;
In the older implementation, there were some unclear variable names such as teamed_count, and some other minor cosmetic issues such as a hyphen being displayed for empty fields where a dash would more visible and clearer. These have been fixed in both the code and the UI of the application to be user-friendly and developer friendly.&lt;br /&gt;
Additionally, comments have been added everywhere to make the code easier to understand and modify for future improvements.&lt;br /&gt;
&lt;br /&gt;
== Fix details and Code Explanation ==&lt;br /&gt;
&lt;br /&gt;
=== Files Changed ===&lt;br /&gt;
The following files were altered and edited in the course of our work on this feature:&lt;br /&gt;
Assessment360_Controller.rb (under app/controller)&lt;br /&gt;
This file contains the controller for the assessment 360 feature which we have improved in our work. The code has been refactored to be cleaner, modular,  easier to understand and also to incorporate DRY. &lt;br /&gt;
&lt;br /&gt;
Index.html.erb (under app/views/assessment360)&lt;br /&gt;
The view for the assessment 360 feature have been combined as stated in the requirements and all the data is now viewable from the index.html.erb page. This page also features checkboxes so the user can view only the data they are interested in without cluttering caused by unneeded data.&lt;br /&gt;
&lt;br /&gt;
=== Issue Fix Details ===&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
=== Automated Testing using RSPEC ===&lt;br /&gt;
=== Testing from UI ===&lt;br /&gt;
The following steps needs to be performed to test this code from UI:&lt;br /&gt;
&lt;br /&gt;
1.	Login as instructor(username: instructor6 , password: password). Please either do step 2 or 3, as both are not required.&lt;br /&gt;
&lt;br /&gt;
2.	Select Courses, and the page will show a list of courses. Please select the rightmost icons for any course (we recommend CSC517 Fall 2017, CSC517 Fall 2011 so there’s more data, but it can take some time for it load since there’s a lot of pre-loaded data in the VCL)&lt;br /&gt;
&lt;br /&gt;
3.	Alternatively, you can login and go to this URL: &amp;lt;needs to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4.	You should be able to see a webpage with a table and checkboxes at the top. This table is an instructor’s 360-degree view of a course, containing the scores of each students in the course for every assignment the instructor has added to Expertiza. &lt;br /&gt;
&lt;br /&gt;
5.	Unchecking some or all of the checkboxes removes and brings back various columns from/to the page. Please feel free to test this out.&lt;br /&gt;
&lt;br /&gt;
== Scope for future improvement ==&lt;br /&gt;
&lt;br /&gt;
The UI of the index page which shows all of the data can be improved to make it look neater and more aesthetically pleasing. The table’s positioning and centering can be improved for a more convenient experience overall.&lt;br /&gt;
&lt;br /&gt;
The user’s preferred (checked on the UI checkboxes) can be saved to either the Database or a cache, and on subsequent reloads be the fields loaded on the page for a particular. We suggest using the database for this rather than a cache. &lt;br /&gt;
&lt;br /&gt;
Lazy loading can be implemented for the columns the user last chose(assuming these are stored in the DB) and other columns can lazy-loaded if the user clicks on the checkboxes on the screen.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143095</id>
		<title>CSC/ECE 517 Spring 2022 - E2219: Improve assessment360 controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143095"/>
		<updated>2022-03-21T00:14:06Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* Testing from UI */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2219 Improve assessment360_controller.rb which was a project in CSC517 Spring 2022. Please see below for a description of the improvements made, issues fixed, code changed, testing information, details required to peer-review the project, and future improvements.&lt;br /&gt;
&lt;br /&gt;
== Peer Review Information ==&lt;br /&gt;
&lt;br /&gt;
For users who would like to view and test the assessment 360 feature, the deployed Expertiza application can be found at the URL mentioned below and can be access using the credentials below: &lt;br /&gt;
&lt;br /&gt;
URL: &amp;lt;needs to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instructor login: &lt;br /&gt;
&lt;br /&gt;
Username: '''instructor6'''&lt;br /&gt;
&lt;br /&gt;
Password: '''password'''&lt;br /&gt;
&lt;br /&gt;
Click on courses and then click on the icon on any one course to go to the assessment 360 page, or log in and paste this URL into the browser: &amp;lt;to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
The Expertiza project contains a feature that allows instructors to get a full or 360-degree view of a course they teach that includes all students in the course, all assignments, and the scores of each student in each assignment, averages, and final scores as well. This feature, called assessment 360, was written in a previous year, and due to issues identified with the code such as variable names, redundancy, code smells etc, a need for improvement of the same was identified. &lt;br /&gt;
&lt;br /&gt;
== Issues and Description of Fixes ==&lt;br /&gt;
In our work on improving this feature, we have handled the following issues and made the following improvements:&lt;br /&gt;
&lt;br /&gt;
=== Refactoring ===&lt;br /&gt;
&lt;br /&gt;
The previous implementation of the controller made a number of database calls, several of which were extraneous and not required. We have refactored the functions pertaining to these calls so that the number of unneeded calls to the database is reduced for loading teammate and metareviews. &lt;br /&gt;
&lt;br /&gt;
The older implementation of the computation for class averages, and students average scores incorrectly factored in students who had not received grades for an assignment or who had not taken part in an assignment. &lt;br /&gt;
In addition to correcting this, we also simplified the process by refactoring the code(please see the next section for the code).&lt;br /&gt;
&lt;br /&gt;
The past implementation of the controller was unclear and non-Rubyistic in naming conventions, function names, and implementation in the code. We have refactored the controller to make it more Rubyistic overall and also re-written parts of the code to make it more understandable, as well as more CRUD-like.&lt;br /&gt;
&lt;br /&gt;
=== UI changes ===&lt;br /&gt;
The previous implementation had 2 views – all_students_all_reviews and course_student_grade_summary. Both of these views cover some of the data required for a 360-degree view of the course. &lt;br /&gt;
We have combined these pages together into a single page that contains checkboxes which allow the user to select what data they would like to view and shows only those columns they would like to see. This both simplifies and enhances the functioning of the feature.&lt;br /&gt;
&lt;br /&gt;
=== Variable Names, Cosmetic Issues, Comments ===&lt;br /&gt;
&lt;br /&gt;
In the older implementation, there were some unclear variable names such as teamed_count, and some other minor cosmetic issues such as a hyphen being displayed for empty fields where a dash would more visible and clearer. These have been fixed in both the code and the UI of the application to be user-friendly and developer friendly.&lt;br /&gt;
Additionally, comments have been added everywhere to make the code easier to understand and modify for future improvements.&lt;br /&gt;
&lt;br /&gt;
== Fix details and Code Explanation ==&lt;br /&gt;
&lt;br /&gt;
=== Files Changed ===&lt;br /&gt;
The following files were altered and edited in the course of our work on this feature:&lt;br /&gt;
Assessment360_Controller.rb (under app/controller)&lt;br /&gt;
This file contains the controller for the assessment 360 feature which we have improved in our work. The code has been refactored to be cleaner, modular,  easier to understand and also to incorporate DRY. &lt;br /&gt;
&lt;br /&gt;
Index.html.erb (under app/views/assessment360)&lt;br /&gt;
The view for the assessment 360 feature have been combined as stated in the requirements and all the data is now viewable from the index.html.erb page. This page also features checkboxes so the user can view only the data they are interested in without cluttering caused by unneeded data.&lt;br /&gt;
&lt;br /&gt;
=== Issue Fix Details ===&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
=== Automated Testing using RSPEC ===&lt;br /&gt;
=== Testing from UI ===&lt;br /&gt;
The following steps needs to be performed to test this code from UI:&lt;br /&gt;
&lt;br /&gt;
1.	Login as instructor(username: instructor6 , password: password). Please either do step 2 or 3, as both are not required.&lt;br /&gt;
&lt;br /&gt;
2.	Select Courses, and the page will show a list of courses. Please select the rightmost icons for any course (we recommend CSC517 Fall 2017, CSC517 Fall 2011 so there’s more data, but it can take some time for it load since there’s a lot of pre-loaded data in the VCL)&lt;br /&gt;
&lt;br /&gt;
3.	Alternatively, you can login and go to this URL: &amp;lt;needs to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4.	You should be able to see a webpage with a table and checkboxes at the top. This table is an instructor’s 360-degree view of a course, containing the scores of each students in the course for every assignment the instructor has added to Expertiza. &lt;br /&gt;
&lt;br /&gt;
5.	Unchecking some or all of the checkboxes removes and brings back various columns from/to the page. Please feel free to test this out.&lt;br /&gt;
&lt;br /&gt;
== Scope for future improvement ==&lt;br /&gt;
&lt;br /&gt;
•	The UI of the index page which shows all of the data can be improved to make it look neater and more aesthetically pleasing. The table’s positioning and centering can be improved for a more convenient experience overall.&lt;br /&gt;
&lt;br /&gt;
•	The user’s preferred (checked on the UI checkboxes) can be saved to either the Database or a cache, and on subsequent reloads be the fields loaded on the page for a particular. We suggest using the database for this rather than a cache. &lt;br /&gt;
&lt;br /&gt;
•	Lazy loading can be implemented for the columns the user last chose(assuming these are stored in the DB) and other columns can lazy-loaded if the user clicks on the checkboxes on the screen.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143094</id>
		<title>CSC/ECE 517 Spring 2022 - E2219: Improve assessment360 controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2219:_Improve_assessment360_controller.rb&amp;diff=143094"/>
		<updated>2022-03-21T00:05:32Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: Information about E2219 - Improve assessment360_controller.rb in Expertiza&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains information about Expertiza Issue E2219 Improve assessment360_controller.rb which was a project in CSC517 Spring 2022. Please see below for a description of the improvements made, issues fixed, code changed, testing information, details required to peer-review the project, and future improvements.&lt;br /&gt;
&lt;br /&gt;
== Peer Review Information ==&lt;br /&gt;
&lt;br /&gt;
For users who would like to view and test the assessment 360 feature, the deployed Expertiza application can be found at the URL mentioned below and can be access using the credentials below: &lt;br /&gt;
&lt;br /&gt;
URL: &amp;lt;needs to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instructor login: &lt;br /&gt;
&lt;br /&gt;
Username: '''instructor6'''&lt;br /&gt;
&lt;br /&gt;
Password: '''password'''&lt;br /&gt;
&lt;br /&gt;
Click on courses and then click on the icon on any one course to go to the assessment 360 page, or log in and paste this URL into the browser: &amp;lt;to be added&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Expertiza project is software to create reusable learning objects through peer review. It also supports team projects, and the submission of almost any document type, including URLs and wiki pages.&lt;br /&gt;
The Expertiza project contains a feature that allows instructors to get a full or 360-degree view of a course they teach that includes all students in the course, all assignments, and the scores of each student in each assignment, averages, and final scores as well. This feature, called assessment 360, was written in a previous year, and due to issues identified with the code such as variable names, redundancy, code smells etc, a need for improvement of the same was identified. &lt;br /&gt;
&lt;br /&gt;
== Issues and Description of Fixes ==&lt;br /&gt;
In our work on improving this feature, we have handled the following issues and made the following improvements:&lt;br /&gt;
&lt;br /&gt;
=== Refactoring ===&lt;br /&gt;
&lt;br /&gt;
The previous implementation of the controller made a number of database calls, several of which were extraneous and not required. We have refactored the functions pertaining to these calls so that the number of unneeded calls to the database is reduced for loading teammate and metareviews. &lt;br /&gt;
&lt;br /&gt;
The older implementation of the computation for class averages, and students average scores incorrectly factored in students who had not received grades for an assignment or who had not taken part in an assignment. &lt;br /&gt;
In addition to correcting this, we also simplified the process by refactoring the code(please see the next section for the code).&lt;br /&gt;
&lt;br /&gt;
The past implementation of the controller was unclear and non-Rubyistic in naming conventions, function names, and implementation in the code. We have refactored the controller to make it more Rubyistic overall and also re-written parts of the code to make it more understandable, as well as more CRUD-like.&lt;br /&gt;
&lt;br /&gt;
=== UI changes ===&lt;br /&gt;
The previous implementation had 2 views – all_students_all_reviews and course_student_grade_summary. Both of these views cover some of the data required for a 360-degree view of the course. &lt;br /&gt;
We have combined these pages together into a single page that contains checkboxes which allow the user to select what data they would like to view and shows only those columns they would like to see. This both simplifies and enhances the functioning of the feature.&lt;br /&gt;
&lt;br /&gt;
=== Variable Names, Cosmetic Issues, Comments ===&lt;br /&gt;
&lt;br /&gt;
In the older implementation, there were some unclear variable names such as teamed_count, and some other minor cosmetic issues such as a hyphen being displayed for empty fields where a dash would more visible and clearer. These have been fixed in both the code and the UI of the application to be user-friendly and developer friendly.&lt;br /&gt;
Additionally, comments have been added everywhere to make the code easier to understand and modify for future improvements.&lt;br /&gt;
&lt;br /&gt;
== Fix details and Code Explanation ==&lt;br /&gt;
&lt;br /&gt;
=== Files Changed ===&lt;br /&gt;
The following files were altered and edited in the course of our work on this feature:&lt;br /&gt;
Assessment360_Controller.rb (under app/controller)&lt;br /&gt;
This file contains the controller for the assessment 360 feature which we have improved in our work. The code has been refactored to be cleaner, modular,  easier to understand and also to incorporate DRY. &lt;br /&gt;
&lt;br /&gt;
Index.html.erb (under app/views/assessment360)&lt;br /&gt;
The view for the assessment 360 feature have been combined as stated in the requirements and all the data is now viewable from the index.html.erb page. This page also features checkboxes so the user can view only the data they are interested in without cluttering caused by unneeded data.&lt;br /&gt;
&lt;br /&gt;
=== Issue Fix Details ===&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
=== Automated Testing using RSPEC ===&lt;br /&gt;
=== Testing from UI ===&lt;br /&gt;
== Scope for future improvement ==&lt;br /&gt;
&lt;br /&gt;
•	The UI of the index page which shows all of the data can be improved to make it look neater and more aesthetically pleasing. The table’s positioning and centering can be improved for a more convenient experience overall.&lt;br /&gt;
&lt;br /&gt;
•	The user’s preferred (checked on the UI checkboxes) can be saved to either the Database or a cache, and on subsequent reloads be the fields loaded on the page for a particular. We suggest using the database for this rather than a cache. &lt;br /&gt;
&lt;br /&gt;
•	Lazy loading can be implemented for the columns the user last chose(assuming these are stored in the DB) and other columns can lazy-loaded if the user clicks on the checkboxes on the screen.&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022&amp;diff=143066</id>
		<title>CSC/ECE 517 Spring 2022</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022&amp;diff=143066"/>
		<updated>2022-03-20T21:19:11Z</updated>

		<summary type="html">&lt;p&gt;Sbabu: /* OSS Projects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== OSS Projects ==&lt;br /&gt;
&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2203: Adding tests for courses_controller, eula_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2204: Adding tests for markup_styles_controller, lock_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2217: Refactor questionnaires_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2216: Refactor late_policies_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2215: Refactor student_quizzes_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2214: Refactor teams_controller]]&lt;br /&gt;
* [[CSC/ECE 517 Spring 2022 - E2219: Improve assessment360_controller.rb]]&lt;br /&gt;
&lt;br /&gt;
== Final Projects ==&lt;/div&gt;</summary>
		<author><name>Sbabu</name></author>
	</entry>
</feed>