Skip to content

Commit cce82e3

Browse files
committed
MOBILE-1986 site: Implement and use function to check site version
1 parent 8e44c7e commit cce82e3

File tree

11 files changed

+177
-42
lines changed

11 files changed

+177
-42
lines changed

www/addons/files/services/files.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ angular.module('mm.addons.files')
2626
"itemid": 0,
2727
"filepath": "",
2828
"filename": ""
29-
},
30-
moodle310version = 2016052300;
29+
};
3130

3231
/**
3332
* Check if core_files_get_files WS call is available.
@@ -429,8 +428,7 @@ angular.module('mm.addons.files')
429428
siteId = siteId || $mmSite.getId();
430429

431430
return $mmSitesManager.getSite(siteId).then(function(site) {
432-
var version = parseInt(site.getInfo().version, 10);
433-
return version && version >= moodle310version;
431+
return site.isVersionGreaterEqualThan('3.1.0');
434432
});
435433
};
436434

@@ -447,16 +445,12 @@ angular.module('mm.addons.files')
447445
siteId = siteId || $mmSite.getId();
448446

449447
return $mmSitesManager.getSite(siteId).then(function(site) {
450-
var version = parseInt(site.getInfo().version, 10);
451-
if (!version) {
452-
// Cannot determine version, return false.
453-
return false;
454-
} else if (version == moodle310version) {
455-
// Uploading is not working right now for Moodle 3.1.0 (2016052300).
456-
return false;
457-
} else if (version > moodle310version) {
448+
if (site.isVersionGreaterEqualThan('3.1.1')) {
458449
// In Moodle 3.1.1 or higher we need a WS to move to private files.
459450
return self.canMoveFromDraftToPrivate(siteId);
451+
} else if (site.isVersionGreaterEqualThan('3.1.0')) {
452+
// Upload private files doesn't work for Moodle 3.1.0 due to a bug.
453+
return false;
460454
}
461455

462456
return true;

www/addons/mod/assign/controllers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ angular.module('mm.addons.mod_assign')
111111
$scope.summary = response.gradingsummary;
112112

113113
$scope.needsGradingAvalaible = response.gradingsummary.submissionsneedgradingcount > 0 &&
114-
parseInt($mmSite.getInfo().version, 10) >= 2016110200;
114+
$mmSite.isVersionGreaterEqualThan('3.2');
115115
}).catch(function() {
116116
// Fail silently (WS is not available, fallback).
117117
return $q.when();

www/addons/mod/assign/submission/onlinetext/handlers.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,8 @@ angular.module('mm.addons.mod_assign')
137137
*/
138138
self.isEnabledForEdit = function() {
139139
// There's a bug in Moodle 3.1.0 that doesn't allow submitting HTML, so we'll disable this plugin in that case.
140-
// Bug was fixed in 3.1.1 minor release (2016052301) and in master version 2016070700.
141-
var version = parseInt($mmSite.getInfo().version, 10),
142-
localMobileEnabled = $mmSite.checkIfAppUsesLocalMobile();
143-
144-
return (version >= 2016052301 && version < 2016052400) || version >= 2016070700 || localMobileEnabled;
140+
// Bug was fixed in 3.1.1 minor release and in 3.2.
141+
return $mmSite.isVersionGreaterEqualThan('3.1.1') || $mmSite.checkIfAppUsesLocalMobile();
145142
};
146143

