CourseWork را مدیریت کنید

برنامه Classroom از سه نوع آیتم جریانی پشتیبانی می‌کند: CourseWork ، CourseWorkMaterials و Announcements . این راهنما نحوه مدیریت CourseWork را شرح می‌دهد، اما APIهای مربوط به همه آیتم‌های جریانی مشابه هستند. برای کسب اطلاعات بیشتر در مورد انواع آیتم‌های جریانی و تفاوت‌های آنها، به منابع API مراجعه کنید.

منبع CourseWork نشان‌دهنده یک آیتم کاری است که در یک دوره خاص به دانشجویان اختصاص داده شده است، از جمله هرگونه مطالب و جزئیات اضافی، مانند تاریخ تحویل یا حداکثر نمره. چهار زیرگروه CourseWork وجود دارد: تکالیف ، تکالیف آزمون ، سوالات پاسخ کوتاه و سوالات چندگزینه‌ای . Classroom API از سه مورد از این زیرگروه‌ها پشتیبانی می‌کند: تکالیف، سوالات پاسخ کوتاه و سوالات چندگزینه‌ای. این نوع‌ها توسط فیلد CourseWork.workType نمایش داده می‌شوند.

علاوه بر منبع CourseWork ، می‌توانید کارهای تکمیل‌شده را با منبع StudentSubmission مدیریت کنید.

ایجاد دوره‌های آموزشی

CourseWork فقط می‌تواند از طرف استاد دوره ایجاد شود. تلاش برای ایجاد CourseWork از طرف یک دانشجو یا مدیر دامنه که استاد دوره نیست، منجر به خطای PERMISSION_DENIED می‌شود. برای کسب اطلاعات بیشتر در مورد نقش‌های مختلف در Classroom، به انواع کاربر مراجعه کنید.

هنگام ایجاد CourseWork با استفاده از متد courses.courseWork.create ، می‌توانید لینک‌ها را به عنوان materials پیوست کنید، همانطور که در کد نمونه زیر نشان داده شده است:

جاوا

کلاس/قطعه کد/src/main/java/CreateCourseWork.java
CourseWork courseWork = null;
try {
  // Create a link to add as a material on course work.
  Link articleLink =
      new Link()
          .setTitle("SR-71 Blackbird")
          .setUrl("https://www.lockheedmartin.com/en-us/news/features/history/blackbird.html");

  // Create a list of Materials to add to course work.
  List<Material> materials = Arrays.asList(new Material().setLink(articleLink));

  /* Create new CourseWork object with the material attached.
  Set workType to `ASSIGNMENT`. Possible values of workType can be found here:
  https://developers.google.com/classroom/reference/rest/v1/CourseWorkType
  Set state to `PUBLISHED`. Possible values of state can be found here:
  https://developers.google.com/classroom/reference/rest/v1/courses.courseWork#courseworkstate */
  CourseWork content =
      new CourseWork()
          .setTitle("Supersonic aviation")
          .setDescription(
              "Read about how the SR-71 Blackbird, the world’s fastest and "
                  + "highest-flying manned aircraft, was built.")
          .setMaterials(materials)
          .setWorkType("ASSIGNMENT")
          .setState("PUBLISHED");

  courseWork = service.courses().courseWork().create(courseId, content).execute();

  /* Prints the created courseWork. */
  System.out.printf("CourseWork created: %s\n", courseWork.getTitle());
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The courseId does not exist: %s.\n", courseId);
  } else {
    throw e;
  }
  throw e;
} catch (Exception e) {
  throw e;
}
return courseWork;

پایتون

کلاس/قطعه قطعه/کلاس_ایجاد_دوره_کار.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def classroom_create_coursework(course_id):
  """
  Creates the coursework the user has access to.
  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """

  creds, _ = google.auth.default()
  # pylint: disable=maybe-no-member

  try:
    service = build("classroom", "v1", credentials=creds)
    coursework = {
        "title": "Ant colonies",
        "description": """Read the article about ant colonies
                              and complete the quiz.""",
        "materials": [
            {"link": {"url": "http://example.com/ant-colonies"}},
            {"link": {"url": "http://example.com/ant-quiz"}},
        ],
        "workType": "ASSIGNMENT",
        "state": "PUBLISHED",
    }
    coursework = (
        service.courses()
        .courseWork()
        .create(courseId=course_id, body=coursework)
        .execute()
    )
    print(f"Assignment created with ID {coursework.get('id')}")
    return coursework

  except HttpError as error:
    print(f"An error occurred: {error}")
    return error


if __name__ == "__main__":
  # Put the course_id of course whose coursework needs to be created,
  # the user has access to.
  classroom_create_coursework(453686957652)

