Created
October 21, 2017 20:06
-
-
Save mouhsinelonly/416113585afcebf60381266c7457b855 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <?php namespace Modules\Classrooms\Libraries; | |
| use Modules\Classrooms\Libraries\AuthHeader; | |
| use SoapClient; | |
| use SoapFault; | |
| use SoapHeader; | |
| use Log; | |
| /** | |
| * Class to create classrooms using Perculus api V2 | |
| */ | |
| class PerculusRepository implements ClassroomInterface { | |
| private $header; | |
| private $code_url = 'https://app.perculus.com/perculus.aspx?c='; | |
| private $client; | |
| public function create($classroom_data = [], $teacher_data = []) | |
| { | |
| if(empty($classroom_data) || empty($teacher_data)) { | |
| return 'classroom and teacher data required'; | |
| } | |
| $start_at = date("Y-m-d", strtotime($classroom_data['start_at'])) . | |
| 'T' . | |
| date("H:i:s", strtotime($classroom_data['start_at'])). "+04:00"; | |
| // Log::info($start_at); | |
| $this->setTarget('rooms'); | |
| $perculus_room_data = [ | |
| 'roomId' => 0, | |
| 'sessionName' => $classroom_data['title'], | |
| 'description' => isset($classroom_data['description']) ? $classroom_data['description'] : $classroom_data['title'], | |
| 'beginDate' => $start_at, | |
| 'duration' => isset($classroom_data['duration']) ? $classroom_data['duration'] : '60', | |
| 'category' => 'LMS', | |
| 'colorCode' => '#FFFFFF', | |
| 'language' => 'ar-OM', | |
| 'streamCount' => 1, | |
| 'externalLogoUrl' => 'https://el-css.edu.om/admin/public/template/images/logo.png', | |
| 'startActive' => 1, | |
| 'formEnroll_Allow' => 0, | |
| 'formEnroll_Capacity' => 0, | |
| 'formEnroll_Type' => 0, | |
| 'sendPassKey' => 1, | |
| 'passKey' => '', | |
| 'sendICS' => 0, | |
| 'userRightSchema' => 'elearning', | |
| 'defaultLayout' => 'elearning', | |
| 'permittedLayoutAliases' => ['elearning'], | |
| 'customModuleSettings' => '' | |
| ]; | |
| // Log::info('trying creating room'); | |
| $perculus_room_result = $this->call('addRoom', $perculus_room_data); | |
| if (!$perculus_room_result OR !$perculus_room_result->response OR !$perculus_room_result->response->success) { | |
| Log::info(print_r($perculus_room_result, true)); | |
| return false; | |
| } | |
| // Log::info('got details'); | |
| $perculus_room_response = $perculus_room_result->response->data->enc_value; | |
| $perculus_teacher_data = [ | |
| 'roomId' => $perculus_room_response->ROOMID, | |
| 'emailAddress' => $teacher_data['email'], | |
| 'surname' => $teacher_data['username'], | |
| 'name' => $teacher_data['name'], | |
| 'inUserType' => 'e' | |
| ]; | |
| $perculus_teacher_result = $this->call('RegisterExternalAttendee', $perculus_teacher_data); | |
| if (!isset($perculus_teacher_result->response)) { | |
| Log::info(print_r($perculus_teacher_result, true)); | |
| return false; | |
| } | |
| // Log::info('created teacher'); | |
| $response = [ | |
| 'room_id' => $perculus_room_response->ROOMID, | |
| 'presenter_url' => $this->code_url . $perculus_teacher_result->response->data, | |
| 'recording_url' => $this->code_url . $perculus_teacher_result->response->data | |
| ]; | |
| return $response; | |
| } | |
| public function addAttendee($room_id = 0, $attendee_data = [], $attendee_type = 'u') | |
| { | |
| $this->setTarget('rooms'); | |
| $perculus_attendee_data = [ | |
| 'roomId' => $room_id, | |
| 'name' => $attendee_data['name'], | |
| 'emailAddress' => $attendee_data['email'], | |
| 'surname' => $attendee_data['username'], | |
| 'inUserType' => $attendee_type | |
| ]; | |
| $perculus_student_result = $this->call('RegisterExternalAttendee', $perculus_attendee_data); | |
| if(!isset($perculus_student_result->response)) { | |
| Log::info(print_r($perculus_student_result, true)); | |
| return false; | |
| } | |
| $response = [ | |
| 'url' => $this->code_url . $perculus_student_result->response->data, | |
| 'recording_url' => $this->code_url . $perculus_student_result->response->data, | |
| 'id' => null | |
| ]; | |
| return $response; | |
| } | |
| public function update($id = 0, $data = []) { | |
| } | |
| /** | |
| * delete specific room | |
| * @param integer $room_id the id of the room | |
| * @return boolean a boolean indecating if the classroom was deleted | |
| */ | |
| public function delete($room_id = 0) { | |
| $this->setTarget('rooms'); | |
| $perculus_room_data = [ | |
| 'roomId' => $room_id | |
| ]; | |
| $result = $this->call('Delete', $perculus_room_data); | |
| if (isset($result->response) AND isset($result->response->success)) { | |
| return $result->response->success; | |
| } | |
| return false; | |
| } | |
| public function getAttendanceReport($room_id = 0) { | |
| $this->setTarget('rooms'); | |
| $this->packRoom($room_id); | |
| $perculus_room_data = [ | |
| 'roomId' => $room_id, | |
| 'withUserStats' => true | |
| ]; | |
| $result = $this->call('GetRoomStat', $perculus_room_data); | |
| if(!isset($result->response) || !isset($result->response->data) || empty($result->response->data) || $result->response->data == 'null') { | |
| return false; | |
| } | |
| $perculus_attendance_data = json_decode($result->response->data, true); | |
| $attendees = []; | |
| foreach ($perculus_attendance_data['UserStats'] as $attendee) { | |
| switch ($attendee['UserType']) { | |
| case 1: | |
| $user_type = 'admin'; | |
| break; | |
| case 2: | |
| $user_type = 'teacher'; | |
| break; | |
| case 3: | |
| $user_type = 'student'; | |
| break; | |
| default: | |
| $user_type = 'student'; | |
| break; | |
| } | |
| $username_parts = explode(" ",strtolower($attendee['UserName'])); | |
| $username = $username_parts[count($username_parts) - 1]; | |
| $attendance = [ | |
| 'username' => $username, | |
| 'id' => NULL, | |
| 'duration' => $attendee['DurationLive'] > 0 ? (float)number_format($attendee['DurationLive'] / 60, 2) : 0, | |
| 'user_type' => $user_type, | |
| 'enter_at' => date('Y-m-d H:i:s', strtotime('+4 hours', strtotime($attendee['FirstAttemptLive']))), | |
| 'exit_at' => date('Y-m-d H:i:s', strtotime('+4 hours', strtotime($attendee['LastAttemptLive']))) | |
| ]; | |
| $attendees[] = $attendance; | |
| } | |
| return $attendees; | |
| } | |
| public function packRoom($room_id = 0) | |
| { | |
| $this->setTarget('rooms'); | |
| $perculus_room_data = [ | |
| 'roomId' => $room_id | |
| ]; | |
| $result = $this->call('PackRoom', $perculus_room_data); | |
| if (isset($result->response) AND isset($result->response->success)) { | |
| return $result->response->success; | |
| } | |
| return true; | |
| } | |
| /** | |
| * set target url for the current opration | |
| * @param string $target target for the current operation eg: rooms | |
| */ | |
| private function setTarget($target = '') | |
| { | |
| $url = "http://app.perculus.com/api/v2/$target.asmx?WSDL"; | |
| $this->client = new SoapClient($url, array("trace" => 1, "exception" => 0)); | |
| $auth = new AuthHeader(config('classrooms.perculus_username'), config('classrooms.perculus_password')); | |
| $this->header = new SoapHeader("http://www.perculus.com/api/v2/$target", "AuthHeader", $auth, false); | |
| } | |
| /** | |
| * call the perculus api and return result | |
| * @param string $operation the operation to execute on perculus api eg: addRoom | |
| * @param array $body the body of the call eg ['roomId' => 0] | |
| * @return array an array containing the data of the classroom if created | |
| */ | |
| private function call($operation = '', $body = []) { | |
| try { | |
| $result = $this->client->__soapCall($operation, [ $operation => $body ], NULL, $this->header); | |
| return $result; | |
| } catch (SoapFault $e) { | |
| return false; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment