Manage Teachers and Students

Students and teachers are specific mappings between a user profile and a course, representing that user's role in the course. Designations of student and teacher are not global: a user can be assigned as a teacher for one course and a student in another. The designation "student" or "teacher" represents a set of permissions for a particular user in a particular course.

Students
A Student resource represents a user who is enrolled as a student in a specific course. Students are permitted to view the course details and teachers for that course.
Teachers
A Teacher resource represents a user who teaches a specific course. Teachers are permitted to view and change the course details, view teachers and students, and manage additional teachers and students.

Students and teachers are identified by the unique ID or email address of the user, as returned by the Google Admin SDK. The current user may also refer to their own ID using the "me" shorthand.

Add or remove teachers

Domain administrators can directly add teachers to courses with teachers.create(), as shown in the following sample:

.NET

string courseId = "123456";
string teacherEmail = "alice@example.Edu";
var teacher = new Teacher
{
    UserId = teacherEmail
};
try
{
    teacher = service.Courses.Teachers.Create(teacher, courseId).Execute();
    Console.WriteLine(
            "User '{0}' was added as a teacher to the course with ID '{1}'.\n",
            teacher.Profile.Name.FullName, courseId);
}
catch (GoogleApiException e)
{
    if (e.HttpStatusCode == HttpStatusCode.Conflict)
    {
        Console.WriteLine("User '{0}' is already a member of this course.\n",
                teacherEmail);
    }
    else
    {
        throw e;
    }
}

Java

String courseId = "123456";
String teacherEmail = "alice@example.edu";
Teacher teacher = new Teacher().setUserId(teacherEmail);
try {
    teacher = service.courses().teachers().create(courseId, teacher).execute();
    System.out.printf("User '%s' was added as a teacher to the course with ID '%s'.\n",
            teacher.getProfile().getName().getFullName(), courseId);
} catch (GoogleJsonResponseException e) {
    GoogleJsonError error = e.getDetails();
    if (error.getCode() == 409) {
        System.out.printf("User '%s' is already a member of this course.\n", teacherEmail);
    } else {
        throw e;
    }
}

PHP

$courseId = '123456';
$teacherEmail = 'alice@example.edu';
$teacher = new Google_Service_Classroom_Teacher(array(
  'userId' => $teacherEmail
));
try {
  $teacher = $service->courses_teachers->create($courseId, $teacher);
  printf("User '%s' was added as a teacher to the course with ID '%s'.\n",
      $teacher->profile->name->fullName, $courseId);
} catch (Google_Service_Exception $e) {
  if ($e->getCode() == 409) {
    printf("User '%s' is already a member of this course.\n", $teacherEmail);
  } else {
    throw $e;
  }
}

Python

course_id = '123456'
teacher_email = 'alice@example.edu'
teacher = {
    'userId': teacher_email
}
try:
    teacher = service.courses().teachers().create(courseId=course_id,
                                                  body=teacher).execute()
    print (u'User {0} was added as a teacher to the course with ID "{1}"'
        .format(teacher.get('profile').get('name').get('fullName'),
        course_id))
except errors.HttpError as e:
    error = simplejson.loads(e.content).get('error')
    if(error.get('code') == 409):
        print u'User "{0}" is already a member of this course.'.format(
            teacher_email)
    else:
        raise

If you are adding additional teachers on behalf of an authenticated teacher, you must use the invitations.create() method.

You can remove additional teachers from a course with the teachers.delete() method. This only removes the specified teacher from the course and does not affect their assignment to other courses or their user profile.

Enroll or remove students

Domain administrators can directly add students with the students.create() method, as shown in the following sample:

.NET

string courseId = "123456";
string enrollmentCode = "abcdef";
var student = new Student
{
    UserId = "me"
};
try
{
    var request = service.Courses.Students.Create(student, courseId);
    request.EnrollmentCode = enrollmentCode;
    student = request.Execute();
    Console.WriteLine(
            "User '{0}' was enrolled  as a student in the course with ID '{1}'.\n",
            student.Profile.Name.FullName, courseId);
}
catch (GoogleApiException e)
{
    if (e.HttpStatusCode == HttpStatusCode.Conflict)
    {
        Console.WriteLine("You are already a member of this course.\n");
    }
    else
    {
        throw e;
    }
}

Java

String courseId = "123456";
String enrollmentCode = "abcdef";
Student student = new Student().setUserId("me");
try {
    student = service.courses().students().create(courseId, student)
            .setEnrollmentCode(enrollmentCode)
            .execute();
    System.out.printf("User '%s' was enrolled as a student in the course with ID '%s'.\n",
            student.getProfile().getName().getFullName(), courseId);
} catch (GoogleJsonResponseException e) {
    GoogleJsonError error = e.getDetails();
    if (error.getCode() == 409) {
        System.out.println("You are already a member of this course.");
    } else {
        throw e;
    }
}

PHP

$courseId = '123456';
$enrollmentCode = 'abcdef';
$student = new Google_Service_Classroom_Student(array(
  'userId' => 'me'
));
$params = array(
  'enrollmentCode' => $enrollmentCode
);
try {
  $student = $service->courses_students->create($courseId, $student, $params);
  printf("User '%s' was enrolled  as a student in the course with ID '%s'.\n",
      $student->profile->name->fullName, $courseId);
} catch (Google_Service_Exception $e) {
  if ($e->getCode() == 409) {
    print "You are already a member of this course.\n";
  } else {
    throw $e;
  }
}

Python

course_id = '123456'
enrollment_code = 'abcdef'
student = {
    'userId': 'me'
}
try:
    student = service.courses().students().create(
        courseId=course_id,
        enrollmentCode=enrollment_code,
        body=student).execute()
    print (
        'User {0} was enrolled as a student in the course with ID "{1}"'
        .format(student.get('profile').get('name').get('fullName'),
        course_id))
except errors.HttpError as e:
    error = simplejson.loads(e.content).get('error')
    if(error.get('code') == 409):
        print 'You are already a member of this course.'
    else:
        raise

If you are adding students on behalf of an authenticated teacher, you must use the invitations.create() method.

You can remove a student from a course with the students.delete() method. This only removes the specified student from the course and does not affect their enrollment in other courses or their user profile.

Retrieve a user's courses

To retrieve a list of courses for a student or teacher, use the courses.list() and supply the corresponding user's studentId or teacherId.

Retrieve a user's profile

Use userProfiles.get() to retrieve the abridged profile that includes the ID, name, email address, photo, and user permissions.

The ID returned corresponds to the Directory API Users resource containing the matching studentId or teacherId.

Enviar comentarios sobre…