1
|
|
|
<?php |
2
|
|
|
/* For licensing terms, see /license.txt */ |
3
|
|
|
|
4
|
|
|
namespace Chamilo\CoreBundle\Component\Utils; |
5
|
|
|
|
6
|
|
|
use ChamiloSession as Session; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ChamiloApi. |
10
|
|
|
* |
11
|
|
|
* @package Chamilo\CoreBundle\Component |
12
|
|
|
*/ |
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() |
31
|
|
|
{ |
32
|
|
|
return self::$configuration; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $variable |
37
|
|
|
* |
38
|
|
|
* @return bool|string |
39
|
|
|
*/ |
40
|
|
|
public static function getConfigurationValue($variable) |
41
|
|
|
{ |
42
|
|
|
$configuration = self::getConfigurationArray(); |
43
|
|
|
if (array_key_exists($variable, $configuration)) { |
44
|
|
|
return $configuration[$variable]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return false; |
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() |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
'540x405' => '540x405 (3/4)', |
59
|
|
|
'640x480' => '640x480 (3/4)', |
60
|
|
|
'720x540' => '720x540 (3/4)', |
61
|
|
|
'800x600' => '800x600 (3/4)', |
62
|
|
|
'1024x576' => '1024x576 (16/9)', |
63
|
|
|
'1024x768' => '1000x750 (3/4)', |
64
|
|
|
'1280x720' => '1280x720 (16/9)', |
65
|
|
|
'1280x860' => '1280x960 (3/4)', |
66
|
|
|
'1400x1050' => '1400x1050 (3/4)', |
67
|
|
|
'1600x900' => '1600x900 (16/9)', |
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) |
81
|
|
|
{ |
82
|
|
|
static $logoPath; |
83
|
|
|
|
84
|
|
|
// If call from CLI it should be reload. |
85
|
|
|
if ('cli' === PHP_SAPI) { |
86
|
|
|
$logoPath = null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!isset($logoPath) || $forcedGetter) { |
90
|
|
|
$theme = empty($theme) ? api_get_visual_theme() : $theme; |
91
|
|
|
$accessUrlId = api_get_current_access_url_id(); |
92
|
|
|
if ('cli' === PHP_SAPI) { |
93
|
|
|
$accessUrl = api_get_configuration_value('access_url'); |
94
|
|
|
if (!empty($accessUrl)) { |
95
|
|
|
$accessUrlId = $accessUrl; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
$themeDir = \Template::getThemeDir($theme); |
99
|
|
|
$customLogoPath = $themeDir."images/header-logo-custom$accessUrlId.png"; |
100
|
|
|
|
101
|
|
|
$svgIcons = api_get_setting('icons_mode_svg'); |
102
|
|
|
if ($svgIcons === 'true') { |
103
|
|
|
$customLogoPathSVG = substr($customLogoPath, 0, -3).'svg'; |
104
|
|
|
if (file_exists(api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPathSVG")) { |
105
|
|
|
if ($getSysPath) { |
106
|
|
|
$logoPath = api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPathSVG"; |
107
|
|
|
|
108
|
|
|
return $logoPath; |
109
|
|
|
} |
110
|
|
|
$logoPath = api_get_path(WEB_CSS_PATH).$customLogoPathSVG; |
111
|
|
|
|
112
|
|
|
return $logoPath; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
if (file_exists(api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPath")) { |
116
|
|
|
if ($getSysPath) { |
117
|
|
|
$logoPath = api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPath"; |
118
|
|
|
|
119
|
|
|
return $logoPath; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$logoPath = api_get_path(WEB_CSS_PATH).$customLogoPath; |
123
|
|
|
|
124
|
|
|
return $logoPath; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$originalLogoPath = $themeDir."images/header-logo.png"; |
128
|
|
|
if ($svgIcons === 'true') { |
129
|
|
|
$originalLogoPathSVG = $themeDir."images/header-logo.svg"; |
130
|
|
|
if (file_exists(api_get_path(SYS_CSS_PATH).$originalLogoPathSVG)) { |
131
|
|
|
if ($getSysPath) { |
132
|
|
|
$logoPath = api_get_path(SYS_CSS_PATH).$originalLogoPathSVG; |
133
|
|
|
|
134
|
|
|
return $logoPath; |
135
|
|
|
} |
136
|
|
|
$logoPath = api_get_path(WEB_CSS_PATH).$originalLogoPathSVG; |
137
|
|
|
|
138
|
|
|
return $logoPath; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (file_exists(api_get_path(SYS_CSS_PATH).$originalLogoPath)) { |
143
|
|
|
if ($getSysPath) { |
144
|
|
|
$logoPath = api_get_path(SYS_CSS_PATH).$originalLogoPath; |
145
|
|
|
|
146
|
|
|
return $logoPath; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$logoPath = api_get_path(WEB_CSS_PATH).$originalLogoPath; |
150
|
|
|
|
151
|
|
|
return $logoPath; |
152
|
|
|
} |
153
|
|
|
$logoPath = ''; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $logoPath; |
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) |
412
|
|
|
{ |
413
|
|
|
$localTime = api_get_local_time($utcTime); |
414
|
|
|
$localTimeZone = api_get_timezone(); |
415
|
|
|
|
416
|
|
|
$localMidnight = new \DateTime($localTime, new \DateTimeZone($localTimeZone)); |
417
|
|
|
$localMidnight->modify('midnight'); |
418
|
|
|
|
419
|
|
|
return $localMidnight; |
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() |
428
|
|
|
{ |
429
|
|
|
$webCodePath = api_get_path(WEB_CODE_PATH); |
430
|
|
|
$cidReq = api_get_cidreq(true, true, 'embeddable'); |
431
|
|
|
$colorPalette = self::getColorPalette(false, true); |
432
|
|
|
|
433
|
|
|
return " |
434
|
|
|
var \$originalNode = $(originalNode), |
435
|
|
|
qMarkersRolls = \$originalNode.data('q-markersrolls') || [], |
436
|
|
|
qMarkersColor = \$originalNode.data('q-markersrolls-color') || '{$colorPalette[0]}'; |
437
|
|
|
|
438
|
|
|
if (0 == qMarkersRolls.length) { |
439
|
|
|
return; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
instance.options.markersRollsColor = qMarkersColor; |
443
|
|
|
instance.options.markersRollsWidth = 2; |
444
|
|
|
instance.options.markersRolls = {}; |
445
|
|
|
|
446
|
|
|
qMarkersRolls.forEach(function (qMarkerRoll) { |
447
|
|
|
var url = '{$webCodePath}exercise/exercise_submit.php?$cidReq&' |
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
|
|
|
|
458
|
|
|
instance.buildmarkersrolls(instance, instance.controls, instance.layers, instance.media); |
459
|
|
|
"; |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
|