mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-03-14 07:00:01 -04:00
Add admin button to update course content for classrooms
This commit is contained in:
parent
e834b93a0a
commit
b1c69b686c
6 changed files with 80 additions and 6 deletions
|
@ -169,3 +169,8 @@ module.exports = class Classroom extends CocoModel
|
|||
options.url = @url() + '/invite-members'
|
||||
options.type = 'POST'
|
||||
@fetch(options)
|
||||
|
||||
updateCourses: (options={}) ->
|
||||
options.url = @url() + '/update-courses'
|
||||
options.type = 'POST'
|
||||
@fetch(options)
|
||||
|
|
|
@ -65,3 +65,9 @@ block modal-footer-content
|
|||
button#save-settings-btn.btn.btn-primary.btn-lg(data-i18n="courses.create_class")
|
||||
else
|
||||
button#save-settings-btn.btn.btn-primary.btn-lg(data-i18n="common.save_changes")
|
||||
|
||||
if me.isAdmin()
|
||||
hr
|
||||
// DNT
|
||||
h3 Admin Only
|
||||
button#update-courses-btn.btn.btn-forest.btn-lg Update Courses
|
||||
|
|
|
@ -10,6 +10,7 @@ module.exports = class ClassroomSettingsModal extends ModalView
|
|||
|
||||
events:
|
||||
'click #save-settings-btn': 'onSubmitForm'
|
||||
'click #update-courses-btn': 'onClickUpdateCoursesButton'
|
||||
'submit form': 'onSubmitForm'
|
||||
|
||||
initialize: (options={}) ->
|
||||
|
@ -51,3 +52,13 @@ module.exports = class ClassroomSettingsModal extends ModalView
|
|||
@listenToOnce @classroom, 'sync', @hide
|
||||
window.tracker?.trackEvent "Teachers Edit Class Saved", category: 'Teachers', classroomID: @classroom.id, ['Mixpanel']
|
||||
|
||||
onClickUpdateCoursesButton: ->
|
||||
@$('#update-courses-btn').attr('disabled', true)
|
||||
Promise.resolve(@classroom.updateCourses())
|
||||
.then =>
|
||||
@$('#update-courses-btn').attr('disabled', false)
|
||||
noty { text: 'Updated', timeout: 2000 }
|
||||
.catch (e) =>
|
||||
console.log 'e', e
|
||||
@$('#update-courses-btn').attr('disabled', false)
|
||||
noty { text: e.responseJSON?.message or e.responseText or 'Error!', type: 'error', timeout: 5000 }
|
||||
|
|
|
@ -17,6 +17,7 @@ User = require '../models/User'
|
|||
CourseInstance = require '../models/CourseInstance'
|
||||
TrialRequest = require '../models/TrialRequest'
|
||||
sendwithus = require '../sendwithus'
|
||||
co = require 'co'
|
||||
|
||||
module.exports =
|
||||
fetchByCode: wrap (req, res, next) ->
|
||||
|
@ -142,6 +143,29 @@ module.exports =
|
|||
database.assignBody(req, classroom)
|
||||
|
||||
# Copy over data from how courses are right now
|
||||
coursesData = yield module.exports.generateCoursesData(req)
|
||||
classroom.set('courses', coursesData)
|
||||
|
||||
# finish
|
||||
database.validateDoc(classroom)
|
||||
classroom = yield classroom.save()
|
||||
res.status(201).send(classroom.toObject({req: req}))
|
||||
|
||||
updateCourses: wrap (req, res) ->
|
||||
throw new errors.Unauthorized() unless req.user and not req.user.isAnonymous()
|
||||
classroom = yield database.getDocFromHandle(req, Classroom)
|
||||
if not classroom
|
||||
throw new errors.NotFound('Classroom not found.')
|
||||
unless req.user._id.equals(classroom.get('ownerID'))
|
||||
throw new errors.Forbidden('Only the owner may update their classroom content')
|
||||
|
||||
coursesData = yield module.exports.generateCoursesData(req)
|
||||
classroom.set('courses', coursesData)
|
||||
classroom = yield classroom.save()
|
||||
res.status(200).send(classroom.toObject({req: req}))
|
||||
|
||||
generateCoursesData: co.wrap (req) ->
|
||||
# helper function for generating the latest version of courses
|
||||
query = {}
|
||||
query = {adminOnly: {$ne: true}} unless req.user?.isAdmin()
|
||||
courses = yield Course.find(query)
|
||||
|
@ -160,12 +184,7 @@ module.exports =
|
|||
_.extend(levelData, _.pick(level, 'type', 'slug', 'name', 'practice', 'practiceThresholdMinutes', 'shareable'))
|
||||
courseData.levels.push(levelData)
|
||||
coursesData.push(courseData)
|
||||
classroom.set('courses', coursesData)
|
||||
|
||||
# finish
|
||||
database.validateDoc(classroom)
|
||||
classroom = yield classroom.save()
|
||||
res.status(201).send(classroom.toObject({req: req}))
|
||||
return coursesData
|
||||
|
||||
join: wrap (req, res) ->
|
||||
unless req.body?.code
|
||||
|
|
|
@ -71,6 +71,7 @@ module.exports.setup = (app) ->
|
|||
app.get('/db/classroom/:handle/members', mw.classrooms.fetchMembers) # TODO: Use mw.auth?
|
||||
app.post('/db/classroom/:classroomID/members/:memberID/reset-password', mw.classrooms.setStudentPassword)
|
||||
app.post('/db/classroom/:anything/members', mw.auth.checkLoggedIn(), mw.classrooms.join)
|
||||
app.post('/db/classroom/:handle/update-courses', mw.classrooms.updateCourses)
|
||||
app.get('/db/classroom/:handle', mw.auth.checkLoggedIn()) # TODO: Finish migrating route, adding now so 401 is returned
|
||||
app.get('/db/classroom/-/users', mw.auth.checkHasPermission(['admin']), mw.classrooms.getUsers)
|
||||
|
||||
|
|
|
@ -550,3 +550,35 @@ describe 'POST /db/classroom/:classroomID/members/:memberID/reset-password', ->
|
|||
changedStudent = yield User.findById(student.id)
|
||||
expect(changedStudent.get('passwordHash')).toEqual(student.get('passwordHash'))
|
||||
done()
|
||||
|
||||
describe 'GET /db/classroom/:handle/update-courses', ->
|
||||
|
||||
it 'updates the courses property for that classroom', utils.wrap (done) ->
|
||||
yield utils.clearModels [User, Classroom, Course, Level, Campaign]
|
||||
|
||||
admin = yield utils.initAdmin()
|
||||
teacher = yield utils.initUser({role: 'teacher'})
|
||||
|
||||
yield utils.loginUser(admin)
|
||||
yield utils.makeCourse({}, {campaign: yield utils.makeCampaign()})
|
||||
|
||||
yield utils.loginUser(teacher)
|
||||
data = { name: 'Classroom 2' }
|
||||
[res, body] = yield request.postAsync {uri: classroomsURL, json: data }
|
||||
classroom = yield Classroom.findById(res.body._id)
|
||||
expect(classroom.get('courses').length).toBe(1)
|
||||
|
||||
yield utils.loginUser(admin)
|
||||
yield utils.makeCourse({}, {campaign: yield utils.makeCampaign()})
|
||||
|
||||
classroom = yield Classroom.findById(res.body._id)
|
||||
expect(classroom.get('courses').length).toBe(1)
|
||||
|
||||
yield utils.loginUser(teacher)
|
||||
[res, body] = yield request.postAsync { uri: classroomsURL + "/#{classroom.id}/update-courses", json: true }
|
||||
expect(body.courses.length).toBe(2)
|
||||
|
||||
classroom = yield Classroom.findById(res.body._id)
|
||||
expect(classroom.get('courses').length).toBe(2)
|
||||
done()
|
||||
|
||||
|
|
Loading…
Reference in a new issue