這是 Classroom 外掛程式導覽系列中的第五個導覽。
在本逐步操作教學課程中,您將修改上一個逐步操作教學課程步驟中的範例,產生活動類型附件。這類附件包括需要學生提交的內容,例如書面回覆、測驗或其他學生製作的項目。
請務必區分內容類型和活動類型附件。 活動類型附件與內容類型附件的差異如下:
- 「繳交」按鈕會顯示在學生檢視畫面 iframe 的右上角。
- 這些 ID 是學生作業的專屬 ID。
- 附件卡片會顯示在 Classroom 評分工具 UI 中。
- 他們可以為所屬作業設定成績。
如要瞭解評分,請參閱下一個逐步解說。在本逐步解說課程中,您將完成下列操作:
- 修改先前的附件建立要求,透過 Classroom API 建立活動類型的附件。
- 為學生提交的內容實作永久儲存空間。
- 修改先前的「學生檢視畫面」路徑,接受學生輸入內容。
- 提供路徑,用於放送學生作業審查 iframe。
完成後,以老師身分登入時,即可透過 Google Classroom 使用者介面,在作業中建立活動類型的附件。班級中的學生也可以在 iframe 中完成活動並提交回覆。老師可以在 Classroom 評分使用者介面中查看學生提交的作業。
在本範例中,請重複使用上一個逐步操作說明中的附件範本,該範本會顯示著名地標的圖片,以及地標名稱的說明文字。這項活動會提示學生提供地標名稱。
修改附件建立要求
前往程式碼部分,您在上一個逐步操作說明中建立內容類型附件。這裡的重點項目是 AddOnAttachment 物件的執行個體,我們先前已為附件指定 teacherViewUri
、studentViewUri
和 title
。
雖然所有外掛程式附件都需要這三個欄位,但附件是活動類型還是內容類型,取決於 studentWorkReviewUri
的存在與否。如果 CREATE
要求已填入 studentWorkReviewUri
,就會成為活動類型的附件;如果 CREATE
要求未填入 studentWorkReviewUri
,就會成為內容類型的附件。
這項要求唯一需要修改的地方,就是填入 studentWorkReviewUri
欄位。在此新增適當命名的路徑,您會在後續步驟中實作該路徑。
Python
在我們提供的範例中,這位於 webapp/attachment_routes.py
檔案的 create_attachments
方法中。
attachment = {
# Specifies the route for a teacher user.
"teacherViewUri": {
"uri":
flask.url_for(
"load_activity_attachment",
_scheme='https',
_external=True),
},
# Specifies the route for a student user.
"studentViewUri": {
"uri":
flask.url_for(
"load_activity_attachment",
_scheme='https',
_external=True)
},
# Specifies the route for a teacher user when the attachment is
# loaded in the Classroom grading view.
# The presence of this field marks this as an activity-type attachment.
"studentWorkReviewUri": {
"uri":
flask.url_for(
"view_submission", _scheme='https', _external=True)
},
# The title of the attachment.
"title": f"Attachment {attachment_count}",
}
為內容類型附件新增永久儲存空間
記錄學生對活動的回應。老師在「學生作業審查」iframe 中查看提交內容時,可以稍後再查閱。
為 Submission
設定資料庫結構定義。我們提供的範例會要求學生輸入圖片中地標的名稱。因此,Submission
包含下列屬性:
attachment_id
:附件的專屬 ID。由 Classroom 指派,並在建立附件時於回應中傳回。submission_id
:學生提交內容的 ID。由 Classroom 指派,並在學生檢視畫面中以getAddOnContext
回應傳回。
student_response
:學生提供的答案。
Python
從上一個步驟擴充 SQLite 和 flask_sqlalchemy
實作項目。
前往您定義先前表格的檔案 (如果您是按照我們提供的範例操作,則為 models.py
)。在檔案底部新增下列內容。
# Database model to represent a student submission.
class Submission(db.Model):
# The attachmentId is the unique identifier for the attachment.
submission_id = db.Column(db.String(120), primary_key=True)
# The unique identifier for the student's submission.
attachment_id = db.Column(db.String(120), primary_key=True)
# The student's response to the question prompt.
student_response = db.Column(db.String(120))
將新的 Submission
類別匯入伺服器檔案,並使用附件處理路徑。
修改學生檢視畫面路徑
接著,修改先前的「學生檢視畫面」路徑,顯示小型表單並接受學生的輸入內容。您可以重複使用先前逐步解說中的大部分程式碼。
找出提供學生檢視畫面路徑的伺服器程式碼。這是建立連結時在 studentViewUri
欄位中指定的路徑。首先要進行的變更是從 getAddOnContext
回應中擷取 submissionId
。
Python
在我們提供的範例中,這位於 webapp/attachment_routes.py
檔案的 load_activity_attachment
方法中。
# Issue a request to the courseWork.getAddOnContext endpoint
addon_context_response = classroom_service.courses().courseWork(
).getAddOnContext(
courseId=flask.session["courseId"],
itemId=flask.session["itemId"]).execute()
# One of studentContext or teacherContext will be populated.
user_context = "student" if addon_context_response.get(
"studentContext") else "teacher"
# If the user is a student...
if user_context == "student":
# Extract the submissionId from the studentContext object.
# This value is provided by Google Classroom.
flask.session["submissionId"] = addon_context_response.get(
"studentContext").get("submissionId")
您也可以發出要求,取得學生繳交作業的狀態。
回應包含 SubmissionState
值,可指出學生是否已開啟或繳交附件等狀態。如果您想禁止學生編輯已繳交的作業,或是想讓老師深入瞭解學生的進度,這項功能就非常實用:
Python
在我們提供的範例中,這是上述 load_activity_attachment
方法的延續。
# Issue a request to get the status of the student submission.
submission_response = classroom_service.courses().courseWork(
).addOnAttachments().studentSubmissions().get(
courseId=flask.session["courseId"],
itemId=flask.session["itemId"],
attachmentId=flask.session["attachmentId"],
submissionId=flask.session["submissionId"]).execute()
最後,從資料庫擷取附件資訊,並提供輸入表單。我們提供的範例表單包含字串輸入欄位和提交按鈕。顯示地標圖片,並提示學生輸入地標名稱。收到回覆後,請記錄在資料庫中。
Python
在我們提供的範例中,這是上述 load_activity_attachment
方法的延續。
# Look up the attachment in the database.
attachment = Attachment.query.get(flask.session["attachmentId"])
message_str = f"I see that you're a {user_context}! "
message_str += (
f"I've loaded the attachment with ID {attachment.attachment_id}. "
if user_context == "teacher" else
"Please complete the activity below.")
form = activity_form_builder()
if form.validate_on_submit():
# Record the student's response in our database.
# Check if the student has already submitted a response.
# If so, update the response stored in the database.
student_submission = Submission.query.get(flask.session["submissionId"])
if student_submission is not None:
student_submission.student_response = form.student_response.data
else:
# Store the student's response by the submission ID.
new_submission = Submission(
submission_id=flask.session["submissionId"],
attachment_id=flask.session["attachmentId"],
student_response=form.student_response.data)
db.session.add(new_submission)
db.session.commit()
return flask.render_template(
"acknowledge-submission.html",
message="Your response has been recorded. You can close the " \
"iframe now.",
instructions="Please Turn In your assignment if you have " \
"completed all tasks."
)
# Show the activity.
return flask.render_template(
"show-activity-attachment.html",
message=message_str,
image_filename=attachment.image_filename,
image_caption=attachment.image_caption,
user_context=user_context,
form=form,
responses=response_strings)
為區分使用者,請考慮停用提交功能,改為在老師檢視畫面中顯示正確答案。
為學生作業審查 iframe 新增路徑
最後,新增路徑來提供學生作業審查 iframe。這個路徑的名稱應與建立附件時提供的 studentWorkReviewUri
相符。老師在 Classroom 評分工具使用者介面中查看學生繳交的作業時,系統會開啟這條路徑。
Classroom 開啟「學生作業審查」iframe 時,您會收到 submissionId
查詢參數。您可以使用這個函式,從本機資料庫擷取學生的作業:
Python
在我們提供的範例中,這位於 webapp/attachment_routes.py
檔案中。
@app.route("/view-submission")
def view_submission():
"""
Render a student submission using the show-student-submission.html template.
"""
# Save the query parameters passed to the iframe in the session, just as we did
# in previous routes. Abbreviated here for readability.
add_iframe_query_parameters_to_session(flask.request.args)
# For the sake of brevity in this example, we'll skip the conditional logic
# to see if we need to authorize the user as we have done in previous steps.
# We can assume that the user that reaches this route is a teacher that has
# already authorized and created an attachment using the add-on.
# In production, we recommend fully validating the user's authorization at
# this stage as well.
# Look up the student's submission in our database.
student_submission = Submission.query.get(flask.session["submissionId"])
# Look up the attachment in the database.
attachment = Attachment.query.get(student_submission.attachment_id)
# Render the student's response alongside the correct answer.
return flask.render_template(
"show-student-submission.html",
message=f"Loaded submission {student_submission.submission_id} for "\
f"attachment {attachment.attachment_id}.",
student_response=student_submission.student_response,
correct_answer=attachment.image_caption)
測試外掛程式
重複上一個逐步說明中的「測試外掛程式」步驟。您應附加學生可開啟的檔案。
如要測試活動附件,請完成下列步驟:
- 以其中一位學生測試使用者身分登入 Google Classroom,並加入與老師測試使用者相同的課程。
- 前往「課堂作業」分頁,然後展開測試作業。
- 按一下外掛程式附件資訊卡,開啟學生檢視畫面,然後提交活動的回覆。
- 完成活動後,請關閉 iframe。(選用) 按一下「繳交」按鈕。
完成活動後,Classroom 應該不會有任何變更。現在測試「查看學生作業」iframe:
- 以老師測試使用者身分登入 Classroom。
- 在「成績」分頁中,找出測驗作業的欄。按一下測驗作業名稱。
- 找出測試學生使用者的資訊卡。按一下資訊卡上的附件。
確認學生看到的是正確的提交內容。
恭喜!現在可以進行下一個步驟:同步附件成績。