設定及更新成績

本指南提供 Google Classroom API 的評分相關程式碼範例。參閱成績指南,熟悉 Classroom 的評分概念。

為學生繳交的作業設定成績

StudentSubmission 資源有兩個用來儲存成績的欄位:assignedGrade 是向學生回報的成績,draftGrade 則是只有老師才能查看的暫定成績。這些欄位會使用 courses.courseWork.studentSubmissions.patch 進行更新。

Python

studentSubmission = {
  'assignedGrade': 99,
  'draftGrade': 80
}

service.courses().courseWork().studentSubmissions().patch(
    courseId=course_id,
    courseWorkId=coursework_id,
    id=studentsubmission_id,
    updateMask='assignedGrade,draftGrade',
    body=studentSubmission).execute()

Java

classroom/snippets/src/main/java/PatchStudentSubmission.java
StudentSubmission studentSubmission = null;
try {
  // Updating the draftGrade and assignedGrade fields for the specific student submission.
  StudentSubmission content =
      service
          .courses()
          .courseWork()
          .studentSubmissions()
          .get(courseId, courseWorkId, id)
          .execute();
  content.setAssignedGrade(90.00);
  content.setDraftGrade(80.00);

  // The updated studentSubmission object is returned with the new draftGrade and assignedGrade.
  studentSubmission =
      service
          .courses()
          .courseWork()
          .studentSubmissions()
          .patch(courseId, courseWorkId, id, content)
          .set("updateMask", "draftGrade,assignedGrade")
          .execute();

  /* Prints the updated student submission. */
  System.out.printf(
      "Updated student submission draft grade (%s) and assigned grade (%s).\n",
      studentSubmission.getDraftGrade(), studentSubmission.getAssignedGrade());
} 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 使用者介面時,老師必須先儲存草稿成績,才能指派成績。接著,系統會將指定的成績發還給學生。應用程式可以透過下列任一方式為學生的作業評分:

  • 僅指派 draftGrade。舉例來說,這項功能可讓老師在最終定案前手動查看成績。學生無法查看草稿成績。

  • 同時指派draftGradeassignedGrade,即可完全批改作業。

讀取已指派的成績

您可以探索 courses.courseWork.studentSubmissions.list 方法的回應物件,列出特定課程項目的所有成績:

Python

response = coursework.studentSubmissions().list(
    courseId=course_id,
    courseWorkId=coursework_id,
    pageSize=10 # optionally include `pageSize` to restrict the number of student submissions included in the response.
).execute()
submissions.extend(response.get('studentSubmissions', []))

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

print('Student Submissions:')

for submission in submissions:
    print(f"Submitted at:"
          f"{(submission.get('userId'), submission.get('assignedGrade'))}")

Java

classroom/snippets/src/main/java/ListStudentSubmissions.java
  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 submissions found.");
} else {
  for (StudentSubmission submission : studentSubmissions) {
    System.out.printf(
        "User ID %s, Assigned grade: %s\n",
        submission.getUserId(), submission.getAssignedGrade());
  }
}

決定課程的整體成績

Classroom API 不允許開發人員讀取或寫入整體課程成績,但您可以透過程式輔助方式計算成績。設定評分說明中心文章提供這項計算方式的訣竅。Course 資源包含 gradebookSettings 欄位,可協助您執行計算。

如要計算整體成績,請參閱相關指引,瞭解如何處理遲交、有正當理由未交和未交的課程作業。

管理學生回覆狀態

學生的回應可能未繳交、已繳交或已發還。StudentSubmission 中的狀態欄位會指出目前的狀態。如要變更狀態,請呼叫下列任一方法:

這些方法都接受空白的 body 參數,例如:

Python

service.courses().courseWork().studentSubmission().turnIn(
    courseId=course_id,
    courseWorkId=coursework_id,
    id=studentsubmission_id,
    body={}).execute()

Java

classroom/snippets/src/main/java/ReturnStudentSubmission.java
try {
  service
      .courses()
      .courseWork()
      .studentSubmissions()
      .classroomReturn(courseId, courseWorkId, id, null)
      .execute();
} 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;
}

評分外掛程式附件

如果您是 Classroom 外掛程式開發人員,可以為個別外掛程式附件設定成績,並設定老師批改學生作業時就會看到成績。詳情請參閱「活動類型附件」和「成績回傳」逐步操作說明。