(function () { 'use strict'; var app = angular.module('rolesAdmin'); var templatePath = modulesSharedResourcesUrl + 'Modules/MyRolesAdmin/Views/'; app.directive('rolesList', ['config', 'rolesAdminDataContext', 'rolesAdminService', 'common', '$location', '$modal', '$q', function (config, rolesAdminDataContext, rolesAdminService, common, $location, $modal, $q) { return { restrict: 'E', templateUrl: templatePath + 'roleslist.html', link: link }; function link($scope, elem, attrs) { // Set the log notifiers up var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn("rolesAdmin", "success"); var logError = getLogFn("rolesAdmin", "error"); // Set the default ordering $scope.orderByPredicate = 'name'; $scope.currentAppCode = config.appCode; // Make sure we clear any previously selected role rolesAdminService.setEditingRole(null); // Get all the roles for both app code 'MS' and 'MSA' function getRoles() { $scope.roles = []; // Get roles for MSA (Myshowcase.me Admin) rolesAdminDataContext.getAllRoles(config.appCode).then(function (data) { for (i in data) { $scope.roles.push(data[i]); } // Roles have been loaded $scope.adminRolesLoaded = true; }); } // Update an existing role $scope.updateRole = function (role) { rolesAdminDataContext.updateRole(role).then(function (data) { // Successfully updated role so notify the user logSuccess("Role " + role.name + " updated"); }); } // Delete an existing role $scope.removeRole = function (role) { rolesAdminDataContext.deleteRole(role).then(function (data) { // Successfully deleted role so notify the user logSuccess("Role " + role.name + " Deleted"); // Refresh the roles list getRoles(); }); } // Manage an existing role $scope.manageRole = function (role) { // Set the selected role and navigate user to manage view rolesAdminService.setEditingRole(role); $location.path('/roles/manage/' + role.roleAppCodes[0].appCodeId); } getRoles(); } }]); app.directive('manageRole', [ 'rolesAdminDataContext', 'rolesAdminService', 'OrganisationAdminService', '$rootScope', 'common', '$location', '$window', '$modal', '$routeParams', '$q', 'config', 'terminology', function ( rolesAdminDataContext, rolesAdminService, OrganisationAdminService, $rootScope, common, $location, $window, $modal, $routeParams, $q, config, terminology) { return { restrict: 'E', templateUrl: templatePath + 'managerole.html', link: link }; function link($scope, elem, attrs) { // Set the log notifiers up var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn("rolesAdmin", "success"); var logError = getLogFn("rolesAdmin", "error"); var logWarning = getLogFn("rolesAdmin", "warning"); $scope.config = config; $scope.terminology = terminology; // Set changes made variable $scope.changesMadeToRole = false; // Get the appcode from URI $scope.appCode = $routeParams.appCode; // If no app code present redirect user to roles list if (!$scope.appCode) { $location.path('/roles'); } // Function to go back to the roles list $scope.goBack = function () { $location.path('/roles'); }; // Watch for changes to the current role model $scope.$watchCollection('role', function (newValue, oldValue) { if (newValue !== oldValue) { $scope.changesMadeToRole = true; } }, true); // Watch for changes to the current role functions model $scope.$watchCollection('selectedFunctions', function (newValue, oldValue) { if (newValue !== oldValue) { $scope.changesMadeToRole = true; } console.log($scope.selectedFunctions) }, true); // Initialize all the available function arrays $scope.selectedFunctions = [] $scope.userFunctions = []; $scope.formFunctions = []; $scope.activityFunctions = []; $scope.badgeFunctions = []; $scope.frameworkFunctions = []; $scope.progressFunctions = []; $scope.apprenticeshipsFunctions = []; $scope.gradingFunctions = []; $scope.taskFunctions = []; $scope.showcaseFunctions = []; $scope.showboardFunctions = []; $scope.subscriptionFunctions = []; $scope.organisationFunctions = []; $scope.employersFunctions = []; $scope.rulesFunctions = []; $scope.badgeReportingFunctions = []; $scope.backpackFunctions = []; $scope.collectionsFunctions = []; $scope.badgeDashboardFunctions = []; $scope.townSignaturesFunctions = []; $scope.analyticsFunctions = []; $scope.interventionsFunctions = []; $scope.forecastingFunctions = []; $scope.regionstownsFunctions = []; $scope.myformsFunctions = []; $scope.tutorialsFunctions = []; $scope.dashboardFunctions = []; $scope.workflowsFunctions = []; // Get the roles for myshowcase.me admin function getAdminRoleFunctions() { rolesAdminDataContext.getAllRoleFunctions(config.appCode).then(function (data) { console.log(data); $scope.rootFunctions = data; // Set all available functions into corresponding arrays var checker = rolesAdminService.fnChecker(); $scope.rootFunctions.forEach(function (fn) { if (fn.shortCode == 'roles_allocate') $scope.userFunctions.push(fn); else if (checker.isUsers(fn)) $scope.userFunctions.unshift(fn); else if (checker.isForms(fn)) $scope.formFunctions.push(fn); else if (checker.isActivities(fn)) $scope.activityFunctions.push(fn); else if (checker.isBadges(fn)) $scope.badgeFunctions.push(fn); else if (checker.isFrameworks(fn)) $scope.frameworkFunctions.push(fn); else if (checker.isProgress(fn)) $scope.progressFunctions.push(fn); else if (checker.isApprenticeships(fn)) $scope.apprenticeshipsFunctions.push(fn); else if (checker.isGrading(fn)) $scope.gradingFunctions.push(fn); else if (checker.isTasks(fn)) $scope.taskFunctions.push(fn); else if (checker.isShowcaseInbox(fn)) $scope.showcaseFunctions.push(fn); else if (checker.isShowboards(fn)) $scope.showboardFunctions.push(fn); else if (checker.isSubscription(fn)) $scope.subscriptionFunctions.push(fn); else if (checker.isBadgesDashboard(fn)) $scope.badgeDashboardFunctions.push(fn); else if (checker.isOrganisations(fn)) $scope.organisationFunctions.push(fn); else if (checker.isEmployers(fn)) $scope.employersFunctions.push(fn); else if (checker.isRules(fn)) $scope.rulesFunctions.push(fn); else if (checker.isBadgeReporting(fn)) $scope.badgeReportingFunctions.push(fn); else if (checker.isBackpack(fn)) $scope.backpackFunctions.push(fn); else if (checker.isCollections(fn)) $scope.collectionsFunctions.push(fn); else if (checker.isTownSignatures(fn)) $scope.townSignaturesFunctions.push(fn); else if (checker.isAnalytics(fn)) $scope.analyticsFunctions.push(fn); else if (checker.isInterventions(fn)) $scope.interventionsFunctions.push(fn); else if (checker.isForecasting(fn)) $scope.forecastingFunctions.push(fn); else if (checker.isRegionsTowns(fn)) $scope.regionstownsFunctions.push(fn); else if (checker.isMyForms(fn)) $scope.myformsFunctions.push(fn); else if (checker.isTutorials(fn)) $scope.tutorialsFunctions.push(fn); else if (checker.isMaybeDashboard(fn)) $scope.dashboardFunctions.push(fn); else if (checker.isWorkflows(fn)) $scope.workflowsFunctions.push(fn); }); // We now have all the functions $scope.roleFunctionsLoaded = true; // for displaying the allocate roles switch which should only show when access users role is checked var accessUsers = $scope.rootFunctions.filter(function (fn) { return fn.shortCode == 'users_users_access'; })[0]; var rolesAllocate = $scope.rootFunctions.filter(function (fn) { return fn.shortCode == 'roles_allocate'; })[0]; $scope.accessUsersSelected = function () { if ($scope.selectedFunctions.includes(accessUsers.id)) return true; else { for (var i in $scope.selectedFunctions) { if ($scope.selectedFunctions[i] == rolesAllocate.id) { $scope.selectedFunctions.splice(i, 1); break; } } } } }); } // Get the roles for myshowcase.me function getUserRoleFunctions() { rolesAdminDataContext.getAllRoleFunctions('MS').then(function (data) { // We dont know what these are yet // We now have all the functions $scope.roleFunctionsLoaded = true; }); } // If we are managing instead of creating get the role $scope.role = rolesAdminService.getEditingRole(); if (!$scope.role) { // If role is blank we are creating a new one $scope.role = rolesAdminService.createBlankRole(); } else { // Loop through the existing set functions to build the checklist for (i in $scope.role.roleFunctions) { $scope.selectedFunctions.push($scope.role.roleFunctions[i].functionId); } } $scope.checkFunctionIsSelected = function (func, functions) { var fullFunction = functions.filter(function (f) { return f.shortCode === func })[0]; if (!fullFunction) return false; if ($scope.selectedFunctions.includes(fullFunction.id)) return false; return true; } // Save a role $scope.saveRole = function () { $scope.role.isAdmin = true; $scope.isSaving = true; $scope.role.roleFunctions = []; // Set the app code $scope.role.appCode = $scope.appCode; // Get the selected functions from their ids. var selectedFunctions = $scope.selectedFunctions.map(function (id) { return $scope.rootFunctions.filter(function (fn) { return fn.id === id; })[0]; }); // Finds the function for it's shortcode, then checks if it exists in role.roleFunctions, if not add it to role's functions var setParentFn = function (shortCode) { var parentFn = $scope.rootFunctions.filter(function (fn) { return shortCode == fn.shortCode; })[0]; var exists = $scope.role.roleFunctions.filter(function (fn) { if (parentFn) return fn.functionId == parentFn.id; return false; })[0]; if (!exists) $scope.role.roleFunctions.push({ functionId: parentFn.id }); }; var removeParentFn = function (shortCode) { var parentFn = $scope.rootFunctions.filter(function (fn) { return shortCode == fn.shortCode; })[0]; var exists = $scope.role.roleFunctions.filter(function (fn) { if (parentFn) return fn.functionId == parentFn.id; return false; })[0]; if (exists) $scope.role.roleFunctions = $scope.role.roleFunctions.filter(function (fn) { return fn.functionId != parentFn.id; }); }; var shortCodes = rolesAdminService.parentShortCodes(); var hasUsers = false; var hasForms = false; var hasActivities = false; var hasBadges = false; var hasFrameworks = false; var hasProgress = false; var hasApprenticeships = false; var hasGrading = false; var hasTasks = false; var hasShowcaseInbox = false; var hasShowboards = false; var hasSubscriptions = false; var hasBadgesDashboard = false; var hasOrganisations = false; var hasEmployers = false; var hasRules = false; var hasBadgeReporting = false; var hasBackpack = false; var hasCollections = false; var hasAnalytics = false; var hasForecasting = false; var hasWorkflows = false; // Loop through all the selected functions and add them to the role's functions. Add the parent role functions for the functions too. selectedFunctions.forEach(function (fn) { var s = fn.shortCode; $scope.role.roleFunctions.push({ functionId: fn.id }); if (s.includes('users') && s !== shortCodes.users) { setParentFn(shortCodes.users); hasUsers = true } else if (s.includes('badges_reporting_access')) { setParentFn(shortCodes.badgeReporting); hasBadgeReporting = true } else if (s.includes('form') && s !== shortCodes.forms && s !== 'access_myforms') { setParentFn(shortCodes.forms); hasForms = true } else if (s.includes('activities') && s !== shortCodes.activities) { setParentFn(shortCodes.activities); hasActivities = true } else if (s.includes('badges') && !s.includes('dashboard') && s !== shortCodes.badges) { setParentFn(shortCodes.badges); hasBadges = true } else if (s.includes('frameworks') && s !== shortCodes.frameworks) { setParentFn(shortCodes.frameworks); hasFrameworks = true } else if (s.includes('progress') && s !== shortCodes.progress) { setParentFn(shortCodes.progress); hasProgress = true } else if (s.includes('apprenticeships') && s !== shortCodes.apprenticeships) { setParentFn(shortCodes.apprenticeships); hasApprenticeships = true } else if (s.includes('grading') && s !== shortCodes.grading) { setParentFn(shortCodes.grading); hasGrading = true } else if (s.includes('tasks') && s !== shortCodes.tasks) { setParentFn(shortCodes.tasks); hasTasks = true } else if (s.includes('showcaseinbox') && s !== shortCodes.showcaseInbox) { setParentFn(shortCodes.showcaseInbox); hasShowcaseInbox = true } else if (s.includes('showboards') && s !== shortCodes.showboards) { setParentFn(shortCodes.showboards); hasShowboards = true } else if (s.includes('subscriptions') && s !== shortCodes.subscriptions) { setParentFn(shortCodes.subscriptions); hasSubscriptions = true } else if (s.includes('badgesDashboard')) { setParentFn(shortCodes.badgesDashboard); hasBadgesDashboard = true } else if (s.includes('organisations')) { setParentFn(shortCodes.organisations); hasOrganisations = true } else if (s.includes('employers')) { setParentFn(shortCodes.employers); hasEmployers = true } else if (s.includes('rules') && s !== shortCodes.rules) { setParentFn(shortCodes.rules); hasRules = true } else if (s.includes('backpack')) { setParentFn(shortCodes.backpack); hasBackpack = true } else if (s.includes('collections')) { setParentFn(shortCodes.collections); hasCollections = true } else if (s.includes('analytics') && s !== shortCodes.analytics) { setParentFn(shortCodes.analytics); hasAnalytics = true } else if (s.includes('forecasting') && s !== shortCodes.forecasting) { setParentFn(shortCodes.forecasting); hasForecasting = true } else if (s.includes('workflows') && s !== shortCodes.workflows) { setParentFn(shortCodes.workflows); hasWorkflows = true } }); if (!hasUsers) removeParentFn(shortCodes.users) if (!hasForms) removeParentFn(shortCodes.forms); if (!hasActivities) removeParentFn(shortCodes.activities); if (!hasBadges) removeParentFn(shortCodes.badges); if (!hasFrameworks) removeParentFn(shortCodes.frameworks); if (!hasProgress) removeParentFn(shortCodes.progress); if (!hasApprenticeships) removeParentFn(shortCodes.apprenticeships); if (!hasGrading) removeParentFn(shortCodes.grading); if (!hasTasks) removeParentFn(shortCodes.tasks); if (!hasShowcaseInbox) removeParentFn(shortCodes.showcaseInbox); if (!hasShowboards) removeParentFn(shortCodes.showboards); if (!hasSubscriptions) removeParentFn(shortCodes.subscriptions); if (!hasBadgesDashboard) removeParentFn(shortCodes.badgesDashboard); if (!hasOrganisations) removeParentFn(shortCodes.organisations) if (!hasEmployers) removeParentFn(shortCodes.employers) if (!hasRules) removeParentFn(shortCodes.rules) if (!hasBadgeReporting) removeParentFn(shortCodes.badgeReporting) if (!hasBackpack) removeParentFn(shortCodes.backpack) if (!hasCollections) removeParentFn(shortCodes.collections) if (!hasAnalytics) removeParentFn(shortCodes.analytics) if (!hasForecasting) removeParentFn(shortCodes.forecasting); if (!hasWorkflows) removeParentFn(shortCodes.workflows); if ($scope.role.id) { // If we have an id then update the existing role var updateRole$ = rolesAdminDataContext.updateRole($scope.role); var getAllocation$ = function () { var myRoles = rolesAdminService.getMyRoles(); if (myRoles) { var existingAllocation = myRoles.filter(function (role) { return role.roleId == $scope.role.id; })[0]; if (existingAllocation) return rolesAdminDataContext.getRoleAllocation($scope.role.id); return $q.resolve(false); } return $q.resolve(false); }; var handleSuccess = function () { logSuccess("Role " + $scope.role.name.toLowerCase() + " updated"); $location.path('/roles'); }; // Update the role updateRole$ // Then check if the user has a role allocation for this role and return it if they do .then(getAllocation$) // Then handle replace the old role allocation with the new updated one or just handle the success .then(function (roleAllocation) { $scope.isSaving = false; if (roleAllocation) { rolesAdminService.getAllRoles().forEach(function (role) { if (role.roleAllocationId == roleAllocation.id) { roleAllocation.organisationName = role.organisationName; role = roleAllocation; }; }); } handleSuccess(); }).catch(function (reason) { $scope.isSaving = false; logWarning("Failed to save role " + $scope.role.name.toLowerCase() + "! Role names must be unique!"); }); } else { // We have no id so must be creating OrganisationAdminService.getTopLevelOrg().then(function (org) { $scope.role.organisationId = org.id; rolesAdminDataContext.createRole($scope.role).then(function (data) { if (data) { // Successfully created role so notify the user logSuccess("Role " + $scope.role.name.toLowerCase() + " created"); // Redirect the user back to roles list $location.path('/roles'); } else { $scope.isSaving = false; logWarning("Failed to save role " + $scope.role.name.toLowerCase() + "! Role names must be unique!"); } }) }); } } // Function to copy a role $scope.copyRole = function () { // We need to remove the current id as we are now creating delete $scope.role.id; // Remove the default disabler delete $scope.role.isDefault; // Set the role as unavailable $scope.role.makeAvailable = true; // If the name is above 43 characters we need to trim to make room for the appended ' - Copy' string if ($scope.role.name.length > 43) { $scope.role.name = $scope.role.name.substring(43, 0) + ' - Copy'; $scope.role.displayName = $scope.role.displayName.substring(43, 0) + ' - Copy'; // Append the ' - Copy' string } else { $scope.role.name = $scope.role.name + ' - Copy'; $scope.role.displayName = $scope.role.displayName + ' - Copy'; } // Set the new copied role rolesAdminService.setEditingRole($scope.role); logSuccess("Role " + $scope.role.name + " copied"); } // We are creating/managing a myshowcase.me admin role so get the available functions if ($scope.appCode == 'MSA') { // Set the page title $scope.title = 'admin'; getAdminRoleFunctions(); } // We are creating/managing a myshowcase.me role so get the available functions if ($scope.appCode == 'MS') { // Set the page title $scope.title = 'user'; getUserRoleFunctions(); } // We are creating/managing a myshowcase.me admin role so get the available functions if ($scope.appCode == 'OB') { // Set the page title $scope.title = 'admin'; getAdminRoleFunctions(); } if ($scope.appCode == 'BDSU') { // Set the page title $scope.title = 'admin'; getAdminRoleFunctions(); } } }]); })();