CSC/ECE 517 Spring 2023 - E2320. Reimplement the Question Hierarchy

From Expertiza_Wiki
Jump to navigation Jump to search

Design Doc

Problem Statement

The current implementation of the question hierarchy is not very clean and contains confusing variable and method names. Also, many methods return long HTML strings which are difficult to read. The goal of this project is to reimplement this part of the application to make it more readable, understandable and maintainable.

Explanation of Feature - Question Hierarchy

The Question class and its sub-classes are used to implement all rubric items, quiz questions, and survey questions in Expertiza. For example, a professor could use this feature to make up a review quiz for their class. There are 3 main types of questions in Experitza: scored, unscored, and upload file.

Here is the hierarchy:

1. Choice question - Choice questions are types of questions that give the quiz taker a list of answers to choose from. It is broken into two subcategories scored and unscored.

  • Scored question - will give a number value depending upon if a user clicks on a particular answer.
    • Scale
    • Criterion
  • Unscored question - don't have a score associated with them and come in a few different styles.
    • Dropdown - only shows the answer choices after the user clicks on the dropdown arrow and clicks on an answer.
    • Multiple Choice - present all the answers choices with a selectable bubble beside them and allow the user to click on the bubble to indicate their answer.
    • Check Box - the user must click on a certain number of boxes beside the answers they think are correct.

2. Text Response - allow the user to actually type in their answer instead of just being provided answers

  • Text Area - question box has a height and width associated with it that can be adjusted
  • Text Field - 1 input line that does not change shape.

3. Upload File - The last type of question is upload file which allows the user to upload a file to use as a quiz (as the name suggests). This option allows a person to upload a pre-made quiz document to be which will be converted into a quiz. So, a professor could type up their quiz questions and answers in a text file then choose upload and have Expertiza convert it into a quiz for them.

Goals

  • Improved naming of variables and methods
  • Included comments for further clarification
  • Implemented partials to deal with the HTML strings
  • Added RSpec test cases

Plan of work

Based on the feedback provided to us by both our mentors and also peer reviews our team has decided upon a few key design goals to work on for the continuation of our work into Program 4. These goals are outlined below.

Design Goals

1. Update code comments

Some methods and classes had little to no comments, and existing comments were not in depth enough to give proper context for functionality. One of our design goals is to write more in depth comments that better explain functionality for the code.

The files listed below have been updated with new comments:

  • questionnaires_controller.rb
  • checkbox.rb
  • criterion.rb
  • dropdown.rb
  • multiple_choice_checkbox.rb
  • multiple_choice_radio.rb
  • scale.rb
  • text_area.rb
  • text_field.rb
  • text_response.rb
  • upload_file.rb

Given below is an example of the comments we've added to clarify what the methods are doing because the method name alone can be confusing. From checkbox.rb:

 # Returns what to display for the first and second inputs (comments and scores).
 def complete_first_second_input(count, answer = nil)
   html = "<input id=\"responses_#{count.to_s}_comments\" name=\"responses[#{count.to_s}][comment]\" type=\"hidden\" value=\"\">"
   html += "<input id=\"responses_#{count.to_s}_score\" name=\"responses[#{count.to_s}][score]\" type=\"hidden\""
   html += if !answer.nil? && (answer.answer == 1)
             'value="1"'
           else
             'value="0"'
           end
   html += '>'
   html
 end
 
 # Returns what to display for the third input (the checkbox itself).
 def complete_third_input(count, answer = nil)
   html = "<input id=\"responses_#{count.to_s}_checkbox\" type=\"checkbox\" onchange=\"checkbox#{count.to_s}Changed()\""
   html += 'checked="checked"' if !answer.nil? && (answer.answer == 1)
   html += '>'
   html
 end
 
 # Create the executable script for client-side interaction on the checkbox
 def complete_script(count)
   html = "<script>function checkbox#{count.to_s}Changed() {"
   html += " var checkbox = jQuery(\"#responses_#{count.to_s}_checkbox\");"
   html += " var response_score = jQuery(\"#responses_#{count.to_s}_score\");"
   html += 'if (checkbox.is(":checked")) {'
   html += 'response_score.val("1");'
   html += '} else {'
   html += 'response_score.val("0");}}</script>'
   html
 end

