@@ -111,6 +111,7 @@ class Rest extends WebService
111
111
public const SAVE_SESSION = 'save_session ' ;
112
112
public const CREATE_SESSION_FROM_MODEL = 'create_session_from_model ' ;
113
113
public const UPDATE_SESSION = 'update_session ' ;
114
+ public const GET_SESSIONS = 'get_sessions ' ;
114
115
115
116
public const SUBSCRIBE_USER_TO_COURSE = 'subscribe_user_to_course ' ;
116
117
public const SUBSCRIBE_USER_TO_COURSE_PASSWORD = 'subscribe_user_to_course_password ' ;
@@ -120,6 +121,10 @@ class Rest extends WebService
120
121
public const ADD_COURSES_SESSION = 'add_courses_session ' ;
121
122
public const ADD_USERS_SESSION = 'add_users_session ' ;
122
123
public const SUBSCRIBE_USER_TO_SESSION_FROM_USERNAME = 'subscribe_user_to_session_from_username ' ;
124
+ public const SUBSCRIBE_USERS_TO_SESSION = 'subscribe_users_to_session ' ;
125
+ public const UNSUBSCRIBE_USERS_FROM_SESSION = 'unsubscribe_users_from_session ' ;
126
+ public const GET_USERS_SUBSCRIBED_TO_SESSION = 'get_users_subscribed_to_session ' ;
127
+
123
128
124
129
public const GET_COURSE_QUIZ_MDL_COMPAT = 'get_course_quiz_mdl_compat ' ;
125
130
@@ -1649,6 +1654,36 @@ public function getCoursesCampus($campusId = null): array
1649
1654
);
1650
1655
}
1651
1656
1657
+ /**
1658
+ * Returns a list of sessions in the given URL. If no URL is provided, we assume we are not in a multi-URL setup and
1659
+ * return all the sessions.
1660
+ *
1661
+ * @param int $campusId
1662
+ */
1663
+ public function getSessionsCampus (int $ campusId = null ): array
1664
+ {
1665
+ self ::protectAdminEndpoint ();
1666
+
1667
+ $ list = SessionManager::get_sessions_list (
1668
+ [],
1669
+ [],
1670
+ null ,
1671
+ null ,
1672
+ $ campusId
1673
+ );
1674
+ $ shortList = [];
1675
+ foreach ($ list as $ session ) {
1676
+ $ shortList [] = [
1677
+ 'id ' => $ session ['id ' ],
1678
+ 'name ' => $ session ['name ' ],
1679
+ 'access_start_date ' => $ session ['access_start_date ' ],
1680
+ 'access_end_date ' => $ session ['access_end_date ' ],
1681
+ ];
1682
+ }
1683
+
1684
+ return $ shortList ;
1685
+ }
1686
+
1652
1687
/**
1653
1688
* @throws Exception
1654
1689
*/
@@ -2148,9 +2183,21 @@ public function addCoursesSession(array $params): array
2148
2183
}
2149
2184
2150
2185
/**
2186
+ * Simple legacy shortcut to subscribeUsersToSession
2151
2187
* @throws Exception
2152
2188
*/
2153
2189
public function addUsersSession (array $ params ): array
2190
+ {
2191
+ return self ::subscribeUsersToSession ($ params );
2192
+ }
2193
+
2194
+ /**
2195
+ * Subscribe a list of users to the given session
2196
+ * @param array $params Containing 'id_session' and 'list_users' entries
2197
+ * @return array
2198
+ * @throws Exception
2199
+ */
2200
+ public function subscribeUsersToSession (array $ params ): array
2154
2201
{
2155
2202
$ sessionId = $ params ['id_session ' ];
2156
2203
$ userList = $ params ['list_users ' ];
@@ -2175,6 +2222,39 @@ public function addUsersSession(array $params): array
2175
2222
'message ' => get_lang ('UsersAdded ' ),
2176
2223
];
2177
2224
}
2225
+ /**
2226
+ * Unsubscribe a given list of users from the given session
2227
+ * @param array $params
2228
+ * @return array
2229
+ * @throws Exception
2230
+ */
2231
+ public function unsubscribeUsersFromSession (array $ params ): array
2232
+ {
2233
+ self ::protectAdminEndpoint ();
2234
+
2235
+ $ sessionId = $ params ['id_session ' ];
2236
+ $ userList = $ params ['list_users ' ];
2237
+
2238
+ if (!is_array ($ userList )) {
2239
+ $ userList = [];
2240
+ }
2241
+
2242
+ if (!api_is_platform_admin () && !in_array ($ this ->user ->getId (), $ userList )) {
2243
+ self ::throwNotAllowedException ();
2244
+ }
2245
+
2246
+ foreach ($ userList as $ userId ) {
2247
+ SessionManager::unsubscribe_user_from_session (
2248
+ $ sessionId ,
2249
+ $ userId
2250
+ );
2251
+ }
2252
+
2253
+ return [
2254
+ 'status ' => true ,
2255
+ 'message ' => get_lang ('UserUnsubscribed ' ),
2256
+ ];
2257
+ }
2178
2258
2179
2259
/**
2180
2260
* Creates a session from a model session.
@@ -2372,6 +2452,38 @@ public function getSessionFromExtraField($fieldName, $fieldValue)
2372
2452
return intval ($ sessionIdList [0 ]['item_id ' ]);
2373
2453
}
2374
2454
2455
+ /**
2456
+ * Get a list of users subscribed to the given session
2457
+ * @params int $sessionId
2458
+ * @params int $moveInfo Whether to return the "moved_*" fields or not
2459
+ * @return array
2460
+ */
2461
+ public function getUsersSubscribedToSession (int $ sessionId , int $ moveInfo = 0 ): array
2462
+ {
2463
+ self ::protectAdminEndpoint ();
2464
+
2465
+ $ users = SessionManager::get_users_by_session ($ sessionId );
2466
+
2467
+ $ userList = [];
2468
+ foreach ($ users as $ user ) {
2469
+ $ userInfo = [
2470
+ 'user_id ' => $ user ['user_id ' ],
2471
+ 'username ' => $ user ['username ' ],
2472
+ 'firstname ' => $ user ['firstname ' ],
2473
+ 'lastname ' => $ user ['lastname ' ],
2474
+ 'status ' => $ user ['relation_type ' ],
2475
+ ];
2476
+ if (1 === $ moveInfo ) {
2477
+ $ userInfo ['moved_to ' ] = $ user ['moved_to ' ];
2478
+ $ userInfo ['moved_status ' ] = $ user ['moved_status ' ];
2479
+ $ userInfo ['moved_at ' ] = $ user ['moved_at ' ];
2480
+ }
2481
+ $ userList [] = $ userInfo ;
2482
+ }
2483
+
2484
+ return $ userList ;
2485
+ }
2486
+
2375
2487
/**
2376
2488
* Updates a user identified by its login name.
2377
2489
*
0 commit comments