|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +/** |
| 18 | + * LTI helper class for filter opencast. |
| 19 | + * @package filter_opencast |
| 20 | + * @copyright 2024 Farbod Zamani Boroujeni, ELAN e.V. |
| 21 | + * @author Farbod Zamani Boroujeni <[email protected]> |
| 22 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 23 | + */ |
| 24 | + |
| 25 | +namespace filter_opencast\local; |
| 26 | + |
| 27 | +use oauth_helper; |
| 28 | +use tool_opencast\local\settings_api; |
| 29 | + |
| 30 | +/** |
| 31 | + * LTI helper class for filter opencast. |
| 32 | + * @package filter_opencast |
| 33 | + * @copyright 2024 Farbod Zamani Boroujeni, ELAN e.V. |
| 34 | + * @author Farbod Zamani Boroujeni <[email protected]> |
| 35 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 36 | + */ |
| 37 | +class lti_helper { |
| 38 | + |
| 39 | + /** */ |
| 40 | + const LTI_LAUNCH_PATH = '/filter/opencast/ltilaunch.php'; |
| 41 | + |
| 42 | + /** |
| 43 | + * Create necessary lti parameters. |
| 44 | + * @param string $consumerkey LTI consumer key. |
| 45 | + * @param string $consumersecret LTI consumer secret. |
| 46 | + * @param string $endpoint of the opencast instance. |
| 47 | + * @param string $customtool the custom tool |
| 48 | + * @param int $courseid the course id to add into the parameters |
| 49 | + * |
| 50 | + * @return array lti parameters |
| 51 | + */ |
| 52 | + public static function create_lti_parameters($consumerkey, $consumersecret, $endpoint, $customtool, $courseid) { |
| 53 | + global $CFG, $USER; |
| 54 | + |
| 55 | + $course = get_course($courseid); |
| 56 | + |
| 57 | + require_once($CFG->dirroot . '/mod/lti/locallib.php'); |
| 58 | + require_once($CFG->dirroot . '/lib/oauthlib.php'); |
| 59 | + |
| 60 | + $helper = new oauth_helper(['oauth_consumer_key' => $consumerkey, |
| 61 | + 'oauth_consumer_secret' => $consumersecret, ]); |
| 62 | + |
| 63 | + // Set all necessary parameters. |
| 64 | + $params = []; |
| 65 | + $params['oauth_version'] = '1.0'; |
| 66 | + $params['oauth_nonce'] = $helper->get_nonce(); |
| 67 | + $params['oauth_timestamp'] = $helper->get_timestamp(); |
| 68 | + $params['oauth_consumer_key'] = $consumerkey; |
| 69 | + |
| 70 | + $params['context_id'] = $course->id; |
| 71 | + $params['context_label'] = trim($course->shortname); |
| 72 | + $params['context_title'] = trim($course->fullname); |
| 73 | + $params['resource_link_id'] = 'o' . random_int(1000, 9999) . '-' . random_int(1000, 9999); |
| 74 | + $params['resource_link_title'] = 'Opencast'; |
| 75 | + $params['context_type'] = ($course->format == 'site') ? 'Group' : 'CourseSection'; |
| 76 | + $params['launch_presentation_locale'] = current_language(); |
| 77 | + $params['ext_lms'] = 'moodle-2'; |
| 78 | + $params['tool_consumer_info_product_family_code'] = 'moodle'; |
| 79 | + $params['tool_consumer_info_version'] = strval($CFG->version); |
| 80 | + $params['oauth_callback'] = 'about:blank'; |
| 81 | + $params['lti_version'] = 'LTI-1p0'; |
| 82 | + $params['lti_message_type'] = 'basic-lti-launch-request'; |
| 83 | + $urlparts = parse_url($CFG->wwwroot); |
| 84 | + $params['tool_consumer_instance_guid'] = $urlparts['host']; |
| 85 | + $params['custom_tool'] = urlencode($customtool); |
| 86 | + |
| 87 | + // User data. |
| 88 | + $params['user_id'] = $USER->id; |
| 89 | + $params['lis_person_name_given'] = $USER->firstname; |
| 90 | + $params['lis_person_name_family'] = $USER->lastname; |
| 91 | + $params['lis_person_name_full'] = $USER->firstname . ' ' . $USER->lastname; |
| 92 | + $params['ext_user_username'] = $USER->username; |
| 93 | + $params['lis_person_contact_email_primary'] = $USER->email; |
| 94 | + $params['roles'] = lti_get_ims_role($USER, null, $course->id, false); |
| 95 | + |
| 96 | + if (!empty($CFG->mod_lti_institution_name)) { |
| 97 | + $params['tool_consumer_instance_name'] = trim(html_to_text($CFG->mod_lti_institution_name, 0)); |
| 98 | + } else { |
| 99 | + $params['tool_consumer_instance_name'] = get_site()->shortname; |
| 100 | + } |
| 101 | + |
| 102 | + $params['launch_presentation_document_target'] = 'iframe'; |
| 103 | + $params['oauth_signature_method'] = 'HMAC-SHA1'; |
| 104 | + $params['oauth_signature'] = $helper->sign("POST", $endpoint, $params, $consumersecret . '&'); |
| 105 | + return $params; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Retrieves the LTI consumer key and consumer secret for a given Opencast instance ID. |
| 110 | + * |
| 111 | + * @param int $ocinstanceid The ID of the Opencast instance. |
| 112 | + * |
| 113 | + * @return array An associative array containing the 'consumerkey' and 'consumersecret' for the given Opencast instance. |
| 114 | + * If the credentials are not found, an empty array is returned. |
| 115 | + */ |
| 116 | + public static function get_lti_credentials(int $ocinstanceid) { |
| 117 | + $lticonsumerkey = settings_api::get_lticonsumerkey($ocinstanceid); |
| 118 | + $lticonsumersecret = settings_api::get_lticonsumersecret($ocinstanceid); |
| 119 | + return ['consumerkey' => $lticonsumerkey, 'consumersecret' => $lticonsumersecret]; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Checks if LTI credentials are configured for a given Opencast instance. |
| 124 | + * |
| 125 | + * This function verifies whether both the LTI consumer key and consumer secret |
| 126 | + * are set for the specified Opencast instance. |
| 127 | + * |
| 128 | + * @param int $ocinstanceid The ID of the Opencast instance to check. |
| 129 | + * |
| 130 | + * @return bool Returns true if both LTI consumer key and secret are configured, |
| 131 | + * false otherwise. |
| 132 | + */ |
| 133 | + public static function is_lti_credentials_configured(int $ocinstanceid) { |
| 134 | + $lticredentials = self::get_lti_credentials($ocinstanceid); |
| 135 | + return !empty($lticredentials['consumerkey']) && !empty($lticredentials['consumersecret']); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Retrieves an object containing LTI settings for a given Opencast instance. |
| 140 | + * |
| 141 | + * This function gathers the LTI credentials and API URL for the specified Opencast instance |
| 142 | + * and returns them as a structured object. |
| 143 | + * |
| 144 | + * @param int $ocinstanceid The ID of the Opencast instance for which to retrieve LTI settings. |
| 145 | + * |
| 146 | + * @return object An object containing the following properties: |
| 147 | + * - ocinstanceid: The ID of the Opencast instance. |
| 148 | + * - consumerkey: The LTI consumer key for the instance. |
| 149 | + * - consumersecret: The LTI consumer secret for the instance. |
| 150 | + * - baseurl: The API URL for the Opencast instance. |
| 151 | + */ |
| 152 | + public static function get_lti_set_object(int $ocinstanceid) { |
| 153 | + $lticredentials = self::get_lti_credentials($ocinstanceid); |
| 154 | + $baseurl = settings_api::get_apiurl($ocinstanceid); |
| 155 | + |
| 156 | + return (object) [ |
| 157 | + 'ocinstanceid' => $ocinstanceid, |
| 158 | + 'consumerkey' => $lticredentials['consumerkey'], |
| 159 | + 'consumersecret' => $lticredentials['consumersecret'], |
| 160 | + 'baseurl' => $baseurl, |
| 161 | + ]; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Generates the LTI launch URL for the Opencast filter. |
| 166 | + * |
| 167 | + * This function creates a URL for launching LTI content specific to the Opencast filter, |
| 168 | + * incorporating necessary parameters such as course ID, Opencast instance ID, and episode ID. |
| 169 | + * |
| 170 | + * @param int $ocinstanceid The ID of the Opencast instance. |
| 171 | + * @param int $courseid The ID of the course. |
| 172 | + * @param string $episodeid The ID of the Opencast episode. |
| 173 | + * @param bool $output Optional. If true, returns the URL as a string. If false, returns a moodle_url object. Default is true. |
| 174 | + * |
| 175 | + * @return string|moodle_url If $output is true, returns the LTI launch URL as a string. |
| 176 | + * If $output is false, returns a moodle_url object representing the LTI launch URL. |
| 177 | + */ |
| 178 | + public static function get_filter_lti_launch_url(int $ocinstanceid, int $courseid, string $episodeid, bool $output = true) { |
| 179 | + $params = [ |
| 180 | + 'courseid' => $courseid, |
| 181 | + 'ocinstanceid' => $ocinstanceid, |
| 182 | + 'episodeid' => $episodeid, |
| 183 | + 'sesskey' => sesskey(), |
| 184 | + ]; |
| 185 | + $ltilaunchurl = new \moodle_url(self::LTI_LAUNCH_PATH, $params); |
| 186 | + if ($output) { |
| 187 | + return $ltilaunchurl->out(false); |
| 188 | + } |
| 189 | + return $ltilaunchurl; |
| 190 | + } |
| 191 | +} |
0 commit comments