在 Google Classroom 以外的地方建立附件

本指南會說明如何在網站或應用程式中建立外掛程式附件。這些互動與使用 CourseWork API 端點建立作業類似。請實作這個流程,讓使用者從您的網站或應用程式建立外掛程式附件。

工作流程

大致來說,附件的建立流程如下:

  1. 老師使用者開啟您的網站或應用程式,選取要指派給學生的內容。
  2. 檢查使用者能否建立外掛程式附件
  3. 如果使用者無法建立外掛程式附件,請建立 CourseWork 指派作業,並在指派內容的網址做為連結 Material。
  4. 如果使用者可以建立外掛程式附件,請按照下列步驟操作:
    1. 建立作業。
    2. 建立外掛程式附件,連結至所選內容,並將該附件與新作業建立關聯。
    3. 請通知老師,作業已成功建立。

以下各節將說明每個動作。

檢查使用者是否能建立外掛程式附件

您可以代表符合資格的使用者建立外掛程式附件。當您要建立課程時,「而且」擁有 Teaching & Learning 或 Education Plus 的 Google Workspace for Education 版本授權,則符合資格的使用者就是其中一位老師。

首先,請判斷使用者是否能在指定的 Course 中建立外掛程式。向 courses.checkAddOnCreationEligibility 端點發出要求,包括課程 ID。

Python

eligibility_response = (
  classroom_service.courses()
  .checkAddOnCreationEligibility(courseId=course_id)
  .execute()
)
is_create_attachment_eligible = (
  eligibility_response.get('isCreateAttachmentEligible')
)
print(f'User eligibility for course {eligibility_response.get("courseId")}'
      f': {is_create_attachment_eligible}.')

如果使用者符合資格,回應就會包含設為 true 的布林值 isCreateAttachmentEligible 值。如果使用者不符合資格,回應就不會傳回 isCreateAttachmentEligible 布林值。

根據使用者的資格轉送使用者

資格條件會決定您是否能為使用者建立外掛程式附件。

不符資格的使用者

如果使用者無法建立外掛程式附件,請建立新的 CourseWork 指派作業,並將使用者選取的內容網址做為 Link 指派。

Python

if not is_create_attachment_eligible:
  coursework = {
    'title': 'My CourseWork Assignment with Link Material',
    'description': 'Created using the Classroom CourseWork API.',
    'workType': 'ASSIGNMENT',
    'state': 'DRAFT',  # Set to 'PUBLISHED' to assign to students.
    'maxPoints': 100,
    'materials': [
      {'link': {'url': my_content_url}}
    ]
  }

  assignment = (
    service.courses()
    .courseWork()
    .create(courseId=course_id, body=coursework)
    .execute()
  )

  print(
    f'Link Material assignment created with ID: {assignment.get("id")}'
  )

回應包含要求課程中的作業,其中附加了內容。使用者可以按一下 Link,在新分頁中開啟網站內容。

使用連結教材草擬 CourseWork 作業

圖 1 老師透過連結 Material 查看 CourseWork 作業草稿。

符合資格的使用者

如果使用者「可以」建立外掛程式附件,請按照下列步驟操作。

  1. 建立不含任何附件的新CourseWork作業。
  2. 建立外掛程式附件。

Python

if is_create_attachment_eligible:
  coursework = {
    'title': 'My CourseWork Assignment with Add-on Attachment',
    'description': 'Created using the Classroom CourseWork API.',
    'workType': 'ASSIGNMENT',
    'state': 'DRAFT',  # Set to 'PUBLISHED' to assign to students.
    'maxPoints': 100,
  }

  assignment = (
    classroom_service.courses()
    .courseWork()
    .create(courseId=course_id, body=coursework)
    .execute()
  )

  print(
    f'Empty assignment created with ID: {assignment.get("id")}'
  )

  attachment = {
    'teacherViewUri': {'uri': teacher_view_url},
    'studentViewUri': {'uri': student_view_url},
    'studentWorkReviewUri': {'uri': grade_student_work_url},
    'title': f'Test Attachment {test_label}',
  }

  add_on_attachment = (
    service.courses()
    .courseWork()
    .addOnAttachments()
    .create(
      courseId=course_id,
      itemId=assignment.get("id"),  # ID of the new assignment.
      body=attachment,
    )
    .execute()
  )

  print(
    f'Add-on attachment created with ID: {add_on_attachment.get("id")}'
  )

這個外掛程式在 Classroom 中會以「附件卡」的形式顯示。要求中指定的網址會在適當的 每個資料檢視 iframe 中開啟。