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

Add users filtering while exporting #362

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
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);
})
Comment on lines +181 to +204
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Filtering logic can be simplified.
  • Some variable names are misleading, like requiredIDs.
  • Fix indentation.
  • Maybe consider using Array.filter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I will look into it. Thank you for the feedback!

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}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making these parameters part of the URL, it'll be better group them in a query.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that makes more sense.

success: function (res) {
if (res && res.success) {
var headers = {
Expand Down