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 export of course users #5411

Merged
merged 7 commits into from
Apr 4, 2024
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
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 @@
@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]))

Check warning on line 43 in app/controllers/course_members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/course_members_controller.rb#L43

Added line #L43 was not covered by tests
end
format.js do
@course_memberships = @course_memberships.paginate(page: parse_pagination_param(params[:page]))
end
format.csv do
Copy link
Member

Choose a reason for hiding this comment

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

The x_sendfile setting isn't present here (and was in the old code). Is this something that isn't needed anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"X-Sendfile is a special header option that tells the web server to ignore the content of the response and replace it by the file that is specified in the X-Sendfile header."

This is not relevant in this case, as the response is now the actual csv

headers['Content-Disposition'] = "attachment; filename=\"#{@course.name} - #{I18n.t('courses.index.users')}.csv\""

Check warning on line 49 in app/controllers/course_members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/course_members_controller.rb#L49

Added line #L49 was not covered by tests
end
end
end

def show
Expand Down Expand Up @@ -70,15 +81,6 @@
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 @@ -25,6 +25,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 @@ -25,6 +25,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