本指南說明如何在網站或應用程式中建立外掛程式附件。互動內容與使用 CourseWork API 端點建立指派作業類似。實作這個歷程,讓使用者從您的網站或應用程式建立外掛程式附件。
工作流程
大致來說,附件建立流程會依以下順序執行:
- 老師使用者開啟您的網站或應用程式,選取要指派給學生的內容。
- 檢查使用者是否能建立外掛程式附件。
- 如果使用者「無法」建立外掛程式附件,請建立 CourseWork 指派作業,並使用所選內容的網址做為連結教材。
- 如果使用者「可以」建立外掛程式附件,請按照下列步驟操作:
- 建立作業。
- 建立外掛程式附件,連結至所選內容,並將附件與新指派作業建立關聯。
- 通知老師作業已成功建立。
我們會在以下各節中說明每個動作。
檢查使用者能否建立外掛程式附件
您可以代表符合資格的使用者建立外掛程式附件。符合資格的使用者是指在您嘗試在課程中建立 CourseWork 作業的使用者時,並且已獲派 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
,在新分頁中開啟網站內容。
圖 1 老師檢視畫面顯示含有 Link Material 的 CourseWork 作業草稿。
符合資格的使用者
如果使用者「可以」建立外掛程式附件,請按照下列步驟操作。
- 建立不含任何附件的新「
CourseWork
」作業。 - 建立外掛程式附件。
- 將
AddOnAttachment
的itemId
設為新建指派作業的id
。 - 請確保您針對支援的每個檢視畫面,都為使用者選取的內容提供網址。
- 將
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 中開啟。