/**
* 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
});
};
} |