สร้างและจัดการกลุ่มนักเรียน

คุณสามารถใช้กลุ่มนักเรียนเพื่อจัดนักเรียนออกเป็นกลุ่มเฉพาะสำหรับ ประสบการณ์การสอนที่ดียิ่งขึ้น เช่น งานที่กำหนดเป้าหมายและ กิจกรรมการทำงานร่วมกัน ใช้ Classroom API เพื่อ สร้าง แก้ไข และอ่านกลุ่มนักเรียนภายในหลักสูตรในนามของ ผู้ดูแลระบบและครู

คุณสามารถสร้าง อัปเดต ลบ และอ่านกลุ่มนักเรียนได้โดยใช้วิธีการต่อไปนี้

นอกจากนี้ คุณยังเพิ่ม นำออก และอ่านสมาชิกภายในกลุ่มนักเรียนได้โดยใช้วิธีต่อไปนี้

เข้าถึงวิธีการของกลุ่มนักเรียนในโปรแกรมทดลองใช้สำหรับนักพัฒนาแอป

หากต้องการเข้าถึงวิธีการของกลุ่มนักเรียนในเวอร์ชันตัวอย่าง คุณต้องดำเนินการต่อไปนี้

ข้อกำหนดของการออกใบอนุญาตและการได้รับสิทธิ์

หากต้องการสร้าง แก้ไข หรือลบกลุ่มนักเรียนในหลักสูตร รวมถึงเพิ่มหรือนำสมาชิกออกจากกลุ่มนักเรียน จะต้องมีคุณสมบัติตามเงื่อนไขต่อไปนี้

  • ผู้ใช้ที่ส่งคำขอต้องเป็นครูในหลักสูตรหรือผู้ดูแลระบบโดเมน
  • ผู้ใช้ที่ส่งคำขอต้องมีใบอนุญาต Google Workspace for Education Plus ที่มอบหมายให้
  • เจ้าของหลักสูตรต้องมีใบอนุญาต Google Workspace for Education Plus ที่มอบหมายให้

กำลังอ่านกลุ่มนักเรียน/นักศึกษาและสมาชิกในกลุ่ม

ผู้ดูแลระบบและครูของหลักสูตรจะอ่านข้อมูลกลุ่มนักเรียน/นักศึกษาได้ ไม่ว่าจะมีใบอนุญาตใดก็ตาม ซึ่งหมายความว่าคำขอ ไปยังปลายทาง ListStudentGroups และ ListStudentGroupMembers ได้รับอนุญาต ในนามของผู้ดูแลระบบหรือครูในหลักสูตร

ข้อกำหนดเบื้องต้นของตัวอย่างโค้ด

คู่มือนี้มีตัวอย่างโค้ดใน Python และถือว่าคุณมีสิ่งต่อไปนี้

  • โปรเจ็กต์ Google Cloud คุณตั้งค่าได้โดยทำตามวิธีการในคู่มือเริ่มต้นใช้งาน Python
  • เพิ่มขอบเขตต่อไปนี้ลงในหน้าจอขอความยินยอม OAuth ของโปรเจ็กต์
    • https://www.googleapis.com/auth/classroom.rosters
    • https://www.googleapis.com/auth/classroom.rosters.readonly สำหรับปลายทางแบบอ่านอย่างเดียว
  • รหัสของหลักสูตรที่ควรจัดการกลุ่มนักเรียน เจ้าของหลักสูตรต้องมีใบอนุญาต Google Workspace for Education Plus
  • สิทธิ์เข้าถึงข้อมูลเข้าสู่ระบบของครูหรือผู้ดูแลระบบที่มีใบอนุญาต Google Workspace for Education Plus

หากคุณทำตามการเริ่มต้นใช้งาน Python อย่างรวดเร็วเป็นจุดเริ่มต้น ให้แก้ไขบริการ Classroom เพื่อเข้าถึงเมธอดเวอร์ชันตัวอย่าง

Python

classroom_service = googleapiclient.discovery.build(
    serviceName='classroom',
    version='v1',
    credentials=creds,
    static_discovery=False,
    discoveryServiceUrl='https://classroom.googleapis.com/$discovery/rest?labels=DEVELOPER_PREVIEW&key=API_KEY')

ตรวจสอบสิทธิ์ของผู้ใช้

Classroom API มีuserProfiles.checkUserCapability ปลายทางที่จะช่วยให้คุณกำหนดได้ล่วงหน้าว่าผู้ใช้จะสร้างและ แก้ไขกลุ่มนักเรียนและสมาชิกได้หรือไม่

