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

คุณสามารถใช้กลุ่มนักเรียนเพื่อจัดนักเรียนออกเป็นกลุ่มเฉพาะเพื่อประสบการณ์การสอนที่ดียิ่งขึ้น เช่น งานที่กำหนดเป้าหมายและกิจกรรมการทำงานร่วมกัน ใช้ 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

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

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

Python

classroom_service_with_capability_endpoint = 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')

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

Python

def check_student_groups_update_capability():
    """Checks whether a user is eligible to create and modify student groups."""
    capability = classroom_service_with_capability_endpoint.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 eligible to create and modify student groups.")
    else:
        print("User is not eligible 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
    ).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"
    ).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
    ).execute()

    print(response)

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

Python

def list_student_groups(classroom_service, course_id):
    results = classroom_service.courses().studentGroups().list(
        courseId=course_id
    ).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
    ).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"
    ).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
    ).execute()

    print(results.get("studentGroupMembers"))

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