Total Complexity | 57 |
Total Lines | 435 |
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 |
||
13 | class ChamiloApi |
||
14 | { |
||
15 | private static $configuration; |
||
16 | |||
17 | /** |
||
18 | * ChamiloApi constructor. |
||
19 | * |
||
20 | * @param $configuration |
||
21 | */ |
||
22 | public function __construct(array $configuration) |
||
23 | { |
||
24 | self::$configuration = $configuration; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @return array |
||
29 | */ |
||
30 | public static function getConfigurationArray() |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param string $variable |
||
37 | * |
||
38 | * @return bool|string |
||
39 | */ |
||
40 | public static function getConfigurationValue($variable) |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Returns an array of resolutions that can be used for the conversion of documents to images. |
||
52 | * |
||
53 | * @return array |
||
54 | */ |
||
55 | public static function getDocumentConversionSizes() |
||
68 | ]; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Get the platform logo path. |
||
73 | * |
||
74 | * @param string $theme |
||
75 | * @param bool $getSysPath |
||
76 | * @param bool $forcedGetter |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | public static function getPlatformLogoPath($theme = '', $getSysPath = false, $forcedGetter = false) |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get the platform logo. |
||
161 | * Return a <img> if the logo image exists. Otherwise return a <h2> with the institution name. |
||
162 | * |
||
163 | * @param string $theme |
||
164 | * @param array $imageAttributes optional |
||
165 | * @param bool $getSysPath |
||
166 | * @param bool $forcedGetter |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | public static function getPlatformLogo( |
||
171 | $theme = '', |
||
172 | $imageAttributes = [], |
||
173 | $getSysPath = false, |
||
174 | $forcedGetter = false |
||
175 | ) { |
||
176 | $logoPath = self::getPlatformLogoPath($theme, $getSysPath, $forcedGetter); |
||
177 | $institution = api_get_setting('Institution'); |
||
178 | $institutionUrl = api_get_setting('InstitutionUrl'); |
||
179 | $siteName = api_get_setting('siteName'); |
||
180 | |||
181 | $homeURL = api_get_path(WEB_PATH).'index.php'; |
||
182 | |||
183 | $replaceHomeURL = api_get_configuration_value('platform_logo_url'); |
||
184 | if (!empty($replaceHomeURL)) { |
||
185 | $homeURL = $replaceHomeURL; |
||
186 | } |
||
187 | |||
188 | if ($logoPath === null) { |
||
189 | $headerLogo = \Display::url($siteName, $homeURL); |
||
190 | |||
191 | if (!empty($institutionUrl) && !empty($institution)) { |
||
192 | $headerLogo .= ' - '.\Display::url($institution, $institutionUrl); |
||
193 | } |
||
194 | |||
195 | $courseInfo = api_get_course_info(); |
||
196 | if (isset($courseInfo['extLink']) && !empty($courseInfo['extLink']['name'])) { |
||
197 | $headerLogo .= '<span class="extLinkSeparator"> - </span>'; |
||
198 | if (!empty($courseInfo['extLink']['url'])) { |
||
199 | $headerLogo .= \Display::url( |
||
200 | $courseInfo['extLink']['name'], |
||
201 | $courseInfo['extLink']['url'], |
||
202 | ['class' => 'extLink'] |
||
203 | ); |
||
204 | } elseif (!empty($courseInfo['extLink']['url'])) { |
||
205 | $headerLogo .= $courseInfo['extLink']['url']; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | return \Display::tag('h2', $headerLogo, ['class' => 'text-left']); |
||
210 | } |
||
211 | |||
212 | $image = \Display::img($logoPath, $institution, $imageAttributes); |
||
213 | |||
214 | return \Display::url($image, $homeURL); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Like strip_tags(), but leaves an additional space and removes only the given tags. |
||
219 | * |
||
220 | * @param string $string |
||
221 | * @param array $tags Tags to be removed |
||
222 | * |
||
223 | * @return string The original string without the given tags |
||
224 | */ |
||
225 | public static function stripGivenTags($string, $tags) |
||
226 | { |
||
227 | foreach ($tags as $tag) { |
||
228 | $string2 = preg_replace('#</\b'.$tag.'\b[^>]*>#i', ' ', $string); |
||
229 | if ($string2 != $string) { |
||
230 | $string = preg_replace('/<\b'.$tag.'\b[^>]*>/i', ' ', $string2); |
||
231 | } |
||
232 | } |
||
233 | |||
234 | return $string; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Adds or Subtract a time in hh:mm:ss to a datetime. |
||
239 | * |
||
240 | * @param string $time Time to add or substract in hh:mm:ss format |
||
241 | * @param string $datetime Datetime to be modified as accepted by the Datetime class constructor |
||
242 | * @param bool $operation True for Add, False to Subtract |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | public static function addOrSubTimeToDateTime($time, $datetime = 'now', $operation = true) |
||
247 | { |
||
248 | $date = new \DateTime($datetime); |
||
249 | $hours = $minutes = $seconds = 0; |
||
250 | sscanf($time, "%d:%d:%d", $hours, $minutes, $seconds); |
||
251 | $timeSeconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes; |
||
252 | if ($operation) { |
||
253 | $date->add(new \DateInterval('PT'.$timeSeconds.'S')); |
||
254 | } else { |
||
255 | $date->sub(new \DateInterval('PT'.$timeSeconds.'S')); |
||
256 | } |
||
257 | |||
258 | return $date->format('Y-m-d H:i:s'); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Returns the course id (integer) for the given course directory or the current ID if no directory is defined. |
||
263 | * |
||
264 | * @param string $directory The course directory/path that appears in the URL |
||
265 | * |
||
266 | * @return int |
||
267 | */ |
||
268 | public static function getCourseIdByDirectory($directory = null) |
||
269 | { |
||
270 | if (!empty($directory)) { |
||
271 | $directory = \Database::escape_string($directory); |
||
272 | $row = \Database::select( |
||
273 | 'id', |
||
274 | \Database::get_main_table(TABLE_MAIN_COURSE), |
||
275 | ['where' => ['directory = ?' => [$directory]]], |
||
276 | 'first' |
||
277 | ); |
||
278 | |||
279 | if (is_array($row) && isset($row['id'])) { |
||
280 | return $row['id']; |
||
281 | } else { |
||
282 | return false; |
||
283 | } |
||
284 | } |
||
285 | |||
286 | return Session::read('_real_cid', 0); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Check if the current HTTP request is by AJAX. |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | public static function isAjaxRequest() |
||
295 | { |
||
296 | $requestedWith = $_SERVER['HTTP_X_REQUESTED_WITH'] ?? null; |
||
297 | |||
298 | return $requestedWith === 'XMLHttpRequest'; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Get a variable name for language file from a text. |
||
303 | * |
||
304 | * @param string $text |
||
305 | * @param string $prefix |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | public static function getLanguageVar($text, $prefix = '') |
||
310 | { |
||
311 | $text = api_replace_dangerous_char($text); |
||
312 | $text = str_replace(['-', ' ', '.'], '_', $text); |
||
313 | $text = preg_replace('/\_{1,}/', '_', $text); |
||
314 | //$text = str_replace('_', '', $text); |
||
315 | $text = api_underscore_to_camel_case($text); |
||
316 | |||
317 | return $prefix.$text; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Get the stylesheet path for HTML documents created with CKEditor. |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | public static function getEditorDocStylePath() |
||
326 | { |
||
327 | $visualTheme = api_get_visual_theme(); |
||
328 | |||
329 | $cssFile = api_get_path(SYS_CSS_PATH)."themes/$visualTheme/document.css"; |
||
330 | |||
331 | if (is_file($cssFile)) { |
||
332 | return api_get_path(WEB_CSS_PATH)."themes/$visualTheme/document.css"; |
||
333 | } |
||
334 | |||
335 | return api_get_path(WEB_CSS_PATH).'document.css'; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Get the stylesheet path for HTML blocks created with CKEditor. |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | public static function getEditorBlockStylePath() |
||
344 | { |
||
345 | $visualTheme = api_get_visual_theme(); |
||
346 | |||
347 | $cssFile = api_get_path(SYS_CSS_PATH)."themes/$visualTheme/editor_content.css"; |
||
348 | |||
349 | if (is_file($cssFile)) { |
||
350 | return api_get_path(WEB_CSS_PATH)."themes/$visualTheme/editor_content.css"; |
||
351 | } |
||
352 | |||
353 | return api_get_path(WEB_CSS_PATH).'editor_content.css'; |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Get a list of colors from the palette at main/palette/pchart/default.color |
||
358 | * and return it as an array of strings. |
||
359 | * |
||
360 | * @param bool $decimalOpacity Whether to return the opacity as 0..100 or 0..1 |
||
361 | * @param bool $wrapInRGBA Whether to return it as 1,1,1,100 or rgba(1,1,1,100) |
||
362 | * @param int $fillUpTo If the number of colors is smaller than this number, generate more colors |
||
363 | * |
||
364 | * @return array An array of string colors |
||
365 | */ |
||
366 | public static function getColorPalette( |
||
367 | $decimalOpacity = false, |
||
368 | $wrapInRGBA = false, |
||
369 | $fillUpTo = 0 |
||
370 | ) { |
||
371 | // Get the common colors from the palette used for pchart |
||
372 | $paletteFile = api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color'; |
||
373 | $palette = file($paletteFile); |
||
374 | if ($decimalOpacity) { |
||
375 | // Because the pchart palette has transparency as integer values |
||
376 | // (0..100) and chartjs uses percentage (0.0..1.0), we need to divide |
||
377 | // the last value by 100, which is a bit overboard for just one chart |
||
378 | foreach ($palette as $index => $color) { |
||
379 | $components = preg_split('/,/', trim($color)); |
||
380 | $components[3] = round($components[3] / 100, 1); |
||
381 | $palette[$index] = join(',', $components); |
||
382 | } |
||
383 | } |
||
384 | if ($wrapInRGBA) { |
||
385 | foreach ($palette as $index => $color) { |
||
386 | $color = trim($color); |
||
387 | $palette[$index] = 'rgba('.$color.')'; |
||
388 | } |
||
389 | } |
||
390 | // If we want more colors, loop through existing colors |
||
391 | $count = count($palette); |
||
392 | if (isset($fillUpTo) && $fillUpTo > $count) { |
||
393 | for ($i = $count; $i < $fillUpTo; $i++) { |
||
394 | $palette[$i] = $palette[$i % $count]; |
||
395 | } |
||
396 | } |
||
397 | |||
398 | return $palette; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Get the local time for the midnight. |
||
403 | * |
||
404 | * @param string|null $utcTime Optional. The time to ve converted. |
||
405 | * See api_get_local_time. |
||
406 | * |
||
407 | * @throws \Exception |
||
408 | * |
||
409 | * @return \DateTime |
||
410 | */ |
||
411 | public static function getServerMidnightTime($utcTime = null) |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Get JavaScript code necessary to load quiz markers-rolls in medialement's Markers Rolls plugin. |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | public static function getQuizMarkersRollsJS() |
||
448 | + $.param({ |
||
449 | exerciseId: qMarkerRoll[1], |
||
450 | learnpath_id: 0, |
||
451 | learnpath_item_id: 0, |
||
452 | learnpath_item_view_id: 0 |
||
453 | }); |
||
454 | |||
455 | instance.options.markersRolls[qMarkerRoll[0]] = url; |
||
456 | }); |
||
457 | |||
462 |