(function () { 'use strict'; var app = angular.module('skillsPassportsManager'); var templatePath = modulesSharedResourcesUrl + 'Modules/SkillsPassportsAdmin/Views/'; app.directive('jobRoleTrainingRequirementsManager', ['config', '$location', 'skillsPassportsDataContext', 'myUsersDataContext', 'common', '$modal', 'OrganisationAdminService', '$rootScope', 'terminology', function (config, $location, skillsPassportsDataContext, myUsersDataContext, common, $modal, OrganisationAdminService, $rootScope, terminology) { return { restrict: 'E', templateUrl: templatePath + 'jobroletrainingrequirements.html', link: link }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var log = getLogFn('branding'); var logSuccess = getLogFn('branding', "success"); $scope.topLevelOrg = ""; activate(); function activate() { getJobRoleTrainingRequirements(); //get the data for the listing that shows the job roles with the number of training requirements against each } function getJobRoleTrainingRequirements() { OrganisationAdminService.getTopLevelOrg().then(function (data) { $scope.topLevelOrg = data.id; myUsersDataContext.getOrganisationsFullFlat(config.jobRoleTypeId).then(function (jobRoles) { log('Got job roles'); $scope.jobRoles = jobRoles; myUsersDataContext.getOrganisationsFullFlat(0).then(function (orgs) { for (var i = 0; i < $scope.jobRoles.length; i++) { for (var j = 0; j < orgs.length; j++) { if ($scope.jobRoles[i].parentId === orgs[j].id) $scope.jobRoles[i].organisation = orgs[j]; } } //Get the training rqeuirements for each of these job roles var jobRoleIds = []; $.each(jobRoles, function (index) { jobRoleIds.push(jobRoles[index].id); }); skillsPassportsDataContext.getJobRoleTrainingRequirementsForJobRoles(jobRoleIds).then(function (trainingRequirements) { log('Got job role training requirements'); //Match up the job roles to their training requirements on the ui for (var i = 0; i < $scope.jobRoles.length; i++) { $scope.jobRoles[i].trainingRequirements = []; for (var j = 0; j < trainingRequirements.length; j++) { if ($scope.jobRoles[i].id === trainingRequirements[j].jobRoleId) $scope.jobRoles[i].trainingRequirements.push(trainingRequirements[j]); } } }) }); }); }); } //Click on a job role to edit its training requirements $scope.editJobRoleTrainingRequirements = function (jobRole) { $location.path('/skillspassport/jobroles/' + jobRole.id + '/trainingrequirements'); } //TODO: perhaps have a modal to show selected requirements readonly from listings page? function showCreateUpdateUi(jobRole, parentId, notOriginal) { $modal.open({ templateUrl: templatePath + 'editjobroletrainingrequirements.html', controller: editJobRoleTrainingRequirementsController, size: 'sm', backdrop: 'static', resolve: { jobRole: function () { return jobRole; }, orgId: function () { return $scope.organisationId; }, topLevelOrg: function () { return $scope.topLevelOrg; }, notOriginal: function () { return notOriginal; }, terminology: function () { return terminology; } } }); } //controller to handle the edit job role training requirements modal view var editJobRoleTrainingRequirementsController = function ($scope, $modalInstance, jobRole, orgId, topLevelOrg, notOriginal, terminology) { $scope.organisationId = orgId; $scope.topLevelOrg = topLevelOrg; $scope.parentOrganisationId = ""; $scope.terminology = terminology; if (jobRole) { //must be updating myUsersDataContext.getOrganisationDetails(jobRole.id).then(function (data) { $scope.jobRole = data; $scope.parentOrganisationId = data.parentId; $scope.loaded = true; }); $scope.isEdit = true; } else { //must be adding a new job role $scope.jobRole = {}; $scope.isEdit = false; $scope.loaded = true; } function getorganisations() { myUsersDataContext.getOrganisationsFullFlat(0).then(function (data) { $scope.organisations = data; }); } getorganisations(); //ok clicked - save the job role training requirements $scope.ok = function () { saveJobRoleTrainingRequirements(); }; function saveJobRoleTrainingRequirements() { //TODO: save the training requirements for the job role $scope.saving = true; $scope.jobRole.ordinal = 0; if ($scope.jobRole.id) { myUsersDataContext.editOrganisation($scope.jobRole.id, $scope.jobRole).then(function (result) { logSuccess("Successfully updated"); myUsersDataContext.associateGroupWithOrg([{ id: $scope.jobRole.id }], $scope.parentOrganisationId).then(function (data) { //close the modal $modalInstance.close(); //broadcast the event so the job roles list refreshesupdateOrg $rootScope.$broadcast('UpdateJobRoles'); }); }); } else { myUsersDataContext.createJobRole($scope.jobRole).then(function (result) { logSuccess("Successfully created"); myUsersDataContext.associateGroupWithOrg([{ id: result }], $scope.parentOrganisationId).then(function (data) { //close the modal $modalInstance.close(); //broadcast the event so the job roles list refreshesupdateOrg $rootScope.$broadcast('UpdateJobRoles'); }); }); } } $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; }; $scope.$on('UpdateJobRoleTrainingRequirements', function () { activate(); }); } }]); app.directive('editJobRoleTrainingRequirements', ['$rootScope', 'config', 'skillsPassportsDataContext','coursesDataContext', 'user', 'usersAdminService', 'common', '$location', 'myUsersDataContext', 'OrganisationAdminService', 'rolesAdminDataContext', 'usersAdminDataContext', '$q', 'fullScreenLoaderContext', 'usersAdminConfig', 'terminology', '$injector', function ($rootScope, config, skillsPassportsDataContext, coursesDataContext, user, usersAdminService, common, $location, myUsersDataContext, OrganisationAdminService, rolesAdminDataContext, usersAdminDataContext, $q, fullScreenLoaderContext, usersAdminConfig, terminology, $injector) { return { restrict: 'E', templateUrl: templatePath + 'editjobroletrainingrequirements.html', scope: { jobRoleId: '=', organisationId: '=', singleRole: '=', canEdit: '=' }, 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"); $scope.saving = false; $scope.selectedRole = {}; $scope.terminology = terminology; $scope.relationshipTypes = []; /* **** * EXISTINGREQUIREMENTS populates the left hand panel with already selected * courses that are required on a job role * */ $scope.existingRequirements = []; /* **** * AVAILABLECOURSES populates the right hand panel with selectable course * to be made requirements on a job role * */ $scope.availableCourses = []; /* ******* * SELECTEDREQUIREMENTIDS is the final array that stores the course ids as requirements for this job role * - selecting and deselecting courses should add to and remove course ids from to this accordingly * - it's initialised with existing training requirement course ids * */ $scope.selectedRequirementIds = []; /* **** * Call to get job role data * */ getJobRoleDetails(); /* **** * Call to get existing training requirements * for this job role * */ getExistingRequirements(); /* **** * Function to request job role data * */ function getJobRoleDetails() { myUsersDataContext.getOrganisationDetails($scope.jobRoleId).then(function (data) { getOrgInfo(data.parentId).then(function (org) { /* **** * The JOBROLE details and its existing * course training requirements * */ $scope.jobRole = { "id": data.id, "name": data.name, "identifier": data.reference, "description": data.description, "dateCreated": data.createdOn, "organisation": { //note: a job role is a child group "id": org.id, "name": org.name } } }); }); } /* **** * Function to request existing training requirements data * */ function getExistingRequirements() { $scope.existingRequirements = []; skillsPassportsDataContext.getJobRoleTrainingRequirementsForJobRole($scope.jobRoleId).then(function (requirements) { /* **** * EXISTINGREQUIREMENTS populates the left hand panel with already selected * courses that are required on a job role * */ $.each(requirements, function (index) { $scope.existingRequirements.push({ "course": { "name": requirements[index].course.title, "identifier": requirements[index].course.code, "id": requirements[index].course.id, "description": requirements[index].course.description, "selected": false }, "id": requirements[index].id, "selected": true, }); }); /* **** * Set up available courses based on existing requirements * */ getCourses(); /* **** * Initialisation to set existing requirement course ids * on the array that will be saved later - the 'true' list if you like * */ $scope.selectedRequirementIds = []; $.each($scope.existingRequirements, function (index) { $scope.selectedRequirementIds.push($scope.existingRequirements[index].course.id); }); /* **** * When data is ready stop loading the ui * */ $scope.coursesLoaded = true; }); } /* **** * Function to fetch available courses data * */ function getCourses() { OrganisationAdminService.getCurrentOrg().then(function (data) { $scope.topLevelOrg = data.id; return coursesDataContext.getCoursesIncludingParents(data.id).then(function (courses) { /* **** * AVAILABLECOURSES populates the right hand panel with selectable course * to be made requirements on a job role * */ $scope.availableCourses = []; $.each(courses, function (index) { //If in 'already selected' list don't push to 'available list' if ($scope.existingRequirements.find(function(er) { er.course.id == courses[index].id }) == null) $scope.availableCourses.push({ "name": courses[index].title, "identifier": courses[index].code, "id": courses[index].id, "description": courses[index].description, "selected": false }); }); /* **** * When data is ready stop loading the ui * */ $scope.coursesLoaded = true; }); }); } /* **** * When an existing course requirement is clicked * handle it here * */ $scope.selectRequirement = function (requirement) { requirement.course.selected = requirement.selected; addOrRemoveCourse(requirement.course); } /* **** * When an available course is selected * handle it here * */ $scope.selectCourse = function (course) { addOrRemoveCourse(course); } /* **** * When the update button is clicked * handle it here * */ $scope.saveTrainingRequirements = function () { save(); } /* **** * Function to add and remove course ids from the saving list * */ function addOrRemoveCourse(course) { if (course.selected) { //Add to the selected course ids var courseItemIndex = $scope.selectedRequirementIds.indexOf(course.id); if (courseItemIndex - 1) { $scope.selectedRequirementIds.push(course.id); } } else { //Remove from selected course ids var courseItemIndex = $scope.selectedRequirementIds.indexOf(course.id); $scope.selectedRequirementIds.splice(courseItemIndex, 1); } } /* **** * Function to save the course ids list as training requirements * */ function save() { $scope.saving = true; skillsPassportsDataContext.updateJobRoleTrainingRequirements($scope.jobRoleId, $scope.selectedRequirementIds).then(function () { logSuccess("Successfully updated"); //$location.path('/skillspassport/jobroles/trainingrequirements'); $scope.saving = false; getExistingRequirements(); }, function () { $scope.saving = false; logError("Problem updating"); }); } function getOrgInfo(orgId) { return OrganisationAdminService.getOrganisationDetails(orgId, 0, 2).then(function (data) { return data; }); } } }]); })();