Skip to content

Commit e49fb41

Browse files
feat: added List, ModifyAttachments, Patch, Return methods for StudentSubmission resource (googleworkspace#467)
* add alias snippet and unit test * java snippets for topics, create coursework, create alias * updates based on team review * renamed alias methods, added devsite comments * added missing license header to TestDeleteTopic.java * Adding List, ModifyAttachments, Patch, Return StudentSubmission methods * minor fixes * adding comments for developer * modifications based on team review * modifications based on team review * Adding print statements and devsite tag to ListCourseAliases
1 parent 2fa7089 commit e49fb41

11 files changed

+534
-7
lines changed

classroom/snippets/src/main/java/CreateCourseWork.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ public static CourseWork createCourseWork(String courseId) throws IOException {
8080
.setDescription("Read about how the SR-71 Blackbird, the world’s fastest and "
8181
+ "highest-flying manned aircraft, was built.")
8282
.setMaterials(materials)
83-
.setDueDate(new Date().setMonth(12).setDay(10).setYear(2022))
84-
.setDueTime(new TimeOfDay().setHours(15).setMinutes(0))
8583
.setWorkType("ASSIGNMENT")
8684
.setState("PUBLISHED");
8785

8886
courseWork = service.courses().courseWork().create(courseId, content)
8987
.execute();
88+
89+
/* Prints the created courseWork. */
90+
System.out.printf("CourseWork created: %s\n", courseWork.getTitle());
9091
} catch (GoogleJsonResponseException e) {
9192
//TODO (developer) - handle error appropriately
9293
GoogleJsonError error = e.getDetails();

classroom/snippets/src/main/java/GetTopic.java

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public static Topic getTopic(String courseId, String topicId) throws IOException
6060
try {
6161
// Get the topic.
6262
topic = service.courses().topics().get(courseId, topicId).execute();
63+
System.out.printf("Topic '%s' found.\n", topic.getName());
6364
} catch (GoogleJsonResponseException e) {
6465
//TODO (developer) - handle error appropriately
6566
GoogleJsonError error = e.getDetails();

classroom/snippets/src/main/java/ListCourseAliases.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515

16-
// [START classroom_list_aliases]
16+
// [START classroom_list_aliases_class]
1717

1818
import com.google.api.client.googleapis.json.GoogleJsonError;
1919
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
@@ -57,6 +57,8 @@ public static List<CourseAlias> listCourseAliases(String courseId)
5757
.setApplicationName("Classroom samples")
5858
.build();
5959

60+
// [START classroom_list_aliases_code_snippet]
61+
6062
String pageToken = null;
6163
List<CourseAlias> courseAliases = new ArrayList<>();
6264

@@ -89,6 +91,8 @@ public static List<CourseAlias> listCourseAliases(String courseId)
8991
}
9092
}
9193
return courseAliases;
94+
95+
// [END classroom_list_aliases_code_snippet]
9296
}
9397
}
94-
// [END classroom_list_aliases]
98+
// [END classroom_list_aliases_class]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
// [START classroom_list_student_submissions_class]
17+
18+
import com.google.api.client.googleapis.json.GoogleJsonError;
19+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
20+
import com.google.api.client.http.HttpRequestInitializer;
21+
import com.google.api.client.http.javanet.NetHttpTransport;
22+
import com.google.api.client.json.gson.GsonFactory;
23+
import com.google.api.services.classroom.Classroom;
24+
import com.google.api.services.classroom.ClassroomScopes;
25+
import com.google.api.services.classroom.model.ListStudentSubmissionsResponse;
26+
import com.google.api.services.classroom.model.StudentSubmission;
27+
import com.google.auth.http.HttpCredentialsAdapter;
28+
import com.google.auth.oauth2.GoogleCredentials;
29+
import java.io.IOException;
30+
import java.util.ArrayList;
31+
import java.util.Collections;
32+
import java.util.List;
33+
34+
/* Class to demonstrate the use of Classroom List StudentSubmissions API. */
35+
public class ListStudentSubmissions {
36+
/**
37+
* Retrieves a specific student's submissions for the specified course work.
38+
*
39+
* @param courseId - identifier of the course.
40+
* @param courseWorkId - identifier of the course work.
41+
* @param userId - identifier of the student whose work to return.
42+
* @return - list of student submissions.
43+
* @throws IOException - if credentials file not found.
44+
*/
45+
public static List<StudentSubmission> listStudentSubmissions(String courseId, String courseWorkId,
46+
String userId) throws IOException {
47+
/* Load pre-authorized user credentials from the environment.
48+
TODO(developer) - See https://developers.google.com/identity for
49+
guides on implementing OAuth2 for your application. */
50+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
51+
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS));
52+
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
53+
credentials);
54+
55+
// Create the classroom API client.
56+
Classroom service = new Classroom.Builder(new NetHttpTransport(),
57+
GsonFactory.getDefaultInstance(),
58+
requestInitializer)
59+
.setApplicationName("Classroom samples")
60+
.build();
61+
62+
// [START classroom_list_student_submissions_code_snippet]
63+
64+
List<StudentSubmission> studentSubmissions = new ArrayList<>();
65+
String pageToken = null;
66+
67+
try {
68+
do {
69+
// Set the userId as a query parameter on the request.
70+
ListStudentSubmissionsResponse response = service.courses().courseWork().studentSubmissions()
71+
.list(courseId, courseWorkId)
72+
.set("userId", userId)
73+
.execute();
74+
if (response.getStudentSubmissions() != null) {
75+
studentSubmissions.addAll(response.getStudentSubmissions());
76+
pageToken = response.getNextPageToken();
77+
}
78+
} while (pageToken != null);
79+
80+
if (studentSubmissions.isEmpty()) {
81+
System.out.println("No student submission found.");
82+
} else {
83+
for (StudentSubmission submission : studentSubmissions) {
84+
System.out.printf("Student submission: %s.\n", submission.getId());
85+
}
86+
}
87+
} catch (GoogleJsonResponseException e) {
88+
//TODO (developer) - handle error appropriately
89+
GoogleJsonError error = e.getDetails();
90+
if (error.getCode() == 404) {
91+
System.out.printf("The courseId (%s), courseWorkId (%s), or userId (%s) does "
92+
+ "not exist.\n", courseId, courseWorkId, userId);
93+
} else {
94+
throw e;
95+
}
96+
} catch (Exception e) {
97+
throw e;
98+
}
99+
return studentSubmissions;
100+
101+
// [END classroom_list_student_submissions_code_snippet]
102+
103+
}
104+
}
105+
// [END classroom_list_student_submissions_class]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
// [START classroom_list_submissions_class]
17+
18+
import com.google.api.client.googleapis.json.GoogleJsonError;
19+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
20+
import com.google.api.client.http.HttpRequestInitializer;
21+
import com.google.api.client.http.javanet.NetHttpTransport;
22+
import com.google.api.client.json.gson.GsonFactory;
23+
import com.google.api.services.classroom.Classroom;
24+
import com.google.api.services.classroom.ClassroomScopes;
25+
import com.google.api.services.classroom.model.ListStudentSubmissionsResponse;
26+
import com.google.api.services.classroom.model.StudentSubmission;
27+
import com.google.auth.http.HttpCredentialsAdapter;
28+
import com.google.auth.oauth2.GoogleCredentials;
29+
import java.io.IOException;
30+
import java.util.ArrayList;
31+
import java.util.Collections;
32+
import java.util.List;
33+
34+
/* Class to demonstrate the use of Classroom List StudentSubmissions API. */
35+
public class ListSubmissions {
36+
/**
37+
* Retrieves submissions for all students for the specified course work in a course.
38+
*
39+
* @param courseId - identifier of the course.
40+
* @param courseWorkId - identifier of the course work.
41+
* @return - list of student submissions.
42+
* @throws IOException - if credentials file not found.
43+
*/
44+
public static List<StudentSubmission> listSubmissions(String courseId, String courseWorkId)
45+
throws IOException {
46+
/* Load pre-authorized user credentials from the environment.
47+
TODO(developer) - See https://developers.google.com/identity for
48+
guides on implementing OAuth2 for your application. */
49+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
50+
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS));
51+
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
52+
credentials);
53+
54+
// Create the classroom API client.
55+
Classroom service = new Classroom.Builder(new NetHttpTransport(),
56+
GsonFactory.getDefaultInstance(),
57+
requestInitializer)
58+
.setApplicationName("Classroom samples")
59+
.build();
60+
61+
// [START classroom_list_submissions_code_snippet]
62+
63+
List<StudentSubmission> studentSubmissions = new ArrayList<>();
64+
String pageToken = null;
65+
66+
try {
67+
do {
68+
ListStudentSubmissionsResponse response = service.courses().courseWork().studentSubmissions()
69+
.list(courseId, courseWorkId)
70+
.execute();
71+
if (response.getStudentSubmissions() != null) {
72+
studentSubmissions.addAll(response.getStudentSubmissions());
73+
}
74+
} while (pageToken != null);
75+
76+
if (studentSubmissions.isEmpty()) {
77+
System.out.println("No student submission found.");
78+
} else {
79+
for (StudentSubmission submission : studentSubmissions) {
80+
System.out.printf("Student id (%s), student submission id (%s)\n", submission.getUserId(),
81+
submission.getId());
82+
}
83+
}
84+
} catch (GoogleJsonResponseException e) {
85+
//TODO (developer) - handle error appropriately
86+
GoogleJsonError error = e.getDetails();
87+
if (error.getCode() == 404) {
88+
System.out.printf("The courseId (%s) or courseWorkId (%s) does not exist.\n", courseId,
89+
courseWorkId);
90+
} else {
91+
throw e;
92+
}
93+
} catch (Exception e) {
94+
throw e;
95+
}
96+
return studentSubmissions;
97+
98+
// [END classroom_list_submissions_code_snippet]
99+
100+
}
101+
}
102+
// [END classroom_list_submissions_class]

