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

feat: add functionality to create honor course modes through studio #28114

Closed
Closed
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
83 changes: 81 additions & 2 deletions cms/djangoapps/contentstore/views/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
from organizations.api import add_organization_course, ensure_organization
from organizations.exceptions import InvalidOrganizationException
from rest_framework.exceptions import ValidationError

from cms.djangoapps.contentstore.views.certificates import CertificateManager
from cms.djangoapps.contentstore.utils import get_lms_link_for_certificate_web_view, reverse_course_url
from cms.djangoapps.course_creators.views import add_user_with_status_unrequested, get_course_creator_status
from cms.djangoapps.course_creators.models import CourseCreator
from cms.djangoapps.models.settings.course_grading import CourseGradingModel
Expand Down Expand Up @@ -131,7 +132,7 @@
'course_notifications_handler',
'textbooks_list_handler', 'textbooks_detail_handler',
'group_configurations_list_handler', 'group_configurations_detail_handler',
'get_course_and_check_access']
'get_course_and_check_access', 'enrollment_tracks_handler']

WAFFLE_NAMESPACE = 'studio_home'
ENABLE_GLOBAL_STAFF_OPTIMIZATION = WaffleSwitch( # lint-amnesty, pylint: disable=toggle-missing-annotation
Expand Down Expand Up @@ -753,6 +754,8 @@ def course_index(request, course_key):
'mfe_proctored_exam_settings_url': get_proctored_exam_settings_url(course_block.id),
'advance_settings_url': reverse_course_url('advanced_settings_handler', course_block.id),
'proctoring_errors': proctoring_errors,
'enable_course_mode_creation': settings.FEATURES.get('ENABLE_COURSE_MODE_CREATION', False),
'enrollment_tracks_url': reverse_course_url('enrollment_tracks_handler', course_block.id),
})


Expand Down Expand Up @@ -1955,3 +1958,79 @@ def get_organizations(user):
organizations = course_creator.organizations.all().values_list('short_name', flat=True)

return organizations


@login_required
@require_http_methods(("GET", "POST"))
@ensure_csrf_cookie
def enrollment_tracks_handler(request, course_key_string):
"""
A RESTful handler for Enrollment tracks

GET
html: return Enrollment Tracks page (Backbone application)
POST
json: edit enrollment tracks for course
"""
course_key = CourseKey.from_string(course_key_string)
store = modulestore()
with store.bulk_operations(course_key):
try:
course = get_course_and_check_access(course_key, request.user)
except PermissionDenied:
msg = _('PermissionDenied: Failed in authenticating {user}').format(user=request.user)
return JsonResponse({"error": msg}, status=403)

if 'text/html' in request.META.get('HTTP_ACCEPT', 'text/html'):
certificate_url = reverse_course_url('certificates_list_handler', course_key)
course_outline_url = reverse_course_url('course_handler', course_key)
upload_asset_url = reverse_course_url('assets_handler', course_key)
activation_handler_url = reverse_course_url(
handler_name='certificate_activation_handler',
course_key=course_key
)
course_mode_creation_url = reverse(
'course_modes_api:v1:course_modes_list',
args=(str(course_key),),
)
course_modes = [
mode.slug for mode in CourseMode.modes_for_course(
course_id=course.id, include_expired=True
) if mode.slug != 'audit'
]
# Check whether the audit mode associated with the course exists in database
has_audit_mode = CourseMode.objects.filter(course_id=course.id, mode_slug='audit')

has_certificate_modes = len(course_modes) > 0

enable_course_mode_creation = settings.FEATURES.get('ENABLE_COURSE_MODE_CREATION', False)

