-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from sanjuktaghosh7/classroom-snippets
Classroom Snippets
- Loading branch information
Showing
8 changed files
with
125 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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]'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.