बाहरी अटैचमेंट और चालू करें

यह Classroom के ऐड-ऑन के बारे में जानकारी देने वाली सीरीज़ का सातवां वॉकथ्रू है.

इस वॉकथ्रू में, आपको वेब ऐप्लिकेशन में व्यवहार जोड़ने का तरीका बताया गया है, ताकि Google Classroom के बाहर से ऐड-ऑन अटैचमेंट बनाए जा सकें. इस व्यवहार का इस्तेमाल करके, अपने उपयोगकर्ताओं को मौजूदा प्रॉडक्ट या वेबसाइट से ऐड-ऑन वाले अटैचमेंट बनाने की अनुमति दें. यह CourseWork इंटिग्रेशन के लिए भी एक बेहतरीन सुविधा है. इसकी मदद से, मौजूदा ट्रैफ़िक को सीधे तौर पर ऐड-ऑन की ओर भेजा जा सकता है. इससे लोगों को बेहतर अनुभव मिलता है और उनके फ़्लो में कोई बदलाव नहीं होता. सुझाया गया तरीका, Classroom के बाहर अटैचमेंट बनाएं गाइड पेज पर दिया गया है.

आपके पास अपने ऐड-ऑन में व्यवहार जोड़ने का विकल्प भी होता है, ताकि ऐड-ऑन अटैचमेंट की मदद से असाइनमेंट में प्रोग्राम के हिसाब से बदलाव किया जा सके. आपके पास ऐसे किसी भी असाइनमेंट में बदलाव करने का विकल्प होता है जिसमें आपके ऐड-ऑन से अटैच की गई फ़ाइलें शामिल हों. इससे कोई फ़र्क़ नहीं पड़ता कि असाइनमेंट किसने बनाया है. यह सुविधा, छात्र-छात्राओं के लिए खास तौर पर तब काम आती है, जब वे कोई गतिविधि पूरी करने के बाद असाइनमेंट सबमिट करते हैं. इससे शिक्षक को यह पता चलता है कि छात्र-छात्राओं ने असाइन किए गए टास्क पूरे कर लिए हैं और अब उनके काम की समीक्षा की जा सकती है.

आपको अपने ऐड-ऑन का फ़ाइनल वर्शन उपलब्ध कराना होगा. इसमें content-type या activity-type अटैचमेंट की सुविधा होनी चाहिए. इस गाइड में, कॉन्टेंट-टाइप अटैचमेंट का इस्तेमाल किया गया है.

असाइनमेंट मैनेजमेंट के लिए 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 असाइनमेंट बनाने या उनमें बदलाव करने के लिए किया जाता है. इस स्कोप को अपने Cloud प्रोजेक्ट के Google Workspace Marketplace SDK, OAuth सहमति स्क्रीन, और सर्वर कोड में स्कोप की सूचियों में जोड़ें.

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>

अपनी वेबसाइट पर किसी डेस्टिनेशन को दिखाने के लिए, एचटीएमएल टेंप्लेट जोड़ें. इस पेज पर, वह कॉन्टेंट दिखेगा जिसे आपके 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>

CourseWork से जुड़े रास्तों को मैनेज करने के लिए, नई 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 फ़ील्ड की जांच करें. ज़रूरी शर्तें पूरी करने वाले उपयोगकर्ताओं के लिए, ऐड-ऑन अटैचमेंट के साथ असाइनमेंट बनाने का तरीका अपनाएं. इसके अलावा, Link Material बनाएं. आपको उस कोर्स का आईडी पता होना चाहिए जिसमें उपयोगकर्ता को असाइनमेंट बनाना है. आम तौर पर, आपको उपयोगकर्ता से यह पूछना होगा कि उसे कौनसे कोर्स का इस्तेमाल करना है. आसानी से समझने के लिए, हमने इस उदाहरण में हार्ड-कोड की गई वैल्यू का इस्तेमाल किया है.

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. Google Classroom में बिना किसी अटैचमेंट के courseWork असाइनमेंट बनाने के लिए, एपीआई अनुरोध भेजें.
  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 होता है.

इसे दिखाने के लिए, आपको किसी स्ट्रीम आइटम में बदलाव करने के लिए एक रूट जोड़ना होगा. इस तरीके का इस्तेमाल करके, यह पुष्टि करें कि आपके पास एपीआई का इस्तेमाल करके बनाए गए स्ट्रीम आइटम को ऐक्सेस करने और उनमें बदलाव करने का अधिकार है और शिक्षक ने 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()
)

ऐड-ऑन की जांच करना

आसान बनाने के लिए, दिए गए उदाहरणों में कोर्स और असाइनमेंट के आइडेंटिफ़ायर को हार्ड-कोड किया गया है. टीचर के क्रेडेंशियल का इस्तेमाल करके, courses और courseWork संसाधनों के get और list तरीकों से अनुरोध करके, इन आइडेंटिफ़ायर को पाया जा सकता है. असाइनमेंट बनाते समय, जवाब में भी इन्हें शामिल किया जाता है.courseWork

अपना सर्वर चलाएं. इसके बाद, अपने इंडेक्स पेज पर जाएं और शिक्षक के तौर पर साइन इन करें. इसके लिए, आपके पास Google Workspace for Education Teaching & Learning या Plus का लाइसेंस नहीं होना चाहिए. अपने टेस्ट डोमेन की Admin console से, किसी उपयोगकर्ता के लाइसेंस का स्टेटस टॉगल किया जा सकता है. CourseWork असाइनमेंट बनाएं बटन पर क्लिक करें. इसके बाद, Google Classroom का यूज़र इंटरफ़ेस (यूआई) खोलें और पुष्टि करें कि लिंक किए गए मटीरियल अटैचमेंट वाला असाइनमेंट बनाया गया है. अटैचमेंट में, लिंक किए गए वेब पेज का टाइटल और यूआरएल दिखना चाहिए.

ऐड-ऑन अटैचमेंट बनाने की सुविधा की जांच करना

इंडेक्स पेज पर वापस जाएं और Google Workspace for Education Teaching & Learning या Plus के लाइसेंस वाले शिक्षक खाते से साइन इन करें. कोर्सवर्क असाइनमेंट बनाएं बटन पर क्लिक करें. इसके बाद, Google Classroom का यूज़र इंटरफ़ेस (यूआई) खोलें और पुष्टि करें कि ऐड-ऑन अटैचमेंट वाला असाइनमेंट बन गया है. अटैचमेंट में, आपके ऐड-ऑन ऐप्लिकेशन का नाम और आपके कोड में दिया गया टाइटल दिखना चाहिए.

असाइनमेंट में किए गए बदलावों की जांच करना

इंडेक्स पेज पर वापस जाएं और पक्का करें कि आपने शिक्षक के तौर पर साइन इन किया हो. साथ ही, आपके पास Teaching & Learning या Plus का लाइसेंस हो. CourseWork असाइनमेंट में बदलाव करें बटन पर क्लिक करें. इसके बाद, Google Classroom के यूज़र इंटरफ़ेस (यूआई) पर वापस जाएं और पुष्टि करें कि असाइनमेंट का टाइटल बदल गया है.

बधाई हो! आपने वॉकट्रू सीरीज़ पूरी कर ली है.