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

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

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

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

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

असाइनमेंट में बदलाव करने की सुविधा की जांच करना

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

बधाई हो! आपने लेखों की सीरीज़ पूरी कर ली है.