<commit_before>'use strict';

/**
* Service that allows users to select the event for which the current page should display information about.  Admins should be able to view all events
* at all times, the eventSelector will have to behave slightly differently if the user is an admin.  Otherwise, the user should only view the events that are in
* their status array.  While this is handled in the backend, the data sent to the frontend differs slightly.
*
* Since the eventSelector is refreshed every time the page is refreshed, we should store the value that
* the user selected before refreshing the page so this event can automatically be restored.  We are using
* cacheService to deal with saving these values to the localStorage.
*/
angular.module('core').service('eventSelector', ['$rootScope', '$http', '$location', 'cacheService', 'Authentication', '$window',

	function($rootScope, $http, $location, cacheService, Authentication, $window) {
		var thisService = this;
		var cache = cacheService;

		this.events = [];				//List of events viewable to this user.
		this.selectedEvent = "Select Event";
		this.postEventId = null;
		this.numRecruiting = 0;
		this.recruiterEvent = true;		//Whether or not this user is recruiting for the selected event.
		this.nresDisabled = false;		//Whether or not the events for which this user is only attending (not recruiting) are disabled (used on pages that require recruiter/admin privileges).
		this.disabled = false;
		this.admin = false;				//Whether or not this user is an admin.

		var keys = [];
		var put = function(key, value) {
			cache.setData(key,value);
		}

		this.hideEventSelector = function() {
			var path = $location.path();

			return (path === '/signin' || path === '/settings/profile' || path === '/settings/password');
		}

		thisService.eventSelect = function() {
			/**
			* Functions will be defined based on whether or not the current user is an admin.  Since
			* admins should be able to see all events, their eventSelector will behave differently than
			* attendees and recruiters.
			*/
			if(_.intersection(Authentication.user.roles, ['admin']).length > 0) {
				thisService.admin = true;

				var checkEvent = function(needle) {
					for (var i=0; i<thisService.events.length; i++) {
						if(thisService.events[i]._id === needle)
							return true;
					}
					return false;
				}

				$http.get('/users/events').success(function(data) {
					thisService.events = data;

					var cachedEvent = cache.getData('selectedEvent'), cachedId = cache.getData('eventId');

					if(cachedEvent && cachedId && thisService.events.length && checkEvent(cachedId)) {
						thisService.selectedEvent = cache.getData('selectedEvent');
						thisService.postEventId = cache.getData('eventId');
					}
				}).error(function(error, status) {
					thisService.selectedEvent = "Error";
					console.log(error);
				});

				thisService.changeEvent = function(event) {
					thisService.selectedEvent = event.name;
					thisService.postEventId = event._id;
					put('selectedEvent', event.name);
					put('eventId', event._id);
				};

				/**
				* Admins have permission to do anything with any event so there is no need for a divider
				* or to make events disabled on any page.
				*/<commit_msg>made the event selector default to the first available event if no cache is present<commit_after>'use strict';

/**
* Service that allows users to select the event for which the current page should display information about.  Admins should be able to view all events
* at all times, the eventSelector will have to behave slightly differently if the user is an admin.  Otherwise, the user should only view the events that are in
* their status array.  While this is handled in the backend, the data sent to the frontend differs slightly.
*
* Since the eventSelector is refreshed every time the page is refreshed, we should store the value that
* the user selected before refreshing the page so this event can automatically be restored.  We are using
* cacheService to deal with saving these values to the localStorage.
*/
angular.module('core').service('eventSelector', ['$rootScope', '$http', '$location', 'cacheService', 'Authentication', '$window',

	function($rootScope, $http, $location, cacheService, Authentication, $window) {
		var thisService = this;
		var cache = cacheService;

		this.events = [];				//List of events viewable to this user.
		this.selectedEvent = "Select Event";
		this.postEventId = null;
		this.numRecruiting = 0;
		this.recruiterEvent = true;		//Whether or not this user is recruiting for the selected event.
		this.nresDisabled = false;		//Whether or not the events for which this user is only attending (not recruiting) are disabled (used on pages that require recruiter/admin privileges).
		this.disabled = false;
		this.admin = false;				//Whether or not this user is an admin.

		var keys = [];
		var put = function(key, value) {
			cache.setData(key,value);
		}

		this.hideEventSelector = function() {
			var path = $location.path();

			return (path === '/signin' || path === '/settings/profile' || path === '/settings/password');
		}

		thisService.eventSelect = function() {
			/**
			* Functions will be defined based on whether or not the current user is an admin.  Since
			* admins should be able to see all events, their eventSelector will behave differently than
			* attendees and recruiters.
			*/
			if(_.intersection(Authentication.user.roles, ['admin']).length > 0) {
				thisService.admin = true;

				var checkEvent = function(needle) {
					for (var i=0; i<thisService.events.length; i++) {
						if(thisService.events[i]._id === needle)
							return true;
					}
					return false;
				}

				$http.get('/users/events').success(function(data) {
					thisService.events = data;

					var cachedEvent = cache.getData('selectedEvent'), cachedId = cache.getData('eventId');

					if(cachedEvent && cachedId && thisService.events.length && checkEvent(cachedId)) {
						thisService.selectedEvent = cache.getData('selectedEvent');
						thisService.postEventId = cache.getData('eventId');
					}
					else {
						thisService.selectedEvent = this.events[0].event_id.name;
						thisService.postEventId = this.events[0].event_id._id;
					}
				}).error(function(error, status) {
					thisService.selectedEvent = "Error";
					console.log(error);
				});

				thisService.changeEvent = function(event) {
					thisService.selectedEvent = event.name;
					thisService.postEventId = event._id;
					put('selectedEvent', event.name);
					put('eventId', event._id);
				};

				/**
				* Admins have permission to do anything with any event so there is no need for a divider
				* or to make events disabled on any page.
				*/
{'fraction_non_alphanumeric': 0.10817236255572066, 'fraction_numerical': 0.001188707280832095, 'mean_word_length': 3.591405184174625, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '935884', 'n_tokens_mistral': 2181, 'n_tokens_neox': 1828, 'n_words': 679}