From 0b1be05ea6c1ff2ec5f99c7e0999df3e9c561938 Mon Sep 17 00:00:00 2001 From: jorg-vr Date: Fri, 21 Jun 2024 11:06:28 +0200 Subject: [PATCH 1/4] Allow exporting score items --- app/controllers/score_items_controller.rb | 7 +++++++ app/policies/evaluation_exercise_policy.rb | 12 ++++++++++++ app/views/score_items/index.csv.erb | 9 +++++++++ config/routes.rb | 4 ++++ 4 files changed, 32 insertions(+) create mode 100644 app/views/score_items/index.csv.erb diff --git a/app/controllers/score_items_controller.rb b/app/controllers/score_items_controller.rb index 0b67381489..9d91b46481 100644 --- a/app/controllers/score_items_controller.rb +++ b/app/controllers/score_items_controller.rb @@ -22,6 +22,13 @@ def copy end end + def index + @evaluation_exercise = EvaluationExercise.find(params[:evaluation_exercise_id]) + authorize @evaluation_exercise, :show? + + @score_items = policy_scope(@evaluation_exercise.score_items) + end + def create @score_item = ScoreItem.new(permitted_attributes(ScoreItem)) @score_item.last_updated_by = current_user diff --git a/app/policies/evaluation_exercise_policy.rb b/app/policies/evaluation_exercise_policy.rb index 43d72dcac2..5ada94a2f5 100644 --- a/app/policies/evaluation_exercise_policy.rb +++ b/app/policies/evaluation_exercise_policy.rb @@ -6,6 +6,14 @@ def show_total? record.visible_score? && record.evaluation.released? end + def show? + return true if course_admin? + + return false unless evaluation_member? + + record&.visible_score? && record&.evaluation&.released? + end + def permitted_attributes %i[visible_score] end @@ -16,4 +24,8 @@ def course_admin? course = record&.evaluation&.series&.course user&.course_admin?(course) end + + def evaluation_member? + record&.evaluation&.users&.include?(user) + end end diff --git a/app/views/score_items/index.csv.erb b/app/views/score_items/index.csv.erb new file mode 100644 index 0000000000..ab39c2ad21 --- /dev/null +++ b/app/views/score_items/index.csv.erb @@ -0,0 +1,9 @@ +<%= CSV.generate_line %w[name maximum description visible], row_sep: nil %> +<% @score_items.each do |cm| %> +<%= CSV.generate_line([ + cm.name, + cm.maximum, + cm.description, + cm.visible +], row_sep: nil).html_safe %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 13a9d5269b..5707a0f632 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -228,6 +228,10 @@ post 'add_all', on: :collection end resources :scores, only: %i[show create update destroy] + + resources :evaluation_exercise do + resources :score_items, only: %i[index] + end end resources :feedbacks, only: %i[show edit update] do delete 'scores', action: :destroy_scores, controller: :feedbacks, on: :member From 4bced1d208d97d73df9f52bfe3a5cb29221bc69f Mon Sep 17 00:00:00 2001 From: jorg-vr Date: Fri, 21 Jun 2024 14:36:34 +0200 Subject: [PATCH 2/4] Fix file upload --- app/controllers/score_items_controller.rb | 39 ++++++++++++++++++++++ app/policies/evaluation_exercise_policy.rb | 4 +++ app/views/score_items/_exercise.html.erb | 31 +++++++++++++++-- app/views/score_items/_upload.html.erb | 25 ++++++++++++++ config/locales/views/score_items/en.yml | 6 ++++ config/locales/views/score_items/nl.yml | 6 ++++ config/routes.rb | 4 ++- 7 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 app/views/score_items/_upload.html.erb diff --git a/app/controllers/score_items_controller.rb b/app/controllers/score_items_controller.rb index 9d91b46481..465686ee57 100644 --- a/app/controllers/score_items_controller.rb +++ b/app/controllers/score_items_controller.rb @@ -22,6 +22,45 @@ def copy end end + def upload + return render json: { message: I18n.t('course_members.upload_labels_csv.no_file') }, status: :unprocessable_entity if params[:upload].nil? || params[:upload][:file] == 'undefined' || params[:upload][:file].nil? + + file = params[:upload][:file] + + @evaluation_exercise = EvaluationExercise.find(params[:evaluation_exercise_id]) + authorize @evaluation_exercise, :update? + + begin + headers = CSV.foreach(file.path).first + %w[name maximum].each do |column| + return render json: { message: I18n.t('course_members.upload_labels_csv.missing_column', column: column) }, status: :unprocessable_entity unless headers&.include?(column) + end + + # Remove existing score items. + @evaluation_exercise.score_items.destroy_all + + CSV.foreach(file.path, headers: true) do |row| + row = row.to_hash + score_item = ScoreItem.new( + name: row['name'], + maximum: row['maximum'], + visible: row.key?('visible') ? row['visible'] : true, + description: row.key?('description') ? row['description'] : nil, + evaluation_exercise: @evaluation_exercise + ) + score_item.save! + end + rescue CSV::MalformedCSVError + return render json: { message: I18n.t('course_members.upload_labels_csv.malformed') }, status: :unprocessable_entity + end + + + respond_to do |format| + format.js { render 'score_items/index', locals: { new: nil, evaluation_exercise: @evaluation_exercise.reload } } + format.json { head :no_content } + end + end + def index @evaluation_exercise = EvaluationExercise.find(params[:evaluation_exercise_id]) authorize @evaluation_exercise, :show? diff --git a/app/policies/evaluation_exercise_policy.rb b/app/policies/evaluation_exercise_policy.rb index 5ada94a2f5..1c1d58addb 100644 --- a/app/policies/evaluation_exercise_policy.rb +++ b/app/policies/evaluation_exercise_policy.rb @@ -14,6 +14,10 @@ def show? record&.visible_score? && record&.evaluation&.released? end + def update? + course_admin? + end + def permitted_attributes %i[visible_score] end diff --git a/app/views/score_items/_exercise.html.erb b/app/views/score_items/_exercise.html.erb index 4d88cf0b35..6e7000d8b4 100644 --- a/app/views/score_items/_exercise.html.erb +++ b/app/views/score_items/_exercise.html.erb @@ -2,8 +2,30 @@ <%= javascript_include_tag 'score_item' %> <% end %> <% maximum_score = evaluation_exercise.maximum_score %> -
-

<%= evaluation_exercise.exercise.name %>

+
+
+

<%= evaluation_exercise.exercise.name %>

+
+ +
@@ -104,6 +126,11 @@ <%= render 'score_items/copy', evaluation_exercise: evaluation_exercise, evaluation: @evaluation %> +