فیلدهای title و workType الزامی هستند. سایر فیلدها اختیاری هستند. اگر state مشخص نشود، CourseWork در وضعیت پیش‌نویس ایجاد می‌شود.

برای افزودن مطالب لینک‌شده به CourseWork از یک منبع Link با یک url هدف مشخص استفاده کنید. Classroom به‌طور خودکار title و آدرس اینترنتی تصویر کوچک ( thumbnailUrl ) را دریافت می‌کند. Classroom API همچنین به‌طور بومی از مطالب Google Drive و YouTube پشتیبانی می‌کند که می‌توانند به روشی مشابه با یک منبع DriveFile یا منبع YouTubeVideo گنجانده شوند.

برای تعیین تاریخ سررسید، فیلدهای dueDate و dueTime را روی زمان UTC مربوطه تنظیم کنید. تاریخ سررسید باید در آینده باشد.

پاسخ CourseWork شامل یک شناسه اختصاص داده شده توسط سرور است که می‌تواند برای ارجاع به این تکلیف در سایر درخواست‌های API استفاده شود.

بازیابی دوره‌های آموزشی

شما می‌توانید CourseWork از طرف دانش‌آموزان و معلمان دوره مربوطه بازیابی کنید. همچنین می‌توانید CourseWork از طرف مدیران دامنه، حتی اگر آنها معلم آن دوره نباشند، بازیابی کنید. برای بازیابی یک CourseWork خاص، courses.courseWork.get استفاده کنید. برای بازیابی همه CourseWork (که به صورت اختیاری با برخی از معیارها مطابقت دارند)، از courses.courseWork.list استفاده کنید.

محدوده مورد نیاز به نقشی که کاربر درخواست کننده در دوره دارد بستگی دارد. اگر کاربر دانشجو است، از یکی از محدوده‌های زیر استفاده کنید:

  • https://www.googleapis.com/auth/classroom.coursework.me.readonly
  • https://www.googleapis.com/auth/classroom.coursework.me

اگر کاربر معلم یا مدیر دامنه است، از یکی از حوزه‌های زیر استفاده کنید:

  • https://www.googleapis.com/auth/classroom.coursework.students.readonly
  • https://www.googleapis.com/auth/classroom.coursework.students

داشتن مجوز برای بازیابی یک CourseWork به معنای مجوز دسترسی به مطالب یا فراداده‌های مطالب نیست. در عمل، این بدان معناست که اگر یک مدیر عضو دوره نباشد، ممکن است عنوان یک فایل پیوست شده در Drive را نبیند.

مدیریت پاسخ‌های دانش‌آموزان

یک منبع StudentSubmission نشان دهنده کاری است که توسط یک دانشجو برای یک CourseWork انجام می‌شود. این منبع شامل ابرداده‌های مربوط به کار، مانند وضعیت کار و نمره است. یک StudentSubmission به طور ضمنی برای هر دانشجو هنگام ایجاد یک CourseWork جدید ایجاد می‌شود.

بخش‌های بعدی اقدامات رایجی را که پاسخ‌های دانش‌آموزان را مدیریت می‌کنند، توضیح می‌دهند.

بازیابی پاسخ‌های دانش‌آموزان

دانش‌آموزان می‌توانند مطالب ارسالی خود را بازیابی کنند، معلمان می‌توانند مطالب ارسالی همه دانش‌آموزان در دوره‌های خود را بازیابی کنند، و مدیران دامنه می‌توانند تمام مطالب ارسالی همه دانش‌آموزان در دامنه خود را بازیابی کنند. به هر StudentSubmission یک شناسه اختصاص داده می‌شود. اگر شناسه را می‌دانید، courses.courseWork.studentSubmissions.get برای بازیابی مطالب ارسالی استفاده کنید.

از متد courses.courseWork.studentSubmissions.list برای دریافت تمام منابع StudentSubmission که با برخی معیارها مطابقت دارند، همانطور که در نمونه زیر نشان داده شده است، استفاده کنید:

جاوا

class/snippets/src/main/java/ListSubmissions.java
List<StudentSubmission> studentSubmissions = new ArrayList<>();
String pageToken = null;

try {
  do {
    ListStudentSubmissionsResponse response =
        service
            .courses()
            .courseWork()
            .studentSubmissions()
            .list(courseId, courseWorkId)
            .setPageToken(pageToken)
            .execute();

    /* Ensure that the response is not null before retrieving data from it to avoid errors. */
    if (response.getStudentSubmissions() != null) {
      studentSubmissions.addAll(response.getStudentSubmissions());
      pageToken = response.getNextPageToken();
    }
  } while (pageToken != null);

  if (studentSubmissions.isEmpty()) {
    System.out.println("No student submission found.");
  } else {
    for (StudentSubmission submission : studentSubmissions) {
      System.out.printf(
          "Student id (%s), student submission id (%s)\n",
          submission.getUserId(), submission.getId());
    }
  }
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf(
        "The courseId (%s) or courseWorkId (%s) does not exist.\n", courseId, courseWorkId);
  } else {
    throw e;
  }
} catch (Exception e) {
  throw e;
}
return studentSubmissions;

