misc = require('smc-util/misc')
{defaults, required} = misc
immutable = require('immutable')
exports.STEPS = (peer) ->
if peer
return ['assignment', 'collect', 'peer_assignment', 'peer_collect', 'return_graded']
else
return ['assignment', 'collect', 'return_graded']
exports.previous_step = (step, peer) ->
switch step
when 'collect'
return 'assignment'
when 'return_graded'
if peer
return 'peer_collect'
else
return 'collect'
when 'assignment'
return
when 'peer_assignment'
return 'collect'
when 'peer_collect'
return 'peer_assignment'
else
console.warn("BUG! previous_step('#{step}')")
exports.step_direction = (step) ->
switch step
when 'assignment'
return 'to'
when 'collect'
return 'from'
when 'return_graded'
return 'to'
when 'peer_assignment'
return 'to'
when 'peer_collect'
return 'from'
else
console.warn("BUG! step_direction('#{step}')")
exports.step_verb = (step) ->
switch step
when 'assignment'
return 'assign'
when 'collect'
return 'collect'
when 'return_graded'
return 'return'
when 'peer_assignment'
return 'assign'
when 'peer_collect'
return 'collect'
else
console.warn("BUG! step_verb('#{step}')")
exports.step_ready = (step, n) ->
switch step
when 'assignment'
return ''
when 'collect'
return if n >1 then ' who have already received it' else ' who has already received it'
when 'return_graded'
return ' whose work you have graded'
when 'peer_assignment'
return ' for peer grading'
when 'peer_collect'
return ' who should have peer graded it'
exports.parse_students = (student_map, user_map, redux) ->
v = exports.immutable_to_list(student_map, 'student_id')
for x in v
if x.account_id?
user = user_map.get(x.account_id)
x.first_name ?= user?.get('first_name') ? ''
x.last_name ?= user?.get('last_name') ? ''
if x.project_id?
x.last_active = redux.getStore('projects').get_last_active(x.project_id)?.get(x.account_id)?.getTime?()
upgrades = redux.getStore('projects').get_total_project_quotas(x.project_id)
if upgrades?
x.hosting = upgrades.member_host
x.first_name ?= ""
x.last_name ?= ""
x.last_active ?= 0
x.hosting ?= false
x.email_address ?= ""
return v
exports.immutable_to_list = (x, primary_key) ->
if not x?
return
v = []
x.map (val, key) ->
v.push(misc.merge(val.toJS(), {"#{primary_key}":key}))
return v
exports.compute_match_list = (opts) ->
opts = defaults opts,
list : required
search_key : required
search : required
ignore_case : true
{list, search, search_key, ignore_case} = opts
if not search
return {list:list, num_omitted:0}
num_omitted = 0
words = misc.split(search)
matches = (x) =>
if ignore_case
k = x[search_key].toLowerCase?()
else
k = x[search_key]
for w in words
if k.indexOf(w) == -1
num_omitted += 1
return false
return true
list = list.filter matches
return {list:list, num_omitted:num_omitted}
exports.order_list = (opts) ->
opts = defaults opts,
list : required
compare_function : required
reverse : false
include_deleted : false
{list, compare_function, include_deleted} = opts
x = list.filter (x) => x.deleted
sorted_deleted = x.sort compare_function
y = list.filter (x) => not x.deleted
list = y.sort compare_function
if opts.reverse
list.reverse()
if include_deleted
list = list.concat(sorted_deleted)
return {list:list, deleted:x, num_deleted:sorted_deleted.length}
sort_on_string_field = (field) ->
(a,b) -> misc.cmp(a[field].toLowerCase(), b[field].toLowerCase())
sort_on_numerical_field = (field) ->
(a,b) -> misc.cmp(a[field] * -1, b[field] * -1)
exports.pick_student_sorter = (sort) ->
switch sort.column_name
when "email" then sort_on_string_field("email_address")
when "first_name" then sort_on_string_field("first_name")
when "last_name" then sort_on_string_field("last_name")
when "last_active" then sort_on_numerical_field("last_active")
when "hosting" then sort_on_numerical_field("hosting")