CSC/ECE 517 Spring 2016/Refactor different question types from quiz feature: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 2: Line 2:


== Introduction ==
== Introduction ==
'''Background'''
====Background====


Expertiza is an open source project based on Ruby on Rails framework. Expertiza has a quizzing feature which allow student authors to create quiz questions and test the peer reviewers. The idea behind this is, if a reviewer can answer the quiz questions which were created by the author correctly, we assume that the reviewer has read the artifact carefully enough and thereby we trust the peer-review.
Expertiza is an open source project based on Ruby on Rails framework. Expertiza has a quizzing feature which allow student authors to create quiz questions and test the peer reviewers. The idea behind this is, if a reviewer can answer the quiz questions which were created by the author correctly, we assume that the reviewer has read the artifact carefully enough and thereby we trust the peer-review.


'''Current Implementation and Problems'''
====Current Implementation and Problems====
* Following three different types of questions are supported in quizzing feature of Expertiza
* Following three different types of questions are supported in quizzing feature of Expertiza
# Multiple Choice Radio
# Multiple Choice Radio
Line 16: Line 16:
* Code is repeated for different question types.
* Code is repeated for different question types.
* Proper Ruby naming conventions are not followed
* Proper Ruby naming conventions are not followed
'''Changes Implemented'''
====Changes Implemented====
* All logic moved to model. Four separate methods - 'edit', 'view_question_text', 'complete' and 'view_completed_question' in order to generate the same html code for each of the question types. We used polymorphism by declaring all these methods in QuizQuestion class which inherits from Questions class and has sub-types MultipleChoiceRadio, MultipleChoiceCheckbox, and TrueFalse.  
* All logic moved to model. Four separate methods - 'edit', 'view_question_text', 'complete' and 'view_completed_question' in order to generate the same html code for each of the question types. We used polymorphism by declaring all these methods in QuizQuestion class which inherits from Questions class and has sub-types MultipleChoiceRadio, MultipleChoiceCheckbox, and TrueFalse.  
* When questionnaire author creates a quiz, 'edit' method is called. To call this on UI, you can login as a student, click 'create quiz' / 'edit quiz' on 'Your Works' page. The 'edit' method is now called from /view/questionnaires/_quiz_question.html.erb
* When questionnaire author creates a quiz, 'edit' method is called. To call this on UI, you can login as a student, click 'create quiz' / 'edit quiz' on 'Your Works' page. The 'edit' method is now called from /view/questionnaires/_quiz_question.html.erb

Revision as of 23:00, 23 March 2016

This wiki page is for the description of changes made under E1602 OSS assignment for Spring 2016, CSC/ECE 517.

Introduction

Background

Expertiza is an open source project based on Ruby on Rails framework. Expertiza has a quizzing feature which allow student authors to create quiz questions and test the peer reviewers. The idea behind this is, if a reviewer can answer the quiz questions which were created by the author correctly, we assume that the reviewer has read the artifact carefully enough and thereby we trust the peer-review.

Current Implementation and Problems

  • Following three different types of questions are supported in quizzing feature of Expertiza
  1. Multiple Choice Radio
  2. Multiple Choice Checkbox
  3. True/False
  • The current implementation of Quizzing feature is not consistent with the current questions and questionnaires. Since Quiz questionnaire is one sub type of questionnaire, so it should follow the design of other type of questionnaires. But, the current implementation is inconsistent with other questionnaires.
  • In order to create a quiz, the existing code is written in the views which has a series of if-then structures to implement the functionality of each different question type. This kind of implementation makes the code slower and violates the principles of MVC architecture.
  • Current HTML code doesn't looks like source code.
  • Code is repeated for different question types.
  • Proper Ruby naming conventions are not followed

