(function(){ "use strict"; // canonical client-side student collection angular.module('shared') .provider('students', function studentsProvider() { var students; return { $get: ['groups', 'NameUtil', 'memberInfo', 'StudentsUtil', '_', studentsFactory], setStudents: setStudents, }; function studentsFactory(groups, NameUtil, memberInfo, StudentsUtil, _) { groups.get().$promise.then(function (groups) { initStudentGroups(groups); }); //Ensure referential integrity for student group collections client side (build collections of // references to canonical group objects) function initStudentGroups(groups) { _.each(students, function replaceStudentGroupsCanonically(student) { student.groups = _.map(student.groups, function replaceGroupWithCanonical(student_group) { return _.find(groups, function groupMatchesStudentGroup(group) { return group.grouping_id == student_group.grouping_id; }); }); }); } (function initStudentsSharedWith() { memberInfo.showSharedWith = memberInfo.canShareStudents && anyStudentsSharedWithOthers(students); if (!memberInfo.showSharedWith) { return; } _.map(students, function initStudentSharedWith(student) { student.others_shared_with = student.is_shared ? [] : sortMembers(otherMembers(student.shared_with)); }); function anyStudentsSharedWithOthers(students) { return _.any(students, isSharedWithOthers); } function isSharedWithOthers(student) { return !student.is_shared && otherMembers(student.shared_with).length > 0; } function otherMembers(members) { return _.reject(members, isMe); } function sortMembers(members) { return _.chain(members) .sortBy('username') .sortBy('last_name') .sortBy('first_name').value(); } function isMe(member) { return member.member_id == memberInfo.member_id; } })(); (function () { memberInfo.showTeacherUsername = memberInfo.canShareStudents && getShared().length > 0; })(); function get() { return students; } function indexOfStudentById(student) { var idx; for (idx = 0; idx < students.length; ++idx) { if (students[idx].student_id == student.student_id) { return idx; } } return -1; } function getStudentByStudentId(studentId) { return _.find(students, function (student) { return student.student_id == studentId; }); } function remove(student) { var idx = indexOfStudentById(student); if (idx != -1) { students.splice(idx, 1); } } function getHomeroom() { return _.reject(students, function (student) { return student.is_shared; }); } function getShared() { return StudentsUtil.getShared(students); } return { get: get, getStudentByStudentId: getStudentByStudentId, getHomeroom: getHomeroom, getShared: getShared, remove: remove }; } function setStudents(src) { students = src; } }); })();