(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; }); rolesAdminDataContext.getAllRoles("MS").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); var appCode = role.roleAppCodes[0].appCodeId; if (appCode) $location.path("/roles/manage/" + appCode); else $location.path("/roles/manage/ms"); }; getRoles(); } }, ]); app.directive("manageRole", [ "rolesAdminDataContext", "rolesAdminService", "OrganisationAdminService", "$rootScope", "common", "$location", "$window", "$modal", "$routeParams", "$q", "config", "terminology", "tenantService", function ( rolesAdminDataContext, rolesAdminService, OrganisationAdminService, $rootScope, common, $location, $window, $modal, $routeParams, $q, config, terminology, tenantService ) { 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; //Special check on folders functions to show a 'root' folders helptext textarea if any selected showHideFoldersRootArea(); } console.log($scope.selectedFunctions); }, true ); // Initialize all the available function arrays $scope.selectedFunctions = []; $scope.userFunctions = []; $scope.profileFunctions = []; $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 = []; $scope.folderFunctions = []; $scope.stuffFunctions = []; $scope.showcasesFunctions = []; $scope.skillsPassportFunctions = []; $scope.personalSettingsFunctions = []; $scope.helpTextPlaceHolder = "Optionally add some help text to the menu option"; $scope.helpTextHover = "Optional help text to display on the menu option"; $scope.helpTextMinLength = 1; $scope.helpTextMaxLength = 10000; $scope.foldersRootFunction = { name: "RootManageFolders", shortCode: "manage_folders", }; /* * If any of the folder functions have been selected then the root folders area can be shown * */ function showHideFoldersRootArea() { var show = false; $scope.selectedFunctions.forEach(function (functionId) { if ( $scope.folderFunctions.find((fn) => fn.id == functionId) != null ) { show = true; } }); $scope.showFoldersRoot = show; } // Get the roles for myshowcase.me admin function getAdminRoleFunctions() { rolesAdminDataContext .getAllRoleFunctions(config.appCode) .then(function (data) { $scope.rootFunctions = data; $scope.rootFunctions.push($scope.foldersRootFunction); //Specifying manage_folders as it's not in the db? if ($scope.role && $scope.role.organisationId) { tenantService .getAllRoleFunctionHelpTexts($scope.role) .then(function (data) { setFunctions(data, true); }); } else { setFunctions(null); } }); } function setFunctions(helpTexts, hideHiddenRoles) { // Set all available functions into corresponding arrays var checker = rolesAdminService.fnChecker(); $scope.rootFunctions.forEach(function (fn) { if (hideHiddenRoles) { //There are only certain areas that display in the sidebar menu - filter out those that aren't if ( (fn.shortCode.startsWith("manage_") || fn.shortCode.startsWith("profiles") || fn.shortCode.contains("_access") || fn.shortCode.contains("access_") || (fn.shortCode.contains("progress") && !fn.shortCode.contains("comment") && !fn.shortCode.contains("mark") && !fn.shortCode.contains("export") && !fn.shortCode.contains("complete")) || fn.shortCode.contains("activities_manage") || fn.shortCode.contains("activities_view") || fn.shortCode.contains("activities_add") || fn.shortCode.contains("grading") || fn.shortCode.startsWith("tasks") || fn.shortCode.contains("showboards_manage") || fn.shortCode.contains("showcaseinbox_showcases")) && !fn.shortCode.contains("folders_") ) { fn.canHaveHelpText = true; } } else { fn.canHaveHelpText = true; } if (helpTexts) { var helpText = helpTexts.find((x) => x.functionIds.find((y) => y === fn.id || y === fn.shortCode) ); if (helpText) fn.helpText = helpText.value; } if (fn.shortCode == "roles_allocate") $scope.userFunctions.push(fn); else if (checker.isUsers(fn)) $scope.userFunctions.unshift(fn); else if (checker.isProfiles(fn)) $scope.profileFunctions.push(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); else if (checker.isFolders(fn)) $scope.folderFunctions.push(fn); else if (checker.isStuff(fn)) $scope.stuffFunctions.push(fn); else if (checker.isShowcases(fn)) $scope.showcasesFunctions.push(fn); else if (checker.isDashboard(fn)) $scope.dashboardFunctions.push(fn); else if (checker.isSkillsPassport(fn)) $scope.skillsPassportFunctions.push(fn); else if (checker.isPersonalSettings(fn)) $scope.personalSettingsFunctions.push(fn); //If any of the folder functions are checked, show the root folders area showHideFoldersRootArea(); }); // 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 console.log(data); $scope.rootFunctions = data; if ($scope.role && $scope.role.organisationId) { tenantService .getAllRoleFunctionHelpTexts($scope.role) .then(function (data) { setFunctions(data, false); }); } else { setFunctions(null); } $scope.roleFunctionsLoaded = true; }); } // If we are managing instead of creating get the role $scope.role = rolesAdminService.getEditingRole(); $scope.helpTexts = []; if (!$scope.role) { // If role is blank we are creating a new one $scope.role = rolesAdminService.createBlankRole(); toggleSkillsPassport(); } 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 ); } toggleSkillsPassport(); } /* * Set in config whether the current user's org has skills passport enabled or not */ function toggleSkillsPassport() { $scope.enableSkillsPassport = false; var orgs = JSON.parse($scope.config.orgs); if (orgs) { var firstOrgId = orgs[0]; if (firstOrgId) { tenantService .getOrgUISettings(firstOrgId) .then(function (settings) { for (var i = 0; i < settings.length; i++) { if (settings[i].key === "enableTrainingRecords") $scope.enableSkillsPassport = settings[i].value === "yes"; } }); } } } $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 () { if ($scope.appCode != "MS" && $scope.appCode != "MS") { $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 && parentFn) $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 hasProfiles = 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; var hasFolders = false; var hasSkillsPassport = 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 .filter((s) => s != null) .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("profiles")) { setParentFn(shortCodes.profiles); hasProfiles = 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("badgeReporting")) { setParentFn(shortCodes.badgeReporting); hasBadgeReporting = 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; } else if (s.includes("folders") && s !== shortCodes.folders) { setParentFn(shortCodes.folders); hasFolders = true; } else if ( s.includes("skillspassport") && s !== shortCodes.accessSkillsPassport ) { setParentFn(shortCodes.accessSkillsPassport); hasSkillsPassport = true; } }); if (!hasUsers) removeParentFn(shortCodes.users); if (!hasProfiles) removeParentFn(shortCodes.profiles); 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 (!hasFolders) removeParentFn(shortCodes.folders); if (!hasSkillsPassport) removeParentFn(shortCodes.accessSkillsPassport); 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 + " 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; } }); } saveHelpTexts().then(function (helpData) { handleSuccess(); }); }) .catch(function (reason) { $scope.isSaving = false; logWarning( "Failed to save role " + $scope.role.name + "! 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) { $scope.role.id = data.id; saveHelpTexts().then(function (helpData) { // Successfully created role so notify the user logSuccess( "Role " + $scope.role.name + " created" ); // Redirect the user back to roles list $location.path("/roles"); }); } else { $scope.isSaving = false; logWarning( "Failed to save role " + $scope.role.name + "! Role names must be unique!" ); } }); }); } }; // Save help texts for the functions on the current role function saveHelpTexts() { var toSave = []; //Specific code for folders which is set up differently in roles/functions var rootFoldersManageOrgsFunction = $scope.rootFunctions.filter( function (fn) { return fn.name === "ManageOrganisationalFolders"; } ); var rootFoldersManageFunction = $scope.rootFunctions.filter(function ( fn ) { return fn.name === "RootManageFolders"; }); if (rootFoldersManageFunction[0] && rootFoldersManageOrgsFunction[0]) rootFoldersManageOrgsFunction[0].helpText = rootFoldersManageFunction[0].helpText; angular.forEach( $scope.rootFunctions.filter( (x) => typeof x.helpText !== "undefined" && x.helpText !== "" && x.helpText !== "\n " && //Rich text adds this by default but we don't want to save this x.helpText !== "\n " && //Rich text adds this by default but we don't want to save this x.name !== "RootManageFolders" ), //We don't want to save the root folders text function (item, index) { toSave.push({ objectIds: [$scope.role.id], functionIds: [item.shortCode], value: item.helpText, organisationId: $scope.role.organisationId, }); } ); return tenantService.saveRoleFunctionHelpTexts( toSave, $scope.role.organisationId ); } // 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(); } } }, ]); })();