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

Implemented timezone sync to moodle #116

Merged
merged 1 commit into from
Dec 4, 2024
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
29 changes: 29 additions & 0 deletions openedx/core/djangoapps/courseware_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@

from .serializers import CourseInfoSerializer

from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
import crum
import requests
import logging
from pytz import UTC, timezone
log = logging.getLogger(__name__)

class CoursewareMeta:
"""
Expand Down Expand Up @@ -504,6 +510,28 @@ def set_last_seen_courseware_timezone(self, user):
defaults={'last_seen_courseware_timezone': browser_timezone},
)

#SA || updateTimeZoneToMoodle
def update_moodle_timezone(self, user):
browser_timezone = self.request.query_params.get('browser_timezone', None)

from lms.djangoapps.courseware.context_processor import user_timezone_locale_prefs
user_timezone_locale = user_timezone_locale_prefs(crum.get_current_request())
user_timezone = timezone(user_timezone_locale['user_timezone'] or browser_timezone or str(UTC))

try:
moodle_url = configuration_helpers.get_value("MOODLE_URL") + "/webservice/rest/server.php"
payload = {
"wstoken" : configuration_helpers.get_value("MOODLE_TOKEN", ""),
"wsfunction" : "core_user_update_users",
"moodlewsrestformat" : "json",
"users[0][id]" : -100,
"users[0][old_email]": user.email,
"users[0][timezone]" : user_timezone
}
requests.post(moodle_url, data = payload)
except Exception as e:
log.error(e)

def get_object(self):
"""
Return the requested course object, if the user has appropriate
Expand All @@ -525,6 +553,7 @@ def get_object(self):

# Record a user's browser timezone
self.set_last_seen_courseware_timezone(original_user)
self.update_moodle_timezone(original_user)

return overview

Expand Down
22 changes: 22 additions & 0 deletions openedx/core/djangoapps/user_api/preferences/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
update_user_preferences
)

from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
import requests
import logging
log = logging.getLogger(__name__)

class PreferencesView(APIView):
"""
Expand Down Expand Up @@ -125,6 +129,24 @@ def patch(self, request, username):
)
try:
with transaction.atomic():
#SA || updateTimeZoneToMoodle
try:
payload = request.data
if 'time_zone' in payload:
time_zone = payload['time_zone'] if payload['time_zone'] else '99' #default value 99 refers to local timezone in moodle
moodle_url = configuration_helpers.get_value("MOODLE_URL") + "/webservice/rest/server.php"
payload = {
"wstoken" : configuration_helpers.get_value("MOODLE_TOKEN", ""),
"wsfunction" : "core_user_update_users",
"moodlewsrestformat" : "json",
"users[0][id]" : -100,
"users[0][old_email]": request.user.email,
"users[0][timezone]" : time_zone
}
requests.post(moodle_url, data = payload)
except Exception as e:
log.error(e)

update_user_preferences(request.user, request.data, user=username)
except UserNotAuthorized:
return Response(status=status.HTTP_403_FORBIDDEN)
Expand Down
Loading