diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f11bcf..9e8db6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,3 +31,4 @@ - Support for OC 16 more in the area of either eliminating the use of search endpoint or making it (backward) compatible. - Upgrade behat test! - Use the latest moodle ci workflows! +- Swtich from opencast services endpoint to api base endpoint (#8) diff --git a/classes/local/opencast_manager.php b/classes/local/opencast_manager.php index 951799d..daf01c6 100644 --- a/classes/local/opencast_manager.php +++ b/classes/local/opencast_manager.php @@ -101,7 +101,7 @@ public static function get_course_videos($courseid) { */ public static function get_episode_tracks($identifier) { // Get tool_opencast api instance for search service. - $api = self::get_opencast_search_service_api_instance(); + $api = self::get_opencast_presentation_node_api_instance(); // Prepare params for search. $params = [ @@ -183,48 +183,67 @@ public static function get_episode_tracks($identifier) { /** - * Get api instance from tool_opencast for search service. + * Retrieves the tool_opencast API instance or the url for the Opencast presentation node. * - * @param boolean $returnbaseurl whether to return only the baseurl or the api object back + * @param bool $returnbaseurl If true, the function returns the base URL of the presentation node API. + * If false, the function returns the tool_opencast API instance. + * Default value is false. * - * @return tool_opencast\local\api opencast api instance. + * @return tool_opencast\local\api The tool_opencast API instance for the Opencast presentation node or the base URL, + * depending on the value of $returnbaseurl. + * + * @throws opencast_api_response_exception If there is an error with the API request. */ - public static function get_opencast_search_service_api_instance($returnbaseurl = false) { + public static function get_opencast_presentation_node_api_instance($returnbaseurl = false) { + // We are working with default opencast instance for now. + $defaultocinstanceid = settings_api::get_default_ocinstance()->id; + // Get api instance from tool_opencast. - $api = api::get_instance(); + $api = api::get_instance($defaultocinstanceid); - // Get the search service data. - $response = $api->opencastapi->services->getServiceJSON('org.opencastproject.search'); - $code = $response['code']; + // We get teh admin node url first, in order to use it as fallback. + $adminnodeurl = settings_api::get_apiurl($defaultocinstanceid); - // Make sure everything goes fine. + // Try to get the engage ui url, which represents the presentation node url. + $response = $api->opencastapi->baseApi->getOrgEngageUIUrl(); + $code = $response['code']; + // If something went wrong, we throw opencast_api_response_exception exception. if ($code != 200 && $code != 404) { throw new opencast_api_response_exception($response); } - // If it could not find the search service, we return the the api instance. + // If it could not find the search service, we return the the api instance or the url if requested. if ($code == 404) { - return $api; + return $returnbaseurl ? $adminnodeurl : $api; } - // Get the services object from the get call. - $servicesobj = $response['body']; - // Check if the get call returns any services, if not we return the default oc instance api. - if (!property_exists($servicesobj, 'services') || !property_exists($servicesobj->services, 'service') - || empty($servicesobj->services->service)) { - return $api; - } + // Fallback to the default url. + $engageurl = $adminnodeurl; - // Parse the service object to array, which is easier to use! - $searchservice = (array) $servicesobj->services->service; + // Get the engage ui object from the get call. + $engageuiobj = (array) $response['body']; - // Check if the search service is active and online to make calls. - if (!empty($searchservice) && $searchservice['active'] && $searchservice['online']) { - // We are working with default opencast instance for now. - $defaultocinstanceid = settings_api::get_default_ocinstance()->id; - // Initialize the custom configs with the search service's host. + // Check if we have a valid engage ui url. + if (isset($engageuiobj['org.opencastproject.engage.ui.url'])) { + $engageuiurl = $engageuiobj['org.opencastproject.engage.ui.url']; + + // Check if the engage ui url is valid. + // We validate the scenario where the url of engage ui (presentation) has to have https + // and not be a localhost, unless the admin node is on http://localhost that determines that it is a local environment. + $islocal = strpos($adminnodeurl, 'localhost') !== false; + $isvalid = $islocal ? true + : (strpos($engageuiurl, 'http://') === false && strpos($engageuiurl, 'localhost') === false); + + if (!empty($engageuiurl) && $isvalid ) { + $engageurl = $engageuiurl; + } + } + + // In case the multi node server is identified. + if ($engageurl != $adminnodeurl) { + // Initialize the custom configs with the presentation node's host. $customconfigs = [ - 'apiurl' => preg_replace(["/\/docs/"], [''], $searchservice['host']), + 'apiurl' => $engageurl, 'apiusername' => settings_api::get_apiusername($defaultocinstanceid), 'apipassword' => settings_api::get_apipassword($defaultocinstanceid), 'apitimeout' => settings_api::get_apitimeout($defaultocinstanceid), @@ -233,9 +252,10 @@ public static function get_opencast_search_service_api_instance($returnbaseurl = // Create the tool_opencast api instance with search service's host url. $api = api::get_instance(null, [], $customconfigs); } + // If only the baseurl is needed. if ($returnbaseurl) { - return preg_replace(["/\/docs/"], [''], $searchservice['host']); + return $engageurl; } // Finally, we return the tool_opencast api instance to make search calls. return $api; @@ -259,14 +279,14 @@ public static function get_lti_params($courseid) { $defaultocinstanceid = settings_api::get_default_ocinstance()->id; $mainltiendpoint = settings_api::get_apiurl($defaultocinstanceid); // Generate lti params for the main oc instance. - $params['main'] = self::generate_lti_params($defaultocinstanceid, $courseid, $mainltiendpoint); - // Get the endpoint url of the search node instance. - $searchnodeltiendpoint = self::get_opencast_search_service_api_instance(true); + $params['admin'] = self::generate_lti_params($defaultocinstanceid, $courseid, $mainltiendpoint); + // Get the endpoint url of the presentation node instance. + $presentationnodeltiendpoint = self::get_opencast_presentation_node_api_instance(true); // Check if the opencast uses different nodes. - if ($mainltiendpoint != $searchnodeltiendpoint) { + if ($mainltiendpoint != $presentationnodeltiendpoint) { // Generate lti params for the search node. - $params['search'] = self::generate_lti_params($defaultocinstanceid, $courseid, $searchnodeltiendpoint); + $params['presentation'] = self::generate_lti_params($defaultocinstanceid, $courseid, $presentationnodeltiendpoint); } return $params; diff --git a/lib/js/H5PRunLTI.js b/lib/js/H5PRunLTI.js index 18456f7..d40a19c 100644 --- a/lib/js/H5PRunLTI.js +++ b/lib/js/H5PRunLTI.js @@ -4,16 +4,16 @@ function runLti() { var opencastLtiParams = opencastLTIParamsAjaxCallSync(); - var ltiParams = opencastLtiParams?.main ?? null; + var ltiParams = opencastLtiParams?.admin ?? null; // Does not perform the LTI, in case the ltiParams is not configured in the admin or there is some error! if (ltiParams == null || !ltiParams) { return; } performLti('ltiLaunchForm', ltiParams); - var searchNodeLtiParams = opencastLtiParams?.search ?? null; - if (searchNodeLtiParams) { - performLti('searchNodeLtiLaunchForm', searchNodeLtiParams); + var presentationNodeLtiParams = opencastLtiParams?.presentation ?? null; + if (presentationNodeLtiParams) { + performLti('presentationNodeLtiLaunchForm', presentationNodeLtiParams); } } diff --git a/tests/behat/local_och5pcore_add_video.feature b/tests/behat/local_och5pcore_add_video.feature index f8884dd..4a23fcd 100644 --- a/tests/behat/local_och5pcore_add_video.feature +++ b/tests/behat/local_och5pcore_add_video.feature @@ -34,10 +34,10 @@ Feature: Add Opencast Video into H5P Core | Available themes to extend | Boost | And I press "Save changes" Then I should see "Changes saved" - And the following config values are set as admin: - | unaddableblocks | | theme_boost| And I am on site homepage And I turn editing mode on + And the following config values are set as admin: + | unaddableblocks | | theme_boost| And I add the "Navigation" block if not present And I configure the "Navigation" block And I set the following fields to these values: