all files / js/administration/ collegeDepartmentManagement.controller.js

4% Statements 2/50
0% Branches 0/14
0% Functions 0/21
4% Lines 2/50
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                                                                                                                                                                                                                                                                                                                                                                       
/**
 * Created by cklimkowsky on 4/20/15.
 */
angular
    .module('coopEval')
    .controller('CollegeDepartmentManagementController', CollegeDepartmentManagementController);
 
function CollegeDepartmentManagementController($scope, $rootScope, $modal, $location, administrationService) {
 
    if ($location.path() == '/administration/college-management') {
        administrationService.getColleges()
            .success(function (result) {
                $scope.colleges = result;
            });
    } else if ($location.path() == '/administration/department-management') {
        administrationService.getColleges()
            .success(function (result) {
                $scope.colleges = result;
                $scope.selectedCollege = $scope.colleges[0];
            });
    }
 
    $scope.$watch('selectedCollege', function () {
        if ($scope.selectedCollege) {
            administrationService.getDepartments($scope.selectedCollege.collegeId)
                .success(function (result) {
                    $scope.departments = result;
                });
        }
    });
 
    /**
     * Opens a modal window to create a new college.
     */
    $scope.addNewCollege = function (size) {
        var modalInstance = $modal.open({
            templateUrl: 'partials/administration/add-college-modal.html',
            controller: 'AddCollegeModalController',
            size: size
        });
 
        modalInstance.result.then(function (college) {
            administrationService.addCollege(college)
                .success(function (result) {
                    $rootScope.message = {
                        type: 'success',
                        content: 'The new college ' +
                        result.collegeName + ' was created successfully.',
                        location: $location.path()
                    };
                    $scope.colleges.push(result);
                });
        });
    };
 
    /**
     * Deletes the given college from the database.
     * @param college The College object to be deleted
     */
    $scope.removeCollege = function (college) {
        administrationService.removeCollege(college.collegeId)
            .success(function (result) {
                $rootScope.message = {
                    type: 'success',
                    content: 'The college ' + college.collegeName + ' was successfully removed from the system.',
                    location: $location.path()
                };
                for (var i = 0; i < $scope.colleges.length; i++) {
                    if ($scope.colleges[i].collegeId == college.collegeId) {
                        $scope.colleges.splice(i, 1);
                        break;
                    }
                }
            })
            .error(function(data, status, headers, config) {
                if (status == 403) {
                    $rootScope.message = {
                        type: 'danger',
                        content: 'The college ' + college.collegeName + ' could not be removed from the system ' +
                        'because it has one or more evaluations and/or departments associated with it.',
                        location: $location.path()
                    };
                } else {
                    $rootScope.message = {
                        type: 'danger',
                        content: 'The college ' + college.collegeName + ' could not be removed from the system ' +
                        'because of an unknown error.',
                        location: $location.path()
                    };
                }
            });
    };
 
    /**
     * Opens a modal window to create a new department.
     */
    $scope.addNewDepartment = function (size) {
        var modalInstance = $modal.open({
            templateUrl: 'partials/administration/add-department-modal.html',
            controller: 'AddDepartmentModalController',
            size: size,
            resolve: {
                college: function() {
                    return $scope.selectedCollege;
                }
            }
        });
 
        modalInstance.result.then(function (department) {
            administrationService.addDepartment(department)
                .success(function (result) {
                    $rootScope.message = {
                        type: 'success',
                        content: 'The new department ' + result.departmentName + ' (' +
                        result.departmentCode + ') was created successfully.',
                        location: $location.path()
                    };
                    $scope.departments.push(result);
                });
        });
    };
 
    /**
     * Deletes the given department from the database.
     * @param department
     */
    $scope.removeDepartment = function (department) {
        administrationService.removeDepartment(department.departmentId)
            .success(function (result) {
                $rootScope.message = {
                    type: 'success',
                    content: 'The department ' + department.departmentName + ' (' +
                    department.departmentCode + ') was successfully removed from the system.',
                    location: $location.path()
                };
                for (var i = 0; i < $scope.departments.length; i++) {
                    if ($scope.departments[i].departmentId == department.departmentId) {
                        $scope.departments.splice(i, 1);
                        break;
                    }
                }
            })
            .error(function(data, status, headers, config) {
                if (status == 403) {
                    $rootScope.message = {
                        type: 'danger',
                        content: 'The department ' + department.departmentName + ' (' +
                        department.departmentCode + ') could not be removed from the system ' +
                        'because it has one or more evaluations associated with it.',
                        location: $location.path()
                    };
                } else {
                    $rootScope.message = {
                        type: 'danger',
                        content: 'The department ' + department.departmentName + ' (' +
                        department.departmentCode + ') could not be removed from the system ' +
                        'because of an unknown error.',
                        location: $location.path()
                    };
                }
            });
    };
 
    /**
     * Opens a modal window to create a new form.
     */
    $scope.addNewForm = function (formType) {
        var modalInstance = $modal.open({
            templateUrl: 'partials/administration/add-form-modal.html',
            controller: 'AddFormModalController',
            resolve: {
                formType: function() {
                    return formType;
                }
            }
        });
 
        modalInstance.result.then(function (form) {
            // TODO: Submit a request to save the new form in the database
        });
    };
}