Total Complexity | 44 |
Total Lines | 318 |
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) |
||
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() |
||
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 = '') |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Get the stylesheet path for HTML documents created with CKEditor. |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | public static function getEditorDocStylePath() |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Get the stylesheet path for HTML blocks created with CKEditor. |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | public static function getEditorBlockStylePath() |
||
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( |
||
333 |