User:Mxu: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(22 intermediate revisions by 2 users not shown)
Line 1: Line 1:
===CSC/ECE 517 Spring 2016 E1621: Refactor and test the quizzing feature===
= Introduction to Expertiza =
= Introduction to Expertiza =
Expertiza<ref>https://github.com/expertiza/expertiza</ref> is a project developed using Ruby on Rails<ref>http://guides.rubyonrails.org/getting_started.html</ref>. It provides features like peer review, team assignments and submission of projects. This can be achieved by submitting code base, URL of hosted code on remote server and Wiki submissions. It is an [https://en.wikipedia.org/wiki/Open_source open source] application and the code can be cloned from [https://github.com/expertiza/expertiza GitHub]. This application provides an efficient way to manage assignments, grades and reviews. This makes the process easier and faster when the class strength is large.
Expertiza<ref>https://github.com/expertiza/expertiza</ref> is a project developed using Ruby on Rails<ref>http://guides.rubyonrails.org/getting_started.html</ref>. It provides features like peer review, team assignments and submission of projects. This can be achieved by submitting code base, URL of hosted code on remote server and Wiki submissions. It is an [https://en.wikipedia.org/wiki/Open_source open source] application and the code can be cloned from [https://github.com/expertiza/expertiza GitHub]. This application provides an efficient way to manage assignments, grades and reviews.


= Problem Statement =
= Problem Statement =
The Expertiza quiz feature is in need of refactoring. Currently, quizzes are checked by <code>questionnaires_controller.valid_quiz</code>. This method is rather long, involved, and does not take advantage of object-oriented practices. It combs through the parameters hash received from a request and manually checks all quiz fields, all question fields, all question choice fields, and verifies that each question has a correct choice.


== Classes involved ==
We will refactor the method to construct a quiz questionniare and then call <code>valid?</code> to recursively validate the object tree using ActiveRecord validations. This is an improvement over the current code which checks all questions manually. The purpose of refactoring in this manner is to enhance readability, DRYness, and maintainability of Expertiza code.
*'''questionnaires_controller.rb'''
our own files in /specs/ folder.
 
== What it does ==
#It handles the display, submission, deletion of hyperlinks and files by the teams.
 
== What is wrong with it ==
#The submitted hyperlinks and files are stored in '''participants''' table, which means they are associated with individuals instead of team. This is not a good design because in #Expertiza, students are always grouped in teams, even when there is only one person in each team (individual assignments).
#Currently, when a team member wants to see all the submitted hyperlinks of his team, Expertiza needs to load all the submitted hyperlinks from each participant in this team and merge them #all before display. Similar overhead happens when a team member wants to delete a submitted hyperlink. Also, the '''directory_num''' field needs to be moved from the '''participants''' #table to the '''teams''' table. It signifies the sub-directory which stores all the files submitted by this team. Intuitively it is clear that each team should ideally have only one #common copy of '''directory_num'''.
 
== What needs to be done ==
Student-generated quizzes is the feature that students use for writing quizzes for the Wikipedia contribution assignment. In this project, you need to do refactoring and create functional tests for this feature:
*Refactoring:
**Refactor the valid_quiz method in questionnaires_controller. This method should not check the questions one by one, instead, it should create the quiz_question objects and quiz_choice objects, then call “quiz_question.valid?to check whether the question is valid.
*Testing:
**The instructor can set up an assignment which supports quizzing feature by
***Checking the “has quiz” box
***Setting the # of question for each set of quiz
***Setting in which deadline can student reviewers take the quizzes
**Student authors can create quizzes and edit them:
***They can create quizzes on the “Your work” page
***They can edit the quiz questions.
***They can view the quiz questions.
***If the quiz question has something missing, the system will flash an error message, either:
****The name of quiz is missing.
****The question text is missing for one or more questions.
****The choices are missing for one or more questions.
****The correct answer(s) have not been provided.
**Student reviewers can take the quizzes on the work they have reviewed/they need to review
***They need to request the artifact to review first. If this artifact has a quiz associated, they can take the quiz in the round which quiz-taking is allowed.
***They can click “take quiz” then request quizzes to take.
***They can fill in their choices on the quizzes.
***After taking the quizzes, and submitting, they will see their grade on the “Take quizzes” page
***On the “take quizzes” page, they can see their question-by-question scores for finished quizzes by clicking “view”
Instructor can view the quiz questions and quiz scores on the tree display by clicking “view quiz questions” icon.
 
