Skip to content

Commit

Permalink
Merge pull request #5411 from dodona-edu/feat/export-user-csv
Browse files Browse the repository at this point in the history
Add export of course users
  • Loading branch information
jorg-vr authored Apr 4, 2024
2 parents 04798c3 + 0691f53 commit 0055cf1
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 33 deletions.
9 changes: 7 additions & 2 deletions app/assets/javascripts/components/search/search_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,16 @@ export class SearchActions extends DodonaElement {
}

async performAction(action: SearchAction): Promise<boolean> {
if (!action.action && !action.js) {
if (!action.action && !action.js && !action.url) {
return true;
}

if (action.url) {
const url: string = searchQueryState.addParametersToUrl(action.url);
window.open(url);
return false;
}

if (!action.action) {
eval(action.js);
return false;
Expand Down Expand Up @@ -147,7 +153,6 @@ export class SearchActions extends DodonaElement {
${this.getSearchActions().map(action => html`
<li>
<a class="action dropdown-item"
href='${action.url ? action.url : "#"}'
data-type="${action.type}"
@click=${() => this.performAction(action)}
>
Expand Down
22 changes: 12 additions & 10 deletions app/controllers/course_members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,22 @@ def index
@course_memberships = apply_scopes(@course.course_memberships.order_by_status_in_course_and_name('ASC'))
.includes(:course_labels, user: [:institution])
.where(status: statuses)
.paginate(page: parse_pagination_param(params[:page]))

@title = I18n.t('courses.index.users')
@crumbs = [[@course.name, course_path(@course)], [I18n.t('courses.index.users'), '#']]
@course_labels = CourseLabel.where(course: @course)

respond_to do |format|
format.html do
@course_memberships = @course_memberships.paginate(page: parse_pagination_param(params[:page]))
end
format.js do
@course_memberships = @course_memberships.paginate(page: parse_pagination_param(params[:page]))
end
format.csv do
headers['Content-Disposition'] = "attachment; filename=\"#{@course.name} - #{I18n.t('courses.index.users')}.csv\""
end
end
end

def show
Expand Down Expand Up @@ -70,15 +81,6 @@ def update
end
end

def download_labels_csv
csv = @course.labels_csv
send_data csv[:data],
type: 'application/csv',
filename: csv[:filename],
disposition: 'attachment',
x_sendfile: true
end

def upload_labels_csv
return render json: { message: I18n.t('course_members.upload_labels_csv.no_file') }, status: :unprocessable_entity if params[:file] == 'undefined'

Expand Down
16 changes: 0 additions & 16 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,22 +393,6 @@ def scoresheet
}
end

def labels_csv
sorted_course_memberships = course_memberships
.where.not(status: %i[unsubscribed pending])
.includes(:user)
.order(status: :asc)
.order(Arel.sql('users.permission ASC'))
.order(Arel.sql('users.last_name ASC'), Arel.sql('users.first_name ASC'))
data = CSV.generate(force_quotes: true) do |csv|
csv << %w[id username last_name first_name email labels]
sorted_course_memberships.each do |cm|
csv << [cm.user.id, cm.user.username, cm.user.last_name, cm.user.first_name, cm.user.email, cm.course_labels.map(&:name).join(';')]
end
end
{ filename: "#{name}-users-labels.csv", data: data }
end

def self.format_year(year)
year.sub(/ ?- ?/, '–')
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/annotations/question_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<div class="card-supporting-text" id="question-container">
<% actions = [] %>
<% if current_user&.a_course_admin? %>
<% actions << { text: t('questions.index.watch'), search: { refresh: true }, click: 'window.dodona.toggleIndexReload()' } %>
<% actions << { text: t('questions.index.watch'), search: { refresh: true } } %>
<% actions << { text: t('questions.index.everything'), search: { everything: true } } if @unfiltered %>
<% end %>
<%= render partial: 'layouts/searchbar', locals: { actions: actions, refresh_element: "#question-container", courses: @courses, question_states: Question.question_states.keys } %>
Expand Down
13 changes: 13 additions & 0 deletions app/views/course_members/index.csv.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= CSV.generate_line %w[id username last_name first_name email correct_exercises attempted_exercises labels], row_sep: nil %>
<% @course_memberships.each do |cm| %>
<%= CSV.generate_line([
cm.user.id,
cm.user.username,
cm.user.last_name,
cm.user.first_name,
cm.user.email,
cm.user.correct_exercises(course: @course),
cm.user.attempted_exercises(course: @course),
cm.course_labels.map(&:name).join(';')
], row_sep: nil).html_safe %>
<% end %>
7 changes: 6 additions & 1 deletion app/views/course_members/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<%= t ".first_download_labels" %>
</p>
<p>
<a class="btn btn-outline" href="<%= download_labels_csv_course_members_path(@course) %>"><%= t ".download" %>
<a class="btn btn-outline" href="<%= course_members_path(@course, format: :csv) %>"><%= t ".download" %>
</a>
</p>
</li>
Expand Down Expand Up @@ -82,6 +82,11 @@
text: t(".edit_all_labels"),
js: 'bootstrap.Modal.getOrCreateInstance(document.querySelector("#labelsUploadModal")).show()',
type: 'enrolled'
},
{
icon: 'cloud-download',
text: t(".download_user_csv"),
url: course_members_path(@course, format: :csv),
}
],
course_labels: @course_labels,
Expand Down
2 changes: 1 addition & 1 deletion app/views/submissions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<%
actions << {icon: 'replay', text: t(".reevaluate_submissions"), confirm: t(".confirm_reevaluate_submissions"), action: mass_rejudge_submissions_path(user_id: @user&.id, activity_id: @activity&.id, course_id: @course&.id, series_id: @series&.id, judge_id: @judge&.id)} if policy(Submission).mass_rejudge?
actions << {icon: 'done', text: t('.most_recent'), search: {most_recent_per_user: true}} if @activity
actions << {icon: 'file-eye', text: t('.watch_submissions'), search: {refresh: true}, click: 'window.dodona.toggleIndexReload()'}
actions << {icon: 'file-eye', text: t('.watch_submissions'), search: {refresh: true}}
%>
<% end %>
<%= render partial: 'layouts/searchbar', locals: {actions: actions, course_labels: @course_labels, statuses: Submission.statuses.keys, refresh_element: "#refresh_element"} %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/views/course_members/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ en:
close: "Close"
upload: "Upload changes"
edit_all_labels: "Edit all labels"
download_user_csv: "Download user list"
first_download_labels: "Download a list of all users and their labels as a CSV file."
then_edit: "Open the file you just downloaded and edit the labels. Only changes to the labels column will be used. Note that the labels are in one field, separated by semicolons."
finally_upload: "When you are happy with your changes, select the modified file below."
Expand Down
1 change: 1 addition & 0 deletions config/locales/views/course_members/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ nl:
close: "Sluiten"
upload: "Aanpassingen uploaden"
edit_all_labels: "Alle labels bewerken"
download_user_csv: "Gebruikerslijst downloaden"
first_download_labels: "Download een lijst van alle gebruikers en hun labels als een CSV bestand."
then_edit: "Open het tekstbestand dat je net gedownload hebt, en pas de labels aan. Enkel wijzigingen aan de labels kolom zullen doorgevoerd worden. Merk op dat de labels in één veld zitten, gescheiden met puntkomma's."
finally_upload: "Als je tevreden bent met je aanpassingen, selecteer hieronder dan het aangepaste bestand."
Expand Down
1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
resources :submissions, only: [:index]
resources :activity_read_states, only: [:index]
resources :members, only: %i[index show edit update], controller: :course_members do
get 'download_labels_csv', on: :collection
post 'upload_labels_csv', on: :collection
end
member do
Expand Down
11 changes: 10 additions & 1 deletion test/javascript/components/search/search_actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,16 @@ describe("SearchActions", () => {
});

test("clicking a link action should navigate to the url", async () => {
expect(screen.getByText("link-test").closest("a").href).toBe("https://test.dodona.be/");
jest.spyOn(window, "open").mockImplementation(() => window);
await userEvent.click(screen.queryByText("link-test"));
expect(window.open).toHaveBeenCalledWith("https://test.dodona.be");
});

test("clicking a link action should add query params to the url", async () => {
jest.spyOn(window, "open").mockImplementation(() => window);
searchQueryState.queryParams.set("foo", "bar");
await userEvent.click(screen.queryByText("link-test"));
expect(window.open).toHaveBeenCalledWith("https://test.dodona.be/?foo=bar");
});

test("clicking a confirm action should show a confirmation dialog", async () => {
Expand Down

0 comments on commit 0055cf1

Please sign in to comment.