จัดการคำเชิญให้เข้าร่วมหลักสูตร

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

Invitation ทรัพยากรแต่ละรายการมีช่องต่อไปนี้

  • id: ตัวระบุที่ Classroom กำหนดสำหรับการเชิญ
  • userId: รหัสของผู้ใช้ที่ได้รับเชิญให้เข้าร่วมหลักสูตร
  • courseId: หลักสูตรที่ผู้ใช้ได้รับเชิญให้เข้าร่วม
  • role: บทบาทในหลักสูตรที่ผู้ใช้ที่ได้รับเชิญจะมีในหลักสูตร

สร้างคำเชิญ

คุณสามารถใช้วิธี invitations.create() เพื่อเชิญผู้ใช้ให้เข้าร่วมหลักสูตร โดยมีบทบาทที่เฉพาะเจาะจง ใส่Invitation ทรัพยากรในเนื้อหาคำขอ และระบุ courseId, userId และ role

Java

classroom/snippets/src/main/java/CreateInvitation.java
Invitation invitation = null;
try {
  /* Set the role the user is invited to have in the course. Possible values of CourseRole can be
  found here: https://developers.google.com/classroom/reference/rest/v1/invitations#courserole.*/
  Invitation content =
      new Invitation().setCourseId(courseId).setUserId(userId).setRole("TEACHER");

  invitation = service.invitations().create(content).execute();

  System.out.printf(
      "User (%s) has been invited to course (%s).\n",
      invitation.getUserId(), invitation.getCourseId());
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The course or user does not exist.\n");
  }
  throw e;
} catch (Exception e) {
  throw e;
}
return invitation;

ดึงข้อมูลคำเชิญ

เรียกคำเชิญที่เฉพาะเจาะจงโดยเรียกใช้เมธอด invitations.get() และระบุ id ของคำเชิญ

Java

classroom/snippets/src/main/java/GetInvitation.java
Invitation invitation = null;
try {
  invitation = service.invitations().get(id).execute();
  System.out.printf(
      "Invitation (%s) for user (%s) in course (%s) retrieved.\n",
      invitation.getId(), invitation.getUserId(), invitation.getCourseId());
} catch (GoogleJsonResponseException e) {
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The invitation id (%s) does not exist.\n", id);
  }
  throw e;
} catch (Exception e) {
  throw e;
}
return invitation;

ยอมรับคำเชิญ

การยอมรับคำเชิญจะลบคำเชิญและเพิ่มผู้ใช้ที่ได้รับเชิญลงในหลักสูตรโดยมีบทบาทที่ระบุในคำเชิญ ยอมรับคำเชิญโดยเรียกใช้เมธอด invitations.accept() และระบุ id ของคำเชิญ

Java

classroom/snippets/src/main/java/AcceptInvitation.java
try {
  service.invitations().accept(id).execute();
  System.out.printf("Invitation (%s) was accepted.\n", id);
} catch (GoogleJsonResponseException e) {
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The invitation id (%s) does not exist.\n", id);
  }
  throw e;
} catch (Exception e) {
  throw e;
}

ลบคำเชิญ

วิธีเดียวในการอัปเดตคำเชิญคือการลบคำเชิญนั้นและสร้างคำเชิญใหม่ หากต้องการลบคำเชิญ ให้เรียกใช้เมธอด invitations.delete() และระบุ id

Java

classroom/snippets/src/main/java/DeleteInvitation.java
try {
  service.invitations().delete(id).execute();
  System.out.printf("Invitation (%s) was deleted.\n", id);
} catch (GoogleJsonResponseException e) {
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The invitation id (%s) does not exist.\n", id);
  }
  throw e;
} catch (Exception e) {
  throw e;
}