Conditions | 12 |
Paths | 246 |
Total Lines | 106 |
Code Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
136 | function check_system_version() |
||
137 | { |
||
138 | // Check if curl is available. |
||
139 | if (!in_array('curl', get_loaded_extensions())) { |
||
140 | return '<span style="color:red">'.get_lang('ImpossibleToContactVersionServerPleaseTryAgain').'</span>'; |
||
141 | } |
||
142 | |||
143 | $url = 'https://version.chamilo.org'; |
||
144 | $options = [ |
||
145 | 'verify' => false, |
||
146 | ]; |
||
147 | |||
148 | $urlValidated = false; |
||
149 | |||
150 | try { |
||
151 | $client = new GuzzleHttp\Client(); |
||
152 | $res = $client->request('GET', $url, $options); |
||
153 | if ($res->getStatusCode() == '200' || $res->getStatusCode() == '301') { |
||
154 | $urlValidated = true; |
||
155 | } |
||
156 | } catch (Exception $e) { |
||
|
|||
157 | } |
||
158 | |||
159 | // the chamilo version of your installation |
||
160 | $system_version = trim(api_get_configuration_value('system_version')); |
||
161 | |||
162 | if ($urlValidated) { |
||
163 | // The number of courses |
||
164 | $number_of_courses = Statistics::countCourses(); |
||
165 | |||
166 | // The number of users |
||
167 | $number_of_users = Statistics::countUsers(); |
||
168 | $number_of_active_users = Statistics::countUsers( |
||
169 | null, |
||
170 | null, |
||
171 | null, |
||
172 | true |
||
173 | ); |
||
174 | |||
175 | // The number of sessions |
||
176 | $number_of_sessions = SessionManager::count_sessions(api_get_current_access_url_id()); |
||
177 | $packager = api_get_configuration_value('packager'); |
||
178 | if (empty($packager)) { |
||
179 | $packager = 'chamilo'; |
||
180 | } |
||
181 | |||
182 | $uniqueId = ''; |
||
183 | $entityManager = Database::getManager(); |
||
184 | /** @var BranchSyncRepository $branch */ |
||
185 | $repository = $entityManager->getRepository('ChamiloCoreBundle:BranchSync'); |
||
186 | /** @var BranchSync $branch */ |
||
187 | $branch = $repository->getTopBranch(); |
||
188 | if (is_a($branch, '\Chamilo\CoreBundle\Entity\BranchSync')) { |
||
189 | $uniqueId = $branch->getUniqueId(); |
||
190 | } |
||
191 | |||
192 | $data = [ |
||
193 | 'url' => api_get_path(WEB_PATH), |
||
194 | 'campus' => api_get_setting('siteName'), |
||
195 | 'contact' => api_get_setting('emailAdministrator'), // the admin's e-mail, with the only purpose of being able to contact admins to inform about critical security issues |
||
196 | 'version' => $system_version, |
||
197 | 'numberofcourses' => $number_of_courses, // to sum up into non-personal statistics - see https://version.chamilo.org/stats/ |
||
198 | 'numberofusers' => $number_of_users, // to sum up into non-personal statistics |
||
199 | 'numberofactiveusers' => $number_of_active_users, // to sum up into non-personal statistics |
||
200 | 'numberofsessions' => $number_of_sessions, |
||
201 | //The donotlistcampus setting recovery should be improved to make |
||
202 | // it true by default - this does not affect numbers counting |
||
203 | 'donotlistcampus' => api_get_setting('donotlistcampus'), |
||
204 | 'organisation' => api_get_setting('Institution'), |
||
205 | 'language' => api_get_setting('platformLanguage'), //helps us know the spread of language usage for campuses, by main language |
||
206 | 'adminname' => api_get_setting('administratorName').' '.api_get_setting('administratorSurname'), //not sure this is necessary... |
||
207 | 'ip' => $_SERVER['REMOTE_ADDR'], //the admin's IP address, with the only purpose of trying to geolocate portals around the globe to draw a map |
||
208 | // Reference to the packager system or provider through which |
||
209 | // Chamilo is installed/downloaded. Packagers can change this in |
||
210 | // the default config file (main/install/configuration.dist.php) |
||
211 | // or in the installed config file. The default value is 'chamilo' |
||
212 | 'packager' => $packager, |
||
213 | 'unique_id' => $uniqueId, |
||
214 | ]; |
||
215 | |||
216 | $version = null; |
||
217 | $client = new GuzzleHttp\Client(); |
||
218 | $url .= '?'; |
||
219 | foreach ($data as $k => $v) { |
||
220 | $url .= urlencode($k).'='.urlencode($v).'&'; |
||
221 | } |
||
222 | $res = $client->request('GET', $url, $options); |
||
223 | if ($res->getStatusCode() == '200') { |
||
224 | $versionData = $res->getHeader('X-Chamilo-Version'); |
||
225 | if (isset($versionData[0])) { |
||
226 | $version = trim($versionData[0]); |
||
227 | } |
||
228 | } |
||
229 | |||
230 | if (version_compare($system_version, $version, '<')) { |
||
231 | $output = '<span style="color:red">'.get_lang('YourVersionNotUpToDate').'<br /> |
||
232 | '.get_lang('LatestVersionIs').' <b>Chamilo '.$version.'</b>. <br /> |
||
233 | '.get_lang('YourVersionIs').' <b>Chamilo '.$system_version.'</b>. <br />'.str_replace('http://www.chamilo.org', '<a href="http://www.chamilo.org">http://www.chamilo.org</a>', get_lang('PleaseVisitOurWebsite')).'</span>'; |
||
234 | } else { |
||
235 | $output = '<span style="color:green">'.get_lang('VersionUpToDate').': Chamilo '.$version.'</span>'; |
||
236 | } |
||
237 | |||
238 | return $output; |
||
239 | } |
||
240 | |||
241 | return '<span style="color:red">'.get_lang('ImpossibleToContactVersionServerPleaseTryAgain').'</span>'; |
||
242 | } |
||
273 |