Skip to content

Commit

Permalink
Add users filtering while exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank190801 committed Feb 17, 2023
1 parent 140f95a commit c5f3b20
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
33 changes: 31 additions & 2 deletions dashboard/controller/users/exportCSV.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand Down Expand Up @@ -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;
});
Expand Down
1 change: 1 addition & 0 deletions dashboard/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions dashboard/views/users.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<% if (account.user && account.user.role=="admin") { %>
<option data-l10n-id="teacher" value="teacher" <% if(query.role=='teacher'){ %>selected="selected"<% } %> >Teacher</option>
<option data-l10n-id="admin" value="admin" <% if(query.role=='admin'){ %>selected="selected"<% } %> >Admin</option>
<option data-l10n-id="all" value="all" <% if(query.role=='all'){ %>selected="selected"<% } %> >All</option>
<% } %>
</select>
<script>$('#user-form [name="role"]').select2().on("change", () => show_class_fields())</script>
Expand Down Expand Up @@ -71,11 +72,16 @@
<a href="#" onclick="deleteMultipleUsers(); return false;" class="btn btn-round" data-l10n-id="multidelete" title="Delete Multiple"><i class="material-icons text-muted">delete_forever</i></a>
</div>
<div class="btn pull-right btn-round" onclick="downloadUsers()" data-l10n-id="ExportUsers" id="users-exportusers">Export Users</div>
<script>
<script>
var roleValue = document.getElementById('user-type-select2').value;
var usernameValue = document.getElementById('username').value;
var classroomValue = document.getElementById('classroom_select').value;
classroomValue = classroomValue == '' ? 'undefined' : classroomValue;
usernameValue = usernameValue == '' ? 'undefined' : usernameValue;
function downloadUsers(event) {
$.ajax({
type: "GET",
url: "/dashboard/users/export",
url: `/dashboard/users/export/${roleValue}/${usernameValue}/${classroomValue}`,
success: function (res) {
if (res && res.success) {
var headers = {
Expand Down

0 comments on commit c5f3b20

Please sign in to comment.