2016-04-27 18:36:16 -04:00
|
|
|
errors = require '../commons/errors'
|
2016-08-11 18:29:53 -04:00
|
|
|
log = require 'winston'
|
2016-04-27 18:36:16 -04:00
|
|
|
wrap = require 'co-express'
|
|
|
|
database = require '../commons/database'
|
|
|
|
mongoose = require 'mongoose'
|
|
|
|
Campaign = require '../models/Campaign'
|
|
|
|
CourseInstance = require '../models/CourseInstance'
|
|
|
|
Classroom = require '../models/Classroom'
|
|
|
|
Course = require '../models/Course'
|
|
|
|
User = require '../models/User'
|
|
|
|
Level = require '../models/Level'
|
|
|
|
parse = require '../commons/parse'
|
|
|
|
|
|
|
|
module.exports =
|
2016-08-11 18:29:53 -04:00
|
|
|
|
|
|
|
fetchLevelSolutions: wrap (req, res) ->
|
|
|
|
unless req.user?.isTeacher() or req.user?.isAdmin()
|
|
|
|
log.debug "courses.fetchLevelSolutions: level solutions only for teachers, (#{req.user?.id})"
|
|
|
|
throw new errors.Forbidden()
|
|
|
|
|
|
|
|
course = yield database.getDocFromHandle(req, Course)
|
|
|
|
throw new errors.NotFound('Course not found.') unless course
|
|
|
|
|
|
|
|
campaign = yield Campaign.findById course.get('campaignID')
|
|
|
|
throw new errors.NotFound('Campaign not found.') unless campaign
|
|
|
|
|
2016-08-12 01:26:47 -04:00
|
|
|
# TODO: why does campaign.get('levels') return opposite order from direct db query?
|
|
|
|
sortedLevelIDs = _.keys campaign.get('levels')
|
|
|
|
sortedLevelIDs.reverse()
|
|
|
|
|
|
|
|
levelOriginals = (mongoose.Types.ObjectId(levelID) for levelID in sortedLevelIDs)
|
2016-08-11 18:29:53 -04:00
|
|
|
query = { original: { $in: levelOriginals }, slug: { $exists: true }}
|
2016-08-31 20:34:15 -04:00
|
|
|
select = {documentation: 1, intro: 1, name: 1, original: 1, practice: 1, slug: 1, thangs: 1, i18n: 1}
|
2016-08-11 18:29:53 -04:00
|
|
|
levels = yield Level.find(query).select(select).lean()
|
2016-08-12 01:26:47 -04:00
|
|
|
levels.sort((a, b) -> sortedLevelIDs.indexOf(a.original + '') - sortedLevelIDs.indexOf(b.original + ''))
|
2016-08-11 18:29:53 -04:00
|
|
|
res.status(200).send(levels)
|
|
|
|
|
2016-04-27 18:36:16 -04:00
|
|
|
fetchNextLevel: wrap (req, res) ->
|
|
|
|
levelOriginal = req.params.levelOriginal
|
|
|
|
if not database.isID(levelOriginal)
|
|
|
|
throw new errors.UnprocessableEntity('Invalid level original ObjectId')
|
|
|
|
|
|
|
|
course = yield database.getDocFromHandle(req, Course)
|
|
|
|
if not course
|
2016-08-11 18:29:53 -04:00
|
|
|
throw new errors.NotFound('Course not found.')
|
2016-04-27 18:36:16 -04:00
|
|
|
|
|
|
|
campaign = yield Campaign.findById course.get('campaignID')
|
|
|
|
if not campaign
|
|
|
|
throw new errors.NotFound('Campaign not found.')
|
|
|
|
|
|
|
|
levels = _.values(campaign.get('levels'))
|
|
|
|
levels = _.sortBy(levels, 'campaignIndex')
|
|
|
|
|
|
|
|
nextLevelOriginal = null
|
|
|
|
foundLevelOriginal = false
|
|
|
|
for level, index in levels
|
|
|
|
if level.original.toString() is levelOriginal
|
|
|
|
foundLevelOriginal = true
|
|
|
|
nextLevelOriginal = levels[index+1]?.original
|
|
|
|
break
|
2016-07-15 16:28:35 -04:00
|
|
|
|
2016-04-27 18:36:16 -04:00
|
|
|
if not foundLevelOriginal
|
|
|
|
throw new errors.NotFound('Level original ObjectId not found in that course')
|
2016-07-15 16:28:35 -04:00
|
|
|
|
2016-04-27 18:36:16 -04:00
|
|
|
if not nextLevelOriginal
|
2016-06-17 13:35:22 -04:00
|
|
|
return res.status(200).send({})
|
2016-07-15 16:28:35 -04:00
|
|
|
|
2016-04-27 18:36:16 -04:00
|
|
|
dbq = Level.findOne({original: mongoose.Types.ObjectId(nextLevelOriginal)})
|
2016-07-15 16:28:35 -04:00
|
|
|
|
2016-04-27 18:36:16 -04:00
|
|
|
dbq.sort({ 'version.major': -1, 'version.minor': -1 })
|
|
|
|
dbq.select(parse.getProjectFromReq(req))
|
|
|
|
level = yield dbq
|
|
|
|
level = level.toObject({req: req})
|
|
|
|
res.status(200).send(level)
|
2016-07-15 16:28:35 -04:00
|
|
|
|
|
|
|
get: (Model, options={}) -> wrap (req, res) ->
|
|
|
|
query = {}
|
2016-07-22 19:37:53 -04:00
|
|
|
if req.query.releasePhase
|
|
|
|
query.releasePhase = req.query.releasePhase
|
2016-07-15 16:28:35 -04:00
|
|
|
dbq = Model.find(query)
|
|
|
|
dbq.select(parse.getProjectFromReq(req))
|
|
|
|
results = yield database.viewSearch(dbq, req)
|
2016-07-16 03:35:52 -04:00
|
|
|
results = Course.sortCourses results
|
2016-07-15 16:28:35 -04:00
|
|
|
res.send(results)
|