diff --git a/controllers/courses.js b/controllers/courses.js index 9dbbf5acd8..f66781c828 100644 --- a/controllers/courses.js +++ b/controllers/courses.js @@ -33,6 +33,41 @@ const markSelected = (options, values = []) => options.map((option) => { return option; }); +// checks for user's 'STUDENT_LIST' permission and filters checked students +const filterStudents = (ctx, s) => ( + !ctx.locals.currentUser.permissions.includes('STUDENT_LIST') + ? s.filter(({ selected }) => selected) : s +); + +const selectedElementIdsToString = (arr = []) => { + const txt = arr.filter((obj) => obj.selected).map((obj) => (obj.id !== undefined ? obj.id : obj._id)).join(','); + return txt; +}; + +const getSyncedElementIds = ( + course, + classesAndGroups, + classAndGroupIdsOfCourse, + teachers, + substitutions, + students, + res, +) => { + const startDate = course.startDate ? timesHelper.formatDate(course.startDate, 'DD.MM.YYYY') : undefined; + const untilDate = course.untilDate ? timesHelper.formatDate(course.untilDate, 'DD.MM.YYYY') : undefined; + + const selectedElements = { + teachersSelected: selectedElementIdsToString(markSelected(teachers, course.teacherIds)), + substitutionSelected: selectedElementIdsToString(markSelected(substitutions, course.substitutionIds)), + classesAndGroupsSelected: selectedElementIdsToString(markSelected(classesAndGroups, classAndGroupIdsOfCourse)), + studentsSelected: selectedElementIdsToString(filterStudents(res, markSelected(students, course.userIds))), + startDate, + untilDate, + }; + + return selectedElements; +}; + const getDefaultRedirectUrl = (courseId) => `/rooms/${courseId}`; /** @@ -241,14 +276,18 @@ const editCourseHandler = (req, res, next) => { '#795548', ]; - // checks for user's 'STUDENT_LIST' permission and filters checked students - const filterStudents = (ctx, s) => ( - !ctx.locals.currentUser.permissions.includes('STUDENT_LIST') - ? s.filter(({ selected }) => selected) : s - ); - const classAndGroupIdsOfCourse = [...(course.classIds || []), ...(course.groupIds || [])]; + const syncedElementIds = course.syncedWithGroup ? getSyncedElementIds( + course, + classesAndGroups, + classAndGroupIdsOfCourse, + teachers, + substitutions, + students, + res, + ) : {}; + if (req.params.courseId) { if (!_scopePermissions.includes('COURSE_EDIT')) return next(new Error(res.$t('global.text.403'))); return res.render('courses/edit-course', { @@ -260,17 +299,12 @@ const editCourseHandler = (req, res, next) => { course, colors, classesAndGroups: markSelected(classesAndGroups, classAndGroupIdsOfCourse), - teachers: markSelected( - teachers, - course.teacherIds, - ), - substitutions: markSelected( - substitutions, - course.substitutionIds, - ), + teachers: markSelected(teachers, course.teacherIds), + substitutions: markSelected(substitutions, course.substitutionIds), students: filterStudents(res, markSelected(students, course.userIds)), scopePermissions: _scopePermissions, schoolData: res.locals.currentSchoolData, + ...syncedElementIds, }); } return res.render('courses/create-course', { @@ -379,12 +413,6 @@ const copyCourseHandler = (req, res, next) => { course.isArchived = false; - // checks for user's 'STUDENT_LIST' permission and filters checked students - const filterStudents = (ctx, s) => ( - !ctx.locals.currentUser.permissions.includes('STUDENT_LIST') - ? s.filter(({ selected }) => selected) : s - ); - res.render('courses/edit-course', { action, method, @@ -764,6 +792,19 @@ router.patch('/:courseId', async (req, res, next) => { req.body.substitutionIds = []; } + if (typeof req.body.teacherIds === 'string') { + req.body.teacherIds = req.body.teacherIds.split(','); + } + if (typeof req.body.substitutionIds === 'string') { + req.body.substitutionIds = req.body.substitutionIds.split(','); + } + if (typeof req.body.classIds === 'string') { + req.body.classIds = req.body.classIds.split(','); + } + if (typeof req.body.userIds === 'string') { + req.body.userIds = req.body.userIds.split(','); + } + const startDate = timesHelper.dateStringToMoment(req.body.startDate); const untilDate = timesHelper.dateStringToMoment(req.body.untilDate); diff --git a/views/courses/edit-course.hbs b/views/courses/edit-course.hbs index f99da310cf..a302813502 100644 --- a/views/courses/edit-course.hbs +++ b/views/courses/edit-course.hbs @@ -137,15 +137,17 @@
- - + + {{#if course.syncedWithGroup}} + + {{/if}} {{$t "courses.global.input.noCourseTeacher"}}
@@ -153,12 +155,15 @@
+ {{/if}}
@@ -166,7 +171,7 @@ + {{#if course.syncedWithGroup}} + + {{/if}}
+ {{#if course.syncedWithGroup}} + + {{/if}}
@@ -212,12 +223,18 @@
- {{> "lib/forms/form-date-input" label=($t "global.label.from") id="startDate" value=course.startDate disabled=@root.course.isArchived ariaLabel=($t "administration.global.label.chooseDate") }} + {{> "lib/forms/form-date-input" label=($t "global.label.from") id="startDate" value=course.startDate disabled=(isCond @root.course.isArchived '||' @root.course.syncedWithGroup) ariaLabel=($t "administration.global.label.chooseDate") }}
+ {{#if course.syncedWithGroup}} + + {{/if}}
- {{> "lib/forms/form-date-input" label=($t "global.label.to") id="untilDate" value=course.untilDate disabled=@root.course.isArchived ariaLabel=($t "administration.global.label.chooseDate") }} + {{> "lib/forms/form-date-input" label=($t "global.label.to") id="untilDate" value=course.untilDate disabled=(isCond @root.course.isArchived '||' @root.course.syncedWithGroup) ariaLabel=($t "administration.global.label.chooseDate") }}
+ {{#if course.syncedWithGroup}} + + {{/if}}