ไฟล์แนบประเภทกิจกรรม

นี่คือคำแนะนำแบบทีละขั้นที่ 5 ในชุดคำแนะนำแบบทีละขั้นเกี่ยวกับส่วนเสริมของ Classroom

ในคำแนะนำแบบทีละขั้นนี้ คุณจะได้แก้ไขตัวอย่างจากขั้นตอนคำแนะนำแบบทีละขั้นก่อนหน้านี้เพื่อสร้างไฟล์แนบประเภทกิจกรรม ได้แก่ ไฟล์แนบที่ให้นักเรียนส่ง เช่น คำตอบที่เขียนขึ้น แบบทดสอบ หรืออาร์ติแฟกต์อื่นๆ ที่นักเรียนสร้างขึ้น

ความแตกต่างระหว่างไฟล์แนบประเภทเนื้อหากับไฟล์แนบประเภทกิจกรรมเป็นสิ่งสำคัญ ไฟล์แนบของประเภทกิจกรรมจะแตกต่างจากประเภทเนื้อหาดังนี้

  • ปุ่ม "ส่ง" จะปรากฏขึ้นที่ด้านขวาบนของ iframe มุมมองของนักเรียน
  • มีตัวระบุที่ไม่ซ้ำกันสำหรับงานของนักเรียน
  • การ์ดไฟล์แนบของนักเรียนจะปรากฏใน UI การให้คะแนนของ Classroom
  • นักเรียนสามารถตั้งค่าคะแนนสำหรับงานที่ได้รับมอบหมาย

ดูคำแนะนำแบบทีละขั้นถัดไปสำหรับการพูดคุยเกี่ยวกับการให้คะแนน ในคำแนะนำแบบทีละขั้นนี้ คุณจะต้องดำเนินการต่อไปนี้

  • แก้ไขคำขอสร้างไฟล์แนบก่อนหน้าที่ส่งไปยัง Classroom API เพื่อสร้างไฟล์แนบประเภทกิจกรรม
  • ใช้พื้นที่เก็บข้อมูลถาวรสำหรับงานที่นักเรียนส่ง
  • แก้ไขเส้นทางมุมมองของนักเรียนก่อนหน้านี้เพื่อยอมรับข้อมูลที่ป้อนของนักเรียน
  • ระบุเส้นทางเพื่อแสดง iframe การตรวจงานของนักเรียน

เมื่อเสร็จแล้ว คุณสามารถสร้างไฟล์แนบประเภทกิจกรรมในงานผ่าน UI ของ Google Classroom เมื่อเข้าสู่ระบบในฐานะครู นักเรียนในชั้นเรียนก็จะทำกิจกรรมใน iframe จนเสร็จและส่งคำตอบได้ด้วย ครูสามารถดูงานที่นักเรียนส่งได้ใน UI การให้คะแนนของ Classroom

เพื่อวัตถุประสงค์ของตัวอย่างนี้ ให้ใช้เทมเพลตไฟล์แนบจากคำแนะนำแบบทีละขั้นก่อนหน้า ซึ่งแสดงภาพของจุดสังเกตที่มีชื่อเสียงและคำบรรยายภาพพร้อมชื่อของจุดสังเกต กิจกรรมจะประกอบด้วยการแจ้งให้นักเรียน ระบุชื่อของจุดสังเกต

แก้ไขคำขอสร้างไฟล์แนบ

ไปที่ส่วนของโค้ดซึ่งคุณสร้างไฟล์แนบประเภทเนื้อหาในคำแนะนำแบบทีละขั้นก่อนหน้านี้ รายการคีย์ที่นี่เป็นอินสแตนซ์ของออบเจ็กต์ AddOnOptions ซึ่งก่อนหน้านี้เราได้ระบุ teacherViewUri, studentViewUri และ title สำหรับไฟล์แนบ

แม้ว่าไฟล์แนบของส่วนเสริมทั้งหมดจะต้องใช้ 3 ช่องนี้ แต่การมีหรือไม่มี studentWorkReviewUri จะกำหนดว่าไฟล์แนบนั้นเป็นประเภทกิจกรรมหรือประเภทเนื้อหา คำขอ CREATE ที่มีการป้อนข้อมูล studentWorkReviewUri จะกลายเป็นไฟล์แนบประเภทกิจกรรม ขณะที่คำขอ CREATE ที่ไม่มี studentWorkReviewUri จะกลายเป็นไฟล์แนบประเภทเนื้อหา

การแก้ไขเพียงอย่างเดียวที่จะทำได้ต่อคำขอนี้คือการใส่ข้อมูลในช่อง studentWorkReviewUri เพิ่มเส้นทางที่มีชื่อที่เหมาะสมที่นี่ ซึ่งคุณนำไปใช้ในขั้นตอนถัดไป

Python

ในตัวอย่างที่เราให้ไว้ ค่านี้อยู่ในเมธอด create_attachments ในไฟล์ webapp/attachment_routes.py

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: ตัวระบุที่ไม่ซ้ำกันสำหรับไฟล์แนบ มอบหมายโดย Classroom และส่งคืนในคำตอบเมื่อสร้างไฟล์แนบ
  • submission_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 เมื่อสร้างไฟล์แนบ การเปลี่ยนแปลงแรกที่ต้องทำคือการดึงข้อมูล submissionId จากการตอบกลับของ getAddOnContext

Python

ในตัวอย่างที่เราให้ไว้ ค่านี้อยู่ในเมธอด load_activity_attachment ในไฟล์ webapp/attachment_routes.py

# 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 เมื่อสร้างไฟล์แนบ เส้นทางนี้จะเปิดขึ้นเมื่อครูดูงานที่นักเรียนส่งใน UI เครื่องมือให้คะแนนของ Classroom

คุณจะได้รับพารามิเตอร์การค้นหา submissionId เมื่อ Classroom เปิด iframe การตรวจงานของนักเรียน ใช้เพื่อเรียกดูงานของนักเรียนจากฐานข้อมูลในเครื่อง

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 ในฐานะผู้ใช้ทดสอบของครู
  • ค้นหาคอลัมน์สำหรับงานทดสอบในแท็บคะแนน คลิกชื่องานทดสอบของคุณ
  • ค้นหาการ์ดสำหรับผู้ใช้ที่เป็นนักเรียนทดสอบ คลิกไฟล์แนบในการ์ด

ตรวจสอบว่าการส่งที่ถูกต้องปรากฏขึ้นสำหรับนักเรียน

ยินดีด้วย คุณก็พร้อมที่จะทำขั้นตอนถัดไปแล้ว นั่นคือการซิงค์คะแนนไฟล์แนบ