Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

survey.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /** @module survey/survey */
  2. import { _baseRecord } from '../index.js'
  3. import { surveySchema } from './survey.schema.js'
  4. import { answersSchema } from './survey.answer.schema.js'
  5. import { aspectsArr } from '../../utils/lang.js'
  6. const SCORED = aspectsArr
  7. const _isScored = id => SCORED.includes(id)
  8. const _makeCategoryFriendly = responseCategory => {
  9. const labels = responseCategory.split('_vs_')
  10. labels.forEach((a, i) => {
  11. if (a.indexOf('_') == -1) return
  12. labels[i] = a.split('_').join(' ')
  13. })
  14. return labels
  15. }
  16. const _formatAspectQuestions = steps => {
  17. return steps
  18. .map(q => {
  19. if (!_isScored(q.response_key_id)) return null
  20. return {
  21. id: q.response_key_id,
  22. question: q.response_key_prompt,
  23. labels: _makeCategoryFriendly(q.response_key_category),
  24. answer: null,
  25. }
  26. })
  27. .filter(step => step != null)
  28. }
  29. class Survey extends _baseRecord {
  30. constructor(questionSteps) {
  31. super()
  32. this.type = this.constructor.name.toLowerCase()
  33. /** Fields */
  34. this.steps = [...questionSteps] // ! required
  35. this.aspectQuestions = _formatAspectQuestions(this.steps)
  36. console.log('this.aspectQuestions: ', JSON.stringify(this.aspectQuestions))
  37. }
  38. validateAnswer(answer) {
  39. const validate = answersSchema.validate(answer)
  40. if (validate.error) {
  41. console.error(`error: ${validate.error}`)
  42. }
  43. return !validate.error ? true: false
  44. }
  45. isValid() {
  46. const validate = surveySchema.validate(this)
  47. /**
  48. * Log out some useful error messages
  49. */
  50. if (validate.error) {
  51. console.error(`error: ${validate.error} - ${this.type} validation`)
  52. }
  53. /** validate(this) always returns something so force it to a bool */
  54. return !validate.error ? true : false
  55. }
  56. }
  57. export { Survey }