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

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