147144
/**

www/addons/mod/book/services/book.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,7 @@ angular.module('mm.addons.mod_book')
353353
siteId = siteId || $mmSite.getId();
354354

355355
return $mmSitesManager.getSite(siteId).then(function(site) {
356-
var version = site.getInfo().version;
357-
// Require Moodle 2.9.
358-
return version && (parseInt(version) >= 2015051100) && site.canDownloadFiles();
356+
return site.isVersionGreaterEqualThan('2.9') && site.canDownloadFiles();
359357
});
360358
};
361359

www/addons/mod/forum/services/forum.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ angular.module('mm.addons.mod_forum')
203203
self.canAddAttachments = function(siteId) {
204204
return $mmSitesManager.getSite(siteId).then(function(site) {
205205
// Attachments allowed from Moodle 3.1.
206-
var version = parseInt(site.getInfo().version, 10);
207-
return version && version >= 2016052300;
206+
return site.isVersionGreaterEqualThan('3.1');
208207
});
209208
};
210209

www/addons/mod/imscp/services/imscp.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,7 @@ angular.module('mm.addons.mod_imscp')
358358
siteId = siteId || $mmSite.getId();
359359

360360
return $mmSitesManager.getSite(siteId).then(function(site) {
361-
var version = site.getInfo().version;
362-
// Require Moodle 2.9.
363-
return version && (parseInt(version) >= 2015051100) && site.canDownloadFiles();
361+
return site.isVersionGreaterEqualThan('2.9') && site.canDownloadFiles();
364362
});
365363
};
366364

www/addons/mod/wiki/services/wiki.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,8 @@ angular.module('mm.addons.mod_wiki')
448448
}
449449

450450
if (lockonly) {
451-
var version = $mmSite.getInfo().version;
452-
453451
// This parameter requires Moodle 3.2. It saves network usage.
454-
if (version && parseInt(version, 10) >= 2016100700) {
452+
if ($mmSite.isVersionGreaterEqualThan('3.2')) {
455453
params.lockonly = 1;
456454
}
457455
}

www/core/components/course/services/course.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,7 @@ angular.module('mm.core.course')
597597
}
598598

599599
return $mmSitesManager.getSite(siteId).then(function(site) {
600-
var version = parseInt(site.getInfo().version, 10);
601-
602-
if (version >= 2015051100) {
600+
if (site.isVersionGreaterEqualThan('2.9')) {
603601
// From Moodle 2.9 the course contents can be filtered, so maybe the module doesn't have contents
604602
// because they were filtered. Try to get its contents.
605603
return self.getModule(module.id, courseId, sectionId, preferCache, ignoreCache, siteId).then(function(mod) {

www/core/lib/site.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,32 @@ angular.module('mm.core')
498498
* Check if a certain feature is disabled in the site.
499499
*/
500500

