Skip to content

Commit 09fb91d

Browse files
committed
Webservice: Add update_session_from_extra_field WS - refs BT#22302
1 parent 806f347 commit 09fb91d

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

main/inc/lib/webservices/Rest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class Rest extends WebService
156156
public const GET_AUDIT_ITEMS = 'get_audit_items';
157157
public const SUBSCRIBE_COURSE_TO_SESSION_FROM_EXTRA_FIELD = 'subscribe_course_to_session_from_extra_field';
158158
public const SUBSCRIBE_USER_TO_SESSION_FROM_EXTRA_FIELD = 'subscribe_user_to_session_from_extra_field';
159+
public const UPDATE_SESSION_FROM_EXTRA_FIELD = 'update_session_from_extra_field';
159160

160161
/**
161162
* @var Session
@@ -4563,7 +4564,72 @@ public function subscribeUserToSessionFromExtraField($params)
45634564
];
45644565
}
45654566
}
4567+
/**
4568+
* Update a specific session, identified via extra field value.
4569+
*
4570+
* This method:
4571+
* - Locates the session ID using the provided extra field name/value via ExtraFieldValue('session').
4572+
* - Calls updateSession() with the located ID and provided update parameters (e.g., name, coach_username, dates).
4573+
*
4574+
* Required parameters:
4575+
* - field_name: Name of the extra field for sessions (e.g., 'peoplesoft_sid').
4576+
* - field_value: Value of the session extra field (e.g., PeopleSoft ID).
4577+
* - Optional update fields: name, coach_username, access_start_date, access_end_date, etc.
4578+
*
4579+
* @param array $params Associative array of POST parameters.
4580+
* @return array Response in format: ['error' => bool, 'data' => array] on success, or ['error' => true, 'message' => string] on failure.
4581+
*/
4582+
public function updateSessionFromExtraField($params)
4583+
{
4584+
// Validate required parameters (redundant with v2.php but for safety)
4585+
$required = ['field_name', 'field_value'];
4586+
foreach ($required as $key) {
4587+
if (empty($params[$key])) {
4588+
return [
4589+
'error' => true,
4590+
'message' => 'Missing required parameter: ' . $key
4591+
];
4592+
}
4593+
}
4594+
4595+
$fieldName = $params['field_name'];
4596+
$fieldValue = $params['field_value'];
45664597

4598+
// Get session ID from extra field value using ExtraFieldValue model
4599+
$sessionValueModel = new ExtraFieldValue('session');
4600+
$sessionIdList = $sessionValueModel->get_item_id_from_field_variable_and_field_value(
4601+
$fieldName,
4602+
$fieldValue,
4603+
false,
4604+
false,
4605+
true
4606+
);
4607+
if (empty($sessionIdList)) {
4608+
return [
4609+
'error' => true,
4610+
'message' => 'No session found with extra field value "' . $fieldValue . '".'
4611+
];
4612+
}
4613+
$sessionId = (int) $sessionIdList[0]['item_id']; // Extract item_id from sub-array, assume single match
4614+
4615+
// Prepare params for updateSession() by adding the located ID
4616+
$params['id_session'] = $sessionId;
4617+
4618+
// Get coach ID if we got it as username
4619+
if (!empty($params['coach_username'])) {
4620+
$param['id_coach'] = UserManager::get_user_id_from_username($params['coach_username']);
4621+
}
4622+
// Delegate to existing updateSession() method (mirrors its logic)
4623+
$result = $this->updateSession($params);
4624+
4625+
// Override message and include ID in data if successful
4626+
if (!$result['error']) {
4627+
$result['data']['id_session'] = $sessionId;
4628+
$result['data']['message'] = 'Session updated';
4629+
}
4630+
4631+
return $result;
4632+
}
45674633
/**
45684634
* Generate an API key for webservices access for the given user ID.
45694635
*/

main/webservices/api/v2.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,40 @@
11581158
$restResponse->setData($result['data']);
11591159
}
11601160
break;
1161+
/**
1162+
* Update a session using extra field value for identification.
1163+
*
1164+
* Validates parameters from $_POST and calls the Rest method, handling response via $restResponse.
1165+
*
1166+
* Required POST parameters:
1167+
* - api_key: API key for authentication.
1168+
* - username: Username for authentication.
1169+
* - field_name: Name of the extra field for sessions.
1170+
* - field_value: Value of the session extra field.
1171+
* - Optional: name, coach_username, access_start_date, access_end_date, etc.
1172+
*
1173+
* @return void Sets response via existing $restResponse object.
1174+
*/
1175+
case Rest::UPDATE_SESSION_FROM_EXTRA_FIELD:
1176+
$required_params = ['api_key', 'username', 'field_name', 'field_value'];
1177+
$missing = [];
1178+
foreach ($required_params as $param) {
1179+
if (empty($_POST[$param])) {
1180+
$missing[] = $param;
1181+
}
1182+
}
1183+
if (!empty($missing)) {
1184+
$restResponse->setErrorMessage('Missing required parameters: ' . implode(', ', $missing));
1185+
break;
1186+
}
1187+
$params = $_POST;
1188+
$result = $restApi->updateSessionFromExtraField($params);
1189+
if ($result['error']) {
1190+
$restResponse->setErrorMessage($result['message']);
1191+
} else {
1192+
$restResponse->setData($result['data']);
1193+
}
1194+
break;
11611195
default:
11621196
throw new Exception(get_lang('InvalidAction'));
11631197
}

0 commit comments

Comments
 (0)