From 26678daf1feac2416e7d27346c687e86b1331f14 Mon Sep 17 00:00:00 2001 From: "Albert (AJ) St. Aubin" Date: Tue, 13 Jul 2021 17:59:45 -0400 Subject: [PATCH] feat: Add the Request Certificate button to a Course card [MICROBA-678] When a certificate is in an unexpected state (i.e. notpassing with a passing grade) this alert will allow the user to attempt to resolve the issue on their own. It will run the code that checks the certificates status. It requires that the course is configured to allow users to Request Certificates though. --- common/djangoapps/student/helpers.py | 11 +++++++ .../js/learner_dashboard/certificate_api.js | 23 ++++++++++++++ lms/templates/dashboard.html | 1 + .../_dashboard_certificate_information.html | 31 ++++++++++++------- themes/edx.org/lms/templates/dashboard.html | 2 ++ 5 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 lms/static/js/learner_dashboard/certificate_api.js diff --git a/common/djangoapps/student/helpers.py b/common/djangoapps/student/helpers.py index a632dc4696..80b65724e3 100644 --- a/common/djangoapps/student/helpers.py +++ b/common/djangoapps/student/helpers.py @@ -37,6 +37,7 @@ from common.djangoapps.student.models import ( from common.djangoapps.util.password_policy_validators import normalize_password from lms.djangoapps.certificates.api import ( certificates_viewable_for_course, + cert_generation_enabled, get_certificate_url, has_html_certificates_enabled ) @@ -46,6 +47,7 @@ from lms.djangoapps.grades.api import CourseGradeFactory from lms.djangoapps.verify_student.models import VerificationDeadline from lms.djangoapps.verify_student.services import IDVerificationService from lms.djangoapps.verify_student.utils import is_verification_expiring_soon, verification_for_datetime +from openedx.core.djangoapps.certificates.api import auto_certificate_generation_enabled from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers from openedx.core.djangoapps.theming.helpers import get_themes from openedx.core.djangoapps.user_authn.utils import is_safe_login_or_logout_redirect @@ -592,6 +594,15 @@ def _cert_info(user, course_overview, cert_status): ) status_dict['grade'] = str(max_grade) + # If the grade is passing, the status is one of these statuses, and request certificate + # is enabled for a course then we need to provide the option to the learner + if ( + status_dict['status'] != CertificateStatuses.downloadable and + (cert_generation_enabled(course_overview.id) or auto_certificate_generation_enabled()) and + persisted_grade.passed + ): + status_dict['status'] = CertificateStatuses.requesting + return status_dict diff --git a/lms/static/js/learner_dashboard/certificate_api.js b/lms/static/js/learner_dashboard/certificate_api.js new file mode 100644 index 0000000000..1a5b3a27bf --- /dev/null +++ b/lms/static/js/learner_dashboard/certificate_api.js @@ -0,0 +1,23 @@ +$(document).ready(() => { + 'use strict'; + + const requestButtons = document.getElementsByClassName('request-cert'); + + for (let i = 0; i < requestButtons.length; i++) { + requestButtons[i].addEventListener('click', (event) => { + event.preventDefault(); + const endpoint = !!event.target.dataset.endpoint && event.target.dataset.endpoint; + $.ajax({ + type: 'POST', + url: endpoint, + dataType: 'text', + success: () => { + location.reload(); + }, + error: (jqXHR, textStatus, errorThrown) => { + location.reload(); + }, + }); + }); + } +}); diff --git a/lms/templates/dashboard.html b/lms/templates/dashboard.html index 4f8a262721..0543f827a0 100644 --- a/lms/templates/dashboard.html +++ b/lms/templates/dashboard.html @@ -40,6 +40,7 @@ from common.djangoapps.student.models import CourseEnrollment <%block name="js_extra"> + <%static:js group='dashboard'/> + + <%static:js group='dashboard'/>