expose poll_choice#is_correct to students when available

fixes CNVS-13474
This commit exposes the is_correct attribute on poll_choices when a
student has submitted their answer to the latest poll_session, and that
poll_session has been closed.

Test plan
- As a teacher, create a poll, relevant poll choices, and a poll session
- Publish the poll session
- As a student, at this point is_correct should not be viewable on poll
  choices
- As a student, submit a poll submission
- As a student, at this point is_correct should not be viewable on poll
  choices
- As a teacher, close the poll session
- As a student, at this point is_correct should be viewable on poll
  choices

Change-Id: I20a8bb534affe5e1a23c1dddbb727043535dd23d
Reviewed-on: https://gerrit.instructure.com/36031
Reviewed-by: Jason Madsen <jmadsen@instructure.com>
Tested-by: Jenkins <jenkins@instructure.com>
QA-Review: Trevor deHaan <tdehaan@instructure.com>
Product-Review: Josh Simpson <jsimpson@instructure.com>
This commit is contained in:
Josh Simpson 2014-06-06 10:59:55 -06:00
parent 21de3036c0
commit 38917d0c32
6 changed files with 82 additions and 8 deletions

View File

@ -85,7 +85,6 @@ module Polling
# #
def show def show
@poll_choice = @poll.poll_choices.find(params[:id]) @poll_choice = @poll.poll_choices.find(params[:id])
if authorized_action(@poll, @current_user, :read) if authorized_action(@poll, @current_user, :read)
render json: serialize_jsonapi(@poll_choice) render json: serialize_jsonapi(@poll_choice)
end end

View File

@ -70,7 +70,7 @@ module Polling
# } # }
# #
def index def index
@polls = @current_user.polls @polls = @current_user.polls.order('created_at DESC')
@polls = Api.paginate(@polls, self, api_v1_polls_url) @polls = Api.paginate(@polls, self, api_v1_polls_url)
render json: serialize_jsonapi(@polls) render json: serialize_jsonapi(@polls)

View File

@ -45,6 +45,24 @@ module Polling
can :read can :read
end end
def closed_and_viewable_for?(user)
results = poll_sessions.with_each_shard do |scope|
scope
.joins(:poll_submissions)
.where(["polling_poll_submissions.user_id = ? AND is_published=? AND course_id IN (?) AND (course_section_id IS NULL OR course_section_id IN (?))",
user,
false,
user.enrollments.map(&:course_id).compact,
user.enrollments.map(&:course_section_id).compact]
)
.order('polling_poll_sessions.created_at DESC')
.limit(1)
.exists?
end
results.any?
end
def total_results def total_results
poll_sessions.reduce(Hash.new(0)) do |poll_results, session| poll_sessions.reduce(Hash.new(0)) do |poll_results, session|
poll_results = poll_results.merge(session.results) do |key, poll_result_value, session_result_value| poll_results = poll_results.merge(session.results) do |key, poll_result_value, session_result_value|

View File

@ -31,7 +31,9 @@ module Polling
end end
def student_keys def student_keys
[:id, :text] keys = [:id, :text]
keys << :is_correct if poll.closed_and_viewable_for?(current_user)
keys
end end
end end
end end

View File

@ -114,14 +114,31 @@ describe Polling::PollChoicesController, type: :request do
response.code.should == '200' response.code.should == '200'
end end
it "doesn't display is_correct within poll choices" do context "with opened sessions" do
Polling::PollSession.create!(course: @course, poll: @poll) it "doesn't display is_correct within poll choices" do
Polling::PollSession.create!(course: @course, poll: @poll).publish!
json = get_show json = get_show
poll_choice_json = json['poll_choices'].first poll_choice_json = json['poll_choices'].first
poll_choice_json.should_not have_key('is_correct') poll_choice_json.should_not have_key('is_correct')
end
end end
context "with closed, available sessions" do
it "displays is_correct within poll choices" do
session = Polling::PollSession.create!(course: @course, poll: @poll)
session.publish!
session.poll_submissions.create!(user: @student, poll: @poll, poll_choice: @poll_choice)
session.close!
json = get_show
poll_choice_json = json['poll_choices'].first
poll_choice_json.should have_key('is_correct')
end
end
end end
end end

View File

@ -42,6 +42,44 @@ describe Polling::Poll do
end end
end end
describe "#closed_and_viewable_for?" do
it "returns false if the latest poll session available to the user is opened" do
student = student_in_course(active_user:true).user
poll = @teacher.polls.create!(question: 'A Test Poll')
session = poll.poll_sessions.create(course: @course)
session.publish!
poll.closed_and_viewable_for?(student).should be_false
end
context "the latest poll session available to the user is closed" do
before(:each) do
@student = student_in_course(active_user:true).user
@poll = @teacher.polls.create!(question: 'A Test Poll')
@choice = @poll.poll_choices.create!(text: 'Choice A', is_correct: true)
@session = @poll.poll_sessions.create(course: @course)
end
it "returns true if the user has submitted" do
@session.publish!
@session.poll_submissions.create!(
poll: @poll,
user: @student,
poll_choice: @choice
)
@session.close!
@poll.closed_and_viewable_for?(@student).should be_true
end
it "returns false if the user hasn't submitted" do
@session.publish!
@session.close!
@poll.closed_and_viewable_for?(@student).should be_false
end
end
end
describe "#total_results" do describe "#total_results" do
def create_submission(session, choice) def create_submission(session, choice)
student = student_in_course(active_user:true).user student = student_in_course(active_user:true).user