501+
/**
502+
* @module mm.core
503+
* @ngdoc method
504+
* @name $mmSite#isVersionGreaterEqualThan
505+
* @param {Mixed} versions Version or list of versions to check.
506+
* @return {Boolean} True if greater or equal, false otherwise.
507+
* @description
508+
*
509+
* Check if the site version is greater than one or some versions.
510+
* This function accepts a string or an array of strings. If array, the last version must be the highest.
511+
*
512+
* If a string is supplied (e.g. '3.2.1'), it will check if the site version is greater or equal than this version.
513+
*
514+
* If an array of versions is supplied, it will check if the site version is greater or equal than the last version,
515+
* or if it's higher or equal than any of the other releases supplied but lower than the next major release. The last
516+
* version of the array must be the highest version.
517+
* For example, if the values supplied are ['3.0.5', '3.2.3', '3.3.1'] the function will return true if the site version
518+
* is either:
519+
* - Greater or equal than 3.3.1.
520+
* - Greater or equal than 3.2.3 but lower than 3.3.
521+
* - Greater or equal than 3.0.5 but lower than 3.1.
522+
*
523+
* This function only accepts versions from 2.4.0 and above. If any of the versions supplied isn't found, it will assume
524+
* it's the last released major version.
525+
*/
526+
501527
/**
502528
* @module mm.core
503529
* @ngdoc method

www/core/lib/sitesfactory.js

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,18 @@ angular.module('mm.core')
236236
"moodle_webservice_get_siteinfo": "core_webservice_get_site_info",
237237
};
238238

239-
var self = {};
239+
var self = {},
240+
moodleReleases = {
241+
'2.4': 2012120300,
242+
'2.5': 2013051400,
243+
'2.6': 2013111800,
244+
'2.7': 2014051200,
245+
'2.8': 2014111000,
246+
'2.9': 2015051100,
247+
'3.0': 2015111600,
248+
'3.1': 2016052300,
249+
'3.2': 2016120500
250+
};
240251

241252
/**
242253
* Site object to store site data.
@@ -752,7 +763,7 @@ angular.module('mm.core')
752763
*/
753764
Site.prototype.uploadFile = function(uri, options) {
754765
if (!options.fileArea) {
755-
if (parseInt(this.infos.version, 10) >= 2016052300) {
766+
if (this.isVersionGreaterEqualThan('3.1')) {
756767
// From Moodle 3.1 only draft is allowed.
757768
options.fileArea = 'draft';
758769
} else {
@@ -1290,6 +1301,123 @@ angular.module('mm.core')
12901301
return method;
12911302
};
12921303

1304+
/**
1305+
* Check if the site version is greater than one or some versions.
1306+
* This function accepts a string or an array of strings. If array, the last version must be the highest.
1307+
*
1308+
* @param {Mixed} versions Version or list of versions to check.
1309+
* @return {Boolean} True if greater or equal, false otherwise.
1310+
* @description
1311+
* If a string is supplied (e.g. '3.2.1'), it will check if the site version is greater or equal than this version.
1312+
*
1313+
* If an array of versions is supplied, it will check if the site version is greater or equal than the last version,
1314+
* or if it's higher or equal than any of the other releases supplied but lower than the next major release. The last
1315+
* version of the array must be the highest version.
1316+
* For example, if the values supplied are ['3.0.5', '3.2.3', '3.3.1'] the function will return true if the site version
1317+
* is either:
1318+
* - Greater or equal than 3.3.1.
1319+
* - Greater or equal than 3.2.3 but lower than 3.3.
1320+
* - Greater or equal than 3.0.5 but lower than 3.1.
1321+
*
1322+
* This function only accepts versions from 2.4.0 and above. If any of the versions supplied isn't found, it will assume
1323+
* it's the last released major version.
1324+
*/
1325+
Site.prototype.isVersionGreaterEqualThan = function(versions) {
1326+
var siteVersion = parseInt(this.getInfo().version, 10);
1327+
1328+
if (angular.isArray(versions)) {
1329+
if (!versions.length) {
1330+
return false;
1331+
}
1332+
1333+
for (var i = 0; i < versions.length; i++) {
1334+
var versionNumber = getVersionNumber(versions[i]);
1335+
if (i == versions.length - 1) {
1336+
// It's the last version, check only if site version is greater than this one.
1337+
return siteVersion >= versionNumber;
1338+
} else {
1339+
// Check if site version if bigger than this number but lesser than next major.
1340+
if (siteVersion >= versionNumber && siteVersion < getNextMajorVersionNumber(versions[i])) {
1341+
return true;
1342+
}
1343+
}
1344+
}
1345+
} else if (typeof versions == 'string') {
1346+
// Compare with this version.
1347+
return siteVersion >= getVersionNumber(versions);
1348+
}
1349+
1350+
return false;
1351+
};
1352+
1353+
/**
1354+
* Get a version number from a release version.
1355+
* If release version is valid but not found in the list of Moodle releases, it will use the last released major version.
1356+
*
1357+
* @param {String} version Release version to convert to version number.
1358+
* @return {Number} Version number, 0 if invalid.
1359+
*/
1360+
function getVersionNumber(version) {
1361+
var data = getMajorAndMinor(version);
1362+
1363+
if (!data) {
1364+
// Invalid version.
1365+
return 0;
1366+
}
1367+
1368+
if (typeof moodleReleases[data.major] == 'undefined') {
1369+
// Major version not found. Use the last one.
1370+
data.major = Object.keys(moodleReleases).slice(-1);
1371+
}
1372+
1373+
return moodleReleases[data.major] + data.minor;
1374+
}
1375+
1376+
/**
1377+
* Given a release version, return the major and minor versions.
1378+
*
1379+
* @param {String} version Release version (e.g. '3.1.0').
1380+
* @return {Object} Object with major and minor. Returns false if invalid version.
1381+
*/
1382+
function getMajorAndMinor(version) {
1383+
var match = version.match(/(\d)+(?:\.(\d)+)?(?:\.(\d)+)?/);
1384+
if (!match || !match[1]) {
1385+
// Invalid version.
1386+
return false;
1387+
}
1388+
1389+
return {
1390+
major: match[1] + '.' + (match[2] || '0'),
1391+
minor: parseInt(match[3] || 0, 10)
1392+
};
1393+
}
1394+
1395+
/**
1396+
* Given a release version, return the next major version number.
1397+
*
1398+
* @param {String} version Release version (e.g. '3.1.0').
1399+
* @return {Number} Next major version number.
1400+
*/
1401+
function getNextMajorVersionNumber(version) {
1402+
var data = getMajorAndMinor(version),
1403+
position,
1404+
releases = Object.keys(moodleReleases);
1405+
1406+
if (!data) {
1407+
// Invalid version.
1408+
return 0;
1409+
}
1410+
1411+
position = releases.indexOf(data.major);
1412+
1413+
if (position == -1 || position == releases.length -1) {
1414+
// Major version not found or it's the last one. Use the last one.
1415+
return moodleReleases[releases[position]];
1416+
}
1417+
1418+
return moodleReleases[releases[position + 1]];
1419+
}
1420+
12931421
/**
12941422
* Get cache ID.
12951423
*

0 commit comments

Comments
 (0)