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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user