User:Mister98/sandbox
This wiki page is for the description of changes made under E1602 OSS assignment for Spring 2016, CSC/ECE 517.
Introduction
[edit]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 i, 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
- Multiple Choice Radio
- Multiple Choice Checkbox
- 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
[edit]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
[edit]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.
<% if question_type.eql? 'MultipleChoiceRadio' %>
<% QuizQuestionChoice.where(question_id: question.id).each do |answer| %>
<% if(answer.iscorrect) %>
<b><%= p answer.txt %></b> -- Correct <br>
<%else%>
<%= p answer.txt %><br>
<% end %>
<% end %>
<br>
<%user_answer=Answer.where(response_id: @response.id, question_id: question.id).first%>
Your answer is: <b><%= user_answer.comments %></b>
<%if user_answer.answer==1%>
<img src="/assets/Check-icon.png"/>
<%else%>
<img src="/assets/delete_icon.png"/>
<%end%>
<br><br>
<% 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)%>
<b>Question <%= i %>: </b><%= label_tag "#{question.id}", question.txt %> <br>
<%= 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: <b>"
html+= QuizQuestionChoice.where(question_id: @question.id,iscorrect: 1).first.txt
html+= "</b><br/>"
html+= "Your answer is: <b>"
html+= answer.first.coments + "</b>"
if(answer.first.answer == 1)
html+= "<img src=/assets/Check-icon.png/>"
else
html+= "<img src=/assets/delete_icon.png/>"
end
html+= "<br><br>"
end
Remove irrelevant comments from the student_quizzes_controller
[edit]The student_quizzes_controller has a few comments which are irrelevant and make the method bulky. These could be safely removed from the code.
This is a user sandbox of Mister98. You can use it for testing or practicing edits. This is not the sandbox where you should draft your assigned article for a dashboard.wikiedu.org course. To find the right sandbox for your assignment, visit your Dashboard course page and follow the Sandbox Draft link for your assigned article in the My Articles section. |