پایتون

class/snippets/classroom_list_submissions.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def classroom_list_submissions(course_id, coursework_id):
  """
  Creates the courses the user has access to.
  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """

  creds, _ = google.auth.default()
  # pylint: disable=maybe-no-member
  submissions = []
  page_token = None

  try:
    service = build("classroom", "v1", credentials=creds)
    while True:
      coursework = service.courses().courseWork()
      response = (
          coursework.studentSubmissions()
          .list(
              pageToken=page_token,
              courseId=course_id,
              courseWorkId=coursework_id,
              pageSize=10,
          )
          .execute()
      )
      submissions.extend(response.get("studentSubmissions", []))
      page_token = response.get("nextPageToken", None)
      if not page_token:
        break

    if not submissions:
      print("No student submissions found.")

    print("Student Submissions:")
    for submission in submissions:
      print(
          "Submitted at:"
          f"{(submission.get('id'), submission.get('creationTime'))}"
      )

  except HttpError as error:
    print(f"An error occurred: {error}")
    submissions = None
  return submissions


if __name__ == "__main__":
  # Put the course_id and coursework_id of course whose list needs to be
  # submitted.
  classroom_list_submissions(453686957652, 466086979658)

منابع StudentSubmission که متعلق به یک دانشجوی خاص است را با مشخص کردن پارامتر userId بازیابی کنید، همانطور که در نمونه زیر نشان داده شده است:

جاوا

class/snippets/src/main/java/ListStudentSubmissions.java
List<StudentSubmission> studentSubmissions = new ArrayList<>();
String pageToken = null;

