{backboneFailure, genericFailure} = require 'core/errors' errors = require 'core/errors' RootView = require 'views/core/RootView' template = require 'templates/admin' AdministerUserModal = require 'views/admin/AdministerUserModal' forms = require 'core/forms' # TODO: respect classroom versioning Campaigns = require 'collections/Campaigns' Classroom = require 'models/Classroom' CocoCollection = require 'collections/CocoCollection' Course = require 'models/Course' LevelSessions = require 'collections/LevelSessions' User = require 'models/User' Users = require 'collections/Users' module.exports = class MainAdminView extends RootView id: 'admin-view' template: template lastUserSearchValue: '' events: 'submit #espionage-form': 'onSubmitEspionageForm' 'submit #user-search-form': 'onSubmitUserSearchForm' 'click #stop-spying-btn': 'onClickStopSpyingButton' 'click #increment-button': 'incrementUserAttribute' 'click #user-search-result': 'onClickUserSearchResult' 'click #create-free-sub-btn': 'onClickFreeSubLink' 'click #terminal-create': 'onClickTerminalSubLink' 'click .classroom-progress-csv': 'onClickExportProgress' getTitle: -> return $.i18n.t('account_settings.admin') initialize: -> @campaigns = new Campaigns() @courses = new CocoCollection([], { url: "/db/course", model: Course}) if window.amActually @amActually = new User({_id: window.amActually}) @amActually.fetch() @supermodel.trackModel(@amActually) if me.isAdmin() @supermodel.trackRequest @campaigns.fetchByType('course', { data: { project: 'levels' } }) @supermodel.loadCollection(@courses, 'courses') super() onLoaded: -> campaignCourseIndexMap = {} for course, index in @courses.models campaignCourseIndexMap[course.get('campaignID')] = index + 1 @courseLevels = [] for campaign in @campaigns.models continue unless campaignCourseIndexMap[campaign.id] for levelID, level of campaign.get('levels') @courseLevels.push({ levelID slug: level.slug courseIndex: campaignCourseIndexMap[campaign.id] }) super() onClickStopSpyingButton: -> button = @$('#stop-spying-btn') forms.disableSubmit(button) me.stopSpying({ success: -> document.location.reload() error: -> forms.enableSubmit(button) errors.showNotyNetworkError(arguments...) }) onSubmitEspionageForm: (e) -> e.preventDefault() button = @$('#enter-espionage-mode') userNameOrEmail = @$el.find('#espionage-name-or-email').val().toLowerCase() forms.disableSubmit(button) me.spy(userNameOrEmail, { success: -> window.location.reload() error: -> forms.enableSubmit(button) errors.showNotyNetworkError(arguments...) }) onSubmitUserSearchForm: (e) -> e.preventDefault() searchValue = @$el.find('#user-search').val() return if searchValue is @lastUserSearchValue return @onSearchRequestSuccess [] unless @lastUserSearchValue = searchValue.toLowerCase() forms.disableSubmit(@$('#user-search-button')) $.ajax type: 'POST', url: '/db/user/-/admin_search' data: {search: @lastUserSearchValue} success: @onSearchRequestSuccess error: @onSearchRequestFailure onSearchRequestSuccess: (users) => forms.enableSubmit(@$('#user-search-button')) result = '' if users.length result = ("
#{user._id}