2. DRY out code with partials

Another one of our design goals is to implement additional partials for the question types. We added partials by looking at the similarities between all three question types and pull out the similarities into a new class.

  • Ex: Question_Controller - Since all of the sub-categories inherit from the Question class we plan to make a question controller to hold all of the common functionality. One example of this can be seen with the method call 'edit' which exists in many of the subclasses like text_response.rb, text_area.rb, scale.rb, and criterion.rb. The partials will be moved into the question controller.

Tests

RSpec

We reimplemented the test cases to match the changes we've made to the question-related models. Front-end testing wasn't necessary because our project doesn't involve changing any controller.

The most notable models that were tested are:

checkbox.rb 
criterion.rb
dropdown.rb
multiple_choice_checkbox.rb
multiple_choice_radio.rb
scale.rb
text_area.rb
text_field.rb
text_response.rb
upload_file.rb

One of the primary goals we had were to make sure that the existing tests still passed. Several tests were removed for the edit methods in a few of the models (such as checkbox_spec.rb and dropdown_spec.rb) because those methods were replaced with partials. Partials have not been fully implemented which is why we did not include tests for them. However, this is something that should be revisited in the future.

Given below is an example of the tests we've written:

describe Dropdown do
 let!(:dropdown) { Dropdown.create(id: 4, type: 'Dropdown', seq: 4.0, txt: 'Test text', weight: 13) }
 let!(:answer) { Answer.create(id: 1, question_id: 4, questionnaire_type_id: 1, answer: 1, comments: "Test comment") }
 describe '#view_question_text' do
   it 'returns the html' do
     html = dropdown.view_question_text

expect(html).to eq(' Test text Dropdown13—')

   end
 end
 describe '#view_completed_question' do
   it 'returns the html' do
     html = dropdown.view_completed_question(1, answer)
     expect(html).to eq('<b>1. Test text</b><BR>&nbsp&nbsp&nbsp&nbspTest comment')
   end
 end
 describe '#complete_for_alternatives' do
   it 'returns the html' do
     alternatives = ['Alternative 1', 'Alternative 2', 'Alternative 3']
     html = dropdown.complete_for_alternatives(alternatives, answer)
     expect(html).to eq('<option value="Alternative 1">Alternative 1</option><option value="Alternative 2">Alternative 2</option><option value="Alternative 3">Alternative 3</option>')
   end
 end
 describe '#complete' do
   it 'returns the html' do
     alternatives = ['Alternative 1|Alternative 2|Alternative 3']
     allow(dropdown).to receive(:alternatives).and_return(alternatives)
     allow(dropdown).to receive(:complete_for_alternatives).and_return()
     html = dropdown.complete(1, answer)
     expect(html).to eq('<p style="width: 80%;"><label for="responses_1"">Test text  </label>'
      + '<input id="responses_1_score" name="responses[1][score]" type="hidden" value="" style="min-width: 100px;">'
      + '<select id="responses_1_comments" label=Test text name="responses[1][comment]"></select></p>')
   end
 end
end

Given below is the full list of tests:

Conclusions

Tasks Completed

We met our goals of adding comments and improving the names of methods and variables to make the code easily understandable to the user. We implemented several partials to handle the HTML strings and added RSpec test cases. We cleaned up the unused comments/code to make the code readable and understandable.

Next Steps

In the future, we would like to fully implement partials and thoroughly test them. We believe partials will improve the code by making it more readable and reusable.

Team

Mentor
  • Prof. Edward F. Gehringer
  • Priyam Garg
Members
  • Colleen Britt
  • Kimberly Jones
  • Priyanka Arghode

References

  1. Expertiza
  2. Organizing Partials
  3. Render Views and Partials Outside Controllers
  4. Github Repo
  5. Link to initial pull request
  6. GitHub Project Board