สร้างและจัดการผู้ปกครอง

แหล่งข้อมูล Guardian แสดงถึงผู้ใช้ เช่น ผู้ปกครอง ที่ได้รับข้อมูลเกี่ยวกับหลักสูตรและงานของนักเรียน โดยต้องเชิญผู้ปกครองซึ่งปกติแล้วไม่ได้เป็นสมาชิกของโดเมน Classroom ของนักเรียนโดยใช้อีเมลของผู้ปกครอง

คําเชิญจะแสดงด้วยแหล่งข้อมูล GuardianInvitation ผู้ใช้ที่ได้รับเชิญจะได้รับอีเมลแจ้งให้ยอมรับคำเชิญ หากอีเมลไม่ได้เชื่อมโยงกับบัญชี Google ระบบจะแจ้งให้ผู้ใช้สร้างบัญชีก่อนตอบรับคำเชิญ

เมื่อเชิญผู้ใช้และก่อนที่ผู้ใช้จะยอมรับคำเชิญ GuardianInvitation จะมีสถานะเป็น PENDING เมื่อผู้ใช้ยอมรับคำเชิญ ระบบจะทำเครื่องหมาย GuardianInvitation เป็น COMPLETED และสร้างทรัพยากร Guardian

สถานะ GuardianInvitation อาจเปลี่ยนเป็น COMPLETED ด้วยหากหมดอายุ หรือหากผู้ใช้ที่ได้รับอนุญาตยกเลิกคำเชิญ (เช่น ใช้วิธีการ PatchGuardianInvitation) ผู้ปกครอง ครูใน Classroom หรือผู้ดูแลระบบอาจยกเลิกความสัมพันธ์ของผู้ปกครองได้โดยใช้เว็บแอปพลิเคชันของ Classroom หรือวิธี DeleteGuardian

ผู้ที่มีสิทธิ์จัดการผู้ปกครอง

ตารางต่อไปนี้อธิบายการดำเนินการที่ทำได้กับผู้ปกครองตามประเภทของผู้ใช้ที่ได้รับการตรวจสอบสิทธิ์

ตาราง ACL ที่เกี่ยวข้องกับผู้ปกครองตามประเภทผู้ใช้

ขอบเขต

สิทธิ์ระดับที่ให้คุณจัดการผู้ปกครองมี 3 ระดับดังนี้

  • https://www.googleapis.com/auth/classroom.guardianlinks.me.readonly: ดูผู้ใช้เอง
  • https://www.googleapis.com/auth/classroom.guardianlinks.students.readonly: ดูผู้ปกครองและคำเชิญผู้ปกครองสำหรับนักเรียนที่ผู้ใช้สอนหรือดูแล
  • https://www.googleapis.com/auth/classroom.guardianlinks.students: ดูและจัดการผู้ปกครองและคําเชิญผู้ปกครองสําหรับนักเรียนที่ผู้ใช้สอนหรือดูแล

การทำงานทั่วไป

ส่วนนี้จะอธิบายการดำเนินการทั่วไปบางอย่างของผู้ปกครองที่คุณอาจต้องการดำเนินการโดยใช้ Google Classroom API

สร้างคำเชิญให้ผู้ปกครอง

ตัวอย่างต่อไปนี้แสดงวิธีสร้างคำเชิญผู้ปกครองโดยใช้เมธอด userProfiles.guardianInvitations.create()

Java

classroom/snippets/src/main/java/CreateGuardianInvitation.java
GuardianInvitation guardianInvitation = null;

/* Create a GuardianInvitation object with state set to PENDING. See
https://developers.google.com/classroom/reference/rest/v1/userProfiles.guardianInvitations#guardianinvitationstate
for other possible states of guardian invitations. */
GuardianInvitation content =
    new GuardianInvitation()
        .setStudentId(studentId)
        .setInvitedEmailAddress(guardianEmail)
        .setState("PENDING");
try {
  guardianInvitation =
      service.userProfiles().guardianInvitations().create(studentId, content).execute();

  System.out.printf("Invitation created: %s\n", guardianInvitation.getInvitationId());
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("There is no record of studentId: %s", studentId);
  } else {
    throw e;
  }
} catch (Exception e) {
  throw e;
}
return guardianInvitation;

Python

guardianInvitation = {
  'invitedEmailAddress': 'guardian@gmail.com',
}
guardianInvitation = service.userProfiles().guardianInvitations().create(
                      studentId='student@mydomain.edu',
                          body=guardianInvitation).execute()
print("Invitation created with id: {0}".format(guardianInvitation.get('invitationId')))

การตอบกลับจะมีตัวระบุที่เซิร์ฟเวอร์กำหนดซึ่งสามารถใช้เพื่ออ้างอิง GuardianInvitation

ยกเลิกคำเชิญผู้ปกครอง

หากต้องการยกเลิกคำเชิญ ให้แก้ไขสถานะของคำเชิญจาก PENDING เป็น COMPLETE โดยเรียกใช้เมธอด userProfiles.guardianInvitations.patch() วิธีนี้เป็นวิธีเดียวในการนำคำเชิญออก

Java

classroom/snippets/src/main/java/CancelGuardianInvitation.java
GuardianInvitation guardianInvitation = null;

try {
  /* Change the state of the GuardianInvitation from PENDING to COMPLETE. See
  https://developers.google.com/classroom/reference/rest/v1/userProfiles.guardianInvitations#guardianinvitationstate
  for other possible states of guardian invitations. */
  GuardianInvitation content =
      service.userProfiles().guardianInvitations().get(studentId, invitationId).execute();
  content.setState("COMPLETE");

  guardianInvitation =
      service
          .userProfiles()
          .guardianInvitations()
          .patch(studentId, invitationId, content)
          .set("updateMask", "state")
          .execute();

  System.out.printf(
      "Invitation (%s) state set to %s\n.",
      guardianInvitation.getInvitationId(), guardianInvitation.getState());
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf(
        "There is no record of studentId (%s) or invitationId (%s).", studentId, invitationId);
  } else {
    throw e;
  }
} catch (Exception e) {
  throw e;
}
return guardianInvitation;

