Quản lý chủ đề

Topic là nhãn dùng để nhóm các mục CourseWorkCourseWorkMaterial trong một khoá học. Topic thường được dùng để phân loại các mục này theo mức độ tương đồng, chẳng hạn như tuần mà các mục được chỉ định hoặc chủ đề của các mục. Người dùng có thể sắp xếp và lọc các mục trên luồng theo cách trực quan trong chế độ xem Bài tập trên lớp của giao diện người dùng Lớp học.

Mỗi Topic được xác định bằng một mã nhận dạng duy nhất do máy chủ chỉ định. Tài nguyên Topic cũng chứa những nội dung sau:

  • name: Tên hiển thị xuất hiện trong giao diện người dùng của Lớp học
  • updateTime: Thời gian cập nhật gần đây nhất của Topic
  • courseId: Mã nhận dạng của khoá học mà Topic được liên kết

Tạo một chủ đề

Bạn có thể tạo một Topic mới trong một khoá học bằng phương thức topics.create(), như minh hoạ trong mẫu sau:

Java

classroom/snippets/src/main/java/CreateTopic.java
Topic topic = null;
try {
  // Create the new Topic.
  Topic content = new Topic().setName("Semester 1");
  topic = service.courses().topics().create(courseId, content).execute();
  System.out.println("Topic id: " + topic.getTopicId() + "\n" + "Course id: " + courseId);
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The courseId does not exist: %s.\n", courseId);
  } else {
    throw e;
  }
} catch (Exception e) {
  throw e;
}
return topic;

Python

topic = {
    "name": 'Example Topic'
}
response = service.courses().topics().create(
  courseId=<course ID or alias>,
  body=topic).execute()
print('Topic created: ', response['name'])

Trường name luôn là trường bắt buộc và phải là một chuỗi không trống. Tất cả các trường khác đều không bắt buộc.

Truy xuất thông tin chi tiết về Chủ đề

Bạn có thể truy xuất một Topic cụ thể theo mã nhận dạng bằng phương thức topics.get(), như minh hoạ trong mẫu sau:

Java

classroom/snippets/src/main/java/GetTopic.java
Topic topic = null;
try {
  // Get the topic.
  topic = service.courses().topics().get(courseId, topicId).execute();
  System.out.printf("Topic '%s' found.\n", topic.getName());
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The courseId or topicId does not exist: %s, %s.\n", courseId, topicId);
  }
  throw e;
} catch (Exception e) {
  throw e;
}
return topic;

Python

response = service.courses().topics().get(
  courseId=<course ID or alias>,
  id=<topic ID>).execute()
print('{0} ({1})'.format(response['name'], response['topicId']))

Sử dụng phương thức topics.list() để truy xuất tất cả Topic trong một khoá học, như trong mẫu sau:

Java

classroom/snippets/src/main/java/ListTopics.java
List<Topic> topics = new ArrayList<>();
String pageToken = null;

try {
  do {
    ListTopicResponse response =
        service
            .courses()
            .topics()
            .list(courseId)
            .setPageSize(100)
            .setPageToken(pageToken)
            .execute();

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

  if (topics.isEmpty()) {
    System.out.println("No topics found.");
  } else {
    for (Topic topic : topics) {
      System.out.printf("%s (%s)\n", topic.getName(), topic.getTopicId());
    }
  }
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The courseId does not exist: %s.\n", courseId);
  } else {
    throw e;
  }
} catch (Exception e) {
  throw e;
}
return topics;

Python

topics = []
page_token = None
while True:
    response = service.courses().topics().list(
        pageToken=page_token,
        pageSize=30,
        courseId=<course ID or alias>).execute()
    topics.extend(response.get('topic', []))
    page_token = response.get('nextPageToken', None)
    if not page_token:
        break
if not topics:
    print('No topics found.')
else:
    print('Topics:')
    for topic in topics:
        print('{0} ({1})'.format(topic['name'], topic['topicId']))

Cập nhật một chủ đề

Bạn có thể cập nhật một Topic name hiện có bằng phương thức topics.patch(), như minh hoạ trong mẫu sau:

Java

classroom/snippets/src/main/java/UpdateTopic.java
Topic topic = null;
try {
  // Retrieve the topic to update.
  Topic topicToUpdate = service.courses().topics().get(courseId, topicId).execute();

  // Update the name field for the topic retrieved.
  topicToUpdate.setName("Semester 2");

  /* Call the patch endpoint and set the updateMask query parameter to the field that needs to
  be updated. */
  topic =
      service
          .courses()
          .topics()
          .patch(courseId, topicId, topicToUpdate)
          .set("updateMask", "name")
          .execute();

  /* Prints the updated topic. */
  System.out.printf("Topic '%s' updated.\n", topic.getName());
} catch (GoogleJsonResponseException e) {
  // TODO(developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The courseId or topicId does not exist: %s, %s.\n", courseId, topicId);
  } else {
    throw e;
  }
} catch (Exception e) {
  throw e;
}
return topic;

Python

topic = {
  "name": "New Topic Name"
}
response = service.courses().topics().patch(
  courseId=<course ID or alias>,
  id=<topic ID>,
  updateMask="name",
  body=topic).execute()
print('{0} ({1})'.format(response['name'], response['topicId']))

Các trường Topic idupdateTime do máy chủ tạo và không thể cập nhật bằng API.

Xoá một chủ đề

Bạn có thể xoá Topic hiện có bằng phương thức topics.delete(), như trong mẫu sau:

Java

classroom/snippets/src/main/java/DeleteTopic.java
try {
  service.courses().topics().delete(courseId, topicId).execute();
} catch (GoogleJsonResponseException e) {
  // TODO(developer) - handle error appropriately
  throw e;
} catch (Exception e) {
  throw e;
}