|
| 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 | +namespace core_ltix\local\lticore\message\payload; |
| 18 | + |
| 19 | +use core_ltix\constants; |
| 20 | +use core_ltix\helper; |
| 21 | +use core_ltix\local\lticore\facades\service\deep_linking_launch_service_facade; |
| 22 | +use core_ltix\local\lticore\message\payload\custom\custom_param_parser; |
| 23 | + |
| 24 | +/** |
| 25 | + * Generates payload data for a 1p1 deep linking launch (ContentItemSelectionRequest). |
| 26 | + * |
| 27 | + * @package core_ltix |
| 28 | + * @copyright 2025 Muhammad Arnaldo <muhammad.arnaldo@moodle.com> |
| 29 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 30 | + */ |
| 31 | +class v1p1_deep_linking_launch_payload_builder { |
| 32 | + /** |
| 33 | + * Constructor. |
| 34 | + * |
| 35 | + * @param \stdClass $toolconfig the tool configuration. |
| 36 | + * @param \stdClass $user the user data. |
| 37 | + * @param deep_linking_launch_service_facade $servicefacade the service facade. |
| 38 | + * @param custom_param_parser $customparamparser the custom parameter parser. |
| 39 | + * @param int $contextid the launch context ID. |
| 40 | + */ |
| 41 | + public function __construct( |
| 42 | + protected \stdClass $toolconfig, |
| 43 | + protected \stdClass $user, |
| 44 | + protected deep_linking_launch_service_facade $servicefacade, |
| 45 | + protected custom_param_parser $customparamparser, |
| 46 | + protected int $contextid, |
| 47 | + ) { |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get the array of all parameters for this request type. |
| 52 | + * |
| 53 | + * @return array |
| 54 | + */ |
| 55 | + public function get_params(): array { |
| 56 | + // Create basic unformatted payload data |
| 57 | + $unformattedpayloaddata = [ |
| 58 | + 'context' => $this->get_unformatted_context_data(), |
| 59 | + 'toolplatform' => $this->get_unformatted_tool_platform_data(), |
| 60 | + 'lis' => $this->get_unformatted_lis_data(), |
| 61 | + 'user' => $this->get_unformatted_user_data(), |
| 62 | + ]; |
| 63 | + $unformattedpayloaddata = array_merge(...array_values($unformattedpayloaddata)); |
| 64 | + |
| 65 | + // Add custom param data configured by the tool - do NOT substitute yet. |
| 66 | + $toolunformattedpayloaddata = $this->get_unformatted_custom_data($this->toolconfig); |
| 67 | + $unformattedpayloaddata = array_merge($toolunformattedpayloaddata, $unformattedpayloaddata); |
| 68 | + |
| 69 | + // Allow services to add claims, again using unformatted payload data. |
| 70 | + $serviceunformattedpayloaddata = $this->get_unformatted_service_custom_data($this->servicefacade); |
| 71 | + $unformattedpayloaddata = array_merge($unformattedpayloaddata, $serviceunformattedpayloaddata); |
| 72 | + |
| 73 | + // Perform substitution for custom params. |
| 74 | + return $this->resolve_substitution($unformattedpayloaddata, $this->customparamparser); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Resolve substitution for custom parameters. |
| 79 | + * |
| 80 | + * @param array $payloaddata the payload data. |
| 81 | + * @param custom_param_parser $customparamparser the custom parameter parser. |
| 82 | + * @return array the resolved payload data. |
| 83 | + */ |
| 84 | + protected function resolve_substitution( |
| 85 | + array $payloaddata, |
| 86 | + custom_param_parser $customparamparser |
| 87 | + ): array { |
| 88 | + foreach ($payloaddata as $key => $value) { |
| 89 | + // Substitution is only performed for custom params. |
| 90 | + if (str_starts_with($key, 'custom_')) { |
| 91 | + $payloaddata[$key] = $customparamparser->parse($value, $payloaddata); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return $payloaddata; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Get the unformatted custom data from tool configuration. |
| 100 | + * |
| 101 | + * @param \stdClass $toolconfig the tool configuration. |
| 102 | + * @return array the custom data. |
| 103 | + */ |
| 104 | + protected function get_unformatted_custom_data(\stdClass $toolconfig): array { |
| 105 | + $customdata = []; |
| 106 | + |
| 107 | + if (!empty($toolconfig->lti_customparameters)) { |
| 108 | + $toolcustom = helper::split_parameters($toolconfig->lti_customparameters); |
| 109 | + foreach ($toolcustom as $key => $val) { |
| 110 | + $key2 = helper::map_keyname($key); |
| 111 | + $customdata['custom_' . $key2] = $val; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return $customdata; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get unformatted context data. |
| 120 | + * |
| 121 | + * @return array|null the context data, or null if not applicable. |
| 122 | + */ |
| 123 | + protected function get_unformatted_context_data(): ?array { |
| 124 | + $context = \core\context::instance_by_id($this->contextid); |
| 125 | + |
| 126 | + if (($coursecontext = $context->get_course_context(false)) === false) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + $course = get_course($coursecontext->instanceid); |
| 131 | + $contexttype = $course->format == 'site' ? 'Group' |
| 132 | + : 'CourseSection'; |
| 133 | + |
| 134 | + return [ |
| 135 | + 'context_id' => $course->id, |
| 136 | + 'context_label' => $context->get_context_name(), |
| 137 | + 'context_title' => $context->get_context_name(), |
| 138 | + 'context_type' => $contexttype, |
| 139 | + ]; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Get unformatted tool platform data. |
| 144 | + * |
| 145 | + * @return array the tool platform data. |
| 146 | + */ |
| 147 | + protected function get_unformatted_tool_platform_data(): array { |
| 148 | + global $CFG; |
| 149 | + if (!empty($CFG->ltix_institution_name)) { |
| 150 | + $name = trim(html_to_text($CFG->ltix_institution_name, 0)); |
| 151 | + } else if (!empty($CFG->mod_lti_institution_name)) { |
| 152 | + // TODO final removal of the mod_lti_institution_name fallback code in Moodle 6.0. |
| 153 | + debugging('mod_lti_institution_name is deprecated. Please use ltix_institution_name instead.', DEBUG_DEVELOPER); |
| 154 | + $name = trim(html_to_text($CFG->mod_lti_institution_name, 0)); |
| 155 | + } else { |
| 156 | + $name = get_site()->shortname; |
| 157 | + } |
| 158 | + |
| 159 | + return [ |
| 160 | + 'tool_consumer_info_product_family_code' => 'moodle', |
| 161 | + 'tool_consumer_info_version' => strval($CFG->version), |
| 162 | + 'tool_consumer_instance_guid' => helper::get_organizationid((array)$this->toolconfig), |
| 163 | + 'tool_consumer_instance_name' => $name, |
| 164 | + 'tool_consumer_instance_description' => trim(html_to_text(get_site()->fullname, 0)), |
| 165 | + ]; |
| 166 | + } |
| 167 | + |
| 168 | + protected function get_unformatted_lis_data(): array { |
| 169 | + // Some lis properties only apply when in course-related contexts. |
| 170 | + $context = \core\context::instance_by_id($this->contextid); |
| 171 | + if (($coursecontext = $context->get_course_context(false)) !== false) { |
| 172 | + $course = get_course($coursecontext->instanceid); |
| 173 | + $coursesectionsourcedid = $course->idnumber; |
| 174 | + } |
| 175 | + |
| 176 | + return [ |
| 177 | + ...(isset($coursesectionsourcedid) ? ['lis_course_section_sourcedid' => $coursesectionsourcedid] : []), |
| 178 | + ]; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Get unformatted user data. |
| 183 | + * |
| 184 | + * @return array the user data. |
| 185 | + */ |
| 186 | + protected function get_unformatted_user_data(): array { |
| 187 | + $userpayloaddata = []; |
| 188 | + // lti1p1 DOES send user data in the initial payload; there is no OIDC auth step. |
| 189 | + if ($this->toolconfig->lti_sendname == constants::LTI_SETTING_ALWAYS) { |
| 190 | + $userpayloaddata['lis_person_name_given'] = $this->user->firstname; |
| 191 | + $userpayloaddata['lis_person_name_family'] = $this->user->lastname; |
| 192 | + $userpayloaddata['lis_person_name_full'] = fullname($this->user); |
| 193 | + $userpayloaddata['ext_user_username'] = $this->user->username; |
| 194 | + $userpayloaddata['lis_person_sourcedid'] = $this->user->idnumber; |
| 195 | + } |
| 196 | + |
| 197 | + if ($this->toolconfig->lti_sendemailaddr == constants::LTI_SETTING_ALWAYS) { |
| 198 | + $userpayloaddata['lis_person_contact_email_primary'] = $this->user->email; |
| 199 | + } |
| 200 | + |
| 201 | + return $userpayloaddata; |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Get unformatted service custom data. |
| 206 | + * |
| 207 | + * @param deep_linking_launch_service_facade $servicefacade the service facade. |
| 208 | + * @return array the custom data. |
| 209 | + */ |
| 210 | + protected function get_unformatted_service_custom_data(deep_linking_launch_service_facade $servicefacade): array { |
| 211 | + $servicecustomdata = []; |
| 212 | + foreach ($servicefacade->get_launch_parameters() as $param => $val) { |
| 213 | + $servicecustomdata['custom_' . $param] = $val; |
| 214 | + } |
| 215 | + return $servicecustomdata; |
| 216 | + } |
| 217 | +} |
0 commit comments