Python

guardian_invite = {
     'state': 'COMPLETE'
}
guardianInvitation = service.userProfiles().guardianInvitations().patch(
  studentId='student@mydomain.edu',
  invitationId=1234, # Replace with the invitation ID of the invitation you want to cancel
  updateMask='state',
  body=guardianInvitation).execute()

แสดงรายการคำเชิญสำหรับนักเรียนที่ต้องการ

คุณดูรายการคำเชิญทั้งหมดที่ส่งสำหรับนักเรียนที่ต้องการได้โดยใช้เมธอด userProfiles.guardianInvitations.list() โดยค่าเริ่มต้น ระบบจะแสดงเฉพาะคำเชิญ PENDING รายการ นอกจากนี้ ผู้ดูแลระบบโดเมนยังเรียกข้อมูลคำเชิญในสถานะ COMPLETED ได้ด้วยโดยระบุพารามิเตอร์ states

Java

classroom/snippets/src/main/java/ListGuardianInvitationsByStudent.java
List<GuardianInvitation> guardianInvitations = new ArrayList<>();
String pageToken = null;

try {
  do {
    ListGuardianInvitationsResponse response =
        service
            .userProfiles()
            .guardianInvitations()
            .list(studentId)
            .setPageToken(pageToken)
            .execute();

    /* Ensure that the response is not null before retrieving data from it to avoid errors. */
    if (response.getGuardianInvitations() != null) {
      guardianInvitations.addAll(response.getGuardianInvitations());
      pageToken = response.getNextPageToken();
    }
  } while (pageToken != null);

  if (guardianInvitations.isEmpty()) {
    System.out.println("No guardian invitations found.");
  } else {
    for (GuardianInvitation invitation : guardianInvitations) {
      System.out.printf("Guardian invitation id: %s\n", invitation.getInvitationId());
    }
  }
} catch (GoogleJsonResponseException e) {
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("There is no record of studentId (%s).", studentId);
  } else {
    throw e;
  }
} catch (Exception e) {
  throw e;
}
return guardianInvitations;

Python

guardian_invites = []
page_token = None

while True:
    response = service.userProfiles().guardianInvitations().list(
                                      studentId='student@mydomain.edu').execute()
    guardian_invites.extend(response.get('guardian_invites', []))
    page_token = response.get('nextPageToken', None)
    if not page_token:
        break

if not courses:
    print('No guardians invited for this {0}.'.format(response.get('studentId')))
else:
    print('Guardian Invite:')
    for guardian in guardian_invites:
        print('An invite was sent to '.format(guardian.get('id'),
                                              guardian.get('guardianId')))

แสดงรายการผู้ปกครองที่ใช้งานอยู่

หากต้องการดูว่าผู้ใช้รายใดเป็นผู้ดูแลที่ใช้งานอยู่สำหรับนักเรียนคนใดคนหนึ่ง ให้ใช้เมธอด userProfiles.guardians.list() ผู้ดูแลที่ใช้งานอยู่คือผู้ดูแลที่ยอมรับคำเชิญ

Java

classroom/snippets/src/main/java/ListGuardians.java
List<Guardian> guardians = new ArrayList<>();
String pageToken = null;

try {
  do {
    ListGuardiansResponse response =
        service.userProfiles().guardians().list(studentId).setPageToken(pageToken).execute();

    /* Ensure that the response is not null before retrieving data from it to avoid errors. */
    if (response.getGuardians() != null) {
      guardians.addAll(response.getGuardians());
      pageToken = response.getNextPageToken();
    }
  } while (pageToken != null);

  if (guardians.isEmpty()) {
    System.out.println("No guardians found.");
  } else {
    for (Guardian guardian : guardians) {
      System.out.printf(
          "Guardian name: %s, guardian id: %s, guardian email: %s\n",
          guardian.getGuardianProfile().getName().getFullName(),
          guardian.getGuardianId(),
          guardian.getInvitedEmailAddress());
    }
  }

} catch (GoogleJsonResponseException e) {
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("There is no record of studentId (%s).", studentId);
  } else {
    throw e;
  }
} catch (Exception e) {
  throw e;
}
return guardians;

Python

guardian_invites = []
page_token = None

while True:
    response = service.userProfiles().guardians().list(studentId='student@mydomain.edu').execute()
    guardian_invites.extend(response.get('guardian_invites', []))
    page_token = response.get('nextPageToken', None)
    if not page_token:
        break

if not courses:
    print('No guardians invited for this {0}.'.format(response.get('studentId')))
else:
    print('Guardian Invite:')
    for guardian in guardian_invites:
        print('An invite was sent to '.format(guardian.get('id'),
                                              guardian.get('guardianId')))

นำผู้ปกครองออก

นอกจากนี้ คุณยังนำผู้ปกครองออกจากนักเรียนได้โดยใช้วิธี userProfiles.guardians.delete() ดังนี้

Java

classroom/snippets/src/main/java/DeleteGuardian.java
try {
  service.userProfiles().guardians().delete(studentId, guardianId).execute();
  System.out.printf("The guardian with id %s was deleted.\n", guardianId);
} catch (GoogleJsonResponseException e) {
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("There is no record of guardianId (%s).", guardianId);
  }
}

Python

service.userProfiles().guardians().delete(studentId='student@mydomain.edu',
                                        guardianId='guardian@gmail.com').execute()