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

Conversation

Danyal-Faheem
Copy link
Collaborator

@Danyal-Faheem Danyal-Faheem commented Nov 15, 2023

Changes

  • Added a service to the edx-platform runtime through which we can get the tally of the poll and can update the tally as well
  • Anonymous voter can see the results of the poll after voting as well now

Service
A service was necessary as the FieldDataCache object was being used by the xblocks to store and update data. FieldDataCache was not storing or fetching cache for anonymous users. As the cache was not present in the case of anonymous users, the ORM would try to add a new entry for the poll with the same primary key, thereby resulting in an integrity error.

To get around this and not having to change the FieldDataCache class, we decided to create a service and then update the tally in the database ourselves rather than let the LMS handle it in the case of an anonymous users vote.

IP Hashing

IP hashing was not done as xblocks in openedx have a student_module table where they store every interaction of the student with every single xblock. Each vote of the poll by an authenticated user is stored there. Xblocks manage this data themselves and they do not keep a similar record for unauthenticated users.

Unauthenticated users have their state managed for the session by the LMS itself and then the data is deleted. To store IP hash, we will need to keep track of unauthenticated users ourselves. For that reason, we would have had to create a new model and store the IP hash there. To avoid the scenario of running migrations, we did not go through with this idea.

Authenticated users flow

Authenticated users have no difference in the experience from before.

Fixes

  • Max_submissions was not rendering when max_submissions is set to more than 1. That is now fixed. The issue was that the max_submissions value was not being set on frontend after getting the response from the backend.

poll/poll.py Outdated
@@ -662,6 +664,12 @@ def get_results(self, data, suffix=''):
if self.private_results and not self.can_view_private_results():
detail, total = {}, None
else:
# Get tally from the db directly
tally = self.get_tally()
if not self.runtime._services['user'].get_current_user().opt_attrs['edx-platform.is_authenticated'] and tally:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code readability could be improved by converting first condition into a function.

Suggested change
if not self.runtime._services['user'].get_current_user().opt_attrs['edx-platform.is_authenticated'] and tally:
if self.is_anonymous_user() and tally:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@Danyal-Faheem Danyal-Faheem self-assigned this Nov 23, 2023
Copy link
Collaborator

@ahmed-arb ahmed-arb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@Danyal-Faheem Danyal-Faheem merged commit 1d40d2d into develop Nov 23, 2023
@Danyal-Faheem Danyal-Faheem deleted the Danyal-Faheem/make-polls-public branch November 23, 2023 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants