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
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public static function getPlatformLogoPath($theme = '', $getSysPath = false) |
80
|
|
|
{ |
81
|
|
|
$theme = empty($theme) ? api_get_visual_theme() : $theme; |
82
|
|
|
$accessUrlId = api_get_current_access_url_id(); |
83
|
|
|
$themeDir = \Template::getThemeDir($theme); |
84
|
|
|
$customLogoPath = $themeDir."images/header-logo-custom$accessUrlId.png"; |
85
|
|
|
|
86
|
|
|
if (file_exists(api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPath")) { |
87
|
|
|
if ($getSysPath) { |
88
|
|
|
return api_get_path(SYS_PUBLIC_PATH)."css/$customLogoPath"; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return api_get_path(WEB_CSS_PATH).$customLogoPath; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$originalLogoPath = $themeDir."images/header-logo.png"; |
95
|
|
|
|
96
|
|
|
if (file_exists(api_get_path(SYS_CSS_PATH).$originalLogoPath)) { |
97
|
|
|
if ($getSysPath) { |
98
|
|
|
return api_get_path(SYS_CSS_PATH).$originalLogoPath; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return api_get_path(WEB_CSS_PATH).$originalLogoPath; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return ''; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the platform logo. |
109
|
|
|
* Return a <img> if the logo image exists. Otherwise return a <h2> with the institution name. |
110
|
|
|
* |
111
|
|
|
* @param string $theme |
112
|
|
|
* @param array $imageAttributes optional |
113
|
|
|
* @param bool $getSysPath |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public static function getPlatformLogo($theme = '', $imageAttributes = [], $getSysPath = false) |
118
|
|
|
{ |
119
|
|
|
$logoPath = self::getPlatformLogoPath($theme, $getSysPath); |
120
|
|
|
$institution = api_get_setting('Institution'); |
121
|
|
|
$institutionUrl = api_get_setting('InstitutionUrl'); |
122
|
|
|
$siteName = api_get_setting('siteName'); |
123
|
|
|
|
124
|
|
|
if ($logoPath === null) { |
125
|
|
|
$headerLogo = \Display::url($siteName, api_get_path(WEB_PATH).'index.php'); |
126
|
|
|
|
127
|
|
|
if (!empty($institutionUrl) && !empty($institution)) { |
128
|
|
|
$headerLogo .= ' - '.\Display::url($institution, $institutionUrl); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$courseInfo = api_get_course_info(); |
132
|
|
|
if (isset($courseInfo['extLink']) && !empty($courseInfo['extLink']['name'])) { |
133
|
|
|
$headerLogo .= '<span class="extLinkSeparator"> - </span>'; |
134
|
|
|
|
135
|
|
|
if (!empty($courseInfo['extLink']['url'])) { |
136
|
|
|
$headerLogo .= \Display::url( |
137
|
|
|
$courseInfo['extLink']['name'], |
138
|
|
|
$courseInfo['extLink']['url'], |
139
|
|
|
['class' => 'extLink'] |
140
|
|
|
); |
141
|
|
|
} elseif (!empty($courseInfo['extLink']['url'])) { |
142
|
|
|
$headerLogo .= $courseInfo['extLink']['url']; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return \Display::tag('h2', $headerLogo, ['class' => 'text-left']); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$image = \Display::img($logoPath, $institution, $imageAttributes); |
150
|
|
|
|
151
|
|
|
return \Display::url($image, api_get_path(WEB_PATH).'index.php'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Like strip_tags(), but leaves an additional space and removes only the given tags. |
156
|
|
|
* |
157
|
|
|
* @param string $string |
158
|
|
|
* @param array $tags Tags to be removed |
159
|
|
|
* |
160
|
|
|
* @return string The original string without the given tags |
161
|
|
|
*/ |
162
|
|
|
public static function stripGivenTags($string, $tags) |
163
|
|
|
{ |
164
|
|
|
foreach ($tags as $tag) { |
165
|
|
|
$string2 = preg_replace('#</'.$tag.'[^>]*>#i', ' ', $string); |
166
|
|
|
if ($string2 != $string) { |
167
|
|
|
$string = preg_replace('/<'.$tag.'[^>]*>/i', ' ', $string2); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $string; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Adds or Subtract a time in hh:mm:ss to a datetime. |
176
|
|
|
* |
177
|
|
|
* @param string $time Time in hh:mm:ss format |
178
|
|
|
* @param string $datetime Datetime as accepted by the Datetime class constructor |
179
|
|
|
* @param bool $operation True for Add, False to Subtract |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public static function addOrSubTimeToDateTime($time, $datetime = 'now', $operation = true) |
184
|
|
|
{ |
185
|
|
|
$date = new \DateTime($datetime); |
186
|
|
|
$hours = $minutes = $seconds = 0; |
187
|
|
|
sscanf($time, "%d:%d:%d", $hours, $minutes, $seconds); |
188
|
|
|
$timeSeconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes; |
189
|
|
|
if ($operation) { |
190
|
|
|
$date->add(new \DateInterval('PT'.$timeSeconds.'S')); |
191
|
|
|
} else { |
192
|
|
|
$date->sub(new \DateInterval('PT'.$timeSeconds.'S')); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $date->format('Y-m-d H:i:s'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Returns the course id (integer) for the given course directory or the current ID if no directory is defined. |
200
|
|
|
* |
201
|
|
|
* @param string $directory The course directory/path that appears in the URL |
202
|
|
|
* |
203
|
|
|
* @return int |
204
|
|
|
*/ |
205
|
|
|
public static function getCourseIdByDirectory($directory = null) |
206
|
|
|
{ |
207
|
|
|
if (!empty($directory)) { |
208
|
|
|
$directory = \Database::escape_string($directory); |
209
|
|
|
$row = \Database::select( |
210
|
|
|
'id', |
211
|
|
|
\Database::get_main_table(TABLE_MAIN_COURSE), |
212
|
|
|
['where' => ['directory = ?' => [$directory]]], |
213
|
|
|
'first' |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
if (is_array($row) && isset($row['id'])) { |
217
|
|
|
return $row['id']; |
218
|
|
|
} else { |
219
|
|
|
return false; |
|
|
|
|
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return Session::read('_real_cid', 0); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Check if the current HTTP request is by AJAX. |
228
|
|
|
* |
229
|
|
|
* @return bool |
230
|
|
|
*/ |
231
|
|
|
public static function isAjaxRequest() |
232
|
|
|
{ |
233
|
|
|
$requestedWith = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] : null; |
234
|
|
|
|
235
|
|
|
return $requestedWith === 'XMLHttpRequest'; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get a variable name for language file from a text. |
240
|
|
|
* |
241
|
|
|
* @param string $text |
242
|
|
|
* @param string $prefix |
243
|
|
|
* |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
public static function getLanguageVar($text, $prefix = '') |
247
|
|
|
{ |
248
|
|
|
$text = api_replace_dangerous_char($text); |
249
|
|
|
$text = str_replace(['-', ' ', '.'], '_', $text); |
250
|
|
|
$text = preg_replace('/\_{1,}/', '_', $text); |
251
|
|
|
//$text = str_replace('_', '', $text); |
252
|
|
|
$text = api_underscore_to_camel_case($text); |
253
|
|
|
|
254
|
|
|
return $prefix.$text; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get the stylesheet path for HTML documents created with CKEditor. |
259
|
|
|
* |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
|
public static function getEditorDocStylePath() |
263
|
|
|
{ |
264
|
|
|
$visualTheme = api_get_visual_theme(); |
265
|
|
|
|
266
|
|
|
$cssFile = api_get_path(SYS_CSS_PATH)."themes/$visualTheme/document.css"; |
267
|
|
|
|
268
|
|
|
if (is_file($cssFile)) { |
269
|
|
|
return api_get_path(WEB_CSS_PATH)."themes/$visualTheme/document.css"; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return api_get_path(WEB_CSS_PATH).'document.css'; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Get the stylesheet path for HTML blocks created with CKEditor. |
277
|
|
|
* |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
|
public static function getEditorBlockStylePath() |
281
|
|
|
{ |
282
|
|
|
$visualTheme = api_get_visual_theme(); |
283
|
|
|
|
284
|
|
|
$cssFile = api_get_path(SYS_CSS_PATH)."themes/$visualTheme/editor_content.css"; |
285
|
|
|
|
286
|
|
|
if (is_file($cssFile)) { |
287
|
|
|
return api_get_path(WEB_CSS_PATH)."themes/$visualTheme/editor_content.css"; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
return api_get_path(WEB_CSS_PATH).'editor_content.css'; |
291
|
|
|
} |
292
|
|
|
/** |
293
|
|
|
* Get a list of colors from the palette at main/palette/pchart/default.color |
294
|
|
|
* and return it as an array of strings |
295
|
|
|
* @param bool $decimalOpacity Whether to return the opacity as 0..100 or 0..1 |
296
|
|
|
* @param bool $wrapInRGBA Whether to return it as 1,1,1,100 or rgba(1,1,1,100) |
297
|
|
|
* @param int $fillUpTo If the number of colors is smaller than this number, generate more colors |
298
|
|
|
* @return array An array of string colors |
299
|
|
|
*/ |
300
|
|
|
public static function getColorPalette( |
301
|
|
|
$decimalOpacity = false, |
302
|
|
|
$wrapInRGBA = false, |
303
|
|
|
$fillUpTo = null |
304
|
|
|
) { |
305
|
|
|
// Get the common colors from the palette used for pchart |
306
|
|
|
$paletteFile = api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color'; |
307
|
|
|
$palette = file($paletteFile); |
308
|
|
|
if ($decimalOpacity) { |
309
|
|
|
// Because the pchart palette has transparency as integer values |
310
|
|
|
// (0..100) and chartjs uses percentage (0.0..1.0), we need to divide |
311
|
|
|
// the last value by 100, which is a bit overboard for just one chart |
312
|
|
|
foreach ($palette as $index => $color) { |
313
|
|
|
$components = preg_split('/,/', trim($color)); |
314
|
|
|
$components[3] = round($components[3] / 100, 1); |
315
|
|
|
$palette[$index] = join(',', $components); |
|
|
|
|
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
if ($wrapInRGBA) { |
319
|
|
|
foreach ($palette as $index => $color) { |
320
|
|
|
$palette[$index] = 'rgba('.$palette[$index].')'; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
// If we want more colors, loop through existing colors |
324
|
|
|
$count = count($palette); |
|
|
|
|
325
|
|
|
if (isset($fillUpTo) && $fillUpTo > $count) { |
326
|
|
|
for ($i = $count; $i < $fillUpTo; $i++) { |
327
|
|
|
$palette[$i] = $palette[$i%$count]; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
return $palette; |
|
|
|
|
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|