设置和更新成绩

本指南提供了 Google Classroom API 的评分相关代码示例。 阅读成绩指南,熟悉 Google 课堂中的评分概念。

为学生提交的作业设置成绩

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;

在 Google 课堂界面中,教师必须先保存草稿成绩,然后才能分配成绩。然后,系统会将分配的成绩发回给学生。您的应用可以通过以下两种方式之一为学生的作业评分:

  • 只分配 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 中的 state 字段表示当前状态。如需更改状态,请调用以下方法之一:

所有这些方法都接受空的 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;
}

为插件附件评分

如果您是 Google 课堂插件开发者,则可以为各个插件附件设置成绩,并将成绩配置为在教师查看学生作业时显示。如需了解详情,请参阅“活动”类型的附件成绩回传演示文稿。