生徒グループを使用すると、的を絞った課題や共同作業など、授業の質を高めるために生徒を特定のグループに分けることができます。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。
- 生徒グループを管理するコースの ID。コースの所有者には 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 が含まれます。