קבצים מצורפים חיצוניים והגשה

זהו המדריך השביעי בסדרת המדריכים בנושא תוספים ל-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 הוא מוסיפים את שלושת המסלולים הבאים. שימו לב שתצטרכו למלא חלק מהתוכן בהמשך.

# /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.

יצירת מטלה עם קובץ מצורף של תוסף למשתמשים שעומדים בדרישות

אם המשתמש עומד בדרישות ליצירת קבצים מצורפים של תוספים, צריך לבצע את הפעולות הבאות:

  1. שליחת בקשת API ליצירת מטלה courseWork ב-Google Classroom ללא קבצים מצורפים.
  2. מחפשים את id של המטלה החדשה שיצרתם.
  3. יוצרים CourseWork AddOnAttachment חדש.
  4. שולחים בקשה ליצירת קובץ מצורף של תוסף במטלה החדשה שנוצרה ב-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, לשנות אותו, לשלוח אותו, לבטל את השליחה שלו או להחזיר אותו, אם יש בו לפחות קובץ מצורף אחד של תוסף, בלי קשר למי שיצר את הפריט בפיד. פריטים בעדכונים הם כל Announcement, CourseWork מטלה או CourseWorkMaterial.

כדי להדגים את זה, תוסיפו מסלול לשינוי פריט נתון בזרם. אפשר להשתמש בשיטה הזו כדי לוודא שיש לכם גישה לפריטים בפיד שנוצרו על ידכם באמצעות ה-API וגם לפריטים שנוצרו על ידי מורה דרך ממשק המשתמש של Google Classroom, ושיש לכם אפשרות לשנות אותם.

מוסיפים עוד קישור או כפתור לדף האינטרנט שערכתם קודם במדריך הזה. ייפתח מסלול חדש לשינוי CourseWork מטלה.

Python

בדוגמה של Python שמופיעה כאן, משנים את המסלול /index שהשתנה קודם במדריך הזה.

<!-- /webapp/templates/index.html -->
<a href="modify-coursework-assignment.html">Create a CourseWork Assignment</a>

יוצרים נתיב חדש לטיפול בנתיבים שקשורים לכלי המשימות. אפשר לראות את זה בקובץ 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.

מריצים את השרת, עוברים לדף האינדקס ונכנסים בתור משתמש שהוא מורה ללא רישיון ל-Teaching & Learning או ל-Plus ב-Google Workspace for Education. אתם יכולים לשנות את סטטוס הרישיון של משתמש ממסוף Admin של דומיין הבדיקה.לוחצים על הלחצן יצירת מטלה ב-CourseWork, ואז פותחים את ממשק המשתמש של Google Classroom ומוודאים שנוצרה מטלה עם קובץ מצורף מסוג Link Material. בקובץ המצורף צריכות להופיע הכותרת של דף האינטרנט המקושר וכתובת URL.

בדיקה של יצירת קובץ מצורף של תוסף

חוזרים לדף האינדקס ונכנסים בתור משתמש מורה עם רישיון Google Workspace for Education Teaching & Learning או Plus. לוחצים על הלחצן יצירת מטלה, ואז פותחים את ממשק המשתמש של Google Classroom ומוודאים שנוצרה מטלה עם קובץ מצורף של תוסף. בקובץ המצורף צריך להופיע השם של אפליקציית התוסף והכותרת שצוינה בקוד.

בדיקת שינוי הקצאה

חוזרים לדף האינדקס ומוודאים שנכנסתם לחשבון בתור משתמש עם רישיון Teaching & Learning או Plus. לוחצים על הלחצן Modify a CourseWork Assignment (שינוי מטלה), ואז חוזרים לממשק המשתמש של Google Classroom ומוודאים ששם המטלה השתנה.

מעולה! סיימתם את סדרת ההדרכות.