موضوعات را مدیریت کنید

یک Topic برچسبی برای گروه‌بندی آیتم‌های جریان CourseWork و CourseWorkMaterial در یک دوره است. یک Topic معمولاً برای دسته‌بندی این آیتم‌ها بر اساس شباهت، مانند هفته‌ای که آیتم‌ها اختصاص داده شده‌اند یا موضوع آنها، استفاده می‌شود. کاربران می‌توانند آیتم‌های جریان را به صورت بصری در نمای Classwork از رابط کاربری Classroom سازماندهی و فیلتر کنند.

هر Topic (Topic) توسط یک شناسه (ID) منحصر به فرد که توسط سرور اختصاص داده می‌شود، شناسایی می‌شود. منبع Topic همچنین شامل موارد زیر است:

  • name : نام نمایشی که در رابط کاربری کلاس درس نمایش داده می‌شود.
  • updateTime : آخرین باری که Topic به‌روزرسانی شده است
  • courseId : شناسه دوره‌ای که Topic به آن مرتبط است.

ایجاد یک موضوع

شما می‌توانید با استفاده از متد topics.create() یک Topic جدید در یک دوره ایجاد کنید، همانطور که در نمونه زیر نشان داده شده است:

جاوا

کلاس/قطعه کد/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;

پایتون

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

فیلد name همیشه الزامی است و باید یک رشته غیر خالی باشد. سایر فیلدها اختیاری هستند.

جزئیات موضوع را بازیابی کنید

شما می‌توانید یک Topic خاص را بر اساس شناسه (ID) با استفاده از متد topics.get() بازیابی کنید، همانطور که در نمونه زیر نشان داده شده است:

جاوا

کلاس/قطعه کد/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;

پایتون

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

از متد topics.list() برای بازیابی تمام Topic های یک دوره استفاده کنید، همانطور که در نمونه زیر نشان داده شده است:

جاوا

class/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;

پایتون

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

به‌روزرسانی یک موضوع

شما می‌توانید name یک Topic موجود را با استفاده از متد topics.patch() به‌روزرسانی کنید، همانطور که در نمونه زیر نشان داده شده است:

جاوا

کلاس/قطعه قطعه/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;

پایتون

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

فیلدهای شناسه Topic id و updateTime توسط سرور ایجاد می‌شوند و نمی‌توان آن‌ها را با API به‌روزرسانی کرد.

حذف یک موضوع

شما می‌توانید یک Topic موجود را با استفاده از متد topics.delete() حذف کنید، همانطور که در نمونه زیر نشان داده شده است:

جاوا

کلاس/قطعه قطعه/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;
}