if has_certificate_modes:
certificate_web_view_url = get_lms_link_for_certificate_web_view(
course_key=course_key,
mode=course_modes[0] # CourseMode.modes_for_course returns default mode if doesn't find anyone.
)
enable_course_mode_creation = False
else:
certificate_web_view_url = None
is_active, certificates = CertificateManager.is_activated(course)
return render_to_response('enrollment_tracks.html', {
'context_course': course,
'certificate_url': certificate_url,
'course_outline_url': course_outline_url,
'course_mode_creation_url': course_mode_creation_url,
'enable_course_mode_creation': enable_course_mode_creation and not has_audit_mode,
'course_id': str(course_key),
'upload_asset_url': upload_asset_url,
'certificates': certificates,
'has_certificate_modes': has_certificate_modes,
'course_modes': course_modes,
'certificate_web_view_url': certificate_web_view_url,
'is_active': is_active,
'is_global_staff': GlobalStaff().has_user(request.user),
'certificate_activation_handler_url': activation_handler_url,
'mfe_proctored_exam_settings_url': get_proctored_exam_settings_url(course.id),
'enrollment_tracks_url': reverse_course_url('enrollment_tracks_handler', course_key),
})
else:
return HttpResponse(status=406)
11 changes: 11 additions & 0 deletions cms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,17 @@
# in the LMS and CMS.
# .. toggle_tickets: 'https://github.com/open-craft/edx-platform/pull/429'
'DISABLE_UNENROLLMENT': False,

# .. toggle_name: ENABLE_COURSE_MODE_CREATION
# .. toggle_implementation: DjangoSetting
# .. toggle_default: False
# .. toggle_description: Set to True to enable course mode creation through studio.
# .. toggle_category: n/a
# .. toggle_use_cases: temporary
# .. toggle_creation_date: 2023-02-27
# .. toggle_target_removal_date: None
# .. toggle_tickets: https://openedx.atlassian.net/browse/OSPR-5906
'ENABLE_COURSE_MODE_CREATION': False,
}

# .. toggle_name: ENABLE_COPPA_COMPLIANCE
Expand Down
1 change: 1 addition & 0 deletions cms/static/cms/js/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'js/factories/export',
'js/factories/group_configurations',
'js/certificates/factories/certificates_page_factory',
'js/certificates/factories/course_mode_factory',
'js/factories/index',
'js/factories/manage_users',
'js/factories/outline',
Expand Down
3 changes: 2 additions & 1 deletion cms/static/cms/js/spec/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@
'js/certificates/spec/views/certificate_details_spec',
'js/certificates/spec/views/certificate_editor_spec',
'js/certificates/spec/views/certificates_list_spec',
'js/certificates/spec/views/certificate_preview_spec'
'js/certificates/spec/views/certificate_preview_spec',
'js/certificates/spec/views/add_course_mode_spec'
];

i = 0;
Expand Down
15 changes: 15 additions & 0 deletions cms/static/js/certificates/factories/course_mode_factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
define([
'jquery',
'js/certificates/views/add_course_mode'
],
function($, CourseModeHandler) {
'use strict';
return function(enableCourseModeCreation, courseModeCreationUrl, courseId) {
// Execute the page object's rendering workflow
new CourseModeHandler({
enableCourseModeCreation: enableCourseModeCreation,
courseModeCreationUrl: courseModeCreationUrl,
courseId: courseId
}).show();
};
});
89 changes: 89 additions & 0 deletions cms/static/js/certificates/spec/views/add_course_mode_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Jasmine Test Suite: Course modes creation

