all files / js/administration/ assignForm.controller.js

5% Statements 2/40
0% Branches 0/20
0% Functions 0/11
5% Lines 2/40
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                                                                                                                                                                                                                                 
/**
 * Created by cklimkowsky on 5/5/15.
 */
angular
    .module('coopEval')
    .controller('AssignFormController', AssignFormController);
 
function AssignFormController($scope, $rootScope, $location, administrationService) {
 
    // Get the list of all colleges in the system
    administrationService.getColleges()
        .success(function (result) {
            $scope.colleges = result;
            $scope.selectedCollege = $scope.colleges[0];
        });
 
    // Get the list of all future terms in the system
    administrationService.getFutureTerms()
        .success(function (result) {
            $scope.futureTerms = result;
        });
 
    // Get the list of all Student forms in the system
    administrationService.getStudentForms()
        .success(function (result) {
            $scope.studentForms = result;
            $scope.forms = $scope.studentForms;
        });
 
    // Get the list of all Employer forms in the system
    administrationService.getEmployerForms()
        .success(function (result) {
            $scope.employerForms = result;
        });
 
    // The form type that is currently selected in the UI (default is Student)
    $scope.formType = 0;
 
    // The name of the form currently assigned to the selected department and term (default is None)
    $scope.currentFormName = 'None';
 
    // When the user selects another colleges, get the departments for the newly selected college
    $scope.$watch('selectedCollege', function () {
        if ($scope.selectedCollege) {
            administrationService.getDepartments($scope.selectedCollege.collegeId)
                .success(function (result) {
                    $scope.departments = result;
                    $scope.selectedDepartment = $scope.departments[0];
                });
        }
    });
 
    // When the user selects a term, get the name of the form currently
    // assigned to the selected department for that term
    $scope.$watch('selectedTerm', function () {
        if ($scope.selectedDepartment) {
            if ($scope.formType == 0 && $scope.selectedDepartment.currentStudentFormName) {
                $scope.currentFormName = $scope.selectedDepartment.currentStudentFormName.name;
            } else if ($scope.formType == 1 && $scope.selectedDepartment.currentEmployerFormName) {
                $scope.currentFormName = $scope.selectedDepartment.currentEmployerFormName.name;
            } else {
                $scope.currentFormName = 'None';
            }
        }
    });
 
    // When the selected form type changes, use the appropriate list of forms
    $scope.$watch('formType', function () {
       if ($scope.formType == 0) {
           $scope.forms = $scope.studentForms;
       } else {
           $scope.forms = $scope.employerForms;
       }
    });
 
    /**
     * Assigns the selected form to the selected department for the given term.
     */
    $scope.assignForm = function () {
        // If the user has not yet selected a term and/or form, display
        // an error message and cancel the assignment
        if ((!$scope.selectedTerm) || (!$scope.selectedForm)) {
            $rootScope.message = {
                type: 'danger',
                content: 'You must select a semester and form before assigning a form to a department.',
                location: $location.path()
            };
            return;
        }
 
        // Compile the data selected in the UI to be sent to the back-end
        $scope.form = {
            isStudentForm: $scope.formType == 0 ? '1' : '0',
            department: $scope.selectedDepartment,
            termCode: $scope.selectedTerm,
            form: $scope.selectedForm
        };
 
        administrationService.assignForm($scope.form)
            .success(function (result) {
                $rootScope.message = {
                    type: 'success',
                    content: 'The form ' + $scope.form.form.name +
                        ' was successfully assigned to the department ' +
                        $scope.form.department.departmentCode + ' - ' + $scope.form.department.departmentName + '.',
                    location: $location.path()
                };
 
                // Reset the selections in the UI
                $scope.selectedTerm = undefined;
                $scope.selectedForm = undefined;
                $scope.selectedCollege = $scope.colleges[0];
            });
    };
}