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

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

ऐड-ऑन को आज़माएं

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

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

ऐड-ऑन अटैचमेंट बनाने की जांच करें

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

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

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

बधाई हो! आपने सिलसिलेवार तरीके से निर्देश देने वाली सीरीज़ पूरी कर ली है.