Skip to content

Commit

Permalink
Merge pull request #12 from sanjuktaghosh7/classroom-snippets
Browse files Browse the repository at this point in the history
Classroom Snippets
  • Loading branch information
sanjuktaghosh7 authored Jun 16, 2022
2 parents 64e6a14 + 5082faf commit 99f358e
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 111 deletions.
37 changes: 19 additions & 18 deletions classroom/snippets/src/classroom_add_student.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,33 @@
*/

// [START classroom_add_student]
require __DIR__ . '/vendor/autoload.php';
use Google\Service\Classroom\Student;

function enrollAsStudent($service, $courseId, $enrollmentCode) {
$student = new Google_Service_Classroom_Student(array(
'userId' => 'me'
));
$params = array(
'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) {
if ($e->getCode() == 409) {
print "You are already a member of this course.\n";
} else {
throw $e;
$student = new Google_Service_Classroom_Student(array(
'userId' => 'me'
));
$params = array(
'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) {
if ($e->getCode() == 409) {
print "You are already a member of this course.\n";
} else {
throw $e;
}
}
}
return $student;
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");
Expand Down
38 changes: 19 additions & 19 deletions classroom/snippets/src/classroom_add_teacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,37 @@
* limitations under the License.
*/
// [START classroom_add_teacher]
require __DIR__ . '/vendor/autoload.php';
use Google\Service\Classroom\Teacher;

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

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

// [START classroom_batch_add_students]
require __DIR__ . '/vendor/autoload.php';
use Google\Service\Classroom\Student;
use Google\Service\Exception;

function batchAddStudents($service, $courseId, $studentEmails) {
$service->getClient()->setUseBatch(true);
//create batch
$batch = $service->createBatch();
foreach($studentEmails as $studentEmail) {
$student = new Google_Service_Classroom_Student(array(
'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 Google_Service_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);
function batchAddStudents($service, $courseId, $studentEmails)
{
$service->getClient()->setUseBatch(true);
//create batch
$batch = $service->createBatch();
foreach ($studentEmails as $studentEmail) {
$student = new Google_Service_Classroom_Student(array(
'userId' => $studentEmail
));
$request = $service->courses_students->create($courseId, $student);
$requestId = $studentEmail;
$batch->add($request, $requestId);
}
}
$service->getClient()->setUseBatch(false);
return $results;
//executing request
$results = $batch->execute();
foreach ($results as $responseId => $student) {
$studentEmail = substr($responseId, strlen('response-') + 1);
if ($student instanceof Google_Service_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;
}

/* 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");
Expand Down
4 changes: 2 additions & 2 deletions classroom/snippets/src/classroom_create_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

// [START classroom_create_course]
require __DIR__ . '/vendor/autoload.php';
use Google\Service\Classroom\Course;

function createCourse($service)
{
Expand Down Expand Up @@ -44,11 +44,11 @@ function createCourse($service)
/* 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);
?>
24 changes: 13 additions & 11 deletions classroom/snippets/src/classroom_get_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@
*/

// [START classroom_get_course]
require __DIR__ . '/vendor/autoload.php';
use Google\Service\Exception;
use Google\Service\Classroom;

function getCourse($service, $courseId) {
try {
$course = $service->courses->get($courseId);
printf("Course '%s' found.\n", $course->name);
} catch (Google_Service_Exception $e) {
if ($e->getCode() == 404) {
printf("Course with ID '%s' not found.\n", $courseId);
} else {
throw $e;
try {
$course = $service->courses->get($courseId);
printf("Course '%s' found.\n", $course->name);
return $course;
} catch (Google_Service_Exception $e) {
if ($e->getCode() == 404) {
printf("Course with ID '%s' not found.\n", $courseId);
} else {
throw $e;
}
}
}
return $course;
}

/* 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");
Expand Down
46 changes: 25 additions & 21 deletions classroom/snippets/src/classroom_list_courses.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,39 @@
*/

// [START classroom_list_courses]
require __DIR__ . '/vendor/autoload.php';
function listCourses($service) {
$pageToken = NULL;
$courses = array();
use Google\Service\Classroom;

do {
$params = array(
'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);
function listCourses($service): array
{
$courses = array();
$pageToken = '';

do {
$params = array(
'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;
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");
Expand Down
7 changes: 4 additions & 3 deletions classroom/snippets/src/classroom_patch_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*/

// [START classroom_patch_course]
require __DIR__ . '/vendor/autoload.php';
use Google\Service\Classroom\Course;

function patchCourse($service, $courseId)
{

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

$course = new Google_Service_Classroom_Course(array(
Expand All @@ -39,6 +39,7 @@ 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");
Expand Down
20 changes: 10 additions & 10 deletions classroom/snippets/src/classroom_update_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@
*/

// [START classroom_update_course]
require __DIR__ . '/vendor/autoload.php';
use Google\Service\Classroom;

function updateCourse($service, $courseId) {
$course = $service->courses->get($courseId);
$course->section = 'Period 3';
$course->room = '302';
$course = $service->courses->update($courseId, $course);
printf("Course '%s' updated.\n", $course->name);
return $course;
function updateCourse($service, $courseId){
$course = $service->courses->get($courseId);
$course->section = 'Period 3';
$course->room = '302';
$course = $service->courses->update($courseId, $course);
printf("Course '%s' updated.\n", $course->name);
return $course;
}

/* 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_update_course]

updateCourse($service,531365794650);
updateCourse($service,'531365794650');
?>

0 comments on commit 99f358e

Please sign in to comment.