moodle-local_adonis/classes/adonis.php

159 lines
4.6 KiB
PHP
Raw Normal View History

2022-11-27 11:52:19 +00:00
<?php
namespace local_adonis;
2022-11-28 07:14:22 +00:00
require_once("exceptions.php");
require_once("crew.php");
2022-11-28 07:11:31 +00:00
2022-11-27 11:52:19 +00:00
class Adonis {
public $base_url, $username, $password, $lifetime;
public static $USER_AGENT = "AdonisPHP/dev (https://kumi.systems)";
public static $CREW_PORTAL = 0;
public static $INTEGRATION = 1;
public static $INTEGRATION_PATH = "/AIWS/AdonisIntegrationWebService.svc/";
public static $CREW_PORTAL_PATH = "/AdonisWebServices/CrewPortalWebService.svc/";
function __construct($base_url, $username, $password, $lifetime = 30) {
$this->base_url = $base_url;
$this->username = $username;
$this->password = $password;
$this->lifetime = $lifetime;
}
function request($endpoint, $payload, $api = null, $add_token = true, $wrap_request = true, $extract_response = true) {
if ($api == null) {
$api = $this::$CREW_PORTAL;
}
$service_url = ($api == $this::$CREW_PORTAL) ? $this::$CREW_PORTAL_PATH : $this::$INTEGRATION_PATH;
$headers = array();
$headers[] = "User-Agent: " . $this::$USER_AGENT;
$headers[] = "Content-Type: application/json";
$headers[] = "Accept: application/json";
if ($add_token == true) {
$headers[] = "Authentication_Token: " . $this->getToken($api);
}
if ($wrap_request == true) {
$payload = array("request" => $payload);
}
$req_data = json_encode($payload);
$req = curl_init($this->base_url . $service_url . $endpoint);
curl_setopt($req, CURLOPT_HTTPHEADER, $headers);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_POSTFIELDS, $req_data);
$res_raw = curl_exec($req);
curl_close($req);
$res = json_decode($res_raw);
if ($extract_response == true) {
$res = $res[$endpoint . "Result"];
}
if ($res["Authentication_Approved"] != true) {
throw new AdonisAuthenticationDeniedException();
}
if ($res["ErrorText"]) {
throw new AdonisException($res["ErrorText"]);
}
}
function getToken($api) {
$data = array("credentials" => array(
"Login" => $this->username,
"Password" => $this->password,
"Lifetime" => $this->lifetime
));
$res = $this->request("GNL_API_AUTHENTICATION", $data, $api, false, false, true);
return $res["Authentication_Token"];
}
function getCrewPersonalData($pin) {
$data = array("Pin" => $pin);
$res = $this->request("DG_GeneralDetailsRead", $data, $this::$CREW_PORTAL);
return $res;
}
function getCrewProfilePicture($pin) {
$data = array("Pin" => $pin);
try {
$res = $this->request("DG_ProfilePictureRead", $data, $this::$CREW_PORTAL);
} catch (AdonisAuthenticationDeniedException $e) {
throw $e;
} catch (AdonisException $e) {
return;
}
return $res["FileBase64"];
}
function getCrew($pin, $personal_data = True, $profile_picture = True) {
$crew = new Crew();
$crew->pin = $pin;
if ($personal_data) {
$crew->personal_data = $this->getCrewPersonalData($pin);
}
if ($profile_picture) {
$crew->profile_picture = $this->getCrewProfilePicture($pin);
}
return $crew;
}
function getCrewListView($view, $filters = "", $pagination = "None", $paginate_by = "", $page = "") {
$data = array(
"View" => $view,
"Filter" => $filters,
"Pagination" => $pagination,
"RowsByPage" => $paginate_by,
"Page" => $page
);
try {
$res = $this->request("GNL_APMCrewListViews", $data, $this::$INTEGRATION);
} catch (AdonisAuthenticationDeniedException $e) {
throw $e;
} catch (AdonisException $e) {
return;
}
return json_decode($res);
}
function getRequirements($self, $view, $paginate_by = 100) {
$requirements = array();
$page_num = 1;
while (true) {
$page = $this->getCrewListView($view, $pagination = "True", $paginate_by = (string) $paginate_by, $page = (string) $page_num);
foreach ($page["Result"] as $record) {
$requirements[] = array($record["PIN"], $record["Document code"]);
}
if ($page["CurrentPage"] == $page["TotalPages"]) {
break;
}
$page_num += 1;
}
return $requirements;
}
}