Changes Implemented

  • All logic moved to model. Four separate methods - 'edit', 'view_question_text', 'complete' and 'view_completed_question' in order to generate the same html code for each of the question types. We used polymorphism by declaring all these methods in QuizQuestion class which inherits from Questions class and has sub-types MultipleChoiceRadio, MultipleChoiceCheckbox, and TrueFalse.
  • When questionnaire author creates a quiz, 'edit' method is called. To call this on UI, you can login as a student, click 'create quiz' / 'edit quiz' on 'Your Works' page. The 'edit' method is now called from /view/questionnaires/_quiz_question.html.erb
  • When questions are viewed by the author or the instructor, 'view_question_text' method is called. To call this on UI, you can either login as a student, click 'view quiz' on 'Ypur works' page or login as an instructor, find an assignment with quizzing feature and click the icon 'view quiz questions'. The 'view_question_text' calls view/questionnaires/view.html.erb
  • When the quiz takers tries to take the quiz, “complete” is called. To call this on UI, you can login as a quiz taker, request a quiz to take on “Take quizzes” page, then click “begin”. This method is now called on “view/student_quizzes/take_quiz”.
  • When the quiz taker finished taking a quiz and wants to view the quiz result again,  “view_completed_question” is called. To call this on UI, you can login as a quiz taker, find a finished quiz and click “view”. This method is called on “view/student_quizzes/finished_quiz”.
  • Using polymorphism and DRY principles, code repetition has been eliminated.

Refactoring complete

Logic is written in multiple_choice_checkbox model for multiple choice checkbox question type. Similarly, same process is followed for remaining question types. Now, to begin a quiz, complete method is called from the model.

def complete(count, answer=nil)
     @question = self
     html = ""
     html += label_tag("#{question.id}", question.txt) +"<br>"
     quiz_question_choices = QuizQuestionChoice.where(question_id: @question.id)
     quiz_question_choices.each do |choice|
       if answer=="view"
         html += check_box_tag ("#{question.id}[]", "#{choice.txt}", choice.iscorrect)
         html += label_tag("#{choice.txt}", choice.txt) + "<br>"
       end
       if answer=="take"
         html += check_box_tag ("#{question.id}[]", "#{choice.txt}")
         html += label_tag("#{choice.txt}", choice.txt) + "<br>"
       end
       html.html_safe
     end
    end

Refactoring view_completed_question

The logic for viewing completed question and their answers is written in app/views/student_quizzes/finished_quiz.html.erb using a redundant if-elsif structure as shown below. <syntaxhighlight lang="ruby">

<% if question_type.eql? 'MultipleChoiceRadio' %>
   <% QuizQuestionChoice.where(question_id: question.id).each do |answer|  %>
     <% if(answer.iscorrect) %>
       <%= p answer.txt  %> -- Correct 
<%else%> <%= p answer.txt  %>
<% end %> <% end %>
<%user_answer=Answer.where(response_id: @response.id, question_id: question.id).first%> Your answer is: <%= user_answer.comments  %> <%if user_answer.answer==1%> <img src="/assets/Check-icon.png"/> <%else%> <img src="/assets/delete_icon.png"/> <%end%>

<% elsif question_type.eql? 'MultipleChoiceCheckbox' %> ... <% elsif(question_type == 'TrueFalse') %> ...

The app/views/student_quizzes/finished_quiz.html.erb was refactored by removing the logic from the view and making it clean. We used a single construct which determines question type and answer type by calling view_completed_question method declared in quiz class as shown below.

<%user_answer=Answer.where(response_id: @response.id, question_id:  question.id)%>
Question <%= i %>: <%= label_tag "#{question.id}", question.txt %> 
<%= question.view_completed_question(i,user_answer) %> <% i += 1 %>

Similarly, logic was written in models for each of the question types. Following code snippet shows view_completed_question for true/false question type. def view_completed_question(count, answer)

    @question = self
    html=""
    html+= "Correct Answer is: "
    html+= QuizQuestionChoice.where(question_id: @question.id,iscorrect: 1).first.txt
    html+= "
" html+= "Your answer is: " html+= answer.first.coments + "" if(answer.first.answer == 1) html+= "<img src=/assets/Check-icon.png/>" else html+= "<img src=/assets/delete_icon.png/>" end html+= "

" end

Remove irrelevant comments from the student_quizzes_controller

The student_quizzes_controller has a few comments which are irrelevant and make the method bulky. These could be safely removed from the code.