Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unauthenticated users to vote and see results of polls #2

Merged
merged 5 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions poll/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from xblockutils.resources import ResourceLoader
from xblockutils.settings import ThemableXBlockMixin, XBlockWithSettingsMixin


from .utils import DummyTranslationService, _

try:
Expand Down Expand Up @@ -439,6 +440,8 @@ def wrapper(self, request_json, suffix=''):

@XBlock.wants('settings')
@XBlock.needs('i18n')
@XBlock.needs('user')
@XBlock.needs('anonymous_user_poll')
class PollBlock(PollBase, CSVExportMixin):
"""
Poll XBlock. Allows a teacher to poll users, and presents the results so
Expand Down Expand Up @@ -662,6 +665,14 @@ def get_results(self, data, suffix=''):
if self.private_results and not self.can_view_private_results():
detail, total = {}, None
else:
if self.is_anonymous_user():
anonymous_user_poll_service = self.get_anonymous_user_poll_service()
usage_id = self.scope_ids.usage_id
# Get tally from the db directly
tally = anonymous_user_poll_service.get_tally(usage_id)
self.tally = tally
# Clear dirty fields so that the LMS does not try to update tally
self._clear_dirty_fields()
self.publish_event_from_dict(self.event_namespace + '.view_results', {})
detail, total = self.tally_detail()
return {
Expand All @@ -675,6 +686,8 @@ def get_results(self, data, suffix=''):
# a11y: Transfer block ID to enable creating unique ids for questions and answers in the template
'block_id': self._get_block_id(),
}



@XBlock.json_handler
def vote(self, data, suffix=''):
Expand Down Expand Up @@ -714,18 +727,44 @@ def vote(self, data, suffix=''):
if old_choice is not None:
self.tally[old_choice] -= 1
self.choice = choice
result['max_submissions'] = self.max_submissions

# Check if user is unauthenticated
if self.is_anonymous_user():
anonymous_user_poll_service = self.get_anonymous_user_poll_service()
usage_id = self.scope_ids.usage_id
result, tally_updated = anonymous_user_poll_service.vote(choice, result, usage_id)
if tally_updated:
# Clear the dirty fields so that the LMS knows not to update tally
self._clear_dirty_fields()
# Let the LMS know that the user has voted
self.send_vote_event({'choice': self.choice})
return result

self.tally[choice] += 1
self.submissions_count += 1

result['success'] = True
result['can_vote'] = self.can_vote()
result['submissions_count'] = self.submissions_count
result['max_submissions'] = self.max_submissions


self.send_vote_event({'choice': self.choice})

return result

def get_anonymous_user_poll_service(self):
return self.runtime.service(self, "anonymous_user_poll")

def is_anonymous_user(self):
"""
Return if the current user is authenticated or not
"""
user_service = self.runtime.service(self, 'user')
user = user_service.get_current_user()
return not user.opt_attrs.get('edx-platform.is_authenticated')



@XBlock.json_handler
def studio_submit(self, data, suffix=''):
result = {'success': True, 'errors': []}
Expand Down
1 change: 1 addition & 0 deletions poll/public/js/poll.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function PollUtil (runtime, element, pollType) {
}
var can_vote = data['can_vote'];
$('.poll-current-count', element).text(data['submissions_count']);
$('.poll-max-submissions', element).text(data['max_submissions']);
if (data['max_submissions'] > 1) {
$('.poll-submissions-count', element).show();
}
Expand Down