def certificate_info_for_user(user, course_id, grade, user_is_allowlisted, user_certificate): """ Returns the certificate info for a user for grade report. """ certificate_is_delivered = 'N' certificate_type = 'N/A' status = _certificate_status(user_certificate) certificate_generated = status[ 'status'] == CertificateStatuses.downloadable course_overview = get_course_overview_or_none(course_id) if not course_overview: return None can_have_certificate = _should_certificate_be_visible( course_overview.certificates_display_behavior, course_overview.certificates_show_before_end, course_overview.has_ended(), course_overview.certificate_available_date, course_overview.self_paced) enrollment_mode, __ = CourseEnrollment.enrollment_mode_for_user( user, course_id) mode_is_verified = enrollment_mode in CourseMode.VERIFIED_MODES user_is_verified = grade is not None and mode_is_verified eligible_for_certificate = 'Y' if (user_is_allowlisted or user_is_verified or certificate_generated) \ else 'N' if certificate_generated and can_have_certificate: certificate_is_delivered = 'Y' certificate_type = status['mode'] return [ eligible_for_certificate, certificate_is_delivered, certificate_type ]
def certificates_viewable_for_course(course): """ Returns True if certificates are viewable for any student enrolled in the course, False otherwise. Arguments: course (CourseOverview or course descriptor): The course to check if certificates are viewable Returns: boolean: whether the certificates are viewable or not """ # The CourseOverview contains validation logic on the certificates_display_behavior and certificate_available_date # fields. Thus, we prefer to use the CourseOverview, but will fall back to the course in case the CourseOverview is # not available. if not isinstance(course, CourseOverview): course_overview = get_course_overview_or_none(course.id) if course_overview: course = course_overview return _should_certificate_be_visible(course.certificates_display_behavior, course.certificates_show_before_end, course.has_ended(), course.certificate_available_date, course.self_paced)
def certificate_downloadable_status(student, course_key): """ Check the student existing certificates against a given course. if status is not generating and not downloadable or error then user can view the generate button. Args: student (user object): logged-in user course_key (CourseKey): ID associated with the course Returns: Dict containing student passed status also download url, uuid for cert if available """ current_status = _certificate_status_for_student(student, course_key) # If the certificate status is an error user should view that status is "generating". # On the back-end, need to monitor those errors and re-submit the task. response_data = { 'is_downloadable': False, 'is_generating': True if current_status['status'] in [ CertificateStatuses.generating, # pylint: disable=simplifiable-if-expression CertificateStatuses.error ] else False, 'is_unverified': True if current_status['status'] == CertificateStatuses.unverified else False, # pylint: disable=simplifiable-if-expression 'download_url': None, 'uuid': None, } course_overview = get_course_overview_or_none(course_key) if settings.FEATURES.get("ENABLE_V2_CERT_DISPLAY_SETTINGS"): display_behavior_is_valid = ( course_overview.certificates_display_behavior == CertificatesDisplayBehaviors.END_WITH_DATE) else: display_behavior_is_valid = True if (not certificates_viewable_for_course(course_overview) and CertificateStatuses.is_passing_status(current_status['status']) and display_behavior_is_valid and course_overview.certificate_available_date): response_data['earned_but_not_available'] = True response_data[ 'certificate_available_date'] = course_overview.certificate_available_date may_view_certificate = _should_certificate_be_visible( course_overview.certificates_display_behavior, course_overview.certificates_show_before_end, course_overview.has_ended(), course_overview.certificate_available_date, course_overview.self_paced) if current_status[ 'status'] == CertificateStatuses.downloadable and may_view_certificate: response_data['is_downloadable'] = True response_data['download_url'] = current_status[ 'download_url'] or get_certificate_url(student.id, course_key, current_status['uuid']) response_data['is_pdf_certificate'] = bool( current_status['download_url']) response_data['uuid'] = current_status['uuid'] return response_data