try {
  do {
    // Set the userId as a query parameter on the request.
    ListStudentSubmissionsResponse response =
        service
            .courses()
            .courseWork()
            .studentSubmissions()
            .list(courseId, courseWorkId)
            .setPageToken(pageToken)
            .set("userId", userId)
            .execute();

    /* Ensure that the response is not null before retrieving data from it to avoid errors. */
    if (response.getStudentSubmissions() != null) {
      studentSubmissions.addAll(response.getStudentSubmissions());
      pageToken = response.getNextPageToken();
    }
  } while (pageToken != null);

  if (studentSubmissions.isEmpty()) {
    System.out.println("No student submission found.");
  } else {
    for (StudentSubmission submission : studentSubmissions) {
      System.out.printf("Student submission: %s.\n", submission.getId());
    }
  }

پایتون

class/snippets/classroom_list_student_submissions.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def classroom_list_student_submissions(course_id, coursework_id, user_id):
  """
  Creates the courses the user has access to.
  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """

  creds, _ = google.auth.default()
  # pylint: disable=maybe-no-member
  submissions = []
  page_token = None

  try:
    service = build("classroom", "v1", credentials=creds)
    while True:
      coursework = service.courses().courseWork()
      response = (
          coursework.studentSubmissions()
          .list(
              pageToken=page_token,
              courseId=course_id,
              courseWorkId=coursework_id,
              userId=user_id,
          )
          .execute()
      )
      submissions.extend(response.get("studentSubmissions", []))
      page_token = response.get("nextPageToken", None)
      if not page_token:
        break

    if not submissions:
      print("No student submissions found.")

    print("Student Submissions:")
    for submission in submissions:
      print(
          "Submitted at:"
          f"{(submission.get('id'), submission.get('creationTime'))}"
      )

  except HttpError as error:
    print(f"An error occurred: {error}")
  return submissions


if __name__ == "__main__":
  # Put the course_id, coursework_id and user_id of course whose list needs
  # to be submitted.
  classroom_list_student_submissions(453686957652, 466086979658, "me")

دانش‌آموزان با شناسه یا آدرس ایمیل منحصر به فرد، همانطور که در منبع Student نشان داده شده است، شناسایی می‌شوند. کاربر فعلی همچنین می‌تواند با استفاده از اختصار "me" به شناسه خود مراجعه کند.

همچنین می‌توان تکالیف ارسالی دانشجویان را برای همه تکالیف درون یک دوره بازیابی کرد. برای انجام این کار، از "-" به عنوان courseWorkId استفاده کنید، همانطور که در نمونه زیر نشان داده شده است:

جاوا

service.courses().courseWork().studentSubmissions()
    .list(courseId, "-")
    .set("userId", userId)
    .execute();

پایتون

service.courses().courseWork().studentSubmissions().list(
    courseId=<course ID or alias>,
    courseWorkId='-',
    userId=<user ID>).execute()

محدوده مورد نیاز به نقشی که کاربر درخواست کننده در دوره دارد بستگی دارد. اگر کاربر معلم یا مدیر دامنه است، از محدوده زیر استفاده کنید:

  • https://www.googleapis.com/auth/classroom.coursework.students.readonly
  • https://www.googleapis.com/auth/classroom.coursework.students

اگر کاربر دانشجو است، از محدوده زیر استفاده کنید:

  • https://www.googleapis.com/auth/classroom.coursework.me.readonly
  • https://www.googleapis.com/auth/classroom.coursework.me

داشتن مجوز بازیابی یک StudentSubmission به معنای مجوز دسترسی به پیوست‌ها یا ابرداده‌های پیوست نیست. در عمل، این بدان معناست که اگر یک مدیر عضو دوره نباشد، ممکن است عنوان یک فایل Drive پیوست شده را نبیند.

افزودن پیوست به پاسخ دانشجو

شما می‌توانید با پیوست کردن یک منبع Link ، DriveFile یا YouTubeVideo ، لینک‌هایی را به مطالب ارسالی دانشجو پیوست کنید. این کار با استفاده از courses.courseWork.studentSubmissions.modifyAttachments انجام می‌شود، همانطور که در نمونه زیر نشان داده شده است:

جاوا

class/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java
StudentSubmission studentSubmission = null;
try {
  // Create ModifyAttachmentRequest object that includes a new attachment with a link.
  Link link = new Link().setUrl("https://en.wikipedia.org/wiki/Irrational_number");
  Attachment attachment = new Attachment().setLink(link);
  ModifyAttachmentsRequest modifyAttachmentsRequest =
      new ModifyAttachmentsRequest().setAddAttachments(Arrays.asList(attachment));

  // The modified studentSubmission object is returned with the new attachment added to it.
  studentSubmission =
      service
          .courses()
          .courseWork()
          .studentSubmissions()
          .modifyAttachments(courseId, courseWorkId, id, modifyAttachmentsRequest)
          .execute();

  /* Prints the modified student submission. */
  System.out.printf(
      "Modified student submission attachments: '%s'.\n",
      studentSubmission.getAssignmentSubmission().getAttachments());
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf(
        "The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does "
            + "not exist.\n",
        courseId, courseWorkId, id);
  } else {
    throw e;
  }
} catch (Exception e) {
  throw e;
}
return studentSubmission;

پایتون

فایل classroom/snippets/classroom_add_attachment.py
def classroom_add_attachment(course_id, coursework_id, submission_id):
  """
  Adds attachment to existing course with specific course_id.
  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """
  creds, _ = google.auth.default()
  # pylint: disable=maybe-no-member
  request = {
      "addAttachments": [
          {"link": {"url": "http://example.com/quiz-results"}},
          {"link": {"url": "http://example.com/quiz-reading"}},
      ]
  }

  try:
    service = build("classroom", "v1", credentials=creds)
    while True:
      coursework = service.courses().courseWork()
      coursework.studentSubmissions().modifyAttachments(
          courseId=course_id,
          courseWorkId=coursework_id,
          id=submission_id,
          body=request,
      ).execute()

  except HttpError as error:
    print(f"An error occurred: {error}")


if __name__ == "__main__":
  # Put the course_id, coursework_id and submission_id of course in which
  # attachment needs to be added.
  classroom_add_attachment("course_id", "coursework_id", "me")

یک پیوست Link توسط url هدف تعریف می‌شود؛ Classroom به طور خودکار title و تصویر کوچک ( thumbnailUrl ) را دریافت می‌کند. برای کسب اطلاعات بیشتر در مورد مطالبی که می‌توانند به StudentSubmissions پیوست شوند، به Material مراجعه کنید.

StudentSubmission فقط می‌تواند توسط استاد درس یا دانشجوی مالک آن تغییر داده شود. شما فقط در صورتی می‌توانید Materials پیوست کنید که CourseWorkType StudentSubmission ASSIGNMENT باشد.

محدوده مورد نیاز به نقشی که کاربر درخواست کننده در دوره دارد بستگی دارد. اگر کاربر معلم است، از محدوده زیر استفاده کنید:

  • https://www.googleapis.com/auth/classroom.coursework.students

اگر کاربر دانشجو است، از محدوده زیر استفاده کنید:

  • https://www.googleapis.com/auth/classroom.coursework.me