| Conditions | 15 |
| Paths | 736 |
| Total Lines | 128 |
| Code Lines | 84 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 124 | function check_system_version() |
||
| 125 | { |
||
| 126 | // Check if curl is available. |
||
| 127 | if (!in_array('curl', get_loaded_extensions())) { |
||
| 128 | return '<span style="color:red">'. |
||
| 129 | get_lang('Impossible to contact the version server right now. Please try again later.').'</span>'; |
||
| 130 | } |
||
| 131 | |||
| 132 | $url = 'https://version.chamilo.org'; |
||
| 133 | $options = [ |
||
| 134 | 'verify' => false, |
||
| 135 | ]; |
||
| 136 | |||
| 137 | $urlValidated = false; |
||
| 138 | |||
| 139 | try { |
||
| 140 | $client = new GuzzleHttp\Client(); |
||
| 141 | $res = $client->request('GET', $url, $options); |
||
| 142 | if ('200' == $res->getStatusCode() || '301' == $res->getStatusCode()) { |
||
| 143 | $urlValidated = true; |
||
| 144 | } |
||
| 145 | } catch (Exception $e) { |
||
|
|
|||
| 146 | } |
||
| 147 | |||
| 148 | // the chamilo version of your installation |
||
| 149 | $system_version = ''; |
||
| 150 | $versionStatus = ''; |
||
| 151 | $versionFile =__DIR__.'/../../install/version.php'; |
||
| 152 | if (is_file($versionFile)) { |
||
| 153 | $versionDetails = include($versionFile); |
||
| 154 | $system_version = trim($versionDetails['new_version']); |
||
| 155 | if (!empty($versionDetails['new_version_status']) && $versionDetails['new_version_status'] != 'stable') { |
||
| 156 | $versionStatus = ' ('.$versionDetails['new_version_status'].')'; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($urlValidated) { |
||
| 161 | // The number of courses |
||
| 162 | $number_of_courses = Statistics::countCourses(); |
||
| 163 | |||
| 164 | // The number of users |
||
| 165 | $number_of_users = Statistics::countUsers(); |
||
| 166 | $number_of_active_users = Statistics::countUsers( |
||
| 167 | null, |
||
| 168 | null, |
||
| 169 | true, |
||
| 170 | true |
||
| 171 | ); |
||
| 172 | |||
| 173 | // The number of sessions |
||
| 174 | $number_of_sessions = SessionManager::count_sessions(api_get_current_access_url_id()); |
||
| 175 | $packager = api_get_setting('platform.packager'); |
||
| 176 | if (empty($packager)) { |
||
| 177 | $packager = 'chamilo'; |
||
| 178 | } |
||
| 179 | |||
| 180 | $uniqueId = ''; |
||
| 181 | $entityManager = Database::getManager(); |
||
| 182 | /** @var BranchSyncRepository $repository */ |
||
| 183 | $repository = $entityManager->getRepository(BranchSync::class); |
||
| 184 | /** @var BranchSync $branch */ |
||
| 185 | $branch = $repository->getTopBranch(); |
||
| 186 | if (is_a($branch, BranchSync::class)) { |
||
| 187 | $uniqueId = $branch->getUniqueId(); |
||
| 188 | } |
||
| 189 | |||
| 190 | $data = [ |
||
| 191 | 'url' => api_get_path(WEB_PATH), |
||
| 192 | 'campus' => api_get_setting('siteName'), |
||
| 193 | '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 |
||
| 194 | 'version' => $system_version, |
||
| 195 | 'numberofcourses' => $number_of_courses, // to sum up into non-personal statistics - see https://version.chamilo.org/stats/ |
||
| 196 | 'numberofusers' => $number_of_users, // to sum up into non-personal statistics |
||
| 197 | 'numberofactiveusers' => $number_of_active_users, // to sum up into non-personal statistics |
||
| 198 | 'numberofsessions' => $number_of_sessions, |
||
| 199 | //The donotlistcampus setting recovery should be improved to make |
||
| 200 | // it true by default - this does not affect numbers counting |
||
| 201 | 'donotlistcampus' => api_get_setting('donotlistcampus'), |
||
| 202 | 'organisation' => api_get_setting('Institution'), |
||
| 203 | 'language' => api_get_setting('platformLanguage'), //helps us know the spread of language usage for campuses, by main language |
||
| 204 | 'adminname' => api_get_setting('administratorName').' '.api_get_setting('administratorSurname'), //not sure this is necessary... |
||
| 205 | '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 |
||
| 206 | // Reference to the packager system or provider through which |
||
| 207 | // Chamilo is installed/downloaded. Packagers can change this in |
||
| 208 | // the default config file (main/install/configuration.dist.php) |
||
| 209 | // or in the installed config file. The default value is 'chamilo' |
||
| 210 | 'packager' => $packager, |
||
| 211 | 'unique_id' => $uniqueId, |
||
| 212 | ]; |
||
| 213 | |||
| 214 | $version = null; |
||
| 215 | $client = new GuzzleHttp\Client(); |
||
| 216 | $url .= '?'; |
||
| 217 | foreach ($data as $k => $v) { |
||
| 218 | $url .= urlencode($k).'='.urlencode($v).'&'; |
||
| 219 | } |
||
| 220 | $res = $client->request('GET', $url, $options); |
||
| 221 | if ('200' == $res->getStatusCode()) { |
||
| 222 | $versionData = $res->getHeader('X-Chamilo-Version'); |
||
| 223 | if (isset($versionData[0])) { |
||
| 224 | $version = trim($versionData[0]); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | if (version_compare($system_version, $version, '<')) { |
||
| 229 | $output = '<span style="color:red">'. |
||
| 230 | get_lang('Your version is not up-to-date').'<br />'. |
||
| 231 | get_lang('The latest version is').' <b>Chamilo '.$version.'</b>. <br />'. |
||
| 232 | get_lang('Your version is').' <b>Chamilo '.$system_version.$versionStatus.'</b>. <br />'. |
||
| 233 | str_replace( |
||
| 234 | 'http://www.chamilo.org', |
||
| 235 | '<a href="https://chamilo.org/download">https://chamilo.org/download</a>', |
||
| 236 | get_lang('Please visit our website: http://www.chamilo.org') |
||
| 237 | ). |
||
| 238 | '</span>'; |
||
| 239 | } else { |
||
| 240 | $output = '<span style="color:green">'. |
||
| 241 | get_lang('Your version is up-to-date').'<br />'. |
||
| 242 | get_lang('The latest version is').' <b>Chamilo '.$version.'</b>. <br />'. |
||
| 243 | get_lang('Your version is').' <b>Chamilo '.$system_version.$versionStatus.'</b>. <br />'. |
||
| 244 | '</span>'; |
||
| 245 | } |
||
| 246 | |||
| 247 | return $output; |
||
| 248 | } |
||
| 249 | |||
| 250 | return '<span style="color:red">'. |
||
| 251 | get_lang('Impossible to contact the version server right now. Please try again later.').'</span>'; |
||
| 252 | } |
||
| 283 |