all files / js/administration/ userAdministration.controller.js

26.87% Statements 18/67
16.67% Branches 2/12
6.67% Functions 2/30
26.87% Lines 18/67
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237                                                                                                                                                                                                                                                                                                                                                                                                                                                     
/**
 * Created by eknelson17 on 4/2/15.
 */
 
angular
    .module('coopEval')
    .controller('UserAdministrationController', UserAdministrationController);
 
function UserAdministrationController($scope, $rootScope, $location, $modal, administrationService) {
 
    $scope.colleges = [];
    $scope.selectedCollege = null;
    $scope.departments = [];
    $scope.selectedDepartment = null;
    $scope.deptUsers = [];
 
    Iif ($location.path() == '/administration/user-management-dept') {
        administrationService.getColleges()
            .success(function (result) {
                $scope.colleges = result;
                $scope.selectedCollege = $scope.colleges[0];
 
                administrationService.getDepartments($scope.selectedCollege.collegeId)
                    .success(function (result) {
                        $scope.departments = result;
                        $scope.selectedDepartment = $scope.departments[0];
 
                        administrationService.getDeptUsers($scope.selectedDepartment.departmentId)
                            .success(function (result) {
                                $scope.deptUsers = result;
                            })
                            .error(function () {
                                $rootScope.message = {
                                    type: 'danger',
                                    content: 'An error occurred when loading department users.',
                                    location: $location.path()
                                };
                            });
                    });
 
                //$scope.getDepartments($scope.selectedCollege.collegeId);
                //$scope.getDeptUsers($scope.selectedDepartment.departmentId);
            });
    } else Iif ($location.path() == '/administration/user-management-admin') {
        administrationService.getAdmins()
            .success(function (result) {
                $scope.adminUsers = result;
            })
            .error(function () {
                // Todo: Error handling - get error from server to be more specifc?
                $rootScope.message = {
                    type: 'danger',
                    content: 'An error occurred when loading administrative users.',
                    location: $location.path()
                };
            });
    }
 
    $scope.$watch('selectedCollege', function () {
        if ($scope.selectedCollege) {
            $scope.getDepartments($scope.selectedCollege.collegeId);
        }
    });
 
    $scope.$watch('selectedDepartment', function () {
        if ($scope.selectedDepartment) {
            $scope.getDeptUsers($scope.selectedDepartment.departmentId);
        }
    });
 
    /**
     * Opens a modal window to create a new administrator.
     */
    $scope.addNewAdminUser = function (size) {
        var modalInstance = $modal.open({
            templateUrl: 'partials/administration/add-admin-modal.html',
            controller: 'AddAdminUserModalController',
            size: size
        });
 
        modalInstance.result.then(function (adminUser) {
            administrationService.addAdmin(adminUser)
                .success(function (result) {
                    $rootScope.message = {
                        type: 'success',
                        content: 'New administrator created as ' +
                            result.name + ' (' +
                            result.userName + ').',
                        location: $location.path()
                    };
                    $scope.adminUsers.push(result);
                })
                .error(function(data, status, headers, config) {
                    // TODO: Handle error - want to add an error coming from the server to be more specific?
                    $rootScope.message = {
                        type: 'danger',
                        content: 'New administrator could not be created.',
                        location: $location.path()
                    };
                });
        });
    };
 
    /**
     * Deletes the given adminUser from the database.
     * @param adminUser The User object to be deleted
     */
    $scope.removeAdminUser = function (adminUser) {
        administrationService.removeAdminUser(adminUser.administrationUserId)
            .success(function (result) {
                $rootScope.message = {
                    type: 'success',
                    content: 'Successfully deleted administrator ' +
                        adminUser.name + ' (' +
                        adminUser.userName + ').',
                    location: $location.path()
                };
                for (var i = 0; i < $scope.adminUsers.length; i++) {
                    if ($scope.adminUsers[i].administrationUserId == adminUser.administrationUserId) {
                        $scope.adminUsers.splice(i, 1);
                        break;
                    }
                }
            })
            .error(function() {
                $rootScope.message = {
                    type: 'danger',
                    content: adminUser.name + ' (' +
                        adminUser.userName + ') could not be removed because of an unknown error.',
                    location: $location.path()
                };
            });
    };
 
    /**
     * Get all the departments in a given college
     * @param collegeId - college id
     */
    $scope.getDepartments = function(collegeId) {
        administrationService.getDepartments(collegeId)
            .success(function (result) {
                $scope.departments = result;
                $scope.selectedDepartment = $scope.departments[0];
            });
    };
 
    /**
     * Get all users in a given department
     * @param deptId - department Id
     */
    $scope.getDeptUsers = function(deptId) {
        administrationService.getDeptUsers(deptId)
            .success(function (result) {
                $scope.deptUsers = result;
            })
            .error(function () {
                $rootScope.message = {
                    type: 'danger',
                    content: 'An error occurred when loading department users.',
                    location: $location.path()
                };
            });
    };
 
    /**
     * Opens a modal window to create a new department user.
     */
    $scope.addNewDeptUser = function (size) {
        var modalInstance = $modal.open({
            templateUrl: 'partials/administration/add-dept-user-modal.html',
            controller: 'AddDeptUserModalController',
            size: size,
            resolve: {
                college: function() {
                    return $scope.selectedCollege;
                },
                department: function() {
                    return $scope.selectedDepartment;
                }
            }
        });
 
        modalInstance.result.then(function (deptUser) {
            administrationService.addDeptUser(deptUser.department.departmentId, deptUser)
                .success(function (result) {
                    $rootScope.message = {
                        type: 'success',
                        content: 'The new department user ' + result.name + ' (' +
                        result.userName + ') was created successfully.',
                        location: $location.path()
                    };
                    $scope.deptUsers.push(result);
                })
                .error(function(data, status, headers, config) {
                    // TODO: Error handling - want to add an error coming from the server to be more specific?
                    $rootScope.message = {
                        type: 'danger',
                        content: 'New department user could not be created.',
                        location: $location.path()
                    };
                });
        });
    };
 
    /**
     * Deletes the given deptUser from the database.
     * @param deptUser The User object to be deleted
     */
    $scope.removeDeptUser = function (deptUser) {
        administrationService.removeDeptUser($scope.selectedDepartment.departmentId, deptUser.departmentUserId)
            .success(function () {
                $rootScope.message = {
                    type: 'success',
                    content: 'The department user ' + deptUser.name + ' (' +
                    deptUser.userName + ') was successfully removed from the system.',
                    location: $location.path()
                };
                for (var i = 0; i < $scope.deptUsers.length; i++) {
                    if ($scope.deptUsers[i].departmentUserId == deptUser.departmentUserId) {
                        $scope.deptUsers.splice(i, 1);
                        break;
                    }
                }
            })
            .error(function(data, status, headers, config) {
                // TODO: Error handling - want to add an error coming from the server to be more specific?
                $rootScope.message = {
                    type: 'danger',
                    content: 'The department user ' + deptUser.name + ' (' +
                    deptUser.userName + ') could not be removed from the system ' +
                    'because of an unknown error.',
                    location: $location.path()
                };
            });
    };
 
}