活動類型附件

這是 Classroom 外掛程式系列的第第五逐步操作說明。

在這個逐步操作說明中,您將修改上一個逐步操作說明步驟中的範例,以產生活動類型附件。這些是需要學生繳交的所有附件,例如書面回覆、測驗或其他學生產生的成果。

請務必區分內容類型和活動類型附件。活動類型附件與內容類型有以下差異:

  • 學生檢視畫面 iframe 的右上方會顯示「繳交」按鈕。
  • 這類 ID 會提供學生作業的專屬 ID。
  • 學生的附件卡會顯示在 Classroom 評分工具 UI 中。
  • 也可以設定作業成績。

請參閱下逐步操作說明,瞭解如何評分。在本逐步操作說明中,您必須完成下列步驟:

  • 修改先前向 Classroom API 建立附件的要求,以建立活動類型的附件。
  • 為學生繳交的作業建立永久儲存空間。
  • 修改先前的「學生檢視」路徑,接受學生輸入的內容。
  • 提供提供學生工作回顧 iframe 的路線。

以老師身分登入後,您可以透過 Google Classroom UI 為作業建立活動類型附件。課程的學生還可以在 iframe 中完成活動並提交回應。老師可在 Classroom 評分 UI 中查看學生繳交的作業。

為了達到這個範例的目的,請重複使用上一個逐步操作說明中的附件範本,其中顯示知名地標的圖片和地標名稱的說明文字。此活動包括提示學生提供地標名稱。

修改連結建立要求

前往您在上個逐步操作說明中建立內容類型附件的程式碼部分。這裡的鍵項目是 AddOnAttachment 物件的執行個體,我們之前在此為附件指定 teacherViewUristudentViewUrititle

雖然所有外掛程式連結都需要這三個欄位,但 studentWorkReviewUri 是否存在,會決定連結屬於活動類型或內容類型。已填入 studentWorkReviewUriCREATE 要求會變為活動類型附件,不含 studentWorkReviewUriCREATE 要求則會成為內容類型附件。

如要修改這項要求,只需填入 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 類別匯入包含附件處理路徑的伺服器檔案。

修改學生檢視畫面路徑

接著,修改先前的「Student View」路徑以顯示小型表單,並接受學生輸入內容。您可以重複使用上一個逐步操作說明中的大部分程式碼。

找出提供學生檢視畫面路線的伺服器程式碼。這是建立連結時 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 成績工具 UI 中查看學生繳交的作業時,系統就會開啟這個路徑。

當 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。您也可以點選「Turn In」(繳交) 按鈕。

完成活動後,Classroom 應該不會有任何變化。現在測試學生作業評量 iframe:

  • 老師測試使用者身分登入 Classroom。
  • 在「成績」分頁中,找到測試作業的資料欄。按一下測試指派名稱。
  • 找出測試學生使用者的資訊卡。按一下卡片上的附件。

確認系統向學生顯示正確的作業。

恭喜!您可以繼續進行下一個步驟:同步處理附件成績