Skip to content

Commit

Permalink
Merge pull request #106 from RajeshGogo/classroom-snippets
Browse files Browse the repository at this point in the history
Classroom snippets
  • Loading branch information
sqrrrl authored Jun 17, 2022
2 parents 3949c2c + 1f215fb commit a9b259b
Show file tree
Hide file tree
Showing 9 changed files with 434 additions and 0 deletions.
9 changes: 9 additions & 0 deletions classroom/snippets/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"require": {
"google/apiclient": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"monolog/monolog": "^3.0"
}
}
56 changes: 56 additions & 0 deletions classroom/snippets/src/classroom_add_student.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* 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
*
* http://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_add_student]
use Google\Client;
use Google\Service\Classroom;
use Google\Service\Classroom\Student;
use Google\Service\Exception;

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 = [
'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 (Exception $e) {
if ($e->getCode() == 409) {
print "You are already a member of this course.\n";
} else {
throw $e;
}
}
return $student;
}

// [END classroom_add_student]

require 'vendor/autoload.php';
enrollAsStudent( '123456','abcdef');
54 changes: 54 additions & 0 deletions classroom/snippets/src/classroom_add_teacher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* 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
*
* http://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_add_teacher]
use Google\Client;
use Google\Service\Classroom;
use Google\Service\Classroom\Teacher;
use Google\service\Exception;

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 (Exception $e) {
if ($e->getCode() == 409) {
printf("User '%s' is already a member of this course.\n", $teacherEmail);
} else {
throw $e;
}
}
return $teacher;
}

// [END classroom_add_teacher]

require 'vendor/autoload.php';
addTeacher('531365794650','[email protected]');
63 changes: 63 additions & 0 deletions classroom/snippets/src/classroom_batch_add_students.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* 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
*
* http://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_batch_add_students]
use Google\Client;
use Google\Service\Classroom;
use Google\Service\Classroom\Student;
use Google\Service\Exception;

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 Student([
'userId' => $studentEmail
]);
$request = $service->courses_students->create($courseId, $student);
$requestId = $studentEmail;
$batch->add($request, $requestId);
}
//executing request
$results = $batch->execute();
foreach ($results as $responseId => $student) {
$studentEmail = substr($responseId, strlen('response-') + 1);
if ($student instanceof Exception) {
$e = $student;
printf("Error adding user '%s' to the course: %s\n", $studentEmail,
$e->getMessage());
} else {
printf("User '%s' was added as a student to the course.\n",
$student->profile->name->fullName, $courseId);
}
}
$service->getClient()->setUseBatch(false);
return $results;
}

// [END classroom_batch_add_students]
require 'vendor/autoload.php';
batchAddStudents('123456',['a', 'b']);
54 changes: 54 additions & 0 deletions classroom/snippets/src/classroom_create_course.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* 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
*
* http://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_create_course]
use Google\Client;
use Google\Service\Classroom;
use Google\Service\Classroom\Course;
use Google\Service\Exception;

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 Course([
'name' => '10th Grade Biology',
'section' => 'Period 2',
'descriptionHeading' => 'Welcome to 10th Grade Biology',
'description' => 'We\'ll be learning about about the structure of living ' .
'creatures from a combination of textbooks, guest ' .
'lectures, and lab work. Expect to be excited!',
'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();
}
}
// [END classroom_create_course]
require 'vendor/autoload.php';
createCourse();
49 changes: 49 additions & 0 deletions classroom/snippets/src/classroom_get_course.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* 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
*
* http://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_get_course]
use Google\Client;
use Google\Service\Classroom;
use Google\Service\Exception;

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 (Exception $e) {
if ($e->getCode() == 404) {
printf("Course with ID '%s' not found.\n", $courseId);
} else {
throw $e;
}
}
}

// [END classroom_get_course]

require 'vendor/autoload.php';
getCourse('123456');

58 changes: 58 additions & 0 deletions classroom/snippets/src/classroom_list_courses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* 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
*
* http://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_courses]
use Google\Service\Classroom;
use Google\Client;

function listCourses(): 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 = [
'pageSize' => 100,
'pageToken' => $pageToken
];
$response = $service->courses->listCourses($params);
$courses = array_merge($courses, $response->courses);
$pageToken = $response->nextPageToken;
} while (!empty($pageToken));

if (count($courses) == 0) {
print "No courses found.\n";
} else {
print "Courses:\n";
foreach ($courses as $course) {
printf("%s (%s)\n", $course->name, $course->id);
}
}
return $courses;
}

// [END classroom_list_courses]
require 'vendor/autoload.php';
listCourses();

Loading

0 comments on commit a9b259b

Please sign in to comment.