{"size": 4978, "ext": "js", "max_stars_repo_path": "modules/templates/client/controllers/enrolment.client.controller.js", "max_stars_repo_name": "zlatkovtv/career_center_mean", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "modules/templates/client/controllers/enrolment.client.controller.js", "max_issues_repo_name": "zlatkovtv/career_center_mean", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "modules/templates/client/controllers/enrolment.client.controller.js", "max_forks_repo_name": "zlatkovtv/career_center_mean", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 45.2545454545, "max_line_length": 181, "alphanum_fraction": 0.5701084773}
(function () {
    'use strict';

    angular
    .module('templates')
    .controller('EnrolmentController', EnrolmentController);

    EnrolmentController.$inject = ['$scope', '$uibModal', '$uibModalInstance', 'UsersService', 'FacultyService', 'Notification', 'facultyClass'];

    function EnrolmentController($scope, $uibModal, $uibModalInstance, UsersService, FacultyService, Notification, facultyClass) {
        $scope.facultyClass = facultyClass;
        $scope.studentNameFilter = "";
        $scope.allStudents = [];
        $scope.enrolments = [];
        $scope.enroledStudentIds = [];
        $scope.isTranscriptContainerShown = false;
        $scope.selectedStudentTranscript = {};
        $scope.transcriptModalInstance = {};

        $scope.getAllStudents = () => {
            $scope.allStudents = UsersService.getAllStudents(function (response) {
                $scope.allStudents = response;
            }, function (response) {
                Notification.error({ message: response.data.message, title: '<i class="glyphicon glyphicon-remove"></i> Could not retrieve classes for some reason!' });
            });

            $scope.enrolments = FacultyService.getEnrolmentsByClassId({ classId: $scope.facultyClass._id }, {}, function (response) {
                $scope.enrolments = response;
                $scope.enroledStudentIds = $scope.enrolments.map(entry => entry.student);
            }, function (response) {
                Notification.error({ message: response.data.message, title: '<i class="glyphicon glyphicon-remove"></i> Could not retrieve enrollments for some reason!' });
            });
        };

        $scope.addToClass = (student) => {
            FacultyService.addUserToClass({ classId: $scope.facultyClass._id }, student, function (response) {
                $scope.getAllStudents();
                Notification.success({ title: '<i class="glyphicon glyphicon-ok"></i>Success', message: 'Added ' + student.displayName + ' to the class!' });
            }, function (response) {
                Notification.error({ message: response.data.message, title: '<i class="glyphicon glyphicon-remove"></i> Could not retrieve classes for some reason!' });
            });
        };

        $scope.close = () => {
            $uibModalInstance.close();
        };

        $scope.nonEnrolled = (student) => {
            if ($scope.enroledStudentIds.indexOf(student._id) !== -1) {
                return false;
            }

            if (student.displayName.toLowerCase().indexOf($scope.studentNameFilter.toLowerCase()) !== -1) {
                return true;
            }

            return false;
        };

        $scope.enrolled = (student) => {
            if ($scope.enroledStudentIds.indexOf(student._id) !== -1) {
                return true;
            }

            return false;
        };

        $scope.showTranscriptContainer = (student) => {
            $scope.isTranscriptContainerShown = true;
            $scope.selectedStudentTranscript.enrolment = $scope.enrolments.filter(function (enrolment) {
                return enrolment.student === student._id;
            })[0]._id;

            $scope.selectedStudentTranscript.grade = FacultyService.getTranscriptByEnrolmentId({ enrolmentId: $scope.selectedStudentTranscript.enrolment }, {}, function (response) {
                $scope.selectedStudentTranscript.grade = response.grade;

                popTranscriptModal();
            }, function (response) {
                Notification.error({ message: response.data.message, title: '<i class="glyphicon glyphicon-remove"></i> Could not retrieve grade for some reason!' });
            });

            function popTranscriptModal() {
                $scope.transcriptModalInstance = $uibModal.open({
                    templateUrl: '/modules/templates/client/views/transcript-input.client.modal.html',
                    controller: 'TranscriptInputController',
                    resolve: {
                        grade: $scope.selectedStudentTranscript.grade
                    }
                });
    
                $scope.transcriptModalInstance.result.then(function (result) {
                    if (!result) {
                        return;
                    }
    
                    $scope.selectedStudentTranscript.grade = result;
                    FacultyService.saveStudentTranscript({}, $scope.selectedStudentTranscript, function (response) {
                        Notification.success({ title: '<i class="glyphicon glyphicon-ok"></i>Success', message: 'Saved grade for ' + student.displayName + '!' });
                    }, function (response) {
                        Notification.error({ message: response.data.message, title: '<i class="glyphicon glyphicon-remove"></i> Could not save grade for some reason!' });
                    });
                }, function () {
                });
            }
        };

        $scope.getAllStudents();
    }
}());
