Adapt authoring tool UI documentation

v1.0.0-rc.4

adapt-authoring-spoortracking/lib/SpoorTrackingModule.js

  1. import { AbstractModule } from 'adapt-authoring-core'
  2. /**
  3. * Module for making course content compatible with spoor
  4. * @memberof spoortracking
  5. * @extends {AbstractModule}
  6. */
  7. class SpoorTrackingModule extends AbstractModule {
  8. /** @override */
  9. async init () {
  10. const [auth, content, server] = await this.app.waitForModule('auth', 'content', 'server')
  11. content.preInsertHook.tap(this.insertTrackingId.bind(this))
  12. server.api.createChildRouter('spoortracking').addRoute({
  13. route: '/reset/:_courseId',
  14. handlers: { post: this.resetTrackingHandler.bind(this) },
  15. meta: {
  16. post: {
  17. summary: 'Reset course tracking IDs',
  18. description: 'Fully resets all tracking IDs for a single course.',
  19. responses: { 204: {} }
  20. }
  21. }
  22. })
  23. auth.secureRoute('/api/spoortracking/reset/:_courseId', 'post', ['write:content'])
  24. }
  25. /**
  26. * Adds the latest tracking ID to a block
  27. * @param {Object} data The block data to update
  28. */
  29. async insertTrackingId (data) {
  30. if (data._type !== 'block' || Number.isInteger(data._trackingId)) {
  31. return
  32. }
  33. const content = await this.app.waitForModule('content')
  34. const [{ _trackingId }] = await content.find({ _courseId: data._courseId }, {}, { limit: 1, sort: [['_trackingId', -1]] })
  35. data._trackingId = (_trackingId ?? 0) + 1
  36. }
  37. /**
  38. * Resets all tracking IDs for a single course
  39. * @param {String} _courseId The course _id
  40. * @return {Promise}
  41. */
  42. async resetCourseTrackingIds (_courseId) {
  43. const content = await this.app.waitForModule('content')
  44. const blocks = await content.find({ _type: 'block', _courseId }, {}, { sort: [['_trackingId', 1]] })
  45. await Promise.all(blocks.map((b, i) => content.update({ _id: b._id }, { _trackingId: i + 1 }, { schemaName: 'block' })))
  46. this.log('debug', 'RESET', _courseId)
  47. }
  48. /**
  49. * Express handler for reseting all tracking IDs for a single course
  50. * @param {external:ExpressRequest} req
  51. * @param {external:ExpressResponse} res
  52. * @param {Function} next
  53. */
  54. async resetTrackingHandler (req, res, next) {
  55. try {
  56. await this.resetCourseTrackingIds(req.params._courseId)
  57. res.sendStatus(204)
  58. } catch (e) {
  59. return next(e)
  60. }
  61. }
  62. }
  63. export default SpoorTrackingModule