= Scope =
#This project can be divided into four major work items:
#Moving the '''hyperlinks''' field from the '''participants''' table to the '''teams''' table. After this change, relevant changes are needed in the code to support this database change.
#Moving the '''directory_num''' field from the '''participants''' table to the '''teams''' table. After this change, relevant changes are needed in the code to support this database change.
#Moving the '''set_student_directory_num''' method from the '''participants_controller''' to the '''teams_controller'''. It just needs to check '''directory_num''' from the '''team''' table.
#Write test cases to verify student-submitted hyperlinks and files.
 
= Implementation =
#Expertiza files that will be modified for each work item along with the description:
{| class="wikitable"
! scope="col" style="width: 50px;" | Work Item #
! scope="col" style="width: 225px;" | Files to be modified
! scope="col" style="width: 500px;" | Description
|-
| 1 || assignment_participant.rb<br>assignment_team.rb<br>db:participants table<br>db:teams table || - Create a new column '''hyperlinks''' in '''teams''' table<br>- Write db migration, move all the participants’ submitted hyperlinks from '''participants''' table into '''teams''' table. All duplicated hyperlinks have to be removed.<br>- Write db migration, remove the '''hyperlinks''' field from '''participants''' table.<br>- hyperlink-related methods in both '''assignment_participant.rb''' and '''assignment_team.rb''' have to be rewritten to make sure the new code works with the new db design.
|-
| 2 || participants_controller.rb<br>teams_controller.rb<br>db:participants table<br>db:teams table || - Write db migration, move the '''directory_num''' field (also the content) to '''teams''' table.<br>- Move the '''set_student_directory_num''' from '''participant_controller''' to '''team_controller''', then refactor this method into smaller methods.<br>- This method no longer needs to test all the participants and see if any of them have '''directory_num''', instead, it just check the '''directory_num''' from '''teams''' table.<br>- Make change to the submitted-file-related code to make sure it works with the new design.
|-
| 3 || New files to be added || - Write test cases to test student-submitted hyperlinks and files.
|}


= Design =
In addition to this refactoring we will implement integration testing on the quiz feature to verify that the expected behavior is present for all use cases.
Consider a simple use case where we need to retrieve the submitted hyperlinks and files corresponding to '''team_id''', say, '''23841'''. Since there is no existing correlation between teams and submitted content, we will first need to look up the '''participants''' in the team using the '''teams_users''' table. And then, using those user_id values one at a time, we need to look up the '''participants''' table.


