Skip to content

Instantly share code, notes, and snippets.

@tigusigalpa
Last active January 9, 2024 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tigusigalpa/af051a9112512b1b0369572b5dbea2fd to your computer and use it in GitHub Desktop.
Save tigusigalpa/af051a9112512b1b0369572b5dbea2fd to your computer and use it in GitHub Desktop.
Check the current Moodle version function by given params
/**
* Function to check the current Moodle version pass your params
*
* @param string $version Number of version or release['3.5' or '4.1'] (depends on @param $checkfor)
* @param string $checkfor ['all' or 'release' ($version like '3.1.3')] or 'version' ($version like '2016052303')
* @param string $compare http://php.net/manual/function.version-compare.php#refsect1-function.version-compare-parameters
* @return bool
* @author Igor Sazonov <sovletig@gmail.com>
* @example $check = checkMoodleVersion('3.9'); //if current Moodle version is 4.2 - returns true because $compare is >=
*/
function checkMoodleVersion($version, $compare = '>=', $checkfor = 'all') {
global $CFG;
$versions = [
'3.1' => [
'version' => '2016052300',
'release' => '3.1',
],
'3.2' => [
'version' => '2016120500',
'release' => '3.2',
],
'3.3' => [
'version' => '2017051500',
'release' => '3.3',
],
'3.4' => [
'version' => '2017111300',
'release' => '3.4',
],
'3.5' => [
'version' => '2018051700',
'release' => '3.5',
],
'3.6' => [
'version' => '2018120300',
'release' => '3.6',
],
'3.7' => [
'version' => '2019052000',
'release' => '3.7',
],
'3.8' => [
'version' => '2019111800',
'release' => '3.8',
],
'3.9' => [
'version' => '2020061500',
'release' => '3.9',
],
'4.0' => [
'version' => '2022041900',
'release' => '4.0',
],
'4.1' => [
'version' => '2022112800',
'release' => '4.1',
],
'4.2' => [
'version' => '2023042400',
'release' => '4.2',
],
'4.3' => [
'version' => '2023100900',
'release' => '4.3',
],
];
$release = (strpos($CFG->release, ' ') === false) ? $CFG->release : explode(' ', $CFG->release)[0];
switch ($checkfor) {
case 'all':
if (isset($versions[$version]['version'])) {
return version_compare($CFG->version, $versions[$version]['version'], $compare) &&
version_compare($release, $version, $compare);
}
break;
case 'version':
case 'release':
if (isset($versions[$version][$checkfor])) {
if ($checkfor == 'release') {
return version_compare($release, $versions[$version][$checkfor], $compare);
}
return version_compare($CFG->$checkfor, $versions[$version][$checkfor], $compare);
}
break;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment