@@ -111,6 +111,7 @@ class Rest extends WebService
111111 public const SAVE_SESSION = 'save_session ' ;
112112 public const CREATE_SESSION_FROM_MODEL = 'create_session_from_model ' ;
113113 public const UPDATE_SESSION = 'update_session ' ;
114+ public const GET_SESSIONS = 'get_sessions ' ;
114115
115116 public const SUBSCRIBE_USER_TO_COURSE = 'subscribe_user_to_course ' ;
116117 public const SUBSCRIBE_USER_TO_COURSE_PASSWORD = 'subscribe_user_to_course_password ' ;
@@ -120,6 +121,10 @@ class Rest extends WebService
120121 public const ADD_COURSES_SESSION = 'add_courses_session ' ;
121122 public const ADD_USERS_SESSION = 'add_users_session ' ;
122123 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+
123128
124129 public const GET_COURSE_QUIZ_MDL_COMPAT = 'get_course_quiz_mdl_compat ' ;
125130
@@ -1649,6 +1654,36 @@ public function getCoursesCampus($campusId = null): array
16491654 );
16501655 }
16511656
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+
16521687 /**
16531688 * @throws Exception
16541689 */
@@ -2148,9 +2183,21 @@ public function addCoursesSession(array $params): array
21482183 }
21492184
21502185 /**
2186+ * Simple legacy shortcut to subscribeUsersToSession
21512187 * @throws Exception
21522188 */
21532189 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
21542201 {
21552202 $ sessionId = $ params ['id_session ' ];
21562203 $ userList = $ params ['list_users ' ];
@@ -2175,6 +2222,39 @@ public function addUsersSession(array $params): array
21752222 'message ' => get_lang ('UsersAdded ' ),
21762223 ];
21772224 }
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+ }
21782258
21792259 /**
21802260 * Creates a session from a model session.
@@ -2372,6 +2452,38 @@ public function getSessionFromExtraField($fieldName, $fieldValue)
23722452 return intval ($ sessionIdList [0 ]['item_id ' ]);
23732453 }
23742454
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+
23752487 /**
23762488 * Updates a user identified by its login name.
23772489 *
0 commit comments