codecombat/app/views/courses/ActivateLicensesModal.coffee
phoenixeliot e2d08fa7cf Stuff
Partially fix ActivateLicensesModal.spec

[IN PROGRESS] Don't display deleted users

Move userID to classroom.deletedMembers on user delete (not retroactive)

Fix PDF links for course guides, remove old PDFs from repo

Remove deprecated SalesView

Remove underline for not-yet-linked student names

Only show class select when there's more than one

Ignore case when sorting student names

Use student.broadName instead of name for display and sorting

Fix initial load not showing progress after joining a course (hacky)

Fix text entry for enrollment number input

Fix enrollment statistics

Fix enrollment stats completely (and add back in per-class unenrolled count)

Add deletedMembers to classroom schema

More fixes to enrollment stats (don't count nonmember prepaids)

Don't use 0 as implicit false for openSpots

Update suggested number of credit to buy automatically

Fix classroom edit form ignoring cleared values

Add alert text when more users selected than enrollments available

Alert user when trying to assign course to unenrolled students

Alert user when assigning course to nobody

Add some tests for TeacherClassView bulk assign alerts

Fix TeacherClassView tests failing without demos

Use model/collection.fakeRequests :D

Remove unused comment

Fix handling of improperly sorted deleted users on clientside

Add test for moving deleted users to deletedMembers

Add script for moving all deleted classroom members to classroom.deletedMembers

Completely rewrite tallying up enrollment statistics

Fix some tests to not be dependent on logged-in user

Address PR comments

Fix default number of enrollments to buy

Fix i18n for not enough enrollments

Use custom error message for classroom name length
2016-04-19 12:32:20 -07:00

120 lines
4.3 KiB
CoffeeScript

ModalView = require 'views/core/ModalView'
template = require 'templates/courses/activate-licenses-modal'
CocoCollection = require 'collections/CocoCollection'
Prepaids = require 'collections/Prepaids'
Classrooms = require 'collections/Classrooms'
User = require 'models/User'
Users = require 'collections/Users'
module.exports = class ActivateLicensesModal extends ModalView
id: 'activate-licenses-modal'
template: template
events:
'change input': 'updateSelectionSpans'
'change select': 'replaceStudentList'
'submit form': 'onSubmitForm'
initialize: (options) ->
@classroom = options.classroom
@users = options.users
@selectedUsers = options.selectedUsers
@prepaids = new Prepaids()
@prepaids.comparator = '_id'
@prepaids.fetchByCreator(me.id)
@supermodel.trackCollection(@prepaids)
@classrooms = new Classrooms()
@classrooms.fetchMine({
data: {archived: false}
success: =>
@classrooms.each (classroom) =>
classroom.users = new Users()
jqxhrs = classroom.users.fetchForClassroom(classroom, { removeDeleted: true })
@supermodel.trackRequests(jqxhrs)
})
@supermodel.trackCollection(@classrooms)
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
@$('.not-enough-enrollments').toggleClass('visible', depleted)
@$('#activate-licenses-btn').toggleClass('disabled', depleted).toggleClass('btn-success', not depleted).toggleClass('btn-default', depleted)
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
showProgress: ->
@$('#submit-form-area').addClass('hide')
@$('#progress-area').removeClass('hide')
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()
prepaid = @prepaids.find((prepaid) -> prepaid.get('properties')?.endDate? and prepaid.openSpots() > 0)
prepaid = @prepaids.find((prepaid) -> prepaid.openSpots() > 0) unless prepaid
$.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)}%")
application.tracker?.trackEvent 'Enroll modal finished enroll student', category: 'Courses', userID: user.id
@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')
})
finishRedeemUsers: ->
@trigger 'redeem-users'