From 00b53287d52d6ef9bf1bf0baebbb164609dbe6de Mon Sep 17 00:00:00 2001 From: Ahtisham Shahid Date: Thu, 2 Dec 2021 17:05:35 +0500 Subject: [PATCH] Added anonymous user id and extra params in program lti (#29429) * fix: added anonymous user id and extra params in program lti * refactor: updated flag name * fix: fixed linter issues --- lms/djangoapps/learner_dashboard/programs.py | 34 ++++++++++++++----- .../learner_dashboard/tests/test_programs.py | 6 ++-- .../spec/program_details_sidebar_view_spec.js | 4 +-- .../spec/program_details_view_spec.js | 2 +- .../views/program_details_sidebar_view.js | 4 +-- .../views/program_details_view.js | 6 ++-- .../program_details_fragment.html | 2 +- .../program_details_sidebar.underscore | 2 +- .../program_details_tab_view.underscore | 6 ++-- 9 files changed, 41 insertions(+), 25 deletions(-) diff --git a/lms/djangoapps/learner_dashboard/programs.py b/lms/djangoapps/learner_dashboard/programs.py index b3dc1cb5bd..3da8a567fe 100644 --- a/lms/djangoapps/learner_dashboard/programs.py +++ b/lms/djangoapps/learner_dashboard/programs.py @@ -4,11 +4,12 @@ Fragments for rendering programs. import json +from urllib.parse import quote from django.contrib.sites.shortcuts import get_current_site from django.http import Http404 from django.template.loader import render_to_string from django.urls import reverse -from django.utils.translation import gettext_lazy as _ # lint-amnesty, pylint: disable=unused-import +from django.utils.translation import get_language, to_locale, gettext_lazy as _ # lint-amnesty, pylint: disable=unused-import from lti_consumer.lti_1p1.contrib.django import lti_embed from web_fragments.fragment import Fragment @@ -17,7 +18,7 @@ from lms.djangoapps.commerce.utils import EcommerceService from lms.djangoapps.learner_dashboard.utils import FAKE_COURSE_KEY, program_tab_view_is_enabled, strip_course_id from openedx.core.djangoapps.catalog.constants import PathwayType -from openedx.core.djangoapps.catalog.utils import get_pathways +from openedx.core.djangoapps.catalog.utils import get_pathways, get_programs from openedx.core.djangoapps.credentials.utils import get_credentials_records_url from openedx.core.djangoapps.discussions.models import ProgramDiscussionsConfiguration from openedx.core.djangoapps.plugin_api.views import EdxFragmentView @@ -30,6 +31,7 @@ from openedx.core.djangoapps.programs.utils import ( ) from openedx.core.djangoapps.user_api.preferences.api import get_user_preferences from openedx.core.djangolib.markup import HTML +from common.djangoapps.student.models import anonymous_id_for_user class ProgramsFragmentView(EdxFragmentView): @@ -147,9 +149,9 @@ class ProgramDetailsFragmentView(EdxFragmentView): 'certificate_data': certificate_data, 'industry_pathways': industry_pathways, 'credit_pathways': credit_pathways, - 'program_discussions_enabled': program_tab_view_is_enabled(), + 'program_tab_view_enabled': program_tab_view_is_enabled(), 'discussion_fragment': { - 'enabled': bool(program_discussion_lti.configuration), + 'configured': bool(program_discussion_lti.configuration), 'iframe': program_discussion_lti.render_iframe() } } @@ -175,6 +177,7 @@ class ProgramDiscussionLTI: def __init__(self, program_uuid, request): self.program_uuid = program_uuid + self.program = get_programs(uuid=self.program_uuid) self.request = request self.configuration = self.get_configuration() @@ -205,6 +208,16 @@ class ProgramDiscussionLTI: all_roles = [basic_role] return ','.join(all_roles) + def _get_additional_lti_parameters(self): + lti_config = self.configuration.lti_configuration + return lti_config.lti_config.get('additional_parameters', {}) + + def _get_context_title(self) -> str: + return "{} - {}".format( + self.program.get('title', ''), + self.program.get('subtitle', ''), + ) + def _get_lti_embed_code(self) -> str: """ Returns the LTI embed code for embedding in the program discussions tab @@ -217,13 +230,16 @@ class ProgramDiscussionLTI: return lti_embed( html_element_id='lti-tab-launcher', lti_consumer=self.configuration.lti_configuration.get_lti_consumer(), - resource_link_id=resource_link_id, - user_id=str(self.request.user.id), + resource_link_id=quote(resource_link_id), + user_id=quote(anonymous_id_for_user(self.request.user, None)), roles=self.get_user_roles(), - context_id=self.program_uuid, - context_title=self.program_uuid, + context_id=quote(self.program_uuid), + context_title=self._get_context_title(), context_label=self.program_uuid, - result_sourcedid=result_sourcedid + result_sourcedid=quote(result_sourcedid), + locale=to_locale(get_language()), + additional_params=self._get_additional_lti_parameters() + ) def render_iframe(self) -> str: diff --git a/lms/djangoapps/learner_dashboard/tests/test_programs.py b/lms/djangoapps/learner_dashboard/tests/test_programs.py index af75fb48c5..ad3963d37e 100644 --- a/lms/djangoapps/learner_dashboard/tests/test_programs.py +++ b/lms/djangoapps/learner_dashboard/tests/test_programs.py @@ -328,8 +328,8 @@ class TestProgramDetailsFragmentView(SharedModuleStoreTestCase, ProgramCacheMixi def test_discussion_flags_exist(self): """ - Test if programDiscussionEnabled and discussionFragment exist in html. + Test if programTabViewEnabled and discussionFragment exist in html. """ response = self.client.get(self.url) - self.assertContains(response, 'programDiscussionEnabled: true',) - self.assertContains(response, 'discussionFragment: {"enabled": false, "iframe": ""') + self.assertContains(response, 'programTabViewEnabled: true',) + self.assertContains(response, 'discussionFragment: {"configured": false, "iframe": ""') diff --git a/lms/static/js/learner_dashboard/spec/program_details_sidebar_view_spec.js b/lms/static/js/learner_dashboard/spec/program_details_sidebar_view_spec.js index 7bb88f7955..cc1fd64a40 100644 --- a/lms/static/js/learner_dashboard/spec/program_details_sidebar_view_spec.js +++ b/lms/static/js/learner_dashboard/spec/program_details_sidebar_view_spec.js @@ -57,7 +57,7 @@ describe('Program Progress View', () => { programRecordUrl: '/foo/bar', industryPathways: data.industryPathways, creditPathways: data.creditPathways, - programDiscussionEnabled: false + programTabViewEnabled: false }); @@ -155,7 +155,7 @@ describe('Program Progress View', () => { programRecordUrl: '/foo/bar', industryPathways: [], creditPathways: [], - programDiscussionEnabled: false + programTabViewEnabled: false }); expect(emptyView.$('.program-credit-pathways .divider-heading')).toHaveLength(0); diff --git a/lms/static/js/learner_dashboard/spec/program_details_view_spec.js b/lms/static/js/learner_dashboard/spec/program_details_view_spec.js index 8a2de60ad6..1fe537278f 100644 --- a/lms/static/js/learner_dashboard/spec/program_details_view_spec.js +++ b/lms/static/js/learner_dashboard/spec/program_details_view_spec.js @@ -493,7 +493,7 @@ describe('Program Details Header View', () => { destination_url: 'industry.com', }, ], - programDiscussionEnabled: false + programTabViewEnabled: false }; const data = options.programData; diff --git a/lms/static/js/learner_dashboard/views/program_details_sidebar_view.js b/lms/static/js/learner_dashboard/views/program_details_sidebar_view.js index ff4a32427b..7b30c12bbe 100644 --- a/lms/static/js/learner_dashboard/views/program_details_sidebar_view.js +++ b/lms/static/js/learner_dashboard/views/program_details_sidebar_view.js @@ -29,7 +29,7 @@ class ProgramDetailsSidebarView extends Backbone.View { this.industryPathways = options.industryPathways; this.creditPathways = options.creditPathways; this.programModel = options.model; - this.programDiscussionEnabled = options.programDiscussionEnabled; + this.programTabViewEnabled = options.programTabViewEnabled; this.render(); } @@ -40,7 +40,7 @@ class ProgramDetailsSidebarView extends Backbone.View { programRecordUrl: this.programRecordUrl, industryPathways: this.industryPathways, creditPathways: this.creditPathways, - programDiscussionEnabled: this.programDiscussionEnabled + programTabViewEnabled: this.programTabViewEnabled }); HtmlUtils.setHtml(this.$el, this.tpl(data)); diff --git a/lms/static/js/learner_dashboard/views/program_details_view.js b/lms/static/js/learner_dashboard/views/program_details_view.js index 996942acbb..43af3c768d 100644 --- a/lms/static/js/learner_dashboard/views/program_details_view.js +++ b/lms/static/js/learner_dashboard/views/program_details_view.js @@ -27,7 +27,7 @@ class ProgramDetailsView extends Backbone.View { initialize(options) { this.options = options; - if (this.options.programDiscussionEnabled) { + if (this.options.programTabViewEnabled) { this.tpl = HtmlUtils.template(tabPageTpl); } else { this.tpl = HtmlUtils.template(pageTpl); @@ -79,7 +79,7 @@ class ProgramDetailsView extends Backbone.View { remainingCount, completedCount, completeProgramURL: buyButtonUrl, - programDiscussionEnabled: this.options.programDiscussionEnabled, + programTabViewEnabled: this.options.programTabViewEnabled, industryPathways: this.options.industryPathways, creditPathways: this.options.creditPathways, discussionFragment: this.options.discussionFragment, @@ -132,7 +132,7 @@ class ProgramDetailsView extends Backbone.View { programRecordUrl: this.options.urls.program_record_url, industryPathways: this.options.industryPathways, creditPathways: this.options.creditPathways, - programDiscussionEnabled: this.options.programDiscussionEnabled, + programTabViewEnabled: this.options.programTabViewEnabled, }); } diff --git a/lms/templates/learner_dashboard/program_details_fragment.html b/lms/templates/learner_dashboard/program_details_fragment.html index 018a237f55..d4d59042dd 100644 --- a/lms/templates/learner_dashboard/program_details_fragment.html +++ b/lms/templates/learner_dashboard/program_details_fragment.html @@ -20,7 +20,7 @@ ProgramDetailsFactory({ userPreferences: ${user_preferences | n, dump_js_escaped_json}, industryPathways: ${industry_pathways | n, dump_js_escaped_json}, creditPathways: ${credit_pathways | n, dump_js_escaped_json}, - programDiscussionEnabled: ${program_discussions_enabled | n, dump_js_escaped_json}, + programTabViewEnabled: ${program_tab_view_enabled | n, dump_js_escaped_json}, discussionFragment: ${discussion_fragment, | n, dump_js_escaped_json} }); diff --git a/lms/templates/learner_dashboard/program_details_sidebar.underscore b/lms/templates/learner_dashboard/program_details_sidebar.underscore index 2c4dd9415c..aa51bc724d 100644 --- a/lms/templates/learner_dashboard/program_details_sidebar.underscore +++ b/lms/templates/learner_dashboard/program_details_sidebar.underscore @@ -21,7 +21,7 @@ <% } %> -<% if (!programDiscussionEnabled) { %> +<% if (!programTabViewEnabled) { %> <% if (creditPathways.length > 0) { %>