Skip to content

Commit ee59dbd

Browse files
committed
feat: add Event model
1 parent dd2fb53 commit ee59dbd

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/Model/Event.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/**
4+
*
5+
* Event class
6+
*
7+
* @copyright Apereo
8+
* @category OpenLRW
9+
* @package Entity
10+
* @author Xavier Chopin <[email protected]>
11+
* @license http://www.osedu.org/licenses/ECL-2.0 ECL-2.0 License
12+
*/
13+
14+
15+
namespace OpenLRW\Model;
16+
17+
use DateTime;
18+
use OpenLRW\OpenLRW;
19+
use OpenLRW\Exception\GenericException;
20+
21+
class Event extends Model
22+
{
23+
24+
private static function postCaliper($data)
25+
{
26+
$header = [
27+
'X-Requested-With' => 'XMLHttpRequest',
28+
'Content-Type' => 'application/json',
29+
'Authorization' => OpenLRW::getKey()
30+
];
31+
32+
return OpenLRW::guzzlePost('key/caliper', ['headers' => $header, 'body' => $data])->getStatusCode();
33+
}
34+
35+
36+
/**
37+
* Create and send Caliper Event
38+
*
39+
* @param $userId
40+
* @param $action
41+
* @param string $applicationName
42+
* @param string $description
43+
* @param string $groupId
44+
* @return string
45+
*/
46+
public static function caliperFactory($userId, $action, $description = '', $groupId = 'null', $applicationName = 'php-api-client')
47+
{
48+
$date = date_format(new DateTime('NOW'), 'Y-m-d\TH:i:s.755\Z');
49+
$eventId = sha1($userId . $date);
50+
$json = '
51+
{
52+
"data": [
53+
{
54+
"@context": "http://purl.imsglobal.org/ctx/caliper/v1p1",
55+
"@type": "Event",
56+
"action": "' . $action . '",
57+
"actor": {
58+
"@id": "' . $userId . '",
59+
"@type": "Person"
60+
},
61+
"eventTime": "' . $date . '",
62+
"object": {
63+
"@id": "' . $eventId . '",
64+
"@type": "SoftwareApplication",
65+
"name": "' . $applicationName . '",
66+
"description": "' . $description . '"
67+
},
68+
"group": {
69+
"@id": "' . $groupId . '",
70+
"@type": "Group"
71+
}
72+
}
73+
],
74+
"sendTime": "' . $date . '",
75+
"sensor": "http://localhost/web/php-api-client"
76+
}';
77+
78+
try {
79+
return self::postCaliper($json);
80+
} catch (\Exception $e) {
81+
throw new GenericException($e->getMessage());
82+
}
83+
}
84+
85+
86+
}

0 commit comments

Comments
 (0)