Skip to content

Commit

Permalink
feat: filter courses by user language by default (#33647)
Browse files Browse the repository at this point in the history
Adds a feature flag to filter courses by users preferred language by default
  • Loading branch information
navinkarkera authored Feb 6, 2024
1 parent d28684d commit 4a46ae9
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
13 changes: 13 additions & 0 deletions lms/djangoapps/courseware/toggles.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@
'RET.enable_optimizely_in_courseware', __name__
)

# .. toggle_name: courseware.discovery_default_language_filter
# .. toggle_implementation: WaffleSwitch
# .. toggle_default: False
# .. toggle_description: Enable courses to be filtered by user language by default.
# .. toggle_use_cases: open_edx
# .. toggle_creation_date: 2023-11-02
# .. toggle_target_removal_date: None
# .. toggle_warning: The ENABLE_COURSE_DISCOVERY feature flag should be enabled.
# .. toggle_tickets: https://github.com/openedx/edx-platform/pull/33647
ENABLE_COURSE_DISCOVERY_DEFAULT_LANGUAGE_FILTER = WaffleSwitch(
f'{WAFFLE_FLAG_NAMESPACE}.discovery_default_language_filter', __name__
)


def courseware_mfe_is_active() -> bool:
"""
Expand Down
7 changes: 6 additions & 1 deletion lms/djangoapps/courseware/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@

from ..block_render import get_block, get_block_by_usage_id, get_block_for_descriptor
from ..tabs import _get_dynamic_tabs
from ..toggles import COURSEWARE_OPTIMIZED_RENDER_XBLOCK
from ..toggles import (
COURSEWARE_OPTIMIZED_RENDER_XBLOCK,
ENABLE_COURSE_DISCOVERY_DEFAULT_LANGUAGE_FILTER,
)

log = logging.getLogger("edx.courseware")

Expand Down Expand Up @@ -275,6 +278,7 @@ def courses(request):
"""
courses_list = []
course_discovery_meanings = getattr(settings, 'COURSE_DISCOVERY_MEANINGS', {})
set_default_filter = ENABLE_COURSE_DISCOVERY_DEFAULT_LANGUAGE_FILTER.is_enabled()
if not settings.FEATURES.get('ENABLE_COURSE_DISCOVERY'):
courses_list = get_courses(request.user)

Expand All @@ -292,6 +296,7 @@ def courses(request):
{
'courses': courses_list,
'course_discovery_meanings': course_discovery_meanings,
'set_default_filter': set_default_filter,
'programs_list': programs_list,
}
)
Expand Down
11 changes: 9 additions & 2 deletions lms/static/js/discovery/discovery_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'js/discovery/views/search_form', 'js/discovery/views/courses_listing',
'js/discovery/views/filter_bar', 'js/discovery/views/refine_sidebar'],
function(Backbone, SearchState, Filters, SearchForm, CoursesListing, FilterBar, RefineSidebar) {
return function(meanings, searchQuery, userLanguage, userTimezone) {
return function(meanings, searchQuery, userLanguage, userTimezone, setDefaultFilter) {
var dispatcher = _.extend({}, Backbone.Events);
var search = new SearchState();
var filters = new Filters();
Expand All @@ -21,10 +21,16 @@
userLanguage: userLanguage,
userTimezone: userTimezone
};
if (setDefaultFilter && userLanguage) {
filters.add({
type: 'language',
query: userLanguage,
name: refineSidebar.termName('language', userLanguage)
});
}
listing = new CoursesListing({model: courseListingModel});

dispatcher.listenTo(form, 'search', function(query) {
filters.reset();
form.showLoadingIndicator();
search.performSearch(query, filters.getTerms());
});
Expand All @@ -42,6 +48,7 @@
dispatcher.listenTo(filterBar, 'clearFilter', removeFilter);

dispatcher.listenTo(filterBar, 'clearAll', function() {
filters.reset();
form.doSearch('');
});

Expand Down
12 changes: 7 additions & 5 deletions lms/static/js/discovery/views/search_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@
},

showNotFoundMessage: function(term) {
var msg = interpolate(
gettext('We couldn\'t find any results for "%s".'),
[_.escape(term)]
);
this.$message.html(msg);
if (term) {
var msg = interpolate(
gettext('We couldn\'t find any results for "%s".'),
[_.escape(term)]
);
this.$message.html(msg);
}
this.clearSearch();
},

Expand Down
32 changes: 31 additions & 1 deletion lms/static/js/spec/discovery/discovery_factory_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ define([
start: '1970-01-01T05:00:00+00:00',
image_url: '/c4x/edX/DemoX/asset/images_course_image.jpg',
org: 'edX',
id: 'edX/DemoX/Demo_Course'
id: 'edX/DemoX/Demo_Course',
language: 'en'
}
}
],
Expand Down Expand Up @@ -195,4 +196,33 @@ define([
expect($('.active-filter [data-value="edX1"]').length).toBe(0);
});
});

describe('discovery.DiscoveryFactory default filters', function() {
beforeEach(function() {
loadFixtures('js/fixtures/discovery.html');
TemplateHelpers.installTemplates([
'templates/discovery/course_card',
'templates/discovery/facet',
'templates/discovery/facet_option',
'templates/discovery/filter',
'templates/discovery/filter_bar'
]);
// setDefaultFilter to true
DiscoveryFactory(MEANINGS, '', 'en', 'Asia/Kolkata', true);

jasmine.clock().install();
});

afterEach(function() {
jasmine.clock().uninstall();
});

it('filters by default language', function() {
var requests = AjaxHelpers.requests(this);
$('.discovery-submit').trigger('click');
var request = AjaxHelpers.currentRequest(requests);
// make sure language filter is set
expect(request.requestBody).toMatch(/language=en/);
});
});
});
11 changes: 9 additions & 2 deletions lms/static/js/spec/discovery/views/search_form_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ define(['jquery', 'js/discovery/views/search_form'], function($, SearchForm) {
it('shows messages', function() {
this.form.showFoundMessage(123);
expect($('#discovery-message')).toContainHtml(123);
this.form.showNotFoundMessage();
expect($('#discovery-message')).not.toBeEmpty();
this.form.showErrorMessage();
expect($('#discovery-message')).not.toBeEmpty();
});

it('shows not found messages', function() {
// message should not be displayed if search term is empty
this.form.showNotFoundMessage();
expect($('#discovery-message')).toBeEmpty();
this.form.showNotFoundMessage('xyz');
expect($('#discovery-message')).not.toBeEmpty();
expect($('#discovery-message')).toContainHtml('xyz');
});

it('shows default error message', function() {
this.form.showErrorMessage();
expect(this.form.$message).toContainHtml('There was an error, try searching again.');
Expand Down
3 changes: 2 additions & 1 deletion lms/templates/courseware/courses.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
${course_discovery_meanings | n, dump_js_escaped_json},
getParameterByName('search_query'),
"${user_language | n, js_escaped_string}",
"${user_timezone | n, js_escaped_string}"
"${user_timezone | n, js_escaped_string}",
${set_default_filter | n, dump_js_escaped_json}
);
</%static:require_module>
</%block>
Expand Down

0 comments on commit 4a46ae9

Please sign in to comment.