<commit_before>define(['../module'], function(controllers) {
    'use strict';

    controllers.controller('ListAttendanceCtrl', ['$scope', '$modal', '$http', '$log', '$state', '$filter', 'saveAs', 'AttendanceSvc', 'StudentSvc', 'WorkshopSvc', 'ClassSvc', 'ProgramSvc', 'AchievementSvc', 'RankSvc',
        function($scope, $modal, $http, $log, $state, $filter, saveAs, AttendanceSvc, StudentSvc, WorkshopSvc, ClassSvc, ProgramSvc, AchievementSvc, RankSvc) {
            var defaultColumnDefs = [
                    { field: 'checkInTime', displayName: 'Check-in Time', cellFilter: 'dateTime'},
                    { field: 'fullName', displayName: 'Student'},
                    { field: 'eventName', displayName: 'Event Attended'},
                    { sortable: false, displayName: 'Actions', cellTemplate: '/partials/attendance/list/studentViewButton'}
                ],
                studentColumnDefs = [
                    { field: 'checkInTime', displayName: 'Check-in Time', cellFilter: 'dateTime'},
                    { field: 'eventName', displayName: 'Event Attended'},
                    { field: 'achievementNames', displayName: 'Achievement(s)', cellFilter: 'stringArray'},
                    { sortable: false, displayName: 'Actions', cellTemplate: '/partials/attendance/list/studentAttendanceButtons', minWidth: '250'}
                ],
                classColumnDefs = [
                    { field: 'name', displayName: 'Class Name'},
                    { field: 'programName', displayName: 'Program'},
                    { sortable: false, displayName: 'Actions', cellTemplate: '/partials/attendance/list/classViewButton'}
                ],
                workshopColumnDefs = [
                    { field: 'name', displayName: 'Workshop Name'},
                    { field: 'workshopDate', displayName: 'Date', cellFilter: 'dateTime'},
                    { field: 'numAttendees', displayName: 'Attendees'},
                    { sortable: false, displayName: 'Actions', cellTemplate: '/partials/attendance/list/workshopViewButton'}
                ];

            var emails = [];

            $scope.allData = [];
            $scope.filterStudent = {};
            $scope.studentTab = {};

            $scope.filterOptions = {
                filterText: '',
                useExternalFilter: true
            };

            $scope.totalServerItems = 0;
            $scope.pagingOptions = {
                pageSizes: [25, 50, 100, 250, 500, 1000],
                pageSize: 25,
                currentPage: 1
            };

            $scope.columnDefs = defaultColumnDefs;

            function resetNewTab() {
                $scope.viewingStudent = false;
                $scope.viewingClass = false;
                $scope.viewingWorkshop = false;

                $scope.currentClass = null;
                $scope.currentStudent = null;
                $scope.currentWorkshop = null;              
            }

            $scope.selectStudents = function() {
                resetNewTab();

                $scope.columnDefs = defaultColumnDefs;
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
            };

            $scope.selectClasses = function() {
                resetNewTab();

                $scope.columnDefs = classColumnDefs;
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);             
            };

            $scope.selectWorkshops = function() { 
                resetNewTab();

                $scope.columnDefs = workshopColumnDefs;
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);             
            };

            $scope.viewStudentAttendance = function(row) {
                StudentSvc.read(row.entity.student, null, false).then(function(student) {
                    $scope.studentTab.active = true;
                    $scope.currentStudent = student;
                    $scope.currentStudent.fullName = student.firstName + " " + student.lastName;
                    $scope.viewingStudent = true;
                    $scope.columnDefs = studentColumnDefs;
                    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, {student: row.entity.student});
                });
            };

            $scope.viewClassAttendance = function(row) {
                $scope.currentClass = row.entity.name;
                $scope.viewingClass = true;
                $scope.columnDefs = defaultColumnDefs;
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, {classAttended: row.entity._id});
            };

            $scope.viewWorkshopAttendance = function(row) {
                $scope.currentWorkshop = row.entity.name;
                $scope.viewingWorkshop = true;
                $scope.columnDefs = defaultColumnDefs;
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, {classAttended: row.entity._id});
            };

            $scope.searchByStudent = function() {
                var data = [];

                if (!$scope.filterStudent.name || $scope.filterStudent.name.length === 0) {
                    data = $scope.allData;
                } else {
                    var searchCriteria = $scope.filterStudent.name.toLowerCase();

                    _($scope.allData).forEach(function(attendance) {
                        if (attendance.fullName.toLowerCase().indexOf(searchCriteria) > -1) {
                            data.push(attendance);
                        }
                    });
                }                
                $scope.setPagingData(data, $scope.pagingOptions.currentPage, $scope.pagingOptions.pageSize);
            };

            $scope.$watch('filterStudent.name', function(newVal) {
                $scope.searchByStudent();
            });

            $scope.setPagingData = function(data, page, pageSize){
                var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
                $scope.myData = pagedData;
                $scope.totalServerItems = data.length;
                if (!$scope.$$phase) {
                    $scope.$apply();
                }
            };

            $scope.getPagedDataAsync = function (pageSize, page, filterOptions) {
                setTimeout(function () {
                    var data = [];

                    if ($scope.columnDefs === classColumnDefs) { // Clicked the class tab
                        ClassSvc.list().then(function(classes) {
                            async.each(classes,
                                function(classItem, callback) {
                                    ProgramSvc.read(classItem.program, null, false).then(function(program) {
                                        if (!program) {
                                            callback();
                                            return;
                                        }

                                        classItem.programName = program.name;
                                        data.push(classItem);
                                        callback();
                                    });
                                },
                                function(err) {
                                    $scope.setPagingData(data,page,pageSize);
                            });
                        });
                    } else if ($scope.columnDefs === workshopColumnDefs) { // Clicked the workshops tab
                        WorkshopSvc.list().then(function(workshops) {
                            _(workshops).forEach(function(workshop) {
                                workshop.numAttendees = 0;
                                if (workshop.attendanceList) {
                                    workshop.numAttendees = workshop.attendanceList.length;
                                }
                                data.push(workshop);
                            });
                            $scope.setPagingData(data,page,pageSize);
                        });
                    } else if ($scope.columnDefs === studentColumnDefs) { // Viewing a student's attendace records
                        AttendanceSvc.list().then(function(attendances) {<commit_msg>Adjust the number of workshop attendees so that it counts attendance items rather than relying on workshop attendance list<commit_after>define(['../module'], function(controllers) {
    'use strict';

    controllers.controller('ListAttendanceCtrl', ['$scope', '$modal', '$http', '$log', '$state', '$filter', 'saveAs', 'AttendanceSvc', 'StudentSvc', 'WorkshopSvc', 'ClassSvc', 'ProgramSvc', 'AchievementSvc', 'RankSvc',
        function($scope, $modal, $http, $log, $state, $filter, saveAs, AttendanceSvc, StudentSvc, WorkshopSvc, ClassSvc, ProgramSvc, AchievementSvc, RankSvc) {
            var defaultColumnDefs = [
                    { field: 'checkInTime', displayName: 'Check-in Time', cellFilter: 'dateTime'},
                    { field: 'fullName', displayName: 'Student'},
                    { field: 'eventName', displayName: 'Event Attended'},
                    { sortable: false, displayName: 'Actions', cellTemplate: '/partials/attendance/list/studentViewButton'}
                ],
                studentColumnDefs = [
                    { field: 'checkInTime', displayName: 'Check-in Time', cellFilter: 'dateTime'},
                    { field: 'eventName', displayName: 'Event Attended'},
                    { field: 'achievementNames', displayName: 'Achievement(s)', cellFilter: 'stringArray'},
                    { sortable: false, displayName: 'Actions', cellTemplate: '/partials/attendance/list/studentAttendanceButtons', minWidth: '250'}
                ],
                classColumnDefs = [
                    { field: 'name', displayName: 'Class Name'},
                    { field: 'programName', displayName: 'Program'},
                    { sortable: false, displayName: 'Actions', cellTemplate: '/partials/attendance/list/classViewButton'}
                ],
                workshopColumnDefs = [
                    { field: 'name', displayName: 'Workshop Name'},
                    { field: 'workshopDate', displayName: 'Date', cellFilter: 'dateTime'},
                    { field: 'numAttendees', displayName: 'Attendees'},
                    { sortable: false, displayName: 'Actions', cellTemplate: '/partials/attendance/list/workshopViewButton'}
                ];

            var emails = [];

            $scope.allData = [];
            $scope.filterStudent = {};
            $scope.studentTab = {};

            $scope.filterOptions = {
                filterText: '',
                useExternalFilter: true
            };

            $scope.totalServerItems = 0;
            $scope.pagingOptions = {
                pageSizes: [25, 50, 100, 250, 500, 1000],
                pageSize: 25,
                currentPage: 1
            };

            $scope.columnDefs = defaultColumnDefs;

            function resetNewTab() {
                $scope.viewingStudent = false;
                $scope.viewingClass = false;
                $scope.viewingWorkshop = false;

                $scope.currentClass = null;
                $scope.currentStudent = null;
                $scope.currentWorkshop = null;              
            }

            $scope.selectStudents = function() {
                resetNewTab();

                $scope.columnDefs = defaultColumnDefs;
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
            };

            $scope.selectClasses = function() {
                resetNewTab();

                $scope.columnDefs = classColumnDefs;
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);             
            };

            $scope.selectWorkshops = function() { 
                resetNewTab();

                $scope.columnDefs = workshopColumnDefs;
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);             
            };

            $scope.viewStudentAttendance = function(row) {
                StudentSvc.read(row.entity.student, null, false).then(function(student) {
                    $scope.studentTab.active = true;
                    $scope.currentStudent = student;
                    $scope.currentStudent.fullName = student.firstName + " " + student.lastName;
                    $scope.viewingStudent = true;
                    $scope.columnDefs = studentColumnDefs;
                    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, {student: row.entity.student});
                });
            };

            $scope.viewClassAttendance = function(row) {
                $scope.currentClass = row.entity.name;
                $scope.viewingClass = true;
                $scope.columnDefs = defaultColumnDefs;
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, {classAttended: row.entity._id});
            };

            $scope.viewWorkshopAttendance = function(row) {
                $scope.currentWorkshop = row.entity.name;
                $scope.viewingWorkshop = true;
                $scope.columnDefs = defaultColumnDefs;
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, {classAttended: row.entity._id});
            };

            $scope.searchByStudent = function() {
                var data = [];

                if (!$scope.filterStudent.name || $scope.filterStudent.name.length === 0) {
                    data = $scope.allData;
                } else {
                    var searchCriteria = $scope.filterStudent.name.toLowerCase();

                    _($scope.allData).forEach(function(attendance) {
                        if (attendance.fullName.toLowerCase().indexOf(searchCriteria) > -1) {
                            data.push(attendance);
                        }
                    });
                }                
                $scope.setPagingData(data, $scope.pagingOptions.currentPage, $scope.pagingOptions.pageSize);
            };

            $scope.$watch('filterStudent.name', function(newVal) {
                $scope.searchByStudent();
            });

            $scope.setPagingData = function(data, page, pageSize){
                var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
                $scope.myData = pagedData;
                $scope.totalServerItems = data.length;
                if (!$scope.$$phase) {
                    $scope.$apply();
                }
            };

            $scope.getPagedDataAsync = function (pageSize, page, filterOptions) {
                setTimeout(function () {
                    var data = [];

                    if ($scope.columnDefs === classColumnDefs) { // Clicked the class tab
                        ClassSvc.list().then(function(classes) {
                            async.each(classes,
                                function(classItem, callback) {
                                    ProgramSvc.read(classItem.program, null, false).then(function(program) {
                                        if (!program) {
                                            callback();
                                            return;
                                        }

                                        classItem.programName = program.name;
                                        data.push(classItem);
                                        callback();
                                    });
                                },
                                function(err) {
                                    $scope.setPagingData(data,page,pageSize);
                            });
                        });
                    } else if ($scope.columnDefs === workshopColumnDefs) { // Clicked the workshops tab
                        AttendanceSvc.list().then(function(attendances) {
                            WorkshopSvc.list().then(function(workshops) {
                                _(workshops).forEach(function(workshop) {
                                    workshop.numAttendees = _.where(attendances, {classAttended: workshop._id}).length;
                                    data.push(workshop);
                                });
                                $scope.setPagingData(data,page,pageSize);
                            });
                        });

                        
                    } else if ($scope.columnDefs === studentColumnDefs) { // Viewing a student's attendace records
                        AttendanceSvc.list().then(function(attendances) {
{'fraction_non_alphanumeric': 0.11081763221224394, 'fraction_numerical': 0.003228267887538886, 'mean_word_length': 1.4181095657110416, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6708080', 'n_tokens_mistral': 4254, 'n_tokens_neox': 4004, 'n_words': 675}