Skip to content

Commit 386d8cd

Browse files
Webservice: Add rest get_audit_items - refs BT#21206
1 parent e4818a8 commit 386d8cd

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

main/inc/lib/events.lib.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,4 +2818,71 @@ public static function logUserSubscribedInCourseSession(int $subscribedId, int $
28182818
$sessionId
28192819
);
28202820
}
2821+
2822+
/**
2823+
* Retrieves audit items from the track_e_default table.
2824+
*
2825+
* This function fetches audit data based on various optional criteria and
2826+
* formats the result to remove the "default_" prefix from each field.
2827+
*
2828+
*/
2829+
public static function getAuditItems(
2830+
string $defaultEventType,
2831+
?int $cId = null,
2832+
?int $sessionId = null,
2833+
?string $afterDate = null,
2834+
?string $beforeDate = null,
2835+
?int $userId = null,
2836+
int $offset = 0,
2837+
int $limit = 100
2838+
): array
2839+
{
2840+
$tblTrackEDefault = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
2841+
2842+
$whereConditions = ['default_event_type = ? ' => $defaultEventType];
2843+
2844+
if ($cId !== null) {
2845+
$whereConditions[' AND c_id = ? '] = $cId;
2846+
}
2847+
if ($sessionId !== null) {
2848+
$whereConditions[' AND session_id = ? '] = $sessionId;
2849+
}
2850+
if ($afterDate !== null) {
2851+
$whereConditions[' AND default_date >= ? '] = $afterDate;
2852+
}
2853+
if ($beforeDate !== null) {
2854+
$whereConditions[' AND default_date <= ? '] = $beforeDate;
2855+
}
2856+
if ($userId !== null) {
2857+
$whereConditions[' AND default_user_id = ? '] = $userId;
2858+
}
2859+
2860+
$conditions = [
2861+
'where' => $whereConditions,
2862+
'order' => 'default_date DESC',
2863+
'limit' => "$offset, $limit",
2864+
];
2865+
2866+
$results = Database::select(
2867+
'default_user_id, c_id, default_date, default_event_type, default_value_type, default_value, session_id',
2868+
$tblTrackEDefault,
2869+
$conditions,
2870+
'all',
2871+
'ASSOC',
2872+
true
2873+
);
2874+
2875+
$formattedResults = [];
2876+
foreach ($results as $result) {
2877+
$formattedResult = [];
2878+
foreach ($result as $key => $value) {
2879+
$newKey = str_replace('default_', '', $key);
2880+
$formattedResult[$newKey] = $value;
2881+
}
2882+
$formattedResults[] = $formattedResult;
2883+
}
2884+
2885+
return $formattedResults;
2886+
}
2887+
28212888
}

main/inc/lib/webservices/Rest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class Rest extends WebService
149149
public const DELETE_GROUP_SUB_USER = 'delete_group_sub_user';
150150
public const DELETE_GROUP_SUB_COURSE = 'delete_group_sub_course';
151151
public const DELETE_GROUP_SUB_SESSION = 'delete_group_sub_session';
152+
public const GET_AUDIT_ITEMS = 'get_audit_items';
152153

153154
/**
154155
* @var Session
@@ -4209,4 +4210,33 @@ private function generateUrl(array $additionalParams = []): string
42094210
return api_get_self().'?'
42104211
.http_build_query(array_merge($queryParams, $additionalParams));
42114212
}
4213+
4214+
/**
4215+
* Get audit items from track_e_default.
4216+
*/
4217+
public function getAuditItems(
4218+
string $defaultEventType,
4219+
?int $cId = null,
4220+
?int $sessionId = null,
4221+
?string $afterDate = null,
4222+
?string $beforeDate = null,
4223+
?int $userId = null,
4224+
int $offset = 0,
4225+
int $limit = 100
4226+
): array {
4227+
self::protectAdminEndpoint();
4228+
4229+
$auditItems = Event::getAuditItems(
4230+
$defaultEventType,
4231+
$cId,
4232+
$sessionId,
4233+
$afterDate,
4234+
$beforeDate,
4235+
$userId,
4236+
$offset,
4237+
$limit
4238+
);
4239+
4240+
return $auditItems;
4241+
}
42124242
}

main/webservices/api/v2.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,26 @@
963963
Event::addEvent(LOG_WS.$action, 'group_id', $groupId);
964964
$restResponse->setData($data);
965965
break;
966+
case Rest::GET_AUDIT_ITEMS:
967+
$defaultEventType = $httpRequest->request->get('default_event_type');
968+
969+
$cId = $httpRequest->request->has('c_id') ? $httpRequest->request->getInt('c_id') : null;
970+
$sessionId = $httpRequest->request->has('session_id') ? $httpRequest->request->getInt('session_id') : null;
971+
$userId = $httpRequest->request->has('user_id') ? $httpRequest->request->getInt('user_id') : null;
972+
973+
$afterDate = $httpRequest->request->get('after_date');
974+
$beforeDate = $httpRequest->request->get('before_date');
975+
$offset = $httpRequest->request->getInt('offset', 0);
976+
$limit = $httpRequest->request->getInt('limit', 100);
977+
978+
if (empty($defaultEventType)) {
979+
throw new Exception('default_event_type is required');
980+
}
981+
982+
$data = $restApi->getAuditItems($defaultEventType, $cId, $sessionId, $afterDate, $beforeDate, $userId, $offset, $limit);
983+
Event::addEvent(LOG_WS.$action, 'success', 'true');
984+
$restResponse->setData($data);
985+
break;
966986
default:
967987
throw new Exception(get_lang('InvalidAction'));
968988
}

0 commit comments

Comments
 (0)