2015-11-30 16:59:22 -05:00
|
|
|
ModalView = require 'views/core/ModalView'
|
|
|
|
template = require 'templates/courses/activate-licenses-modal'
|
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
2015-12-04 17:17:16 -05:00
|
|
|
Prepaids = require 'collections/Prepaids'
|
2016-03-30 16:57:19 -04:00
|
|
|
Classrooms = require 'collections/Classrooms'
|
2015-11-30 16:59:22 -05:00
|
|
|
User = require 'models/User'
|
2016-03-30 16:57:19 -04:00
|
|
|
Users = require 'collections/Users'
|
2015-11-30 16:59:22 -05:00
|
|
|
|
|
|
|
module.exports = class ActivateLicensesModal extends ModalView
|
|
|
|
id: 'activate-licenses-modal'
|
|
|
|
template: template
|
2015-12-08 15:05:08 -05:00
|
|
|
|
2015-11-30 16:59:22 -05:00
|
|
|
events:
|
|
|
|
'change input': 'updateSelectionSpans'
|
2016-03-30 16:57:19 -04:00
|
|
|
'change select': 'replaceStudentList'
|
2015-11-30 16:59:22 -05:00
|
|
|
'submit form': 'onSubmitForm'
|
2015-12-08 15:05:08 -05:00
|
|
|
|
2015-11-30 16:59:22 -05:00
|
|
|
initialize: (options) ->
|
|
|
|
@classroom = options.classroom
|
|
|
|
@users = options.users
|
2016-03-30 16:57:19 -04:00
|
|
|
@selectedUsers = options.selectedUsers
|
2015-12-04 17:17:16 -05:00
|
|
|
@prepaids = new Prepaids()
|
2015-11-30 16:59:22 -05:00
|
|
|
@prepaids.comparator = '_id'
|
2015-12-04 17:17:16 -05:00
|
|
|
@prepaids.fetchByCreator(me.id)
|
2016-03-30 16:57:19 -04:00
|
|
|
@supermodel.trackCollection(@prepaids)
|
|
|
|
@classrooms = new Classrooms()
|
|
|
|
@classrooms.fetchMine({
|
|
|
|
data: {archived: false}
|
|
|
|
success: =>
|
|
|
|
@classrooms.each (classroom) =>
|
|
|
|
classroom.users = new Users()
|
2016-04-07 17:55:42 -04:00
|
|
|
jqxhrs = classroom.users.fetchForClassroom(classroom, { removeDeleted: true })
|
2016-03-30 19:20:37 -04:00
|
|
|
@supermodel.trackRequests(jqxhrs)
|
2016-03-30 16:57:19 -04:00
|
|
|
})
|
|
|
|
@supermodel.trackCollection(@classrooms)
|
|
|
|
|
2015-11-30 16:59:22 -05:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
@updateSelectionSpans()
|
|
|
|
|
|
|
|
updateSelectionSpans: ->
|
|
|
|
targets = @$('input[name="targets"]:checked').val()
|
|
|
|
if targets is 'given'
|
|
|
|
numToActivate = 1
|
|
|
|
else
|
|
|
|
numToActivate = @$('input[name="user"]:checked:not(:disabled)').length
|
|
|
|
@$('#total-selected-span').text(numToActivate)
|
|
|
|
remaining = @prepaids.totalMaxRedeemers() - @prepaids.totalRedeemers() - numToActivate
|
|
|
|
depleted = remaining < 0
|
2016-04-07 17:55:42 -04:00
|
|
|
@$('.not-enough-enrollments').toggleClass('visible', depleted)
|
2015-12-04 17:53:30 -05:00
|
|
|
@$('#activate-licenses-btn').toggleClass('disabled', depleted).toggleClass('btn-success', not depleted).toggleClass('btn-default', depleted)
|
2016-03-30 16:57:19 -04:00
|
|
|
|
|
|
|
replaceStudentList: (e) ->
|
|
|
|
selectedClassroomID = $(e.currentTarget).val()
|
|
|
|
@classroom = @classrooms.get(selectedClassroomID)
|
|
|
|
if selectedClassroomID == 'all-classrooms'
|
|
|
|
@classroom = new Classroom({ id: 'all-students' }) # TODO: This is a horrible hack so the select shows the right option!
|
|
|
|
users = _.uniq _.flatten @classrooms.map (classroom) -> classroom.users.models
|
|
|
|
@users.reset(users)
|
|
|
|
else
|
|
|
|
@users.reset(@classrooms.get(selectedClassroomID).users.models)
|
|
|
|
@trigger('users:change')
|
|
|
|
@render()
|
|
|
|
null
|
2015-12-08 15:05:08 -05:00
|
|
|
|
2015-11-30 16:59:22 -05:00
|
|
|
showProgress: ->
|
|
|
|
@$('#submit-form-area').addClass('hide')
|
|
|
|
@$('#progress-area').removeClass('hide')
|
2015-12-08 15:05:08 -05:00
|
|
|
|
2015-11-30 16:59:22 -05:00
|
|
|
hideProgress: ->
|
|
|
|
@$('#submit-form-area').removeClass('hide')
|
|
|
|
@$('#progress-area').addClass('hide')
|
|
|
|
|
|
|
|
onSubmitForm: (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
@$('#error-alert').addClass('hide')
|
|
|
|
@usersToRedeem = new CocoCollection([], {model: User})
|
|
|
|
targets = @$('input[name="targets"]:checked').val()
|
|
|
|
if targets is 'given'
|
|
|
|
@usersToRedeem.add(@user)
|
|
|
|
else
|
|
|
|
checkedBoxes = @$('input[name="user"]:checked:not(:disabled)')
|
|
|
|
_.each checkedBoxes, (el) =>
|
|
|
|
$el = $(el)
|
|
|
|
userID = $el.data('user-id')
|
|
|
|
@usersToRedeem.add @users.get(userID)
|
|
|
|
return unless @usersToRedeem.size()
|
|
|
|
@usersToRedeem.originalSize = @usersToRedeem.size()
|
|
|
|
@showProgress()
|
|
|
|
@redeemUsers()
|
|
|
|
|
|
|
|
redeemUsers: ->
|
|
|
|
if not @usersToRedeem.size()
|
|
|
|
@finishRedeemUsers()
|
|
|
|
return
|
|
|
|
|
|
|
|
user = @usersToRedeem.first()
|
2016-04-07 17:55:42 -04:00
|
|
|
prepaid = @prepaids.find((prepaid) -> prepaid.get('properties')?.endDate? and prepaid.openSpots() > 0)
|
|
|
|
prepaid = @prepaids.find((prepaid) -> prepaid.openSpots() > 0) unless prepaid
|
2015-11-30 16:59:22 -05:00
|
|
|
$.ajax({
|
|
|
|
method: 'POST'
|
|
|
|
url: _.result(prepaid, 'url') + '/redeemers'
|
|
|
|
data: { userID: user.id }
|
|
|
|
context: @
|
|
|
|
success: ->
|
|
|
|
@usersToRedeem.remove(user)
|
|
|
|
pct = 100 * (@usersToRedeem.originalSize - @usersToRedeem.size() / @usersToRedeem.originalSize)
|
|
|
|
@$('#progress-area .progress-bar').css('width', "#{pct.toFixed(1)}%")
|
2015-12-21 18:45:14 -05:00
|
|
|
application.tracker?.trackEvent 'Enroll modal finished enroll student', category: 'Courses', userID: user.id
|
2015-11-30 16:59:22 -05:00
|
|
|
@redeemUsers()
|
|
|
|
error: (jqxhr, textStatus, errorThrown) ->
|
|
|
|
if jqxhr.status is 402
|
|
|
|
message = arguments[2]
|
|
|
|
else
|
|
|
|
message = "#{jqxhr.status}: #{jqxhr.responseText}"
|
|
|
|
@$('#error-alert').text(message).removeClass('hide')
|
|
|
|
})
|
|
|
|
|
2015-12-08 15:05:08 -05:00
|
|
|
finishRedeemUsers: ->
|
2015-11-30 16:59:22 -05:00
|
|
|
@trigger 'redeem-users'
|