นี่เป็นคำแนะนำที่ 7 ในซีรีส์คำแนะนำแบบทีละขั้นเกี่ยวกับส่วนเสริมของ Classroom
ในคำแนะนำแบบทีละขั้นนี้ คุณจะเพิ่มลักษณะการทำงานไปยังเว็บแอปพลิเคชันเพื่อสร้างไฟล์แนบของส่วนเสริมจากภายนอก Google Classroom ใช้ลักษณะการทำงานนี้เพื่อให้ผู้ใช้สร้างไฟล์แนบของส่วนเสริมจากผลิตภัณฑ์หรือเว็บไซต์ที่มีอยู่ วิธีนี้ยังเป็นส่วนเพิ่มเติมที่ยอดเยี่ยมในการผสานรวม CourseWork
เนื่องจากคุณนำการเข้าชมที่มีอยู่ไปยังประสบการณ์ของผู้ใช้ที่ดีขึ้นซึ่งส่วนเสริมมีให้โดยไม่เปลี่ยนขั้นตอน โปรดดูขั้นตอนที่แนะนำในหน้าคำแนะนำในการสร้างไฟล์แนบนอก Classroom
นอกจากนี้ คุณยังเพิ่มลักษณะการทำงานของส่วนเสริมเพื่อแก้ไขงานด้วยไฟล์แนบของส่วนเสริมแบบเป็นโปรแกรมได้ด้วย คุณจะแก้ไขงานที่มีไฟล์แนบของส่วนเสริมได้ ไม่ว่าใครจะเป็นผู้สร้างงานนั้นก็ตาม วิธีนี้เป็นประโยชน์อย่างยิ่งโดยเฉพาะในการส่งงานหลังจากที่นักเรียนทำกิจกรรมเสร็จ ซึ่งเป็นการบอกให้ครูทราบว่างานที่มอบหมายเสร็จสมบูรณ์แล้ว และงานของนักเรียนพร้อมสำหรับการตรวจสอบ
คุณขยายส่วนเสริมเวอร์ชันล่าสุดที่รองรับ content-type หรือไฟล์แนบประเภทกิจกรรมได้ ในคู่มือนี้จะใช้ไฟล์แนบของประเภทเนื้อหา
เพิ่มขอบเขต OAuth การจัดการงาน
ตรวจสอบว่าแอปพลิเคชันของคุณขอขอบเขตต่อไปนี้
https://www.googleapis.com/auth/classroom.addons.teacher
https://www.googleapis.com/auth/classroom.addons.student
https://www.googleapis.com/auth/classroom.coursework.students
ก่อนหน้านี้ไม่จำเป็นต้องมีขอบเขต classroom.coursework.students
และใช้เพื่อสร้างหรือแก้ไขการมอบหมาย CourseWork
เพิ่มขอบเขตนี้ไปยังรายการขอบเขตใน Google Workspace Marketplace SDK, หน้าจอขอความยินยอมของ OAuth และโค้ดเซิร์ฟเวอร์ของโปรเจ็กต์ระบบคลาวด์
Python
SCOPES = [
"https://www.googleapis.com/auth/classroom.addons.teacher",
"https://www.googleapis.com/auth/classroom.addons.student",
"https://www.googleapis.com/auth/classroom.coursework.students",
]
สร้างงานใน Classroom
เพิ่มปุ่มในหน้าเว็บที่ไม่ใช่ iframe
ขั้นตอนที่อธิบายไว้ในคำแนะนำแบบทีละขั้นนี้อนุญาตให้ผู้ใช้สร้างงานและไฟล์แนบใน Google Classroom จากผลิตภัณฑ์ที่ไม่ใช่ของ Google ในทางปฏิบัติ น่าจะเป็นเว็บไซต์หรือแอปพลิเคชันที่คุณมีอยู่ สำหรับตัวอย่างนี้ คุณต้องสร้างหน้าเว็บจำลองเพื่อทำหน้าที่เป็นเว็บไซต์ภายนอก คุณต้องมีปุ่มหรือลิงก์ที่เปิดเส้นทางใหม่ซึ่งทำขั้นตอน CourseWork
ที่แนะนำเพื่อสร้างการมอบหมายใหม่ เมื่อคลิก
นอกจากนี้คุณจะต้องเพิ่มปุ่มหรือลิงก์เพื่ออนุญาตให้ผู้ใช้ลงชื่อเข้าใช้หากยังไม่มีปุ่ม คุณจะต้องใช้ข้อมูลเข้าสู่ระบบของผู้ใช้เพื่อสร้างคำขอ API ครั้งต่อๆ ไป เพื่อให้ผู้ใช้ต้องดำเนินการแฮนด์เชค OAuth 2.0 ให้เสร็จสมบูรณ์ ดูคำแนะนำแบบทีละขั้นในการลงชื่อเข้าใช้เพื่อดูคำแนะนำที่เฉพาะเจาะจง
Python
ตัวอย่าง Python ที่ระบุจะแก้ไขเส้นทาง /index
ที่เริ่มใช้ในขั้นตอนแรกแบบทีละขั้น
<!-- /webapp/templates/index.html -->
<a href="clear-credentials.html">Logout</a>
<a href="start-auth-flow.html">Login</a>
<br>
<a href="create-coursework-assignment.html">Create a CourseWork Assignment</a>
เพิ่มเทมเพลต HTML เพื่อแสดงปลายทางในเว็บไซต์ หน้านี้จะแสดงเนื้อหาที่จะแนบไปกับงาน CourseWork
<!-- /webapp/templates/example-coursework-assignment.html -->
<h1>CourseWork assignment loaded!</h1>
<p>You've loaded a CourseWork assignment! It was created from an external web page.</p>
สร้างไฟล์โมดูล Python ใหม่เพื่อจัดการเส้นทางที่เกี่ยวข้องกับ CourseWork
นี่คือ coursework_routes.py
ในตัวอย่างที่เราให้ไว้ เพิ่มเส้นทาง 3 เส้นทางต่อไปนี้ โปรดทราบว่าคุณจะต้องใส่เนื้อหาบางส่วนในภายหลัง
# /webapp/coursework_routes.py
@app.route("/create-coursework-assignment")
def create_coursework_assignment():
"""
Completes the assignment creation flow.
"""
# Check that the user is signed in. If not, perform the OAuth 2.0
# authorization flow.
credentials = get_credentials()
if not credentials:
return start_auth_flow("coursework_assignment_callback")
# Construct the Google Classroom service.
classroom_service = get_classroom_service()
pass # To be completed later.
@app.route("/example-coursework-assignment/<assignment_type>")
def example_coursework_assignment(assignment_type):
"""
Renders the "example-coursework-assignment.html" template.
"""
return flask.render_template(
"example-coursework-assignment.html", assignment_type=assignment_type
)
@app.route("/coursework-assignment-callback")
def coursework_assignment_callback():
"""
Completes the OAuth 2.0 handshake and stores credentials in the session.
This is identical to the callback introduced in the sign-in walkthrough,
but redirects the user to the index page instead of the attachment
discovery page.
"""
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
CLIENT_SECRETS_FILE,
scopes=SCOPES,
state=flask.session["state"],
redirect_uri=flask.url_for("coursework_assignment_callback", _external=True),
)
flow.fetch_token(authorization_response=flask.request.url)
credentials = flow.credentials
flask.session["credentials"] = session_credentials_to_dict(
credentials
)
# Close the current window and redirect the user to the index page.
return flask.render_template("close-me.html", redirect_destination="index")
ตรวจสอบสิทธิ์ในการสร้างส่วนเสริมของผู้ใช้
ต้องมีข้อกำหนดเบื้องต้นหลายอย่างที่ผู้ใช้ต้องมีก่อน คุณจึงจะสร้างไฟล์แนบของส่วนเสริมในนามของผู้ใช้ได้ Google ได้จัดเตรียมเมธอด courses.checkAddOnCreationEligibility
ไว้เพื่อช่วยในการพิจารณาว่าผู้ใช้มีคุณสมบัติตรงตามข้อกำหนดเบื้องต้นเหล่านี้หรือไม่ ผู้ใช้ที่มีคุณสมบัติตรงตามข้อกำหนดเบื้องต้นจะเรียกว่าผู้ใช้ที่มีสิทธิ์
เพิ่มการตรวจสอบสิทธิ์ลงในการใช้งานเส้นทางการสร้าง CourseWork
จากนั้นทดสอบช่อง isCreateAttachmentEligible
ในคำตอบ สำหรับผู้ใช้ที่มีสิทธิ์ ให้ทำตามตรรกะเพื่อสร้างงานที่มีไฟล์แนบ หรือสร้างเนื้อหาของลิงก์ คุณจะต้องทราบรหัสหลักสูตรที่ผู้ใช้ต้องการสร้างงาน โดยปกติแล้ว คุณจะแจ้งให้ผู้ใช้ระบุ
หลักสูตรที่จะใช้ ในตัวอย่างนี้จะใช้ค่าที่ฮาร์ดโค้ดเพื่อให้เข้าใจง่าย
Python
# /webapp/coursework_routes.py
@app.route("/create-coursework-assignment")
def create_coursework_assignment():
"""
Completes the assignment creation flow.
"""
# ... Check that the user is signed in and get the Classroom service ...
# The ID of the course to which the assignment will be added.
course_id = 1234567890 # TODO(developer) Replace with an actual course ID.
# Check whether the user can create add-on attachments.
eligibility_response = (
classroom_service.courses()
.checkAddOnCreationEligibility(courseId=course_id)
.execute()
)
is_create_attachment_eligible = eligibility_response.get("isCreateAttachmentEligible")
if is_create_attachment_eligible:
# See the "Create an assignment with add-on attachment for eligible users" section for implementation.
if not is_create_attachment_eligible:
# See the "Create a Link Material" section for implementation.
สร้างงานพร้อมไฟล์แนบของส่วนเสริมสำหรับผู้ใช้ที่มีสิทธิ์
หากผู้ใช้มีสิทธิ์สร้างไฟล์แนบของส่วนเสริม ให้ทำตามขั้นตอนต่อไปนี้
- ส่งคำขอ API เพื่อสร้างงาน
courseWork
ใน Google Classroom โดยไม่ต้องแนบไฟล์ - ดึงข้อมูล
id
ของงานที่สร้างใหม่ - สร้าง CoursWork
AddOnAttachment
ใหม่ - ส่งคำขอสร้างไฟล์แนบของส่วนเสริมในงานที่สร้างขึ้นใหม่ใน Google Classroom
Python
# /webapp/coursework_routes.py
if is_create_attachment_eligible:
# Create an assignment.
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.
}
# Issue a request to create the assignment.
create_assignment_response = (
classroom_service.courses()
.courseWork()
.create(courseId=course_id, body=coursework)
.execute()
)
# Create an add-on attachment that links to the selected content and
# associate it with the new assignment.
content_url = flask.url_for(
"example_coursework_assignment",
assignment_type="add-on-attachment",
_scheme="https",
_external=True,
)
# Construct an AddOnAttachment instance.
attachment = {
"teacherViewUri": {"uri": content_url},
"studentViewUri": {"uri": content_url},
"title": f'Test Attachment for Assignment {create_assignment_response.get("id")}',
}
# Issue a request to create the attachment.
add_on_attachment_response = (
classroom_service.courses()
.courseWork()
.addOnAttachments()
.create(
courseId=course_id,
itemId=create_assignment_response.get("id"), # ID of the new assignment.
body=attachment,
)
.execute()
)
สร้างเนื้อหาลิงก์
หากผู้ใช้ไม่มีสิทธิ์สร้างไฟล์แนบของส่วนเสริม ให้สร้างเนื้อหาของลิงก์แทนโดยทำตามขั้นตอนต่อไปนี้
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.
# Specify the URL for your content as a Link Material.
"materials": [
{
"link": {
"url": flask.url_for(
"example_coursework_assignment",
assignment_type="link-material",
_scheme="https",
_external=True,
)
}
}
],
}
# Issue a request to create the assignment.
assignment_response = (
classroom_service.courses()
.courseWork()
.create(courseId=course_id, body=coursework)
.execute()
)
แก้ไขการมอบหมายที่สร้างไว้แล้ว
คุณจะเข้าถึง แก้ไข ส่ง อ้างสิทธิ์อีกครั้ง หรือส่งคืนรายการสตรีมของ Google Classroom ที่มีไฟล์แนบของส่วนเสริมอย่างน้อย 1 รายการได้ ไม่ว่าใครจะเป็นผู้สร้างรายการสตรีมนั้นก็ตาม รายการสตรีมคืองาน Announcement
, CourseWork
หรือ CourseWorkMaterial
ในการสาธิตนี้ คุณจะต้องเพิ่มเส้นทางเพื่อแก้ไขรายการสตรีมที่กำหนด ใช้วิธีนี้เพื่อยืนยันว่าคุณสามารถเข้าถึงและแก้ไขรายการสตรีมที่คุณสร้างขึ้นโดยใช้ API และที่ครูสร้างผ่าน UI ของ Google Classroom
เพิ่มลิงก์หรือปุ่มอีก 1 ปุ่มไปยังหน้าเว็บที่คุณแก้ไขครั้งแรกในคำแนะนำแบบทีละขั้นนี้ ซึ่งควรเปิดเส้นทางใหม่เพื่อแก้ไขงาน CourseWork
Python
ตัวอย่าง Python ที่ให้ไว้จะแก้ไขเส้นทาง /index
ที่มีการแก้ไขก่อนหน้านี้ในคำแนะนำแบบทีละขั้นนี้
<!-- /webapp/templates/index.html -->
<a href="modify-coursework-assignment.html">Create a CourseWork Assignment</a>
สร้างเส้นทางใหม่เพื่อจัดการเส้นทางที่เกี่ยวข้องกับ CourseWork ไฟล์นี้อยู่ในไฟล์ coursework_routes.py
ในตัวอย่างที่ให้ไว้
# Check that the user is signed in.
credentials = get_credentials()
if not credentials:
return start_auth_flow("coursework_assignment_callback")
# Get the Google Classroom service.
classroom_service = get_classroom_service()
# The ID of the course to which the assignment will be added.
# Ordinarily, you'll prompt the user to specify which course to use. For
# simplicity, we use a hard-coded value in this example.
course_id = 1234567890 # TODO(developer) Replace with an actual course ID.
assignment_id = 1234567890 # TODO(developer) Replace with an actual assignment ID.
# Retrieve details about the CourseWork assignment.
get_coursework_response = (
classroom_service.courses()
.courseWork()
.get(courseId=course_id, id=assignment_id)
.execute()
)
# Alter the current title.
assignment_title = f"{get_coursework_response.get('title')} (Modified by API request)"
# Issue a request to modify the assignment.
modify_coursework_response = (
classroom_service.courses()
.courseWork()
.patch(
courseId=course_id,
id=assignment_id,
updateMask="title",
body={"title": assignment_title},
)
.execute()
)
ทดสอบส่วนเสริม
ตัวอย่างที่ให้ไว้จะใช้ตัวระบุงานและหลักสูตรแบบฮาร์ดโค้ดเพื่อให้เข้าใจง่าย คุณดูตัวระบุเหล่านี้ได้โดยการส่งคำขอด้วยข้อมูลเข้าสู่ระบบของครูไปยังเมธอด get
และ list
ของ courses
และ courseWork
และจะแสดงผลในคำตอบด้วยเมื่อสร้างงาน courseWork
รายการ
ทดสอบการสร้างเนื้อหาของลิงก์
เรียกใช้เซิร์ฟเวอร์ จากนั้นไปที่หน้าดัชนี แล้วลงชื่อเข้าใช้ในฐานะผู้ใช้ครู ที่ไม่มีใบอนุญาต Google Workspace for Education Teaching and Learning หรือ Plus คุณสลับสถานะใบอนุญาตของผู้ใช้ได้จากคอนโซลผู้ดูแลระบบของโดเมนทดสอบ คลิกปุ่มสร้างงานในหลักสูตร แล้วเปิด UI ของ Google Classroom และยืนยันว่ามีการสร้างงานที่มีไฟล์แนบเนื้อหาลิงก์แล้ว ไฟล์แนบควรแสดงชื่อ ของหน้าเว็บที่เชื่อมโยงและ URL
ทดสอบการสร้างไฟล์แนบของส่วนเสริม
กลับไปที่หน้าดัชนี แล้วลงชื่อเข้าใช้ในฐานะผู้ใช้ครูที่มีใบอนุญาต Google Workspace for Education Teaching and Learning หรือ Plus คลิกปุ่มสร้างงานของหลักสูตร จากนั้นเปิด UI ของ Google Classroom และยืนยันว่ามีการสร้างงานที่มีไฟล์แนบของส่วนเสริม ไฟล์แนบควรแสดงชื่อแอปพลิเคชันส่วนเสริมและชื่อที่ระบุในโค้ดของคุณ
การแก้ไขการกำหนดการทดสอบ
กลับไปที่หน้าดัชนี แล้วตรวจสอบว่าได้ลงชื่อเข้าใช้ในฐานะผู้ใช้ครูที่มีใบอนุญาต Teaching and Learning หรือ Plus คลิกปุ่มแก้ไขงานของหลักสูตร จากนั้นกลับไปที่ UI ของ Google Classroom และยืนยันว่าชื่องานมีการเปลี่ยนแปลง
ยินดีด้วย คุณอ่านชุดคำแนะนำแบบทีละขั้นจบแล้ว