設定及更新成績

本指南提供 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 外掛程式開發人員,可以為個別外掛程式附件設定成績,並設定老師在查看學生作業時可查看的成績。詳情請參閱活動類型附件成績回傳操作說明。