MDL-64994 analytics: Add a simple semantic version check method

This method is to be used for checking that a compatible version of the
moodlemlbackend package is installed on the server. The package is
expected to use the semantic versioning scheme (semver.org).
This commit is contained in:
David Mudrák
2019-03-05 13:47:14 +01:00
parent 3271c39c74
commit 2d8405e99e
2 changed files with 218 additions and 0 deletions
@@ -395,4 +395,39 @@ class processor implements \core_analytics\classifier, \core_analytics\regresso
// This is not ideal, but there is no read access to moodle filesystem files.
return $file->copy_content_to_temp('core_analytics');
}
/**
* Check that the given package version can be used and return the error status.
*
* When evaluating the version, we assume the sematic versioning scheme as described at
* https://semver.org/.
*
* @param string $actual The actual Python package version
* @param string $required The required version of the package
* @return int -1 = actual version is too low, 1 = actual version too high, 0 = actual version is ok
*/
public static function check_pip_package_version($actual, $required = self::REQUIRED_PIP_PACKAGE_VERSION) {
if (empty($actual)) {
return -1;
}
if (version_compare($actual, $required, '<')) {
return -1;
}
$parts = explode('.', $required);
$requiredapiver = reset($parts);
$parts = explode('.', $actual);
$actualapiver = reset($parts);
if ($requiredapiver > 0 || $actualapiver > 1) {
if (version_compare($actual, $requiredapiver + 1, '>=')) {
return 1;
}
}
return 0;
}
}