Total Complexity | 56 |
Total Lines | 411 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ChamiloApi often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ChamiloApi, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ChamiloApi |
||
21 | { |
||
22 | public const COURSE_MANAGER = 1; |
||
23 | public const SESSION_ADMIN = 3; |
||
24 | public const DRH = 4; |
||
25 | public const STUDENT = 5; |
||
26 | public const ANONYMOUS = 6; |
||
27 | |||
28 | private static array $configuration; |
||
29 | |||
30 | public function __construct(array $configuration) |
||
31 | { |
||
32 | self::$configuration = $configuration; |
||
33 | } |
||
34 | |||
35 | public static function getConfigurationArray(): array |
||
36 | { |
||
37 | return self::$configuration; |
||
38 | } |
||
39 | |||
40 | public static function getConfigurationValue(string $variable): mixed |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Returns an array of resolutions that can be used for the conversion of documents to images. |
||
52 | */ |
||
53 | public static function getDocumentConversionSizes(): array |
||
66 | ]; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get the platform logo path. |
||
71 | * |
||
72 | * @throws Exception |
||
73 | */ |
||
74 | public static function getPlatformLogoPath( |
||
75 | string $theme = '', |
||
76 | bool $getSysPath = false, |
||
77 | bool $forcedGetter = false |
||
78 | ): ?string { |
||
79 | static $logoPath; |
||
80 | |||
81 | // If call from CLI it should be reloaded. |
||
82 | if ('cli' === PHP_SAPI) { |
||
83 | $logoPath = null; |
||
84 | } |
||
85 | |||
86 | if (!isset($logoPath) || $forcedGetter) { |
||
87 | $theme = empty($theme) ? api_get_visual_theme() : $theme; |
||
88 | $accessUrlId = api_get_current_access_url_id(); |
||
89 | if ('cli' === PHP_SAPI) { |
||
90 | $accessUrl = api_get_configuration_value('access_url'); |
||
91 | if (!empty($accessUrl)) { |
||
92 | $accessUrlId = $accessUrl; |
||
93 | } |
||
94 | } |
||
95 | $themeDir = Template::getThemeDir($theme); |
||
96 | $customLogoPath = $themeDir.sprintf('images/header-logo-custom%s.png', $accessUrlId); |
||
97 | |||
98 | $svgIcons = api_get_setting('icons_mode_svg'); |
||
99 | if ('true' === $svgIcons) { |
||
100 | $customLogoPathSVG = substr($customLogoPath, 0, -3).'svg'; |
||
101 | if (file_exists(api_get_path(SYS_PUBLIC_PATH).sprintf('css/%s', $customLogoPathSVG))) { |
||
102 | if ($getSysPath) { |
||
103 | return api_get_path(SYS_PUBLIC_PATH).sprintf('css/%s', $customLogoPathSVG); |
||
104 | } |
||
105 | |||
106 | return api_get_path(WEB_CSS_PATH).$customLogoPathSVG; |
||
107 | } |
||
108 | } |
||
109 | if (file_exists(api_get_path(SYS_PUBLIC_PATH).sprintf('css/%s', $customLogoPath))) { |
||
110 | if ($getSysPath) { |
||
111 | return api_get_path(SYS_PUBLIC_PATH).sprintf('css/%s', $customLogoPath); |
||
112 | } |
||
113 | |||
114 | return api_get_path(WEB_CSS_PATH).$customLogoPath; |
||
115 | } |
||
116 | |||
117 | $originalLogoPath = $themeDir.'images/header-logo.png'; |
||
118 | if ('true' === $svgIcons) { |
||
119 | $originalLogoPathSVG = $themeDir.'images/header-logo.svg'; |
||
120 | if (file_exists(api_get_path(SYS_CSS_PATH).$originalLogoPathSVG)) { |
||
121 | if ($getSysPath) { |
||
122 | return api_get_path(SYS_CSS_PATH).$originalLogoPathSVG; |
||
123 | } |
||
124 | |||
125 | return api_get_path(WEB_CSS_PATH).$originalLogoPathSVG; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | if (file_exists(api_get_path(SYS_CSS_PATH).$originalLogoPath)) { |
||
130 | if ($getSysPath) { |
||
131 | return api_get_path(SYS_CSS_PATH).$originalLogoPath; |
||
132 | } |
||
133 | |||
134 | return api_get_path(WEB_CSS_PATH).$originalLogoPath; |
||
135 | } |
||
136 | $logoPath = ''; |
||
137 | } |
||
138 | |||
139 | return $logoPath; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Get the platform logo. |
||
144 | * Return a <img> if the logo image exists. |
||
145 | * Otherwise, return a <h2> with the institution name. |
||
146 | * |
||
147 | * @throws Exception |
||
148 | */ |
||
149 | public static function getPlatformLogo( |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Like strip_tags(), but leaves an additional space and removes only the given tags. |
||
196 | * |
||
197 | * @param array $tags Tags to be removed |
||
198 | * |
||
199 | * @return string The original string without the given tags |
||
200 | */ |
||
201 | public static function stripGivenTags(string $string, array $tags): string |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Adds or Subtract a time in hh:mm:ss to a datetime. |
||
215 | * |
||
216 | * @param string $time Time to add or substract in hh:mm:ss format |
||
217 | * @param string $datetime Datetime to be modified as accepted by the Datetime class constructor |
||
218 | * @param bool $operation True for Add, False to Subtract |
||
219 | * |
||
220 | * @throws Exception |
||
221 | */ |
||
222 | public static function addOrSubTimeToDateTime( |
||
223 | string $time, |
||
224 | string $datetime = 'now', |
||
225 | bool $operation = true |
||
226 | ): string { |
||
227 | $date = new DateTime($datetime); |
||
228 | $hours = 0; |
||
229 | $minutes = 0; |
||
230 | $seconds = 0; |
||
231 | sscanf($time, '%d:%d:%d', $hours, $minutes, $seconds); |
||
232 | $timeSeconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes; |
||
233 | if ($operation) { |
||
234 | $date->add(new DateInterval('PT'.$timeSeconds.'S')); |
||
235 | } else { |
||
236 | $date->sub(new DateInterval('PT'.$timeSeconds.'S')); |
||
237 | } |
||
238 | |||
239 | return $date->format('Y-m-d H:i:s'); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Returns the course id (integer) for the given course directory or the current ID if no directory is defined. |
||
244 | * |
||
245 | * @param string|null $directory The course directory/path that appears in the URL |
||
246 | * |
||
247 | * @throws Exception |
||
248 | */ |
||
249 | public static function getCourseIdByDirectory(?string $directory = null): int |
||
250 | { |
||
251 | if (!empty($directory)) { |
||
252 | $directory = Database::escape_string($directory); |
||
253 | $row = Database::select( |
||
254 | 'id', |
||
255 | Database::get_main_table(TABLE_MAIN_COURSE), |
||
256 | [ |
||
257 | 'where' => [ |
||
258 | 'directory = ?' => [$directory], |
||
259 | ], |
||
260 | ], |
||
261 | 'first' |
||
262 | ); |
||
263 | |||
264 | if (\is_array($row) && isset($row['id'])) { |
||
265 | return $row['id']; |
||
266 | } |
||
267 | |||
268 | return 0; |
||
269 | } |
||
270 | |||
271 | return (int) Session::read('_real_cid', 0); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Check if the current HTTP request is by AJAX. |
||
276 | */ |
||
277 | public static function isAjaxRequest(): bool |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Get a variable name for language file from a text. |
||
286 | */ |
||
287 | public static function getLanguageVar(string $text, string $prefix = ''): string |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Get the stylesheet path for HTML blocks created with CKEditor. |
||
300 | */ |
||
301 | public static function getEditorBlockStylePath(): string |
||
302 | { |
||
303 | $visualTheme = api_get_visual_theme(); |
||
304 | |||
305 | $cssFile = api_get_path(SYS_CSS_PATH).sprintf('themes/%s/editor_content.css', $visualTheme); |
||
306 | |||
307 | if (is_file($cssFile)) { |
||
308 | return api_get_path(WEB_CSS_PATH).sprintf('themes/%s/editor_content.css', $visualTheme); |
||
309 | } |
||
310 | |||
311 | return api_get_path(WEB_CSS_PATH).'editor_content.css'; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Get a list of colors from the palette at main/palette/pchart/default.color |
||
316 | * and return it as an array of strings. |
||
317 | * |
||
318 | * @param bool $decimalOpacity Whether to return the opacity as 0..100 or 0..1 |
||
319 | * @param bool $wrapInRGBA Whether to return it as 1,1,1,100 or rgba(1,1,1,100) |
||
320 | * @param int|null $fillUpTo If the number of colors is smaller than this number, generate more colors |
||
321 | * |
||
322 | * @return array An array of string colors |
||
323 | */ |
||
324 | public static function getColorPalette( |
||
325 | bool $decimalOpacity = false, |
||
326 | bool $wrapInRGBA = false, |
||
327 | ?int $fillUpTo = null |
||
328 | ): array { |
||
329 | // Get the common colors from the palette used for pchart |
||
330 | $paletteFile = api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color'; |
||
331 | $palette = file($paletteFile); |
||
332 | if ($decimalOpacity) { |
||
333 | // Because the pchart palette has transparency as integer values |
||
334 | // (0..100) and chartjs uses percentage (0.0..1.0), we need to divide |
||
335 | // the last value by 100, which is a bit overboard for just one chart |
||
336 | foreach ($palette as $index => $color) { |
||
337 | $components = explode(',', trim($color)); |
||
338 | $components[3] = round((int) $components[3] / 100, 1); |
||
339 | $palette[$index] = implode(',', $components); |
||
340 | } |
||
341 | } |
||
342 | if ($wrapInRGBA) { |
||
343 | foreach ($palette as $index => $color) { |
||
344 | $color = trim($color); |
||
345 | $palette[$index] = 'rgba('.$color.')'; |
||
346 | } |
||
347 | } |
||
348 | // If we want more colors, loop through existing colors |
||
349 | $count = \count($palette); |
||
350 | if (isset($fillUpTo) && $fillUpTo > $count) { |
||
351 | for ($i = $count; $i < $fillUpTo; $i++) { |
||
352 | $palette[$i] = $palette[$i % $count]; |
||
353 | } |
||
354 | } |
||
355 | |||
356 | return $palette; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Get the local time for the midnight. |
||
361 | * |
||
362 | * @param null|string $utcTime Optional. The time to ve converted. |
||
363 | * See api_get_local_time. |
||
364 | * |
||
365 | * @throws Exception |
||
366 | */ |
||
367 | public static function getServerMidnightTime(?string $utcTime = null): DateTime |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Get JavaScript code necessary to load quiz markers-rolls in medialement's Markers Rolls plugin. |
||
380 | */ |
||
381 | public static function getQuizMarkersRollsJS(): string |
||
402 | + $.param({ |
||
403 | exerciseId: qMarkerRoll[1], |
||
404 | learnpath_id: 0, |
||
405 | learnpath_item_id: 0, |
||
406 | learnpath_item_view_id: 0 |
||
407 | }); |
||
408 | |||
409 | instance.options.markersRolls[qMarkerRoll[0]] = url; |
||
410 | }); |
||
411 | |||
412 | instance.buildmarkersrolls(instance, instance.controls, instance.layers, instance.media); |
||
413 | "; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Performs a redirection to the specified URL. |
||
418 | * |
||
419 | * This method sends a direct HTTP Location header to the client, |
||
420 | * causing the browser to navigate to the specified URL. It should be |
||
421 | * used with caution and only in scenarios where Symfony's standard |
||
422 | * response handling is not applicable. The method terminates script |
||
423 | * execution after sending the header. |
||
424 | */ |
||
425 | public static function redirectTo(string $url): void |
||
434 |