define([
'underscore',
'jquery',
'js/models/course',
'js/certificates/views/add_course_mode',
'common/js/spec_helpers/template_helpers',
'common/js/spec_helpers/view_helpers',
'edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers'
],
function(_, $, Course, AddCourseMode, TemplateHelpers, ViewHelpers, AjaxHelpers) {
'use strict';

var SELECTORS = {
addCourseMode: '.add-course-mode'
};

describe('Add Course Modes Spec:', function() {
beforeEach(function() {
window.course = new Course({
id: '5',
name: 'Course Name',
url_name: 'course_name',
org: 'course_org',
num: 'course_num',
revision: 'course_rev'
});
window.CMS.User = {isGlobalStaff: true, isCourseInstructor: true};

TemplateHelpers.installTemplate('course-modes', true);
appendSetFixtures('<div class="wrapper-certificates nav-actions"></div>');
appendSetFixtures('<p class="account-username">test</p>');
this.view = new AddCourseMode({
el: $('.wrapper-certificates'),
courseId: window.course.id,
courseModeCreationUrl: '/api/course_modes/v1/courses/' + window.course.id + '/',
enableCourseModeCreation: true
});
appendSetFixtures(this.view.render().el);
});

afterEach(function() {
delete window.course;
delete window.CMS.User;
});

describe('Add course modes', function() {
it('course mode creation event works fine', function() {
spyOn(this.view, 'addCourseMode');
this.view.delegateEvents();
this.view.$(SELECTORS.addCourseMode).click();
expect(this.view.addCourseMode).toHaveBeenCalled();
});

it('add course modes button works fine', function() {
var requests = AjaxHelpers.requests(this),
notificationSpy = ViewHelpers.createNotificationSpy();
this.view.$(SELECTORS.addCourseMode).click();
AjaxHelpers.expectJsonRequest(
requests,
'POST', '/api/course_modes/v1/courses/' + window.course.id + '/?username=test',
{
course_id: window.course.id,
mode_slug: 'honor',
mode_display_name: 'Honor',
currency: 'usd'
});
ViewHelpers.verifyNotificationShowing(notificationSpy, /Enabling honor course mode/);
});

it('enable course mode creation should be false when method "remove" called', function() {
this.view.remove();
expect(this.view.enableCourseModeCreation).toBe(false);
});

it('add course mode should be removed when method "remove" called', function() {
this.view.remove();
expect(this.view.el.innerHTML).toBe('');
});

it('method "show" should call the render function', function() {
spyOn(this.view, 'render');
this.view.show();
expect(this.view.render).toHaveBeenCalled();
});
});
});
});
70 changes: 70 additions & 0 deletions cms/static/js/certificates/views/add_course_mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
define([
'underscore',
'gettext',
'js/views/baseview',
'common/js/components/views/feedback_notification',
'text!templates/course-modes.underscore',
'edx-ui-toolkit/js/utils/html-utils'
],
function(_, gettext, BaseView, NotificationView, CourseModes, HtmlUtils) {
'use strict';

var AddCourseMode = BaseView.extend({
el: $('.wrapper-certificates'),
events: {
'click .add-course-mode': 'addCourseMode'
},

initialize: function(options) {
this.enableCourseModeCreation = options.enableCourseModeCreation;
this.courseModeCreationUrl = options.courseModeCreationUrl;
this.courseId = options.courseId;
},

render: function() {
HtmlUtils.setHtml(this.$el, HtmlUtils.template(CourseModes)({
enableCourseModeCreation: this.enableCourseModeCreation,
courseModeCreationUrl: this.courseModeCreationUrl,
courseId: this.courseId
}));
return this;
},

addCourseMode: function() {
var notification = new NotificationView.Mini({
title: gettext('Enabling honor course mode')
});
var username = $('.account-username')[0].innerText;
$.ajax({
url: this.courseModeCreationUrl + '?username=' + username,
Copy link
Member Author

Choose a reason for hiding this comment

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

note to author: how can we update the course mode API so we don't necessarily have to send the username

dataType: 'json',
contentType: 'application/json',
data: JSON.stringify({
course_id: this.courseId,
mode_slug: 'honor',
mode_display_name: 'Honor',
Comment on lines +44 to +45
Copy link
Member Author

@mariajgrimaldi mariajgrimaldi Feb 27, 2023

Choose a reason for hiding this comment

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

note to author: here we need a dictionary or list[tuple] of course modes

currency: 'usd'
Copy link
Member Author

Choose a reason for hiding this comment

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

note to author: is there a setting for this? Search.

}),
type: 'POST',
beforeSend: function() {
notification.show();
},
success: function() {
notification.hide();
location.reload();
}
});
},

show: function() {
this.render();
},

remove: function() {
this.enableCourseModeCreation = false;
this.$el.empty();
return this;
}
});
return AddCourseMode;
});
Loading