moodle-local_adonis/classes/adonis.php

163 lines
4.6 KiB
PHP

<?php
namespace local_adonis;
require_once("exceptions.php");
require_once("crew.php");
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) {
$payload["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);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
$res_raw = curl_exec($req);
curl_close($req);
$res = json_decode($res_raw, true);
if ($extract_response == true) {
$res = $res[$endpoint . "Result"];
}
if ($res["Authentication_Approved"] != 1) {
throw new AdonisAuthenticationDeniedException();
}
if ($res["ErrorText"]) {
throw new AdonisException($res["ErrorText"]);
}
return $res;
}
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 $res;
}
function getRequirements($view, $paginate_by = 100) {
$requirements = array();
$page_num = 1;
while (true) {
$page = $this->getCrewListView($view, "", "True", (string) $paginate_by, (string) $page_num);
foreach ($page["Result"] as $record) {
$requirements[] = $record;
}
if ($page["CurrentPage"] == $page["TotalPages"]) {
break;
}
$page_num += 1;
}
return $requirements;
}
}