diff --git a/dashboard/controller/users/exportCSV.js b/dashboard/controller/users/exportCSV.js index 7e2350c5..91d79b78 100644 --- a/dashboard/controller/users/exportCSV.js +++ b/dashboard/controller/users/exportCSV.js @@ -7,6 +7,11 @@ module.exports = function exportCSV(req, res) { var users = []; var Classrooms = {}; + common.reinitLocale(req); + var selectedUsername = req.params.username; + var selectedRole = req.params.role; + var selectedClassroom = req.params.classroom; + function validateUser(user) { var validUser = { _id: "", @@ -173,10 +178,34 @@ module.exports = function exportCSV(req, res) { }); } ], function() { - if (users.length == 0) { + var filteredUsers = []; + var requiredIDs = new Set(); + if(selectedClassroom !== 'undefined'){ + selectedClassroom.split(',').forEach(id => { + requiredIDs.add(id); + }); + } + users.forEach(user => { + let required = true; + if (selectedUsername !== 'undefined') { + if (user.name !== selectedUsername) { + required = false; + } + } + if (required && selectedRole !== 'all' && user.type !== selectedRole) { + required = false; + } + if (required && selectedRole === 'student' && selectedClassroom !== 'undefined') { + if (!requiredIDs.has(user._id)) { + required = false; + } + } + if(required) filteredUsers.push(user); + }) + if (filteredUsers.length == 0) { res.json({success: false, msg: common.l10n.get('NoUsersFound')}); } else { - res.json({success: true, msg: common.l10n.get('ExportSuccess'), data: users}); + res.json({success: true, msg: common.l10n.get('ExportSuccess'), data: filteredUsers}); } return; }); diff --git a/dashboard/route.js b/dashboard/route.js index 52ce63e9..5dd8a261 100644 --- a/dashboard/route.js +++ b/dashboard/route.js @@ -40,6 +40,7 @@ module.exports = function (app, ini) { app.post('/dashboard/users/add', authController.validateSession, usersController.addUser); app.post('/dashboard/users/import', upload.single('file'), usersController.importCSV); app.get('/dashboard/users/export', authController.validateSession, usersController.exportCSV); + app.get('/dashboard/users/export/:role/:username/:classroom', authController.validateSession, usersController.exportCSV); app.get('/dashboard/users/edit/:uid', authController.validateSession, usersController.editUser); app.post('/dashboard/users/edit/:uid', authController.validateSession, usersController.editUser); app.get('/dashboard/users/delete/:uid', authController.validateSession, usersController.deleteUser); diff --git a/dashboard/views/users.ejs b/dashboard/views/users.ejs index 77ea5044..42bb36ff 100644 --- a/dashboard/views/users.ejs +++ b/dashboard/views/users.ejs @@ -23,6 +23,7 @@ <% if (account.user && account.user.role=="admin") { %> + <% } %> @@ -71,11 +72,16 @@ delete_forever
Export Users
-