Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions classroom/snippets/src/main/java/CreateCourseWork.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ public static CourseWork createCourseWork(String courseId) throws IOException {
.setDescription("Read about how the SR-71 Blackbird, the world’s fastest and "
+ "highest-flying manned aircraft, was built.")
.setMaterials(materials)
.setDueDate(new Date().setMonth(12).setDay(10).setYear(2022))
.setDueTime(new TimeOfDay().setHours(15).setMinutes(0))
.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();
Expand Down
1 change: 1 addition & 0 deletions classroom/snippets/src/main/java/GetTopic.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static Topic getTopic(String courseId, String topicId) throws IOException
try {
// Get the topic.
topic = service.courses().topics().get(courseId, topicId).execute();
System.out.printf("Topic '%s' found.\n", topic.getName());
} catch (GoogleJsonResponseException e) {
//TODO (developer) - handle error appropriately
GoogleJsonError error = e.getDetails();
Expand Down
8 changes: 6 additions & 2 deletions classroom/snippets/src/main/java/ListCourseAliases.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.


// [START classroom_list_aliases]
// [START classroom_list_aliases_class]

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

// [START classroom_list_aliases_code_snippet]

String pageToken = null;
List<CourseAlias> courseAliases = new ArrayList<>();

Expand Down Expand Up @@ -89,6 +91,8 @@ public static List<CourseAlias> listCourseAliases(String courseId)
}
}
return courseAliases;

// [END classroom_list_aliases_code_snippet]
}
}
// [END classroom_list_aliases]
// [END classroom_list_aliases_class]
105 changes: 105 additions & 0 deletions classroom/snippets/src/main/java/ListStudentSubmissions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


// [START classroom_list_student_submissions_class]

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.classroom.Classroom;
import com.google.api.services.classroom.ClassroomScopes;
import com.google.api.services.classroom.model.ListStudentSubmissionsResponse;
import com.google.api.services.classroom.model.StudentSubmission;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/* Class to demonstrate the use of Classroom List StudentSubmissions API. */
public class ListStudentSubmissions {
/**
* Retrieves a specific student's submissions for the specified course work.
*
* @param courseId - identifier of the course.
* @param courseWorkId - identifier of the course work.
* @param userId - identifier of the student whose work to return.
* @return - list of student submissions.
* @throws IOException - if credentials file not found.
*/
public static List<StudentSubmission> listStudentSubmissions(String courseId, String courseWorkId,
String userId) throws IOException {
/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS));
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
credentials);

// Create the classroom API client.
Classroom service = new Classroom.Builder(new NetHttpTransport(),
GsonFactory.getDefaultInstance(),
requestInitializer)
.setApplicationName("Classroom samples")
.build();

// [START classroom_list_student_submissions_code_snippet]

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)
.set("userId", userId)
.execute();
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());
}
}
} 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 userId (%s) does "
+ "not exist.\n", courseId, courseWorkId, userId);
} else {
throw e;
}
} catch (Exception e) {
throw e;
}
return studentSubmissions;

// [END classroom_list_student_submissions_code_snippet]

}
}
// [END classroom_list_student_submissions_class]
102 changes: 102 additions & 0 deletions classroom/snippets/src/main/java/ListSubmissions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


// [START classroom_list_submissions_class]

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.classroom.Classroom;
import com.google.api.services.classroom.ClassroomScopes;
import com.google.api.services.classroom.model.ListStudentSubmissionsResponse;
import com.google.api.services.classroom.model.StudentSubmission;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/* Class to demonstrate the use of Classroom List StudentSubmissions API. */
public class ListSubmissions {
/**
* Retrieves submissions for all students for the specified course work in a course.
*
* @param courseId - identifier of the course.
* @param courseWorkId - identifier of the course work.
* @return - list of student submissions.
* @throws IOException - if credentials file not found.
*/
public static List<StudentSubmission> listSubmissions(String courseId, String courseWorkId)
throws IOException {
/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS));
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
credentials);

// Create the classroom API client.
Classroom service = new Classroom.Builder(new NetHttpTransport(),
GsonFactory.getDefaultInstance(),
requestInitializer)
.setApplicationName("Classroom samples")
.build();

// [START classroom_list_submissions_code_snippet]

List<StudentSubmission> studentSubmissions = new ArrayList<>();
String pageToken = null;

try {
do {
ListStudentSubmissionsResponse response = service.courses().courseWork().studentSubmissions()
.list(courseId, courseWorkId)
.execute();
if (response.getStudentSubmissions() != null) {
studentSubmissions.addAll(response.getStudentSubmissions());
}
} 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;

// [END classroom_list_submissions_code_snippet]

}
}
// [END classroom_list_submissions_class]
6 changes: 4 additions & 2 deletions classroom/snippets/src/main/java/ListTopics.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ public static List<Topic> listTopics(String courseId) throws IOException {
.setPageSize(100)
.setPageToken(pageToken)
.execute();
topics.addAll(response.getTopic());
pageToken = response.getNextPageToken();
if (response.getTopic() != null) {
topics.addAll(response.getTopic());
pageToken = response.getNextPageToken();
}
} while (pageToken != null);

if (topics.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


// [START classroom_modify_attachments_student_submissions_class]

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.classroom.Classroom;
import com.google.api.services.classroom.ClassroomScopes;
import com.google.api.services.classroom.model.Attachment;
import com.google.api.services.classroom.model.Link;
import com.google.api.services.classroom.model.ModifyAttachmentsRequest;
import com.google.api.services.classroom.model.StudentSubmission;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;

/* Class to demonstrate the use of Classroom ModifyAttachments StudentSubmissions API. */
public class ModifyAttachmentsStudentSubmission {
/**
* Modify attachments on a student submission.
*
* @param courseId - identifier of the course.
* @param courseWorkId - identifier of the course work.
* @param id - identifier of the student submission.
* @return - the modified student submission.
* @throws IOException - if credentials file not found.
*/
public static StudentSubmission modifyAttachments(String courseId, String courseWorkId, String id)
throws IOException {
/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS));
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
credentials);

// Create the classroom API client.
Classroom service = new Classroom.Builder(new NetHttpTransport(),
GsonFactory.getDefaultInstance(),
requestInitializer)
.setApplicationName("Classroom samples")
.build();

// [START classroom_modify_attachments_student_submissions_code_snippet]

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;

// [END classroom_modify_attachments_student_submissions_code_snippet]

}
}
// [END classroom_modify_attachments_student_submissions_class]
Loading