Skip to content

Commit

Permalink
Merge pull request #13 from sanjuktaghosh7/classroom-snippets
Browse files Browse the repository at this point in the history
Classroom Snippets
  • Loading branch information
sanjuktaghosh7 authored Jun 17, 2022
2 parents 0699e9c + ab165cc commit 1f215fb
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 117 deletions.
35 changes: 19 additions & 16 deletions classroom/snippets/src/classroom_add_student.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,31 @@
*/

// [START classroom_add_student]
use Google\Client;
use Google\Service\Classroom;
use Google\Service\Classroom\Student;
use Google\Service\Exception;

function enrollAsStudent($service, $courseId, $enrollmentCode) {
$student = new Google_Service_Classroom_Student(array(
function enrollAsStudent($courseId,$enrollmentCode)
{
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.profile.emails");
$service = new Classroom($client);
$student = new Student([
'userId' => 'me'
));
$params = array(
]);
$params = [
'enrollmentCode' => $enrollmentCode
);
];
try {
$student = $service->courses_students->create($courseId, $student, $params);
printf("User '%s' was enrolled as a student in the course with ID '%s'.\n",
$student->profile->name->fullName, $courseId);
} catch (Google_Service_Exception $e) {
} catch (Exception $e) {
if ($e->getCode() == 409) {
print "You are already a member of this course.\n";
} else {
Expand All @@ -39,15 +50,7 @@ function enrollAsStudent($service, $courseId, $enrollmentCode) {
return $student;
}

/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
require 'vendor/autoload.php';
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.profile.emails");
$service = new Google_Service_Classroom($client);
// [END classroom_add_student]

enrollAsStudent($service, '531365794650' ,'[email protected]');
?>
require 'vendor/autoload.php';
enrollAsStudent( '123456','abcdef');
33 changes: 18 additions & 15 deletions classroom/snippets/src/classroom_add_teacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,31 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START classroom_add_teacher]
use Google\Client;
use Google\Service\Classroom;
use Google\Service\Classroom\Teacher;
use Google\service\Exception;

function addTeacher($service, $courseId, $teacherEmail) {
$teacher = new Google_Service_Classroom_Teacher(array(
function addTeacher($courseId, $teacherEmail)
{
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.profile.photos");
$service = new Classroom($client);
$teacher = new Teacher([
'userId' => $teacherEmail
));
]);
try {
// calling create teacher
$teacher = $service->courses_teachers->create($courseId, $teacher);
printf("User '%s' was added as a teacher to the course with ID '%s'.\n",
$teacher->profile->name->fullName, $courseId);
} catch (Google_Service_Exception $e) {
} catch (Exception $e) {
if ($e->getCode() == 409) {
printf("User '%s' is already a member of this course.\n", $teacherEmail);
} else {
Expand All @@ -36,16 +48,7 @@ function addTeacher($service, $courseId, $teacherEmail) {
return $teacher;
}

/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
require 'vendor/autoload.php';
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.profile.photos");
$service = new Google_Service_Classroom($client);
// [END classroom_add_teacher]

//method call
addTeacher($service,'531365794650','[email protected]');
?>
require 'vendor/autoload.php';
addTeacher('531365794650','[email protected]');
30 changes: 15 additions & 15 deletions classroom/snippets/src/classroom_batch_add_students.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,27 @@
*/

// [START classroom_batch_add_students]
use Google\Client;
use Google\Service\Classroom;
use Google\Service\Classroom\Student;
use Google\Service\Exception;

function batchAddStudents($service, $courseId, $studentEmails)
function batchAddStudents($courseId, $studentEmails)
{
/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.profile.emails");
$service = new Classroom($client);
$service->getClient()->setUseBatch(true);
//create batch
$batch = $service->createBatch();
foreach ($studentEmails as $studentEmail) {
$student = new Google_Service_Classroom_Student(array(
$student = new Student([
'userId' => $studentEmail
));
]);
$request = $service->courses_students->create($courseId, $student);
$requestId = $studentEmail;
$batch->add($request, $requestId);
Expand All @@ -36,7 +45,7 @@ function batchAddStudents($service, $courseId, $studentEmails)
$results = $batch->execute();
foreach ($results as $responseId => $student) {
$studentEmail = substr($responseId, strlen('response-') + 1);
if ($student instanceof Google_Service_Exception) {
if ($student instanceof Exception) {
$e = $student;
printf("Error adding user '%s' to the course: %s\n", $studentEmail,
$e->getMessage());
Expand All @@ -49,15 +58,6 @@ function batchAddStudents($service, $courseId, $studentEmails)
return $results;
}

/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
require 'vendor/autoload.php';
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.profile.emails");
$service = new Google_Service_Classroom($client);
// [END classroom_batch_add_students]

batchAddStudents($service, '531365794650', ['a', 'b']);
?>
require 'vendor/autoload.php';
batchAddStudents('123456',['a', 'b']);
30 changes: 15 additions & 15 deletions classroom/snippets/src/classroom_create_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@
*/

// [START classroom_create_course]
use Google\Client;
use Google\Service\Classroom;
use Google\Service\Classroom\Course;
use Google\Service\Exception;

function createCourse($service)
function createCourse()
{
/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.courses");
$service = new Classroom($client);
try {

$course = new Google_Service_Classroom_Course(array(
$course = new Course([
'name' => '10th Grade Biology',
'section' => 'Period 2',
'descriptionHeading' => 'Welcome to 10th Grade Biology',
Expand All @@ -32,23 +41,14 @@ function createCourse($service)
'room' => '301',
'ownerId' => 'me',
'courseState' => 'PROVISIONED'
));
]);
$course = $service->courses->create($course);
printf("Course created: %s (%s)\n", $course->name, $course->id);
return $course;
} catch (Exception $e) {
echo 'Message: ' . $e->getMessage();
}
}

/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
require 'vendor/autoload.php';
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.courses");
$service = new Google_Service_Classroom($client);
// [END classroom_create_course]
createCourse($service);
?>
require 'vendor/autoload.php';
createCourse();
28 changes: 15 additions & 13 deletions classroom/snippets/src/classroom_get_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@
*/

// [START classroom_get_course]
use Google\Service\Exception;
use Google\Client;
use Google\Service\Classroom;
use Google\Service\Exception;

function getCourse($service, $courseId) {
function getCourse($courseId)
{
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.courses");
$service = new Classroom($client);
try {
$course = $service->courses->get($courseId);
printf("Course '%s' found.\n", $course->name);
return $course;
} catch (Google_Service_Exception $e) {
} catch (Exception $e) {
if ($e->getCode() == 404) {
printf("Course with ID '%s' not found.\n", $courseId);
} else {
Expand All @@ -33,15 +42,8 @@ function getCourse($service, $courseId) {
}
}

/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
require 'vendor/autoload.php';
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.courses");
$service = new Google_Service_Classroom($client);
// [END classroom_get_course]

getCourse($service, '531365794650');
?>
require 'vendor/autoload.php';
getCourse('123456');

29 changes: 14 additions & 15 deletions classroom/snippets/src/classroom_list_courses.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@

// [START classroom_list_courses]
use Google\Service\Classroom;
use Google\Client;


function listCourses($service): array
function listCourses(): array
{
$courses = array();
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.courses");
$service = new Classroom($client);
$courses = [];
$pageToken = '';

do {
$params = array(
$params = [
'pageSize' => 100,
'pageToken' => $pageToken
);
];
$response = $service->courses->listCourses($params);
$courses = array_merge($courses, $response->courses);
$pageToken = $response->nextPageToken;
Expand All @@ -45,15 +52,7 @@ function listCourses($service): array
return $courses;
}

/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
require 'vendor/autoload.php';
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.courses");
$service = new Google_Service_Classroom($client);
// [END classroom_list_courses]
require 'vendor/autoload.php';
listCourses();

listCourses($service);
?>
33 changes: 16 additions & 17 deletions classroom/snippets/src/classroom_patch_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@
*/

// [START classroom_patch_course]
use Google\Service\Classroom;
use Google\Service\Classroom\Course;
use Google\Client;

function patchCourse($courseId)
{
/* Load pre-authorized user credentials from the environment.
TODO (developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.courses");
$service = new Classroom($client);

function patchCourse($service, $courseId){
try {

$course = new Google_Service_Classroom_Course(array(
$course = new Course([
'section' => 'Period 3',
'room' => '302'
));
$params = array(
'updateMask' => 'section,room');
]);
$params = ['updateMask' => 'section,room'];
$course = $service->courses->patch($courseId, $course, $params);
printf("Course '%s' updated.\n", $course->name);
return $course;
Expand All @@ -36,15 +44,6 @@ function patchCourse($service, $courseId){
}
}

/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
require 'vendor/autoload.php';
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/classroom.courses");
$service = new Google_Service_Classroom($client);
// [END classroom_patch_course]

patchCourse($service,'531365683519');
?>
require 'vendor/autoload.php';
patchCourse('531365683519');
Loading

0 comments on commit 1f215fb

Please sign in to comment.