[[File:db.jpg]]
A full description of the assignment may be found [https://docs.google.com/document/d/13oRmKemlwRzXG1ItcI2zTx4r3gAAfvmuobZjxiy0oVo/edit#heading=h.su78b3gdq1a7 here]


Finally, we need to merge the results before displaying it.
=Classes involved=
* questionnaires_controller.rb
* quiz_quesetionnaire.rb
* quiz_question.rb
* quiz_question_choice.rb
* spec/features/quiz_spec.rb


<pre>
=Use Case and Scenarios=
def hyperlinks
The Quiz Use Case allows for instructors to create and manage assignments where students may write quizzes for reviewers on materials they have submitted. See the following scenarios for a complete description of how the feature is expected to work.
  links = Array.new
  self.participants.each { |team_member|
    links.concat(team_member.hyperlinks_array) if team_member.hyperlinks_array}
  links
end
</pre>


So, it becomes a tedious 3-step process. It is evident from this example that the current design is inefficient.
==Instructors are able to create and manage an assignment's quiz feature==
An instructor chooses to create an assignment that has a quiz [S1]. While editing the assignment they may choose the number of quiz questions [S2] and set which phases students are allowed to take the quizzes [S3].


Instead, if the hyperlinks data were available in '''teams''' table, we could directly access all the submitted content for a team in one shot, making it much more streamlined.
* [S1] - When creating the assignment there is a checkbox labeled "has quiz." Checking this box creates an assignment that includes a quiz.
* [S2] - When an assignment has a quiz there is an input field that accepts the number of questions that will be on each quiz. Setting this number appropriately changes the number of quiz questions.
* [S3] - Students may not take quizzes on a phase that does not allow them to do so. When on a stage that does allow for quizzes, they may take quizzes on work that they have reviewed.


As a part of this change, we’ll have to move the following routines in '''assignment_participants.rb''', which deal hyperlink handling to '''assignment_team.rb'''.
==Instructors can view quiz questions and scores from the tree view==
After an assignment has been created the instructor may choose to view student quizzes from the tree view [S1]. While on the quiz view page they see student quizzes [S2] and student responses [S3].


<pre>
* [S1] - The instructor may navigate to the list of assignments. Assignments with quizzes enabled will provide a link for the instructor to follow and view student quizzes.
  # Appends the hyperlink to a list that is stored in YAML format in the DB
* [S2] - The instructor shall be presented with the quiz title, questions, and answer choices. The correct answer choices will be in bold.
  # @exception  If is hyperlink was already there
* [S3] - The score for each student who has taken the quiz shall be listed along with the average quiz score.
  #            If it is an invalid URL
  def submit_hyperlink(hyperlink)
    hyperlink.strip!
    raise "The hyperlink cannot be empty" if hyperlink.empty?
    url = URI.parse(hyperlink)
    # If not a valid URL, it will throw an exception
    Net::HTTP.start(url.host, url.port)
    hyperlinks = self.hyperlinks_array
    hyperlinks << hyperlink
    self.submitted_hyperlinks = YAML::dump(hyperlinks)
    self.save
  end


  # Note: This method is not used yet. It is here in the case it will be
==Students can create and edit quizzes==
    needed.
A student navigates to the assignment work page and chooses to create a new quiz [S1] or edit an existing one [S2]. By doing so they are able to choose a quiz title and quiz questions [S3]. Invalid input results in an error message prompting the student to fix the issue [E1].
  # @exception If the index does not exist in the array
  def remove_hyperlink(hyperlink_to_delete)
    hyperlinks = self.hyperlinks_array
    hyperlinks.delete(hyperlink_to_delete)
    self.submitted_hyperlinks = YAML::dump(hyperlinks)
    self.save
  end


  def hyperlinks
* [S1] - A student may only create a quiz if they have not yet done so.
    team.try(:hyperlinks) || []
* [S2] - A student may only edit a quiz if they have previously created one.
  end
* [S3] - The quiz has the number of quiz questions defined by the instructor in the assignment.
  def hyperlinks_array
    self.submitted_hyperlinks.blank? ? [] :YAML::load(self.submitted_hyperlinks)
  end
</pre>


Similar refactoring work needs to be done for the '''directory_num''' field as well.
* [E1] - Possible errors include:
Finally, the refactoring needs to be thoroughly tested by writing test cases for student-submitted hyperlinks and files by making sure that:
** The quiz has no name.
*the submitted hyperlinks and files are correctly displayed on the submitted content page.
** One or more questions is missing text.
*the reviewer can see the submitted content from the reviewees.
** One or more choices is missing text.
*the instructor can see the submitted content from the reviewees.
** A question is missing a correct answer choice.


== Use Cases ==
==Students can take quizzes on assignments they have reviewed==
'''Use Case 1:''' Submit hyperlink for an assignment by student.
A student decides to take a quiz [S1] on another team's assignment that they have previously reviewed. They select an artifact for review [S2] and fill in their answers. After submitting the quiz they can view their score on the take quizzes page [S3] and see question-by-question scores by clicking view [S4].
'''Description:''' A student who is an assignment participant should be able to submit hyperlink on the “Your work” section of an assignment.
'''Preconditions:''' Student should be a participant for the assignment he is trying to submit the hyperlink.
'''Postconditions:''' The submitted hyperlink should be stored in the teams table of the database.
'''Success Scenario:'''
#The submitted hyperlink is visible every time the student views the “Your work” section for the Assignment.
#All other team members should be able to view the submitted hyperlink.
#A reviewer should be able to see the submitted hyperlink.
#An instructor should be able to see the submitted hyperlink.


'''Use Case 2:''' Submit file for an assignment by student.
* [S1] - The student may only take a quiz during a stage in which the assignment is configured to allow them to do so.
* [S2] - Only quizzes from submissions they have reviewed are available.
'''Description:''' A student who is an assignment participant should be able to submit a file on the “Your work” section of an assignment.
* [S3] - The take quizzes page lists the final score for each quiz.
* [S4] - Choosing to view a quiz will show a student each question along with their answers and the correct answers.
'''Preconditions:''' Student should be a participant for the assignment he is trying to submit the file.
'''Postconditions:''' The directory_num field which stores the directory number of the directory storing all the submitted files for a team should be stored in the teams table of the database.
'''Success Scenario:'''
#The submitted file is visible every time the student views the “Your work” section for the Assignment.
#All other team members should be able to view the submitted file.
#A reviewer should be able to see the submitted file.
#An instructor should be able to see the submitted file.


=Design and Implementation=
Design for the refactoring has been driven by object oriented principles and test-driven-development, and makes use of the existing object hierarchy described in the following UML diagram.


'''Use Case 3:''' Delete hyperlink for an assignment by student.
[[File:Expertiza_quiz_hierarchy.png|Expertiza Quiz Hierarchy]]
'''Description:''' A student who is an assignment participant should be able to delete submitted hyperlink on the “Your work” section of an assignment.
'''Preconditions:''' Student should be a participant for the assignment he is trying to delete the hyperlink.
'''Postconditions:''' The deleted hyperlink should be removed from the teams table in DB.
'''Success Scenario:'''
#The deleted hyperlink should not be visible the next time the student clicks on “Your work” section.
#All other team members should not see the deleted hyperlink.
#A reviewer should not be able to see the deleted hyperlink.
#An instructor should not be able to see the deleted hyperlink.


See the below sections for a part-by-part breakdown of the planned implementation for this project.


= Accomplishments =
==Tests==
Quiz tests will be added to the file spec/features/quiz_spec.rb and will make use of
* rspec
* capybara
* selenium


The project was divided into 4 categories:
Tests will be made which fully cover the expected behavior of each listed scenario in the Use Case.
# Database migrations
# Refactor ''submitted_hyperlink'' related methods
# Refactor ''directory_num'' related methods
# Testing
<br>


We followed the steps listed below in the process of refactoring the submitted content controller:
==Refactoring==
Each of the following files will be refactored as described.


* Writing database migrations to move the ''submitted_hyperlinks'' and ''directory_num'' attributes from the Participants table to the Teams table.
===QuestionnaireController===
* Rewriting ''submitted_hyperlinks'' related methods in the Participants model to read from and write to the Teams table.
Refactor the <code>valid_quiz</code> method. It will be renamed to <code>validate_quiz</code> to more properly describe the action that occurs as a result of it being called. The behavior will be changed to construct a new quiz questionnaire with submitted questions wich will be validated using <code>quiz.valid?</code>.
* Modifying the corresponding views to read ''hyperlinks_array'' instead of ''hyperlinks''. ''hyperlinks_array'' stores the the submissions of all the team members of a team.
* Removing ''directory_num'' related methods from the participant model.
* Rewriting ''directory_num'' related methods in the team model.
* Rewriting the corresponding views to read ''directory_num'' from the teams table instead of the participants table.
* For testing, we first created fixtures for all required tables.
* We tested our changes by writing functional tests to test the SubmittedContentController.
* Our test suite included the following tests:
** Testing submission of a hyperlink by one student - Check the count of submmited hyperlinks, verify that the hyperlink is stored properly in the database and displayed correctly from the UI.
** Testing submission of two hyperlinks by two students in a team -  Check the count of submmited hyperlinks, verify that the hyperlink is stored properly in the database and displayed correctly from the UI.
** Testing submission of the same hyperlink by two students in a team - Check that the duplicate hyperlinks does not get stored.
** Testing deletion of a hyperlink - Check the count of submmited hyperlinks and the updated list is displayed correctly from the UI.
** Testing submission of one file by one student - Check the submission of a file and check the updated count of submitted files.
** Testing deletion of one file by one student - Check the deletion of a submitted file and check the updated count of submitted files.
** Testing submission of multiple files by multiple students - Check the submission of multiple files and check the updated count of submitted files.
** Testing deletion of multiple files by multiple students - Check the deletion of multiple submitted files and check the updated count of submitted files.


<br>
===QuizQuestionnaire===
We can conclusively say that, after refactoring the submitted content controller, the query time for retrieving submissions will improve significantly as they are stored together on a team-basis rather than for each participant.
The following fields will be validated using ActiveRecord validations.
* Presence of name


Video demo of our work can be found at https://www.youtube.com/watch?v=VHVWF_UKgOc&feature=youtu.be <br>
===QuizQuestion===
GitHub repository containing the refactoring changes can be found at https://github.com/nitinnagaraja/expertiza/ <br>
The following fields will be validated using ActiveRecord validations
Link to the pull request to Expertiza is https://github.com/expertiza/expertiza/pull/629
* Presence of text
* Presence of type
* Has a correct answer


= Future Work =
===QuizQuestionChoice===
A proposal for further improvement would be to possibly create a new '''submitted_content''' table in the database. This table could include attributes like '''user_id''', '''team_id''', '''can_submit''', '''hyperlink''', '''directory_num''', '''uploaded_at''', '''updated_at'''. This approach would resolve the existing hyperlinks issue and more importantly, aid in recording a history of the submitted hyperlinks. There could be a “Show previous submissions” view for a team. So, if ever there’s a scenario where a student or an instructor needs to revert or go back and check a previous submission, he/she can easily access it from this table.
The following fields will be validated using ActiveRecord validations
* Presence of text


===Test Cases===
In order to verify that refactoring has been done correctly and maintains the same external behavior, we will write test cases to the existing implementation. These test cases will be the ground truth of the validation feature. Refactored code will not be considered complete until all of these test cases once again pass.


= References =
= References =
<references/>
<references/>

Latest revision as of 01:38, 16 April 2016

CSC/ECE 517 Spring 2016 E1621: Refactor and test the quizzing feature

Introduction to Expertiza

Expertiza<ref>https://github.com/expertiza/expertiza</ref> is a project developed using Ruby on Rails<ref>http://guides.rubyonrails.org/getting_started.html</ref>. It provides features like peer review, team assignments and submission of projects. This can be achieved by submitting code base, URL of hosted code on remote server and Wiki submissions. It is an open source application and the code can be cloned from GitHub. This application provides an efficient way to manage assignments, grades and reviews.

Problem Statement

The Expertiza quiz feature is in need of refactoring. Currently, quizzes are checked by questionnaires_controller.valid_quiz. This method is rather long, involved, and does not take advantage of object-oriented practices. It combs through the parameters hash received from a request and manually checks all quiz fields, all question fields, all question choice fields, and verifies that each question has a correct choice.

We will refactor the method to construct a quiz questionniare and then call valid? to recursively validate the object tree using ActiveRecord validations. This is an improvement over the current code which checks all questions manually. The purpose of refactoring in this manner is to enhance readability, DRYness, and maintainability of Expertiza code.

In addition to this refactoring we will implement integration testing on the quiz feature to verify that the expected behavior is present for all use cases.

A full description of the assignment may be found here

Classes involved

  • questionnaires_controller.rb
  • quiz_quesetionnaire.rb
  • quiz_question.rb
  • quiz_question_choice.rb
  • spec/features/quiz_spec.rb

Use Case and Scenarios

The Quiz Use Case allows for instructors to create and manage assignments where students may write quizzes for reviewers on materials they have submitted. See the following scenarios for a complete description of how the feature is expected to work.

Instructors are able to create and manage an assignment's quiz feature

An instructor chooses to create an assignment that has a quiz [S1]. While editing the assignment they may choose the number of quiz questions [S2] and set which phases students are allowed to take the quizzes [S3].

  • [S1] - When creating the assignment there is a checkbox labeled "has quiz." Checking this box creates an assignment that includes a quiz.
  • [S2] - When an assignment has a quiz there is an input field that accepts the number of questions that will be on each quiz. Setting this number appropriately changes the number of quiz questions.
  • [S3] - Students may not take quizzes on a phase that does not allow them to do so. When on a stage that does allow for quizzes, they may take quizzes on work that they have reviewed.

Instructors can view quiz questions and scores from the tree view

After an assignment has been created the instructor may choose to view student quizzes from the tree view [S1]. While on the quiz view page they see student quizzes [S2] and student responses [S3].

  • [S1] - The instructor may navigate to the list of assignments. Assignments with quizzes enabled will provide a link for the instructor to follow and view student quizzes.
  • [S2] - The instructor shall be presented with the quiz title, questions, and answer choices. The correct answer choices will be in bold.
  • [S3] - The score for each student who has taken the quiz shall be listed along with the average quiz score.

Students can create and edit quizzes

A student navigates to the assignment work page and chooses to create a new quiz [S1] or edit an existing one [S2]. By doing so they are able to choose a quiz title and quiz questions [S3]. Invalid input results in an error message prompting the student to fix the issue [E1].

  • [S1] - A student may only create a quiz if they have not yet done so.
  • [S2] - A student may only edit a quiz if they have previously created one.
  • [S3] - The quiz has the number of quiz questions defined by the instructor in the assignment.
  • [E1] - Possible errors include:
    • The quiz has no name.
    • One or more questions is missing text.
    • One or more choices is missing text.
    • A question is missing a correct answer choice.

Students can take quizzes on assignments they have reviewed

A student decides to take a quiz [S1] on another team's assignment that they have previously reviewed. They select an artifact for review [S2] and fill in their answers. After submitting the quiz they can view their score on the take quizzes page [S3] and see question-by-question scores by clicking view [S4].

  • [S1] - The student may only take a quiz during a stage in which the assignment is configured to allow them to do so.
  • [S2] - Only quizzes from submissions they have reviewed are available.
  • [S3] - The take quizzes page lists the final score for each quiz.
  • [S4] - Choosing to view a quiz will show a student each question along with their answers and the correct answers.

Design and Implementation

Design for the refactoring has been driven by object oriented principles and test-driven-development, and makes use of the existing object hierarchy described in the following UML diagram.

Expertiza Quiz Hierarchy

See the below sections for a part-by-part breakdown of the planned implementation for this project.

Tests

Quiz tests will be added to the file spec/features/quiz_spec.rb and will make use of

  • rspec
  • capybara
  • selenium

Tests will be made which fully cover the expected behavior of each listed scenario in the Use Case.

Refactoring

Each of the following files will be refactored as described.

QuestionnaireController

Refactor the valid_quiz method. It will be renamed to validate_quiz to more properly describe the action that occurs as a result of it being called. The behavior will be changed to construct a new quiz questionnaire with submitted questions wich will be validated using quiz.valid?.

QuizQuestionnaire

The following fields will be validated using ActiveRecord validations.

  • Presence of name

QuizQuestion

The following fields will be validated using ActiveRecord validations

  • Presence of text
  • Presence of type
  • Has a correct answer

QuizQuestionChoice

The following fields will be validated using ActiveRecord validations

  • Presence of text

Test Cases

In order to verify that refactoring has been done correctly and maintains the same external behavior, we will write test cases to the existing implementation. These test cases will be the ground truth of the validation feature. Refactored code will not be considered complete until all of these test cases once again pass.

References

<references/>