classroom/snippets/src/main/java/ListTopics.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ public static List<Topic> listTopics(String courseId) throws IOException {
6767
.setPageSize(100)
6868
.setPageToken(pageToken)
6969
.execute();
70-
topics.addAll(response.getTopic());
71-
pageToken = response.getNextPageToken();
70+
if (response.getTopic() != null) {
71+
topics.addAll(response.getTopic());
72+
pageToken = response.getNextPageToken();
73+
}
7274
} while (pageToken != null);
7375

7476
if (topics.isEmpty()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
// [START classroom_modify_attachments_student_submissions_class]
17+
18+
import com.google.api.client.googleapis.json.GoogleJsonError;
19+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
20+
import com.google.api.client.http.HttpRequestInitializer;
21+
import com.google.api.client.http.javanet.NetHttpTransport;
22+
import com.google.api.client.json.gson.GsonFactory;
23+
import com.google.api.services.classroom.Classroom;
24+
import com.google.api.services.classroom.ClassroomScopes;
25+
import com.google.api.services.classroom.model.Attachment;
26+
import com.google.api.services.classroom.model.Link;
27+
import com.google.api.services.classroom.model.ModifyAttachmentsRequest;
28+
import com.google.api.services.classroom.model.StudentSubmission;
29+
import com.google.auth.http.HttpCredentialsAdapter;
30+
import com.google.auth.oauth2.GoogleCredentials;
31+
import java.io.IOException;
32+
import java.util.Arrays;
33+
import java.util.Collections;
34+
35+
/* Class to demonstrate the use of Classroom ModifyAttachments StudentSubmissions API. */
36+
public class ModifyAttachmentsStudentSubmission {
37+
/**
38+
* Modify attachments on a student submission.
39+
*
40+
* @param courseId - identifier of the course.
41+
* @param courseWorkId - identifier of the course work.
42+
* @param id - identifier of the student submission.
43+
* @return - the modified student submission.
44+
* @throws IOException - if credentials file not found.
45+
*/
46+
public static StudentSubmission modifyAttachments(String courseId, String courseWorkId, String id)
47+
throws IOException {
48+
/* Load pre-authorized user credentials from the environment.
49+
TODO(developer) - See https://developers.google.com/identity for
50+
guides on implementing OAuth2 for your application. */
51+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
52+
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS));
53+
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
54+
credentials);
55+
56+
// Create the classroom API client.
57+
Classroom service = new Classroom.Builder(new NetHttpTransport(),
58+
GsonFactory.getDefaultInstance(),
59+
requestInitializer)
60+
.setApplicationName("Classroom samples")
61+
.build();
62+
63+
// [START classroom_modify_attachments_student_submissions_code_snippet]
64+
65+
StudentSubmission studentSubmission = null;
66+
try {
67+
// Create ModifyAttachmentRequest object that includes a new attachment with a link.
68+
Link link = new Link().setUrl("https://en.wikipedia.org/wiki/Irrational_number");
69+
Attachment attachment = new Attachment().setLink(link);
70+
ModifyAttachmentsRequest modifyAttachmentsRequest = new ModifyAttachmentsRequest()
71+
.setAddAttachments(Arrays.asList(attachment));
72+
73+
// The modified studentSubmission object is returned with the new attachment added to it.
74+
studentSubmission = service.courses().courseWork().studentSubmissions().modifyAttachments(
75+
courseId, courseWorkId, id, modifyAttachmentsRequest)
76+
.execute();
77+
78+
/* Prints the modified student submission. */
79+
System.out.printf("Modified student submission attachments: '%s'.\n", studentSubmission
80+
.getAssignmentSubmission()
81+
.getAttachments());
82+
} catch (GoogleJsonResponseException e) {
83+
//TODO (developer) - handle error appropriately
84+
GoogleJsonError error = e.getDetails();
85+
if (error.getCode() == 404) {
86+
System.out.printf("The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does "
87+
+ "not exist.\n", courseId, courseWorkId, id);
88+
} else {
89+
throw e;
90+
}
91+
} catch(Exception e) {
92+
throw e;
93+
}
94+
return studentSubmission;
95+
96+
// [END classroom_modify_attachments_student_submissions_code_snippet]
97+
98+
}
99+
}
100+
// [END classroom_modify_attachments_student_submissions_class]

0 commit comments

Comments
 (0)