المرفقات الخارجية والاستلام

هذه هي الجولة التفصيلية السابعة في سلسلة الجولات التفصيلية حول إضافات 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 أو تعديلها. يمكنك إضافة هذا النطاق إلى قوائم النطاقات في حزمة تطوير البرامج (SDK) في Google Workspace Marketplace وشاشة موافقة OAuth ورمز الخادم ضمن مشروعك على Google 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 المُقترَح لإنشاء مهمة جديدة.

عليك أيضًا إضافة زر أو رابط للسماح للمستخدم بتسجيل الدخول إذا لم يسبق لك إجراء ذلك. ستحتاج إلى بيانات اعتماد المستخدم لإجراء طلبات واجهة برمجة التطبيقات اللاحقة، ولذلك يجب على المستخدمين إكمال تأكيد اتصال 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. هذا هو 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 طريقة courses.checkAddOnCreationEligibility لتحديد ما إذا كان المستخدم يستوفي هذه المتطلبات الأساسية. يُشار إلى المستخدم الذي يستوفي المتطلبات الأساسية كمستخدم مؤهل.

أضِف التحقّق من الأهلية إلى عملية تنفيذ مسار الإنشاء "CourseWork". اختبِر بعد ذلك الحقل isCreateAttachmentEligible في الردّ. بالنسبة إلى المستخدمين المؤهَّلين، اتّبِع المنطق لإنشاء مهمة تتضمن إضافة إضافية. وإذا لم تفعل ذلك، أنشئ مادة الرابط. ستحتاج إلى معرفة معرّف الدورة التدريبية التي يريد المستخدم فيها إنشاء مهمة. عادةً، ستطلب من المستخدم تحديد الدورة التدريبية التي يجب استخدامها. للتبسيط، نستخدم قيمة غير قابلة للتغيير في هذا المثال.

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

  # The ID of the course to which the assignment will be added.
  course_id = 1234567890  # TODO(developer) Replace with an actual course ID.

  # Check whether the user can create add-on attachments.
  eligibility_response = (
      classroom_service.courses()
      .checkAddOnCreationEligibility(courseId=course_id)
      .execute()
  )
  is_create_attachment_eligible = eligibility_response.get("isCreateAttachmentEligible")

  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. يمكنك إرسال طلب من واجهة برمجة التطبيقات لإنشاء مهمة "courseWork" في Google Classroom بدون أي مرفقات.
  2. استخرِج id للمهمة الجديدة التي تم إنشاؤها.
  3. يمكنك إنشاء CourseWork AddOnAttachment جديد.
  4. أرسِل طلبًا لإنشاء مرفق إضافة على المهمة التي تم إنشاؤها حديثًا في Google Classroom.

Python

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

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.

لتوضيح ذلك، يمكنك إضافة مسار لتعديل عنصر بث مباشر معيّن. استخدِم هذه الطريقة لإثبات إمكانية وصولك إلى عناصر البث التي أنشأتها وتعديلها باستخدام واجهة برمجة التطبيقات والتي أنشأها معلّم من خلال واجهة مستخدم Google Classroom.

أضِف رابطًا أو زرًا آخر إلى صفحة الويب التي عدّلتها لأول مرة في هذه الجولة التفصيلية. ومن المفترَض أن يتم فتح مسار جديد لتعديل تخصيص 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 ثم افتح واجهة مستخدم Google Classroom وتأكَّد من إنشاء مهمة تتضمّن مرفق "مواد الرابط". يجب أن يعرض المرفق عنوان صفحة الويب المرتبطة وعنوان URL.

اختبار إنشاء مرفقات الإضافة

ارجع إلى صفحة الفهرس وسجِّل الدخول بصفتك معلّمًا لديه ترخيص Google Workspace for Education Teaching & Learning أو Plus. انقر على الزر إنشاء مهمة CourseWork ثم افتح واجهة مستخدم Google Classroom وتحقَّق من أنّه تم إنشاء مهمة دراسية تحتوي على مرفق إضافة. يجب أن يُظهر المرفق اسم تطبيق الإضافة والعنوان المحدد في التعليمات البرمجية الخاصة بك.

اختبار تعديل المهمة

ارجع إلى صفحة الفهرس وتأكَّد من تسجيل الدخول كمستخدم معلّم لديه ترخيص Teaching & Learning أو Plus. انقر على الزر تعديل مهمة CourseWork، ثم ارجع إلى واجهة مستخدم Google Classroom وتحقق من تغيير عنوان المهمة.

تهانينا! لقد أكملت سلسلة الجولات التفصيلية.