|
36 | 36 | */ |
37 | 37 | class core_renderer extends \theme_boost\output\core_renderer |
38 | 38 | { |
| 39 | + |
| 40 | + /** |
| 41 | + * Returns the data context for the global theme header, fetching from API. |
| 42 | + * This method is called by {{# output.get_global_header_data }} in Mustache. |
| 43 | + * |
| 44 | + * @return \stdClass An object containing data for the header. |
| 45 | + */ |
| 46 | + public function get_global_header_data() { |
| 47 | + global $PAGE; // Keep $PAGE just in case for future use/context, though not strictly needed for this API call |
| 48 | + global $DB, $USER; // Declare $DB and $USER as global |
| 49 | + global $SESSION; |
| 50 | + |
| 51 | + $context = new \stdClass(); |
| 52 | + $context->customnavigation = []; // Default to empty array in case of API issues |
| 53 | + |
| 54 | + // Fetch token for current user |
| 55 | + $token = null; // Initialize to null |
| 56 | + $tokenNew = null; // Initialize to null |
| 57 | + $accesstoken = null; // Initialize to null |
| 58 | + $response_content = false; // Initialize to false |
| 59 | + |
| 60 | + // --- NEW: Get the configured .NET application base URL --- |
| 61 | + $dotnet_base_url = get_config('theme_nhse', 'dotnet_base_url'); |
| 62 | + |
| 63 | + if (!empty($dotnet_base_url) && substr($dotnet_base_url, -1) !== '/') { |
| 64 | + $dotnet_base_url .= '/'; |
| 65 | + } |
| 66 | + |
| 67 | + // --- NEW: Add dotnet_base_url to the context for Mustache template --- |
| 68 | + $context->dotnet_base_url = $dotnet_base_url; |
| 69 | + |
| 70 | + // --- NEW: Get the configured API Base URL --- |
| 71 | + $api_base_url = get_config('theme_nhse', 'api_base_url'); |
| 72 | + // Ensure the API base URL ends with a slash if it's not empty |
| 73 | + if (!empty($api_base_url) && substr($api_base_url, -1) !== '/') { |
| 74 | + $api_base_url .= '/'; |
| 75 | + } |
| 76 | + |
| 77 | + if (empty($api_base_url)) { |
| 78 | + // Log an error and return early if the API URL is not configured |
| 79 | + error_log("theme_nhse: ERROR: LH OpenAPI Base URL is not configured in theme settings. Cannot fetch navigation data."); |
| 80 | + return $context; // Return empty context if API URL is missing |
| 81 | + } |
| 82 | + |
| 83 | + // --- NEW: Add login status to the context --- |
| 84 | + // isloggedin() is a Moodle core function that returns true if a user is logged in. |
| 85 | + $context->is_user_logged_in = isloggedin(); |
| 86 | + |
| 87 | + // --- NEW: Add 'Site Administration' link if user is an admin --- |
| 88 | + // Check if the user has the capability to configure the site (i.e., is an admin) |
| 89 | + if (has_capability('moodle/site:config', \context_system::instance())) { |
| 90 | + $admin_link = new \stdClass(); |
| 91 | + $admin_link->title = "Site administration"; |
| 92 | + $admin_link->url = new \moodle_url('/admin/search.php'); // Use the observed URL |
| 93 | + $admin_link->hasnotification = false; |
| 94 | + $admin_link->notificationcount = 0; |
| 95 | + $admin_link->openInNewTab = false; // Or true, if you prefer it opens in a new tab |
| 96 | + |
| 97 | + // Add this admin link to your customnavigation array |
| 98 | + // This ensures it gets rendered by your {{#customnavigation}} block in Mustache |
| 99 | + $context->customnavigation[] = $admin_link; |
| 100 | + error_log("theme_nhse: Added Site Administration link to custom navigation."); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + if (isloggedin()) { // Only try to fetch token if a user is logged in |
| 105 | + $token = $DB->get_record('auth_oidc_token', ['username' => $USER->username]); |
| 106 | + if ($token) { |
| 107 | + error_log("theme_nhse: Found OIDC token for user {$USER->username}."); |
| 108 | + // You would then use $token->accesstoken to construct your bearer token |
| 109 | + $accesstoken = $token->token; |
| 110 | + error_log("theme_nhse: accesstoken {$accesstoken}"); |
| 111 | + |
| 112 | + } else { |
| 113 | + error_log("theme_nhse: No OIDC token found for user {$USER->username}."); |
| 114 | + } |
| 115 | + } else { |
| 116 | + error_log("theme_nhse: User not logged in, skipping OIDC token fetch."); |
| 117 | + } |
| 118 | + |
| 119 | + // --- The rest of your existing API call logic --- |
| 120 | + $api_endpoint_path = 'User/GetLHUserNavigation'; |
| 121 | + $url = $api_base_url . $api_endpoint_path; |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + try |
| 126 | + { |
| 127 | + $curl = new \curl(); |
| 128 | + // Set Authorization header |
| 129 | + $options = [ |
| 130 | + 'HTTPHEADER' => [ |
| 131 | + 'Authorization: Bearer ' . $accesstoken, |
| 132 | + 'Accept: application/json' |
| 133 | + ] |
| 134 | + ]; |
| 135 | + $response = $curl->get($url, null, $options); |
| 136 | + $result = json_decode($response, true); |
| 137 | + // Log the raw response and the decoded result |
| 138 | + error_log("theme_nhse: API Response (raw): " . $response); |
| 139 | + error_log("theme_nhse: API Response (decoded): " . print_r($result, true)); |
| 140 | + // Check for JSON decoding errors |
| 141 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 142 | + error_log("theme_nhse: JSON decoding error: " . json_last_error_msg()); |
| 143 | + } |
| 144 | + } catch (Exception $e) |
| 145 | + { |
| 146 | + debugging('CURL error: ' . $e->getMessage(), DEBUG_DEVELOPER); |
| 147 | + error_log("theme_nhse: CURL Exception caught for URL: " . $url . " Message: " . $e->getMessage()); |
| 148 | + } |
| 149 | + |
| 150 | + // --- Conditional Block for Processing Response (after try-catch) --- |
| 151 | + // We use $result here, which will be null if there was an API error or JSON decoding issue. |
| 152 | + if (is_array($result) && !empty($result)) { |
| 153 | + // JSON decoded successfully and is an array with content |
| 154 | + error_log("theme_nhse: JSON decoded successfully. Processing " . count($result) . " items."); |
| 155 | + $processed_links = []; |
| 156 | + foreach ($result as $item) { |
| 157 | + // Check 'visible' property from API (only include if true or not set) |
| 158 | + // Assuming 'visible' being absent or true means it should be shown |
| 159 | + |
| 160 | + // --- NEW: Skip item if title is "Sign Out" --- |
| 161 | + // We use trim() to handle any potential leading/trailing whitespace. |
| 162 | + if (isset($item['title']) && trim($item['title']) === 'Sign Out') { |
| 163 | + error_log("theme_nhse: Skipping 'Sign Out' link from API response."); |
| 164 | + continue; // Skip to the next item in the loop |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | + if (!isset($item['visible']) || $item['visible'] === true) { |
| 169 | + $processed_item = new \stdClass(); |
| 170 | + $processed_item->title = $item['title'] ?? 'Untitled'; // Default title if missing |
| 171 | + // Handle URLs - convert to moodle_url if internal, keep as string if external |
| 172 | + if (isset($item['url']) && !empty($item['url'])) { |
| 173 | + $item_url_path = $item['url']; // Store the raw URL from the API |
| 174 | + |
| 175 | + // Check if it's an absolute URL (starts with http/s or //) |
| 176 | + if (strpos($item_url_path, 'http') === 0 || strpos($item_url_path, '//') === 0) { |
| 177 | + $processed_item->url = $item_url_path; // Use the absolute URL as is |
| 178 | + error_log("theme_nhse: Processing absolute URL: {$processed_item->url}"); |
| 179 | + } |
| 180 | + // If it's a relative URL AND we have a configured .NET base URL, prepend it |
| 181 | + else if (!empty($dotnet_base_url)) { |
| 182 | + // Prepend .NET base URL, ensuring no double slashes by trimming leading slash from item_url_path |
| 183 | + $processed_item->url = $dotnet_base_url . ltrim($item_url_path, '/'); |
| 184 | + error_log("theme_nhse: Redirecting relative link '{$item_url_path}' to .NET domain: {$processed_item->url}"); |
| 185 | + } |
| 186 | + // Fallback: If it's a relative URL but no .NET base URL is configured, |
| 187 | + // treat it as a Moodle internal URL. |
| 188 | + else { |
| 189 | + $processed_item->url = new \moodle_url($item_url_path); |
| 190 | + error_log("theme_nhse: .NET base URL not configured, processing relative link '{$item_url_path}' as Moodle internal."); |
| 191 | + } |
| 192 | + } else { |
| 193 | + // Default to Moodle home if URL is missing or empty |
| 194 | + $processed_item->url = new \moodle_url('/'); |
| 195 | + error_log("theme_nhse: Item has empty or missing URL, defaulting to Moodle home."); |
| 196 | + } |
| 197 | + $processed_item->hasnotification = $item['hasNotification'] ?? false; |
| 198 | + $processed_item->notificationcount = $item['notificationCount'] ?? 0; |
| 199 | + $processed_item->openInNewTab = $item['openInNewTab'] ?? false; |
| 200 | + |
| 201 | + $processed_links[] = $processed_item; |
| 202 | + } else { |
| 203 | + // Log items that are not visible |
| 204 | + error_log("theme_nhse: Item not visible and filtered out: " . ($item['title'] ?? 'N/A') . " (visible: " . ($item['visible'] ? 'true' : 'false') . ")"); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + // Add API processed links AFTER the static admin link, so it appears first if you want. |
| 209 | + // If you want admin link to appear last, change this to array_unshift or prepend it. |
| 210 | + $context->customnavigation = array_merge($context->customnavigation, $processed_links); |
| 211 | + //$context->customnavigation = array_unshift($context->customnavigation, $processed_links); |
| 212 | + |
| 213 | + error_log("theme_nhse: Processed links for display: " . print_r($context->customnavigation, true)); |
| 214 | + |
| 215 | + |
| 216 | + // Log the final processed links (for debugging) |
| 217 | + error_log("theme_nhse: Processed links for display: " . print_r($processed_links, true)); |
| 218 | + } else { |
| 219 | + // This block handles cases where: |
| 220 | + // 1. $result is null (due to JSON decoding error or CURL exception) |
| 221 | + // 2. $result is not an array (e.g., API returned a non-array JSON like a string or object) |
| 222 | + // 3. $result is an empty array |
| 223 | + $json_error_msg = (json_last_error() !== JSON_ERROR_NONE) ? json_last_error_msg() : 'N/A'; |
| 224 | + error_log("theme_nhse: Failed to process API response. Response was empty, not an array, or an error occurred. JSON Error: " . $json_error_msg); |
| 225 | + error_log("theme_nhse: Raw API response (if available): " . ($response ?: 'No response content')); |
| 226 | + } |
| 227 | + |
| 228 | + return $context; |
| 229 | + } |
| 230 | + |
| 231 | + |
39 | 232 | /** |
40 | 233 | * Wrapper for header elements. |
41 | 234 | * |
|
0 commit comments