Skip to content

Commit 16022a8

Browse files
committed
make sure lti launch gets a proper base url in both allinone and multi-nodes opencast instances
1 parent 88e674a commit 16022a8

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

classes/local/lti_helper.php

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace filter_opencast\local;
2626

2727
use oauth_helper;
28+
use tool_opencast\exception\opencast_api_response_exception;
2829
use tool_opencast\local\settings_api;
2930
use tool_opencast\local\api;
3031
use moodle_exception;
@@ -149,11 +150,11 @@ public static function is_lti_credentials_configured(int $ocinstanceid) {
149150
* - ocinstanceid: The ID of the Opencast instance.
150151
* - consumerkey: The LTI consumer key for the instance.
151152
* - 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.
153154
*/
154155
public static function get_lti_set_object(int $ocinstanceid) {
155156
$lticredentials = self::get_lti_credentials($ocinstanceid);
156-
// get url of the engage.ui
157+
// Get url of the engage.ui.
157158
$baseurl = self::get_engage_url($ocinstanceid);
158159

159160
return (object) [
@@ -193,27 +194,75 @@ public static function get_filter_lti_launch_url(int $ocinstanceid, int $coursei
193194
}
194195

195196
/**
196-
* Calls the URL of the presention node of opencast.
197+
* Retrieves the Engage URL for a given Opencast instance.
197198
*
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.
200203
*
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.
204209
*/
205210
public static function get_engage_url(int $ocinstanceid) {
206211
$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.
209243
$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);
217248
}
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;
218267
}
219268
}

lang/en/filter_opencast.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,3 @@
3535
$string['setting_uselti_desc'] = 'When enabled, Opencast videos are delivered through LTI authentication using the <strong>default Opencast video player</strong>. This is typically used alongside Secure Static Files in Opencast for enhanced security.';
3636
$string['setting_uselti_nolti_desc'] = 'To enable LTI Authentication for Opencast, you must configure the required credentials (Consumer Key and Consumer Secret) for this instance. Please do so via this link: {$a}';
3737
$string['setting_uselti_ocinstance_name'] = 'Opencast API {$a} Instance';
38-
$string['no_engageurl_error'] = 'Opencast API call for Engage URL not successful. Please contact your support {$a}.';

0 commit comments

Comments
 (0)