นี่คือคำแนะนำแบบทีละขั้นตอนที่ 7 ในชุดคำแนะนำแบบทีละขั้นตอนสำหรับส่วนเสริมของ Classroom
ในคำแนะนำแบบทีละขั้นนี้ คุณจะเพิ่มลักษณะการทำงานให้กับเว็บแอปพลิเคชันเพื่อสร้างไฟล์แนบส่วนเสริมจากภายนอก Google Classroom ใช้ลักษณะการทำงานนี้เพื่อให้
ผู้ใช้สร้างไฟล์แนบของส่วนเสริมจากผลิตภัณฑ์หรือเว็บไซต์ที่มีอยู่ นอกจากนี้ ยังเป็นส่วนเสริมที่ยอดเยี่ยมสําหรับCourseWork
การผสานรวม เนื่องจากคุณจะนําการเข้าชมที่มีอยู่ไปยังประสบการณ์ของผู้ใช้ที่ได้รับการปรับปรุงซึ่งมีอยู่ในส่วนเสริมโดยไม่ต้องเปลี่ยนขั้นตอนการทํางานของผู้ใช้ กระบวนการที่แนะนำจะแสดงในหน้าคำแนะนำสร้างไฟล์แนบ
ภายนอก Classroom
นอกจากนี้ คุณยังเพิ่มลักษณะการทำงานให้กับส่วนเสริมเพื่อแก้ไขงานด้วยไฟล์แนบของส่วนเสริมโดยอัตโนมัติได้ด้วย คุณแก้ไขงานที่มีไฟล์แนบของส่วนเสริมได้ ไม่ว่าใครจะเป็นผู้สร้างงานก็ตาม ซึ่งมีประโยชน์อย่างยิ่งในการส่งงานหลังจากที่นักเรียนทำกิจกรรมเสร็จแล้ว เพื่อแจ้งให้ครูทราบว่างานที่มอบหมายเสร็จสมบูรณ์แล้วและงานของนักเรียนพร้อมให้ตรวจแล้ว
คุณขยายเวอร์ชันสุดท้ายของส่วนเสริมที่รองรับประเภทเนื้อหาหรือ ไฟล์แนบประเภทกิจกรรม ในคู่มือนี้จะใช้ไฟล์แนบประเภทเนื้อหา
เพิ่มขอบเขต 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 และโค้ดฝั่งเซิร์ฟเวอร์ของโปรเจ็กต์ Cloud
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_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 มีวิธี
userProfiles.checkUserCapability
เพื่อตรวจสอบว่าผู้ใช้มีคุณสมบัติตรงตามข้อกำหนดเบื้องต้นเหล่านี้หรือไม่ เพื่อความสะดวก ผู้ใช้ที่มีคุณสมบัติตรงตามข้อกำหนดเบื้องต้นจะเรียกว่าผู้ใช้ที่มีสิทธิ์
เพิ่มการตรวจสอบสิทธิ์ลงในการติดตั้งใช้งานเส้นทางการสร้าง CourseWork
จากนั้นทดสอบฟิลด์ allowed
ในการตอบกลับ สำหรับผู้ใช้ที่มีสิทธิ์ ให้ทำตาม
ตรรกะเพื่อสร้างงานที่มีไฟล์แนบส่วนเสริม หรือสร้างลิงก์
เนื้อหา คุณจะต้องทราบรหัสของหลักสูตรที่ผู้ใช้ต้องการสร้างงาน โดยปกติแล้ว คุณจะแจ้งให้ผู้ใช้
ระบุหลักสูตรที่จะใช้ เพื่อความสะดวก เราจะใช้ค่าที่ฮาร์ดโค้ดในตัวอย่างนี้
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 ...
# Check whether the user can create add-on attachments.
eligibility_response = (
classroom_service.userProfiles()
.checkUserCapability(
userId="me",
capability="CREATE_ADD_ON_ATTACHMENT",
# The previewVersion is necessary while the method is available in the
# Workspace Developer Preview Program.
previewVersion="V1_20240930_PREVIEW",
).execute()
)
is_create_attachment_eligible = eligibility_response.get("allowed")
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
ของงานที่สร้างขึ้นใหม่ - สร้างงานในชั้นเรียน
AddOnAttachment
ใหม่ - ส่งคำขอเพื่อสร้างไฟล์แนบของส่วนเสริมในงานที่สร้างขึ้นใหม่ ใน Google Classroom
Python
# The ID of the course to which the assignment will be added.
course_id = 1234567890 # TODO(developer) Replace with an actual course ID.
# /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
# The ID of the course to which the assignment will be added.
course_id = 1234567890 # TODO(developer) Replace with an actual course ID.
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 & Learning หรือ Plus คุณสามารถสลับสถานะใบอนุญาตของผู้ใช้จากคอนโซลผู้ดูแลระบบของโดเมนทดสอบได้ โดยคลิกปุ่มสร้างงาน CourseWork จากนั้นเปิด UI ของ Google Classroom และตรวจสอบว่ามีการสร้างงานที่มีไฟล์แนบเป็นลิงก์ไปยังสื่อการเรียนการสอน ไฟล์แนบควรแสดงชื่อของ หน้าเว็บที่ลิงก์และ URL
ทดสอบการสร้างไฟล์แนบของส่วนเสริม
กลับไปที่หน้าดัชนีและลงชื่อเข้าใช้ในฐานะผู้ใช้ที่เป็นครูที่มีใบอนุญาต Google Workspace for Education Teaching & Learning หรือ Plus คลิกปุ่มสร้างงาน CourseWork จากนั้นเปิด UI ของ Google Classroom แล้วยืนยันว่ามีการสร้างงานที่มี ไฟล์แนบส่วนเสริม ไฟล์แนบควรแสดงชื่อของแอปพลิเคชันส่วนเสริมและชื่อที่ระบุในโค้ด
การแก้ไขงานทดสอบ
กลับไปที่หน้าดัชนีและตรวจสอบว่าคุณลงชื่อเข้าใช้ในฐานะผู้ใช้ที่เป็นครู ที่มีใบอนุญาต Teaching & Learning หรือ Plus คลิกปุ่มแก้ไขงานใน CourseWork จากนั้นกลับไปที่ UI ของ Google Classroom แล้วตรวจสอบว่า ชื่อของงานมีการเปลี่ยนแปลงแล้ว
ยินดีด้วย คุณดูวิดีโอแนะนำแบบทีละขั้นตอนจบแล้ว