Esempio n. 1
0
def _track_certificate_events(request, course, user, user_certificate):
    """
    Tracks web certificate view related events.
    """
    # Badge Request Event Tracking Logic
    course_key = course.location.course_key

    if 'evidence_visit' in request.GET:
        badge_class = get_completion_badge(course_key, user)
        if not badge_class:
            log.warning(
                'Visit to evidence URL for badge, but badges not configured for course "%s"',
                course_key)
            badges = []
        else:
            badges = badge_class.get_for_user(user)
        if badges:
            # There should only ever be one of these.
            badge = badges[0]
            tracker.emit(
                'edx.badge.assertion.evidence_visited', {
                    'badge_name': badge.badge_class.display_name,
                    'badge_slug': badge.badge_class.slug,
                    'badge_generator': badge.backend,
                    'issuing_component': badge.badge_class.issuing_component,
                    'user_id': user.id,
                    'course_id': str(course_key),
                    'enrollment_mode': badge.badge_class.mode,
                    'assertion_id': badge.id,
                    'assertion_image_url': badge.image_url,
                    'assertion_json_url': badge.assertion_url,
                    'issuer': badge.data.get('issuer'),
                })
        else:
            log.warning(
                "Could not find badge for %s on course %s.",
                user.id,
                course_key,
            )

    # track certificate evidence_visited event for analytics when certificate_user and accessing_user are different
    if request.user and request.user.id != user.id:
        emit_certificate_event(
            'evidence_visited', user, str(course.id), course, {
                'certificate_id': user_certificate.verify_uuid,
                'enrollment_mode': user_certificate.mode,
                'social_network': CertificateSocialNetworks.linkedin
            })
    def handle(self, *args, **options):

        # Scrub the username from the log message
        cleaned_options = copy.copy(options)
        if 'username' in cleaned_options:
            cleaned_options['username'] = '******'
        LOGGER.info(
            (
                "Starting to create tasks to regenerate certificates "
                "with arguments %s and options %s"
            ),
            str(args),
            str(cleaned_options)
        )

        # try to parse out the course from the serialized form
        course_id = CourseKey.from_string(options['course'])
        user = options['username']

        kwargs = (Q(username=user) | Q(email=user)) & Q(courseenrollment__course_id=course_id)
        student = User.objects.filter(kwargs).first()
        if not student:
            raise CommandError(f"User {user} does not exist.")

        course = modulestore().get_course(course_id, depth=2)

        if not options['noop']:
            LOGGER.info(
                (
                    "Adding task to the XQueue to generate a certificate "
                    "for student %s in course '%s'."
                ),
                student.id,
                course_id
            )

            if badges_enabled() and course.issue_badges:
                badge_class = get_completion_badge(course_id, student)
                badge = badge_class.get_for_user(student)

                if badge:
                    badge.delete()
                    LOGGER.info("Cleared badge for student %s.", student.id)

            # Add the certificate request to the queue
            regenerate_user_certificates(
                student, course_id, course=course,
                forced_grade=options['grade_value'],
                template_file=options['template_file'],
                insecure=options['insecure']
            )

            LOGGER.info(
                (
                    "Added a certificate regeneration task to the XQueue "
                    "for student %s in course '%s'."
                ),
                student.id,
                str(course_id)
            )

        else:
            LOGGER.info(
                (
                    "Skipping certificate generation for "
                    "student %s in course '%s' "
                    "because the noop flag is set."
                ),
                student.id,
                str(course_id)
            )

        LOGGER.info(
            (
                "Finished regenerating certificates command for "
                "user %s and course '%s'."
            ),
            student.id,
            str(course_id)
        )