Skip to content

Commit bc2e5fd

Browse files
committed
Webservice: Add get_session_info_from_extra_field WS - refs BT#22302
1 parent be0ccea commit bc2e5fd

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

main/inc/lib/webservices/Rest.php

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ class Rest extends WebService
109109
public const GET_COURSES_FROM_EXTRA_FIELD = 'get_courses_from_extra_field';
110110
public const SAVE_COURSE = 'save_course';
111111
public const DELETE_COURSE = 'delete_course';
112-
113112
public const GET_SESSION_FROM_EXTRA_FIELD = 'get_session_from_extra_field';
113+
public const GET_SESSION_INFO_FROM_EXTRA_FIELD = 'get_session_info_from_extra_field';
114114
public const SAVE_SESSION = 'save_session';
115115
public const CREATE_SESSION_FROM_MODEL = 'create_session_from_model';
116116
public const UPDATE_SESSION = 'update_session';
@@ -2514,18 +2514,18 @@ public function subscribeUserToSessionFromUsername(int $sessionId, string $login
25142514
}
25152515

25162516
/**
2517-
* finds the session which has a specific value in a specific extra field.
2517+
* Finds the session which has a specific value in a specific extra field and return its ID (only that)
25182518
*
2519-
* @param $fieldName
2520-
* @param $fieldValue
2519+
* @param string $fieldName
2520+
* @param string $fieldValue
25212521
*
2522+
* @return int The matching session id, or an array with details about the session
25222523
* @throws Exception when no session matched or more than one session matched
25232524
*
2524-
* @return int, the matching session id
25252525
*/
2526-
public function getSessionFromExtraField($fieldName, $fieldValue)
2526+
public function getSessionFromExtraField(string $fieldName, string $fieldValue)
25272527
{
2528-
// find sessions that that have value in field
2528+
// find sessions that have that value in the given field
25292529
$valueModel = new ExtraFieldValue('session');
25302530
$sessionIdList = $valueModel->get_item_id_from_field_variable_and_field_value(
25312531
$fieldName,
@@ -2549,6 +2549,54 @@ public function getSessionFromExtraField($fieldName, $fieldValue)
25492549
return intval($sessionIdList[0]['item_id']);
25502550
}
25512551

2552+
/**
2553+
* Finds the session which has a specific value in a specific extra field and return its details
2554+
*
2555+
* @param string $fieldName
2556+
* @param string $fieldValue
2557+
*
2558+
* @return array The matching session id, or an array with details about the session
2559+
* @throws Exception when no session matched or more than one session matched
2560+
*
2561+
*/
2562+
public function getSessionInfoFromExtraField(string $fieldName, string $fieldValue): array
2563+
{
2564+
$session = [];
2565+
// find sessions that have that value in the given field
2566+
$valueModel = new ExtraFieldValue('session');
2567+
$sessionIdList = $valueModel->get_item_id_from_field_variable_and_field_value(
2568+
$fieldName,
2569+
$fieldValue,
2570+
false,
2571+
false,
2572+
true
2573+
);
2574+
2575+
// throw if none found
2576+
if (empty($sessionIdList)) {
2577+
throw new Exception(get_lang('NoSessionMatched'));
2578+
}
2579+
2580+
// throw if more than one found
2581+
if (count($sessionIdList) > 1) {
2582+
throw new Exception(get_lang('MoreThanOneSessionMatched'));
2583+
}
2584+
2585+
$session = api_get_session_info($sessionIdList[0]['item_id']);
2586+
$bundle = [
2587+
'id' => $session['id'],
2588+
'name' => $session['name'],
2589+
'access_start_date' => $session['access_start_date'],
2590+
'access_end_date' => $session['access_end_date'],
2591+
];
2592+
$extraFieldValues = new ExtraFieldValue('session');
2593+
$extraFields = $extraFieldValues->getAllValuesByItem($session['id']);
2594+
$bundle['extra_fields'] = $extraFields;
2595+
2596+
// return session details, including extra fields that have filter=1
2597+
return $bundle;
2598+
}
2599+
25522600
/**
25532601
* Get a list of users subscribed to the given session.
25542602
*

main/webservices/api/v2.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,18 @@
751751
);
752752
$restResponse->setData([$idSession]);
753753
break;
754+
case Rest::GET_SESSION_INFO_FROM_EXTRA_FIELD:
755+
if (empty($_POST['field_name']) || empty($_POST['field_value'])) {
756+
throw new Exception(get_lang('NoData'));
757+
}
758+
$idSession = $restApi->getSessionInfoFromExtraField($_POST['field_name'], $_POST['field_value']);
759+
Event::addEvent(
760+
LOG_WS.$action,
761+
'extra_field_name-extra_field_value',
762+
Database::escape_string($_POST['field_name']).':'.Database::escape_string($_POST['field_value'])
763+
);
764+
$restResponse->setData([$idSession]);
765+
break;
754766
case Rest::SAVE_SESSION:
755767
$data = $restApi->addSession($_POST);
756768
Event::addEvent(LOG_WS.$action, 'session_id', $data['id_session']);

0 commit comments

Comments
 (0)