Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N21-939 assign groups to course #3326

Merged
merged 19 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/default.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,11 @@
"type": "boolean",
"default": false,
"description": "Enables the new class list view"
},
"FEATURE_GROUPS_IN_COURSE_ENABLED": {
MBergCap marked this conversation as resolved.
Show resolved Hide resolved
"type": "boolean",
"default": false,
"description": "Enables to get groups of type class in courses"
MBergCap marked this conversation as resolved.
Show resolved Hide resolved
}
},
"allOf": [
Expand Down
2 changes: 2 additions & 0 deletions config/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const {
FEATURE_ALERTS_ON_HOMEPAGE_ENABLED,
FEATURE_BUTTONS_ON_LOGINPAGE_ENABLED,
FEATURE_SHOW_NEW_CLASS_VIEW_ENABLED,
FEATURE_GROUPS_IN_COURSE_ENABLED,
} = process.env;

const exp = {
Expand Down Expand Up @@ -82,6 +83,7 @@ const exp = {
FEATURE_ALERTS_ON_HOMEPAGE_ENABLED,
FEATURE_BUTTONS_ON_LOGINPAGE_ENABLED,
FEATURE_SHOW_NEW_CLASS_VIEW_ENABLED,
FEATURE_GROUPS_IN_COURSE_ENABLED,
};

// eslint-disable-next-line no-console
Expand Down
78 changes: 60 additions & 18 deletions controllers/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const { logger, formatError } = require('../helpers');
const timesHelper = require('../helpers/timesHelper');

const OPTIONAL_COURSE_FEATURES = ['messenger', 'videoconference'];
const FEATURE_GROUPS_IN_COURSE_ENABLED = Configuration.get('FEATURE_GROUPS_IN_COURSE_ENABLED');

const router = express.Router();
const { HOST } = require('../config/global');
Expand All @@ -27,7 +28,8 @@ const getSelectOptions = (req, service, query) => api(req).get(`/${service}`, {
}).then((data) => data.data);

const markSelected = (options, values = []) => options.map((option) => {
option.selected = values.includes(option._id);
const optionId = option.id !== undefined ? option.id : option._id;
option.selected = values.includes(optionId);
return option;
});

Expand Down Expand Up @@ -106,6 +108,8 @@ const deleteEventsForCourse = (req, res, courseId) => {
return Promise.resolve(true);
};

let groupIds = [];
let classIds = [];
const editCourseHandler = (req, res, next) => {
let coursePromise;
let action;
Expand All @@ -124,16 +128,23 @@ const editCourseHandler = (req, res, next) => {
action += `?redirectUrl=${req.query.redirectUrl}`;
}

const classesPromise = api(req)
.get('/classes', {
qs: {
schoolId: res.locals.currentSchool,
$populate: ['year'],
$limit: -1,
$sort: { year: -1, displayName: 1 },
},
});
// .then(data => data.data); needed when pagination is not disabled
let classesAndGroupsPromise;
arnegns marked this conversation as resolved.
Show resolved Hide resolved
let classesPromise;
if (FEATURE_GROUPS_IN_COURSE_ENABLED) {
arnegns marked this conversation as resolved.
Show resolved Hide resolved
classesAndGroupsPromise = api(req, { version: 'v3' })
.get('/groups/class');
} else {
classesPromise = api(req)
.get('/classes', {
qs: {
schoolId: res.locals.currentSchool,
$populate: ['year'],
$limit: -1,
$sort: { year: -1, displayName: 1 },
},
});
}

const teachersPromise = getSelectOptions(req, 'users', {
roles: ['teacher'],
$limit: false,
Expand All @@ -153,15 +164,23 @@ const editCourseHandler = (req, res, next) => {

Promise.all([
coursePromise,
classesPromise,
FEATURE_GROUPS_IN_COURSE_ENABLED ? classesAndGroupsPromise : classesPromise,
MBergCap marked this conversation as resolved.
Show resolved Hide resolved
teachersPromise,
studentsPromise,
scopePermissions,
]).then(([course, _classes, _teachers, _students, _scopePermissions]) => {
]).then(([course, _classesAndGroups, _teachers, _students, _scopePermissions]) => {
// these 3 might not change anything because hooks allow just ownSchool results by now, but to be sure:
const classes = _classes.filter(
(c) => c.schoolId === res.locals.currentSchool,
).sort();
let classesAndGroups = [];
if (FEATURE_GROUPS_IN_COURSE_ENABLED) {
classesAndGroups = _classesAndGroups.data;
groupIds = _classesAndGroups.data.filter((g) => g.type === 'group').map((group) => group.id);
classIds = _classesAndGroups.data.filter((c) => c.type === 'class').map((clazz) => clazz.id);
} else {
classesAndGroups = _classesAndGroups.filter(
(c) => c.schoolId === res.locals.currentSchool,
).sort();
}

const teachers = _teachers.filter(
(t) => t.schoolId === res.locals.currentSchool,
);
Expand Down Expand Up @@ -233,6 +252,8 @@ const editCourseHandler = (req, res, next) => {
? s.filter(({ selected }) => selected) : s
);

const classAndGroupIds = [...(course.classIds || []), ...(course.groupIds || [])];

if (req.params.courseId) {
if (!_scopePermissions.includes('COURSE_EDIT')) return next(new Error(res.$t('global.text.403')));
return res.render('courses/edit-course', {
Expand All @@ -243,7 +264,7 @@ const editCourseHandler = (req, res, next) => {
closeLabel: res.$t('global.button.cancel'),
course,
colors,
classes: markSelected(classes, course.classIds),
classesAndGroups: markSelected(classesAndGroups, classAndGroupIds),
teachers: markSelected(
teachers,
course.teacherIds,
Expand All @@ -265,7 +286,7 @@ const editCourseHandler = (req, res, next) => {
closeLabel: res.$t('global.button.cancel'),
course,
colors,
classes: markSelected(classes, course.classIds),
classesAndGroups: markSelected(classesAndGroups, classAndGroupIds),
teachers: markSelected(
teachers,
course.teacherIds,
Expand Down Expand Up @@ -510,6 +531,17 @@ router.post('/', (req, res, next) => {
delete req.body[feature];
});

req.body.groupIds = [];
if (FEATURE_GROUPS_IN_COURSE_ENABLED) {
req.body.groupIds = (req.body.classIds ?? [])
.filter((id) => groupIds.includes(id));

if (groupIds.length > 0) {
req.body.classIds = (req.body.classIds ?? [])
.filter((id) => classIds.includes(id));
}
}

api(req)
.post('/courses/', {
json: req.body, // TODO: sanitize
Expand Down Expand Up @@ -738,13 +770,23 @@ router.patch('/:courseId', async (req, res, next) => {
if (!req.body.classIds) {
req.body.classIds = [];
}
if (!req.body.groupIds) {
req.body.groupIds = [];
}
if (!req.body.userIds) {
req.body.userIds = [];
}
if (!req.body.substitutionIds) {
req.body.substitutionIds = [];
}

if (FEATURE_GROUPS_IN_COURSE_ENABLED) {
req.body.groupIds = req.body.classIds
.filter((id) => groupIds.includes(id));
req.body.classIds = req.body.classIds
.filter((id) => classIds.includes(id));
}

const startDate = timesHelper.dateStringToMoment(req.body.startDate);
const untilDate = timesHelper.dateStringToMoment(req.body.untilDate);

Expand Down
31 changes: 24 additions & 7 deletions views/courses/create-course.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,30 @@
<label for="addClassesToCourse">{{$t "courses.add.label.addClassesToCourse"}}</label>
<select id="addClassesToCourse" name="classIds[]" multiple data-placeholder="{{$t "courses.global.input.selectClasses"}}" data-testid="classes">
<option disabled value="{{$t "administration.global.placeholder.selectClasses"}}">{{$t "administration.global.placeholder.selectClasses"}}</option>
{{#each ../classes}}
<option value="{{this._id}}" {{#if this.selected}}selected{{/if}}>
{{this.displayName}}
{{#if this.year}}
({{this.year.name}})
{{/if}}
</option>
{{#each ../classesAndGroups}}
<option value="{{this.id}}{{#unless this.id}}{{this._id}}{{/unless}}" {{#if this.selected}}selected{{/if}}>
{{#if (getConfig "FEATURE_GROUPS_IN_COURSE_ENABLED")}}
{{this.name}}

{{#if this.schoolYear}}
({{this.schoolYear}})
{{/if}}

{{#if this.externalSourceName}}
| {{this.externalSourceName}}
{{/if}}
{{else}}
{{#if this.displayName}}
{{this.displayName}}
{{else}}
{{this.name}}
{{/if}}

{{#if this.year}}
({{this.year.name}})
{{/if}}
{{/if}}
</option>
{{/each}}
</select>
</div>
Expand Down
31 changes: 21 additions & 10 deletions views/courses/edit-course.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,29 @@

<select id="classId" name="classIds[]" multiple data-placeholder="{{$t "courses.global.input.selectClasses"}}"
{{#if course.isArchived}}disabled{{/if}}>
{{#each classes}}
<option value="{{this._id}}" {{#if this.selected}}selected{{/if}}>
{{#each classesAndGroups}}
<option value="{{this.id}}{{#unless this.id}}{{this._id}}{{/unless}}" {{#if this.selected}}selected{{/if}}>
{{#if (getConfig "FEATURE_GROUPS_IN_COURSE_ENABLED")}}
{{this.name}}

{{#if this.name}}
{{this.displayName}}
{{else}}
{{this.name}}
{{/if}}
{{#if this.schoolYear}}
({{this.schoolYear}})
{{/if}}

{{#if this.year}}
({{this.year.name}})
{{/if}}
{{#if this.externalSourceName}}
| {{this.externalSourceName}}
{{/if}}
{{else}}
{{#if this.displayName}}
{{this.displayName}}
{{else}}
{{this.name}}
{{/if}}

{{#if this.year}}
({{this.year.name}})
{{/if}}
{{/if}}
</option>
{{/each}}
</select>
Expand Down
Loading