userProfiles.checkUserCapabilityปลายทางจะประเมินเพียงว่าผู้ใช้มีสิทธิ์ใช้ความสามารถบางอย่างหรือไม่ เช่น การแก้ไขกลุ่มนักเรียน โดยไม่มีข้อมูลเกี่ยวกับบทบาทในหลักสูตร ตัวอย่างเช่น แม้ว่าผู้ใช้จะมีCREATE_STUDENT_GROUP แต่หากเป็น นักเรียนในหลักสูตร คำขอไปยังปลายทาง CreateStudentGroup จะไม่สำเร็จ

Python

def check_student_groups_update_capability(classroom_service, course_id):
    """Checks whether a user is able to create and modify student groups in a course."""
    capability = classroom_service.userProfiles().checkUserCapability(
        userId="me", # Can also be set to a different user's email address or ID
        capability="CREATE_STUDENT_GROUP",
        previewVersion="V1_20240930_PREVIEW" # Required while the method is in the DPP.
    ).execute()

    if capability.get("allowed"): # Retrieve the `allowed` boolean from the response.
        print("User is allowed to create and modify student groups.")
    else:
        print("User is not allowed to create and modify student groups.")

จัดการกลุ่มนักเรียน

คุณสร้างกลุ่มนักเรียนได้โดยใช้ปลายทาง CreateStudentGroup

Python

def create_student_group(classroom_service, course_id):
    body = {
        "title": "Team Blue"
    }

    response = classroom_service.courses().studentGroups().create(
        courseId=course_id,
        body=body,
        previewVersion="V1_20250630_PREVIEW",
    ).execute()

    print(response)

การตอบกลับจะมี id ของกลุ่มนักเรียนที่สร้างขึ้นใหม่ courseId และtitle ของกลุ่มนักเรียน

คุณใช้กลุ่มนักเรียนidเพื่ออัปเดตหรือลบกลุ่มนักเรียนแต่ละกลุ่มได้

Python

def update_student_group(classroom_service, course_id, student_group_id):
    body = {
        "title": "Team Green"
    }

    response = classroom_service.courses().studentGroups().patch(
        courseId=course_id,
        id=student_group_id,
        body=body,
        updateMask="title",
        previewVersion="V1_20250630_PREVIEW"
    ).execute()

    print(response)
def delete_student_group(classroom_service, course_id, student_group_id):
    response = classroom_service.courses().studentGroups().delete(
        courseId=course_id,
        id=student_group_id,
        previewVersion="V1_20250630_PREVIEW",
    ).execute()

    print(response)

คุณสามารถดึงข้อมูลกลุ่มนักเรียนภายในหลักสูตรได้โดยใช้ปลายทาง ListStudentGroups

Python

def list_student_groups(classroom_service, course_id):
    results = classroom_service.courses().studentGroups().list(
        courseId=course_id,
        previewVersion="V1_20250630_PREVIEW"
    ).execute()

    studentGroups = results.get("studentGroups")

จัดการสมาชิกกลุ่มนักเรียน

เมื่อสร้างกลุ่มนักเรียนเรียบร้อยแล้ว คุณจะเพิ่มสมาชิกในกลุ่มได้

Python

def add_student_group_member(classroom_service, course_id, student_group_id):
    body = {
        "userId": "student@schooldomain.com"
    }

    response = classroom_service.courses().studentGroups().studentGroupMembers().create(
        courseId=course_id,
        studentGroupId=student_group_id,
        body=body,
        previewVersion="V1_20250630_PREVIEW"
    ).execute()

    print(response)

หากต้องการนำสมาชิกออกจากกลุ่มนักเรียน ให้ส่งคำขอเช่นเดียวกับ คำขอต่อไปนี้

Python

def delete_student_group_member(classroom_service, course_id, student_group_id):
    response = classroom_service.courses().studentGroups().studentGroupMembers().delete(
        courseId=course_id,
        studentGroupId=student_group_id,
        userId="student@schooldomain.com",
        previewVersion="V1_20250630_PREVIEW"
    ).execute()
    print(response)

คุณอ่านสมาชิกภายในกลุ่มได้โดยส่งคำขอต่อไปนี้

Python

def list_student_group_members(classroom_service, course_id, student_group_id):
    results = classroom_service.courses().studentGroups().studentGroupMembers().list(
        courseId=course_id,
        studentGroupId=student_group_id,
        previewVersion="V1_20250630_PREVIEW"
    ).execute()

    print(results.get("studentGroupMembers"))

StudentGroupMember แต่ละรายการจะมี courseId, studentGroupId และ userId ของสมาชิกในกลุ่ม