|
25 | 25 | namespace filter_opencast\local;
|
26 | 26 |
|
27 | 27 | use oauth_helper;
|
| 28 | +use tool_opencast\exception\opencast_api_response_exception; |
28 | 29 | use tool_opencast\local\settings_api;
|
29 | 30 | use tool_opencast\local\api;
|
30 | 31 | use moodle_exception;
|
@@ -149,11 +150,11 @@ public static function is_lti_credentials_configured(int $ocinstanceid) {
|
149 | 150 | * - ocinstanceid: The ID of the Opencast instance.
|
150 | 151 | * - consumerkey: The LTI consumer key for the instance.
|
151 | 152 | * - consumersecret: The LTI consumer secret for the instance.
|
152 |
| - * - baseurl: The API URL for the Opencast instance. |
| 153 | + * - baseurl: The API URL for the presentation node of Opencast instance. |
153 | 154 | */
|
154 | 155 | public static function get_lti_set_object(int $ocinstanceid) {
|
155 | 156 | $lticredentials = self::get_lti_credentials($ocinstanceid);
|
156 |
| - // get url of the engage.ui |
| 157 | + // Get url of the engage.ui. |
157 | 158 | $baseurl = self::get_engage_url($ocinstanceid);
|
158 | 159 |
|
159 | 160 | return (object) [
|
@@ -193,27 +194,75 @@ public static function get_filter_lti_launch_url(int $ocinstanceid, int $coursei
|
193 | 194 | }
|
194 | 195 |
|
195 | 196 | /**
|
196 |
| - * Calls the URL of the presention node of opencast. |
| 197 | + * Retrieves the Engage URL for a given Opencast instance. |
197 | 198 | *
|
198 |
| - * This function calls an api endpoint because the url of the engage.ui is different depending on |
199 |
| - * the installation (All-In-One or Multiple Servers). |
| 199 | + * This function attempts to fetch the Engage URL through multiple methods: |
| 200 | + * 1. Using the 'org.opencastproject.engage.ui.player.redirect' service. |
| 201 | + * 2. Falling back to the API URL if the first method fails. |
| 202 | + * 3. Attempting to retrieve the Engage UI URL as a secondary fallback by getting from getOrgEngageUIUrl method. |
200 | 203 | *
|
201 |
| - * @param int $ocinstanceid |
202 |
| - * @return string $url the url of the engage.ui of the opencast installation |
203 |
| - * @throws \moodle_exception |
| 204 | + * @param int $ocinstanceid The ID of the Opencast instance. |
| 205 | + * |
| 206 | + * @return string The Engage URL for the specified Opencast instance. |
| 207 | + * |
| 208 | + * @throws opencast_api_response_exception If there's an error in the API response. |
204 | 209 | */
|
205 | 210 | public static function get_engage_url(int $ocinstanceid) {
|
206 | 211 | $api = api::get_instance($ocinstanceid);
|
207 |
| - // Endpoint to call the engage.ui url of presentation node. |
208 |
| - // Make sure the api user has the rights to call that api endpoint. |
| 212 | + $response = $api->opencastapi->services->getServiceJSON('org.opencastproject.engage.ui.player.redirect'); |
| 213 | + $code = $response['code']; |
| 214 | + |
| 215 | + // Make sure everything goes fine. |
| 216 | + if ($code != 200 && $code != 404) { |
| 217 | + throw new opencast_api_response_exception($response); |
| 218 | + } |
| 219 | + |
| 220 | + $engageurl = null; |
| 221 | + |
| 222 | + // Get the services object from the get call. |
| 223 | + $servicesobj = $response['body']; |
| 224 | + // Check if the get call returns any services, if not we return the default oc instance api. |
| 225 | + if (property_exists($servicesobj, 'services') && property_exists($servicesobj->services, 'service') |
| 226 | + && !empty($servicesobj->services->service)) { |
| 227 | + // Parse the service object to array, which is easier to use! |
| 228 | + $engageservice = (array) $servicesobj->services->service; |
| 229 | + if (!empty($engageservice['host'])) { |
| 230 | + $engageurl = preg_replace(["/\/docs/"], [''], $engageservice['host']); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + // If we have a valid engage url, we return it. |
| 235 | + if (!empty($engageurl)) { |
| 236 | + return $engageurl; |
| 237 | + } |
| 238 | + |
| 239 | + // As a default fallback, we assume that the engage node url is the same as the api url. |
| 240 | + $engageurl = settings_api::get_apiurl($ocinstanceid); |
| 241 | + |
| 242 | + // Try to get the engage url from engage ui url once more, as secondary fallback method. |
209 | 243 | $response = $api->opencastapi->baseApi->getOrgEngageUIUrl();
|
210 |
| - if ($response['code'] != 200) { |
211 |
| - global $CFG; |
212 |
| - $supportemail = $CFG->supportemail; |
213 |
| - throw new \moodle_exception('no_engageurl_error', 'filter_opencast', '', $supportemail); |
214 |
| - } else { |
215 |
| - $engageui = $response['body']; |
216 |
| - return $engageui->{'org.opencastproject.engage.ui.url'}; |
| 244 | + $code = $response['code']; |
| 245 | + // If something went wrong, we throw opencast_api_response_exception exception. |
| 246 | + if ($code != 200) { |
| 247 | + throw new opencast_api_response_exception($response); |
217 | 248 | }
|
| 249 | + |
| 250 | + // Get the engage ui object from the get call. |
| 251 | + $engageuiobj = (array) $response['body']; |
| 252 | + |
| 253 | + // Check if we have a valid engage ui url. |
| 254 | + if (isset($engageuiobj['org.opencastproject.engage.ui.url'])) { |
| 255 | + $engageuiurl = $engageuiobj['org.opencastproject.engage.ui.url']; |
| 256 | + |
| 257 | + // Check if the engage ui url is not empty and not a localhost url. |
| 258 | + if (!empty($engageuiurl) && |
| 259 | + strpos($engageuiurl, 'http://') === false && |
| 260 | + strpos($engageuiurl, 'localhost') === false ) { |
| 261 | + $engageurl = $engageuiurl; |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + // Finally, we return it. |
| 266 | + return $engageurl; |
218 | 267 | }
|
219 | 268 | }
|
0 commit comments