Gestione dei tutori

Una risorsa Tutore rappresenta un utente, ad esempio un genitore, che riceve informazioni sui corsi e sul lavoro di uno studente. Per diventare tutore, il tutore, che in genere non è membro del dominio Classroom dello studente, deve essere invitato utilizzando il suo indirizzo email.

Questo invito crea una risorsa GuardianInviti con stato PENDING. L'utente riceve quindi un'email che lo invita ad accettare l'invito. Se l'indirizzo email non è associato a un Account Google, all'utente viene richiesto di crearne uno prima di accettare l'invito.

Anche se lo stato dell'invito è PENDING, l'utente può accettare l'invito, il che crea una risorsa Guardian e contrassegna l'invito con lo stato COMPLETED. Un invito può anche diventare COMPLETED se scade o se un utente autorizzato lo annulla (ad esempio, utilizzando il metodo PatchGuardianInvitation). Un tutore, un insegnante di Classroom o un amministratore potrebbe interrompere anche il rapporto con il tutore utilizzando l'interfaccia utente di Classroom o il metodo DeleteGuardian.

Chi può gestire i tutori

La tabella seguente descrive le azioni che possono essere eseguite nei confronti dei tutori, in base al tipo di utente attualmente autenticato:

Tabella degli ACL relativi ai tutori per tipo di utente

Ambiti

Esistono tre ambiti che ti consentono di gestire i tutori:

Azioni comuni

Questa sezione descrive alcune delle azioni più comuni dei tutori che possono essere eseguite utilizzando l'API Google Classroom.

Crea un invito per il tutore

L'esempio seguente mostra come creare un invito per il tutore utilizzando il metodo userProfiles.guardianInvitations.create():

Java

classroom/snippets/src/main/java/CreateGuardianInviti.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')))

Il risultato include un identificatore assegnato dal server che può essere utilizzato per fare riferimento a GuardianInvito.

Annullare l'invito di un tutore

Per annullare un invito, modifica lo stato dell'invito da PENDING a COMPLETE chiamando il metodo userProfiles.guardianInvitations.patch(). Tieni presente che al momento questo è l'unico modo per rimuovere un invito.

Java

classroom/snippets/src/main/java/CancelGuardianInvito.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()

Elenca gli inviti per uno studente specifico

Puoi visualizzare un elenco di tutti gli inviti inviati a uno studente specifico utilizzando il metodo userProfiles.guardianInvitations.list():

Java

classroom/snippets/src/main/java/ListGuardianInvitisByStudent.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')))

Per impostazione predefinita, verranno restituiti solo PENDING inviti. In qualità di amministratore di dominio, puoi anche recuperare gli inviti nello stato COMPLETED fornendo un parametro per gli stati.

Elenca tutori attivi

Se vuoi determinare quali utenti sono tutori attivi per uno studente specifico, puoi utilizzare il metodo userProfiles.guardians.list(). I tutori attivi sono tutori che hanno accettato l'invito via email.

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

Rimuovi tutori

Puoi anche rimuovere un tutore da uno studente utilizzando il metodo 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()