Script for pretest completion - Frappe LMS (learning)

I am working on a script that upon achieving a passing score on a pre-test a learner can bypass course content and the course is marked complete and a certificate is generated. Currently after passing the pre-test after writing and enabling the script course progress goes to 50% and the user does not earn a certificate. Does this look correct?

# Constants for the logic
PASSING_THRESHOLD = 80.0  # Percentage required to skip the course
PRE_TEST_NAME = "Pre-Test Evaluation" # Title of your pre-test quiz

def evaluate_pre_test(doc, method):
    # 1. Check if this is actually a Pre-Test
    # Note: Adjust 'quiz' field name based on your LMS app schema
    quiz = frappe.get_doc("LMS Quiz", doc.quiz)
    
    if quiz.title != PRE_TEST_NAME:
        return

    # 2. Evaluate if the learner passed[cite: 1, 2, 3]
    if doc.percentage >= PASSING_THRESHOLD:
        mark_as_completed(doc)
    else:
        frappe.msgprint("Pre-test not passed. Please proceed with the full course modules.")

def mark_as_completed(doc):
    # Find the enrollment record for this user and course
    enrollment = frappe.get_all("Course Enrollment", 
        filters={
            "member": doc.member, 
            "course": doc.course,
            "status": ["!=", "Completed"]
        }, 
        limit=1
    )

    if enrollment:
        enrollment_doc = frappe.get_doc("Course Enrollment", enrollment[0].name)
        
        # Update progress and status
        enrollment_doc.status = "Completed"
        enrollment_doc.completed_on = frappe.utils.nowdate()
        enrollment_doc.progress = 100
        enrollment_doc.save(ignore_permissions=True)

        # 3. Trigger Certificate Generation[cite: 1, 2]
        # This calls the standard LMS function to issue a certificate if the course allows it
        if enrollment_doc.has_certificate:
            create_certificate(enrollment_doc)
            
        frappe.msgprint(f"Congratulations! You passed the pre-test. Course credit has been awarded.")

def create_certificate(enrollment_doc):
    # Logic to insert a record into the Certificate doctype[cite: 1, 3]
    certificate = frappe.get_doc({
        "doctype": "LMS Certificate",
        "member": enrollment_doc.member,
        "course": enrollment_doc.course,
        "enrollment": enrollment_doc.name,
        "issue_date": frappe.utils.nowdate()
    })
    certificate.insert(ignore_permissions=True)
    frappe.db.commit()

# Execution call
evaluate_pre_test(doc, None)